package sketch; import java.io.PrintStream; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Map.Entry; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; import fileIO.ByteFile; import fileIO.ByteStreamWriter; 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.FASTQ; import stream.FastaReadInputStream; import stream.Read; import structures.ByteBuilder; import structures.FloatList; import tax.TaxNode; import tax.TaxTree; import template.Accumulator; import template.ThreadWaiter; import tracker.ReadStats; /** * Compares SSUs, all-to-all or fractional matrix. * * @author Brian Bushnell * @date December 2, 2019 * */ public class CompareSSU 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 CompareSSU x=new CompareSSU(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 CompareSSU(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()); {//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; out1=parser.out1; } validateParams(); if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; checkFileExistence(); //Ensure files can be read and written checkStatics(); //Adjust file-related static fields as needed for this program //Create output FileFormat objects ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, overwrite, append, ordered); tree=(treeFile==null) ? null : TaxTree.loadTaxTree(treeFile, outstream, true, false); SSUMap.r16SFile=in1; if(SSUMap.r16SFile!=null){ SSUMap.load(outstream); HashMap ssuMap=SSUMap.r16SMap; ssuList=new ArrayList(ssuMap.size()); for(Entry e : ssuMap.entrySet()){ int id=e.getKey(); byte[] value=e.getValue(); if(value.length>=minlen && value.length<=maxlen){ Read r=new Read(value, null, id);//Sets numeric ID to TaxID. if(maxns<0 || r.countNocalls()<=maxns){ ssuList.add(r); } } } Collections.shuffle(ssuList); } 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("tree")){ treeFile=b; }else if(a.equals("ordered")){ ordered=Parse.parseBoolean(b); }else if(a.equals("ata") || a.equals("alltoall")){ allToAll=Parse.parseBoolean(b); }else if(a.equals("store") || a.equals("storeresults")){ storeResults=Parse.parseBoolean(b); }else if(a.equals("minlen") || a.equals("maxlength")){ minlen=Parse.parseIntKMG(b); }else if(a.equals("maxlen") || a.equals("maxlength")){ maxlen=Parse.parseIntKMG(b); }else if(a.equalsIgnoreCase("maxns")){ maxns=Parse.parseIntKMG(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; } /** Ensure files can be read and written */ private void checkFileExistence(){ //Ensure output files can be written if(!Tools.testOutputFiles(overwrite, append, false, out1)){ outstream.println((out1==null)+", "+out1); throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out1+"\n"); } //Ensure input files can be read if(!Tools.testInputFiles(false, true, in1)){ throw new RuntimeException("\nCan't read some input files.\n"); } //Ensure that no file was specified multiple times if(!Tools.testForDuplicateFiles(true, in1, out1)){ throw new RuntimeException("\nSome file names were specified multiple times.\n"); } } /** Adjust file-related static fields as needed for this program */ private static void checkStatics(){ //Adjust the number of threads for input file reading if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ ByteFile.FORCE_MODE_BF2=true; } assert(FastaReadInputStream.settingsOK()); } /** Ensure parameter ranges are within bounds and required parameters are set */ private boolean validateParams(){ return true; } /*--------------------------------------------------------------*/ /*---------------- Outer Methods ----------------*/ /*--------------------------------------------------------------*/ /** Create read streams and process all data */ void process(Timer t){ ByteStreamWriter bsw=makeBSW(ffout1); if(bsw!=null){ bsw.forcePrint("#Level\tIdentity\tQueryID\tRefID\n"); } //Reset counters queriesProcessed=0; comparisons=0; //Process the reads in separate threads spawnThreads(bsw); if(verbose){outstream.println("Finished; closing streams.");} //Close the read streams if(bsw!=null){errorState|=bsw.poisonAndWait();} { ByteBuilder bb=new ByteBuilder(); bb.append("\nLevel \tCount\tMean"+(storeResults ? "\tMedian\t90%ile\t10%ile\tSTDev" : "")+"\n"); outstream.print(bb); final int minlen="superkingdom".length(); for(int level=0; level0){ bb.clear(); bb.append(TaxTree.levelToStringExtended(level)); while(bb.length() alpt=new ArrayList(threads); for(int i=0; i(ssuList.size()); listCopy.addAll(ssuList); for(int i=0; i0 ? maxReads : Integer.MAX_VALUE)); for(int num=next.getAndIncrement(); num0 && tree.getNode(rid)!=null){ int aid=tree.commonAncestor(qid, rid); if(aid>0){ TaxNode tn=tree.getNode(aid); if(tn.isRanked()){ int level=tn.levelExtended; long mask=1L< listCopy; final FloatList[] idListsT=new FloatList[taxLevels]; long[] countsT=new long[taxLevels]; double[] sumsT=new double[taxLevels]; } /*--------------------------------------------------------------*/ /*---------------- Fields ----------------*/ /*--------------------------------------------------------------*/ /** Primary input file path */ private String in1=null; private String treeFile="auto"; /** Primary output file path */ private String out1=null; public static ArrayList ssuList=null; final static int taxLevels=TaxTree.numTaxLevelNamesExtended; static final String[] printLevelsArray=new String[] {"strain", "species", "genus", "family", "order", "class", "phylum", "superkingdom", "life"}; static final long printLevels=makePrintLevels(printLevelsArray); private final TaxTree tree; private static final long makePrintLevels(String[] names){ long mask=0; for(String s : names){ int level=TaxTree.stringToLevelExtended(s); mask|=(1L<