Search in sources :

Example 6 with MetadataTemplateField

use of ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField in project irida by phac-nml.

the class SISTRSampleUpdaterTest method testUpdaterPassed.

@SuppressWarnings("unchecked")
@Test
public void testUpdaterPassed() throws PostProcessingException, AnalysisAlreadySetException {
    ImmutableMap<String, String> expectedResults = ImmutableMap.of("SISTR serovar", "Enteritidis", "SISTR cgMLST Subspecies", "enterica", "SISTR QC Status", "PASS");
    Path outputPath = Paths.get("src/test/resources/files/sistr-predictions-pass.json");
    AnalysisOutputFile outputFile = new AnalysisOutputFile(outputPath, null, null, null);
    Analysis analysis = new Analysis(null, ImmutableMap.of("sistr-predictions", outputFile), null, null);
    AnalysisSubmission submission = AnalysisSubmission.builder(UUID.randomUUID()).inputFiles(ImmutableSet.of(new SingleEndSequenceFile(null))).build();
    submission.setAnalysis(analysis);
    Sample sample = new Sample();
    sample.setId(1L);
    ImmutableMap<MetadataTemplateField, MetadataEntry> metadataMap = ImmutableMap.of(new MetadataTemplateField("SISTR Field", "text"), new MetadataEntry("Value1", "text"));
    when(metadataTemplateService.getMetadataMap(any(Map.class))).thenReturn(metadataMap);
    updater.update(Lists.newArrayList(sample), submission);
    ArgumentCaptor<Map> mapCaptor = ArgumentCaptor.forClass(Map.class);
    // this is the important bit.  Ensures the correct values got pulled from the file
    verify(metadataTemplateService).getMetadataMap(mapCaptor.capture());
    Map<String, MetadataEntry> metadata = mapCaptor.getValue();
    int found = 0;
    for (Map.Entry<String, MetadataEntry> e : metadata.entrySet()) {
        if (expectedResults.containsKey(e.getKey())) {
            String expected = expectedResults.get(e.getKey());
            MetadataEntry value = e.getValue();
            assertEquals("metadata values should match", expected, value.getValue());
            found++;
        }
    }
    assertEquals("should have found the same number of results", expectedResults.keySet().size(), found);
    // this bit just ensures the merged data got saved
    verify(sampleService).updateFields(eq(sample.getId()), mapCaptor.capture());
    Map<MetadataTemplateField, MetadataEntry> value = (Map<MetadataTemplateField, MetadataEntry>) mapCaptor.getValue().get("metadata");
    assertEquals(metadataMap.keySet().iterator().next(), value.keySet().iterator().next());
}
Also used : Path(java.nio.file.Path) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) AnalysisSubmission(ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) Analysis(ca.corefacility.bioinformatics.irida.model.workflow.analysis.Analysis) MetadataEntry(ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry) MetadataTemplateField(ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField) AnalysisOutputFile(ca.corefacility.bioinformatics.irida.model.workflow.analysis.AnalysisOutputFile) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 7 with MetadataTemplateField

use of ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField in project irida by phac-nml.

the class RESTSampleMetadataController method saveSampleMetadata.

/**
 * Save new metadata for a {@link Sample}. Note this will overwrite the
 * existing metadata
 *
 * @param sampleId
 *            the id of the {@link Sample} to save new metadata
 * @param metadataMap
 *            the metadata to save to the {@link Sample}
 * @return the updated {@link Sample}
 */
@RequestMapping(value = "/api/samples/{sampleId}/metadata", method = RequestMethod.POST)
public ModelMap saveSampleMetadata(@PathVariable Long sampleId, @RequestBody Map<String, MetadataEntry> metadataMap) {
    Sample s = sampleService.read(sampleId);
    Map<MetadataTemplateField, MetadataEntry> metadata = metadataTemplateService.getMetadataMap(metadataMap);
    s.setMetadata(metadata);
    sampleService.update(s);
    return getSampleMetadata(sampleId);
}
Also used : Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) MetadataEntry(ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry) MetadataTemplateField(ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with MetadataTemplateField

use of ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField in project irida by phac-nml.

the class RESTSampleMetadataController method addSampleMetadata.

/**
 * Add select new metadata fields to the {@link Sample}. Note this will only
 * overwrite duplicate terms. Existing metadata will not be affected.
 *
 * @param sampleId
 *            the {@link Sample} to add metadata to
 * @param metadataMap
 *            the new metadata
 * @return the updated {@link Sample}
 */
@RequestMapping(value = "/api/samples/{sampleId}/metadata", method = RequestMethod.PUT)
public ModelMap addSampleMetadata(@PathVariable Long sampleId, @RequestBody Map<String, MetadataEntry> metadataMap) {
    Sample s = sampleService.read(sampleId);
    Map<MetadataTemplateField, MetadataEntry> metadata = metadataTemplateService.getMetadataMap(metadataMap);
    s.mergeMetadata(metadata);
    sampleService.update(s);
    return getSampleMetadata(sampleId);
}
Also used : Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) MetadataEntry(ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry) MetadataTemplateField(ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with MetadataTemplateField

use of ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField in project irida by phac-nml.

the class MetadataTemplateServiceImplIT method testGetMetadataHeadersForProject.

@Test
@WithMockUser(username = "mrtest", roles = "ADMIN")
public void testGetMetadataHeadersForProject() {
    // check project with 6 fields
    Project project = projectService.read(1L);
    List<MetadataTemplateField> metadataFieldsForProject = metadataTemplateService.getMetadataFieldsForProject(project);
    assertEquals(5, metadataFieldsForProject.size());
    Set<String> fields = Sets.newHashSet();
    fields.add("firstName");
    fields.add("lastName");
    fields.add("healthAuthority");
    fields.add("firstSymptom");
    fields.add("serotype");
    for (MetadataTemplateField metadataTemplateField : metadataFieldsForProject) {
        assertTrue("should contain field", fields.contains(metadataTemplateField.getLabel()));
        fields.remove(metadataTemplateField.getLabel());
    }
    assertTrue("should have found all fields", fields.isEmpty());
    // check a project with 1 field
    project = projectService.read(2L);
    metadataFieldsForProject = metadataTemplateService.getMetadataFieldsForProject(project);
    assertEquals(1, metadataFieldsForProject.size());
    MetadataTemplateField field = metadataFieldsForProject.iterator().next();
    assertEquals("firstName", field.getLabel());
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) MetadataTemplateField(ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test)

Example 10 with MetadataTemplateField

use of ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField 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

MetadataTemplateField (ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField)16 MetadataEntry (ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry)9 Project (ca.corefacility.bioinformatics.irida.model.project.Project)7 MetadataTemplate (ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplate)6 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)6 ProjectMetadataTemplateJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectMetadataTemplateJoin)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 AnalysisOutputFile (ca.corefacility.bioinformatics.irida.model.workflow.analysis.AnalysisOutputFile)2 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)2 UIMetadataTemplate (ca.corefacility.bioinformatics.irida.ria.web.models.UIMetadataTemplate)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Test (org.junit.Test)2 EntityNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)1 PostProcessingException (ca.corefacility.bioinformatics.irida.exceptions.PostProcessingException)1 Join (ca.corefacility.bioinformatics.irida.model.joins.Join)1 PipelineProvidedMetadataEntry (ca.corefacility.bioinformatics.irida.model.sample.metadata.PipelineProvidedMetadataEntry)1 SequencingObject (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject)1