Search in sources :

Example 1 with MetadataEntry

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

the class ProjectSampleMetadataController method saveProjectSampleMetadata.

/**
 * Save uploaded metadata to the
 *
 * @param locale
 * 		{@link Locale} of the current user.
 * @param session
 * 		{@link HttpSession}
 * @param projectId
 * 		{@link Long} identifier for the current project
 *
 * @return {@link Map} of potential errors.
 */
@RequestMapping(value = "/upload/save", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> saveProjectSampleMetadata(Locale locale, HttpSession session, @PathVariable long projectId) {
    Map<String, Object> errors = new HashMap<>();
    Project project = projectService.read(projectId);
    SampleMetadataStorage stored = (SampleMetadataStorage) session.getAttribute("pm-" + projectId);
    if (stored == null) {
        errors.put("stored-error", true);
    }
    List<Sample> samplesToUpdate = new ArrayList<>();
    List<Map<String, String>> found = stored.getFound();
    if (found != null) {
        // Lets try to get a sample
        String sampleNameColumn = stored.getSampleNameColumn();
        List<String> errorList = new ArrayList<>();
        try {
            for (Map<String, String> row : found) {
                String name = row.get(sampleNameColumn);
                Sample sample = sampleService.getSampleBySampleName(project, name);
                row.remove(sampleNameColumn);
                Map<MetadataTemplateField, MetadataEntry> newData = new HashMap<>();
                // Need to overwrite duplicate keys
                for (Entry<String, String> entry : row.entrySet()) {
                    MetadataTemplateField key = metadataTemplateService.readMetadataFieldByLabel(entry.getKey());
                    if (key == null) {
                        key = metadataTemplateService.saveMetadataField(new MetadataTemplateField(entry.getKey(), "text"));
                    }
                    newData.put(key, new MetadataEntry(entry.getValue(), "text"));
                }
                sample.mergeMetadata(newData);
                // Save metadata back to the sample
                samplesToUpdate.add(sample);
            }
            sampleService.updateMultiple(samplesToUpdate);
        } catch (EntityNotFoundException e) {
            // This really should not happen, but hey, you never know!
            errorList.add(messageSource.getMessage("metadata.results.save.sample-not-found", new Object[] { e.getMessage() }, locale));
        }
        if (errorList.size() > 0) {
            errors.put("save-errors", errorList);
        }
    } else {
        errors.put("found-error", messageSource.getMessage("metadata.results.save.found-error", new Object[] {}, locale));
    }
    if (errors.size() == 0) {
        return ImmutableMap.of("success", messageSource.getMessage("metadata.results.save.success", new Object[] { found.size() }, locale));
    }
    return errors;
}
Also used : Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) Project(ca.corefacility.bioinformatics.irida.model.project.Project) SampleMetadataStorage(ca.corefacility.bioinformatics.irida.ria.utilities.SampleMetadataStorage) MetadataEntry(ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry) MetadataTemplateField(ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with MetadataEntry

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

the class Sample method mergeMetadata.

/**
 * Merge {@link MetadataEntry} into the sample's existing metadata collection. Duplicate
 * keys will be overwritten.
 *
 * @param inputMetadata
 *            the metadata to merge into the sample
 */
public void mergeMetadata(Map<MetadataTemplateField, MetadataEntry> inputMetadata) {
    // loop through entry set and see if it already exists
    for (Entry<MetadataTemplateField, MetadataEntry> entry : inputMetadata.entrySet()) {
        // if the value isn't empty
        if (!entry.getValue().getValue().isEmpty()) {
            // if the key is found, replace the entry
            if (metadata.containsKey(entry.getKey())) {
                MetadataEntry metadataEntry = metadata.get(entry.getKey());
                metadataEntry.setValue(entry.getValue().getValue());
            } else {
                // otherwise add the new entry
                metadata.put(entry.getKey(), entry.getValue());
            }
        }
    }
}
Also used : MetadataEntry(ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry)

Example 3 with MetadataEntry

use of ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry 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 4 with MetadataEntry

use of ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry 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 5 with MetadataEntry

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

Aggregations

MetadataEntry (ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry)11 MetadataTemplateField (ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField)9 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)6 ImmutableMap (com.google.common.collect.ImmutableMap)4 Project (ca.corefacility.bioinformatics.irida.model.project.Project)2 AnalysisOutputFile (ca.corefacility.bioinformatics.irida.model.workflow.analysis.AnalysisOutputFile)2 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 EntityNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)1 PostProcessingException (ca.corefacility.bioinformatics.irida.exceptions.PostProcessingException)1 RemoteAPI (ca.corefacility.bioinformatics.irida.model.RemoteAPI)1 Join (ca.corefacility.bioinformatics.irida.model.joins.Join)1 ProjectMetadataTemplateJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectMetadataTemplateJoin)1 ListResourceWrapper (ca.corefacility.bioinformatics.irida.model.remote.resource.ListResourceWrapper)1 ResourceWrapper (ca.corefacility.bioinformatics.irida.model.remote.resource.ResourceWrapper)1 PipelineProvidedMetadataEntry (ca.corefacility.bioinformatics.irida.model.sample.metadata.PipelineProvidedMetadataEntry)1 SequencingObject (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject)1