| 1 |
package ml; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
|
| 5 |
import fileIO.FileFormat; |
| 6 |
import fileIO.ReadWrite; |
| 7 |
import shared.Parser; |
| 8 |
import shared.PreParser; |
| 9 |
import shared.Shared; |
| 10 |
import shared.Timer; |
| 11 |
import shared.Tools; |
| 12 |
import stream.ConcurrentReadInputStream; |
| 13 |
import stream.ConcurrentReadOutputStream; |
| 14 |
import stream.FASTQ; |
| 15 |
import stream.Read; |
| 16 |
import structures.ListNum; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class ProcessBBMergeHeaders { |
| 24 |
|
| 25 |
public static void main(String[] args){ |
| 26 |
Timer t=new Timer(); |
| 27 |
ProcessBBMergeHeaders x=new ProcessBBMergeHeaders(args); |
| 28 |
x.process(t); |
| 29 |
|
| 30 |
|
| 31 |
Shared.closeStream(x.outstream); |
| 32 |
} |
| 33 |
|
| 34 |
public ProcessBBMergeHeaders(String[] args){ |
| 35 |
|
| 36 |
{ |
| 37 |
PreParser pp=new PreParser(args, getClass(), false); |
| 38 |
args=pp.args; |
| 39 |
outstream=pp.outstream; |
| 40 |
} |
| 41 |
|
| 42 |
FASTQ.TEST_INTERLEAVED=false; |
| 43 |
FASTQ.FORCE_INTERLEAVED=false; |
| 44 |
|
| 45 |
Parser parser=new Parser(); |
| 46 |
for(int i=0; i<args.length; i++){ |
| 47 |
String arg=args[i]; |
| 48 |
String[] split=arg.split("="); |
| 49 |
String a=split[0].toLowerCase(); |
| 50 |
String b=split.length>1 ? split[1] : null; |
| 51 |
|
| 52 |
if(parser.parse(arg, a, b)){ |
| 53 |
|
| 54 |
}else if(a.equals("parse_flag_goes_here")){ |
| 55 |
|
| 56 |
}else{ |
| 57 |
outstream.println("Unknown parameter "+args[i]); |
| 58 |
assert(false) : "Unknown parameter "+args[i]; |
| 59 |
|
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
{ |
| 64 |
Parser.processQuality(); |
| 65 |
|
| 66 |
maxReads=parser.maxReads; |
| 67 |
in1=parser.in1; |
| 68 |
out1=parser.out1; |
| 69 |
} |
| 70 |
|
| 71 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, null, true, true, false, false); |
| 72 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true); |
| 73 |
} |
| 74 |
|
| 75 |
void process(Timer t){ |
| 76 |
|
| 77 |
final ConcurrentReadInputStream cris; |
| 78 |
{ |
| 79 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, false, ffin1, null); |
| 80 |
cris.start(); |
| 81 |
} |
| 82 |
|
| 83 |
final ConcurrentReadOutputStream ros; |
| 84 |
if(out1!=null){ |
| 85 |
final int buff=4; |
| 86 |
|
| 87 |
assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name."; |
| 88 |
|
| 89 |
ros=ConcurrentReadOutputStream.getStream(ffout1, null, buff, null, false); |
| 90 |
ros.start(); |
| 91 |
}else{ros=null;} |
| 92 |
|
| 93 |
long readsProcessed=0; |
| 94 |
{ |
| 95 |
|
| 96 |
ListNum<Read> ln=cris.nextList(); |
| 97 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 98 |
|
| 99 |
if(reads!=null && !reads.isEmpty()){ |
| 100 |
Read r=reads.get(0); |
| 101 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 102 |
} |
| 103 |
|
| 104 |
ArrayList<Read> keep=new ArrayList<Read>(reads.size()); |
| 105 |
keep.add(new Read(null, null, headerString(), 0)); |
| 106 |
|
| 107 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 108 |
if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} |
| 109 |
|
| 110 |
for(int idx=0; idx<reads.size(); idx++){ |
| 111 |
final Read r1=reads.get(idx); |
| 112 |
|
| 113 |
Header h=makeHeader(r1.id); |
| 114 |
if(h!=null){ |
| 115 |
r1.id=h.toString(); |
| 116 |
keep.add(r1); |
| 117 |
} |
| 118 |
|
| 119 |
readsProcessed++; |
| 120 |
} |
| 121 |
|
| 122 |
if(ros!=null){ros.add(keep, ln.id);} |
| 123 |
keep=new ArrayList<Read>(); |
| 124 |
|
| 125 |
cris.returnList(ln); |
| 126 |
if(verbose){outstream.println("Returned a list.");} |
| 127 |
ln=cris.nextList(); |
| 128 |
reads=(ln!=null ? ln.list : null); |
| 129 |
} |
| 130 |
if(ln!=null){ |
| 131 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 132 |
} |
| 133 |
} |
| 134 |
ReadWrite.closeStreams(cris, ros); |
| 135 |
if(verbose){outstream.println("Finished.");} |
| 136 |
|
| 137 |
t.stop(); |
| 138 |
outstream.println("Time: \t"+t); |
| 139 |
outstream.println("Reads Processed: "+readsProcessed+" \t"+Tools.format("%.2fk reads/sec", (readsProcessed/(double)(t.elapsed))*1000000)); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
private Header makeHeader(String line){ |
| 147 |
if(!line.startsWith("insert=")){return null;} |
| 148 |
if(!line.contains(" mo=")){return null;} |
| 149 |
Header h=new Header(line); |
| 150 |
return h.valid ? h : null; |
| 151 |
} |
| 152 |
|
| 153 |
public String headerString(){ |
| 154 |
return "#Correct\tminOverlap\tbestOverlap\tbestBadInt\tsecondBestOverlap\tsecondBestBadInt\t" |
| 155 |
+ "expectedErrors\tbestExpectedErrors\tbestRatio\tbestBad\tsecondBestRatio\tsecondBestBad\tprobability"; |
| 156 |
} |
| 157 |
|
| 158 |
private class Header { |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
Header(String line_){ |
| 163 |
line=line_; |
| 164 |
String[] split=line.split(" "); |
| 165 |
trueInsert=Integer.parseInt(split[0].split("=")[1]); |
| 166 |
|
| 167 |
split=split[2].split("_"); |
| 168 |
for(String s : split){ |
| 169 |
String[] split2=s.split("="); |
| 170 |
String a=split2[0], b=split2[1]; |
| 171 |
if(a.equals("mo")){ |
| 172 |
minOverlap=Integer.parseInt(b); |
| 173 |
}else if(a.equals("bi")){ |
| 174 |
bestInsert=Integer.parseInt(b); |
| 175 |
}else if(a.equals("bo")){ |
| 176 |
bestOverlap=Integer.parseInt(b); |
| 177 |
}else if(a.equals("bbi")){ |
| 178 |
bestBadInt=Integer.parseInt(b); |
| 179 |
}else if(a.equals("sbi")){ |
| 180 |
secondBestInsert=Integer.parseInt(b); |
| 181 |
}else if(a.equals("sbo")){ |
| 182 |
secondBestOverlap=Integer.parseInt(b); |
| 183 |
}else if(a.equals("sbbi")){ |
| 184 |
secondBestBadInt=Integer.parseInt(b); |
| 185 |
}else if(a.equals("r1ee")){ |
| 186 |
expectedErrors1=Float.parseFloat(b); |
| 187 |
}else if(a.equals("r2ee")){ |
| 188 |
expectedErrors2=Float.parseFloat(b); |
| 189 |
}else if(a.equals("be")){ |
| 190 |
bestExpected=Float.parseFloat(b); |
| 191 |
}else if(a.equals("pr")){ |
| 192 |
probability=Float.parseFloat(b); |
| 193 |
}else if(a.equals("br")){ |
| 194 |
bestRatio=Float.parseFloat(b); |
| 195 |
}else if(a.equals("bb")){ |
| 196 |
bestBad=Float.parseFloat(b); |
| 197 |
}else if(a.equals("sbr")){ |
| 198 |
secondBestRatio=Float.parseFloat(b); |
| 199 |
}else if(a.equals("sbb")){ |
| 200 |
secondBestBad=Float.parseFloat(b); |
| 201 |
}else{ |
| 202 |
throw new RuntimeException(s); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
correct=(bestInsert==trueInsert); |
| 207 |
valid=(split.length==15 && bestInsert>0 && secondBestInsert>0); |
| 208 |
assert(!valid || bestOverlap>0) : bestOverlap+", "+bestInsert; |
| 209 |
} |
| 210 |
|
| 211 |
@Override |
| 212 |
public String toString(){ |
| 213 |
return Tools.format("%d\t%d\t%d\t%d\t%d\t%d\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.8f", |
| 214 |
correct ? 1 : 0, minOverlap, bestOverlap, bestBadInt, secondBestOverlap, secondBestBadInt, |
| 215 |
expectedErrors1+expectedErrors2, bestExpected, bestRatio, bestBad, secondBestRatio, secondBestBad, probability); |
| 216 |
} |
| 217 |
|
| 218 |
int trueInsert; |
| 219 |
|
| 220 |
int minOverlap; |
| 221 |
float expectedErrors1; |
| 222 |
float expectedErrors2; |
| 223 |
|
| 224 |
float bestExpected; |
| 225 |
float probability; |
| 226 |
|
| 227 |
int bestInsert; |
| 228 |
int bestOverlap; |
| 229 |
float bestRatio; |
| 230 |
float bestBad; |
| 231 |
int bestBadInt; |
| 232 |
|
| 233 |
int secondBestInsert; |
| 234 |
int secondBestOverlap; |
| 235 |
float secondBestRatio; |
| 236 |
float secondBestBad; |
| 237 |
int secondBestBadInt; |
| 238 |
|
| 239 |
boolean correct; |
| 240 |
boolean valid=false; |
| 241 |
|
| 242 |
String line; |
| 243 |
|
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
private String in1=null; |
| 249 |
private String out1=null; |
| 250 |
|
| 251 |
private final FileFormat ffin1; |
| 252 |
private final FileFormat ffout1; |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
private long maxReads=-1; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
private java.io.PrintStream outstream=System.err; |
| 261 |
public static boolean verbose=false; |
| 262 |
|
| 263 |
} |