| 1 |
package org.broadinstitute.dropseqrna.barnyard.digitalallelecounts.sampleassignment; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.Collection; |
| 5 |
import java.util.Map; |
| 6 |
|
| 7 |
import org.broadinstitute.dropseqrna.barnyard.digitalallelecounts.GdacAlleleFrequencyReader; |
| 8 |
|
| 9 |
import htsjdk.samtools.util.Interval; |
| 10 |
import htsjdk.samtools.util.Log; |
| 11 |
import htsjdk.samtools.util.StringUtil; |
| 12 |
import htsjdk.variant.variantcontext.Allele; |
| 13 |
import htsjdk.variant.variantcontext.VariantContext; |
| 14 |
|
| 15 |
/** |
| 16 |
* A hodge-podge of common reusable code across many donor assignment code. |
| 17 |
* @author nemesh |
| 18 |
* |
| 19 |
*/ |
| 20 |
public class CellAssignmentUtils { |
| 21 |
|
| 22 |
private static final Log log = Log.getInstance(CellAssignmentUtils.class); |
| 23 |
|
| 24 |
/** |
| 25 |
* Get the value from a map where the map may be null, or the key missing or value null. |
| 26 |
* @param map A Hashmap that may be null |
| 27 |
* @param key The key in the hashmap to fetch a value for |
| 28 |
* @return If the map is null return null. Otherwise return the value of the key in the map. |
| 29 |
*/ |
| 30 |
public static <K,V> V getNullableValue (Map<K,V> map, K key) { |
| 31 |
if (map==null) return null; |
| 32 |
return map.get(key); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Converts a Double to a string. If the Double is null, emit "NA". |
| 37 |
* @param d The Double to convert |
| 38 |
* @return The conversion result. |
| 39 |
*/ |
| 40 |
public static String convertNullToString (Double d) { |
| 41 |
if (d==null) return ("NA"); |
| 42 |
return d.toString(); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* Gets the most common alternate allele for a variant. |
| 48 |
* If there is no alternate allele, return 'N' instead. |
| 49 |
* @param vc The variant context |
| 50 |
* @return The alternate allele for this context. |
| 51 |
*/ |
| 52 |
public static char getAltAllele (VariantContext vc) { |
| 53 |
Allele alt = vc.getAltAlleleWithHighestAlleleCount(); |
| 54 |
char altAllele='N'; |
| 55 |
if (alt!=null) { |
| 56 |
byte [] altBases = alt.getBases(); |
| 57 |
if (altBases.length>0) |
| 58 |
altAllele=StringUtil.byteToChar(altBases[0]); |
| 59 |
} |
| 60 |
return altAllele; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Parse the contamination file, validate against the expected cell barcodes for the experiment. |
| 65 |
* @param contaminationMapFile |
| 66 |
* @param expectedCellBarcodes |
| 67 |
* @return A map of cell barcode to contamination rate |
| 68 |
*/ |
| 69 |
public static Map<String, Double> getCellContamination (final File inputFile, final Collection<String> expectedCellBarcodes) { |
| 70 |
if (inputFile==null) return null; |
| 71 |
log.info("Per-cell ambient RNA estimates in use to cap likelihoods"); |
| 72 |
// otherwise, parse the file and create the likelihood collection. |
| 73 |
Map<String, Double> map = CellContaminationParser.parseCellContamination(inputFile, expectedCellBarcodes); |
| 74 |
return (map); |
| 75 |
} |
| 76 |
|
| 77 |
// Map<String, Double> map = CellContaminationParser.parseCellContamination(contaminationMapFile, expectedCellBarcodes); |
| 78 |
|
| 79 |
public static Map<Interval, Double> getMinorAlleleFrequencyMap (final File inputFile) { |
| 80 |
if (inputFile==null) return (null); |
| 81 |
log.info("Per SNP allele frequency estimates in use to cap likelhiods"); |
| 82 |
GdacAlleleFrequencyReader reader = new GdacAlleleFrequencyReader(inputFile); |
| 83 |
return reader.getUmiAlleleFrequencyMap(); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
} |