| 1 |
package driver; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
|
| 6 |
import fileIO.TextFile; |
| 7 |
import fileIO.TextStreamWriter; |
| 8 |
import shared.Parse; |
| 9 |
import shared.Parser; |
| 10 |
import shared.PreParser; |
| 11 |
import shared.Tools; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public class SummarizeSealStats { |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public static void main(String[] args){ |
| 25 |
|
| 26 |
SummarizeSealStats sss=new SummarizeSealStats(args); |
| 27 |
|
| 28 |
|
| 29 |
sss.summarize(); |
| 30 |
} |
| 31 |
|
| 32 |
public SummarizeSealStats(String[] args){ |
| 33 |
|
| 34 |
{ |
| 35 |
PreParser pp=new PreParser(args, getClass(), false); |
| 36 |
args=pp.args; |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
ArrayList<String> names=new ArrayList<String>(); |
| 41 |
Parser parser=new Parser(); |
| 42 |
|
| 43 |
|
| 44 |
for(int i=0; i<args.length; i++){ |
| 45 |
|
| 46 |
final String arg=args[i]; |
| 47 |
String[] split=arg.split("="); |
| 48 |
String a=split[0].toLowerCase(); |
| 49 |
String b=split.length>1 ? split[1] : null; |
| 50 |
|
| 51 |
if(a.equals("printtotal") || a.equals("pt")){ |
| 52 |
printTotal=Parse.parseBoolean(b); |
| 53 |
}else if(a.equals("ignoresametaxa")){ |
| 54 |
ignoreSameTaxa=Parse.parseBoolean(b); |
| 55 |
}else if(a.equals("ignoresamebarcode") || a.equals("ignoresameindex")){ |
| 56 |
ignoreSameBarcode=Parse.parseBoolean(b); |
| 57 |
}else if(a.equals("ignoresamelocation") || a.equals("ignoresameloc")){ |
| 58 |
ignoreSameLocation=Parse.parseBoolean(b); |
| 59 |
}else if(a.equals("usetotal") || a.equals("totaldenominator") || a.equals("totald") || a.equals("td")){ |
| 60 |
totalDenominator=Parse.parseBoolean(b); |
| 61 |
}else if(parser.parse(arg, a, b)){ |
| 62 |
|
| 63 |
}else if(!arg.contains("=")){ |
| 64 |
String[] x=(new File(arg).exists() ? new String[] {arg} : arg.split(",")); |
| 65 |
for(String x2 : x){names.add(x2);} |
| 66 |
}else{ |
| 67 |
throw new RuntimeException("Unknown parameter "+arg); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
{ |
| 72 |
out=(parser.out1==null ? "stdout" : parser.out1); |
| 73 |
if(parser.in1!=null){ |
| 74 |
String[] x=(new File(parser.in1).exists() ? new String[] {parser.in1} : parser.in1.split(",")); |
| 75 |
for(String x2 : x){names.add(x2);} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
in=new ArrayList<String>(); |
| 80 |
for(String s : names){ |
| 81 |
Tools.getFileOrFiles(s, in, false, false, false, true); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
public void summarize(){ |
| 86 |
ArrayList<SealSummary> list=new ArrayList<SealSummary>(); |
| 87 |
|
| 88 |
SealSummary total=new SealSummary(null); |
| 89 |
total.pname="TOTAL"; |
| 90 |
for(String fname : in){ |
| 91 |
SealSummary ss=new SealSummary(fname); |
| 92 |
list.add(ss); |
| 93 |
total.add(ss); |
| 94 |
} |
| 95 |
|
| 96 |
TextStreamWriter tsw=new TextStreamWriter(out, true, false, false); |
| 97 |
tsw.start(); |
| 98 |
tsw.print("#File\tPrimary_Name\tPrimary_Count\tOther_Count\tPrimary_Bases\tOther_Bases\tOther_ppm\n"); |
| 99 |
if(printTotal){ |
| 100 |
tsw.println(total.toString()); |
| 101 |
} |
| 102 |
for(SealSummary ss : list){ |
| 103 |
tsw.println(ss.toString()); |
| 104 |
} |
| 105 |
tsw.poisonAndWait(); |
| 106 |
} |
| 107 |
|
| 108 |
private class SealSummary { |
| 109 |
|
| 110 |
SealSummary(String fname_){ |
| 111 |
fname=fname_; |
| 112 |
if(fname!=null){ |
| 113 |
if(ignoreSameTaxa || ignoreSameBarcode || ignoreSameLocation){ |
| 114 |
cleanAndSummarize(); |
| 115 |
}else{ |
| 116 |
summarize(); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
public void add(SealSummary ss){ |
| 122 |
pcount+=ss.pcount; |
| 123 |
ocount+=ss.ocount; |
| 124 |
tcount+=ss.tcount; |
| 125 |
pbases+=ss.pbases; |
| 126 |
obases+=ss.obases; |
| 127 |
tbases+=ss.tbases; |
| 128 |
|
| 129 |
if(totalDenominator && tbases>0){ |
| 130 |
ppm=obases*1000000.0/tbases; |
| 131 |
}else{ |
| 132 |
ppm=(obases==0 ? 0 : obases*1000000.0/(obases+pbases)); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public String toString(){ |
| 138 |
return Tools.format("%s\t%s\t%d\t%d\t%d\t%d\t%.2f", fname, pname, pcount, ocount, pbases, obases, ppm); |
| 139 |
} |
| 140 |
|
| 141 |
private void summarize(){ |
| 142 |
TextFile tf=new TextFile(fname); |
| 143 |
for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){ |
| 144 |
if(line.startsWith("#")){ |
| 145 |
if(line.startsWith("#Total")){ |
| 146 |
String[] split=line.split("\t"); |
| 147 |
tcount=Long.parseLong(split[1]); |
| 148 |
tbases=Long.parseLong(split[2]); |
| 149 |
} |
| 150 |
}else{ |
| 151 |
String[] split=line.split("\t"); |
| 152 |
long count=Long.parseLong(split[1]); |
| 153 |
long bases=Long.parseLong(split[3]); |
| 154 |
if(pcount==0 || bases>pbases || (bases==pbases && count>pcount)){ |
| 155 |
pname=split[0]; |
| 156 |
ocount+=pcount; |
| 157 |
obases+=pbases; |
| 158 |
pcount=count; |
| 159 |
pbases=bases; |
| 160 |
}else{ |
| 161 |
ocount+=count; |
| 162 |
obases+=bases; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
tf.close(); |
| 167 |
if(totalDenominator && tbases>0){ |
| 168 |
ppm=obases*1000000.0/tbases; |
| 169 |
}else{ |
| 170 |
ppm=(obases==0 ? 0 : obases*1000000.0/(obases+pbases)); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
public void cleanAndSummarize(){ |
| 175 |
TextFile tf=new TextFile(fname); |
| 176 |
for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){ |
| 177 |
if(line.startsWith("#")){ |
| 178 |
if(line.startsWith("#Total")){ |
| 179 |
String[] split=line.split("\t"); |
| 180 |
tcount=Long.parseLong(split[1]); |
| 181 |
tbases=Long.parseLong(split[2]); |
| 182 |
} |
| 183 |
}else{ |
| 184 |
String[] split=line.split("\t"); |
| 185 |
String[] name=split[0].toLowerCase().split(","); |
| 186 |
String[] barcode=name[0].split("-"); |
| 187 |
|
| 188 |
long count=Long.parseLong(split[1]); |
| 189 |
long bases=Long.parseLong(split[3]); |
| 190 |
if(pcount==0 || bases>pbases || (bases==pbases && count>pcount)){ |
| 191 |
name0=name; |
| 192 |
barcode0=barcode; |
| 193 |
pname=split[0]; |
| 194 |
ocount+=pcount; |
| 195 |
obases+=pbases; |
| 196 |
pcount=count; |
| 197 |
pbases=bases; |
| 198 |
}else{ |
| 199 |
boolean process=true; |
| 200 |
if(ignoreSameTaxa){ |
| 201 |
if(name[2].contains(name0[2]) || name0[2].contains(name[2])){ |
| 202 |
process=false; |
| 203 |
} |
| 204 |
} |
| 205 |
if(ignoreSameBarcode){ |
| 206 |
if(barcode[0].equals(barcode0[0]) || barcode[1].equals(barcode0[1])){ |
| 207 |
process=false; |
| 208 |
} |
| 209 |
} |
| 210 |
if(ignoreSameLocation){ |
| 211 |
assert(name.length==4) : "Too many delimiters: "+name.length+"\n"+line+"\n"; |
| 212 |
if(name[3].equals(name0[3])){ |
| 213 |
process=false; |
| 214 |
} |
| 215 |
} |
| 216 |
if(process){ |
| 217 |
ocount+=count; |
| 218 |
obases+=bases; |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
tf.close(); |
| 224 |
if(totalDenominator && tbases>0){ |
| 225 |
ppm=obases*1000000.0/tbases; |
| 226 |
}else{ |
| 227 |
ppm=(obases==0 ? 0 : obases*1000000.0/(obases+pbases)); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
final String fname; |
| 232 |
String pname=null; |
| 233 |
long pcount=0, ocount=0, tcount=0; |
| 234 |
long pbases=0, obases=0, tbases=0; |
| 235 |
double ppm; |
| 236 |
String[] name0=null, barcode0=null; |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
final ArrayList<String> in; |
| 241 |
final String out; |
| 242 |
boolean ignoreSameTaxa=false; |
| 243 |
boolean ignoreSameBarcode=false; |
| 244 |
boolean ignoreSameLocation=false; |
| 245 |
boolean totalDenominator=false; |
| 246 |
boolean printTotal=true; |
| 247 |
|
| 248 |
} |