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