-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simple entry point. Fetch MAF records and write them to zip file
- Loading branch information
Showing
8 changed files
with
427 additions
and
70 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/main/java/org/cbioportal/file/export/MafRecordFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.cbioportal.file.export; | ||
|
||
import org.cbioportal.file.model.MafRecord; | ||
import org.cbioportal.model.MolecularProfile; | ||
import org.cbioportal.model.Mutation; | ||
import org.cbioportal.service.MolecularProfileService; | ||
import org.cbioportal.service.MutationService; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
@Component | ||
public class MafRecordFetcher { | ||
|
||
private final MolecularProfileService molecularProfileService; | ||
private final MutationService mutationService; | ||
|
||
public MafRecordFetcher(MolecularProfileService molecularProfileService, MutationService mutationService) { | ||
this.molecularProfileService = molecularProfileService; | ||
this.mutationService = mutationService; | ||
} | ||
|
||
public Iterator<MafRecord> fetch(Map<String, Set<String>> sampleIdsByStudyId) { | ||
List<String> studyIds = List.copyOf(sampleIdsByStudyId.keySet()); | ||
List<String> molecularProfileStableIds = this.molecularProfileService.getMolecularProfilesInStudies(studyIds, "ID").stream() | ||
.map(MolecularProfile::getStableId).toList(); | ||
List<String> sampleIds = List.copyOf(sampleIdsByStudyId.values().stream().flatMap(Set::stream).toList()); | ||
List<Integer> entrezGeneIds = List.of(); | ||
List<Mutation> mutationList = mutationService.getMutationsInMultipleMolecularProfiles( | ||
molecularProfileStableIds, sampleIds, entrezGeneIds, "EXPORT", null, null, null, null); | ||
return mutationList.stream() | ||
.filter(mutation -> sampleIdsByStudyId.get(mutation.getStudyId()).contains(mutation.getSampleId())) | ||
.map(mutation -> new MafRecord( | ||
mutation.getGene().getHugoGeneSymbol(), | ||
mutation.getGene().getEntrezGeneId().toString(), | ||
mutation.getCenter(), | ||
mutation.getNcbiBuild(), | ||
mutation.getChr(), | ||
mutation.getStartPosition(), | ||
mutation.getEndPosition(), | ||
"+", | ||
mutation.getMutationType(), | ||
mutation.getVariantType(), | ||
mutation.getReferenceAllele(), | ||
mutation.getTumorSeqAllele(), | ||
//TODO check if this is correct | ||
mutation.getTumorSeqAllele(), | ||
mutation.getDbSnpRs(), | ||
mutation.getDbSnpValStatus(), | ||
mutation.getSampleId(), | ||
mutation.getMatchedNormSampleBarcode(), | ||
mutation.getMatchNormSeqAllele1(), | ||
mutation.getMatchNormSeqAllele2(), | ||
mutation.getTumorValidationAllele1(), | ||
mutation.getTumorValidationAllele2(), | ||
mutation.getMatchNormValidationAllele1(), | ||
mutation.getMatchNormValidationAllele2(), | ||
mutation.getVerificationStatus(), | ||
mutation.getValidationStatus(), | ||
mutation.getMutationStatus(), | ||
mutation.getSequencingPhase(), | ||
mutation.getSequenceSource(), | ||
mutation.getValidationMethod(), | ||
mutation.getScore() == null ? null : mutation.getScore().toString(), | ||
mutation.getBamFile(), | ||
mutation.getSequencer(), | ||
//TODO how to calculate HgvpShort? | ||
"", | ||
mutation.getTumorAltCount(), | ||
mutation.getTumorRefCount(), | ||
mutation.getNormalAltCount(), | ||
mutation.getNormalRefCount() | ||
)).iterator(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 57 additions & 59 deletions
116
src/main/java/org/cbioportal/service/impl/ExportService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,74 @@ | ||
package org.cbioportal.service.impl; | ||
|
||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.cbioportal.model.*; | ||
import org.cbioportal.file.export.MafRecordFetcher; | ||
import org.cbioportal.file.export.MafRecordWriter; | ||
import org.cbioportal.model.CancerStudy; | ||
import org.cbioportal.model.Sample; | ||
import org.cbioportal.service.*; | ||
import org.cbioportal.service.exception.MolecularProfileNotFoundException; | ||
import org.cbioportal.service.exception.PatientNotFoundException; | ||
import org.cbioportal.service.exception.SampleNotFoundException; | ||
import org.cbioportal.service.exception.StudyNotFoundException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.cbioportal.service.util.SessionServiceRequestHandler; | ||
import org.cbioportal.web.parameter.VirtualStudy; | ||
import org.cbioportal.web.parameter.VirtualStudySamples; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipOutputStream; | ||
|
||
@Service | ||
public class ExportService { | ||
|
||
@Autowired | ||
PatientService patientService; | ||
|
||
@Autowired | ||
SampleService sampleService; | ||
|
||
@Autowired | ||
ClinicalDataService clinicalDataService; | ||
|
||
@Autowired | ||
MutationService mutationService; | ||
|
||
@Autowired | ||
MolecularProfileService molecularProfileService; | ||
private final StudyService studyService; | ||
private final SessionServiceRequestHandler sessionServiceRequestHandler; | ||
private final SampleService sampleService; | ||
private final MafRecordFetcher mafRecordFetcher; | ||
|
||
public ExportService(StudyService studyService, | ||
SessionServiceRequestHandler sessionServiceRequestHandler, | ||
SampleService sampleService, | ||
MafRecordFetcher mafRecordFetcher) { | ||
this.studyService = studyService; | ||
this.sessionServiceRequestHandler = sessionServiceRequestHandler; | ||
this.sampleService = sampleService; | ||
this.mafRecordFetcher = mafRecordFetcher; | ||
} | ||
|
||
public void exportData(Map<String, List<String>> samples) throws SampleNotFoundException, StudyNotFoundException, PatientNotFoundException, MolecularProfileNotFoundException { | ||
if (samples.size() > 1) { | ||
//virtual study | ||
public ByteArrayOutputStream exportStudyDataToZip(String studyId) throws IOException { | ||
List<CancerStudy> studies = studyService.fetchStudies(List.of(studyId), "DETAILED"); | ||
Map<String, Set<String>> studyToSampleMap = new HashMap<>(); | ||
if (studies.isEmpty()) { | ||
VirtualStudy virtualStudy = sessionServiceRequestHandler.getVirtualStudyById(studyId); | ||
studyToSampleMap.putAll( | ||
virtualStudy.getData().getStudies().stream().collect(Collectors.toMap(VirtualStudySamples::getId, VirtualStudySamples::getSamples))); | ||
} else { | ||
List<Sample> samples = sampleService.getAllSamplesInStudies(List.of(studyId), "ID", null, null, null, null); | ||
studyToSampleMap.put(studyId, samples.stream().map(Sample::getStableId).collect(Collectors.toSet())); | ||
} | ||
for (Map.Entry<String, List<String>> studySamples: samples.entrySet()) { | ||
String studyId = studySamples.getKey(); | ||
for (String sampleId: studySamples.getValue()) { | ||
Sample sample = sampleService.getSampleInStudy(studyId, sampleId); | ||
String patientStableId = sample.getPatientStableId(); | ||
String sampleStableId = sample.getStableId(); | ||
List<ClinicalData> sampleClinicalData = clinicalDataService.getAllClinicalDataOfSampleInStudy(studyId, sampleStableId, null, null, null, null, null, null); | ||
for (ClinicalData sampleClinicalDatum : sampleClinicalData) { | ||
ClinicalAttribute clinicalAttribute = sampleClinicalDatum.getClinicalAttribute(); | ||
sampleClinicalDatum.getAttrId(); | ||
sampleClinicalDatum.getAttrValue(); | ||
} | ||
Patient patient = patientService.getPatientInStudy(studyId, patientStableId); | ||
List<ClinicalData> patientClinicalData = clinicalDataService.getAllClinicalDataOfPatientInStudy(studyId, patientStableId, null, null, null, null, null, null); | ||
for (ClinicalData patientClinicalDataItem : patientClinicalData) { | ||
ClinicalAttribute clinicalAttribute = patientClinicalDataItem.getClinicalAttribute(); | ||
patientClinicalDataItem.getAttrId(); | ||
patientClinicalDataItem.getAttrValue(); | ||
} | ||
} | ||
List<MolecularProfileCaseIdentifier> molecularProfileCaseIdentifiers = molecularProfileService.getMolecularProfileCaseIdentifiers(List.of(studyId), studySamples.getValue()); | ||
for (MolecularProfileCaseIdentifier molecularProfileCaseIdentifier : molecularProfileCaseIdentifiers) { | ||
MolecularProfile molecularProfile = molecularProfileService.getMolecularProfile(molecularProfileCaseIdentifier.getMolecularProfileId()); | ||
MolecularProfile.MolecularAlterationType molecularAlterationType = molecularProfile.getMolecularAlterationType(); | ||
molecularProfile.getDatatype(); | ||
molecularProfile.getName(); | ||
switch (molecularAlterationType) { | ||
case MUTATION_EXTENDED -> { | ||
List<Mutation> mutationList = mutationService.getMutationsInMultipleMolecularProfilesByGeneQueries(List.of(molecularProfileCaseIdentifier.getMolecularProfileId()), studySamples.getValue(), List.of(), "DETAILED", 10000, 1, null, null); | ||
for (Mutation mutation : mutationList) { | ||
mutation.getChr(); | ||
} | ||
} | ||
} | ||
} | ||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); | ||
try (ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream)) { | ||
// Add files to the ZIP | ||
StringWriter mafRecordsStringWriter = new StringWriter(); | ||
MafRecordWriter mafRecordWriter = new MafRecordWriter(mafRecordsStringWriter); | ||
//TODO do not produce the file if no data has been retrieved | ||
mafRecordWriter.write(mafRecordFetcher.fetch(studyToSampleMap)); | ||
addFileToZip(zipOutputStream, "data_mutation.txt", mafRecordsStringWriter.toString().getBytes()); | ||
} | ||
return byteArrayOutputStream; | ||
} | ||
|
||
private void addFileToZip(ZipOutputStream zipOutputStream, String fileName, byte[] fileContent) throws IOException { | ||
// Create a new ZIP entry for the file | ||
ZipEntry zipEntry = new ZipEntry(fileName); | ||
zipOutputStream.putNextEntry(zipEntry); | ||
|
||
// Write file content | ||
zipOutputStream.write(fileContent); | ||
zipOutputStream.closeEntry(); | ||
} | ||
} |
Oops, something went wrong.