package prok; import java.io.PrintStream; import java.util.ArrayList; import java.util.Arrays; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; import aligner.SingleStateAlignerFlat2; import fileIO.ByteFile; import fileIO.FileFormat; import fileIO.ReadWrite; import shared.Parse; import shared.Parser; import shared.PreParser; import shared.Shared; import shared.Timer; import shared.Tools; import stream.ConcurrentReadInputStream; import stream.ConcurrentReadOutputStream; import stream.FastaReadInputStream; import stream.Read; import structures.ListNum; import template.Accumulator; import template.ThreadWaiter; import tracker.ReadStats; /** * Splits a mix of ribosomal sequences (such as Silva) into different files per type (16S, 18S, etc). * * @author Brian Bushnell * @date November 19, 2015 * */ public class SplitRibo implements Accumulator { /*--------------------------------------------------------------*/ /*---------------- Initialization ----------------*/ /*--------------------------------------------------------------*/ /** * Code entrance from the command line. * @param args Command line arguments */ public static void main(String[] args){ //Start a timer immediately upon code entrance. Timer t=new Timer(); //Create an instance of this class SplitRibo x=new SplitRibo(args); //Run the object x.process(t); //Close the print stream if it was redirected Shared.closeStream(x.outstream); } /** * Constructor. * @param args Command line arguments */ public SplitRibo(String[] args){ {//Preparse block for help, config files, and outstream PreParser pp=new PreParser(args, getClass(), false); args=pp.args; outstream=pp.outstream; } //Set shared static variables prior to parsing ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; ReadWrite.setZipThreads(Shared.threads()); Shared.capBufferLen(50); ReadWrite.ZIPLEVEL=9; {//Parse the arguments final Parser parser=parse(args); Parser.processQuality(); maxReads=parser.maxReads; overwrite=ReadStats.overwrite=parser.overwrite; append=ReadStats.append=parser.append; in1=parser.in1; qfin1=parser.qfin1; extin=parser.extin; outPattern=parser.out1; extout=parser.extout; } validateParams(); fixExtensions(); //Add or remove .gz or .bz2 as needed checkFileExistence(); //Ensure files can be read and written checkStatics(); //Adjust file-related static fields as needed for this program //Create input FileFormat objects ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true); numTypes=sequenceTypes.length; readsOut=new long[numTypes]; basesOut=new long[numTypes]; consensusSequences=loadConsensusSequenceFromFile(); } /*--------------------------------------------------------------*/ /*---------------- Initialization Helpers ----------------*/ /*--------------------------------------------------------------*/ /** Parse arguments from the command line */ private Parser parse(String[] args){ //Create a parser object Parser parser=new Parser(); //Set any necessary Parser defaults here //parser.foo=bar; //Parse each argument for(int i=0; i1 ? split[1] : null; if(b!=null && b.equalsIgnoreCase("null")){b=null;} if(a.equals("verbose")){ verbose=Parse.parseBoolean(b); }else if(a.equals("ordered")){ ordered=Parse.parseBoolean(b); }else if(a.equalsIgnoreCase("minid")){ minID=Float.parseFloat(b); }else if(a.equalsIgnoreCase("minid2") || a.equalsIgnoreCase("refineid")){ refineID=Float.parseFloat(b); }else if(a.equals("out") || a.equals("pattern") || a.equals("outpattern")){ parser.out1=b; }else if(a.equals("type") || a.equals("types")){ parseTypes(b); }else if(a.equals("parse_flag_goes_here")){ long fake_variable=Parse.parseKMG(b); //Set a variable here }else if(parser.parse(arg, a, b)){//Parse standard flags in the parser //do nothing }else{ outstream.println("Unknown parameter "+args[i]); assert(false) : "Unknown parameter "+args[i]; } } return parser; } private void parseTypes(String b){ sequenceTypes=null; if(b==null){ assert(false) : "'types' flag requires a list of types, such as 'types=16S,18S'"; sequenceTypes=new String[] {"Other"}; }else{ String[] split=b.split(","); sequenceTypes=new String[split.length+1]; sequenceTypes[0]="Other"; for(int i=0; i2){ ByteFile.FORCE_MODE_BF2=true; } assert(FastaReadInputStream.settingsOK()); } /** Ensure parameter ranges are within bounds and required parameters are set */ private boolean validateParams(){ // assert(minfoo>0 && minfoo<=maxfoo) : minfoo+", "+maxfoo; if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} return true; } private final Read[][] loadConsensusSequenceFromFile(){ Read[][] seqs=new Read[numTypes][]; m16S_index=Tools.find("m16S", sequenceTypes); m18S_index=Tools.find("m18S", sequenceTypes); p16S_index=Tools.find("p16S", sequenceTypes); boolean stripM16S=(m16S_index>=0); boolean stripM18S=(m18S_index>=0); boolean stripP16S=(p16S_index>=0); for(int st=1; st alpt=new ArrayList(threads); for(int i=0; i ln=cris.nextList(); //Check to ensure pairing is as expected if(ln!=null && !ln.isEmpty()){ Read r=ln.get(0); assert(r.mate==null); } //As long as there is a nonempty read list... while(ln!=null && ln.size()>0){ // if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} //Disabled due to non-static access processList(ln); //Notify the input stream that the list was used cris.returnList(ln); // if(verbose){outstream.println("Returned a list.");} //Disabled due to non-static access //Fetch a new list ln=cris.nextList(); } //Notify the input stream that the final list was used if(ln!=null){ cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); } } void processList(ListNum ln){ //Grab the actual read list from the ListNum final ArrayList reads=ln.list; @SuppressWarnings("unchecked") final ArrayList[] out=new ArrayList[numTypes]; for(int i=0; i list=new ArrayList(50); out[i]=list; } //Loop through each read in the list for(int idx=0; idxbestID && id>=minID){ bestType=type; bestID=id; } } if(bestType<1 || bestIDbestID && id>=minID){ bestType=type; bestID=id; } } } r.obj=bestID;//If desired... in actuality, more info might be useful, like alignment length return bestID