| 1 |
package gff; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Arrays; |
| 7 |
import java.util.HashMap; |
| 8 |
import java.util.concurrent.atomic.AtomicInteger; |
| 9 |
import java.util.concurrent.atomic.AtomicLong; |
| 10 |
import java.util.concurrent.locks.ReadWriteLock; |
| 11 |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 12 |
|
| 13 |
import aligner.Alignment; |
| 14 |
import fileIO.ByteFile; |
| 15 |
import fileIO.ByteStreamWriter; |
| 16 |
import fileIO.FileFormat; |
| 17 |
import fileIO.ReadWrite; |
| 18 |
import prok.PGMTools; |
| 19 |
import prok.ProkObject; |
| 20 |
import shared.Parse; |
| 21 |
import shared.Parser; |
| 22 |
import shared.PreParser; |
| 23 |
import shared.Shared; |
| 24 |
import shared.Timer; |
| 25 |
import shared.Tools; |
| 26 |
import stream.ConcurrentReadOutputStream; |
| 27 |
import stream.Read; |
| 28 |
import stream.ReadInputStream; |
| 29 |
import structures.ByteBuilder; |
| 30 |
import tax.GiToTaxid; |
| 31 |
import tax.TaxClient; |
| 32 |
import tax.TaxTree; |
| 33 |
import template.Accumulator; |
| 34 |
import template.ThreadWaiter; |
| 35 |
import tracker.ReadStats; |
| 36 |
|
| 37 |
public class CutGff implements Accumulator<CutGff.ProcessThread> { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public static void main(String[] args){ |
| 48 |
|
| 49 |
|
| 50 |
Timer t=new Timer(); |
| 51 |
|
| 52 |
|
| 53 |
CutGff x=new CutGff(args); |
| 54 |
|
| 55 |
|
| 56 |
x.process(t); |
| 57 |
|
| 58 |
|
| 59 |
Shared.closeStream(x.outstream); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public CutGff(String[] args){ |
| 67 |
{ |
| 68 |
PreParser pp=new PreParser(args, null, false); |
| 69 |
args=pp.args; |
| 70 |
outstream=pp.outstream; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 75 |
ReadWrite.setZipThreads(Shared.threads()); |
| 76 |
|
| 77 |
Shared.TRIM_READ_COMMENTS=Shared.TRIM_RNAME=true; |
| 78 |
Read.TO_UPPER_CASE=true; |
| 79 |
Read.VALIDATE_IN_CONSTRUCTOR=true; |
| 80 |
GffLine.parseAttributes=true; |
| 81 |
|
| 82 |
{ |
| 83 |
final Parser parser=parse(args); |
| 84 |
overwrite=parser.overwrite; |
| 85 |
append=parser.append; |
| 86 |
|
| 87 |
out=parser.out1; |
| 88 |
} |
| 89 |
|
| 90 |
if(alignRibo){ |
| 91 |
|
| 92 |
ProkObject.loadConsensusSequenceFromFile(false, false); |
| 93 |
} |
| 94 |
|
| 95 |
fixExtensions(); |
| 96 |
checkFileExistence(); |
| 97 |
checkStatics(); |
| 98 |
|
| 99 |
ffout=FileFormat.testOutput(out, FileFormat.PGM, null, true, overwrite, append, false); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
private Parser parse(String[] args){ |
| 108 |
|
| 109 |
Parser parser=new Parser(); |
| 110 |
parser.overwrite=overwrite; |
| 111 |
for(int i=0; i<args.length; i++){ |
| 112 |
String arg=args[i]; |
| 113 |
String[] split=arg.split("="); |
| 114 |
String a=split[0].toLowerCase(); |
| 115 |
String b=split.length>1 ? split[1] : null; |
| 116 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 117 |
|
| 118 |
|
| 119 |
if(PGMTools.parseStatic(arg, a, b)){ |
| 120 |
|
| 121 |
}else if(a.equals("in") || a.equals("infna") || a.equals("fnain") || a.equals("fna") || a.equals("ref")){ |
| 122 |
assert(b!=null); |
| 123 |
Tools.addFiles(b, fnaList); |
| 124 |
}else if(a.equals("gff") || a.equals("ingff") || a.equals("gffin")){ |
| 125 |
assert(b!=null); |
| 126 |
Tools.addFiles(b, gffList); |
| 127 |
}else if(a.equals("verbose")){ |
| 128 |
verbose=Parse.parseBoolean(b); |
| 129 |
ReadWrite.verbose=verbose; |
| 130 |
}else if(a.equals("alignribo") || a.equals("align")){ |
| 131 |
alignRibo=Parse.parseBoolean(b); |
| 132 |
}else if(a.equals("adjustendpoints")){ |
| 133 |
adjustEndpoints=Parse.parseBoolean(b); |
| 134 |
}else if(a.equalsIgnoreCase("slop16s") || a.equalsIgnoreCase("16sslop") || a.equalsIgnoreCase("ssuslop")){ |
| 135 |
ssuSlop=Integer.parseInt(b); |
| 136 |
}else if(a.equalsIgnoreCase("slop23s") || a.equalsIgnoreCase("23sslop") || a.equalsIgnoreCase("lsuslop")){ |
| 137 |
lsuSlop=Integer.parseInt(b); |
| 138 |
}else if(a.equalsIgnoreCase("maxns") || a.equalsIgnoreCase("maxundefined")){ |
| 139 |
maxNs=Integer.parseInt(b); |
| 140 |
}else if(a.equalsIgnoreCase("maxnrate") || a.equalsIgnoreCase("maxnfraction")){ |
| 141 |
maxNFraction=Integer.parseInt(b); |
| 142 |
}else if(a.equals("invert")){ |
| 143 |
invert=Parse.parseBoolean(b); |
| 144 |
}else if(a.equals("type") || a.equals("types")){ |
| 145 |
types=b; |
| 146 |
}else if(a.equals("attributes") || a.equals("requiredattributes")){ |
| 147 |
requiredAttributes=b.split(","); |
| 148 |
}else if(a.equals("banattributes") || a.equals("bannedattributes")){ |
| 149 |
bannedAttributes=b.split(","); |
| 150 |
}else if(a.equals("banpartial")){ |
| 151 |
banPartial=Parse.parseBoolean(b); |
| 152 |
} |
| 153 |
|
| 154 |
else if(a.equalsIgnoreCase("renameByTaxID")){ |
| 155 |
renameByTaxID=Parse.parseBoolean(b); |
| 156 |
}else if(a.equals("taxmode")){ |
| 157 |
if("accession".equalsIgnoreCase(b)){ |
| 158 |
taxMode=ACCESSION_MODE; |
| 159 |
}else if("header".equalsIgnoreCase(b)){ |
| 160 |
taxMode=HEADER_MODE; |
| 161 |
}else if("gi".equalsIgnoreCase(b)){ |
| 162 |
taxMode=GI_MODE; |
| 163 |
}else if("taxid".equalsIgnoreCase(b)){ |
| 164 |
taxMode=TAXID_MODE; |
| 165 |
}else{ |
| 166 |
assert(false) : "Bad tax mode: "+b; |
| 167 |
} |
| 168 |
}else if(a.equals("requirepresent")){ |
| 169 |
requirePresent=Parse.parseBoolean(b); |
| 170 |
}else if(a.equalsIgnoreCase("onePerFile")){ |
| 171 |
onePerFile=Parse.parseBoolean(b); |
| 172 |
}else if(a.equalsIgnoreCase("pickBest") || a.equalsIgnoreCase("findBest") || a.equalsIgnoreCase("keepBest")){ |
| 173 |
pickBest=Parse.parseBoolean(b); |
| 174 |
} |
| 175 |
|
| 176 |
else if(a.equals("minlen")){ |
| 177 |
minLen=Integer.parseInt(b); |
| 178 |
}else if(a.equals("maxlen")){ |
| 179 |
maxLen=Integer.parseInt(b); |
| 180 |
} |
| 181 |
|
| 182 |
else if(ProkObject.parse(arg, a, b)){ |
| 183 |
|
| 184 |
}else if(parser.parse(arg, a, b)){ |
| 185 |
|
| 186 |
}else if(arg.indexOf('=')<0 && new File(arg).exists() && FileFormat.isFastaFile(arg)){ |
| 187 |
fnaList.add(arg); |
| 188 |
}else{ |
| 189 |
outstream.println("Unknown parameter "+args[i]); |
| 190 |
assert(false) : "Unknown parameter "+args[i]; |
| 191 |
|
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
ArrayList<String> banned=new ArrayList<String>(); |
| 196 |
if(banPartial){banned.add("partial=true");} |
| 197 |
if(bannedAttributes!=null){ |
| 198 |
for(String s : bannedAttributes){banned.add(s);} |
| 199 |
} |
| 200 |
bannedAttributes=banned.isEmpty() ? null : banned.toArray(new String[0]); |
| 201 |
|
| 202 |
if(gffList.isEmpty()){ |
| 203 |
for(String s : fnaList){ |
| 204 |
String prefix=ReadWrite.stripExtension(s); |
| 205 |
String gff=prefix+".gff"; |
| 206 |
File f=new File(gff); |
| 207 |
if(!f.exists()){ |
| 208 |
String gz=gff+".gz"; |
| 209 |
f=new File(gz); |
| 210 |
assert(f.exists() && f.canRead()) : "Can't read file "+gff; |
| 211 |
gff=gz; |
| 212 |
} |
| 213 |
gffList.add(gff); |
| 214 |
} |
| 215 |
} |
| 216 |
assert(gffList.size()==fnaList.size()) : "Number of fna and gff files do not match: "+fnaList.size()+", "+gffList.size(); |
| 217 |
return parser; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
private void fixExtensions(){ |
| 222 |
fnaList=Tools.fixExtension(fnaList); |
| 223 |
gffList=Tools.fixExtension(gffList); |
| 224 |
if(fnaList.isEmpty()){throw new RuntimeException("Error - at least one input file is required.");} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
private void checkFileExistence(){ |
| 229 |
|
| 230 |
if(!Tools.testOutputFiles(overwrite, append, false, out)){ |
| 231 |
outstream.println((out==null)+", "+out); |
| 232 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out+"\n"); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
ArrayList<String> foo=new ArrayList<String>(); |
| 237 |
foo.addAll(fnaList); |
| 238 |
foo.addAll(gffList); |
| 239 |
if(!Tools.testInputFiles(false, true, foo.toArray(new String[0]))){ |
| 240 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
foo.add(out); |
| 245 |
if(!Tools.testForDuplicateFiles(true, foo.toArray(new String[0]))){ |
| 246 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
private static void checkStatics(){ |
| 252 |
|
| 253 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 254 |
ByteFile.FORCE_MODE_BF2=true; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public void process(Timer t){ |
| 267 |
if(Shared.threads()>2 && fnaList.size()>1){ |
| 268 |
processMT(t); |
| 269 |
}else{ |
| 270 |
processST(t); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
public void processST(Timer t){ |
| 275 |
ByteStreamWriter bsw=(ffout==null ? null : new ByteStreamWriter(ffout)); |
| 276 |
if(bsw!=null){bsw.start();} |
| 277 |
|
| 278 |
for(int i=0; i<fnaList.size(); i++){ |
| 279 |
processFileST(fnaList.get(i), gffList.get(i), types, bsw); |
| 280 |
} |
| 281 |
|
| 282 |
if(bsw!=null){bsw.poisonAndWait();} |
| 283 |
t.stop(); |
| 284 |
if(ffout!=null){outstream.println("Wrote "+out);} |
| 285 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 286 |
outstream.println(Tools.readsBasesOut(readsProcessed, basesProcessed, readsOut, basesOut, 8, false)); |
| 287 |
if(alignRibo){ |
| 288 |
outstream.println(Tools.number("Flipped: ", flipped.get(), 8)); |
| 289 |
outstream.println(Tools.number("Failed Alignment: ", failed.get(), 8)); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
public void processMT(Timer t){ |
| 294 |
|
| 295 |
|
| 296 |
final ConcurrentReadOutputStream ros=makeCros(); |
| 297 |
|
| 298 |
|
| 299 |
readsProcessed=readsOut=0; |
| 300 |
basesProcessed=basesOut=0; |
| 301 |
|
| 302 |
|
| 303 |
spawnThreads(ros); |
| 304 |
|
| 305 |
if(verbose){outstream.println("Finished; closing streams.");} |
| 306 |
|
| 307 |
|
| 308 |
errorState|=ReadStats.writeAll(); |
| 309 |
|
| 310 |
errorState|=ReadWrite.closeStream(ros); |
| 311 |
|
| 312 |
|
| 313 |
t.stop(); |
| 314 |
if(ffout!=null){outstream.println("Wrote "+out);} |
| 315 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 316 |
outstream.println(Tools.readsBasesOut(readsProcessed, basesProcessed, readsOut, basesOut, 8, false)); |
| 317 |
if(alignRibo){ |
| 318 |
outstream.println(Tools.number("Flipped: ", flipped.get(), 8)); |
| 319 |
outstream.println(Tools.number("Failed Alignment: ", failed.get(), 8)); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
if(errorState){ |
| 324 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
private boolean hasAttributes(GffLine gline){ |
| 331 |
int len=gline.length(); |
| 332 |
if(len<minLen || len>maxLen){return false;} |
| 333 |
if(hasAttributes(gline, bannedAttributes)){return false;} |
| 334 |
return requiredAttributes==null || hasAttributes(gline, requiredAttributes); |
| 335 |
} |
| 336 |
|
| 337 |
private static boolean hasAttributes(GffLine gline, String[] attributes){ |
| 338 |
if(attributes==null){return false;} |
| 339 |
for(String s : attributes){ |
| 340 |
if(gline.attributes.contains(s)){ |
| 341 |
return true; |
| 342 |
} |
| 343 |
} |
| 344 |
return false; |
| 345 |
} |
| 346 |
|
| 347 |
private void processFileST(String fna, String gff, String types, ByteStreamWriter bsw){ |
| 348 |
ArrayList<Read> reads=processFile(fna, gff, types); |
| 349 |
if(reads!=null){ |
| 350 |
for(Read r : reads){ |
| 351 |
if(bsw!=null){bsw.println(r);} |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
private ArrayList<Read> processFile(String fna, String gff, String types){ |
| 357 |
ArrayList<GffLine> lines=GffLine.loadGffFile(gff, types, false); |
| 358 |
|
| 359 |
ArrayList<Read> list=ReadInputStream.toReads(fna, FileFormat.FA, -1); |
| 360 |
HashMap<String, Read> map=new HashMap<String, Read>(); |
| 361 |
|
| 362 |
for(Read r : list){ |
| 363 |
readsProcessed++; |
| 364 |
basesProcessed+=r.length(); |
| 365 |
map.put(r.id, r); |
| 366 |
} |
| 367 |
|
| 368 |
if(renameByTaxID){ |
| 369 |
renameByTaxID(list); |
| 370 |
} |
| 371 |
|
| 372 |
ArrayList<Read> outList=processLines(lines, map, invert); |
| 373 |
|
| 374 |
if(invert){ |
| 375 |
for(Read r : list){ |
| 376 |
readsOut++; |
| 377 |
basesOut+=r.length(); |
| 378 |
} |
| 379 |
return list; |
| 380 |
}else{ |
| 381 |
if(outList!=null){ |
| 382 |
for(Read r : outList){ |
| 383 |
readsOut++; |
| 384 |
basesOut+=r.length(); |
| 385 |
} |
| 386 |
} |
| 387 |
return outList; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
private void renameByTaxID(ArrayList<Read> list){ |
| 392 |
ByteBuilder bb=new ByteBuilder(); |
| 393 |
for(Read r : list){ |
| 394 |
if(bb.length>0){bb.comma();} |
| 395 |
bb.append(taxMode==HEADER_MODE ? r.id : taxMode==ACCESSION_MODE ? parseAccession(r.id) : parseGi(r.id)); |
| 396 |
} |
| 397 |
final int[] ids; |
| 398 |
if(taxMode==ACCESSION_MODE){ |
| 399 |
ids=TaxClient.accessionToTaxidArray(bb.toString()); |
| 400 |
}else if(taxMode==GI_MODE){ |
| 401 |
ids=TaxClient.giToTaxidArray(bb.toString()); |
| 402 |
}else if(taxMode==HEADER_MODE){ |
| 403 |
ids=TaxClient.headerToTaxidArray(bb.toString()); |
| 404 |
}else if(taxMode==TAXID_MODE){ |
| 405 |
ids=parseTaxIds(list); |
| 406 |
}else{ |
| 407 |
throw new RuntimeException("Bad mode: "+TAXID_MODE); |
| 408 |
} |
| 409 |
assert(ids.length==list.size()) : ids.length+", "+list.size(); |
| 410 |
for(int i=0; i<ids.length; i++){ |
| 411 |
Read r=list.get(i); |
| 412 |
int id=ids[i]; |
| 413 |
|
| 414 |
if(r.id!=null && r.id.startsWith("tid|")){ |
| 415 |
id=TaxTree.parseHeaderStatic(r.id); |
| 416 |
r.obj=id; |
| 417 |
}else { |
| 418 |
assert(id>=0 || !requirePresent) : "Can't find taxID for header: "+id+", "+r.name(); |
| 419 |
|
| 420 |
r.obj=id; |
| 421 |
r.id="tid|"+id+"|"+id; |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
private int[] parseTaxIds(ArrayList<Read> list){ |
| 427 |
int[] ids=new int[list.size()]; |
| 428 |
for(int i=0; i<list.size(); i++){ |
| 429 |
Read r=list.get(i); |
| 430 |
int x=GiToTaxid.parseTaxidNumber(r.id, '|'); |
| 431 |
ids[i]=x; |
| 432 |
} |
| 433 |
return ids; |
| 434 |
} |
| 435 |
|
| 436 |
private String parseAccession(String id){ |
| 437 |
int dot=id.indexOf('.'); |
| 438 |
int space=id.indexOf(' '); |
| 439 |
|
| 440 |
int limit=Tools.min((dot<0 ? id.length() : dot), (space<0 ? id.length() : space)); |
| 441 |
return id.substring(0, limit); |
| 442 |
} |
| 443 |
|
| 444 |
private String parseGi(String id){ |
| 445 |
assert(id.startsWith("gi|")); |
| 446 |
String[] split=id.split("|"); |
| 447 |
return split[1]; |
| 448 |
} |
| 449 |
|
| 450 |
private ArrayList<Read> processLines(ArrayList<GffLine> lines, HashMap<String, Read> map, boolean invertSelection){ |
| 451 |
ArrayList<Read> list=null; |
| 452 |
for(GffLine gline : lines){ |
| 453 |
if(hasAttributes(gline)){ |
| 454 |
Read scaf=map.get(gline.seqid); |
| 455 |
assert(scaf!=null) : "Can't find "+gline.seqid+" in "+map.keySet(); |
| 456 |
|
| 457 |
boolean pass=true; |
| 458 |
Float identity=null; |
| 459 |
if(alignRibo && gline.inbounds(scaf.length())){ |
| 460 |
int type=gline.prokType(); |
| 461 |
identity=align(gline, scaf.bases, type); |
| 462 |
if(identity==null){pass=false;} |
| 463 |
} |
| 464 |
|
| 465 |
if(pass){ |
| 466 |
final int start=gline.start-1; |
| 467 |
final int stop=gline.stop-1; |
| 468 |
|
| 469 |
if(invertSelection){ |
| 470 |
byte[] bases=scaf.bases; |
| 471 |
for(int i=start; i<=stop; i++){ |
| 472 |
if(i>=0 && i<bases.length){ |
| 473 |
bases[i]='N'; |
| 474 |
} |
| 475 |
} |
| 476 |
}else{ |
| 477 |
if(start>=0 && stop<scaf.length()){ |
| 478 |
String id=gline.attributes; |
| 479 |
if(renameByTaxID){ |
| 480 |
id="tid|"+scaf.obj+"|"+id; |
| 481 |
} |
| 482 |
Read r=new Read(Arrays.copyOfRange(scaf.bases, start, stop+1), null, id, 1); |
| 483 |
r.obj=identity; |
| 484 |
|
| 485 |
assert(!r.containsLowercase()) : r.toFasta()+"\n" |
| 486 |
+ "validated="+r.validated()+", scaf.validated="+scaf.validated()+", tuc="+Read.TO_UPPER_CASE+", vic="+Read.VALIDATE_IN_CONSTRUCTOR; |
| 487 |
if(maxNs>=0 || maxNFraction>=0){ |
| 488 |
long allowed=Tools.min(maxNs>=0 ? maxNs : r.length(), (long)(r.length()*(maxNFraction>=0 ? maxNFraction : 1))); |
| 489 |
if(r.countUndefined()>allowed){r=null;} |
| 490 |
} |
| 491 |
|
| 492 |
if(r!=null){ |
| 493 |
if(gline.strand==1){r.reverseComplement();} |
| 494 |
if(list==null){list=new ArrayList<Read>(8);} |
| 495 |
list.add(r); |
| 496 |
if(onePerFile && !pickBest){break;} |
| 497 |
} |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
if(pickBest && list!=null && list.size()>1){ |
| 504 |
Read best=null; |
| 505 |
float bestID=0; |
| 506 |
for(Read r : list){ |
| 507 |
float identity=(r.obj==null ? 0.001f : (Float)r.obj); |
| 508 |
if(identity>bestID){ |
| 509 |
bestID=identity; |
| 510 |
best=r; |
| 511 |
} |
| 512 |
} |
| 513 |
assert(best!=null); |
| 514 |
list.clear(); |
| 515 |
list.add(best); |
| 516 |
} |
| 517 |
return list; |
| 518 |
} |
| 519 |
|
| 520 |
private Float align(GffLine gline, byte[] scaf, int type){ |
| 521 |
Read[] consensusReads=ProkObject.consensusReads(type); |
| 522 |
if(consensusReads==null || consensusReads.length==0){ |
| 523 |
assert(false) : type+"\n"+gline.toString(); |
| 524 |
return null; |
| 525 |
} |
| 526 |
byte[] universal=consensusReads[0].bases; |
| 527 |
float minIdentity=ProkObject.minID(type)*ID_MULT; |
| 528 |
if(universal==null){assert(false); return 1F;} |
| 529 |
|
| 530 |
int start=gline.start-1; |
| 531 |
int stop=gline.stop-1; |
| 532 |
assert(start<=stop) : start+", "+stop+", "+scaf.length; |
| 533 |
assert(start>=0 && start<scaf.length) : start+", "+stop+", "+scaf.length; |
| 534 |
assert(stop>=0 && stop<scaf.length) : start+", "+stop+", "+scaf.length; |
| 535 |
|
| 536 |
|
| 537 |
final int a=Tools.max(0, start); |
| 538 |
final int b=Tools.min(scaf.length-1, stop); |
| 539 |
|
| 540 |
byte[] ref=Arrays.copyOfRange(scaf, a, b+1); |
| 541 |
Read r=new Read(ref, null, 0); |
| 542 |
if(gline.strand==GffLine.MINUS){r.reverseComplement();} |
| 543 |
|
| 544 |
Alignment plus=new Alignment(r); |
| 545 |
plus.align(universal); |
| 546 |
|
| 547 |
|
| 548 |
r.reverseComplement(); |
| 549 |
Alignment minus=new Alignment(r); |
| 550 |
minus.align(universal); |
| 551 |
|
| 552 |
Alignment best=null; |
| 553 |
if(plus.id>=minus.id){ |
| 554 |
best=plus; |
| 555 |
|
| 556 |
}else{ |
| 557 |
best=minus; |
| 558 |
if(minus.id>=minIdentity){ |
| 559 |
if(verbose) {System.err.println("Flipped: "+plus.id+" \t"+minus.id+"");} |
| 560 |
flipped.incrementAndGet(); |
| 561 |
gline.strand=Shared.MINUS; |
| 562 |
} |
| 563 |
} |
| 564 |
if(best.id>=minIdentity){ |
| 565 |
return best.id; |
| 566 |
}else{ |
| 567 |
if(verbose) {System.err.println("Failed alignment: "+plus.id+" \t"+minus.id);} |
| 568 |
failed.incrementAndGet(); |
| 569 |
return null; |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
private ConcurrentReadOutputStream makeCros(){ |
| 598 |
if(ffout==null){return null;} |
| 599 |
|
| 600 |
|
| 601 |
final int buff=(ordered ? Tools.mid(16, 128, (Shared.threads()*2)/3) : 8); |
| 602 |
|
| 603 |
final ConcurrentReadOutputStream ros=ConcurrentReadOutputStream.getStream(ffout, null, buff, null, false); |
| 604 |
ros.start(); |
| 605 |
return ros; |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
private void spawnThreads(ConcurrentReadOutputStream ros){ |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
final int threads=Tools.min(Shared.threads(), fnaList.size()); |
| 614 |
|
| 615 |
|
| 616 |
AtomicInteger atom=new AtomicInteger(0); |
| 617 |
|
| 618 |
|
| 619 |
ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(threads); |
| 620 |
for(int i=0; i<threads; i++){ |
| 621 |
alpt.add(new ProcessThread(atom, ros, i)); |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
boolean success=ThreadWaiter.startAndWait(alpt, this); |
| 626 |
errorState&=!success; |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
} |
| 631 |
|
| 632 |
@Override |
| 633 |
public final void accumulate(ProcessThread pt){ |
| 634 |
readsProcessed+=pt.readsProcessedT; |
| 635 |
basesProcessed+=pt.basesProcessedT; |
| 636 |
readsOut+=pt.readsOutT; |
| 637 |
basesOut+=pt.basesOutT; |
| 638 |
errorState|=(!pt.success); |
| 639 |
} |
| 640 |
|
| 641 |
@Override |
| 642 |
public final boolean success(){return !errorState;} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
class ProcessThread extends Thread { |
| 651 |
|
| 652 |
|
| 653 |
ProcessThread(final AtomicInteger atom_, ConcurrentReadOutputStream ros_, final int tid_){ |
| 654 |
atom=atom_; |
| 655 |
ros=ros_; |
| 656 |
tid=tid_; |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
@Override |
| 661 |
public void run(){ |
| 662 |
|
| 663 |
|
| 664 |
for(int fnum=atom.getAndIncrement(), lim=fnaList.size(); fnum<lim; fnum=atom.getAndIncrement()) { |
| 665 |
String fna=fnaList.get(fnum); |
| 666 |
String gff=gffList.get(fnum); |
| 667 |
|
| 668 |
ArrayList<Read> list=processFileT(fna, gff, types); |
| 669 |
if(ros!=null){ |
| 670 |
if(list==null){list=dummy;} |
| 671 |
ros.add(list, fnum); |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
success=true; |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
private ArrayList<Read> processFileT(String fna, String gff, String types){ |
| 683 |
ArrayList<GffLine> lines=GffLine.loadGffFile(gff, types, false); |
| 684 |
|
| 685 |
ArrayList<Read> list=ReadInputStream.toReads(fna, FileFormat.FA, -1); |
| 686 |
HashMap<String, Read> map=new HashMap<String, Read>(); |
| 687 |
|
| 688 |
for(Read r : list){ |
| 689 |
readsProcessedT++; |
| 690 |
basesProcessedT+=r.length(); |
| 691 |
map.put(r.id, r); |
| 692 |
} |
| 693 |
|
| 694 |
if(renameByTaxID){ |
| 695 |
renameByTaxID(list); |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
ArrayList<Read> outList=processLines(lines, map, invert); |
| 700 |
|
| 701 |
if(invert){ |
| 702 |
for(Read r : list){ |
| 703 |
readsOutT++; |
| 704 |
basesOutT+=r.length(); |
| 705 |
} |
| 706 |
return list; |
| 707 |
}else{ |
| 708 |
if(outList!=null){ |
| 709 |
for(Read r : outList){ |
| 710 |
readsOutT++; |
| 711 |
basesOutT+=r.length(); |
| 712 |
} |
| 713 |
} |
| 714 |
return outList; |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
protected long readsProcessedT=0; |
| 720 |
|
| 721 |
protected long basesProcessedT=0; |
| 722 |
|
| 723 |
|
| 724 |
protected long readsOutT=0; |
| 725 |
|
| 726 |
protected long basesOutT=0; |
| 727 |
|
| 728 |
|
| 729 |
boolean success=false; |
| 730 |
|
| 731 |
|
| 732 |
private final ConcurrentReadOutputStream ros; |
| 733 |
|
| 734 |
final int tid; |
| 735 |
|
| 736 |
final AtomicInteger atom; |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
private ArrayList<String> fnaList=new ArrayList<String>(); |
| 744 |
private ArrayList<String> gffList=new ArrayList<String>(); |
| 745 |
private String out=null; |
| 746 |
private String types="CDS"; |
| 747 |
private boolean invert=false; |
| 748 |
private boolean banPartial=true; |
| 749 |
private int minLen=1; |
| 750 |
private int maxLen=Integer.MAX_VALUE; |
| 751 |
|
| 752 |
private String[] requiredAttributes; |
| 753 |
private String[] bannedAttributes; |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
private long bytesOut=0; |
| 758 |
private boolean renameByTaxID=false; |
| 759 |
private int taxMode=ACCESSION_MODE; |
| 760 |
private boolean requirePresent=false; |
| 761 |
private boolean alignRibo=false; |
| 762 |
private boolean adjustEndpoints=false; |
| 763 |
private boolean onePerFile=false; |
| 764 |
private boolean pickBest=false; |
| 765 |
private int ssuSlop=999; |
| 766 |
private int lsuSlop=999; |
| 767 |
|
| 768 |
private float ID_MULT=0.96f; |
| 769 |
|
| 770 |
private int maxNs=-1; |
| 771 |
private double maxNFraction=-1; |
| 772 |
|
| 773 |
private static int ACCESSION_MODE=0, GI_MODE=1, HEADER_MODE=2, TAXID_MODE=3; |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
protected long readsProcessed=0; |
| 779 |
|
| 780 |
protected long basesProcessed=0; |
| 781 |
|
| 782 |
|
| 783 |
protected long readsOut=0; |
| 784 |
|
| 785 |
protected long basesOut=0; |
| 786 |
|
| 787 |
protected AtomicLong flipped=new AtomicLong(0); |
| 788 |
protected AtomicLong failed=new AtomicLong(0); |
| 789 |
|
| 790 |
|
| 791 |
private long maxReads=-1; |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
private final FileFormat ffout; |
| 798 |
private final ArrayList<Read> dummy=new ArrayList<Read>(); |
| 799 |
|
| 800 |
@Override |
| 801 |
public final ReadWriteLock rwlock() {return rwlock;} |
| 802 |
private final ReadWriteLock rwlock=new ReentrantReadWriteLock(); |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
private PrintStream outstream=System.err; |
| 809 |
public static boolean verbose=false; |
| 810 |
public boolean errorState=false; |
| 811 |
public boolean ordered=true; |
| 812 |
private boolean overwrite=true; |
| 813 |
private boolean append=false; |
| 814 |
|
| 815 |
} |