Search in sources :

Example 11 with EcrfPatient

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

the class XMLPatientReader method readPatient.

@NotNull
private static EcrfPatient readPatient(@NotNull final XMLStreamReader reader, @NotNull final XMLEcrfDatamodel datamodel, @NotNull final FormStatusModel formStatusModel) throws XMLStreamException {
    final String patientId = toCPCTPatientId(reader.getAttributeValue("", PATIENT_ID_ATTRIBUTE));
    final Map<String, List<EcrfStudyEvent>> studyEventsPerOID = Maps.newHashMap();
    final List<EcrfDataField> fields = Lists.newArrayList();
    String currentStudyEventOID = Strings.EMPTY;
    String currentStudyEventIdx = Strings.EMPTY;
    String currentFormOID = Strings.EMPTY;
    String currentFormIdx = Strings.EMPTY;
    String currentItemGroupOID = Strings.EMPTY;
    String currentItemGroupIdx = Strings.EMPTY;
    EcrfStudyEvent currentStudy = new EcrfStudyEvent(patientId);
    EcrfForm currentForm = new EcrfForm(patientId, FormStatusState.UNKNOWN, false);
    EcrfItemGroup currentItemGroup = new EcrfItemGroup(patientId);
    while (reader.hasNext() && !isPatientEnd(reader)) {
        if (isStudyEventStart(reader)) {
            currentStudyEventOID = reader.getAttributeValue("", STUDY_EVENT_OID_ATTRIBUTE);
            currentStudyEventIdx = reader.getAttributeValue("", STUDY_EVENT_REPEAT_KEY_ATTRIBUTE);
            currentStudy = new EcrfStudyEvent(patientId);
            if (!studyEventsPerOID.containsKey(currentStudyEventOID)) {
                studyEventsPerOID.put(currentStudyEventOID, Lists.newArrayList());
            }
            studyEventsPerOID.get(currentStudyEventOID).add(currentStudy);
        } else if (isFormStart(reader)) {
            currentFormOID = reader.getAttributeValue("", FORM_OID_ATTRIBUTE);
            currentFormIdx = reader.getAttributeValue("", FORM_REPEAT_KEY_ATTRIBUTE);
            final String formName = datamodel.forms().get(currentFormOID).name();
            final String studyEventName = datamodel.studyEvents().get(currentStudyEventOID).name();
            final FormStatusKey formKey = new ImmutableFormStatusKey(patientId, formName, currentFormIdx, studyEventName, currentStudyEventIdx);
            final FormStatusData formStatus = formStatusModel.formStatuses().get(formKey);
            if (formStatus != null) {
                currentForm = new EcrfForm(patientId, formStatus.state(), formStatus.locked());
            } else {
                currentForm = new EcrfForm(patientId, FormStatusState.UNKNOWN, false);
            }
            currentStudy.addForm(currentFormOID, currentForm);
        } else if (isItemGroupStart(reader)) {
            currentItemGroupOID = reader.getAttributeValue("", ITEM_GROUP_OID_ATTRIBUTE);
            currentItemGroupIdx = reader.getAttributeValue("", ITEM_GROUP_REPEAT_KEY);
            currentItemGroup = new EcrfItemGroup(patientId);
            currentForm.addItemGroup(currentItemGroupOID, currentItemGroup);
        } else if (isFieldStart(reader)) {
            String OID = reader.getAttributeValue("", ITEM_OID_ATTRIBUTE);
            String value = Strings.EMPTY;
            try {
                value = datamodel.resolveValue(OID, reader.getAttributeValue("", ITEM_VALUE_ATTRIBUTE));
            } catch (EcrfResolveException exception) {
                LOGGER.warn("Resolve issue for " + patientId + ": " + exception.getMessage());
            }
            currentItemGroup.addItem(OID, value);
            fields.add(EcrfDataField.of(patientId, currentStudyEventOID, currentStudyEventIdx, currentFormOID, currentFormIdx, currentItemGroupOID, currentItemGroupIdx, OID, value, currentForm.status().stateString(), Boolean.toString(currentForm.locked())));
        }
        reader.next();
    }
    return new EcrfPatient(patientId, studyEventsPerOID, fields);
}
Also used : FormStatusData(com.hartwig.hmftools.common.ecrf.formstatus.FormStatusData) EcrfItemGroup(com.hartwig.hmftools.common.ecrf.datamodel.EcrfItemGroup) ImmutableFormStatusKey(com.hartwig.hmftools.common.ecrf.formstatus.ImmutableFormStatusKey) FormStatusKey(com.hartwig.hmftools.common.ecrf.formstatus.FormStatusKey) ImmutableFormStatusKey(com.hartwig.hmftools.common.ecrf.formstatus.ImmutableFormStatusKey) EcrfResolveException(com.hartwig.hmftools.common.ecrf.datamodel.EcrfResolveException) EcrfStudyEvent(com.hartwig.hmftools.common.ecrf.datamodel.EcrfStudyEvent) EcrfForm(com.hartwig.hmftools.common.ecrf.datamodel.EcrfForm) List(java.util.List) EcrfPatient(com.hartwig.hmftools.common.ecrf.datamodel.EcrfPatient) EcrfDataField(com.hartwig.hmftools.common.ecrf.datamodel.EcrfDataField) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with EcrfPatient

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

