| 1 |
/* |
| 2 |
* MIT License |
| 3 |
* |
| 4 |
* Copyright 2017 Broad Institute |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and associated documentation files (the "Software"), to deal |
| 8 |
* in the Software without restriction, including without limitation the rights |
| 9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
* copies of the Software, and to permit persons to whom the Software is |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be included in all |
| 14 |
* copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
* SOFTWARE. |
| 23 |
*/ |
| 24 |
package org.broadinstitute.dropseqrna.barnyard.digitalallelecounts.sampleassignment.multisample; |
| 25 |
|
| 26 |
import htsjdk.samtools.util.Interval; |
| 27 |
import htsjdk.samtools.util.StringUtil; |
| 28 |
import htsjdk.variant.variantcontext.GenotypeType; |
| 29 |
|
| 30 |
import java.util.ArrayList; |
| 31 |
import java.util.Arrays; |
| 32 |
import java.util.List; |
| 33 |
|
| 34 |
import org.broadinstitute.dropseqrna.barnyard.digitalallelecounts.LikelihoodUtils; |
| 35 |
|
| 36 |
public class VariantData { |
| 37 |
|
| 38 |
private final Interval snpInterval; |
| 39 |
private final byte refAllele; |
| 40 |
private final byte altAllele; |
| 41 |
private final List<Byte> bases; |
| 42 |
private final List<Byte> qualities; |
| 43 |
private final List<GenotypeType> genotypes; |
| 44 |
private final Double missingDataPenality; |
| 45 |
private final Double maximumObservationProbability; |
| 46 |
private final double [] [] allLikes; |
| 47 |
private final Double minorAlleleFrequency; |
| 48 |
private final Double contamination; |
| 49 |
|
| 50 |
public VariantData(final Interval snpInterval, final byte refAllele, final byte altAllele, |
| 51 |
final GenotypeType s1, final GenotypeType s2, |
| 52 |
final List<Byte> bases, final List<Byte> qualities, final Double missingDataPenality, final Double maximumObservationProbability, |
| 53 |
final Double contamination, final Double minorAlleleFrequency) { |
| 54 |
|
| 55 |
this.snpInterval=snpInterval; |
| 56 |
this.refAllele = refAllele; |
| 57 |
this.altAllele = altAllele; |
| 58 |
this.bases = bases; |
| 59 |
this.qualities = qualities; |
| 60 |
this.genotypes = Arrays.asList(s1, s2); |
| 61 |
this.missingDataPenality=missingDataPenality; |
| 62 |
this.maximumObservationProbability=maximumObservationProbability; |
| 63 |
this.minorAlleleFrequency=minorAlleleFrequency; |
| 64 |
this.contamination=contamination; |
| 65 |
this.allLikes= precomputeLikelihoods(); |
| 66 |
} |
| 67 |
|
| 68 |
public VariantData(final Interval snpInterval, final byte refAllele, final byte altAllele, |
| 69 |
final GenotypeType s1, final GenotypeType s2, |
| 70 |
final List<Byte> bases, final List<Byte> qualities) { |
| 71 |
|
| 72 |
this(snpInterval, refAllele, altAllele, s1, s2, bases, qualities, null, null, null, null); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* A convenience constructor that uses more human-readable objects. |
| 78 |
* |
| 79 |
* @param snpInterval The genomic coordinates of the SNP |
| 80 |
* @param refAllele The reference allele of the observed SNP in the pileup |
| 81 |
* @param altAllele The alternate allele of the observed SNP in the pileup |
| 82 |
* @param s1 Genotype of sample 1 |
| 83 |
* @param s2 Genotype of sample 2 |
| 84 |
* @param bases The bases observed in the sequencing pileup |
| 85 |
* @param qualities The qualities of the bases observed in the sequencing pileup. |
| 86 |
*/ |
| 87 |
public VariantData (final Interval snpInterval, final char refAllele, final char altAllele, |
| 88 |
final GenotypeType s1, final GenotypeType s2, |
| 89 |
final char [] bases, final int [] qualities, final Double missingDataPenality, final Double maximumObservationProbability, |
| 90 |
final Double contamination, final Double minorAlleleFrequency) { |
| 91 |
|
| 92 |
if (bases.length!=qualities.length) |
| 93 |
throw new IllegalArgumentException("Number of bases and qualities are different!"); |
| 94 |
byte ref = StringUtil.charToByte(refAllele); |
| 95 |
byte alt = StringUtil.charToByte(altAllele); |
| 96 |
|
| 97 |
List<Byte> b = new ArrayList<>(); |
| 98 |
for (char base: bases) |
| 99 |
b.add(StringUtil.charToByte(base)); |
| 100 |
|
| 101 |
List<Byte> q = new ArrayList<>(); |
| 102 |
for (int qual: qualities) |
| 103 |
q.add((byte)qual); |
| 104 |
|
| 105 |
this.snpInterval=snpInterval; |
| 106 |
this.refAllele=ref; |
| 107 |
this.altAllele=alt; |
| 108 |
this.bases=b; |
| 109 |
this.qualities=q; |
| 110 |
this.genotypes = Arrays.asList(s1, s2); |
| 111 |
this.missingDataPenality=missingDataPenality; |
| 112 |
this.maximumObservationProbability=maximumObservationProbability; |
| 113 |
this.minorAlleleFrequency=minorAlleleFrequency; |
| 114 |
this.contamination=contamination; |
| 115 |
this.allLikes= precomputeLikelihoods(); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Since the genotype states, ref/alt allele, and bases/qualities are fixed, compute the likelihood of each genotype for each UMI pileup base observed. |
| 120 |
* |
| 121 |
* @return A 2d matrix. The first dimension is 1 base of the UMI pileup. The second dimension contains the likelihoods for the genotypes given the UMI base/quality. |
| 122 |
*/ |
| 123 |
private double [] [] precomputeLikelihoods () { |
| 124 |
double [] [] allLikes = new double [bases.size()] [genotypes.size()]; |
| 125 |
for (int i=0; i<bases.size(); i++) { |
| 126 |
//TODO: add in contamination calculations |
| 127 |
allLikes[i] = LikelihoodUtils.getInstance().getLikelihoodManyObservations(this.refAllele, this.altAllele, this.genotypes, this.bases.get(i), this.qualities.get(i), |
| 128 |
this.missingDataPenality, null, this.maximumObservationProbability, this.minorAlleleFrequency, this.contamination); |
| 129 |
} |
| 130 |
return (allLikes); |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
public VariantData (final Interval snpInterval, final char refAllele, final char altAllele, |
| 136 |
final GenotypeType s1, final GenotypeType s2, |
| 137 |
final char [] bases, final int [] qualities) { |
| 138 |
this(snpInterval, refAllele, altAllele, s1, s2, bases, qualities, null, null, null, null); |
| 139 |
} |
| 140 |
|
| 141 |
public Interval getSNPInterval() { |
| 142 |
return this.snpInterval; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
/** |
| 147 |
* Is genotype one different from genotype two, and is neither of the genotypes a no call? |
| 148 |
* @return |
| 149 |
*/ |
| 150 |
public boolean isInformative() { |
| 151 |
if (this.getGenotypeOne()==GenotypeType.NO_CALL || this.getGenotypeTwo()==GenotypeType.NO_CALL) return false; |
| 152 |
if (this.getGenotypeOne()==this.getGenotypeTwo()) return false; |
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Is genotype one different from genotype two, and is neither of the genotypes a no call? |
| 158 |
* Additionally, the genotype for the sample should be homozygous. |
| 159 |
* @param genotypeIndex |
| 160 |
* @return True if this |
| 161 |
*/ |
| 162 |
public boolean isInformativeHomozygous(final int genotypeIndex) { |
| 163 |
if (!isInformative()) return false; |
| 164 |
// Site is informative, is it homozygous? |
| 165 |
GenotypeType gt = this.genotypes.get(genotypeIndex); |
| 166 |
switch (gt) { |
| 167 |
case HOM_REF: return true; |
| 168 |
case HOM_VAR: return true; |
| 169 |
default: return false; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
public int getNumImpossibleAllelesGenotypeOne() { |
| 174 |
return getNumImpossibleAlleles(0); |
| 175 |
} |
| 176 |
|
| 177 |
public int getNumImpossibleAllelesGenotypeTwo() { |
| 178 |
return getNumImpossibleAlleles(1); |
| 179 |
} |
| 180 |
|
| 181 |
int getNumImpossibleAlleles(final int genotypeIndex) { |
| 182 |
GenotypeType gt = this.genotypes.get(genotypeIndex); |
| 183 |
int count=0; |
| 184 |
switch (gt) { |
| 185 |
case HOM_REF: |
| 186 |
count = getCountAllele(this.altAllele); |
| 187 |
break; |
| 188 |
case HET: |
| 189 |
count=0; |
| 190 |
break; |
| 191 |
case HOM_VAR: |
| 192 |
count = getCountAllele(this.refAllele); |
| 193 |
break; |
| 194 |
} |
| 195 |
return count; |
| 196 |
} |
| 197 |
|
| 198 |
private int getCountAllele (final byte allele) { |
| 199 |
int count=0; |
| 200 |
for (byte b: this.bases) |
| 201 |
if (b==allele) |
| 202 |
count++; |
| 203 |
return (count); |
| 204 |
} |
| 205 |
|
| 206 |
public GenotypeType getGenotypeOne () { |
| 207 |
return this.genotypes.get(0); |
| 208 |
} |
| 209 |
|
| 210 |
public GenotypeType getGenotypeTwo () { |
| 211 |
return this.genotypes.get(1); |
| 212 |
} |
| 213 |
|
| 214 |
public int getGenotypeCountReference() { |
| 215 |
return getCountAllele(this.refAllele); |
| 216 |
} |
| 217 |
|
| 218 |
public int getGenotypeCountAlternate() { |
| 219 |
return getCountAllele(this.altAllele); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* How many observations are there of any allele? |
| 224 |
* @return |
| 225 |
*/ |
| 226 |
public int getNumUMIs() { |
| 227 |
int result = getGenotypeCountReference() + getGenotypeCountAlternate(); |
| 228 |
return result; |
| 229 |
} |
| 230 |
|
| 231 |
public double getLogLikelihood(final double mixture) { |
| 232 |
List<Double> mixtures = Arrays.asList(mixture, 1-mixture); |
| 233 |
// double perSNP = LikelihoodUtils.getInstance() |
| 234 |
// .getLogLikelihoodMixedModel(refAllele, altAllele, genotypes, |
| 235 |
// mixtures, bases, qualities, missingDataPenality, null, maximumObservationProbability); |
| 236 |
double perSNP = LikelihoodUtils.getInstance().getLogLikelihoodMixedModel(this.allLikes, mixtures); |
| 237 |
// if (Math.abs(perSNP-perSNP2)>0.001) |
| 238 |
// System.out.println("STOP"); |
| 239 |
if (Double.isNaN(perSNP)) |
| 240 |
throw new IllegalStateException("Likelihood result can't be NaN"); |
| 241 |
return perSNP; |
| 242 |
} |
| 243 |
|
| 244 |
@Override |
| 245 |
public String toString () { |
| 246 |
StringBuilder b = new StringBuilder(); |
| 247 |
b.append(snpInterval.toString()+" "); |
| 248 |
b.append("Ref [" +StringUtil.byteToChar(this.refAllele)+"] "); |
| 249 |
b.append("Alt [" +StringUtil.byteToChar(this.altAllele)+"] "); |
| 250 |
b.append("Genotype1 ["+ this.genotypes.get(0).toString()+"] "); |
| 251 |
b.append("Genotype2 ["+ this.genotypes.get(1).toString()+"] "); |
| 252 |
b.append("Bases [" +StringUtil.bytesToString(convert(this.bases))+"] "); |
| 253 |
b.append("Quals ["); |
| 254 |
for (int i=0; i<this.qualities.size(); i++) { |
| 255 |
b.append(this.qualities.get(i)); |
| 256 |
if (i<this.qualities.size()-1) |
| 257 |
b.append(","); |
| 258 |
} |
| 259 |
b.append("]"); |
| 260 |
return b.toString(); |
| 261 |
} |
| 262 |
|
| 263 |
private byte [] convert (final List<Byte> x) { |
| 264 |
byte[] base =new byte[x.size()]; |
| 265 |
for (int i=0; i<x.size(); i++) |
| 266 |
base[i]=x.get(i); |
| 267 |
return base; |
| 268 |
} |
| 269 |
|
| 270 |
} |