| 1 |
package barcode; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Collections; |
| 7 |
import java.util.HashMap; |
| 8 |
|
| 9 |
import align2.BandedAlignerConcrete; |
| 10 |
import dna.AminoAcid; |
| 11 |
import fileIO.ByteFile; |
| 12 |
import fileIO.ByteFile1; |
| 13 |
import fileIO.ByteFile2; |
| 14 |
import fileIO.FileFormat; |
| 15 |
import fileIO.ReadWrite; |
| 16 |
import fileIO.TextStreamWriter; |
| 17 |
import shared.Parse; |
| 18 |
import shared.Parser; |
| 19 |
import shared.PreParser; |
| 20 |
import shared.Shared; |
| 21 |
import shared.Timer; |
| 22 |
import shared.Tools; |
| 23 |
import stream.ConcurrentGenericReadInputStream; |
| 24 |
import stream.ConcurrentReadInputStream; |
| 25 |
import stream.ConcurrentReadOutputStream; |
| 26 |
import stream.FASTQ; |
| 27 |
import stream.FastaReadInputStream; |
| 28 |
import stream.Read; |
| 29 |
import structures.ListNum; |
| 30 |
import structures.StringNum; |
| 31 |
import tracker.ReadStats; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class CountBarcodes { |
| 39 |
|
| 40 |
public static void main(String[] args){ |
| 41 |
Timer t=new Timer(); |
| 42 |
CountBarcodes x=new CountBarcodes(args); |
| 43 |
x.process(t); |
| 44 |
|
| 45 |
|
| 46 |
Shared.closeStream(x.outstream); |
| 47 |
} |
| 48 |
|
| 49 |
public CountBarcodes(String[] args){ |
| 50 |
|
| 51 |
{ |
| 52 |
PreParser pp=new PreParser(args, getClass(), false); |
| 53 |
args=pp.args; |
| 54 |
outstream=pp.outstream; |
| 55 |
} |
| 56 |
|
| 57 |
boolean setInterleaved=false; |
| 58 |
|
| 59 |
Shared.capBuffers(4); |
| 60 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 61 |
ReadWrite.setZipThreads(Shared.threads()); |
| 62 |
|
| 63 |
|
| 64 |
expectedCodeMap=new HashMap<String,String>(200); |
| 65 |
validCodeMap=new HashMap<String,String>(200); |
| 66 |
|
| 67 |
Parser parser=new Parser(); |
| 68 |
for(int i=0; i<args.length; i++){ |
| 69 |
String arg=args[i]; |
| 70 |
String[] split=arg.split("="); |
| 71 |
String a=split[0].toLowerCase(); |
| 72 |
String b=split.length>1 ? split[1] : null; |
| 73 |
|
| 74 |
if(parser.parse(arg, a, b)){ |
| 75 |
|
| 76 |
}else if(a.equals("verbose")){ |
| 77 |
verbose=Parse.parseBoolean(b); |
| 78 |
ByteFile1.verbose=verbose; |
| 79 |
ByteFile2.verbose=verbose; |
| 80 |
stream.FastaReadInputStream.verbose=verbose; |
| 81 |
ConcurrentGenericReadInputStream.verbose=verbose; |
| 82 |
stream.FastqReadInputStream.verbose=verbose; |
| 83 |
ReadWrite.verbose=verbose; |
| 84 |
}else if(a.equals("countundefined")){ |
| 85 |
countUndefined=Parse.parseBoolean(b); |
| 86 |
}else if(a.equals("printheader") || a.equals("header")){ |
| 87 |
printHeader=Parse.parseBoolean(b); |
| 88 |
}else if(a.equals("printrows") || a.equals("rows") || a.equals("maxrows")){ |
| 89 |
maxRows=Integer.parseInt(b); |
| 90 |
}else if(a.equals("expected")){ |
| 91 |
if(b!=null){ |
| 92 |
for(String code : b.split(",")){ |
| 93 |
expectedCodeMap.put(code, code); |
| 94 |
validCodeMap.put(code, code); |
| 95 |
} |
| 96 |
} |
| 97 |
}else if(a.equals("valid")){ |
| 98 |
if(b!=null){ |
| 99 |
for(String code : b.split(",")){ |
| 100 |
validCodeMap.put(code, code); |
| 101 |
} |
| 102 |
} |
| 103 |
}else if(a.equals("counts") || a.equals("outc")){ |
| 104 |
outCounts=b; |
| 105 |
}else if(parser.in1==null && i==0 && Tools.looksLikeInputStream(arg)){ |
| 106 |
parser.in1=arg; |
| 107 |
}else if(parser.out1==null && i==1 && !arg.contains("=")){ |
| 108 |
parser.out1=arg; |
| 109 |
}else{ |
| 110 |
outstream.println("Unknown parameter "+args[i]); |
| 111 |
assert(false) : "Unknown parameter "+args[i]; |
| 112 |
|
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
{ |
| 117 |
Parser.processQuality(); |
| 118 |
|
| 119 |
maxReads=parser.maxReads; |
| 120 |
|
| 121 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 122 |
append=ReadStats.append=parser.append; |
| 123 |
|
| 124 |
setInterleaved=parser.setInterleaved; |
| 125 |
|
| 126 |
in1=parser.in1; |
| 127 |
in2=parser.in2; |
| 128 |
qfin1=parser.qfin1; |
| 129 |
qfin2=parser.qfin2; |
| 130 |
|
| 131 |
out1=parser.out1; |
| 132 |
out2=parser.out2; |
| 133 |
qfout1=parser.qfout1; |
| 134 |
qfout2=parser.qfout2; |
| 135 |
|
| 136 |
extin=parser.extin; |
| 137 |
extout=parser.extout; |
| 138 |
} |
| 139 |
|
| 140 |
if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){ |
| 141 |
in2=in1.replace("#", "2"); |
| 142 |
in1=in1.replace("#", "1"); |
| 143 |
} |
| 144 |
if(out1!=null && out2==null && out1.indexOf('#')>-1){ |
| 145 |
out2=out1.replace("#", "2"); |
| 146 |
out1=out1.replace("#", "1"); |
| 147 |
} |
| 148 |
if(in2!=null){ |
| 149 |
if(FASTQ.FORCE_INTERLEAVED){outstream.println("Reset INTERLEAVED to false because paired input files were specified.");} |
| 150 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 151 |
} |
| 152 |
|
| 153 |
assert(FastaReadInputStream.settingsOK()); |
| 154 |
|
| 155 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 156 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 157 |
ByteFile.FORCE_MODE_BF2=true; |
| 158 |
} |
| 159 |
|
| 160 |
if(out1==null && out2!=null){throw new RuntimeException("Error - cannot define out2 without defining out1.");} |
| 161 |
|
| 162 |
if(!setInterleaved){ |
| 163 |
assert(in1!=null && (out1!=null || out2==null)) : "\nin1="+in1+"\nin2="+in2+"\nout1="+out1+"\nout2="+out2+"\n"; |
| 164 |
if(in2!=null){ |
| 165 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 166 |
outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED); |
| 167 |
}else{ |
| 168 |
if(out2!=null){ |
| 169 |
FASTQ.FORCE_INTERLEAVED=true; |
| 170 |
FASTQ.TEST_INTERLEAVED=false; |
| 171 |
outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;} |
| 177 |
if(out2!=null && out2.equalsIgnoreCase("null")){out2=null;} |
| 178 |
|
| 179 |
if(!Tools.testOutputFiles(overwrite, append, false, out1, out2)){ |
| 180 |
outstream.println((out1==null)+", "+(out2==null)+", "+out1+", "+out2); |
| 181 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+out2+"\n"); |
| 182 |
} |
| 183 |
|
| 184 |
validCodes=new ArrayList<String>(validCodeMap.size()); |
| 185 |
validCodes.addAll(validCodeMap.values()); |
| 186 |
expectedCodes=new ArrayList<String>(expectedCodeMap.size()); |
| 187 |
expectedCodes.addAll(expectedCodeMap.values()); |
| 188 |
|
| 189 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 190 |
ffout2=FileFormat.testOutput(out2, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 191 |
ffCounts=FileFormat.testOutput(outCounts, FileFormat.TEXT, null, false, overwrite, append, false); |
| 192 |
|
| 193 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true); |
| 194 |
ffin2=FileFormat.testInput(in2, FileFormat.FASTQ, extin, true, true); |
| 195 |
} |
| 196 |
|
| 197 |
void process(Timer t){ |
| 198 |
|
| 199 |
|
| 200 |
final ConcurrentReadInputStream cris; |
| 201 |
{ |
| 202 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, ffin2, qfin1, qfin2); |
| 203 |
if(verbose){outstream.println("Started cris");} |
| 204 |
cris.start(); |
| 205 |
} |
| 206 |
boolean paired=cris.paired(); |
| 207 |
|
| 208 |
if(!ffin1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));} |
| 209 |
|
| 210 |
|
| 211 |
final ConcurrentReadOutputStream ros; |
| 212 |
if(out1!=null){ |
| 213 |
final int buff=4; |
| 214 |
|
| 215 |
if(cris.paired() && out2==null && (in1==null || !in1.contains(".sam"))){ |
| 216 |
outstream.println("Writing interleaved."); |
| 217 |
} |
| 218 |
|
| 219 |
assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name."; |
| 220 |
assert(out2==null || (!out2.equalsIgnoreCase(in1) && !out2.equalsIgnoreCase(in2))) : "out1 and out2 have same name."; |
| 221 |
|
| 222 |
ros=ConcurrentReadOutputStream.getStream(ffout1, ffout2, qfout1, qfout2, buff, null, false); |
| 223 |
ros.start(); |
| 224 |
}else{ros=null;} |
| 225 |
|
| 226 |
long readsProcessed=0; |
| 227 |
long basesProcessed=0; |
| 228 |
|
| 229 |
HashMap<String, StringNum> map=new HashMap<String, StringNum>(); |
| 230 |
|
| 231 |
{ |
| 232 |
|
| 233 |
ListNum<Read> ln=cris.nextList(); |
| 234 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
if(reads!=null && !reads.isEmpty()){ |
| 239 |
Read r=reads.get(0); |
| 240 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 241 |
} |
| 242 |
|
| 243 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 244 |
|
| 245 |
for(int idx=0; idx<reads.size(); idx++){ |
| 246 |
final Read r1=reads.get(idx); |
| 247 |
final Read r2=r1.mate; |
| 248 |
|
| 249 |
final int initialLength1=r1.length(); |
| 250 |
final int initialLength2=(r1.mateLength()); |
| 251 |
|
| 252 |
{ |
| 253 |
readsProcessed++; |
| 254 |
basesProcessed+=initialLength1; |
| 255 |
} |
| 256 |
if(r2!=null){ |
| 257 |
readsProcessed++; |
| 258 |
basesProcessed+=initialLength2; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
final String barcode=r1.barcode(true); |
| 265 |
|
| 266 |
if(countUndefined || AminoAcid.isFullyDefined(barcode)){ |
| 267 |
StringNum value=map.get(barcode); |
| 268 |
if(value==null){ |
| 269 |
value=new StringNum(barcode, 0); |
| 270 |
map.put(barcode, value); |
| 271 |
} |
| 272 |
value.increment(); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
final ArrayList<Read> listOut=reads; |
| 277 |
|
| 278 |
if(ros!=null){ros.add(listOut, ln.id);} |
| 279 |
|
| 280 |
cris.returnList(ln); |
| 281 |
ln=cris.nextList(); |
| 282 |
reads=(ln!=null ? ln.list : null); |
| 283 |
} |
| 284 |
if(ln!=null){ |
| 285 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
ArrayList<StringNum> list=new ArrayList<StringNum>(map.size()); |
| 290 |
list.addAll(map.values()); |
| 291 |
Shared.sort(list); |
| 292 |
Collections.reverse(list); |
| 293 |
|
| 294 |
TextStreamWriter tsw=new TextStreamWriter(ffCounts); |
| 295 |
tsw.start(); |
| 296 |
if(printHeader){ |
| 297 |
tsw.print("#code\tcount\tHamming_dist\tedit_dist\tvalid\n"); |
| 298 |
} |
| 299 |
for(StringNum sn : list){ |
| 300 |
if(maxRows==0){break;} |
| 301 |
maxRows--; |
| 302 |
int hdist=calcHdist(sn.s, expectedCodes); |
| 303 |
int edist=hdist; |
| 304 |
if(hdist>1){ |
| 305 |
try { |
| 306 |
edist=calcEdist(sn.s, expectedCodes); |
| 307 |
} catch (Exception e) { |
| 308 |
edist=hdist; |
| 309 |
} |
| 310 |
} |
| 311 |
boolean valid=validCodes.contains(sn.s); |
| 312 |
tsw.print(sn+"\t"+hdist+"\t"+edist+"\t"+(valid ? "valid" : "")+"\n"); |
| 313 |
} |
| 314 |
tsw.poisonAndWait(); |
| 315 |
errorState|=tsw.errorState; |
| 316 |
|
| 317 |
errorState|=ReadStats.writeAll(); |
| 318 |
|
| 319 |
errorState|=ReadWrite.closeStreams(cris, ros); |
| 320 |
|
| 321 |
t.stop(); |
| 322 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 323 |
|
| 324 |
if(errorState){ |
| 325 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public static int calcHdist(String s, ArrayList<String> expectedCodes) { |
| 335 |
int min=s.length(); |
| 336 |
for(String code : expectedCodes){ |
| 337 |
min=Tools.min(min, hdist(s, code)); |
| 338 |
if(min<1){break;} |
| 339 |
} |
| 340 |
return min; |
| 341 |
} |
| 342 |
|
| 343 |
static int hdist(String s, String code) { |
| 344 |
final int min=Tools.min(s.length(), code.length()); |
| 345 |
int subs=0; |
| 346 |
for(int i=0; i<min; i++){ |
| 347 |
if(s.charAt(i)!=code.charAt(i)){subs++;} |
| 348 |
} |
| 349 |
return subs; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
int calcEdist(String s, ArrayList<String> expectedCodes) { |
| 358 |
int min=s.length(); |
| 359 |
for(String code : expectedCodes){ |
| 360 |
min=Tools.min(min, edist(s, code)); |
| 361 |
if(min<1){break;} |
| 362 |
} |
| 363 |
return min; |
| 364 |
} |
| 365 |
|
| 366 |
int edist(String s, String code) { |
| 367 |
int x=bandy.alignForward(s.getBytes(), code.getBytes(), 0, 0, s.length(), true); |
| 368 |
return x; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
private String in1=null; |
| 374 |
private String in2=null; |
| 375 |
|
| 376 |
private String qfin1=null; |
| 377 |
private String qfin2=null; |
| 378 |
|
| 379 |
private String out1=null; |
| 380 |
private String out2=null; |
| 381 |
private String outCounts=null; |
| 382 |
|
| 383 |
private String qfout1=null; |
| 384 |
private String qfout2=null; |
| 385 |
|
| 386 |
private String extin=null; |
| 387 |
private String extout=null; |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
private boolean reverseComplementMate=false; |
| 392 |
private boolean reverseComplement=false; |
| 393 |
private boolean countUndefined=true; |
| 394 |
private boolean printHeader=true; |
| 395 |
private int maxRows=-1; |
| 396 |
|
| 397 |
private long maxReads=-1; |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
private final FileFormat ffin1; |
| 402 |
private final FileFormat ffin2; |
| 403 |
|
| 404 |
private final FileFormat ffout1; |
| 405 |
private final FileFormat ffout2; |
| 406 |
private final FileFormat ffCounts; |
| 407 |
|
| 408 |
private final HashMap<String,String> expectedCodeMap; |
| 409 |
private final HashMap<String,String> validCodeMap; |
| 410 |
private final ArrayList<String> expectedCodes; |
| 411 |
private final ArrayList<String> validCodes; |
| 412 |
|
| 413 |
private final BandedAlignerConcrete bandy=new BandedAlignerConcrete(21); |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
private PrintStream outstream=System.err; |
| 418 |
public static boolean verbose=false; |
| 419 |
public boolean errorState=false; |
| 420 |
private boolean overwrite=true; |
| 421 |
private boolean append=false; |
| 422 |
|
| 423 |
} |