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