Skip to content

Commit

Permalink
Enabled deletion of non-loaded agms and their linked disease annotati…
Browse files Browse the repository at this point in the history
…ons on bulk load, SCRUM-1396
  • Loading branch information
mluypaert committed Apr 26, 2022
1 parent 1eabf9b commit ae6e697
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.alliancegenome.curation_api.dao;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;
import javax.persistence.Query;

import org.alliancegenome.curation_api.base.dao.BaseSQLDAO;
import org.alliancegenome.curation_api.model.entities.AffectedGenomicModel;
Expand All @@ -11,4 +14,10 @@ public class AffectedGenomicModelDAO extends BaseSQLDAO<AffectedGenomicModel> {
protected AffectedGenomicModelDAO() {
super(AffectedGenomicModel.class);
}

public List<String> findAllAnnotationIds(String taxonID) {
Query jpqlQuery = entityManager.createQuery("SELECT annotation.curie FROM AffectedGenomicModel annotation WHERE annotation.taxon.curie=:taxonId");
jpqlQuery.setParameter("taxonId", taxonID);
return (List<String>) jpqlQuery.getResultList();
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
package org.alliancegenome.curation_api.jobs.executors;

import java.io.FileInputStream;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.alliancegenome.curation_api.dao.AffectedGenomicModelDAO;
import org.alliancegenome.curation_api.exceptions.ObjectUpdateException;
import org.alliancegenome.curation_api.exceptions.ObjectUpdateException.ObjectUpdateExceptionData;
import org.alliancegenome.curation_api.model.entities.AffectedGenomicModel;
import org.alliancegenome.curation_api.model.entities.bulkloads.*;
import org.alliancegenome.curation_api.model.ingest.fms.dto.*;
import org.alliancegenome.curation_api.response.*;
import org.alliancegenome.curation_api.services.AffectedGenomicModelService;
import org.alliancegenome.curation_api.util.ProcessDisplayHelper;
import org.apache.commons.collections4.ListUtils;

import lombok.extern.jbosslog.JBossLog;

@JBossLog
@ApplicationScoped
public class AgmFmsExecutor extends LoadFileExecutor {

@Inject AffectedGenomicModelDAO affectedGenomicModelDAO;

@Inject AffectedGenomicModelService affectedGenomicModelService;

public void runLoad(BulkLoadFile bulkLoadFile) {
Expand All @@ -38,25 +45,55 @@ public void runLoad(BulkLoadFile bulkLoadFile) {

// Gets called from the API directly
public APIResponse runLoad(AffectedGenomicModelMetaDataFmsDTO agmData) {
List<String> taxonIDs = agmData.getData().stream()
.map( agmDTO -> agmDTO.getTaxonId() ).distinct().collect( Collectors.toList() );

List<String> annotationsIdsBefore = new ArrayList<String>();
for(String taxonID: taxonIDs) {
List<String> annotationIds = affectedGenomicModelDAO.findAllAnnotationIds(taxonID);
log.debug("runLoad: Before: taxonID " + taxonID + " " + annotationIds.size());
annotationsIdsBefore.addAll(annotationIds);
}

log.debug("runLoad: Before: total " + annotationsIdsBefore.size());
List<String> annotationsIdsLoaded = new ArrayList<>();

BulkLoadFileHistory history = new BulkLoadFileHistory(agmData.getData().size());

ProcessDisplayHelper ph = new ProcessDisplayHelper(10000);
ph.startProcess("AGM FMS DTO Update", agmData.getData().size());
for(AffectedGenomicModelFmsDTO agm: agmData.getData()) {
for(AffectedGenomicModelFmsDTO agmDTO: agmData.getData()) {
try {
affectedGenomicModelService.processUpdate(agm);
AffectedGenomicModel annotation = affectedGenomicModelService.processUpdate(agmDTO);
history.incrementCompleted();
annotationsIdsLoaded.add(annotation.getCurie());
} catch (ObjectUpdateException e) {
addException(history, e.getData());
} catch (Exception e) {
addException(history, new ObjectUpdateExceptionData(agm, e.getMessage()));
addException(history, new ObjectUpdateExceptionData(agmDTO, e.getMessage()));
}

ph.progressProcess();
}
ph.finishProcess();


log.debug("runLoad: Loaded: " + taxonIDs.toString() + " " + annotationsIdsLoaded.size());

List<String> distinctLoaded = annotationsIdsLoaded.stream().distinct().collect(Collectors.toList());
log.debug("runLoad: Distinct loaded: " + taxonIDs.toString() + " " + distinctLoaded.size());

List<String> curiesToRemove = ListUtils.subtract(annotationsIdsBefore, distinctLoaded);
log.debug("runLoad: Remove: " + taxonIDs.toString() + " " + curiesToRemove.size());

for (String curie : curiesToRemove) {
SearchResponse<AffectedGenomicModel> agm = affectedGenomicModelDAO.findByField("curie", curie);
if (agm != null && agm.getTotalResults() == 1) {
affectedGenomicModelDAO.remove(agm.getResults().get(0).getCurie());
} else {
log.error("Failed getting annotation: " + curie);
}
}

return new LoadHistoryResponce(history);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class AGMDiseaseAnnotation extends DiseaseAnnotation {

@IndexedEmbedded(includeDepth = 1)
@IndexingDependency(reindexOnUpdate = ReindexOnUpdate.SHALLOW)
@ManyToOne
@ManyToOne(cascade = {})
@org.hibernate.annotations.OnDelete(action = org.hibernate.annotations.OnDeleteAction.CASCADE)
@JoinColumn(foreignKey = @ForeignKey(name="fk_agmdasubject"))
@JsonView({View.FieldsOnly.class})
private AffectedGenomicModel subject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public ObjectResponse<AffectedGenomicModel> update(AffectedGenomicModel uiEntity


@Transactional
public void processUpdate(AffectedGenomicModelFmsDTO agm) throws ObjectUpdateException {
public AffectedGenomicModel processUpdate(AffectedGenomicModelFmsDTO agm) throws ObjectUpdateException {
// TODO: add loading of components
// TODO: add loading of sequenceTargetingReagents
// TODO: add loading of parentalPopulations
Expand All @@ -114,6 +114,8 @@ public void processUpdate(AffectedGenomicModelFmsDTO agm) throws ObjectUpdateExc
handleSecondaryIds(agm, dbAgm);

affectedGenomicModelDAO.persist(dbAgm);

return dbAgm;
}

private void handleCrossReference(AffectedGenomicModelFmsDTO agm, AffectedGenomicModel dbAgm) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
-- This change does require the application to be restarted after initial start-up (so hibernate would create the new constraint)!
ALTER TABLE "genediseaseannotation"
DROP CONSTRAINT "fk8xs26m9hfc38nmy7gvu3cec3t";
ALTER TABLE "agmdiseaseannotation"
DROP CONSTRAINT "fklvr4o1waqclvbktjmyg6x25ls";
Loading

0 comments on commit ae6e697

Please sign in to comment.