Debian Codegraph @

current/barcode/BarcodeMappingStats.java

raw symbols SCIP
1 package barcode;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.Map.Entry;
7
8 import fileIO.ByteStreamWriter;
9 import shared.Tools;
10 import stream.Read;
11 import structures.ByteBuilder;
12
13 public class BarcodeMappingStats {
14
15 /*--------------------------------------------------------------*/
16 /*---------------- Constructor ----------------*/
17 /*--------------------------------------------------------------*/
18
19 public BarcodeMappingStats() {}
20
21 /*--------------------------------------------------------------*/
22 /*---------------- Methods ----------------*/
23 /*--------------------------------------------------------------*/
24
25 public void merge(BarcodeMappingStats bs) {
26 for(Entry<String, Barcode> e : bs.codeMap.entrySet()) {
27 Barcode b=e.getValue();
28 incrementCodeMap(b.name, b.count());
29 }
30 for(Entry<String, HashMap<String, Barcode>> e : bs.sourceMap.entrySet()) {
31 final String readKey=e.getKey();
32 final HashMap<String, Barcode> map=e.getValue();
33 for(Entry<String, Barcode> ee : map.entrySet()) {
34 final Barcode b=ee.getValue();
35 final String refKey=ee.getKey();
36 incrementSourceMap(readKey, refKey, b.count());
37 }
38 }
39 }
40
41 public void increment(Read r, String refKey){
42 String barcode=r.barcode(true);
43 incrementCodeMap(barcode, r.pairCount());
44 incrementSourceMap(barcode, refKey==null ? "UNKNOWN" : refKey, r.pairCount());
45 }
46
47 public void incrementCodeMap(String key, long amt) {
48 Barcode b=codeMap.get(key);
49 if(b==null){
50 b=new Barcode(key);
51 codeMap.put(key, b);
52 }
53 b.increment(amt);
54 }
55
56 public void incrementSourceMap(String readKey, String refKey, long amt) {
57 HashMap<String, Barcode> map=sourceMap.get(readKey);
58 if(map==null){
59 map=new HashMap<String, Barcode>();
60 sourceMap.put(readKey, map);
61 }
62 Barcode b=map.get(refKey);
63 if(b==null){
64 b=new Barcode(refKey);
65 map.put(refKey, b);
66 }
67 b.increment(amt);
68 }
69
70 public void writeStats(String outbarcodes, boolean overwrite) {
71 ByteBuilder bb=new ByteBuilder();
72 ArrayList<Barcode> codeList=toSortedList(codeMap);
73 final long sum=sum(codeList);
74 final double invSum=1.0/(Tools.max(1, sum));
75
76 ByteStreamWriter bsw=new ByteStreamWriter(outbarcodes, overwrite, false, true);
77 bsw.start();
78
79 bsw.println("#Reads\t"+sum);
80 bsw.println("#Barcode\tSource\tCount\tFraction");
81 for(Barcode bc : codeList) {
82 HashMap<String, Barcode> map=sourceMap.get(bc.name);
83 ArrayList<Barcode> sourceList=toSortedList(map);
84 final long sum2=sum(sourceList);
85 final double invSum2=1.0/(Tools.max(1, sum2));
86 for(Barcode source : sourceList) {
87 bb.append(bc.name).tab().append(source.name).tab().append(source.count()).tab().append(source.count()*invSum2, 6).nl();
88 bsw.print(bb);
89 bb.clear();
90 }
91 }
92 errorState|=bsw.poisonAndWait();
93 }
94
95 private static long sum(ArrayList<Barcode> list) {
96 long sum=0;
97 for(Barcode bc : list) {
98 sum+=bc.count();
99 }
100 return sum;
101 }
102
103 private static ArrayList<Barcode> toSortedList(HashMap<String, Barcode> map){
104 if(map==null || map.isEmpty()){return null;}
105 ArrayList<Barcode> list=new ArrayList<Barcode>(map.size());
106 for(Entry<String, Barcode> e : map.entrySet()) {
107 list.add(e.getValue());
108 }
109 Collections.sort(list);
110 return list;
111 }
112
113 /*--------------------------------------------------------------*/
114 /*---------------- Fields ----------------*/
115 /*--------------------------------------------------------------*/
116
117 /** Raw counts of barcodes */
118 public HashMap<String, Barcode> codeMap=new HashMap<String, Barcode>();
119
120 /**
121 * A table of tables. Key1 is the barcode of a read; key2 is where the read mapped to.
122 * The barcode for key2 tracks the number of times reads with key1 barcode mapped to key2's reference.
123 * E.G. if key1 is ABC-DEF then the top barcode in its table would be expected to
124 * be ABC-DEF, and other entries would indicate contamination of that library.
125 *
126 */
127 public HashMap<String, HashMap<String, Barcode>> sourceMap=new HashMap<String, HashMap<String, Barcode>>();
128
129 public boolean errorState=false;
130
131 }