the class CpctEcrfModel method findPatientsById.

@NotNull
public Iterable<EcrfPatient> findPatientsById(@NotNull final Iterable<String> patientIds) {
    final List<EcrfPatient> filteredPatients = Lists.newArrayList();
    for (final String patientId : patientIds) {
        final EcrfPatient patient = findPatientById(patientId);
        if (patient != null) {
            filteredPatients.add(patient);
        } else {
            LOGGER.warn("Did not find patient " + patientId + ": Adding dummy patient.");
            filteredPatients.add(new EcrfPatient(patientId, Maps.newHashMap(), Lists.newArrayList()));
        }
    }
    return filteredPatients;
}
Also used : EcrfPatient(com.hartwig.hmftools.common.ecrf.datamodel.EcrfPatient) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with EcrfPatient

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

the class LoadClinicalData method readEcrfPatients.

@NotNull
private static Map<String, Patient> readEcrfPatients(@NotNull final PatientReader reader, @NotNull final Iterable<EcrfPatient> patients, @NotNull final Map<String, List<SampleData>> samplesPerPatient) throws IOException {
    final Map<String, Patient> patientMap = Maps.newHashMap();
    for (final EcrfPatient ecrfPatient : patients) {
        List<SampleData> samples = samplesPerPatient.get(ecrfPatient.patientId());
        if (samples != null && samples.size() > 0) {
            final Patient patient = reader.read(ecrfPatient, samples);
            patientMap.put(patient.patientIdentifier(), patient);
        }
    }
    return patientMap;
}
Also used : SampleData(com.hartwig.hmftools.patientdb.data.SampleData) EcrfPatient(com.hartwig.hmftools.common.ecrf.datamodel.EcrfPatient) Patient(com.hartwig.hmftools.patientdb.data.Patient) EcrfPatient(com.hartwig.hmftools.common.ecrf.datamodel.EcrfPatient) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with EcrfPatient

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

the class XMLPatientReaderTest method canReadPatients.

