1 package org.broadinstitute.hellbender.tools.walkers.annotator;
2 
3 import htsjdk.variant.variantcontext.Allele;
4 import htsjdk.variant.variantcontext.Genotype;
5 import htsjdk.variant.variantcontext.VariantContext;
6 import org.broadinstitute.barclay.help.DocumentedFeature;
7 import org.broadinstitute.hellbender.engine.ReferenceContext;
8 import org.broadinstitute.hellbender.utils.Utils;
9 import org.broadinstitute.hellbender.utils.genotyper.AlleleLikelihoods;
10 import org.broadinstitute.hellbender.utils.help.HelpConstants;
11 import org.broadinstitute.hellbender.utils.read.GATKRead;
12 import org.broadinstitute.hellbender.utils.variant.GATKVCFConstants;
13 
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Map;
17 
18 /**
19  * List samples that are non-reference at a given variant site
20  *
21  * <p>The output is a list of the samples that are genotyped as having one or more variant alleles. This allows you to
22  * easily determine which samples are non-reference (heterozygous or homozygous-variant) and compare them to samples
23  * that are homozygous-reference. This annotation is particularly useful for large cohorts where the genotype fields are
24  * difficult to manually match with sample names.</p>
25  */
26 @DocumentedFeature(groupName=HelpConstants.DOC_CAT_ANNOTATORS, groupSummary=HelpConstants.DOC_CAT_ANNOTATORS_SUMMARY, summary="List of samples that are not homozygous reference at a variant site (Samples)")
27 public final class SampleList extends InfoFieldAnnotation {
28 
29     @Override
annotate(final ReferenceContext ref, final VariantContext vc, final AlleleLikelihoods<GATKRead, Allele> likelihoods)30     public Map<String, Object> annotate(final ReferenceContext ref,
31                                         final VariantContext vc,
32                                         final AlleleLikelihoods<GATKRead, Allele> likelihoods) {
33         Utils.nonNull(vc);
34         if ( vc.isMonomorphicInSamples() || !vc.hasGenotypes() ) {
35             return Collections.emptyMap();
36         }
37 
38         final StringBuilder samples = new StringBuilder();
39         for ( final Genotype genotype : vc.getGenotypesOrderedByName() ) {
40             if ( genotype.isCalled() && !genotype.isHomRef() ){
41                 if ( samples.length() > 0 ) {
42                     samples.append(",");
43                 }
44                 samples.append(genotype.getSampleName());
45             }
46         }
47 
48         if ( samples.length() == 0 ) {
49             return Collections.emptyMap();
50         }
51 
52         return Collections.singletonMap(getKeyNames().get(0), samples.toString());
53     }
54 
55     @Override
getKeyNames()56     public List<String> getKeyNames() { return Collections.singletonList(GATKVCFConstants.SAMPLE_LIST_KEY); }
57 }
58