File: SummarizeContamReport.java

package info (click to toggle)
bbmap 39.20%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,024 kB
  • sloc: java: 312,743; sh: 18,099; python: 5,247; ansic: 2,074; perl: 96; makefile: 39; xml: 38
file content (320 lines) | stat: -rwxr-xr-x 8,454 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package driver;

import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

import fileIO.FileFormat;
import fileIO.ReadWrite;
import fileIO.TextFile;
import fileIO.TextStreamWriter;
import shared.Parse;
import shared.Parser;
import shared.PreParser;
import shared.Shared;
import shared.Timer;
import shared.Tools;
import tax.TaxNode;
import tax.TaxTree;

/**
 * @author Brian Bushnell
 * @date Oct 17, 2014
 *
 */
public class SummarizeContamReport {
	
	public static void main(String[] args){
		//Start a timer immediately upon code entrance.
		Timer t=new Timer();
		
		//Create an instance of this class
		SummarizeContamReport x=new SummarizeContamReport(args);
		
		//Run the object
		x.process(t);
		
		//Close the print stream if it was redirected
		Shared.closeStream(x.outstream);
	}
	
	public SummarizeContamReport(String[] args){
		
		{//Preparse block for help, config files, and outstream
			PreParser pp=new PreParser(args, getClass(), false);
			args=pp.args;
			outstream=pp.outstream;
		}
		
		Shared.capBuffers(4);
		ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
		ReadWrite.setZipThreads(Shared.threads());
		
		Parser parser=new Parser();
		for(int i=0; i<args.length; i++){
			String arg=args[i];
			String[] split=arg.split("=");
			String a=split[0].toLowerCase();
			String b=split.length>1 ? split[1] : null;

			if(a.equals("verbose")){
				verbose=Parse.parseBoolean(b);
				ReadWrite.verbose=verbose;
			}else if(a.equals("minreads")){
				minReads=Long.parseLong(b);
			}else if(a.equals("minsequnits") || a.equals("minunits") || a.equals("minseqs")){
				minSeqUnits=Long.parseLong(b);
			}else if(a.equals("in")){
				for(String term : b.split(",")){
					in.add(term);
				}
			}else if(a.equals("tree")){
				treeFile=b;
			}else if(b==null && new File(arg).exists()){
				in.add(arg);
			}else if(parser.parse(arg, a, b)){
				//do nothing
			}else{
				outstream.println("Unknown parameter "+args[i]);
				assert(false) : "Unknown parameter "+args[i];
				//				throw new RuntimeException("Unknown parameter "+args[i]);
			}
		}
		
		if("auto".equalsIgnoreCase(sizeFile)){sizeFile=TaxTree.defaultSizeFile();}
		
		{//Process parser fields
			Parser.processQuality();
			
			overwrite=parser.overwrite;
			append=parser.append;

			out1=parser.out1;
		}
		
		if(in.isEmpty()){throw new RuntimeException("Error - at least one input file is required.");}

		if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;}
		
		if(!Tools.testOutputFiles(overwrite, append, false, out1)){
			outstream.println((out1==null)+", "+out1);
			throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n");
		}
		
		ffout1=FileFormat.testOutput(out1, FileFormat.TEXT, null, true, overwrite, append, false);

		ffinArray=new FileFormat[in.size()];
		for(int i=0; i<in.size(); i++){
			ffinArray[i]=FileFormat.testInput(in.get(i), FileFormat.TEXT, null, false, false);
		}
		
