| 1 |
package driver; |
| 2 |
|
| 3 |
import java.util.HashSet; |
| 4 |
|
| 5 |
import fileIO.TextFile; |
| 6 |
import shared.Tools; |
| 7 |
import stream.SamLine; |
| 8 |
|
| 9 |
public class TrimSamFile { |
| 10 |
|
| 11 |
public static void main(String[] args){ |
| 12 |
String fname=args[0]; |
| 13 |
String scaf=args[1]; |
| 14 |
int from=Integer.parseInt(args[2]); |
| 15 |
int to=Integer.parseInt(args[3]); |
| 16 |
TextFile tf=new TextFile(fname, false); |
| 17 |
HashSet<String> set=findBadLines(tf, scaf, from, to); |
| 18 |
tf.reset(); |
| 19 |
printExcludingSet(tf, set); |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
public static HashSet<String> findBadLines(TextFile tf, String scafS, int from, int to){ |
| 24 |
byte[] scaf=scafS.getBytes(); |
| 25 |
HashSet<String> set=new HashSet<String>(16000); |
| 26 |
|
| 27 |
for(String s=tf.nextLine(); s!=null; s=tf.nextLine()){ |
| 28 |
if(s.charAt(0)!='@'){ |
| 29 |
SamLine sl=new SamLine(s); |
| 30 |
|
| 31 |
if(sl.pos>=from && sl.pos<=to && Tools.equals(sl.rname(), scaf)){ |
| 32 |
set.add(sl.qname); |
| 33 |
}else if(sl.pnext>=from && sl.pnext<=to && Tools.equals(sl.rnext(), scaf)){ |
| 34 |
set.add(sl.qname); |
| 35 |
}else if(Tools.equals(sl.rname(), scaf) && Tools.equals(sl.rnext(), scaf) && (sl.pos<from != sl.pnext<from)){ |
| 36 |
set.add(sl.qname); |
| 37 |
}else if(!sl.mapped() || !sl.nextMapped() || !sl.pairedOnSameChrom()){ |
| 38 |
set.add(sl.qname); |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
return set; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
public static void printExcludingSet(TextFile tf, HashSet<String> set){ |
| 47 |
|
| 48 |
for(String s=tf.nextLine(); s!=null; s=tf.nextLine()){ |
| 49 |
if(s.charAt(0)=='@'){ |
| 50 |
System.out.println(s); |
| 51 |
}else{ |
| 52 |
SamLine sl=new SamLine(s); |
| 53 |
|
| 54 |
if(!set.contains(sl.qname)){ |
| 55 |
System.out.println(s); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
} |
| 63 |
|