File: CompareSamFiles.java

package info (click to toggle)
bbmap 38.90%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,520 kB
  • sloc: java: 265,882; sh: 14,954; python: 5,247; ansic: 2,074; perl: 96; xml: 38; makefile: 37
file content (387 lines) | stat: -rwxr-xr-x 11,890 bytes parent folder | download | duplicates (4)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package align2;

import java.io.File;
import java.util.Arrays;
import java.util.BitSet;

import dna.Data;
import fileIO.TextFile;
import shared.KillSwitch;
import shared.Parse;
import shared.PreParser;
import shared.Tools;
import stream.Read;
import stream.SamLine;
import stream.SiteScore;

/** Generate a file containing reads mapped correctly in one file and incorrectly in another file. */
public class CompareSamFiles {
	
	
	public static void main(String[] args){

		{//Preparse block for help, config files, and outstream
			PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false);
			args=pp.args;
			//outstream=pp.outstream;
		}

		String in1=null;
		String in2=null;
		long reads=-1;
		
		for(int i=0; i<args.length; i++){
			final String arg=args[i];
			final String[] split=arg.split("=");
			String a=split[0].toLowerCase();
			String b=split.length>1 ? split[1] : null;
			
			if(a.equals("path") || a.equals("root")){
				Data.setPath(b);
			}else if(a.equals("in") || a.equals("in1")){
				in1=b;
			}else if(a.equals("in2")){
				in2=b;
			}else if(a.equals("parsecustom")){
				parsecustom=Parse.parseBoolean(b);
			}else if(a.equals("thresh")){
				THRESH2=Integer.parseInt(b);
			}else if(a.equals("printerr")){
				printerr=Parse.parseBoolean(b);
//			}else if(a.equals("ssaha2") || a.equals("subtractleadingclip")){
//				SamLine.SUBTRACT_LEADING_SOFT_CLIP=Parse.parseBoolean(b);
			}else if(a.equals("blasr")){
				BLASR=Parse.parseBoolean(b);
			}else if(a.equals("q") || a.equals("quality") || a.startsWith("minq")){
				minQuality=Integer.parseInt(b);
			}else if(in1==null && i==0 && args[i].indexOf('=')<0 && (a.startsWith("stdin") || new File(args[i]).exists())){
				in1=args[i];
			}else if(in2==null && i==1 && args[i].indexOf('=')<0 && (a.startsWith("stdin") || new File(args[i]).exists())){
				in2=args[i];
			}else if(a.equals("reads")){
				reads=Parse.parseKMG(b);
			}else if(i==2 && args[i].indexOf('=')<0 && Tools.isDigit(a.charAt(0))){
				reads=Parse.parseKMG(a);
			}
		}

		assert(in1!=null) : args[0]+".exists() ? "+new File(args[0]).exists();
//		assert(in2!=null) : args[1]+".exists() ? "+new File(args[1]).exists();
		
		if(reads<1){
//			assert(false) : "Number of expected reads was not specified.  Please add a parameter reads=<number> or disable assertions.";
			reads=100000;
			System.err.println("Warning - number of expected reads was not specified.");
		}

		TextFile tf1=new TextFile(in1, false);
		TextFile tf2=null;
		if(in2!=null){tf2=new TextFile(in2, false);}

		BitSet truePos1=new BitSet((int)reads);
		BitSet falsePos1=new BitSet((int)reads);
		BitSet truePos2=new BitSet((int)reads);
		BitSet falsePos2=new BitSet((int)reads);
		
		String s=null;
		
		TextFile tf;
		{
			tf=tf1;
			for(s=tf.nextLine(); s!=null; s=tf.nextLine()){
				char c=s.charAt(0);
				if(c!='@'/* && c!=' ' && c!='\t'*/){
					SamLine sl=new SamLine(s);
					if(sl.primary()){
						Read r=sl.toRead(parsecustom);
						if(parsecustom && r.originalSite==null){
							assert(false);
							System.err.println("Turned off custom parsing.");
							parsecustom=false;
						}
						//System.out.println(r);
						int type=type(r, sl);
						int id=(int)r.numericID;
						if(type==2){truePos1.set(id);}
						else if(type>2){falsePos1.set(id);}
					}
				}
			}
			tf.close();
		}
		if(tf2!=null){
			tf=tf2;
			for(s=tf.nextLine(); s!=null; s=tf.nextLine()){
				char c=s.charAt(0);
				if(c!='@'/* && c!=' ' && c!='\t'*/){
					SamLine sl=new SamLine(s);
					if(sl.primary()){
						Read r=sl.toRead(parsecustom);
						if(parsecustom && r.originalSite==null){
							assert(false);
							System.err.println("Turned off custom parsing.");
							parsecustom=false;
						}
						//System.out.println(r);
						int type=type(r, sl);
						int id=(int)r.numericID;
						if(type==2){truePos2.set(id);}
						else if(type>2){falsePos2.set(id);}
					}
				}
			}
			tf.close();
		}
		
		
		
