| 1 |
package jgi; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.LinkedHashMap; |
| 5 |
|
| 6 |
import fileIO.FileFormat; |
| 7 |
import fileIO.ReadWrite; |
| 8 |
import fileIO.TextFile; |
| 9 |
import shared.KillSwitch; |
| 10 |
import shared.Parse; |
| 11 |
import shared.Parser; |
| 12 |
import shared.PreParser; |
| 13 |
import shared.Shared; |
| 14 |
import shared.Timer; |
| 15 |
import shared.Tools; |
| 16 |
import stream.ConcurrentReadInputStream; |
| 17 |
import stream.ConcurrentReadOutputStream; |
| 18 |
import stream.Read; |
| 19 |
import stream.SamLine; |
| 20 |
import structures.ListNum; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class CutPrimers { |
| 29 |
|
| 30 |
public static void main(String[] args){ |
| 31 |
Timer t=new Timer(); |
| 32 |
CutPrimers x=new CutPrimers(args); |
| 33 |
x.process(t); |
| 34 |
|
| 35 |
|
| 36 |
Shared.closeStream(x.outstream); |
| 37 |
} |
| 38 |
|
| 39 |
public CutPrimers(String[] args){ |
| 40 |
|
| 41 |
{ |
| 42 |
PreParser pp=new PreParser(args, getClass(), false); |
| 43 |
args=pp.args; |
| 44 |
outstream=pp.outstream; |
| 45 |
} |
| 46 |
|
| 47 |
Parser parser=new Parser(); |
| 48 |
for(int i=0; i<args.length; i++){ |
| 49 |
String arg=args[i]; |
| 50 |
String[] split=arg.split("="); |
| 51 |
String a=split[0].toLowerCase(); |
| 52 |
String b=split.length>1 ? split[1] : null; |
| 53 |
|
| 54 |
if(parser.parse(arg, a, b)){ |
| 55 |
|
| 56 |
}else if(a.equals("parse_flag_goes_here")){ |
| 57 |
|
| 58 |
}else if(a.equals("sam1")){ |
| 59 |
sam1=b; |
| 60 |
}else if(a.equals("sam2")){ |
| 61 |
sam2=b; |
| 62 |
}else if(a.equals("fake") || a.equals("addfake")){ |
| 63 |
ADD_FAKE_READS=Parse.parseBoolean(b); |
| 64 |
}else if(a.equals("include") || a.equals("includeprimer") || a.equals("includeprimers")){ |
| 65 |
INCLUDE_PRIMERS=Parse.parseBoolean(b); |
| 66 |
}else{ |
| 67 |
outstream.println("Unknown parameter "+args[i]); |
| 68 |
assert(false) : "Unknown parameter "+args[i]; |
| 69 |
|
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
{ |
| 74 |
Parser.processQuality(); |
| 75 |
|
| 76 |
maxReads=parser.maxReads; |
| 77 |
in1=parser.in1; |
| 78 |
out1=parser.out1; |
| 79 |
} |
| 80 |
|
| 81 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, null, true, true, false, false); |
| 82 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true); |
| 83 |
} |
| 84 |
|
| 85 |
void process(Timer t){ |
| 86 |
|
| 87 |
final ConcurrentReadInputStream cris; |
| 88 |
{ |
| 89 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null); |
| 90 |
if(verbose){outstream.println("Started cris");} |
| 91 |
cris.start(); |
| 92 |
} |
| 93 |
boolean paired=cris.paired(); |
| 94 |
|
| 95 |
final ConcurrentReadOutputStream ros; |
| 96 |
if(out1!=null){ |
| 97 |
final int buff=4; |
| 98 |
|
| 99 |
if(cris.paired() && (in1==null || !in1.contains(".sam"))){ |
| 100 |
outstream.println("Writing interleaved."); |
| 101 |
} |
| 102 |
|
| 103 |
assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name."; |
| 104 |
|
| 105 |
ros=ConcurrentReadOutputStream.getStream(ffout1, null, buff, null, false); |
| 106 |
ros.start(); |
| 107 |
}else{ros=null;} |
| 108 |
|
| 109 |
LinkedHashMap<String, SamLine> p1set=toSamLines(sam1); |
| 110 |
LinkedHashMap<String, SamLine> p2set=toSamLines(sam2); |
| 111 |
long readsProcessed=0, readsSuccess=0; |
| 112 |
{ |
| 113 |
|
| 114 |
ListNum<Read> ln=cris.nextList(); |
| 115 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 116 |
|
| 117 |
if(reads!=null && !reads.isEmpty()){ |
| 118 |
Read r=reads.get(0); |
| 119 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 120 |
} |
| 121 |
|
| 122 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 123 |
ArrayList<Read> readsOut=new ArrayList<Read>(reads.size()); |
| 124 |
if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} |
| 125 |
|
| 126 |
for(int idx=0; idx<reads.size(); idx++){ |
| 127 |
readsProcessed++; |
| 128 |
final Read r=reads.get(idx); |
| 129 |
|
| 130 |
final SamLine sl1=p1set.get(r.id); |
| 131 |
final SamLine sl2=p2set.get(r.id); |
| 132 |
|
| 133 |
int oldSize=readsOut.size(); |
| 134 |
|
| 135 |
final int len=r.length(); |
| 136 |
if(sl1!=null && sl2!=null){ |
| 137 |
final int a1=Tools.mid(0, len, sl1.start(true, false)); |
| 138 |
final int a2=Tools.mid(0, len, sl2.start(true, false)); |
| 139 |
final int b1=Tools.mid(0, len, sl1.stop(a1, true, false)); |
| 140 |
final int b2=Tools.mid(0, len, sl2.stop(a2, true, false)); |
| 141 |
if(Tools.overlap(a1, b1, a2, b2)){ |
| 142 |
|
| 143 |
}else{ |
| 144 |
|
| 145 |
final int from, to; |
| 146 |
if(INCLUDE_PRIMERS){ |
| 147 |
if(a1<a2){ |
| 148 |
from=a1; |
| 149 |
to=b2+1; |
| 150 |
}else{ |
| 151 |
from=a2; |
| 152 |
to=b1+1; |
| 153 |
} |
| 154 |
}else{ |
| 155 |
if(a1<a2){ |
| 156 |
from=b1+1; |
| 157 |
to=a2; |
| 158 |
}else{ |
| 159 |
from=b2+1; |
| 160 |
to=a1; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
assert(from>=0 && from<r.bases.length && to>=from) : from+", "+to+", "+r.bases.length+"\n"+ |
| 165 |
new String(r.bases)+"\n"+sl1+"\n"+sl2+"\n"; |
| 166 |
final byte[] bases=KillSwitch.copyOfRange(r.bases, from, to); |
| 167 |
final byte[] quals=(r.quality==null ? null : KillSwitch.copyOfRange(r.quality, from, to)); |
| 168 |
readsOut.add(new Read(bases, quals, r.id, r.numericID)); |
| 169 |
readsSuccess++; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
if(oldSize==readsOut.size() && ADD_FAKE_READS){ |
| 174 |
readsOut.add(new Read(new byte[] {'N'}, null, r.id, r.numericID)); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
if(ros!=null){ros.add(readsOut, ln.id);} |
| 179 |
|
| 180 |
cris.returnList(ln); |
| 181 |
if(verbose){outstream.println("Returned a list.");} |
| 182 |
ln=cris.nextList(); |
| 183 |
reads=(ln!=null ? ln.list : null); |
| 184 |
} |
| 185 |
if(ln!=null){ |
| 186 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 187 |
} |
| 188 |
} |
| 189 |
ReadWrite.closeStreams(cris, ros); |
| 190 |
if(verbose){outstream.println("Finished.");} |
| 191 |
|
| 192 |
t.stop(); |
| 193 |
outstream.println("Time: \t"+t); |
| 194 |
outstream.println("Reads Processed: "+readsProcessed+" \t"+Tools.format("%.2fk reads/sec", (readsProcessed/(double)(t.elapsed))*1000000)); |
| 195 |
outstream.println("Sequences Generated: "+readsSuccess); |
| 196 |
} |
| 197 |
|
| 198 |
public static LinkedHashMap<String, SamLine> toSamLines(String fname){ |
| 199 |
TextFile tf=new TextFile(fname); |
| 200 |
LinkedHashMap<String, SamLine> list=new LinkedHashMap<String, SamLine>(); |
| 201 |
for(String s=tf.nextLine(); s!=null; s=tf.nextLine()){ |
| 202 |
if(!s.startsWith("@")){ |
| 203 |
SamLine sl=new SamLine(s); |
| 204 |
list.put(new String(sl.rname()), sl); |
| 205 |
} |
| 206 |
} |
| 207 |
tf.close(); |
| 208 |
return list; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
private String in1=null; |
| 216 |
private String sam1=null; |
| 217 |
private String sam2=null; |
| 218 |
private String out1=null; |
| 219 |
|
| 220 |
private boolean ADD_FAKE_READS=true; |
| 221 |
private boolean INCLUDE_PRIMERS=false; |
| 222 |
|
| 223 |
private final FileFormat ffin1; |
| 224 |
private final FileFormat ffout1; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
private long maxReads=-1; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
private java.io.PrintStream outstream=System.err; |
| 233 |
public static boolean verbose=false; |
| 234 |
|
| 235 |
} |