| 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 java.util.ArrayList; |
| 27 |
import java.util.Arrays; |
| 28 |
import java.util.Collections; |
| 29 |
import java.util.HashMap; |
| 30 |
import java.util.List; |
| 31 |
import java.util.Map; |
| 32 |
import java.util.stream.Collectors; |
| 33 |
|
| 34 |
import org.apache.commons.math3.stat.descriptive.summary.Sum; |
| 35 |
import org.broadinstitute.dropseqrna.barnyard.digitalallelecounts.LikelihoodUtils; |
| 36 |
import org.broadinstitute.dropseqrna.barnyard.digitalallelecounts.sampleassignment.CellAssignmentUtils; |
| 37 |
import org.broadinstitute.dropseqrna.barnyard.digitalallelecounts.sampleassignment.SampleGenotypeProbabilities; |
| 38 |
|
| 39 |
import htsjdk.samtools.util.Interval; |
| 40 |
import htsjdk.samtools.util.StringUtil; |
| 41 |
import htsjdk.variant.variantcontext.GenotypeType; |
| 42 |
|
| 43 |
public class VariantDataFactory { |
| 44 |
|
| 45 |
private final List<SampleGenotypeProbabilities> allProbs; |
| 46 |
private final GenotypeMatrix genotypeMatrix; |
| 47 |
private final String cell; |
| 48 |
private final Double fixedErrorRate; |
| 49 |
private final Map<SampleGenotypeProbabilities, Double> missingDataPenalties; |
| 50 |
private final Double maximumObservationProbability; |
| 51 |
private final Double contamination; |
| 52 |
private final Map<Interval, Double> variantMinorAlleleFrequency; |
| 53 |
|
| 54 |
|
| 55 |
public VariantDataFactory (final String cell, final List<SampleGenotypeProbabilities> probs, final GenotypeMatrix genotypeMatrix) { |
| 56 |
this(cell, probs, genotypeMatrix, null, false, null, null, null); |
| 57 |
} |
| 58 |
|
| 59 |
public VariantDataFactory (final String cell, final List<SampleGenotypeProbabilities> probs, final GenotypeMatrix genotypeMatrix, final Double fixedErrorRate) { |
| 60 |
this(cell, probs, genotypeMatrix, fixedErrorRate, false, null, null, null); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Constructs a factory that generates collections of variant data that can be optimized for a mixture of samples. |
| 65 |
* |
| 66 |
* @param cell The cell barcode that all the data comes from |
| 67 |
* @param probs The pileups of SNP data |
| 68 |
* @param genotypeMatrix A matrix of genotype states |
| 69 |
* @param fixedErrorRate A fixed error rate between 0 and 1 or null. |
| 70 |
* @param maximumObservationProbability cap the error rate per UMI at this value globally |
| 71 |
* @param cellContaminationMap Use the estimated ambient RNA contamination in cells to modify likelihood . Key=cell barcode, value=maximum error rate. |
| 72 |
* @param variantMinorAlleleFrequency The estimated minor allele frequency of each variant. |
| 73 |
* @param useMissingDataPenalty Should genotypes that are set to GenotypeType.NO_CALL have a global (across all samples for the missing snp) value generated and used? See @getMissingDataPenalities |
| 74 |
*/ |
| 75 |
public VariantDataFactory (final String cell, final List<SampleGenotypeProbabilities> probs, final GenotypeMatrix genotypeMatrix, final Double fixedErrorRate, |
| 76 |
final boolean useMissingDataPenalty, final Double maximumObservationProbability, Map<String,Double> cellContaminationMap, Map<Interval, Double> variantMinorAlleleFrequency) { |
| 77 |
|
| 78 |
this.contamination = CellAssignmentUtils.getNullableValue(cellContaminationMap, cell); |
| 79 |
this.maximumObservationProbability=maximumObservationProbability; |
| 80 |
this.variantMinorAlleleFrequency = variantMinorAlleleFrequency; |
| 81 |
|
| 82 |
this.genotypeMatrix=genotypeMatrix; |
| 83 |
this.cell=cell; |
| 84 |
this.fixedErrorRate=fixedErrorRate; |
| 85 |
|
| 86 |
// initialize storage. |
| 87 |
this.allProbs=new ArrayList<>(probs.size()); |
| 88 |
for (SampleGenotypeProbabilities p: probs) |
| 89 |
// validate that the probabilities have the cell you say you're using. |
| 90 |
if (p.getCell().equals(cell)) |
| 91 |
allProbs.add(p); |
| 92 |
else |
| 93 |
throw new IllegalArgumentException("While populating data for cell " + cell+ " saw pileup data for cell " + p.getCell()); |
| 94 |
// if we want to use missing data penalties, set all of them. |
| 95 |
if (useMissingDataPenalty) |
| 96 |
this.missingDataPenalties=getMissingDataPenalities(probs, genotypeMatrix, fixedErrorRate, maximumObservationProbability); |
| 97 |
else // empty map. |
| 98 |
this.missingDataPenalties=Collections.emptyMap(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Generates a collection of variants for the two samples. |
| 103 |
* Only generates a list with elements if both sampleOne and sampleTwo are in the genotypeMatrix. |
| 104 |
* Otherwise produces an empty list. |
| 105 |
* Only add a variant if it has at least one called REF/HET/VAR. |
| 106 |
* @param sampleOne |
| 107 |
* @param sampleTwo |
| 108 |
* @return |
| 109 |
*/ |
| 110 |
public VariantDataCollection getVariantData (final String sampleOne, final String sampleTwo) { |
| 111 |
|
| 112 |
// if one of the genotype states is missing, don't add. |
| 113 |
// only test pairs where the data was set - donors |
| 114 |
// to be included should have at least a missing value. |
| 115 |
if (!this.genotypeMatrix.containsDonor(sampleOne) || !this.genotypeMatrix.containsDonor(sampleTwo)) { |
| 116 |
List<VariantData> empty = Collections.emptyList(); |
| 117 |
return new VariantDataCollection(empty, sampleOne, sampleTwo, this.cell); |
| 118 |
} |
| 119 |
|
| 120 |
// this only works in parallel if the allele freqs in the genotype matrix are precomputed. |
| 121 |
List<VariantData> d= this.allProbs.parallelStream().map(x -> getVariantData(x, sampleOne, sampleTwo)).filter(x -> x!=null).collect(Collectors.toList()); |
| 122 |
VariantDataCollection result = new VariantDataCollection(d, sampleOne, sampleTwo, this.cell); |
| 123 |
return result; |
| 124 |
} |
| 125 |
|
| 126 |
private VariantData getVariantData (SampleGenotypeProbabilities p, final String sampleOne, final String sampleTwo) { |
| 127 |
Interval i = p.getSNPInterval(); |
| 128 |
Double maf = CellAssignmentUtils.getNullableValue(this.variantMinorAlleleFrequency, i); |
| 129 |
|
| 130 |
double [] genotypeFreqs = genotypeMatrix.getGenotypeFrequencies(i); |
| 131 |
double sum = new Sum().evaluate(genotypeFreqs); |
| 132 |
// need at least one ref/het/var call in this variant to continue. |
| 133 |
// in practice the variants are never |
| 134 |
if (sum==0) |
| 135 |
return (null); |
| 136 |
GenotypeType s1 = genotypeMatrix.getGenotype(i, sampleOne); |
| 137 |
GenotypeType s2 = genotypeMatrix.getGenotype(i, sampleTwo); |
| 138 |
|
| 139 |
byte refAllele = genotypeMatrix.getRefBase(i); |
| 140 |
byte altAllele = genotypeMatrix.getAltBase(i); |
| 141 |
|
| 142 |
List<Byte> basesFinal = new ArrayList<>(); |
| 143 |
List<Byte> qualsFinal = new ArrayList<>(); |
| 144 |
List<Byte> b = p.getBases(); |
| 145 |
List<Byte> qual = p.getQualities(); |
| 146 |
// filter |
| 147 |
for (int j=0; j<b.size(); j++) { |
| 148 |
byte currentB=b.get(j); |
| 149 |
if (currentB==refAllele || currentB==altAllele) { |
| 150 |
basesFinal.add(currentB); |
| 151 |
qualsFinal.add(qual.get(j)); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
Double missingDataPenality = missingDataPenalties.get(p); |
| 156 |
// should we have the possibility of both donors having a missing value? |
| 157 |
// that would give any two donor pairs the same number of SNPs. |
| 158 |
if ((s1!=GenotypeType.NO_CALL && s2!=GenotypeType.NO_CALL) || (missingDataPenality!=null)) { |
| 159 |
if (this.fixedErrorRate!=null) { |
| 160 |
byte phreadScore = LikelihoodUtils.getInstance().errorProbabilityToPhredScore(this.fixedErrorRate); |
| 161 |
Byte [] q = new Byte [qualsFinal.size()]; |
| 162 |
Arrays.fill(q, phreadScore); |
| 163 |
qualsFinal=Arrays.asList(q); |
| 164 |
} |
| 165 |
VariantData vd =new VariantData(i, refAllele, altAllele, s1, s2, basesFinal, qualsFinal, missingDataPenality, maximumObservationProbability, this.contamination, maf); |
| 166 |
// only add a variant if it has data on the reference or alternate allele, so that it is informative! |
| 167 |
if (vd.getGenotypeCountReference()>0 || vd.getGenotypeCountAlternate()>0) |
| 168 |
return (vd); |
| 169 |
} |
| 170 |
return null; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Computes a missing data penalty for each pileup/variant. |
| 175 |
* This penality is based on the genotype clases observed for this SNP across all samples, and is a weighted mix of |
| 176 |
* L(D|HOM_REF)*fraction HOM_REF + L(D|HET)*fraction HET + L(D|HOM_VAR)*fraction HOM_VAR |
| 177 |
* The value returned is NOT in log space. |
| 178 |
* @param allProbs |
| 179 |
* @param genotypeMatrix |
| 180 |
* @param fixedGenotypeErrorRate |
| 181 |
* @return |
| 182 |
*/ |
| 183 |
public Map<SampleGenotypeProbabilities, Double> getMissingDataPenalities(final List<SampleGenotypeProbabilities> allProbs, final GenotypeMatrix genotypeMatrix, final Double fixedGenotypeErrorRate, final Double maximumObservationProbability) { |
| 184 |
Map<SampleGenotypeProbabilities, Double> result = new HashMap<>(); |
| 185 |
for (SampleGenotypeProbabilities p: allProbs) { |
| 186 |
Interval i = p.getSNPInterval(); |
| 187 |
Double minorAlleleFrequency = CellAssignmentUtils.getNullableValue(this.variantMinorAlleleFrequency, i); |
| 188 |
double [] genotypeFreqs = genotypeMatrix.getGenotypeFrequencies(i); |
| 189 |
char refAllele = StringUtil.byteToChar(genotypeMatrix.getRefBase(i)); |
| 190 |
char altAllele = StringUtil.byteToChar(genotypeMatrix.getAltBase(i)); |
| 191 |
GenotypeType [] genos = {GenotypeType.HOM_REF, GenotypeType.HET, GenotypeType.HOM_VAR}; |
| 192 |
|
| 193 |
List<Double> freqs=new ArrayList<>(); |
| 194 |
for (double genotypeFreq : genotypeFreqs) |
| 195 |
freqs.add(genotypeFreq); |
| 196 |
|
| 197 |
double penalty = p.getLogLikelihoodMissingData(refAllele, altAllele, Arrays.asList(genos), freqs, fixedGenotypeErrorRate, null, maximumObservationProbability, minorAlleleFrequency, this.contamination); |
| 198 |
// go back to normal space instead of log space to be consistent with other data. |
| 199 |
penalty=Math.pow(10,penalty); |
| 200 |
result.put(p, penalty); |
| 201 |
} |
| 202 |
return (result); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public String getCell() { |
| 208 |
return cell; |
| 209 |
} |
| 210 |
} |