use of ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry in project irida by phac-nml.
the class AnalysisController method getMetadataForAnalysisSamples.
/**
* Get the metadata associated with a template for an analysis.
*
* @param submissionId {@link Long} identifier for the {@link AnalysisSubmission}
* @return {@link Map}
*/
@RequestMapping("/ajax/{submissionId}/metadata")
@ResponseBody
public Map<String, Object> getMetadataForAnalysisSamples(@PathVariable Long submissionId) {
AnalysisSubmission submission = analysisSubmissionService.read(submissionId);
Collection<Sample> samples = sampleService.getSamplesForAnalysisSubmission(submission);
// Let's get a list of all the metadata available that is unique.
Set<String> terms = new HashSet<>();
for (Sample sample : samples) {
if (!sample.getMetadata().isEmpty()) {
Map<MetadataTemplateField, MetadataEntry> metadata = sample.getMetadata();
terms.addAll(metadata.keySet().stream().map(MetadataTemplateField::getLabel).collect(Collectors.toSet()));
}
}
// Get the metadata for the samples;
Map<String, Object> metadata = new HashMap<>();
for (Sample sample : samples) {
Map<MetadataTemplateField, MetadataEntry> sampleMetadata = sample.getMetadata();
Map<String, MetadataEntry> stringMetadata = new HashMap<>();
sampleMetadata.entrySet().forEach(e -> {
stringMetadata.put(e.getKey().getLabel(), e.getValue());
});
Map<String, MetadataEntry> valuesMap = new HashMap<>();
for (String term : terms) {
MetadataEntry value = stringMetadata.get(term);
if (value == null) {
// Not all samples will have the same metadata associated with it. If a sample
// is missing one of the terms, just give it an empty string.
value = new MetadataEntry("", "text");
}
valuesMap.put(term, value);
}
metadata.put(sample.getLabel(), valuesMap);
}
return ImmutableMap.of("terms", terms, "metadata", metadata);
}
Aggregations