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