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);
}
Aggregations