Search in sources :

Example 1 with PipelineProvidedMetadataEntry

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);
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) PipelineProvidedMetadataEntry(ca.corefacility.bioinformatics.irida.model.sample.metadata.PipelineProvidedMetadataEntry) PostProcessingException(ca.corefacility.bioinformatics.irida.exceptions.PostProcessingException) BufferedReader(java.io.BufferedReader) PipelineProvidedMetadataEntry(ca.corefacility.bioinformatics.irida.model.sample.metadata.PipelineProvidedMetadataEntry) MetadataEntry(ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry) FileReader(java.io.FileReader) MetadataTemplateField(ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField) AnalysisOutputFile(ca.corefacility.bioinformatics.irida.model.workflow.analysis.AnalysisOutputFile) ImmutableMap(com.google.common.collect.ImmutableMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

PostProcessingException (ca.corefacility.bioinformatics.irida.exceptions.PostProcessingException)1 MetadataTemplateField (ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField)1 MetadataEntry (ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry)1 PipelineProvidedMetadataEntry (ca.corefacility.bioinformatics.irida.model.sample.metadata.PipelineProvidedMetadataEntry)1 AnalysisOutputFile (ca.corefacility.bioinformatics.irida.model.workflow.analysis.AnalysisOutputFile)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1