@Test
public void canReadPatients() throws FileNotFoundException, XMLStreamException {
    final XMLInputFactory factory = XMLInputFactory.newInstance();
    final XMLStreamReader reader = factory.createXMLStreamReader(new FileInputStream(PATIENTS_TEST));
    final String studyOID = "SE.Study";
    final String formOID = "FRM.Form";
    final String itemGroupOID = "GRP.ItemGroup";
    final Map<Integer, String> codeListValues = Maps.newHashMap();
    codeListValues.put(1, "one");
    codeListValues.put(2, "two");
    codeListValues.put(3, "three");
    final String item1OID = "FLD.ItemGroup.field1";
    final String item2OID = "FLD.ItemGroup.field2";
    final String birthDateOID = "FLD.ItemGroup.BIRTHDTC";
    final String codeListOID = "codeList";
    final StudyEvent studyEvent = new ImmutableStudyEvent(studyOID, studyOID, Lists.newArrayList(formOID));
    final Form form = new ImmutableForm(formOID, formOID, Lists.newArrayList(itemGroupOID));
    final ItemGroup itemGroup = new ImmutableItemGroup(itemGroupOID, itemGroupOID, Lists.newArrayList(item1OID, item2OID, birthDateOID));
    final Item item1 = new ImmutableItem(item1OID, item1OID, codeListOID);
    final Item item2 = new ImmutableItem(item2OID, item2OID, "");
    final Item birthDate = new ImmutableItem(birthDateOID, birthDateOID, "");
    final CodeList codeList = new ImmutableCodeList(codeListOID, codeListOID, codeListValues);
    final List<EcrfPatient> patients = XMLPatientReader.readPatients(reader, XMLEcrfDatamodel.of(Lists.newArrayList(studyEvent), Lists.newArrayList(form), Lists.newArrayList(itemGroup), Lists.newArrayList(item1, item2, birthDate), Lists.newArrayList(codeList)), new ImmutableFormStatusModel(Maps.newHashMap()));
    final EcrfDatamodelField field1 = new ImmutableEcrfDatamodelField(studyOID, formOID, itemGroupOID, "FLD.ItemGroup.field1", "", codeListValues);
    final EcrfDatamodelField field2 = new ImmutableEcrfDatamodelField(studyOID, formOID, itemGroupOID, "FLD.ItemGroup.field2", "", Maps.newHashMap());
    final EcrfDatamodelField birthDateField = new ImmutableEcrfDatamodelField(studyOID, formOID, itemGroupOID, "FLD.ItemGroup.BIRTHDTC", "", Maps.newHashMap());
    // @formatter:off
    assertEquals(3, patients.size());
    assertEquals(4, patients.get(0).fields().size());
    assertEquals(PATIENT_1, patients.get(0).patientId());
    verifyFirstFieldValue("one", patients.get(0).fieldValuesByEcrfField(field1));
    verifyFirstFieldValue("hi", patients.get(0).fieldValuesByEcrfField(field2));
    verifyFirstFieldValue("2016-01-01", patients.get(0).fieldValuesByEcrfField(birthDateField));
    verifyFirstFieldValue("one", patients.get(0).studyEventsPerOID(studyOID).get(0).formsPerOID().get(formOID).get(0).itemGroupsPerOID().get(itemGroupOID).get(0).itemsPerOID().get(item1OID));
    verifyFirstFieldValue("hi", patients.get(0).studyEventsPerOID(studyOID).get(0).formsPerOID().get(formOID).get(0).itemGroupsPerOID().get(itemGroupOID).get(0).itemsPerOID().get(item2OID));
    verifyFirstFieldValue("2016-01-01", patients.get(0).studyEventsPerOID(studyOID).get(0).formsPerOID().get(formOID).get(0).itemGroupsPerOID().get(itemGroupOID).get(0).itemsPerOID().get(birthDateOID));
    assertEquals(1, patients.get(0).studyEventsPerOID().size());
    assertEquals(1, patients.get(0).studyEventsPerOID().get(studyOID).size());
    assertEquals(1, patients.get(0).studyEventsPerOID().get(studyOID).get(0).formsPerOID().get(formOID).size());
    assertEquals(1, patients.get(0).studyEventsPerOID().get(studyOID).get(0).formsPerOID().get(formOID).get(0).itemGroupsPerOID().get(itemGroupOID).size());
    assertEquals(4, patients.get(0).studyEventsPerOID().get(studyOID).get(0).formsPerOID().get(formOID).get(0).itemGroupsPerOID().get(itemGroupOID).get(0).itemsPerOID().size());
    assertEquals(4, patients.get(1).fields().size());
    assertEquals(PATIENT_2, patients.get(1).patientId());
    verifyFirstFieldValue("two", patients.get(1).studyEventsPerOID(studyOID).get(0).formsPerOID().get(formOID).get(0).itemGroupsPerOID().get(itemGroupOID).get(0).itemsPerOID().get(item1OID));
    verifyFirstFieldValue("hi there", patients.get(1).studyEventsPerOID(studyOID).get(0).formsPerOID().get(formOID).get(0).itemGroupsPerOID().get(itemGroupOID).get(0).itemsPerOID().get(item2OID));
    verifyFirstFieldValue("2016-01-01", patients.get(1).studyEventsPerOID(studyOID).get(0).formsPerOID().get(formOID).get(0).itemGroupsPerOID().get(itemGroupOID).get(0).itemsPerOID().get(birthDateOID));
    assertEquals(1, patients.get(1).studyEventsPerOID().size());
    assertEquals(1, patients.get(1).studyEventsPerOID().get(studyOID).size());
    assertEquals(1, patients.get(1).studyEventsPerOID().get(studyOID).get(0).formsPerOID().get(formOID).size());
    assertEquals(1, patients.get(1).studyEventsPerOID().get(studyOID).get(0).formsPerOID().get(formOID).get(0).itemGroupsPerOID().get(itemGroupOID).size());
    assertEquals(4, patients.get(1).studyEventsPerOID().get(studyOID).get(0).formsPerOID().get(formOID).get(0).itemGroupsPerOID().get(itemGroupOID).get(0).itemsPerOID().size());
// @formatter:on
}
Also used : ImmutableEcrfDatamodelField(com.hartwig.hmftools.common.ecrf.datamodel.ImmutableEcrfDatamodelField) EcrfDatamodelField(com.hartwig.hmftools.common.ecrf.datamodel.EcrfDatamodelField) XMLStreamReader(javax.xml.stream.XMLStreamReader) ImmutableEcrfDatamodelField(com.hartwig.hmftools.common.ecrf.datamodel.ImmutableEcrfDatamodelField) FileInputStream(java.io.FileInputStream) EcrfPatient(com.hartwig.hmftools.common.ecrf.datamodel.EcrfPatient) ImmutableFormStatusModel(com.hartwig.hmftools.common.ecrf.formstatus.ImmutableFormStatusModel) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Example 15 with EcrfPatient

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

