Search in sources :

Example 11 with MetadataEntry

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

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