Debian Codegraph @

current/driver/SummarizeContamReport.java

raw symbols SCIP
1 package driver;
2
3 import java.io.File;
4 import java.io.PrintStream;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Collections;
8 import java.util.Comparator;
9 import java.util.HashMap;
10
11 import fileIO.FileFormat;
12 import fileIO.ReadWrite;
13 import fileIO.TextFile;
14 import fileIO.TextStreamWriter;
15 import shared.Parse;
16 import shared.Parser;
17 import shared.PreParser;
18 import shared.Shared;
19 import shared.Timer;
20 import shared.Tools;
21 import tax.TaxNode;
22 import tax.TaxTree;
23
24 /**
25 * @author Brian Bushnell
26 * @date Oct 17, 2014
27 *
28 */
29 public class SummarizeContamReport {
30
31 public static void main(String[] args){
32 //Start a timer immediately upon code entrance.
33 Timer t=new Timer();
34
35 //Create an instance of this class
36 SummarizeContamReport x=new SummarizeContamReport(args);
37
38 //Run the object
39 x.process(t);
40
41 //Close the print stream if it was redirected
42 Shared.closeStream(x.outstream);
43 }
44
45 public SummarizeContamReport(String[] args){
46
47 {//Preparse block for help, config files, and outstream
48 PreParser pp=new PreParser(args, getClass(), false);
49 args=pp.args;
50 outstream=pp.outstream;
51 }
52
53 Shared.capBuffers(4);
54 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
55 ReadWrite.setZipThreads(Shared.threads());
56
57 Parser parser=new Parser();
58 for(int i=0; i<args.length; i++){
59 String arg=args[i];
60 String[] split=arg.split("=");
61 String a=split[0].toLowerCase();
62 String b=split.length>1 ? split[1] : null;
63
64 if(a.equals("verbose")){
65 verbose=Parse.parseBoolean(b);
66 ReadWrite.verbose=verbose;
67 }else if(a.equals("minreads")){
68 minReads=Long.parseLong(b);
69 }else if(a.equals("minsequnits") || a.equals("minunits") || a.equals("minseqs")){
70 minSeqUnits=Long.parseLong(b);
71 }else if(a.equals("in")){
72 for(String term : b.split(",")){
73 in.add(term);
74 }
75 }else if(a.equals("tree")){
76 treeFile=b;
77 }else if(b==null && new File(arg).exists()){
78 in.add(arg);
79 }else if(parser.parse(arg, a, b)){
80 //do nothing
81 }else{
82 outstream.println("Unknown parameter "+args[i]);
83 assert(false) : "Unknown parameter "+args[i];
84 // throw new RuntimeException("Unknown parameter "+args[i]);
85 }
86 }
87
88 if("auto".equalsIgnoreCase(sizeFile)){sizeFile=TaxTree.defaultSizeFile();}
89
90 {//Process parser fields
91 Parser.processQuality();
92
93 overwrite=parser.overwrite;
94 append=parser.append;
95
96 out1=parser.out1;
97 }
98
99 if(in.isEmpty()){throw new RuntimeException("Error - at least one input file is required.");}
100
101 if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;}
102
103 if(!Tools.testOutputFiles(overwrite, append, false, out1)){
104 outstream.println((out1==null)+", "+out1);
105 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n");
106 }
107
108 ffout1=FileFormat.testOutput(out1, FileFormat.TEXT, null, true, overwrite, append, false);
109
110 ffinArray=new FileFormat[in.size()];
111 for(int i=0; i<in.size(); i++){
112 ffinArray[i]=FileFormat.testInput(in.get(i), FileFormat.TEXT, null, false, false);
113 }
114
115 tree=TaxTree.loadTaxTree(treeFile, System.err, true, false);
116 if(tree!=null){tree.loadSizeFile(sizeFile);}
117 }
118
119 void process(Timer t){
120
121 for(FileFormat ff : ffinArray){
122 if(ff.canRead()){
123 processOneFile(ff);
124 }else{
125 System.err.println("Skipping unreadable file "+ff.name());
126 }
127 }
128
129 printOutput();
130
131 t.stop();
132 outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, charsProcessed, 8));
133
134 if(errorState){
135 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
136 }
137 }
138
139 void processOneFile(FileFormat ff){
140
141
142 final TextFile tf;
143 {
144 tf=new TextFile(ff);
145 if(verbose){outstream.println("Started tf");}
146 }
147
148 {
149 String line;
150
151 line=tf.nextLine();
152 if(line.startsWith("CONTAM SUMMARY")){
153 line=tf.nextLine();
154 }
155 assert(line.startsWith("Examined ")) : line;
156 line=tf.nextLine();
157 assert(line.startsWith("|Taxonomy")) : line;
158
159 while(/*(maxReads<0 || linesProcessed<maxReads) && */(line=tf.nextLine())!=null){
160 linesProcessed++;
161 charsProcessed+=line.length();
162 if(line.startsWith("|")){
163 if(line.startsWith("|TOTAL")){break;}
164 processLine(line);
165 }else{
166 break;
167 }
168 }
169 }
170 errorState|=tf.close();
171 }
172
173 private void printOutput(){
174 final TextStreamWriter tsw;
175 {
176 tsw=new TextStreamWriter(ffout1);
177 tsw.start();
178 if(verbose){outstream.println("Started tsw");}
179 tsw.println("#Name\tSeqUnits\tReads\tTaxID\tClade\tsize\tcSize\tseqs\tcSeqs\tcNodes");
180 }
181
182 ArrayList<StringLongLong> list=new ArrayList<StringLongLong>(map.size());
183 list.addAll(map.values());
184 Collections.sort(list, new ComparatorA());
185 boolean filterA=minSeqUnits>1, filterB=minReads>1;
186 boolean filter=filterA || filterB;
187 for(StringLongLong sll : list){
188 if(sll.a>=minSeqUnits && sll.b>=minReads){
189 // if(!filter || (filterA && sll.a>=minSeqUnits) || (filterB && sll.b>=minReads)){
190 int tid=-1;
191 TaxNode tn;
192 // TaxNode tn=null;
193 TaxNode ancestor=null;
194
195 long size=0;
196 long cumulative_size=0;
197 long seqs=0;
198 long cumulative_seqs=0;
199 long cumulative_nodes=0;
200 if(tree!=null){
201 tid=tree.parseNameToTaxid(sll.s);
202 if(tid>=0){
203 tn=tree.getNode(tid);
204 ancestor=tree.getNodeAtLevelExtended(tid, TaxTree.SUPERKINGDOM_E);
205 size=tree.toSize(tn);
206 cumulative_size=tree.toSizeC(tn);
207 seqs=tree.toSeqs(tn);
208 cumulative_seqs=tree.toSeqsC(tn);
209 cumulative_nodes=tree.toNodes(tn);
210 }
211 }
212
213 tsw.println(sll.s+"\t"+sll.a+"\t"+sll.b+"\t"+tid+"\t"+(ancestor==null ? "null" : ancestor.name)+
214 "\t"+size+"\t"+cumulative_size+"\t"+seqs+"\t"+cumulative_seqs+"\t"+cumulative_nodes);
215 }
216 }
217 errorState|=tsw.poisonAndWait();
218 }
219
220 private void processLine(String line){
221 String[] split=line.split("\\|");
222 String[] split2=split[1].split(";");
223 String name=split2[split2.length-1];
224 try {
225 long a=Long.parseLong(split[2]);
226 long b=Long.parseLong(split[3]);
227 StringLongLong p=map.get(name);
228 if(p==null){
229 p=new StringLongLong(name, a, b);
230 map.put(name, p);
231 }else{
232 p.a+=a;
233 p.b+=b;
234 }
235 } catch (NumberFormatException e) {
236 // TODO Auto-generated catch block
237 e.printStackTrace();
238 System.err.println(line);
239 System.err.println(Arrays.toString(split));
240 System.err.println(Arrays.toString(split2));
241 shared.KillSwitch.kill();
242 }
243 }
244
245
246 /*--------------------------------------------------------------*/
247
248 class ComparatorA implements Comparator<StringLongLong> {
249
250 @Override
251 public int compare(StringLongLong x, StringLongLong y) {
252 if(x.a!=y.a){return x.a<y.a ? 1 : -1;}
253 if(x.b!=y.b){return x.b<y.b ? 1 : -1;}
254 return x.s.compareTo(y.s);
255 }
256
257 }
258
259 class ComparatorB implements Comparator<StringLongLong> {
260
261 @Override
262 public int compare(StringLongLong x, StringLongLong y) {
263 if(x.b!=y.b){return x.b<y.b ? 1 : -1;}
264 if(x.a!=y.a){return x.a<y.a ? 1 : -1;}
265 return x.s.compareTo(y.s);
266 }
267
268 }
269
270 class StringLongLong {
271
272 StringLongLong(String s_){
273 s=s_;
274 }
275
276 StringLongLong(String s_, long a_, long b_){
277 s=s_;
278 a=a_;
279 b=b_;
280 }
281
282 final String s;
283 long a;
284 long b;
285
286 }
287
288 /*--------------------------------------------------------------*/
289
290 private ArrayList<String> in=new ArrayList<String>();
291 private String out1=null;
292 private String treeFile="auto";
293 private String sizeFile="auto";
294
295 TaxTree tree=null;
296 private HashMap<String, StringLongLong> map=new HashMap<String, StringLongLong>();
297
298 /*--------------------------------------------------------------*/
299
300 long minReads=0;
301 long minSeqUnits=0;
302
303 long linesProcessed=0;
304 long charsProcessed=0;
305
306 /*--------------------------------------------------------------*/
307
308 private final FileFormat ffinArray[];
309 private final FileFormat ffout1;
310
311
312 /*--------------------------------------------------------------*/
313
314 private PrintStream outstream=System.err;
315 public static boolean verbose=false;
316 public boolean errorState=false;
317 private boolean overwrite=true;
318 private boolean append=false;
319
320 }