Skip to content

Commit

Permalink
put generic assay enrichment service in existing enrichment service
Browse files Browse the repository at this point in the history
  • Loading branch information
Djokovic0311 committed Aug 11, 2023
1 parent e1d28ea commit 55b258c
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 278 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ List<GenomicEnrichment> getGenomicEnrichments(String molecularProfileId,
Map<String, List<MolecularProfileCaseIdentifier>> molecularProfileCaseSets, EnrichmentType enrichmentType)
throws MolecularProfileNotFoundException;

List<GenericAssayEnrichment> getGenericAssayEnrichments(String molecularProfileId,
List<GenericAssayEnrichment> getGenericAssayNumericalEnrichments(String molecularProfileId,
Map<String, List<MolecularProfileCaseIdentifier>> molecularProfileCaseSets, EnrichmentType enrichmentType)
throws MolecularProfileNotFoundException;
List<GenericAssayBinaryEnrichment> getGenericAssayBinaryEnrichments(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public List<GenomicEnrichment> getGenomicEnrichments(String molecularProfileId,
MolecularAlterationType.PROTEIN_ARRAY_PROTEIN_LEVEL,
MolecularAlterationType.PROTEIN_ARRAY_PHOSPHORYLATION);

validateMolecularProfile(molecularProfile, validGenomicMolecularAlterationTypes);
validateMolecularProfile(molecularProfile, validGenomicMolecularAlterationTypes,"GENOMIC");

Iterable<GeneMolecularAlteration> maItr = molecularDataRepository
.getGeneMolecularAlterationsIterableFast(molecularProfile.getStableId());
Expand Down Expand Up @@ -98,66 +98,31 @@ public List<GenomicEnrichment> getGenomicEnrichments(String molecularProfileId,
// transaction needs to be setup here in order to return Iterable from
// molecularDataRepository in getGenericAssayMolecularAlterationsIterable
@Transactional(readOnly = true)
public List<GenericAssayEnrichment> getGenericAssayEnrichments(String molecularProfileId,
public List<GenericAssayEnrichment> getGenericAssayNumericalEnrichments(String molecularProfileId,
Map<String, List<MolecularProfileCaseIdentifier>> molecularProfileCaseSets, EnrichmentType enrichmentType)
throws MolecularProfileNotFoundException {

MolecularProfile molecularProfile = molecularProfileService.getMolecularProfile(molecularProfileId);

validateMolecularProfile(molecularProfile, Arrays.asList(MolecularAlterationType.GENERIC_ASSAY));

MolecularProfile molecularProfile = getAndValidateMolecularProfile(molecularProfileId, "LIMIT-VALUE");

Iterable<GenericAssayMolecularAlteration> maItr = molecularDataRepository
.getGenericAssayMolecularAlterationsIterable(molecularProfile.getStableId(), null, "SUMMARY");

Map<String, List<MolecularProfileCaseIdentifier>> filteredMolecularProfileCaseSets;
if (BooleanUtils.isTrue(molecularProfile.getPatientLevel())) {
// Build sampleIdToPatientIdMap to quick find if a sample has shared patientId with other samples
List<String> sampleIds = molecularProfileCaseSets.values().stream().flatMap(Collection::stream).map(MolecularProfileCaseIdentifier::getCaseId).collect(Collectors.toList());
List<String> studyIds = Collections.nCopies(sampleIds.size(), molecularProfile.getCancerStudyIdentifier());
List<Sample> samples = sampleService.fetchSamples(studyIds, sampleIds, "ID");
Map<String, Integer> sampleIdToPatientIdMap = samples.stream().collect(Collectors.toMap(Sample::getStableId, Sample::getPatientId));
// Build filteredMolecularProfileCaseSets
filteredMolecularProfileCaseSets = new HashMap<>();
for (Map.Entry<String, List<MolecularProfileCaseIdentifier>> pair : molecularProfileCaseSets.entrySet()) {
Set<Integer> patientSet = new HashSet<Integer>();
List<MolecularProfileCaseIdentifier> identifierListUniqueByPatientId = new ArrayList<>();
for (MolecularProfileCaseIdentifier caseIdentifier : pair.getValue()) {
if (!patientSet.contains(sampleIdToPatientIdMap.get(caseIdentifier.getCaseId()))) {
identifierListUniqueByPatientId.add(caseIdentifier);
patientSet.add(sampleIdToPatientIdMap.get(caseIdentifier.getCaseId()));
}
}
filteredMolecularProfileCaseSets.put(pair.getKey(), identifierListUniqueByPatientId);
}
} else {
filteredMolecularProfileCaseSets = molecularProfileCaseSets;
}
Map<String, List<MolecularProfileCaseIdentifier>> filteredMolecularProfileCaseSets = filterMolecularProfileCaseSets(molecularProfile, molecularProfileCaseSets);;

List<GenericAssayEnrichment> genericAssayEnrichments = expressionEnrichmentUtil.getEnrichments(molecularProfile,
filteredMolecularProfileCaseSets, enrichmentType, maItr);
List<String> getGenericAssayStableIds = genericAssayEnrichments.stream()
.map(GenericAssayEnrichment::getStableId).collect(Collectors.toList());
Map<String, GenericAssayMeta> genericAssayMetaByStableId = genericAssayService
.getGenericAssayMetaByStableIdsAndMolecularIds(getGenericAssayStableIds,
getGenericAssayStableIds.stream().map(stableId -> molecularProfileId)
.collect(Collectors.toList()),
"SUMMARY")
.stream().collect(Collectors.toMap(GenericAssayMeta::getStableId, Function.identity()));

Map<String, GenericAssayMeta> genericAssayMetaByStableId = getGenericAssayMetaByStableId(getGenericAssayStableIds, molecularProfileId);

return genericAssayEnrichments.stream().map(enrichmentDatum -> {
enrichmentDatum.setGenericEntityMetaProperties(
genericAssayMetaByStableId.get(enrichmentDatum.getStableId()).getGenericEntityMetaProperties());
return enrichmentDatum;
}).collect(Collectors.toList());
}


private void validateMolecularProfile(MolecularProfile molecularProfile,
List<MolecularAlterationType> validMolecularAlterationTypes) throws MolecularProfileNotFoundException {
if (!validMolecularAlterationTypes.contains(molecularProfile.getMolecularAlterationType())) {
throw new MolecularProfileNotFoundException(molecularProfile.getStableId());
}
}


@Override
@Transactional(readOnly = true)
public List<GenericAssayBinaryEnrichment> getGenericAssayBinaryEnrichments(
Expand All @@ -166,7 +131,7 @@ public List<GenericAssayBinaryEnrichment> getGenericAssayBinaryEnrichments(
throws MolecularProfileNotFoundException {

// Validate and fetch molecular profile
MolecularProfile molecularProfile = getAndValidateMolecularProfile(molecularProfileId, true);
MolecularProfile molecularProfile = getAndValidateMolecularProfile(molecularProfileId, "BINARY");

// Get the molecular alterations for the provided profile
Iterable<GenericAssayMolecularAlteration> maItr = molecularDataRepository
Expand Down Expand Up @@ -203,13 +168,12 @@ public List<GenericAssayCategoricalEnrichment> getGenericAssayCategoricalEnrichm
Map<String, List<MolecularProfileCaseIdentifier>> molecularProfileCaseSets, EnrichmentType enrichmentType)
throws MolecularProfileNotFoundException {

MolecularProfile molecularProfile = getAndValidateMolecularProfile(molecularProfileId, false);
MolecularProfile molecularProfile = getAndValidateMolecularProfile(molecularProfileId, "CATEGORICAL");

Iterable<GenericAssayMolecularAlteration> maItr = molecularDataRepository
.getGenericAssayMolecularAlterationsIterable(molecularProfile.getStableId(), null, "SUMMARY");

Map<String, List<MolecularProfileCaseIdentifier>> filteredMolecularProfileCaseSets;
filteredMolecularProfileCaseSets = filterMolecularProfileCaseSets(molecularProfile, molecularProfileCaseSets);
Map<String, List<MolecularProfileCaseIdentifier>> filteredMolecularProfileCaseSets = filterMolecularProfileCaseSets(molecularProfile, molecularProfileCaseSets);

List<GenericAssayCategoricalEnrichment> genericAssayCategoricalEnrichments = expressionEnrichmentUtil.getGenericAssayCategoricalEnrichments(molecularProfile,
filteredMolecularProfileCaseSets, enrichmentType, maItr);
Expand All @@ -226,30 +190,24 @@ public List<GenericAssayCategoricalEnrichment> getGenericAssayCategoricalEnrichm
return enrichmentDatum;
}).collect(Collectors.toList());
}

private MolecularProfile getAndValidateMolecularProfile(String molecularProfileId, boolean isBinary) throws MolecularProfileNotFoundException {
private MolecularProfile getAndValidateMolecularProfile(String molecularProfileId, String dataType) throws MolecularProfileNotFoundException {
MolecularProfile molecularProfile = molecularProfileService.getMolecularProfile(molecularProfileId);
validateMolecularProfile(molecularProfile, Arrays.asList(MolecularProfile.MolecularAlterationType.GENERIC_ASSAY), isBinary);
validateMolecularProfile(molecularProfile, Arrays.asList(MolecularProfile.MolecularAlterationType.GENERIC_ASSAY), dataType);
return molecularProfile;
}

private void validateMolecularProfile(MolecularProfile molecularProfile,
List<MolecularProfile.MolecularAlterationType> validMolecularAlterationTypes,
boolean isBinary) throws MolecularProfileNotFoundException {
String dataType) throws MolecularProfileNotFoundException {
if (!validMolecularAlterationTypes.contains(molecularProfile.getMolecularAlterationType())) {
// Check alteration type
throw new MolecularProfileNotFoundException(molecularProfile.getStableId());
}
// Check datatype for binary or categorical
if(isBinary) {
if(!molecularProfile.getDatatype().equals("BINARY")) {
throw new MolecularProfileNotFoundException(molecularProfile.getStableId());
}
} else {
if(!molecularProfile.getDatatype().equals("CATEGORICAL")) {
throw new MolecularProfileNotFoundException(molecularProfile.getStableId());
}
}
if(molecularProfile.getMolecularAlterationType().equals(MolecularProfile.MolecularAlterationType.GENERIC_ASSAY) &&
!molecularProfile.getDatatype().equals(dataType))
throw new MolecularProfileNotFoundException(molecularProfile.getStableId());
}

private Map<String, List<MolecularProfileCaseIdentifier>> filterMolecularProfileCaseSets(MolecularProfile molecularProfile, Map<String, List<MolecularProfileCaseIdentifier>> molecularProfileCaseSets) {
Expand Down
Loading

0 comments on commit 55b258c

Please sign in to comment.