the class XMLPatientReaderTest method determinesEmptyItemGroupAndForm.

@Test
public void determinesEmptyItemGroupAndForm() throws FileNotFoundException, XMLStreamException {
    final XMLInputFactory factory = XMLInputFactory.newInstance();
    final XMLStreamReader reader = factory.createXMLStreamReader(new FileInputStream(PATIENTS_TEST));
    final String studyOID = "SE.Study";
    final String formOID = "FRM.Form";
    final String itemGroupOID = "GRP.ItemGroup";
    final StudyEvent studyEvent = new ImmutableStudyEvent(studyOID, studyOID, Lists.newArrayList(formOID));
    final Form form = new ImmutableForm(formOID, formOID, Lists.newArrayList(itemGroupOID));
    final List<EcrfPatient> patients = XMLPatientReader.readPatients(reader, XMLEcrfDatamodel.of(Lists.newArrayList(studyEvent), Lists.newArrayList(form), Lists.newArrayList(), Lists.newArrayList(), Lists.newArrayList()), new ImmutableFormStatusModel(Maps.newHashMap()));
    // @formatter:off
    assertEquals(3, patients.size());
    assertEquals(1, patients.get(2).studyEventsPerOID().size());
    assertEquals(1, patients.get(2).studyEventsPerOID().get(studyOID).size());
    assertEquals(2, patients.get(2).studyEventsPerOID().get(studyOID).get(0).formsPerOID().get(formOID).size());
    assertEquals(1, patients.get(2).studyEventsPerOID().get(studyOID).get(0).formsPerOID().get(formOID).get(0).itemGroupsPerOID().get(itemGroupOID).size());
    assertEquals(true, patients.get(2).studyEventsPerOID().get(studyOID).get(0).formsPerOID().get(formOID).get(0).itemGroupsPerOID().get(itemGroupOID).get(0).isEmpty());
    assertEquals(true, patients.get(2).studyEventsPerOID().get(studyOID).get(0).formsPerOID().get(formOID).get(0).isEmpty());
    assertEquals(2, patients.get(2).studyEventsPerOID().get(studyOID).get(0).formsPerOID().get(formOID).get(1).itemGroupsPerOID().get(itemGroupOID).size());
    assertEquals(true, patients.get(2).studyEventsPerOID().get(studyOID).get(0).formsPerOID().get(formOID).get(1).itemGroupsPerOID().get(itemGroupOID).get(0).isEmpty());
    assertEquals(false, patients.get(2).studyEventsPerOID().get(studyOID).get(0).formsPerOID().get(formOID).get(1).itemGroupsPerOID().get(itemGroupOID).get(1).isEmpty());
    assertEquals(false, patients.get(2).studyEventsPerOID().get(studyOID).get(0).formsPerOID().get(formOID).get(1).isEmpty());
// @formatter:on
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) EcrfPatient(com.hartwig.hmftools.common.ecrf.datamodel.EcrfPatient) ImmutableFormStatusModel(com.hartwig.hmftools.common.ecrf.formstatus.ImmutableFormStatusModel) XMLInputFactory(javax.xml.stream.XMLInputFactory) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

EcrfPatient (com.hartwig.hmftools.common.ecrf.datamodel.EcrfPatient)15 Test (org.junit.Test)8 NotNull (org.jetbrains.annotations.NotNull)7 CpctEcrfModel (com.hartwig.hmftools.common.ecrf.CpctEcrfModel)6 EcrfForm (com.hartwig.hmftools.common.ecrf.datamodel.EcrfForm)3 EcrfItemGroup (com.hartwig.hmftools.common.ecrf.datamodel.EcrfItemGroup)3 EcrfStudyEvent (com.hartwig.hmftools.common.ecrf.datamodel.EcrfStudyEvent)3 FileInputStream (java.io.FileInputStream)3 List (java.util.List)3 XMLInputFactory (javax.xml.stream.XMLInputFactory)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 ImmutableFormStatusModel (com.hartwig.hmftools.common.ecrf.formstatus.ImmutableFormStatusModel)2 BaselineData (com.hartwig.hmftools.patientdb.data.BaselineData)2 BiopsyData (com.hartwig.hmftools.patientdb.data.BiopsyData)2 BiopsyTreatmentData (com.hartwig.hmftools.patientdb.data.BiopsyTreatmentData)2 BiopsyTreatmentResponseData (com.hartwig.hmftools.patientdb.data.BiopsyTreatmentResponseData)2 Patient (com.hartwig.hmftools.patientdb.data.Patient)2 PreTreatmentData (com.hartwig.hmftools.patientdb.data.PreTreatmentData)2 TumorMarkerData (com.hartwig.hmftools.patientdb.data.TumorMarkerData)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2