package barcode; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Map.Entry; import fileIO.ByteStreamWriter; import shared.Tools; import stream.Read; import structures.ByteBuilder; public class BarcodeMappingStats { /*--------------------------------------------------------------*/ /*---------------- Constructor ----------------*/ /*--------------------------------------------------------------*/ public BarcodeMappingStats() {} /*--------------------------------------------------------------*/ /*---------------- Methods ----------------*/ /*--------------------------------------------------------------*/ public void merge(BarcodeMappingStats bs) { for(Entry e : bs.codeMap.entrySet()) { Barcode b=e.getValue(); incrementCodeMap(b.name, b.count()); } for(Entry> e : bs.sourceMap.entrySet()) { final String readKey=e.getKey(); final HashMap map=e.getValue(); for(Entry ee : map.entrySet()) { final Barcode b=ee.getValue(); final String refKey=ee.getKey(); incrementSourceMap(readKey, refKey, b.count()); } } } public void increment(Read r, String refKey){ String barcode=r.barcode(true); incrementCodeMap(barcode, r.pairCount()); incrementSourceMap(barcode, refKey==null ? "UNKNOWN" : refKey, r.pairCount()); } public void incrementCodeMap(String key, long amt) { Barcode b=codeMap.get(key); if(b==null){ b=new Barcode(key); codeMap.put(key, b); } b.increment(amt); } public void incrementSourceMap(String readKey, String refKey, long amt) { HashMap map=sourceMap.get(readKey); if(map==null){ map=new HashMap(); sourceMap.put(readKey, map); } Barcode b=map.get(refKey); if(b==null){ b=new Barcode(refKey); map.put(refKey, b); } b.increment(amt); } public void writeStats(String outbarcodes, boolean overwrite) { ByteBuilder bb=new ByteBuilder(); ArrayList codeList=toSortedList(codeMap); final long sum=sum(codeList); final double invSum=1.0/(Tools.max(1, sum)); ByteStreamWriter bsw=new ByteStreamWriter(outbarcodes, overwrite, false, true); bsw.start(); bsw.println("#Reads\t"+sum); bsw.println("#Barcode\tSource\tCount\tFraction"); for(Barcode bc : codeList) { HashMap map=sourceMap.get(bc.name); ArrayList sourceList=toSortedList(map); final long sum2=sum(sourceList); final double invSum2=1.0/(Tools.max(1, sum2)); for(Barcode source : sourceList) { bb.append(bc.name).tab().append(source.name).tab().append(source.count()).tab().append(source.count()*invSum2, 6).nl(); bsw.print(bb); bb.clear(); } } errorState|=bsw.poisonAndWait(); } private static long sum(ArrayList list) { long sum=0; for(Barcode bc : list) { sum+=bc.count(); } return sum; } private static ArrayList toSortedList(HashMap map){ if(map==null || map.isEmpty()){return null;} ArrayList list=new ArrayList(map.size()); for(Entry e : map.entrySet()) { list.add(e.getValue()); } Collections.sort(list); return list; } /*--------------------------------------------------------------*/ /*---------------- Fields ----------------*/ /*--------------------------------------------------------------*/ /** Raw counts of barcodes */ public HashMap codeMap=new HashMap(); /** * A table of tables. Key1 is the barcode of a read; key2 is where the read mapped to. * The barcode for key2 tracks the number of times reads with key1 barcode mapped to key2's reference. * E.G. if key1 is ABC-DEF then the top barcode in its table would be expected to * be ABC-DEF, and other entries would indicate contamination of that library. * */ public HashMap> sourceMap=new HashMap>(); public boolean errorState=false; }