Skip to content

Commit

Permalink
Extract db communicating methods out of the constructor
Browse files Browse the repository at this point in the history
introduce initialise() method
  • Loading branch information
forus committed Jul 11, 2024
1 parent 0233279 commit 0edaec3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ protected GeneticAlterationImporter() {}
public GeneticAlterationImporter(
int geneticProfileId,
List<Integer> orderedSampleList
) throws DaoException {
) {
this.geneticProfileId = geneticProfileId;
this.orderedSampleList = orderedSampleList;
storeOrderedSampleList();
}

protected void storeOrderedSampleList() throws DaoException {
Expand Down Expand Up @@ -93,5 +92,13 @@ private void ensureNumberOfValuesIsCorrect(int valuesNumber) {
}
}


public void initialise() {
try {
storeOrderedSampleList();
} catch (DaoException e) {
throw new RuntimeException(e);
}
}
public void finalise() { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,15 @@ public class GeneticAlterationIncrementalImporter extends GeneticAlterationImpor

private final List<Integer> fileOrderedSampleList;
private final DaoGeneticAlteration daoGeneticAlteration = DaoGeneticAlteration.getInstance();
private final HashMap<Integer, HashMap<Integer, String>> geneticAlterationMap;
private HashMap<Integer, HashMap<Integer, String>> geneticAlterationMap;

public GeneticAlterationIncrementalImporter(
int geneticProfileId,
List<Integer> fileOrderedSampleList
) throws DaoException {
) {

this.geneticProfileId = geneticProfileId;
this.geneticAlterationMap = daoGeneticAlteration.getGeneticAlterationMapForEntityIds(geneticProfileId, null);
this.fileOrderedSampleList = fileOrderedSampleList;

ArrayList <Integer> savedOrderedSampleList = DaoGeneticProfileSamples.getOrderedSampleList(this.geneticProfileId);
int initialOrderSampleListSize = savedOrderedSampleList.size();
geneticAlterationMap.forEach((geneticEntityId, sampleToValue) -> {
if (sampleToValue.size() != initialOrderSampleListSize) {
throw new IllegalStateException("Number of samples ("
+ sampleToValue.size() + ") for genetic entity with id "
+ geneticEntityId + " does not match with the number in the preexisting sample list ("
+ initialOrderSampleListSize + ").");
}
});
// add all new sample ids at the end
this.orderedSampleList = new ArrayList<>(savedOrderedSampleList);
List<Integer> newSampleIds = this.fileOrderedSampleList.stream().filter(sampleId -> !savedOrderedSampleList.contains(sampleId)).toList();
this.orderedSampleList.addAll(newSampleIds);
DaoGeneticProfileSamples.deleteAllSamplesInGeneticProfile(this.geneticProfileId);
this.storeOrderedSampleList();
daoGeneticAlteration.deleteAllRecordsInGeneticProfile(this.geneticProfileId);
}

@Override
Expand All @@ -60,6 +41,33 @@ public boolean store(int geneticEntityId, String[] values) throws DaoException {
return super.store(geneticEntityId, expandedValues);
}

@Override
public void initialise() {
super.initialise();
try {
this.geneticAlterationMap = daoGeneticAlteration.getGeneticAlterationMapForEntityIds(geneticProfileId, null);
ArrayList <Integer> savedOrderedSampleList = DaoGeneticProfileSamples.getOrderedSampleList(this.geneticProfileId);
int initialOrderSampleListSize = savedOrderedSampleList.size();
geneticAlterationMap.forEach((geneticEntityId, sampleToValue) -> {
if (sampleToValue.size() != initialOrderSampleListSize) {
throw new IllegalStateException("Number of samples ("
+ sampleToValue.size() + ") for genetic entity with id "
+ geneticEntityId + " does not match with the number in the preexisting sample list ("
+ initialOrderSampleListSize + ").");
}
});
// add all new sample ids at the end
this.orderedSampleList = new ArrayList<>(savedOrderedSampleList);
List<Integer> newSampleIds = this.fileOrderedSampleList.stream().filter(sampleId -> !savedOrderedSampleList.contains(sampleId)).toList();
this.orderedSampleList.addAll(newSampleIds);
DaoGeneticProfileSamples.deleteAllSamplesInGeneticProfile(this.geneticProfileId);
this.storeOrderedSampleList();
daoGeneticAlteration.deleteAllRecordsInGeneticProfile(this.geneticProfileId);
} catch (DaoException e) {
throw new RuntimeException(e);
}
}

@Override
public void finalise() {
expandRemainingGeneticEntityTabDelimitedRowsWithBlankValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ private void doImportData() throws Exception {
orderedSampleList = newArrayList(toImport.eventsTable.columnKeySet());
this.geneticAlterationGeneImporter = isIncrementalUpdateMode ? new GeneticAlterationIncrementalImporter(geneticProfileId, orderedSampleList)
: new GeneticAlterationImporter(geneticProfileId, orderedSampleList);
geneticAlterationGeneImporter.initialise();
DaoSampleProfile.upsertSampleToProfileMapping(orderedSampleList, geneticProfileId, genePanelId);

for (Long entrezId : toImport.eventsTable.rowKeySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ private void doImportData() throws IOException, DaoException {
this.geneticAlterationImporter = isIncrementalUpdateMode ? new GeneticAlterationIncrementalImporter(geneticProfileId, orderedSampleList)
: new GeneticAlterationImporter(geneticProfileId, orderedSampleList);

geneticAlterationImporter.initialise();

//cache for data found in cna_event' table:
Set<CnaEvent.Event> existingCnaEvents = new HashSet<>();
if (isDiscretizedCnaProfile) {
Expand Down

0 comments on commit 0edaec3

Please sign in to comment.