| 1 |
package prok; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.concurrent.atomic.AtomicInteger; |
| 7 |
|
| 8 |
import fileIO.ByteFile; |
| 9 |
import fileIO.ByteStreamWriter; |
| 10 |
import fileIO.FileFormat; |
| 11 |
import fileIO.ReadWrite; |
| 12 |
import shared.Parse; |
| 13 |
import shared.Parser; |
| 14 |
import shared.PreParser; |
| 15 |
import shared.Shared; |
| 16 |
import shared.Timer; |
| 17 |
import shared.Tools; |
| 18 |
import structures.ByteBuilder; |
| 19 |
import structures.IntList; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class AnalyzeGenes { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public static void main(String[] args){ |
| 40 |
|
| 41 |
Timer t=new Timer(); |
| 42 |
|
| 43 |
|
| 44 |
AnalyzeGenes x=new AnalyzeGenes(args); |
| 45 |
|
| 46 |
|
| 47 |
x.process(t); |
| 48 |
|
| 49 |
|
| 50 |
Shared.closeStream(x.outstream); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public AnalyzeGenes(String[] args){ |
| 58 |
|
| 59 |
{ |
| 60 |
PreParser pp=new PreParser(args, null, false); |
| 61 |
args=pp.args; |
| 62 |
outstream=pp.outstream; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 67 |
ReadWrite.setZipThreads(Shared.threads()); |
| 68 |
|
| 69 |
{ |
| 70 |
final Parser parser=parse(args); |
| 71 |
overwrite=parser.overwrite; |
| 72 |
append=parser.append; |
| 73 |
|
| 74 |
out=parser.out1; |
| 75 |
} |
| 76 |
|
| 77 |
if(alignRibo){ |
| 78 |
|
| 79 |
ProkObject.loadConsensusSequenceFromFile(false, false); |
| 80 |
} |
| 81 |
|
| 82 |
fixExtensions(); |
| 83 |
checkFileExistence(); |
| 84 |
checkStatics(); |
| 85 |
|
| 86 |
|
| 87 |
threads=Tools.min(fnaList.size(), Shared.threads(), Tools.max(32, Shared.CALC_LOGICAL_PROCESSORS()/2)); |
| 88 |
|
| 89 |
ffout=FileFormat.testOutput(out, FileFormat.PGM, null, true, overwrite, append, false); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
private Parser parse(String[] args){ |
| 98 |
|
| 99 |
Parser parser=new Parser(); |
| 100 |
parser.overwrite=overwrite; |
| 101 |
for(int i=0; i<args.length; i++){ |
| 102 |
String arg=args[i]; |
| 103 |
String[] split=arg.split("="); |
| 104 |
String a=split[0].toLowerCase(); |
| 105 |
String b=split.length>1 ? split[1] : null; |
| 106 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 107 |
|
| 108 |
|
| 109 |
if(PGMTools.parseStatic(arg, a, b)){ |
| 110 |
|
| 111 |
}else if(a.equals("in") || a.equals("infna") || a.equals("fnain") || a.equals("fna") || a.equals("ref")){ |
| 112 |
assert(b!=null); |
| 113 |
Tools.addFiles(b, fnaList); |
| 114 |
}else if(a.equals("gff") || a.equals("ingff") || a.equals("gffin")){ |
| 115 |
assert(b!=null); |
| 116 |
Tools.addFiles(b, gffList); |
| 117 |
}else if(a.equals("verbose")){ |
| 118 |
verbose=Parse.parseBoolean(b); |
| 119 |
ReadWrite.verbose=verbose; |
| 120 |
}else if(a.equals("alignribo") || a.equals("align")){ |
| 121 |
alignRibo=Parse.parseBoolean(b); |
| 122 |
}else if(a.equals("adjustendpoints")){ |
| 123 |
adjustEndpoints=Parse.parseBoolean(b); |
| 124 |
} |
| 125 |
|
| 126 |
else if(ProkObject.parse(arg, a, b)){} |
| 127 |
|
| 128 |
else if(parser.parse(arg, a, b)){ |
| 129 |
|
| 130 |
}else if(arg.indexOf('=')<0 && new File(arg).exists() && FileFormat.isFastaFile(arg)){ |
| 131 |
fnaList.add(arg); |
| 132 |
}else{ |
| 133 |
outstream.println("Unknown parameter "+args[i]); |
| 134 |
assert(false) : "Unknown parameter "+args[i]; |
| 135 |
|
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if(gffList.isEmpty()){ |
| 140 |
for(String s : fnaList){ |
| 141 |
String prefix=ReadWrite.stripExtension(s); |
| 142 |
String gff=prefix+".gff"; |
| 143 |
File f=new File(gff); |
| 144 |
if(!f.exists()){ |
| 145 |
String gz=gff+".gz"; |
| 146 |
f=new File(gz); |
| 147 |
assert(f.exists() && f.canRead()) : "Can't read file "+gff; |
| 148 |
gff=gz; |
| 149 |
} |
| 150 |
gffList.add(gff); |
| 151 |
} |
| 152 |
} |
| 153 |
assert(gffList.size()==fnaList.size()) : "Number of fna and gff files do not match: "+fnaList.size()+", "+gffList.size(); |
| 154 |
return parser; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
private void fixExtensions(){ |
| 159 |
fnaList=Tools.fixExtension(fnaList); |
| 160 |
gffList=Tools.fixExtension(gffList); |
| 161 |
if(fnaList.isEmpty()){throw new RuntimeException("Error - at least one input file is required.");} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
private void checkFileExistence(){ |
| 166 |
|
| 167 |
if(!Tools.testOutputFiles(overwrite, append, false, out)){ |
| 168 |
outstream.println((out==null)+", "+out); |
| 169 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out+"\n"); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
ArrayList<String> foo=new ArrayList<String>(); |
| 174 |
foo.addAll(fnaList); |
| 175 |
foo.addAll(gffList); |
| 176 |
if(!Tools.testInputFiles(false, true, foo.toArray(new String[0]))){ |
| 177 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
foo.add(out); |
| 182 |
if(!Tools.testForDuplicateFiles(true, foo.toArray(new String[0]))){ |
| 183 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
private static void checkStatics(){ |
| 189 |
|
| 190 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 191 |
ByteFile.FORCE_MODE_BF2=true; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
void process(Timer t){ |
| 200 |
|
| 201 |
final GeneModel pgm; |
| 202 |
if(Shared.threads()<2 || fnaList.size()<2){ |
| 203 |
pgm=makeModelST(); |
| 204 |
}else{ |
| 205 |
pgm=spawnThreads(); |
| 206 |
} |
| 207 |
|
| 208 |
ByteStreamWriter bsw=ByteStreamWriter.makeBSW(ffout); |
| 209 |
|
| 210 |
ByteBuilder bb=new ByteBuilder(); |
| 211 |
pgm.appendTo(bb); |
| 212 |
bytesOut+=bb.length; |
| 213 |
|
| 214 |
if(bsw!=null){ |
| 215 |
bsw.addJob(bb); |
| 216 |
errorState|=bsw.poisonAndWait(); |
| 217 |
} |
| 218 |
|
| 219 |
t.stop(); |
| 220 |
|
| 221 |
outstream.println(timeReadsBasesGenesProcessed(t, pgm.readsProcessed, pgm.basesProcessed, pgm.genesProcessed, pgm.filesProcessed, 8)); |
| 222 |
|
| 223 |
outstream.println(); |
| 224 |
outstream.println(typesProcessed(pgm, 12)); |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
if(errorState){ |
| 229 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
private static String timeReadsBasesGenesProcessed(Timer t, long readsProcessed, long basesProcessed, long genesProcessed, long filesProcessed, int pad){ |
| 234 |
return ("Time: \t"+t+"\n"+readsBasesGenesProcessed(t.elapsed, readsProcessed, basesProcessed, genesProcessed, filesProcessed, pad)); |
| 235 |
} |
| 236 |
|
| 237 |
private static String readsBasesGenesProcessed(long elapsed, long reads, long bases, long genes, long files, int pad){ |
| 238 |
double rpnano=reads/(double)elapsed; |
| 239 |
double bpnano=bases/(double)elapsed; |
| 240 |
double gpnano=genes/(double)elapsed; |
| 241 |
double fpnano=files/(double)elapsed; |
| 242 |
|
| 243 |
String rstring=Tools.padKMB(reads, pad); |
| 244 |
String bstring=Tools.padKMB(bases, pad); |
| 245 |
String gstring=Tools.padKMB(genes, pad); |
| 246 |
String fstring=Tools.padKMB(files, pad); |
| 247 |
ByteBuilder sb=new ByteBuilder(); |
| 248 |
sb.append("Files Processed: ").append(fstring).append(Tools.format(" \t%.2f files/sec", fpnano*1000000000)).append('\n'); |
| 249 |
sb.append("Sequences Processed:").append(rstring).append(Tools.format(" \t%.2fk seqs/sec", rpnano*1000000)).append('\n'); |
| 250 |
sb.append("Genes Processed: ").append(gstring).append(Tools.format(" \t%.2fk genes/sec", gpnano*1000000)).append('\n'); |
| 251 |
sb.append("Bases Processed: ").append(bstring).append(Tools.format(" \t%.2fm bases/sec", bpnano*1000)); |
| 252 |
return sb.toString(); |
| 253 |
} |
| 254 |
|
| 255 |
private static String typesProcessed(GeneModel pgm, int pad){ |
| 256 |
|
| 257 |
ByteBuilder sb=new ByteBuilder(); |
| 258 |
sb.append("CDS: "+Tools.padLeft(pgm.statsCDS.lengthCount, pad)).nl(); |
| 259 |
sb.append("tRNA: "+Tools.padLeft(pgm.statstRNA.lengthCount, pad)).nl(); |
| 260 |
sb.append("16S: "+Tools.padLeft(pgm.stats16S.lengthCount, pad)).nl(); |
| 261 |
sb.append("23S: "+Tools.padLeft(pgm.stats23S.lengthCount, pad)).nl(); |
| 262 |
sb.append("5S: "+Tools.padLeft(pgm.stats5S.lengthCount, pad)).nl(); |
| 263 |
sb.append("18S: "+Tools.padLeft(pgm.stats18S.lengthCount, pad)); |
| 264 |
return sb.toString(); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
private GeneModel makeModelST(){ |
| 273 |
GeneModel pgmSum=new GeneModel(true); |
| 274 |
|
| 275 |
for(int i=0; i<fnaList.size(); i++){ |
| 276 |
String fna=fnaList.get(i); |
| 277 |
String gff=gffList.get(i); |
| 278 |
pgmSum.process(fna, gff); |
| 279 |
} |
| 280 |
return pgmSum; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
private GeneModel spawnThreads(){ |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
final AtomicInteger aint=new AtomicInteger(0); |
| 293 |
|
| 294 |
|
| 295 |
ArrayList<FileThread> alpt=new ArrayList<FileThread>(threads); |
| 296 |
for(int i=0; i<threads; i++){ |
| 297 |
alpt.add(new FileThread(aint)); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
for(FileThread pt : alpt){ |
| 302 |
pt.start(); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
GeneModel pgm=waitForThreads(alpt); |
| 307 |
|
| 308 |
|
| 309 |
return pgm; |
| 310 |
} |
| 311 |
|
| 312 |
private GeneModel waitForThreads(ArrayList<FileThread> alpt){ |
| 313 |
|
| 314 |
GeneModel pgm=new GeneModel(false); |
| 315 |
|
| 316 |
|
| 317 |
boolean success=true; |
| 318 |
for(FileThread pt : alpt){ |
| 319 |
|
| 320 |
|
| 321 |
while(pt.getState()!=Thread.State.TERMINATED){ |
| 322 |
try { |
| 323 |
|
| 324 |
pt.join(); |
| 325 |
} catch (InterruptedException e) { |
| 326 |
|
| 327 |
e.printStackTrace(); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
pgm.add(pt.pgm); |
| 333 |
|
| 334 |
success&=pt.success; |
| 335 |
errorState|=pt.errorStateT; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
if(!success){errorState=true;} |
| 340 |
return pgm; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
private class FileThread extends Thread { |
| 348 |
|
| 349 |
FileThread(AtomicInteger fnum_){ |
| 350 |
fnum=fnum_; |
| 351 |
pgm=new GeneModel(true); |
| 352 |
} |
| 353 |
|
| 354 |
@Override |
| 355 |
public void run(){ |
| 356 |
for(int i=fnum.getAndIncrement(); i<fnaList.size(); i=fnum.getAndIncrement()){ |
| 357 |
String fna=fnaList.get(i); |
| 358 |
String gff=gffList.get(i); |
| 359 |
errorStateT=pgm.process(fna, gff)|errorState; |
| 360 |
|
| 361 |
} |
| 362 |
success=true; |
| 363 |
} |
| 364 |
|
| 365 |
private final AtomicInteger fnum; |
| 366 |
private final GeneModel pgm; |
| 367 |
boolean errorStateT=false; |
| 368 |
boolean success=false; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
private ArrayList<String> fnaList=new ArrayList<String>(); |
| 376 |
private ArrayList<String> gffList=new ArrayList<String>(); |
| 377 |
private IntList taxList=new IntList(); |
| 378 |
private String out=null; |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
private long bytesOut=0; |
| 383 |
static boolean alignRibo=true; |
| 384 |
static boolean adjustEndpoints=true; |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
private final FileFormat ffout; |
| 391 |
private final int threads; |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
private PrintStream outstream=System.err; |
| 398 |
public static boolean verbose=false; |
| 399 |
public boolean errorState=false; |
| 400 |
private boolean overwrite=true; |
| 401 |
private boolean append=false; |
| 402 |
|
| 403 |
} |
| 404 |
|