File: SummarizeSealStats.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 (248 lines) | stat: -rwxr-xr-x 6,686 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
package driver;

import java.io.File;
import java.util.ArrayList;

import fileIO.TextFile;
import fileIO.TextStreamWriter;
import shared.Parse;
import shared.Parser;
import shared.PreParser;
import shared.Tools;

/**
 * @author Brian Bushnell
 * @date May 8, 2015
 *
 */
public class SummarizeSealStats {
	
	/**
	 * Code entrance from the command line.
	 * @param args Command line arguments
	 */
	public static void main(String[] args){
		//Create a new SummarizeSealStats instance
		SummarizeSealStats sss=new SummarizeSealStats(args);
		
		///And run it
		sss.summarize();
	}
	
	public SummarizeSealStats(String[] args){

		{//Preparse block for help, config files, and outstream
			PreParser pp=new PreParser(args, getClass(), false);
			args=pp.args;
			//outstream=pp.outstream;
		}
		
		ArrayList<String> names=new ArrayList<String>();
		Parser parser=new Parser();
		
		/* Parse arguments */
		for(int i=0; i<args.length; i++){

			final String arg=args[i];
			String[] split=arg.split("=");
			String a=split[0].toLowerCase();
			String b=split.length>1 ? split[1] : null;
			
			if(a.equals("printtotal") || a.equals("pt")){
				printTotal=Parse.parseBoolean(b);
			}else if(a.equals("ignoresametaxa")){
				ignoreSameTaxa=Parse.parseBoolean(b);
			}else if(a.equals("ignoresamebarcode") || a.equals("ignoresameindex")){
				ignoreSameBarcode=Parse.parseBoolean(b);
			}else if(a.equals("ignoresamelocation") || a.equals("ignoresameloc")){
				ignoreSameLocation=Parse.parseBoolean(b);
			}else if(a.equals("usetotal") || a.equals("totaldenominator") || a.equals("totald") || a.equals("td")){
				totalDenominator=Parse.parseBoolean(b);
			}else if(parser.parse(arg, a, b)){
				//do nothing
			}else if(!arg.contains("=")){
				String[] x=(new File(arg).exists() ? new String[] {arg} : arg.split(","));
				for(String x2 : x){names.add(x2);}
			}else{
				throw new RuntimeException("Unknown parameter "+arg);
			}
		}
		
		{//Process parser fields
			out=(parser.out1==null ? "stdout" : parser.out1);
			if(parser.in1!=null){
				String[] x=(new File(parser.in1).exists() ? new String[] {parser.in1} : parser.in1.split(","));
				for(String x2 : x){names.add(x2);}
			}
		}

		in=new ArrayList<String>();
		for(String s : names){
			Tools.getFileOrFiles(s, in, false, false, false, true);
		}
	}
	
	public void summarize(){
		ArrayList<SealSummary> list=new ArrayList<SealSummary>();
		
		SealSummary total=new SealSummary(null);
		total.pname="TOTAL";
		for(String fname : in){
			SealSummary ss=new SealSummary(fname);
			list.add(ss);
			total.add(ss);
		}
		
		TextStreamWriter tsw=new TextStreamWriter(out, true, false, false);
		tsw.start();
		tsw.print("#File\tPrimary_Name\tPrimary_Count\tOther_Count\tPrimary_Bases\tOther_Bases\tOther_ppm\n");
		if(printTotal){
			tsw.println(total.toString());
		}
		for(SealSummary ss : list){
			tsw.println(ss.toString());
		}
		tsw.poisonAndWait();
	}
	
	private class SealSummary {
		
		SealSummary(String fname_){
			fname=fname_;
			if(fname!=null){
				if(ignoreSameTaxa || ignoreSameBarcode || ignoreSameLocation){
					cleanAndSummarize();
				}else{
					summarize();
				}
			}
		}
		
		public void add(SealSummary ss){
			pcount+=ss.pcount;
			ocount+=ss.ocount;
			tcount+=ss.tcount;
			pbases+=ss.pbases;
			obases+=ss.obases;
			tbases+=ss.tbases;
			
			if(totalDenominator && tbases>0){
				ppm=obases*1000000.0/tbases;
			}else{
				ppm=(obases==0 ? 0 : obases*1000000.0/(obases+pbases));
			}
		}
		
		@Override
		public String toString(){
			return Tools.format("%s\t%s\t%d\t%d\t%d\t%d\t%.2f", fname, pname, pcount, ocount, pbases, obases, ppm);
		}
		
		private void summarize(){
			TextFile tf=new TextFile(fname);
			for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){
				if(line.startsWith("#")){
					if(line.startsWith("#Total")){
						String[] split=line.split("\t");
						tcount=Long.parseLong(split[1]);
						tbases=Long.parseLong(split[2]);
					}
				}else{
					String[] split=line.split("\t");
					long count=Long.parseLong(split[1]);
					long bases=Long.parseLong(split[3]);
					if(pcount==0 || bases>pbases || (bases==pbases && count>pcount)){
						pname=split[0];
						ocount+=pcount;
						obases+=pbases;
						pcount=count;
						pbases=bases;
					}else{
						ocount+=count;
						obases+=bases;
					}
				}
			}
			tf.close();
			if(totalDenominator && tbases>0){
				ppm=obases*1000000.0/tbases;
			}else{
				ppm=(obases==0 ? 0 : obases*1000000.0/(obases+pbases));
			}
		}
		
		public void cleanAndSummarize(){
			TextFile tf=new TextFile(fname);
			for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){
				if(line.startsWith("#")){
					if(line.startsWith("#Total")){
						String[] split=line.split("\t");
						tcount=Long.parseLong(split[1]);
						tbases=Long.parseLong(split[2]);
					}
				}else{
					String[] split=line.split("\t");
					String[] name=split[0].toLowerCase().split(",");
					String[] barcode=name[0].split("-");
					
					long count=Long.parseLong(split[1]);
					long bases=Long.parseLong(split[3]);
					if(pcount==0 || bases>pbases || (bases==pbases && count>pcount)){
						name0=name;
						barcode0=barcode;
						pname=split[0];
						ocount+=pcount;
						obases+=pbases;
						pcount=count;
						pbases=bases;
					}else{
						boolean process=true;
						if(ignoreSameTaxa){
							if(name[2].contains(name0[2]) || name0[2].contains(name[2])){
								process=false;
							}
						}
						if(ignoreSameBarcode){
							if(barcode[0].equals(barcode0[0]) || barcode[1].equals(barcode0[1])){
								process=false;
							}
						}
						if(ignoreSameLocation){
							assert(name.length==4) : "Too many delimiters: "+name.length+"\n"+line+"\n";
							if(name[3].equals(name0[3])){
								process=false;
							}
						}
						if(process){
							ocount+=count;
							obases+=bases;
						}
					}
				}
			}
			tf.close();
			if(totalDenominator && tbases>0){
				ppm=obases*1000000.0/tbases;
			}else{
				ppm=(obases==0 ? 0 : obases*1000000.0/(obases+pbases));
			}
		}
		
		final String fname;
		String pname=null;
		long pcount=0, ocount=0, tcount=0;
		long pbases=0, obases=0, tbases=0;
		double ppm;
		String[] name0=null, barcode0=null;
		
	}
	
	final ArrayList<String> in;
	final String out;
	boolean ignoreSameTaxa=false;
	boolean ignoreSameBarcode=false;
	boolean ignoreSameLocation=false;
	boolean totalDenominator=false;
	boolean printTotal=true;
	
}