| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
|
| 5 |
import fileIO.FileFormat; |
| 6 |
import fileIO.TextFile; |
| 7 |
import fileIO.TextStreamWriter; |
| 8 |
import shared.PreParser; |
| 9 |
import shared.Shared; |
| 10 |
import shared.Timer; |
| 11 |
import shared.Tools; |
| 12 |
import stream.SamLine; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public class SplitSam4Way { |
| 20 |
|
| 21 |
public static void main(String[] args){ |
| 22 |
SplitSam4Way x=new SplitSam4Way(args); |
| 23 |
|
| 24 |
|
| 25 |
Shared.closeStream(x.outstream); |
| 26 |
} |
| 27 |
|
| 28 |
private void printOptions(){ |
| 29 |
outstream.println("Syntax:\n"); |
| 30 |
outstream.println("java -ea -Xmx128m -cp <path> jgi.SplitSam4Way <input> <out plus> <out minus> <out chimeric> <out unmapped>"); |
| 31 |
outstream.println("If you do not want one of the output files, use the word 'null'.\n"); |
| 32 |
} |
| 33 |
|
| 34 |
public SplitSam4Way(String[] args){ |
| 35 |
|
| 36 |
{ |
| 37 |
PreParser pp=new PreParser(args, getClass(), false); |
| 38 |
args=pp.args; |
| 39 |
outstream=pp.outstream; |
| 40 |
} |
| 41 |
|
| 42 |
if(args==null || args.length!=5){ |
| 43 |
printOptions(); |
| 44 |
System.exit(1); |
| 45 |
} |
| 46 |
|
| 47 |
Timer t=new Timer(); |
| 48 |
long reads=0, bases=0; |
| 49 |
long preads=0, mreads=0, creads=0, ureads=0; |
| 50 |
|
| 51 |
String fin=args[0]; |
| 52 |
String fplus=args[1]; |
| 53 |
String fminus=args[2]; |
| 54 |
String fchimeric=args[3]; |
| 55 |
String funmapped=args[4]; |
| 56 |
|
| 57 |
TextFile tf=new TextFile(fin, true); |
| 58 |
TextStreamWriter plus=("null".equalsIgnoreCase(fplus) ? null : new TextStreamWriter(fplus, true, false, true, FileFormat.SAM)); |
| 59 |
TextStreamWriter minus=("null".equalsIgnoreCase(fminus) ? null : new TextStreamWriter(fminus, true, false, true, FileFormat.SAM)); |
| 60 |
TextStreamWriter chimeric=("null".equalsIgnoreCase(fchimeric) ? null : new TextStreamWriter(fchimeric, true, false, true, FileFormat.SAM)); |
| 61 |
TextStreamWriter unmapped=("null".equalsIgnoreCase(funmapped) ? null : new TextStreamWriter(funmapped, true, false, true, FileFormat.SAM)); |
| 62 |
|
| 63 |
if(plus!=null){plus.start();} |
| 64 |
if(minus!=null){minus.start();} |
| 65 |
if(chimeric!=null){chimeric.start();} |
| 66 |
if(unmapped!=null){unmapped.start();} |
| 67 |
|
| 68 |
for(String line=tf.nextLine(); line!=null; line=tf.nextLine()){ |
| 69 |
if(line.charAt(0)=='@'){ |
| 70 |
if(plus!=null){plus.println(line);} |
| 71 |
if(minus!=null){minus.println(line);} |
| 72 |
if(chimeric!=null){chimeric.println(line);} |
| 73 |
if(unmapped!=null){unmapped.println(line);} |
| 74 |
}else{ |
| 75 |
SamLine sl=new SamLine(line); |
| 76 |
reads++; |
| 77 |
|
| 78 |
bases+=sl.seq.length; |
| 79 |
|
| 80 |
if(!sl.mapped() || !sl.nextMapped() || !sl.hasMate() || !sl.primary()){ |
| 81 |
if(unmapped!=null){unmapped.println(line);} |
| 82 |
ureads++; |
| 83 |
|
| 84 |
}else if(!sl.pairedOnSameChrom() || sl.strand()==sl.nextStrand()){ |
| 85 |
if(chimeric!=null){chimeric.println(line);} |
| 86 |
creads++; |
| 87 |
|
| 88 |
}else if((sl.firstFragment() ? sl.strand() : sl.nextStrand())==Shared.PLUS){ |
| 89 |
if(plus!=null){plus.println(line);} |
| 90 |
preads++; |
| 91 |
}else if((sl.firstFragment() ? sl.strand() : sl.nextStrand())==Shared.MINUS){ |
| 92 |
if(minus!=null){minus.println(line);} |
| 93 |
mreads++; |
| 94 |
}else{ |
| 95 |
throw new RuntimeException("Unhandled case: "+sl.firstFragment()+", "+sl.lastFragment()+", "+sl.strand()+", "+sl.nextStrand()+"\n"+sl+"\n"); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if(plus!=null){plus.poisonAndWait();} |
| 101 |
if(minus!=null){minus.poisonAndWait();} |
| 102 |
if(chimeric!=null){chimeric.poisonAndWait();} |
| 103 |
if(unmapped!=null){unmapped.poisonAndWait();} |
| 104 |
|
| 105 |
t.stop(); |
| 106 |
outstream.println(Tools.timeReadsBasesProcessed(t, reads, bases, 8)); |
| 107 |
outstream.println("Plus Reads: "+preads); |
| 108 |
outstream.println("Minus Reads: "+mreads); |
| 109 |
outstream.println("Chimeric Reads: "+creads); |
| 110 |
outstream.println("Unmapped Reads: "+ureads); |
| 111 |
} |
| 112 |
|
| 113 |
private PrintStream outstream=System.err; |
| 114 |
|
| 115 |
} |