Debian Codegraph @

current/jgi/CovStatsLine.java

raw symbols SCIP
1 package jgi;
2
3 import java.util.Arrays;
4 import java.util.HashMap;
5
6 import shared.Tools;
7
8 /**
9 * @author Brian Bushnell
10 * @date Oct 10, 2014
11 *
12 */
13 public class CovStatsLine {
14
15 public CovStatsLine(String s){
16 this(s.split("\t"));
17 }
18
19 /**
20 * ID Avg_fold Length Ref_GC Covered_percent Covered_bases Plus_reads Minus_reads (optional.... Read_GC)
21 * @param split
22 */
23 public CovStatsLine(String[] split) {
24 assert(split.length>=8) : Arrays.toString(split);
25 assert(!split[0].startsWith("#")) : Arrays.toString(split);
26 assert(id_FNUM==0) : "Not initialized with header: "+id_FNUM;
27
28 if(id_FNUM>=0){id=split[id_FNUM];}
29 if(length_FNUM>=0){length=Integer.parseInt(split[length_FNUM]);}
30 if(coveredBases_FNUM>=0){coveredBases=Integer.parseInt(split[coveredBases_FNUM]);}
31 if(plusReads_FNUM>=0){plusReads=Long.parseLong(split[plusReads_FNUM]);}
32 if(minusReads_FNUM>=0){minusReads=Long.parseLong(split[minusReads_FNUM]);}
33 if(avgFold_FNUM>=0){avgFold=Double.parseDouble(split[avgFold_FNUM]);}
34 if(refGC_FNUM>=0){refGC=Double.parseDouble(split[refGC_FNUM]);}
35 if(median_FNUM>=0){median=Long.parseLong(split[median_FNUM]);}
36 if(underMin_FNUM>=0){underMin=Integer.parseInt(split[underMin_FNUM]);}
37 if(readGC_FNUM>=0){readGC=Double.parseDouble(split[readGC_FNUM]);}
38 if(stdDev_FNUM>=0){stdDev=Double.parseDouble(split[stdDev_FNUM]);}
39
40 // assert(split.length>=8) : Arrays.toString(split);
41 // assert(!split[0].startsWith("#")) : Arrays.toString(split);
42 // id=split[0];
43 // avgFold=Double.parseDouble(split[1]);
44 // length=Integer.parseInt(split[2]);
45 // refGC=Double.parseDouble(split[3]);
46 //// coveredPercent=Double.parseDouble(split[4]);
47 // coveredBases=Integer.parseInt(split[5]);
48 // plusReads=Long.parseLong(split[6]);
49 // minusReads=Long.parseLong(split[7]);
50 // if(split.length==11){
51 // median=Integer.parseInt(split[8]);
52 // underMin=Integer.parseInt(split[9]);
53 // readGC=Double.parseDouble(split[10]);
54 // }else if(split.length==10){
55 // median=Integer.parseInt(split[8]);
56 // if(CoveragePileup.USE_BITSETS && CoveragePileup.USE_WINDOW){
57 // underMin=Integer.parseInt(split[9]);
58 // }else{
59 // readGC=Double.parseDouble(split[9]);
60 // }
61 // }else if(split.length==9){
62 // readGC=Double.parseDouble(split[8]);
63 // }else if(split.length<9){
64 // //do nothing
65 // }
66 }
67
68 public final double coveredPercent(){
69 return (100.0*coveredBases)/Tools.max(1, length);
70 }
71
72 public final long reads(){return plusReads+minusReads;}
73
74 /**
75 * @param csl
76 */
77 public void add(CovStatsLine csl) {
78 double invlen2=1d/Tools.max(1, length+csl.length);
79 avgFold=((avgFold*length)+(csl.avgFold*csl.length))*invlen2;
80 refGC=((refGC*length)+(csl.refGC*csl.length))*invlen2;
81 readGC=((readGC*reads())+(csl.readGC*csl.reads()))*1.0/(Tools.max(1, reads()+csl.reads()));
82
83 length+=csl.length;
84 coveredBases+=csl.coveredBases;
85 plusReads+=csl.plusReads;
86 minusReads+=csl.minusReads;
87 median=median+csl.median;
88 underMin=underMin+csl.underMin;
89 }
90
91 @Override
92 public String toString(){
93 return Tools.format("%s\t%.4f\t%d\t%.4f\t%.4f\t%d\t%d\t%d\t%d\t%d\t%.4f\t%.4f", id, avgFold, length,
94 refGC, coveredPercent(), coveredBases, plusReads, minusReads, median, underMin, readGC, stdDev);
95 }
96
97 public static void initializeHeader(String header){
98 while(header.startsWith("#")){header=header.substring(1);}
99 String[] split=header.split("\t");
100 HashMap<String, Integer> map=new HashMap<String, Integer>(23);
101 for(int i=0; i<split.length; i++){
102 String s=split[i].toLowerCase();
103 if(s.startsWith("under_")){s="under_min";}
104 map.put(split[i].toLowerCase(), i);
105 }
106 id_FNUM=map.containsKey("id") ? map.get("id") : -1;
107 avgFold_FNUM=map.containsKey("avg_fold") ? map.get("avg_fold") : -1;
108 length_FNUM=map.containsKey("length") ? map.get("length") : -1;
109 refGC_FNUM=map.containsKey("ref_gc") ? map.get("ref_gc") : -1;
110 coveredBases_FNUM=map.containsKey("covered_bases") ? map.get("covered_bases") : -1;
111 plusReads_FNUM=map.containsKey("plus_reads") ? map.get("plus_reads") : -1;
112 minusReads_FNUM=map.containsKey("minus_reads") ? map.get("minus_reads") : -1;
113 median_FNUM=map.containsKey("median_fold") ? map.get("median_fold") : -1;
114 underMin_FNUM=map.containsKey("under_min") ? map.get("under_min") : -1;
115 readGC_FNUM=map.containsKey("read_gc") ? map.get("read_gc") : -1;
116 stdDev_FNUM=map.containsKey("std_dev") ? map.get("std_dev") : -1;
117
118 assert(id_FNUM==0) : "Bad header: "+id_FNUM+"\n"+header+"\n"+map;
119 }
120
121 // public static final String header1="#ID\tAvg_fold\tLength\tRef_GC\tCovered_percent\tCovered_bases\tPlus_reads\tMinus_reads\tMedian_fold\tUnder_min\tRead_GC";
122 // public static final String header2="#ID\tAvg_fold\tLength\tRef_GC\tCovered_percent\tCovered_bases\tPlus_reads\tMinus_reads\tRead_GC";
123
124 public String id;
125 public int length;
126 public int coveredBases;
127 public long plusReads;
128 public long minusReads;
129 public double avgFold;
130 public double refGC;
131 public long median;
132 public int underMin;
133 public double readGC;
134 public double stdDev;
135
136 private static int id_FNUM=-1;
137 private static int length_FNUM=-1;
138 private static int coveredBases_FNUM=-1;
139 private static int plusReads_FNUM=-1;
140 private static int minusReads_FNUM=-1;
141 private static int avgFold_FNUM=-1;
142 private static int refGC_FNUM=-1;
143 private static int median_FNUM=-1;
144 private static int underMin_FNUM=-1;
145 private static int readGC_FNUM=-1;
146 private static int stdDev_FNUM=-1;
147 }