| 1 |
package driver; |
| 2 |
|
| 3 |
import fileIO.ReadWrite; |
| 4 |
import fileIO.TextFile; |
| 5 |
import fileIO.TextStreamWriter; |
| 6 |
import shared.Parse; |
| 7 |
import shared.Shared; |
| 8 |
import shared.Tools; |
| 9 |
import stream.SamLine; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public final class SelectReads { |
| 20 |
|
| 21 |
public static void main(String[] args){ |
| 22 |
|
| 23 |
assert(args.length>=2) : "Need 2 file names: <input> <output>"; |
| 24 |
assert(!args[0].equalsIgnoreCase(args[1])) : "File names must be different."; |
| 25 |
|
| 26 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 27 |
ReadWrite.setZipThreads(Shared.threads()); |
| 28 |
|
| 29 |
|
| 30 |
int minlen=1; |
| 31 |
long reads=Long.MAX_VALUE; |
| 32 |
char symbol='D'; |
| 33 |
if(args.length>2){symbol=(char)args[2].charAt(0);} |
| 34 |
if(args.length>3){minlen=Integer.parseInt(args[3]);} |
| 35 |
if(args.length>4){reads=Parse.parseKMG(args[4]);} |
| 36 |
|
| 37 |
symbol=Tools.toUpperCase(symbol); |
| 38 |
if(symbol=='='){symbol='M';} |
| 39 |
if(symbol=='X'){symbol='S';} |
| 40 |
if(symbol=='N'){symbol='D';} |
| 41 |
if(symbol=='S' || symbol=='H' || symbol=='P'){symbol='C';} |
| 42 |
|
| 43 |
final int index=Tools.indexOf(new char[] {'M','S','D','I','C'}, symbol); |
| 44 |
assert(index>=0) : "Symbol (3rd argument) must be M, S, D, I, C (for match string symbols) or M, =, X, D, N, I, S, H, P (for cigar symbols)."; |
| 45 |
|
| 46 |
TextFile tf=new TextFile(args[0], true); |
| 47 |
TextStreamWriter tsw=new TextStreamWriter(args[1], false, false, true); |
| 48 |
tsw.start(); |
| 49 |
|
| 50 |
for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){ |
| 51 |
if(line.charAt(0)=='@'){ |
| 52 |
tsw.println(line); |
| 53 |
}else{ |
| 54 |
if((reads=reads-1)<0){break;} |
| 55 |
SamLine sl=new SamLine(line); |
| 56 |
if(testLine(sl, minlen, index)){ |
| 57 |
tsw.println(line); |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
tf.close(); |
| 62 |
tsw.poisonAndWait(); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
private static boolean testLine(SamLine sl, int minlen, int index){ |
| 68 |
assert(sl!=null); |
| 69 |
if(!sl.mapped() || sl.cigar==null){return false;} |
| 70 |
int[] msdic=sl.cigarToMdsiMax(sl.cigar); |
| 71 |
return (msdic!=null && msdic[index]>=minlen); |
| 72 |
} |
| 73 |
|
| 74 |
} |