Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -350,16 +350,16 @@ public List<BrAPIGermplasm> getRawGermplasmByAccessionNumber(ArrayList<String> g
}
return resultGermplasm;
}

public List<BrAPIGermplasm> getGermplasmByDisplayName(List<String> germplasmDisplayNames, UUID programId) throws ApiException {
List<BrAPIGermplasm> allGermplasm = getGermplasm(programId);
HashSet<String> requestedNames = new HashSet<>(germplasmDisplayNames);
List<BrAPIGermplasm> matchingGermplasm = new ArrayList<>();
for (BrAPIGermplasm germplasm: allGermplasm) {
if (requestedNames.contains(germplasm.getDefaultDisplayName())) {
matchingGermplasm.add(germplasm);
}

public List<BrAPIGermplasm> getGermplasmByGID(List<String> germplasmGIDs, UUID programId) throws ApiException {
List<BrAPIGermplasm> allGermplasm = getGermplasm(programId);
HashSet<String> requestedGIDs = new HashSet<>(germplasmGIDs);
List<BrAPIGermplasm> matchingGermplasm = new ArrayList<>();
for (BrAPIGermplasm germplasm: allGermplasm) {
if (requestedGIDs.contains(germplasm.getAccessionNumber())) {
matchingGermplasm.add(germplasm);
}
return matchingGermplasm;
}
return matchingGermplasm;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.breedinginsight.brapps.importer.services.processors.germplasm;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import io.micronaut.context.annotation.Property;
import io.micronaut.context.annotation.Prototype;
import io.micronaut.http.HttpStatus;
Expand Down Expand Up @@ -68,15 +67,16 @@ public class GermplasmProcessor implements Processor {
@Property(name = "brapi.server.reference-source")
private String BRAPI_REFERENCE_SOURCE;

private BrAPIGermplasmService brAPIGermplasmService;
private final BrAPIGermplasmService brAPIGermplasmService;
private final BreedingMethodDAO breedingMethodDAO;
private final BrAPIListDAO brAPIListDAO;
private final DSLContext dsl;
private final BrAPIGermplasmDAO brAPIGermplasmDAO;
private final Gson gson = new Gson();

Map<String, PendingImportObject<BrAPIGermplasm>> germplasmByAccessionNumber = new HashMap<>();
Map<String, Integer> fileGermplasmByName = new HashMap<>();
Map<String, Integer> fileGermplasmByGID = new HashMap<>();

Map<String, BrAPIGermplasm> dbGermplasmByName = new HashMap<>();
Map<String, BrAPIGermplasm> dbGermplasmByAccessionNo = new HashMap<>();
Map<String, Integer> germplasmIndexByEntryNo = new HashMap<>();
Expand Down Expand Up @@ -110,7 +110,6 @@ public class GermplasmProcessor implements Processor {

@Inject
public GermplasmProcessor(BrAPIGermplasmService brAPIGermplasmService, DSLContext dsl, BreedingMethodDAO breedingMethodDAO, BrAPIListDAO brAPIListDAO, BrAPIGermplasmDAO brAPIGermplasmDAO) {
this.brAPIGermplasmService = brAPIGermplasmService;
this.dsl = dsl;
this.breedingMethodDAO = breedingMethodDAO;
this.brAPIGermplasmDAO = brAPIGermplasmDAO;
Expand Down Expand Up @@ -146,65 +145,63 @@ public void getExistingBrapiData(List<BrAPIImport> importRows, Program program)
germplasmIndexByEntryNo.put(germplasm.getEntryNo(), i);
}

Integer count = fileGermplasmByName.getOrDefault(germplasm.getGermplasmName(), 0);
fileGermplasmByName.put(germplasm.getGermplasmName(), count+1);
Integer count = fileGermplasmByGID.getOrDefault(germplasm.getAccessionNumber(), 0);
fileGermplasmByGID.put(germplasm.getAccessionNumber(), count+1);
}
}

// Get existing germplasm names
List<BrAPIGermplasm> dbGermplasm = brAPIGermplasmService.getGermplasmByDisplayName(new ArrayList<>(fileGermplasmByName.keySet()), program.getId());
List<BrAPIGermplasm> dbGermplasm = brAPIGermplasmService.getGermplasmByGID(new ArrayList<>(fileGermplasmByGID.keySet()), program.getId());
dbGermplasm.forEach(germplasm -> {
dbGermplasmByName.put(germplasm.getDefaultDisplayName(), germplasm);
dbGermplasmByAccessionNo.put(germplasm.getAccessionNumber(), germplasm);
});

// Get parental accession nos in file
for (int i = 0; i < importRows.size(); i++) {
BrAPIImport germplasmImport = importRows.get(i);
for (BrAPIImport germplasmImport : importRows) {
Germplasm germplasm = germplasmImport.getGermplasm();
if (germplasm != null) {
// Retrieve parent accession numbers to assess if already in db
if (germplasm.getFemaleParentAccessionNumber() != null) {
germplasmAccessionNumbers.put(germplasm.getFemaleParentAccessionNumber(), true);
}
if (germplasm.getMaleParentAccessionNumber() != null) {
germplasmAccessionNumbers.put(germplasm.getMaleParentAccessionNumber(), true);
}
if (germplasm.getFemaleParentAccessionNumber() != null) {
germplasmAccessionNumbers.put(germplasm.getFemaleParentAccessionNumber(), true);
}
if (germplasm.getMaleParentAccessionNumber() != null) {
germplasmAccessionNumbers.put(germplasm.getMaleParentAccessionNumber(), true);
}
}
}

// If a parental accession number is present, it should exist in the database.
existingGermplasm = new ArrayList<>();
List<String> missingParentalAccessionNumbers = germplasmAccessionNumbers.entrySet().stream().filter(Map.Entry::getValue).map(Map.Entry::getKey).collect(Collectors.toList());
List<String> missingAccessionNumbers = germplasmAccessionNumbers.entrySet().stream().filter(entry -> !entry.getValue()).map(Map.Entry::getKey).collect(Collectors.toList());
if (germplasmAccessionNumbers.size() > 0) {
if (!germplasmAccessionNumbers.isEmpty()) {
try {
existingGermplasm = brAPIGermplasmService.getRawGermplasmByAccessionNumber(new ArrayList<>(germplasmAccessionNumbers.keySet()), program.getId());
List<String> existingAccessionNumbers = existingGermplasm.stream()
.map(germplasm -> germplasm.getAccessionNumber())
.map(BrAPIGermplasm::getAccessionNumber)
.collect(Collectors.toList());
missingParentalAccessionNumbers.removeAll(existingAccessionNumbers);
missingAccessionNumbers.removeAll(existingAccessionNumbers);

existingGermplasm.forEach(existingGermplasm -> {
germplasmByAccessionNumber.put(existingGermplasm.getAccessionNumber(), new PendingImportObject<>(ImportObjectState.EXISTING, existingGermplasm));
});
existingGermplasm.forEach(existingGermplasm -> germplasmByAccessionNumber.put(existingGermplasm.getAccessionNumber(), new PendingImportObject<>(ImportObjectState.EXISTING, existingGermplasm)));
} catch (ApiException e) {
// We shouldn't get an error back from our services. If we do, nothing the user can do about it
throw new InternalServerException(e.toString(), e);
}
}

// Check for existing germplasm lists
Boolean listNameDup = false;
if (importRows.size() > 0 && importRows.get(0).getGermplasm().getListName() != null) {
boolean listNameDup = false;
if (!importRows.isEmpty() && importRows.get(0).getGermplasm().getListName() != null) {
try {
Germplasm row = importRows.get(0).getGermplasm();
String listName = Germplasm.constructGermplasmListName(row.getListName(), program);
List<BrAPIListSummary> existingLists = brAPIListDAO.getListsByName(List.of(listName), program.getId());
for (BrAPIListSummary existingList: existingLists) {
if (existingList.getListName().equals(listName)) {
listNameDup = true;
break;
}
}
} catch (ApiException e) {
Expand All @@ -217,14 +214,14 @@ public void getExistingBrapiData(List<BrAPIImport> importRows, Program program)
missingAccessionNumbers.remove("0");

// Parent reference checks
if (missingParentalAccessionNumbers.size() > 0) {
if (!missingParentalAccessionNumbers.isEmpty()) {
throw new HttpStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
String.format(missingParentalGIDsMsg,
arrayOfStringFormatter.apply(missingParentalAccessionNumbers)));
}

//GID existence check
if (missingAccessionNumbers.size() > 0) {
if (!missingAccessionNumbers.isEmpty()) {
throw new HttpStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
String.format(missingGIDsMsg,
arrayOfStringFormatter.apply(missingAccessionNumbers)));
Expand All @@ -247,7 +244,7 @@ public void getExistingBrapiData(List<BrAPIImport> importRows, Program program)
}
}
}
if (missingEntryNumbers.size() > 0) {
if (!missingEntryNumbers.isEmpty()) {
throw new HttpStatusException(HttpStatus.UNPROCESSABLE_ENTITY,
String.format(missingParentalEntryNoMsg,
arrayOfStringFormatter.apply(missingEntryNumbers)));
Expand Down Expand Up @@ -284,7 +281,6 @@ public Map<String, ImportPreviewStatistics> process(ImportUpload upload, List<Br
newGermplasmList = new ArrayList<>();
updatedGermplasmList = new ArrayList<>();
Map<String, ProgramBreedingMethodEntity> breedingMethods = new HashMap<>();
Boolean nullEntryNotFound = false;
List<String> badBreedingMethods = new ArrayList<>();
Map<String, Integer> entryNumberCounts = new HashMap<>();
List<String> userProvidedEntryNumbers = new ArrayList<>();
Expand Down Expand Up @@ -325,7 +321,7 @@ public Map<String, ImportPreviewStatistics> process(ImportUpload upload, List<Br
}

// Check for missing entry numbers
if (userProvidedEntryNumbers.size() > 0 && userProvidedEntryNumbers.size() < importRows.size()) {
if (!userProvidedEntryNumbers.isEmpty() && userProvidedEntryNumbers.size() < importRows.size()) {
throw new HttpStatusException(HttpStatus.UNPROCESSABLE_ENTITY, missingEntryNumbersMsg);
}

Expand Down Expand Up @@ -357,7 +353,7 @@ private void processNewGermplasm(Germplasm germplasm, ValidationErrors validatio
breedingMethod = breedingMethods.get(germplasm.getBreedingMethod());
} else {
List<ProgramBreedingMethodEntity> breedingMethodResults = breedingMethodDAO.findByNameOrAbbreviation(germplasm.getBreedingMethod(), program.getId());
if (breedingMethodResults.size() > 0) {
if (!breedingMethodResults.isEmpty()) {
breedingMethods.put(germplasm.getBreedingMethod(), breedingMethodResults.get(0));
breedingMethod = breedingMethods.get(germplasm.getBreedingMethod());
} else {
Expand All @@ -379,7 +375,7 @@ private void processNewGermplasm(Germplasm germplasm, ValidationErrors validatio

newGermplasmList.add(newGermplasm);
// Assign status of the germplasm
if (fileGermplasmByName.get(newGermplasm.getDefaultDisplayName()) > 1 || dbGermplasmByName.containsKey(newGermplasm.getDefaultDisplayName())) {
if (fileGermplasmByGID.get(newGermplasm.getAccessionNumber()) > 1 || dbGermplasmByAccessionNo.containsKey(newGermplasm.getAccessionNumber())) {
mappedImportRow.setGermplasm(new PendingImportObject<>(ImportObjectState.EXISTING, newGermplasm));
} else {
mappedImportRow.setGermplasm(new PendingImportObject<>(ImportObjectState.NEW, newGermplasm));
Expand Down Expand Up @@ -468,16 +464,6 @@ private boolean hasPedigree(BrAPIGermplasm germplasm) {
germplasm.getAdditionalInfo().get(BrAPIAdditionalInfoFields.MALE_PARENT_UNKNOWN).getAsBoolean());
}

//Used to check if germplasm already has a pedigree in the database
private boolean databaseGermplasmHasPedigree(Germplasm germplasm) {
if (germplasm.getAccessionNumber() == null || dbGermplasmByAccessionNo.get(germplasm.getAccessionNumber()) == null) {
return false;
} else {
BrAPIGermplasm dbGermplasm = dbGermplasmByAccessionNo.get(germplasm.getAccessionNumber());
return hasPedigree(dbGermplasm);
}
}

/**
* Compare an existing germplasm's pedigree to the incoming germplasm's pedigree to ensure they are the same.<br><br>
* Assumes that an empty value for a given parent in the incoming germplasm is equal to the existing germplasm.<br><br>
Expand Down Expand Up @@ -527,7 +513,7 @@ private boolean arePedigreesEqual(BrAPIGermplasm existingGermplasm, Germplasm ge
germplasmPedigreeGIDString.append(existingMalePedigree);
}

return existingPedigreeGIDString.toString().equals(germplasmPedigreeGIDString.toString());
return existingPedigreeGIDString.toString().contentEquals(germplasmPedigreeGIDString);
} else {
return true;
}
Expand Down Expand Up @@ -656,7 +642,7 @@ private void createPostOrder() {
}

totalRecorded += createList.size();
if (createList.size() > 0) {
if (!createList.isEmpty()) {
created.addAll(createList.stream().map(GermplasmImportIdUtils::getImportId).collect(Collectors.toList()));
postOrder.add(createList);
} else if (totalRecorded < newGermplasmList.size()) {
Expand All @@ -667,7 +653,7 @@ private void createPostOrder() {
}

@Override
public void validateDependencies(Map<Integer, PendingImport> mappedBrAPIImport) throws ValidatorException {
public void validateDependencies(Map<Integer, PendingImport> mappedBrAPIImport) {
// TODO
}

Expand Down Expand Up @@ -706,9 +692,7 @@ public void postBrapiData(Map<Integer, PendingImport> mappedBrAPIImport, Program

// Update our records with what is returned
Map<String, BrAPIGermplasm> createdGermplasmMap = new HashMap<>();
createdGermplasm.forEach(germplasm -> {
createdGermplasmMap.put(germplasm.getGermplasmName(), germplasm);
});
createdGermplasm.forEach(germplasm -> createdGermplasmMap.put(germplasm.getGermplasmName(), germplasm));
for (Map.Entry<Integer,PendingImport> entry : mappedBrAPIImport.entrySet()) {
String germplasmName = entry.getValue().getGermplasm().getBrAPIObject().getGermplasmName();
if (createdGermplasmMap.containsKey(germplasmName)) {
Expand Down
Loading