Search in sources :

Example 11 with ValidationFinding

use of com.hartwig.hmftools.common.ecrf.datamodel.ValidationFinding in project hmftools by hartwigmedical.

the class TreatmentResponseMatcher method matchTreatmentResponsesToTreatments.

@NotNull
public static MatchResult<BiopsyTreatmentResponseData> matchTreatmentResponsesToTreatments(@NotNull String patientId, @NotNull List<BiopsyTreatmentData> treatments, @NotNull List<BiopsyTreatmentResponseData> responses) {
    final List<BiopsyTreatmentResponseData> matchedResponses = Lists.newArrayList();
    final List<ValidationFinding> findings = Lists.newArrayList();
    Collections.sort(responses);
    List<BiopsyTreatmentData> sortedTreatments = sortAndFilter(treatments);
    if (hasOverlappingTreatments(sortedTreatments)) {
        if (!responses.isEmpty()) {
            findings.add(responseMatchFinding(patientId, "treatments are overlapping. Cannot match any response.", "treatments: " + sortedTreatments));
        }
        return new MatchResult<>(responses, findings);
    }
    Iterator<BiopsyTreatmentData> treatmentIterator = sortedTreatments.iterator();
    BiopsyTreatmentData currentTreatment = treatmentIterator.hasNext() ? treatmentIterator.next() : null;
    LocalDate firstTreatmentStart = currentTreatment != null ? currentTreatment.startDate() : null;
    BiopsyTreatmentData nextTreatment = treatmentIterator.hasNext() ? treatmentIterator.next() : null;
    boolean hasNewBaselineResponseFound = false;
    for (BiopsyTreatmentResponseData response : responses) {
        LocalDate responseDate = response.date();
        if (responseMatchable(responseDate, firstTreatmentStart)) {
            LocalDate nextTreatmentStart = nextTreatment != null ? nextTreatment.startDate() : null;
            while (nextTreatmentStart != null && responseDate.isAfter(nextTreatmentStart)) {
                currentTreatment = nextTreatment;
                nextTreatment = treatmentIterator.hasNext() ? treatmentIterator.next() : null;
                nextTreatmentStart = nextTreatment != null ? nextTreatment.startDate() : null;
                hasNewBaselineResponseFound = false;
            }
            if (hasNewBaselineResponseFound) {
                matchedResponses.add(response);
                findings.add(responseMatchFinding(patientId, "response after new baseline and before next treatment", "response: " + response));
            } else {
                String actualResponse = response.response();
                if (actualResponse != null && actualResponse.equalsIgnoreCase("ne")) {
                    matchedResponses.add(response);
                    hasNewBaselineResponseFound = true;
                } else {
                    matchedResponses.add(ImmutableBiopsyTreatmentResponseData.builder().from(response).treatmentId(currentTreatment.id()).build());
                }
            }
        } else {
            matchedResponses.add(response);
        }
    }
    return new MatchResult<>(matchedResponses, findings);
}
Also used : ImmutableBiopsyTreatmentResponseData(com.hartwig.hmftools.patientdb.data.ImmutableBiopsyTreatmentResponseData) BiopsyTreatmentResponseData(com.hartwig.hmftools.patientdb.data.BiopsyTreatmentResponseData) ValidationFinding(com.hartwig.hmftools.common.ecrf.datamodel.ValidationFinding) BiopsyTreatmentData(com.hartwig.hmftools.patientdb.data.BiopsyTreatmentData) LocalDate(java.time.LocalDate) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with ValidationFinding

use of com.hartwig.hmftools.common.ecrf.datamodel.ValidationFinding in project hmftools by hartwigmedical.

the class TreatmentResponseValidationTest method reportsFirstMeasurementAfterTreatmentStart.

@Test
public void reportsFirstMeasurementAfterTreatmentStart() {
    BiopsyTreatmentResponseData matchedResponseFeb2015 = ImmutableBiopsyTreatmentResponseData.builder().from(RESPONSE_FEB2015).treatmentId(TREATMENT_JAN_MAR.id()).build();
    final List<ValidationFinding> findings = PatientValidator.validateTreatmentResponses(CPCT_ID, Lists.newArrayList(TREATMENT_JAN_MAR), Lists.newArrayList(matchedResponseFeb2015));
    assertEquals(1, findings.size());
    findings.stream().map(ValidationFinding::patientId).forEach(id -> assertEquals(CPCT_ID, id));
    final List<String> findingsFields = findings.stream().map(ValidationFinding::ecrfItem).collect(Collectors.toList());
    assertTrue(findingsFields.contains(fields(FORM_TREATMENT, FORM_TUMOR_MEASUREMENT)));
}
Also used : ImmutableBiopsyTreatmentResponseData(com.hartwig.hmftools.patientdb.data.ImmutableBiopsyTreatmentResponseData) BiopsyTreatmentResponseData(com.hartwig.hmftools.patientdb.data.BiopsyTreatmentResponseData) ValidationFinding(com.hartwig.hmftools.common.ecrf.datamodel.ValidationFinding) Test(org.junit.Test)

