jpayne@68: package tax; jpayne@68: jpayne@68: import java.io.File; jpayne@68: import java.io.PrintStream; jpayne@68: jpayne@68: import fileIO.ByteFile; jpayne@68: import fileIO.ByteFile1; jpayne@68: import fileIO.ByteFile2; jpayne@68: import fileIO.ByteStreamWriter; jpayne@68: import fileIO.FileFormat; jpayne@68: import fileIO.ReadWrite; jpayne@68: import shared.Parse; jpayne@68: import shared.Parser; jpayne@68: import shared.PreParser; jpayne@68: import shared.Shared; jpayne@68: import shared.Timer; jpayne@68: import shared.Tools; jpayne@68: import stream.ConcurrentGenericReadInputStream; jpayne@68: import stream.FastaReadInputStream; jpayne@68: import structures.ByteBuilder; jpayne@68: import structures.IntHashSet; jpayne@68: jpayne@68: /** jpayne@68: * @author Brian Bushnell jpayne@68: * @date May 9, 2016 jpayne@68: * jpayne@68: */ jpayne@68: public class RenameIMG { jpayne@68: jpayne@68: public static void main(String[] args){ jpayne@68: Timer t=new Timer(); jpayne@68: RenameIMG x=new RenameIMG(args); jpayne@68: x.process(t); jpayne@68: jpayne@68: //Close the print stream if it was redirected jpayne@68: Shared.closeStream(x.outstream); jpayne@68: } jpayne@68: jpayne@68: public RenameIMG(String[] args){ jpayne@68: jpayne@68: {//Preparse block for help, config files, and outstream jpayne@68: PreParser pp=new PreParser(args, getClass(), false); jpayne@68: args=pp.args; jpayne@68: outstream=pp.outstream; jpayne@68: } jpayne@68: jpayne@68: ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; jpayne@68: ReadWrite.MAX_ZIP_THREADS=Shared.threads(); jpayne@68: jpayne@68: Parser parser=new Parser(); jpayne@68: for(int i=0; i1 ? split[1] : null; jpayne@68: jpayne@68: if(a.equals("lines")){ jpayne@68: maxLines=Long.parseLong(b); jpayne@68: if(maxLines<0){maxLines=Long.MAX_VALUE;} jpayne@68: }else if(a.equals("verbose")){ jpayne@68: verbose=Parse.parseBoolean(b); jpayne@68: ByteFile1.verbose=verbose; jpayne@68: ByteFile2.verbose=verbose; jpayne@68: stream.FastaReadInputStream.verbose=verbose; jpayne@68: ConcurrentGenericReadInputStream.verbose=verbose; jpayne@68: stream.FastqReadInputStream.verbose=verbose; jpayne@68: ReadWrite.verbose=verbose; jpayne@68: }else if(a.equals("img")){ jpayne@68: imgFile=b; jpayne@68: }else if(parser.parse(arg, a, b)){ jpayne@68: //do nothing jpayne@68: }else{ jpayne@68: outstream.println("Unknown parameter "+args[i]); jpayne@68: assert(false) : "Unknown parameter "+args[i]; jpayne@68: // throw new RuntimeException("Unknown parameter "+args[i]); jpayne@68: } jpayne@68: } jpayne@68: jpayne@68: {//Process parser fields jpayne@68: overwrite=parser.overwrite; jpayne@68: append=parser.append; jpayne@68: jpayne@68: in1=parser.in1; jpayne@68: jpayne@68: out1=parser.out1; jpayne@68: } jpayne@68: jpayne@68: assert(FastaReadInputStream.settingsOK()); jpayne@68: jpayne@68: if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} jpayne@68: if("auto".equalsIgnoreCase(imgFile)){imgFile=TaxTree.defaultImgFile();}//TODO: why are these set to the same default? jpayne@68: if("auto".equalsIgnoreCase(in1)){in1=TaxTree.defaultImgFile();} jpayne@68: jpayne@68: if(!ByteFile.FORCE_MODE_BF2){ jpayne@68: ByteFile.FORCE_MODE_BF2=false; jpayne@68: ByteFile.FORCE_MODE_BF1=true; jpayne@68: } jpayne@68: jpayne@68: if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;} jpayne@68: jpayne@68: if(!Tools.testOutputFiles(overwrite, append, false, out1)){ jpayne@68: outstream.println((out1==null)+", "+out1); jpayne@68: throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n"); jpayne@68: } jpayne@68: jpayne@68: ffout1=FileFormat.testOutput(out1, FileFormat.FA, null, true, overwrite, append, false); jpayne@68: } jpayne@68: jpayne@68: void copyFiles(ImgRecord[] array){ jpayne@68: if(useSet){set=new IntHashSet(10000);} jpayne@68: ByteStreamWriter bsw=new ByteStreamWriter(ffout1); jpayne@68: bsw.start(); jpayne@68: for(ImgRecord ir : array){ jpayne@68: if(ir.taxID>0){set.add(ir.taxID);} jpayne@68: else{unknownTaxid++;} jpayne@68: FileFormat ffin=FileFormat.testInput(ir.path(), FileFormat.FA, null, true, true); jpayne@68: process_inner(ffin, bsw, ir.imgID); jpayne@68: } jpayne@68: knownTaxid=set.size(); jpayne@68: set=null; jpayne@68: if(bsw!=null){errorState|=bsw.poisonAndWait();} jpayne@68: } jpayne@68: jpayne@68: void process(Timer t){ jpayne@68: ImgRecord[] array=ImgRecord.toArray(in1, TaxTree.IMG_HQ); jpayne@68: if(imgFile==null){ jpayne@68: TaxTree.loadIMG(array); jpayne@68: }else{ jpayne@68: ImgRecord[] array2=ImgRecord.toArray(imgFile, TaxTree.IMG_HQ); jpayne@68: TaxTree.loadIMG(array2); jpayne@68: } jpayne@68: jpayne@68: copyFiles(array); jpayne@68: jpayne@68: t.stop(); jpayne@68: jpayne@68: final int spaces=8; jpayne@68: String fpstring=""+filesProcessed; jpayne@68: String cpstring=Tools.padKM(sequencesProcessed, spaces); jpayne@68: String bapstring=Tools.padKM(basesProcessed, spaces); jpayne@68: String tpstring=""+knownTaxid; jpayne@68: jpayne@68: outstream.println("Time: \t"+t); jpayne@68: outstream.println("Files Processed: "+fpstring); jpayne@68: outstream.println("Contigs Processed: "+cpstring); jpayne@68: outstream.println("Bases Processed: "+bapstring); jpayne@68: if(useSet){outstream.println("TaxIDs Processed: "+tpstring+" \t"+"("+unknownTaxid+" unknown)");} jpayne@68: outstream.println(Tools.linesBytesProcessed(t.elapsed, linesProcessed, bytesProcessed, spaces)); jpayne@68: jpayne@68: outstream.println(); jpayne@68: outstream.println("Valid Files: \t"+filesValid); jpayne@68: outstream.println("Invalid Files: \t"+(filesProcessed-filesValid)); jpayne@68: outstream.println("Valid Lines: \t"+linesValid); jpayne@68: outstream.println("Invalid Lines: \t"+(linesProcessed-linesValid)); jpayne@68: jpayne@68: if(errorState){ jpayne@68: throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); jpayne@68: } jpayne@68: } jpayne@68: jpayne@68: void process_inner(final FileFormat ffin, final ByteStreamWriter bsw, final long img){ jpayne@68: jpayne@68: filesProcessed++; jpayne@68: { jpayne@68: File f=new File(ffin.name()); jpayne@68: if(!f.exists() || !f.canRead()){ jpayne@68: System.err.println("Can't find "+f); jpayne@68: errorState=true; jpayne@68: return; jpayne@68: } jpayne@68: } jpayne@68: final int tid=TaxTree.imgToTaxid(img); jpayne@68: ByteFile bf=ByteFile.makeByteFile(ffin); jpayne@68: jpayne@68: byte[] line=bf.nextLine(); jpayne@68: ByteBuilder bb=new ByteBuilder(); jpayne@68: jpayne@68: while(line!=null){ jpayne@68: if(line.length>0){ jpayne@68: if(maxLines>0 && linesProcessed>=maxLines){break;} jpayne@68: linesProcessed++; jpayne@68: bytesProcessed+=line.length; jpayne@68: jpayne@68: linesValid++; jpayne@68: if(line[0]=='>'){ jpayne@68: sequencesProcessed++; jpayne@68: bb.append('>'); jpayne@68: if(tid>=0){ jpayne@68: bb.append("tid|"); jpayne@68: bb.append(tid); jpayne@68: bb.append('|'); jpayne@68: } jpayne@68: bb.append("img|"); jpayne@68: bb.append(img); jpayne@68: bb.append(' '); jpayne@68: for(int i=1; i