		tree=TaxTree.loadTaxTree(treeFile, System.err, true, false);
		if(tree!=null){tree.loadSizeFile(sizeFile);}
	}
	
	void process(Timer t){
		
		for(FileFormat ff : ffinArray){
			if(ff.canRead()){
				processOneFile(ff);
			}else{
				System.err.println("Skipping unreadable file "+ff.name());
			}
		}
		
		printOutput();
		
		t.stop();
		outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, charsProcessed, 8));
		
		if(errorState){
			throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
		}
	}
	
	void processOneFile(FileFormat ff){

		
		final TextFile tf;
		{
			tf=new TextFile(ff);
			if(verbose){outstream.println("Started tf");}
		}
		
		{
			String line;
			
			line=tf.nextLine();
			if(line.startsWith("CONTAM SUMMARY")){
				line=tf.nextLine();
			}
			assert(line.startsWith("Examined ")) : line;
			line=tf.nextLine();
			assert(line.startsWith("|Taxonomy")) : line;
			
			while(/*(maxReads<0 || linesProcessed<maxReads) && */(line=tf.nextLine())!=null){
				linesProcessed++;
				charsProcessed+=line.length();
				if(line.startsWith("|")){
					if(line.startsWith("|TOTAL")){break;}
					processLine(line);
				}else{
					break;
				}
			}
		}
		errorState|=tf.close();
	}
	
	private void printOutput(){
		final TextStreamWriter tsw;
		{
			tsw=new TextStreamWriter(ffout1);
			tsw.start();
			if(verbose){outstream.println("Started tsw");}
			tsw.println("#Name\tSeqUnits\tReads\tTaxID\tClade\tsize\tcSize\tseqs\tcSeqs\tcNodes");
		}
		
		ArrayList<StringLongLong> list=new ArrayList<StringLongLong>(map.size());
		list.addAll(map.values());
		Collections.sort(list, new ComparatorA());
		boolean filterA=minSeqUnits>1, filterB=minReads>1;
		boolean filter=filterA || filterB;
		for(StringLongLong sll : list){
			if(sll.a>=minSeqUnits && sll.b>=minReads){
//			if(!filter || (filterA && sll.a>=minSeqUnits) || (filterB && sll.b>=minReads)){
				int tid=-1;
				TaxNode tn;
				//			TaxNode tn=null;
				TaxNode ancestor=null;

				long size=0;
				long cumulative_size=0;
				long seqs=0;
				long cumulative_seqs=0;
				long cumulative_nodes=0;
				if(tree!=null){
					tid=tree.parseNameToTaxid(sll.s);
					if(tid>=0){
						tn=tree.getNode(tid);
						ancestor=tree.getNodeAtLevelExtended(tid, TaxTree.SUPERKINGDOM_E);
						size=tree.toSize(tn);
						cumulative_size=tree.toSizeC(tn);
						seqs=tree.toSeqs(tn);
						cumulative_seqs=tree.toSeqsC(tn);
						cumulative_nodes=tree.toNodes(tn);
					}
				}
				
				tsw.println(sll.s+"\t"+sll.a+"\t"+sll.b+"\t"+tid+"\t"+(ancestor==null ? "null" : ancestor.name)+
						"\t"+size+"\t"+cumulative_size+"\t"+seqs+"\t"+cumulative_seqs+"\t"+cumulative_nodes);
			}
		}
		errorState|=tsw.poisonAndWait();
	}
	
	private void processLine(String line){
		String[] split=line.split("\\|");
		String[] split2=split[1].split(";");
		String name=split2[split2.length-1];
		try {
			long a=Long.parseLong(split[2]);
			long b=Long.parseLong(split[3]);
			StringLongLong p=map.get(name);
			if(p==null){
				p=new StringLongLong(name, a, b);
				map.put(name, p);
			}else{
				p.a+=a;
				p.b+=b;
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.err.println(line);
			System.err.println(Arrays.toString(split));
			System.err.println(Arrays.toString(split2));
			shared.KillSwitch.kill();
		}
	}
	
	
	/*--------------------------------------------------------------*/
	
	class ComparatorA implements Comparator<StringLongLong> {

		@Override
		public int compare(StringLongLong x, StringLongLong y) {
			if(x.a!=y.a){return  x.a<y.a ? 1 : -1;}
			if(x.b!=y.b){return  x.b<y.b ? 1 : -1;}
			return x.s.compareTo(y.s);
		}
		
	}
	
	class ComparatorB implements Comparator<StringLongLong> {

		@Override
		public int compare(StringLongLong x, StringLongLong y) {
			if(x.b!=y.b){return  x.b<y.b ? 1 : -1;}
			if(x.a!=y.a){return  x.a<y.a ? 1 : -1;}
			return x.s.compareTo(y.s);
		}
		
	}
	
	class StringLongLong {
		
		StringLongLong(String s_){
			s=s_;
		}
		
		StringLongLong(String s_, long a_, long b_){
			s=s_;
			a=a_;
			b=b_;
		}
		
		final String s;
		long a;
		long b;
		
	}
	
	/*--------------------------------------------------------------*/
	
	private ArrayList<String> in=new ArrayList<String>();
	private String out1=null;
	private String treeFile="auto";
	private String sizeFile="auto";
	
	TaxTree tree=null;
	private HashMap<String, StringLongLong> map=new HashMap<String, StringLongLong>();
	
	/*--------------------------------------------------------------*/

	long minReads=0;
	long minSeqUnits=0;
	
	long linesProcessed=0;
	long charsProcessed=0;
	
	/*--------------------------------------------------------------*/
	
	private final FileFormat ffinArray[];
	private final FileFormat ffout1;
	
	
	/*--------------------------------------------------------------*/
	
	private PrintStream outstream=System.err;
	public static boolean verbose=false;
	public boolean errorState=false;
	private boolean overwrite=true;
	private boolean append=false;
	
}