Example 13 with ValidationFinding

use of com.hartwig.hmftools.common.ecrf.datamodel.ValidationFinding in project hmftools by hartwigmedical.

the class LoadClinicalData method writeClinicalData.

private static void writeClinicalData(@NotNull final Options clinicalOptions, @NotNull final CommandLine cmd, @NotNull final List<RunContext> runContexts, @NotNull final DatabaseAccess dbAccess) throws IOException, XMLStreamException {
    final String ecrfFilePath = cmd.getOptionValue(ECRF_FILE);
    final String limsJson = cmd.getOptionValue(LIMS_JSON);
    final String preLIMSArrivalDatesCsv = cmd.getOptionValue(PRE_LIMS_ARRIVAL_DATES_CSV);
    final String formStatusCsv = cmd.getOptionValue(FORM_STATUS_CSV);
    final String csvOutputDir = cmd.getOptionValue(CSV_OUT_DIR);
    final Optional<String> cancerTypesLink = Optional.ofNullable(cmd.getOptionValue(CANCER_TYPES_LINK));
    final Optional<String> portalDataLink = Optional.ofNullable(cmd.getOptionValue(PORTAL_DATA_LINK));
    if (Utils.anyNull(ecrfFilePath, limsJson, preLIMSArrivalDatesCsv, formStatusCsv, csvOutputDir)) {
        final HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("patient-db", clinicalOptions);
    } else {
        LOGGER.info("Clearing database...");
        dbAccess.clearClinicalTables();
        LOGGER.info("Cleared database, loading samples from LIMS...");
        Lims lims = LimsFactory.fromLimsJsonWithPreLIMSArrivalDates(limsJson, preLIMSArrivalDatesCsv);
        Map<String, List<SampleData>> samplesPerPatient = readSamplesPerPatient(lims, runContexts);
        LOGGER.info("Loaded samples for {} patients from LIMS, loading ecrf...", samplesPerPatient.size());
        FormStatusModel formStatusModel = FormStatusReader.buildModelFromCsv(formStatusCsv);
        CpctEcrfModel ecrfModel = CpctEcrfModel.loadFromXML(ecrfFilePath, formStatusModel);
        TreatmentCurator treatmentCurator = TreatmentCurator.fromProductionResource();
        TumorLocationCurator tumorLocationCurator = TumorLocationCurator.fromProductionResource();
        PatientReader patientReader = new PatientReader(ecrfModel, treatmentCurator, tumorLocationCurator);
        final Map<String, Patient> readPatients = readEcrfPatients(patientReader, ecrfModel.patients(), samplesPerPatient);
        LOGGER.info("Loaded {} patients from ecrf", readPatients.size());
        DumpClinicalData.writeClinicalDumps(csvOutputDir, readPatients.values(), cancerTypesLink, portalDataLink);
        LOGGER.info("Writing clinical data for {} sequenced patients.", readPatients.size());
        for (final String patientIdentifier : samplesPerPatient.keySet()) {
            final Patient patient = readPatients.get(patientIdentifier);
            if (patient == null) {
                if (patientIdentifier.startsWith("CPCT")) {
                    LOGGER.error("Could not find patient with id: " + patientIdentifier + " in ecrf file.");
                }
                dbAccess.writeSampleClinicalData(patientIdentifier, samplesPerPatient.get(patientIdentifier));
            } else {
                dbAccess.writeFullClinicalData(patient);
                final List<ValidationFinding> findings = PatientValidator.validatePatient(patient);
                dbAccess.writeValidationFindings(findings);
                dbAccess.writeValidationFindings(patient.matchFindings());
            }
        }
        dbAccess.writeValidationFindings(CurationValidator.validateTreatmentCurator(treatmentCurator));
        dbAccess.writeValidationFindings(CurationValidator.validateTumorLocationCurator(tumorLocationCurator));
        LOGGER.info("Done!");
    }
}
Also used : FormStatusModel(com.hartwig.hmftools.common.ecrf.formstatus.FormStatusModel) TreatmentCurator(com.hartwig.hmftools.patientdb.curators.TreatmentCurator) Lims(com.hartwig.hmftools.common.lims.Lims) PatientReader(com.hartwig.hmftools.patientdb.readers.PatientReader) EcrfPatient(com.hartwig.hmftools.common.ecrf.datamodel.EcrfPatient) Patient(com.hartwig.hmftools.patientdb.data.Patient) HelpFormatter(org.apache.commons.cli.HelpFormatter) TumorLocationCurator(com.hartwig.hmftools.patientdb.curators.TumorLocationCurator) ValidationFinding(com.hartwig.hmftools.common.ecrf.datamodel.ValidationFinding) List(java.util.List) CpctEcrfModel(com.hartwig.hmftools.common.ecrf.CpctEcrfModel)

Aggregations

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