Debian Codegraph @

current/align2/CompareSamFiles.java

raw symbols SCIP
1 package align2;
2
3 import java.io.File;
4 import java.util.Arrays;
5 import java.util.BitSet;
6
7 import dna.Data;
8 import fileIO.TextFile;
9 import shared.KillSwitch;
10 import shared.Parse;
11 import shared.PreParser;
12 import shared.Tools;
13 import stream.Read;
14 import stream.SamLine;
15 import stream.SiteScore;
16
17 /** Generate a file containing reads mapped correctly in one file and incorrectly in another file. */
18 public class CompareSamFiles {
19
20
21 public static void main(String[] args){
22
23 {//Preparse block for help, config files, and outstream
24 PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false);
25 args=pp.args;
26 //outstream=pp.outstream;
27 }
28
29 String in1=null;
30 String in2=null;
31 long reads=-1;
32
33 for(int i=0; i<args.length; i++){
34 final String arg=args[i];
35 final String[] split=arg.split("=");
36 String a=split[0].toLowerCase();
37 String b=split.length>1 ? split[1] : null;
38
39 if(a.equals("path") || a.equals("root")){
40 Data.setPath(b);
41 }else if(a.equals("in") || a.equals("in1")){
42 in1=b;
43 }else if(a.equals("in2")){
44 in2=b;
45 }else if(a.equals("parsecustom")){
46 parsecustom=Parse.parseBoolean(b);
47 }else if(a.equals("thresh")){
48 THRESH2=Integer.parseInt(b);
49 }else if(a.equals("printerr")){
50 printerr=Parse.parseBoolean(b);
51 // }else if(a.equals("ssaha2") || a.equals("subtractleadingclip")){
52 // SamLine.SUBTRACT_LEADING_SOFT_CLIP=Parse.parseBoolean(b);
53 }else if(a.equals("blasr")){
54 BLASR=Parse.parseBoolean(b);
55 }else if(a.equals("q") || a.equals("quality") || a.startsWith("minq")){
56 minQuality=Integer.parseInt(b);
57 }else if(in1==null && i==0 && args[i].indexOf('=')<0 && (a.startsWith("stdin") || new File(args[i]).exists())){
58 in1=args[i];
59 }else if(in2==null && i==1 && args[i].indexOf('=')<0 && (a.startsWith("stdin") || new File(args[i]).exists())){
60 in2=args[i];
61 }else if(a.equals("reads")){
62 reads=Parse.parseKMG(b);
63 }else if(i==2 && args[i].indexOf('=')<0 && Tools.isDigit(a.charAt(0))){
64 reads=Parse.parseKMG(a);
65 }
66 }
67
68 assert(in1!=null) : args[0]+".exists() ? "+new File(args[0]).exists();
69 // assert(in2!=null) : args[1]+".exists() ? "+new File(args[1]).exists();
70
71 if(reads<1){
72 // assert(false) : "Number of expected reads was not specified. Please add a parameter reads=<number> or disable assertions.";
73 reads=100000;
74 System.err.println("Warning - number of expected reads was not specified.");
75 }
76
77 TextFile tf1=new TextFile(in1, false);
78 TextFile tf2=null;
79 if(in2!=null){tf2=new TextFile(in2, false);}
80
81 BitSet truePos1=new BitSet((int)reads);
82 BitSet falsePos1=new BitSet((int)reads);
83 BitSet truePos2=new BitSet((int)reads);
84 BitSet falsePos2=new BitSet((int)reads);
85
86 String s=null;
87
88 TextFile tf;
89 {
90 tf=tf1;
91 for(s=tf.nextLine(); s!=null; s=tf.nextLine()){
92 char c=s.charAt(0);
93 if(c!='@'/* && c!=' ' && c!='\t'*/){
94 SamLine sl=new SamLine(s);
95 if(sl.primary()){
96 Read r=sl.toRead(parsecustom);
97 if(parsecustom && r.originalSite==null){
98 assert(false);
99 System.err.println("Turned off custom parsing.");
100 parsecustom=false;
101 }
102 //System.out.println(r);
103 int type=type(r, sl);
104 int id=(int)r.numericID;
105 if(type==2){truePos1.set(id);}
106 else if(type>2){falsePos1.set(id);}
107 }
108 }
109 }
110 tf.close();
111 }
112 if(tf2!=null){
113 tf=tf2;
114 for(s=tf.nextLine(); s!=null; s=tf.nextLine()){
115 char c=s.charAt(0);
116 if(c!='@'/* && c!=' ' && c!='\t'*/){
117 SamLine sl=new SamLine(s);
118 if(sl.primary()){
119 Read r=sl.toRead(parsecustom);
120 if(parsecustom && r.originalSite==null){
121 assert(false);
122 System.err.println("Turned off custom parsing.");
123 parsecustom=false;
124 }
125 //System.out.println(r);
126 int type=type(r, sl);
127 int id=(int)r.numericID;
128 if(type==2){truePos2.set(id);}
129 else if(type>2){falsePos2.set(id);}
130 }
131 }
132 }
133 tf.close();
134 }
135
136
137
138 BitSet added=new BitSet((int)reads);
139 {
140 tf=tf1;
141 tf.reset();
142 for(s=tf.nextLine(); s!=null; s=tf.nextLine()){
143 char c=s.charAt(0);
144 if(c!='@'/* && c!=' ' && c!='\t'*/){
145 SamLine sl=new SamLine(s);
146 // assert(false) : s+", "+truePos1.cardinality()+", "+truePos2.cardinality()+", "+falsePos1.cardinality()+", "+falsePos2.cardinality()+", ";
147 if(sl.primary()){
148 Read r=sl.toRead(parsecustom);
149 int id=(int)r.numericID;
150 if(!added.get(id)){
151 // if(truePos1.get(id)!=truePos2.get(id) || falsePos1.get(id)!=falsePos2.get(id)){
152 // System.out.println(s);
153 // added.set(id);
154 // }
155 // if(falsePos1.get(id) && truePos2.get(id)){
156 // System.out.println(s);
157 // added.set(id);
158 // }
159 if(falsePos1.get(id) && !falsePos2.get(id)){
160 System.out.println(s);
161 added.set(id);
162 }
163 }
164 }
165 }
166 }
167 tf.close();
168 }
169 if(tf2!=null){
170 tf=tf2;
171 tf.reset();
172 for(s=tf.nextLine(); s!=null; s=tf.nextLine()){
173 char c=s.charAt(0);
174 if(c!='@'/* && c!=' ' && c!='\t'*/){
175 SamLine sl=new SamLine(s);
176 if(sl.primary()){
177 Read r=sl.toRead(parsecustom);
178 int id=(int)r.numericID;
179 if(!added.get(id)){
180 // if(truePos1.get(id)!=truePos2.get(id) || falsePos1.get(id)!=falsePos2.get(id)){
181 // System.out.println(s);
182 // added.set(id);
183 // }
184 // if(falsePos2.get(id) && truePos1.get(id)){
185 // System.out.println(s);
186 // added.set(id);
187 // }
188 if(falsePos2.get(id) && !falsePos1.get(id)){
189 System.out.println(s);
190 added.set(id);
191 }
192 }
193 }
194 }
195 }
196 tf.close();
197 }
198 }
199
200
201 public static void calcStatistics1(final Read r, SamLine sl){
202
203 int THRESH=0;
204 primary++;
205
206 if(r.discarded()/* || r.mapScore==0*/){
207 discarded++;
208 unmapped++;
209 }else if(r.ambiguous()){
210 // assert(r.mapped()) : "\n"+r+"\n"+sl+"\n";
211 if(r.mapped()){mapped++;}
212 ambiguous++;
213 }else if(r.mapScore<1){
214 unmapped++;
215 }else if(r.mapScore<=minQuality){
216 if(r.mapped()){mapped++;}
217 ambiguous++;
218 }else{
219 if(!r.mapped()){
220 unmapped++;
221 }else{
222 mapped++;
223 mappedRetained++;
224
225 if(parsecustom){
226 SiteScore os=r.originalSite;
227 assert(os!=null);
228 if(os!=null){
229 int trueChrom=os.chrom;
230 byte trueStrand=os.strand;
231 int trueStart=os.start;
232 int trueStop=os.stop;
233 SiteScore ss=new SiteScore(r.chrom, r.strand(), r.start, r.stop, 0, 0);
234 byte[] originalContig=sl.originalContig();
235 if(BLASR){
236 originalContig=(originalContig==null || Tools.indexOf(originalContig, (byte)'/')<0 ? originalContig :
237 KillSwitch.copyOfRange(originalContig, 0, Tools.lastIndexOf(originalContig, (byte)'/')));
238 }
239 int cstart=sl.originalContigStart();
240
241 boolean strict=isCorrectHit(ss, trueChrom, trueStrand, trueStart, trueStop, THRESH, originalContig, sl.rname(), cstart);
242 boolean loose=isCorrectHitLoose(ss, trueChrom, trueStrand, trueStart, trueStop, THRESH+THRESH2, originalContig, sl.rname(), cstart);
243
244 // if(!strict){
245 // System.out.println(ss+", "+new String(originalContig)+", "+new String(sl.rname()));
246 // assert(false);
247 // }
248
249 // System.out.println("loose = "+loose+" for "+r.toText());
250
251 if(loose){
252 // System.err.println("TPL\t"+trueChrom+", "+trueStrand+", "+trueStart+", "+trueStop+"\tvs\t"
253 // +ss.chrom+", "+ss.strand+", "+ss.start+", "+ss.stop);
254 truePositiveLoose++;
255 }else{
256 // System.err.println("FPL\t"+trueChrom+", "+trueStrand+", "+trueStart+", "+trueStop+"\tvs\t"
257 // +ss.chrom+", "+ss.strand+", "+ss.start+", "+ss.stop);
258 falsePositiveLoose++;
259 }
260
261 if(strict){
262 // System.err.println("TPS\t"+trueStart+", "+trueStop+"\tvs\t"+ss.start+", "+ss.stop);
263 truePositiveStrict++;
264 }else{
265 // System.err.println("FPS\t"+trueStart+", "+trueStop+"\tvs\t"+ss.start+", "+ss.stop);
266 falsePositiveStrict++;
267 }
268 }
269 }
270 }
271 }
272 }
273
274
275
276 public static int type(final Read r, SamLine sl){
277
278 int THRESH=0;
279 primary++;
280
281 if(r.discarded()/* || r.mapScore==0*/){
282 return 0;
283 }else if(r.ambiguous()){
284 return 1;
285 }else if(r.mapScore<1){
286 return 0;
287 }else if(r.mapScore<=minQuality){
288 return 1;
289 }else{
290 if(!r.mapped()){
291 return 0;
292 }else{
293
294 if(parsecustom){
295 SiteScore os=r.originalSite;
296 assert(os!=null);
297 if(os!=null){
298 int trueChrom=os.chrom;
299 byte trueStrand=os.strand;
300 int trueStart=os.start;
301 int trueStop=os.stop;
302 SiteScore ss=new SiteScore(r.chrom, r.strand(), r.start, r.stop, 0, 0);
303 byte[] originalContig=sl.originalContig();
304 if(BLASR){
305 originalContig=(originalContig==null || Tools.indexOf(originalContig, (byte)'/')<0 ? originalContig :
306 KillSwitch.copyOfRange(originalContig, 0, Tools.lastIndexOf(originalContig, (byte)'/')));
307 }
308 int cstart=sl.originalContigStart();
309
310 boolean strict=isCorrectHit(ss, trueChrom, trueStrand, trueStart, trueStop, THRESH, originalContig, sl.rname(), cstart);
311 boolean loose=isCorrectHitLoose(ss, trueChrom, trueStrand, trueStart, trueStop, THRESH+THRESH2, originalContig, sl.rname(), cstart);
312
313 if(strict){return 2;}
314 if(loose){return 3;}
315 return 4;
316 }
317 }
318 }
319 }
320 return 0;
321 }
322
323
324
325 public static boolean isCorrectHit(SiteScore ss, int trueChrom, byte trueStrand, int trueStart, int trueStop, int thresh,
326 byte[] originalContig, byte[] contig, int cstart){
327 if(ss.strand!=trueStrand){return false;}
328 if(originalContig!=null){
329 if(!Arrays.equals(originalContig, contig)){return false;}
330 }else{
331 if(ss.chrom!=trueChrom){return false;}
332 }
333
334 assert(ss.stop>ss.start) : ss.toText()+", "+trueStart+", "+trueStop;
335 assert(trueStop>trueStart) : ss.toText()+", "+trueStart+", "+trueStop;
336 int cstop=cstart+trueStop-trueStart;
337 // return (absdif(ss.start, trueStart)<=thresh && absdif(ss.stop, trueStop)<=thresh);
338 return (absdif(ss.start, cstart)<=thresh && absdif(ss.stop, cstop)<=thresh);
339 }
340
341
342 public static boolean isCorrectHitLoose(SiteScore ss, int trueChrom, byte trueStrand, int trueStart, int trueStop, int thresh,
343 byte[] originalContig, byte[] contig, int cstart){
344 if(ss.strand!=trueStrand){return false;}
345 if(originalContig!=null){
346 if(!Arrays.equals(originalContig, contig)){return false;}
347 }else{
348 if(ss.chrom!=trueChrom){return false;}
349 }
350
351 assert(ss.stop>ss.start) : ss.toText()+", "+trueStart+", "+trueStop;
352 assert(trueStop>trueStart) : ss.toText()+", "+trueStart+", "+trueStop;
353 int cstop=cstart+trueStop-trueStart;
354 // return (absdif(ss.start, trueStart)<=thresh || absdif(ss.stop, trueStop)<=thresh);
355 return (absdif(ss.start, cstart)<=thresh || absdif(ss.stop, cstop)<=thresh);
356 }
357
358 private static final int absdif(int a, int b){
359 return a>b ? a-b : b-a;
360 }
361
362 public static int truePositiveStrict=0;
363 public static int falsePositiveStrict=0;
364
365 public static int truePositiveLoose=0;
366 public static int falsePositiveLoose=0;
367
368 public static int mapped=0;
369 public static int mappedRetained=0;
370 public static int unmapped=0;
371
372 public static int discarded=0;
373 public static int ambiguous=0;
374
375 public static long lines=0;
376 public static long primary=0;
377 public static long secondary=0;
378
379 public static int minQuality=3;
380
381 public static boolean parsecustom=true;
382 public static boolean printerr=false;
383
384 public static int THRESH2=20;
385 public static boolean BLASR=false;
386
387 }