Search in sources :

Example 16 with MetadataTemplateField

use of ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField 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);
}
Also used : Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) AnalysisSubmission(ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission) MetadataEntry(ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry) MetadataTemplateField(ca.corefacility.bioinformatics.irida.model.sample.MetadataTemplateField)

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