Search in sources :

Example 6 with BiopsyData

use of com.hartwig.hmftools.patientdb.data.BiopsyData in project hmftools by hartwigmedical.

the class BiopsyMatcher method matchBiopsiesToTumorSamples.

@NotNull
public static MatchResult<BiopsyData> matchBiopsiesToTumorSamples(@NotNull final String patientId, @NotNull final List<SampleData> sequencedBiopsies, @NotNull final List<BiopsyData> clinicalBiopsies) {
    final List<BiopsyData> matchedBiopsies = Lists.newArrayList();
    final List<ValidationFinding> findings = Lists.newArrayList();
    if (clinicalBiopsies.size() < sequencedBiopsies.size()) {
        findings.add(biopsyMatchFinding(patientId, "less ecrf biopsies than biopsies sequenced.", "ecrf biopsies: " + clinicalBiopsies.size() + "; sequenced: " + sequencedBiopsies.size()));
    }
    List<BiopsyData> remainingBiopsies = clinicalBiopsies;
    for (final SampleData sequencedBiopsy : sequencedBiopsies) {
        final Map<Boolean, List<BiopsyData>> partitions = remainingBiopsies.stream().collect(Collectors.partitioningBy(clinicalBiopsy -> isPossibleMatch(sequencedBiopsy, clinicalBiopsy)));
        final List<BiopsyData> possibleMatches = partitions.get(true);
        if (possibleMatches.size() == 1 && possibleMatches.get(0).date() != null) {
            final BiopsyData clinicalBiopsy = possibleMatches.get(0);
            matchedBiopsies.add(ImmutableBiopsyData.builder().from(clinicalBiopsy).sampleId(sequencedBiopsy.sampleId()).build());
            if (hasMismatchOnSamplingDate(sequencedBiopsy, clinicalBiopsy)) {
                findings.add(biopsyMatchFinding(patientId, "sampling date does not equal biopsy date in matched biopsy", "sampling date: " + sequencedBiopsy.samplingDate() + "; ecrf biopsy date: " + clinicalBiopsy.date()));
            }
            remainingBiopsies = partitions.get(false);
        } else if (possibleMatches.size() == 0 || (possibleMatches.size() == 1 && possibleMatches.get(0).date() == null)) {
            findings.add(biopsyMatchFinding(patientId, "could not match any clinical biopsy with sequenced sample.", "sample: " + sampleDataToString(sequencedBiopsy) + "; ecrf biopsies: " + clinicalBiopsies.stream().map(BiopsyData::date).collect(Collectors.toList()) + ". match criteria: " + getMatchDateCriteria(sequencedBiopsy)));
            // MIVO: abort finding new matches if we can't match one sequenced biopsy
            return new MatchResult<>(clinicalBiopsies, findings);
        } else if (possibleMatches.size() > 1) {
            findings.add(biopsyMatchFinding(patientId, "more than 1 possible clinical biopsy match for sequenced sample.", "sample: " + sampleDataToString(sequencedBiopsy) + "; ecrf biopsies: " + clinicalBiopsies.stream().map(BiopsyData::date).collect(Collectors.toList()) + ". match criteria: " + getMatchDateCriteria(sequencedBiopsy)));
            // MIVO: abort finding new matches if we can't match one sequenced biopsy
            return new MatchResult<>(clinicalBiopsies, findings);
        }
    }
    matchedBiopsies.addAll(remainingBiopsies);
    return new MatchResult<>(matchedBiopsies, findings);
}
Also used : Config(com.hartwig.hmftools.patientdb.Config) FORM_BIOPS(com.hartwig.hmftools.patientdb.readers.BiopsyReader.FORM_BIOPS) BiopsyData(com.hartwig.hmftools.patientdb.data.BiopsyData) Collectors(java.util.stream.Collectors) ImmutableBiopsyData(com.hartwig.hmftools.patientdb.data.ImmutableBiopsyData) ValidationFinding(com.hartwig.hmftools.common.ecrf.datamodel.ValidationFinding) FormStatusState(com.hartwig.hmftools.common.ecrf.formstatus.FormStatusState) List(java.util.List) Lists(com.google.common.collect.Lists) Duration(java.time.Duration) LocalDate(java.time.LocalDate) Map(java.util.Map) SampleData(com.hartwig.hmftools.patientdb.data.SampleData) NotNull(org.jetbrains.annotations.NotNull) SampleData(com.hartwig.hmftools.patientdb.data.SampleData) ValidationFinding(com.hartwig.hmftools.common.ecrf.datamodel.ValidationFinding) List(java.util.List) BiopsyData(com.hartwig.hmftools.patientdb.data.BiopsyData) ImmutableBiopsyData(com.hartwig.hmftools.patientdb.data.ImmutableBiopsyData) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

BiopsyData (com.hartwig.hmftools.patientdb.data.BiopsyData)6 NotNull (org.jetbrains.annotations.NotNull)5 ValidationFinding (com.hartwig.hmftools.common.ecrf.datamodel.ValidationFinding)4 LocalDate (java.time.LocalDate)4 Lists (com.google.common.collect.Lists)3 FormStatusState (com.hartwig.hmftools.common.ecrf.formstatus.FormStatusState)3 Config (com.hartwig.hmftools.patientdb.Config)3 BiopsyTreatmentData (com.hartwig.hmftools.patientdb.data.BiopsyTreatmentData)3 Duration (java.time.Duration)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 EcrfPatient (com.hartwig.hmftools.common.ecrf.datamodel.EcrfPatient)2 BaselineData (com.hartwig.hmftools.patientdb.data.BaselineData)2 BiopsyTreatmentResponseData (com.hartwig.hmftools.patientdb.data.BiopsyTreatmentResponseData)2 ImmutableBiopsyData (com.hartwig.hmftools.patientdb.data.ImmutableBiopsyData)2 Patient (com.hartwig.hmftools.patientdb.data.Patient)2 PreTreatmentData (com.hartwig.hmftools.patientdb.data.PreTreatmentData)2 FORM_BIOPS (com.hartwig.hmftools.patientdb.readers.BiopsyReader.FORM_BIOPS)2 FORM_TREATMENT (com.hartwig.hmftools.patientdb.readers.BiopsyTreatmentReader.FORM_TREATMENT)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1