use of ca.corefacility.bioinformatics.irida.model.sample.metadata.PipelineProvidedMetadataEntry in project irida by phac-nml.
the class SISTRSampleUpdater method update.
/**
* Add SISTR results to the metadata of the given {@link Sample}s
*
* @param samples The samples to update.
* @param analysis the {@link AnalysisSubmission} to apply to the samples
* @throws PostProcessingException if the method cannot read the "sistr-predictions" output file
*/
@Override
public void update(Collection<Sample> samples, AnalysisSubmission analysis) throws PostProcessingException {
AnalysisOutputFile sistrFile = analysis.getAnalysis().getAnalysisOutputFile(SISTR_FILE);
Path filePath = sistrFile.getFile();
Map<String, MetadataEntry> stringEntries = new HashMap<>();
try {
// Read the JSON file from SISTR output
@SuppressWarnings("resource") String jsonFile = new Scanner(new BufferedReader(new FileReader(filePath.toFile()))).useDelimiter("\\Z").next();
// map the results into a Map
ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> sistrResults = mapper.readValue(jsonFile, new TypeReference<List<Map<String, Object>>>() {
});
if (sistrResults.size() > 0) {
Map<String, Object> result = sistrResults.get(0);
// loop through each of the requested fields and save the entries
SISTR_FIELDS.entrySet().forEach(e -> {
if (result.containsKey(e.getKey()) && result.get(e.getKey()) != null) {
String value = result.get(e.getKey()).toString();
PipelineProvidedMetadataEntry metadataEntry = new PipelineProvidedMetadataEntry(value, "text", analysis);
stringEntries.put(e.getValue(), metadataEntry);
}
});
// convert string map into metadata fields
Map<MetadataTemplateField, MetadataEntry> metadataMap = metadataTemplateService.getMetadataMap(stringEntries);
// save metadata back to sample
samples.forEach(s -> {
s.mergeMetadata(metadataMap);
sampleService.updateFields(s.getId(), ImmutableMap.of("metadata", s.getMetadata()));
});
} else {
throw new PostProcessingException("SISTR results for file are not correctly formatted");
}
} catch (IOException e) {
throw new PostProcessingException("Error parsing JSON from SISTR results", e);
}
}
Aggregations