Skip to content

Commit

Permalink
SCRUM-4185 update executor to use sqtr gene association service
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgibs committed Jul 9, 2024
1 parent 452982b commit dcb3aac
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;

import org.alliancegenome.curation_api.enums.BackendBulkDataProvider;
Expand All @@ -28,8 +30,10 @@

@ApplicationScoped
public class SequenceTargetingReagentExecutor extends LoadFileExecutor {
@Inject SequenceTargetingReagentService sqtrService;
@Inject SequenceTargetingReagentGeneAssociationService sqtrGeneAssociationService;
@Inject
SequenceTargetingReagentService sqtrService;
@Inject
SequenceTargetingReagentGeneAssociationService sqtrGeneAssociationService;

public void execLoad(BulkLoadFile bulkLoadFile) {

Expand All @@ -53,20 +57,20 @@ public void execLoad(BulkLoadFile bulkLoadFile) {

BackendBulkDataProvider dataProvider = BackendBulkDataProvider.valueOf(fms.getFmsDataSubType());

List<Long> sqtrIdsLoaded = new ArrayList<>();
List<Long> sqtrIdsBefore = sqtrService.getIdsByDataProvider(dataProvider.name());
List<Long> sqtrGeneAssociationIdsLoaded = new ArrayList<>();
List<Long> sqtrGeneAssociationIdsBefore = sqtrService.getIdsByDataProvider(dataProvider.name());
Map<String, List<Long>> idsAdded = new HashMap<String, List<Long>>();
idsAdded.put("SQTR", new ArrayList<Long>());
idsAdded.put("SQTRGeneAssociation", new ArrayList<Long>());

Map<String, List<Long>> previousIds = getPreviouslyLoadedIds(dataProvider);

bulkLoadFileDAO.merge(bulkLoadFile);

BulkLoadFileHistory history = new BulkLoadFileHistory(sqtrIngestFmsDTO.getData().size());
BulkLoadFileHistory history = new BulkLoadFileHistory(sqtrIngestFmsDTO.getData().size() * 2);

runLoad(history, dataProvider, sqtrIngestFmsDTO.getData(), sqtrIdsLoaded, sqtrGeneAssociationIdsLoaded);
runLoad(history, dataProvider, sqtrIngestFmsDTO.getData(), idsAdded.get("SQTR"), idsAdded.get("SQTRGeneAssociation"));

runCleanup(sqtrService, history, dataProvider.name(), sqtrIdsBefore, sqtrIdsLoaded, "SQTR", bulkLoadFile.getMd5Sum());
runCleanup(sqtrService, history, dataProvider.name(), sqtrGeneAssociationIdsBefore, sqtrGeneAssociationIdsLoaded, "SQTR Gene Associations", bulkLoadFile.getMd5Sum());
runCleanup(sqtrService, history, dataProvider.name(), previousIds.get("SQTR"), idsAdded.get("SQTR"), "SQTR", bulkLoadFile.getMd5Sum());
runCleanup(sqtrService, history, dataProvider.name(), previousIds.get("SQTRGeneAssociation"), idsAdded.get("SQTRGeneAssociation"), "SQTR Gene Associations", bulkLoadFile.getMd5Sum());

history.finishLoad();

Expand All @@ -77,11 +81,20 @@ public void execLoad(BulkLoadFile bulkLoadFile) {
}
}

private Map<String, List<Long>> getPreviouslyLoadedIds(BackendBulkDataProvider dataProvider) {
Map<String, List<Long>> previousIds = new HashMap<>();

previousIds.put("SQTR", sqtrService.getIdsByDataProvider(dataProvider.name()));
previousIds.put("SQTRGeneAssociation", sqtrGeneAssociationService.getIdsByDataProvider(dataProvider.name()));

return previousIds;
}

public APIResponse runLoadApi(String dataProviderName, List<SequenceTargetingReagentFmsDTO> sqtrDTOs) {
List<Long> sqtrIdsLoaded = new ArrayList<>();
List<Long> sqtrGeneAssociationIdsLoaded = new ArrayList<>();

BulkLoadFileHistory history = new BulkLoadFileHistory(sqtrDTOs.size());
BulkLoadFileHistory history = new BulkLoadFileHistory(sqtrDTOs.size() * 2);
BackendBulkDataProvider dataProvider = BackendBulkDataProvider.valueOf(dataProviderName);
runLoad(history, dataProvider, sqtrDTOs, sqtrIdsLoaded, sqtrGeneAssociationIdsLoaded);
history.finishLoad();
Expand All @@ -101,7 +114,8 @@ private void runLoad(BulkLoadFileHistory history, BackendBulkDataProvider dataPr

}

private void loadSequenceTargetingReagents(BulkLoadFileHistory history, List<SequenceTargetingReagentFmsDTO> sqtrs, List<Long> idsLoaded, BackendBulkDataProvider dataProvider, ProcessDisplayHelper ph) {
private void loadSequenceTargetingReagents(BulkLoadFileHistory history, List<SequenceTargetingReagentFmsDTO> sqtrs,
List<Long> idsLoaded, BackendBulkDataProvider dataProvider, ProcessDisplayHelper ph) {
for (SequenceTargetingReagentFmsDTO dto : sqtrs) {
try {
SequenceTargetingReagent dbObject = sqtrService.upsert(dto, dataProvider);
Expand All @@ -121,11 +135,13 @@ private void loadSequenceTargetingReagents(BulkLoadFileHistory history, List<Seq
}
}

private void loadSequenceTargetingReagentGeneAssociations(BulkLoadFileHistory history, List<SequenceTargetingReagentFmsDTO> sqtrs, List<Long> idsLoaded, BackendBulkDataProvider dataProvider, ProcessDisplayHelper ph) {
private void loadSequenceTargetingReagentGeneAssociations(BulkLoadFileHistory history,
List<SequenceTargetingReagentFmsDTO> sqtrs, List<Long> idsLoaded, BackendBulkDataProvider dataProvider,
ProcessDisplayHelper ph) {

for (SequenceTargetingReagentFmsDTO dto : sqtrs) {
try {
List<Long> associationIds = sqtrGeneAssociationService.addGeneAssociations(dto, dataProvider);
List<Long> associationIds = sqtrGeneAssociationService.loadGeneAssociations(dto, dataProvider);
history.incrementCompleted();
if (idsLoaded != null) {
idsLoaded.addAll(associationIds);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.alliancegenome.curation_api.services.associations;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.alliancegenome.curation_api.constants.EntityFieldConstants;
import org.alliancegenome.curation_api.dao.associations.SequenceTargetingReagentGeneAssociationDAO;
import org.alliancegenome.curation_api.dao.associations.alleleAssociations.AlleleGeneAssociationDAO;
import org.alliancegenome.curation_api.enums.BackendBulkDataProvider;
Expand All @@ -24,31 +28,15 @@
import jakarta.transaction.Transactional;

@RequestScoped
public class SequenceTargetingReagentGeneAssociationService extends
BaseAssociationDTOCrudService<SequenceTargetingReagentGeneAssociation, SequenceTargetingReagentFmsDTO, SequenceTargetingReagentGeneAssociationDAO> {
public class SequenceTargetingReagentGeneAssociationService {

@Inject
SequenceTargetingReagentGeneAssociationDAO sequenceTargetingReagentGeneAssociationDAO;
@Inject
SequenceTargetingReagentGeneAssociationFmsDTOValidator sequenceTargetingReagentGeneAssociationFmsDTOValidator;

@Override
@PostConstruct
protected void init() {
setSQLDao(sequenceTargetingReagentGeneAssociationDAO);
}

@Transactional
public SequenceTargetingReagentGeneAssociation upsert(SequenceTargetingReagentFmsDTO dto,
BackendBulkDataProvider dataProvider) throws ObjectUpdateException {
// TODO: fix this placeholder code
SequenceTargetingReagentGeneAssociation sequenceTargetingReagentGeneAssociation = new SequenceTargetingReagentGeneAssociation();
return sequenceTargetingReagentGeneAssociation;
}

// TODO: rename?
@Transactional
public List<Long> addGeneAssociations(SequenceTargetingReagentFmsDTO dto, BackendBulkDataProvider dataProvider)
throws ObjectUpdateException {
public List<Long> loadGeneAssociations(SequenceTargetingReagentFmsDTO dto, BackendBulkDataProvider dataProvider) throws ObjectUpdateException {

List<SequenceTargetingReagentGeneAssociation> associations = sequenceTargetingReagentGeneAssociationFmsDTOValidator
.validateSQTRGeneAssociationFmsDTO(dto, dataProvider);
Expand Down Expand Up @@ -102,4 +90,12 @@ private void addAssociationToGene(SequenceTargetingReagentGeneAssociation associ
}

}

public List<Long> getIdsByDataProvider(String dataProvider) {
Map<String, Object> params = new HashMap<>();
params.put(EntityFieldConstants.DATA_PROVIDER, dataProvider);
List<Long> ids = sequenceTargetingReagentGeneAssociationDAO.findIdsByParams(params);
ids.removeIf(Objects::isNull);
return ids;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public List<SequenceTargetingReagentGeneAssociation> validateSQTRGeneAssociation
ObjectResponse<SequenceTargetingReagent> sqtrResponse = new ObjectResponse<>();

SequenceTargetingReagent sqtr;
SearchResponse<SequenceTargetingReagent> sqtrSearchResponse = sqtrDAO.findByField("modEntityId",
dto.getPrimaryId());
SearchResponse<SequenceTargetingReagent> sqtrSearchResponse = sqtrDAO.findByField("modEntityId", dto.getPrimaryId());

if (sqtrSearchResponse == null || sqtrSearchResponse.getSingleResult() == null) {
sqtrResponse.addErrorMessage("modEntityId",
Expand All @@ -51,22 +50,25 @@ public List<SequenceTargetingReagentGeneAssociation> validateSQTRGeneAssociation
relation = relationSearchResponse.getSingleResult();
}

for (String geneId : dto.getTargetGeneIds()) {
Gene gene = geneService.findByIdentifierString(geneId);

if (gene == null) {
sqtrResponse.addErrorMessage("targetGeneIds",
ValidationConstants.INVALID_MESSAGE + " (" + geneId + ")");
} else {
SequenceTargetingReagentGeneAssociation strGeneAssociation = new SequenceTargetingReagentGeneAssociation();
strGeneAssociation.setSequenceTargetingReagentAssociationSubject(sqtr);
strGeneAssociation.setRelation(relation);
strGeneAssociation.setSequenceTargetingReagentGeneAssociationObject(gene);

strGeneAssociations.add(strGeneAssociation);
if(dto.getTargetGeneIds() != null){
for (String geneId : dto.getTargetGeneIds()) {
Gene gene = geneService.findByIdentifierString(geneId);

if (gene == null) {
sqtrResponse.addErrorMessage("targetGeneIds",
ValidationConstants.INVALID_MESSAGE + " (" + geneId + ")");
} else {
SequenceTargetingReagentGeneAssociation strGeneAssociation = new SequenceTargetingReagentGeneAssociation();
strGeneAssociation.setSequenceTargetingReagentAssociationSubject(sqtr);
strGeneAssociation.setRelation(relation);
strGeneAssociation.setSequenceTargetingReagentGeneAssociationObject(gene);

strGeneAssociations.add(strGeneAssociation);
}

}

}

if (sqtrResponse.hasErrors()) {
throw new ObjectValidationException(dto, sqtrResponse.errorMessagesString());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package org.alliancegenome.curation_api.services.validation.dto.fms;

import java.util.ArrayList;
import java.util.List;

import org.alliancegenome.curation_api.constants.ValidationConstants;
import org.alliancegenome.curation_api.dao.SequenceTargetingReagentDAO;
import org.alliancegenome.curation_api.enums.BackendBulkDataProvider;
import org.alliancegenome.curation_api.exceptions.ObjectValidationException;
import org.alliancegenome.curation_api.model.entities.Gene;
import org.alliancegenome.curation_api.model.entities.SequenceTargetingReagent;
import org.alliancegenome.curation_api.model.entities.VocabularyTerm;
import org.alliancegenome.curation_api.model.entities.associations.sequenceTargetingReagentAssociations.SequenceTargetingReagentGeneAssociation;
import org.alliancegenome.curation_api.model.entities.ontology.NCBITaxonTerm;
import org.alliancegenome.curation_api.model.ingest.dto.fms.SequenceTargetingReagentFmsDTO;
import org.alliancegenome.curation_api.response.ObjectResponse;
Expand Down

0 comments on commit dcb3aac

Please sign in to comment.