jpayne@68: package sketch; jpayne@68: jpayne@68: import java.io.File; jpayne@68: import java.io.PrintStream; jpayne@68: import java.util.ArrayList; jpayne@68: jpayne@68: import dna.AminoAcid; 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.ReadStats; jpayne@68: import shared.Shared; jpayne@68: import shared.Timer; jpayne@68: import shared.Tools; jpayne@68: import stream.ConcurrentGenericReadInputStream; jpayne@68: import stream.ConcurrentReadInputStream; jpayne@68: import stream.FASTQ; jpayne@68: import stream.FastaReadInputStream; jpayne@68: import stream.Read; jpayne@68: import structures.ListNum; jpayne@68: import structures.LongHashSet; jpayne@68: jpayne@68: /** jpayne@68: * @author Brian Bushnell jpayne@68: * @date Oct 17, 2014 jpayne@68: * jpayne@68: */ jpayne@68: public class InvertKey extends SketchObject { jpayne@68: jpayne@68: public static void main(String[] args){ jpayne@68: Timer t=new Timer(); jpayne@68: InvertKey x=new InvertKey(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 InvertKey(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: Shared.capBuffers(4); jpayne@68: ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; jpayne@68: ReadWrite.MAX_ZIP_THREADS=Shared.threads(); jpayne@68: jpayne@68: FASTQ.TEST_INTERLEAVED=FASTQ.FORCE_INTERLEAVED=false; jpayne@68: int k_=32, k2_=0; 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("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("key")){ jpayne@68: keyString=b; jpayne@68: }else if(a.equals("out")){ jpayne@68: out1=b; jpayne@68: }else if(a.equalsIgnoreCase("k")){ jpayne@68: assert(b!=null) : "Bad parameter: "+arg; jpayne@68: if(b.indexOf(',')>=0){ jpayne@68: String[] bsplit=b.split(","); jpayne@68: assert(bsplit.length==2) : "Bad argument "+arg; jpayne@68: int x=Integer.parseInt(bsplit[0]); jpayne@68: int y=Integer.parseInt(bsplit[1]); jpayne@68: k_=Tools.max(x, y); jpayne@68: k2_=Tools.min(x, y); jpayne@68: if(k_==k2_){k2_=0;} jpayne@68: }else{ jpayne@68: k_=Integer.parseInt(b); jpayne@68: k2_=0; jpayne@68: } jpayne@68: }else if(a.equalsIgnoreCase("printonce")){ jpayne@68: printOnce=Parse.parseBoolean(b); jpayne@68: }else if(parser.in1==null && i==0 && !arg.contains("=") && (arg.toLowerCase().startsWith("stdin") || new File(arg).exists())){ jpayne@68: parser.in1=arg; jpayne@68: }else if(parser.out1==null && i==1 && !arg.contains("=")){ jpayne@68: out1=arg; 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: k=k_; jpayne@68: k2=k2_; jpayne@68: shift=2*k; jpayne@68: shift2=shift-2; jpayne@68: mask=(shift>63 ? -1L : ~((-1L)<0){ jpayne@68: String[] split=keyString.split(","); jpayne@68: set=new LongHashSet(split.length*2); jpayne@68: for(String s : split){ jpayne@68: long x=Long.MAX_VALUE-Sketch.parseA48(s); jpayne@68: set.add(x); jpayne@68: // assert(set.contains(x)) : x+", "+set.size()+", "+set.toStringListView(); jpayne@68: } jpayne@68: key0=-1; jpayne@68: // System.err.println(set.toStringListView()+", "+set.size()); jpayne@68: assert(!set.isEmpty()); jpayne@68: }else if(keyString.endsWith(".sketch")){ jpayne@68: SketchTool tool=new SketchTool(10000, 0, false, false); jpayne@68: Sketch sk=tool.loadSketchesFromFile(keyString, null, 0, 1000000, SketchObject.ONE_SKETCH, 1f, 0f, 0f, (byte)0, false).get(0); jpayne@68: set=new LongHashSet(sk.length()*2); jpayne@68: for(long x : sk.keys){set.add(Long.MAX_VALUE-x);} jpayne@68: key0=-1; jpayne@68: // System.err.println(set.toStringListView()+", "+set.size()); jpayne@68: assert(!set.isEmpty()); jpayne@68: }else{ jpayne@68: key0=Long.MAX_VALUE-Sketch.parseA48(keyString); jpayne@68: set=null; jpayne@68: // System.err.println(key0); jpayne@68: } jpayne@68: } jpayne@68: jpayne@68: void process(Timer t){ jpayne@68: jpayne@68: final ConcurrentReadInputStream cris; jpayne@68: { jpayne@68: cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null, null, null); jpayne@68: cris.start(); jpayne@68: if(verbose){outstream.println("Started cris");} jpayne@68: } jpayne@68: boolean paired=cris.paired(); jpayne@68: // if(verbose){ jpayne@68: if(!ffin1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));} jpayne@68: // } jpayne@68: jpayne@68: final ByteStreamWriter bsw; jpayne@68: if(out1!=null){ jpayne@68: fasta=ffout1.fasta() && !out1.endsWith(".txt"); jpayne@68: bsw=new ByteStreamWriter(ffout1); jpayne@68: bsw.start(); jpayne@68: }else{bsw=null;} jpayne@68: jpayne@68: long readsProcessed=0; jpayne@68: long basesProcessed=0; jpayne@68: boolean finished=false; jpayne@68: jpayne@68: { jpayne@68: jpayne@68: ListNum ln=cris.nextList(); jpayne@68: ArrayList reads=(ln!=null ? ln.list : null); jpayne@68: jpayne@68: // outstream.println("Fetched "+reads); jpayne@68: jpayne@68: if(reads!=null && !reads.isEmpty()){ jpayne@68: Read r=reads.get(0); jpayne@68: assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); jpayne@68: } jpayne@68: jpayne@68: while(reads!=null && reads.size()>0 && !finished){ jpayne@68: jpayne@68: for(int idx=0; idx>>2)|(x2<=k){ jpayne@68: kmersProcessed++; jpayne@68: final long hashcode=hash(kmer, rkmer); jpayne@68: boolean found=(key0>=0 ? hashcode==key0 : set.contains(hashcode)); jpayne@68: if(found){ jpayne@68: if(fasta){bsw.println(">"+Sketch.toA48(Long.MAX_VALUE-hashcode)+" "+(i-k+1)+" "+r.id);} jpayne@68: bsw.println(AminoAcid.kmerToString(Tools.min(kmer, rkmer), k)); jpayne@68: if(printOnce){ jpayne@68: if(key0>=0){return true;} jpayne@68: else{ jpayne@68: set.remove(hashcode); jpayne@68: return set.isEmpty(); jpayne@68: } jpayne@68: } jpayne@68: } jpayne@68: } jpayne@68: } jpayne@68: return false; jpayne@68: } jpayne@68: jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: jpayne@68: final long key0; jpayne@68: final LongHashSet set; jpayne@68: jpayne@68: final int shift; jpayne@68: final int shift2; jpayne@68: final long mask; jpayne@68: jpayne@68: boolean printOnce=true; jpayne@68: long kmersProcessed=0; jpayne@68: jpayne@68: private String in1=null; jpayne@68: boolean fasta; jpayne@68: boolean sketch; jpayne@68: private String keyString=null; jpayne@68: jpayne@68: private String out1="stdout.fa"; jpayne@68: jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: jpayne@68: private long maxReads=-1; jpayne@68: jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: jpayne@68: private final FileFormat ffin1; jpayne@68: jpayne@68: private final FileFormat ffout1; jpayne@68: jpayne@68: jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: jpayne@68: private PrintStream outstream=System.err; jpayne@68: public static boolean verbose=false; jpayne@68: public boolean errorState=false; jpayne@68: private boolean overwrite=false; jpayne@68: private boolean append=false; jpayne@68: jpayne@68: }