		BitSet added=new BitSet((int)reads);
		{
			tf=tf1;
			tf.reset();
			for(s=tf.nextLine(); s!=null; s=tf.nextLine()){
				char c=s.charAt(0);
				if(c!='@'/* && c!=' ' && c!='\t'*/){
					SamLine sl=new SamLine(s);
//					assert(false) : s+", "+truePos1.cardinality()+", "+truePos2.cardinality()+", "+falsePos1.cardinality()+", "+falsePos2.cardinality()+", ";
					if(sl.primary()){
						Read r=sl.toRead(parsecustom);
						int id=(int)r.numericID;
						if(!added.get(id)){
//							if(truePos1.get(id)!=truePos2.get(id) || falsePos1.get(id)!=falsePos2.get(id)){
//								System.out.println(s);
//								added.set(id);
//							}
//							if(falsePos1.get(id) && truePos2.get(id)){
//								System.out.println(s);
//								added.set(id);
//							}
							if(falsePos1.get(id) && !falsePos2.get(id)){
								System.out.println(s);
								added.set(id);
							}
						}
					}
				}
			}
			tf.close();
		}
		if(tf2!=null){
			tf=tf2;
			tf.reset();
			for(s=tf.nextLine(); s!=null; s=tf.nextLine()){
				char c=s.charAt(0);
				if(c!='@'/* && c!=' ' && c!='\t'*/){
					SamLine sl=new SamLine(s);
					if(sl.primary()){
						Read r=sl.toRead(parsecustom);
						int id=(int)r.numericID;
						if(!added.get(id)){
//							if(truePos1.get(id)!=truePos2.get(id) || falsePos1.get(id)!=falsePos2.get(id)){
//								System.out.println(s);
//								added.set(id);
//							}
//							if(falsePos2.get(id) && truePos1.get(id)){
//								System.out.println(s);
//								added.set(id);
//							}
							if(falsePos2.get(id) && !falsePos1.get(id)){
								System.out.println(s);
								added.set(id);
							}
						}
					}
				}
			}
			tf.close();
		}
	}
	

	public static void calcStatistics1(final Read r, SamLine sl){
		
		int THRESH=0;
		primary++;
		
		if(r.discarded()/* || r.mapScore==0*/){
			discarded++;
			unmapped++;
		}else if(r.ambiguous()){
//			assert(r.mapped()) : "\n"+r+"\n"+sl+"\n";
			if(r.mapped()){mapped++;}
			ambiguous++;
		}else if(r.mapScore<1){
			unmapped++;
		}else if(r.mapScore<=minQuality){
			if(r.mapped()){mapped++;}
			ambiguous++;
		}else{
			if(!r.mapped()){
				unmapped++;
			}else{
				mapped++;
				mappedRetained++;
				
				if(parsecustom){
					SiteScore os=r.originalSite;
					assert(os!=null);
					if(os!=null){
						int trueChrom=os.chrom;
						byte trueStrand=os.strand;
						int trueStart=os.start;
						int trueStop=os.stop;
						SiteScore ss=new SiteScore(r.chrom, r.strand(), r.start, r.stop, 0, 0);
						byte[] originalContig=sl.originalContig();
						if(BLASR){
							originalContig=(originalContig==null || Tools.indexOf(originalContig, (byte)'/')<0 ? originalContig :
								KillSwitch.copyOfRange(originalContig, 0, Tools.lastIndexOf(originalContig, (byte)'/')));
						}
						int cstart=sl.originalContigStart();

						boolean strict=isCorrectHit(ss, trueChrom, trueStrand, trueStart, trueStop, THRESH, originalContig, sl.rname(), cstart);
						boolean loose=isCorrectHitLoose(ss, trueChrom, trueStrand, trueStart, trueStop, THRESH+THRESH2, originalContig, sl.rname(), cstart);

						//				if(!strict){
						//					System.out.println(ss+", "+new String(originalContig)+", "+new String(sl.rname()));
						//					assert(false);
						//				}

						//				System.out.println("loose = "+loose+" for "+r.toText());

						if(loose){
							//					System.err.println("TPL\t"+trueChrom+", "+trueStrand+", "+trueStart+", "+trueStop+"\tvs\t"
							//							+ss.chrom+", "+ss.strand+", "+ss.start+", "+ss.stop);
							truePositiveLoose++;
						}else{
							//					System.err.println("FPL\t"+trueChrom+", "+trueStrand+", "+trueStart+", "+trueStop+"\tvs\t"
							//							+ss.chrom+", "+ss.strand+", "+ss.start+", "+ss.stop);
							falsePositiveLoose++;
						}

						if(strict){
							//					System.err.println("TPS\t"+trueStart+", "+trueStop+"\tvs\t"+ss.start+", "+ss.stop);
							truePositiveStrict++;
						}else{
							//					System.err.println("FPS\t"+trueStart+", "+trueStop+"\tvs\t"+ss.start+", "+ss.stop);
							falsePositiveStrict++;
						}
					}
				}
			}
		}
	}
	
	

	public static int type(final Read r, SamLine sl){
		
		int THRESH=0;
		primary++;
		
		if(r.discarded()/* || r.mapScore==0*/){
			return 0;
		}else if(r.ambiguous()){
			return 1;
		}else if(r.mapScore<1){
			return 0;
		}else if(r.mapScore<=minQuality){
			return 1;
		}else{
			if(!r.mapped()){
				return 0;
			}else{
				
				if(parsecustom){
					SiteScore os=r.originalSite;
					assert(os!=null);
					if(os!=null){
						int trueChrom=os.chrom;
						byte trueStrand=os.strand;
						int trueStart=os.start;
						int trueStop=os.stop;
						SiteScore ss=new SiteScore(r.chrom, r.strand(), r.start, r.stop, 0, 0);
						byte[] originalContig=sl.originalContig();
						if(BLASR){
							originalContig=(originalContig==null || Tools.indexOf(originalContig, (byte)'/')<0 ? originalContig :
								KillSwitch.copyOfRange(originalContig, 0, Tools.lastIndexOf(originalContig, (byte)'/')));
						}
						int cstart=sl.originalContigStart();

						boolean strict=isCorrectHit(ss, trueChrom, trueStrand, trueStart, trueStop, THRESH, originalContig, sl.rname(), cstart);
						boolean loose=isCorrectHitLoose(ss, trueChrom, trueStrand, trueStart, trueStop, THRESH+THRESH2, originalContig, sl.rname(), cstart);

						if(strict){return 2;}
						if(loose){return 3;}
						return 4;
					}
				}
			}
		}
		return 0;
	}
	
	
	
	public static boolean isCorrectHit(SiteScore ss, int trueChrom, byte trueStrand, int trueStart, int trueStop, int thresh,
			byte[] originalContig, byte[] contig, int cstart){
		if(ss.strand!=trueStrand){return false;}
		if(originalContig!=null){
			if(!Arrays.equals(originalContig, contig)){return false;}
		}else{
			if(ss.chrom!=trueChrom){return false;}
		}

		assert(ss.stop>ss.start) : ss.toText()+", "+trueStart+", "+trueStop;
		assert(trueStop>trueStart) : ss.toText()+", "+trueStart+", "+trueStop;
		int cstop=cstart+trueStop-trueStart;
//		return (absdif(ss.start, trueStart)<=thresh && absdif(ss.stop, trueStop)<=thresh);
		return (absdif(ss.start, cstart)<=thresh && absdif(ss.stop, cstop)<=thresh);
	}
	
	
	public static boolean isCorrectHitLoose(SiteScore ss, int trueChrom, byte trueStrand, int trueStart, int trueStop, int thresh,
			byte[] originalContig, byte[] contig, int cstart){
		if(ss.strand!=trueStrand){return false;}
		if(originalContig!=null){
			if(!Arrays.equals(originalContig, contig)){return false;}
		}else{
			if(ss.chrom!=trueChrom){return false;}
		}

		assert(ss.stop>ss.start) : ss.toText()+", "+trueStart+", "+trueStop;
		assert(trueStop>trueStart) : ss.toText()+", "+trueStart+", "+trueStop;
		int cstop=cstart+trueStop-trueStart;
//		return (absdif(ss.start, trueStart)<=thresh || absdif(ss.stop, trueStop)<=thresh);
		return (absdif(ss.start, cstart)<=thresh || absdif(ss.stop, cstop)<=thresh);
	}
	
	private static final int absdif(int a, int b){
		return a>b ? a-b : b-a;
	}

	public static int truePositiveStrict=0;
	public static int falsePositiveStrict=0;
	
	public static int truePositiveLoose=0;
	public static int falsePositiveLoose=0;

	public static int mapped=0;
	public static int mappedRetained=0;
	public static int unmapped=0;
	
	public static int discarded=0;
	public static int ambiguous=0;

	public static long lines=0;
	public static long primary=0;
	public static long secondary=0;
	
	public static int minQuality=3;

	public static boolean parsecustom=true;
	public static boolean printerr=false;

	public static int THRESH2=20;
	public static boolean BLASR=false;
	
}