1 package org.broadinstitute.hellbender.tools.walkers.genotyper;
2 
3 import htsjdk.variant.variantcontext.Allele;
4 import htsjdk.variant.variantcontext.GenotypeLikelihoods;
5 import org.broadinstitute.hellbender.utils.Utils;
6 import org.broadinstitute.hellbender.utils.genotyper.AlleleList;
7 import org.broadinstitute.hellbender.utils.genotyper.SampleList;
8 
9 import java.util.List;
10 
11 /**
12  * Genotyping Likelihoods collection.
13  */
14 public final class GenotypingLikelihoods<A extends Allele> implements SampleList, AlleleList<A> {
15 
16     private final GenotypeLikelihoods[] likelihoods;
17 
18     private final PloidyModel ploidyModel;
19 
20     private final AlleleList<A> alleles;
21 
22     /**
23      * Creates a new genotyping-likelihoods collection given the genotype alleles, the sample ploidy model and the
24      *   likelihoods themselves.
25      * <p>
26      * Notice that this constructor does not check whether the likelihood array lengths corresponds to the sample plodies and
27      * number of alleles.
28      * </p>
29      *
30      * @param alleles the genotyping alleles.
31      * @param ploidyModel the ploidy model.
32      * @param likelihoods the actual genotype likelihoods, one element per sample.
33      *
34      * @throws IllegalArgumentException if any argument is {@code null}, or the number of samples in {@code ploidyModel}
35      *  does not correspond with the number of likelihoods arrays in {@code likelihoods}
36      */
GenotypingLikelihoods(final AlleleList<A> alleles, final PloidyModel ploidyModel, final List<GenotypeLikelihoods> likelihoods)37     GenotypingLikelihoods(final AlleleList<A> alleles, final PloidyModel ploidyModel, final List<GenotypeLikelihoods> likelihoods) {
38         Utils.validateArg (ploidyModel.numberOfSamples() ==  likelihoods.size(), "there must be exactly one likelihood set for each sample");
39         this.likelihoods = Utils.nonNull(likelihoods, "the likelihood collection cannot be null").toArray(new GenotypeLikelihoods[likelihoods.size()]);
40         Utils.containsNoNull(likelihoods, "no genotype likelihood is allowed to be null");
41         this.alleles = Utils.nonNull(alleles, "allele list cannot be null");
42         this.ploidyModel = Utils.nonNull(ploidyModel, "the ploidy model cannot be null");
43     }
44 
45     @Override
numberOfSamples()46     public int numberOfSamples() {
47         return ploidyModel.numberOfSamples();
48     }
49 
50     @Override
indexOfSample(final String sample)51     public int indexOfSample(final String sample) {
52         return ploidyModel.indexOfSample(sample);
53     }
54 
55     @Override
getSample(final int sampleIndex)56     public String getSample(final int sampleIndex) {
57         return ploidyModel.getSample(sampleIndex);
58     }
59 
60     /**
61      * Returns the ploidy of the sample given its index in the collection.
62      *
63      * @param sampleIndex the query sample index.
64      *
65      * @throws IllegalArgumentException if {@code sampleIndex} is not a valid index for this collection:
66      *   [0,{@link #numberOfSamples()).
67      *
68      * @return 0 or greater.
69      */
samplePloidy(final int sampleIndex)70     public int samplePloidy(final int sampleIndex) {
71         Utils.validIndex(sampleIndex, numberOfSamples());
72         return ploidyModel.samplePloidy(sampleIndex);
73     }
74 
75     /**
76      * Returns the genotype-likelihoods of the sample given its index in the collection.
77      *
78      * @param sampleIndex the query sample index.
79      *
80      * @throws IllegalArgumentException if {@code sampleIndex} is not a valid index for this collection:
81      *   [0,{@link #numberOfSamples()).
82      *
83      * @return never {@code null}.
84      */
sampleLikelihoods(final int sampleIndex)85     public GenotypeLikelihoods sampleLikelihoods(final int sampleIndex) {
86         Utils.validIndex(sampleIndex, numberOfSamples());
87         return likelihoods[sampleIndex];
88     }
89 
90     @Override
numberOfAlleles()91     public int numberOfAlleles() {
92         return alleles.numberOfAlleles();
93     }
94 
95     @Override
indexOfAllele(final A allele)96     public int indexOfAllele(final A allele) {
97         Utils.nonNull(allele);
98         return alleles.indexOfAllele(allele);
99     }
100 
101     @Override
getAllele(final int index)102     public A getAllele(final int index) {
103         Utils.validIndex(index, numberOfAlleles());
104         return alleles.getAllele(index);
105     }
106 }
107