jpayne@68: package prok; 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.Data; 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 structures.ByteBuilder; jpayne@68: jpayne@68: /** jpayne@68: * Static helpers for manipulating pgm files. jpayne@68: * main() merges pgm files. jpayne@68: * @author Brian Bushnell jpayne@68: * @date Sep 24, 2018 jpayne@68: * jpayne@68: */ jpayne@68: public class PGMTools extends ProkObject { jpayne@68: jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: /*---------------- Main ----------------*/ jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: jpayne@68: /** Combines multiple pgm files into a single file */ jpayne@68: public static void main(String[] args){ jpayne@68: jpayne@68: //Start a timer immediately upon code entrance. jpayne@68: Timer t=new Timer(); jpayne@68: jpayne@68: {//Preparse block for help, config files, and outstream jpayne@68: PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); jpayne@68: args=pp.args; jpayne@68: outstream=pp.outstream; jpayne@68: } jpayne@68: jpayne@68: ProkObject.call18S=true; jpayne@68: boolean overwrite=true; jpayne@68: boolean allowDupes=false; jpayne@68: String out=null; jpayne@68: ArrayList in=new ArrayList(); jpayne@68: jpayne@68: { jpayne@68: Parser parser=new Parser(); jpayne@68: for(int i=0; i1 ? split[1] : null; jpayne@68: if(b!=null && b.equalsIgnoreCase("null")){b=null;} jpayne@68: jpayne@68: if(a.equals("in")){ jpayne@68: assert(b!=null); jpayne@68: Tools.addFiles(b, in); jpayne@68: }else if(parseStatic(arg, a, b)){ jpayne@68: //do nothing jpayne@68: }else if(a.equals("allowdupes") || a.equals("allowduplicates") || a.equals("dupes")){ jpayne@68: allowDupes=Parse.parseBoolean(b); jpayne@68: }else if(a.equals("verbose")){ jpayne@68: verbose=Parse.parseBoolean(b); jpayne@68: ReadWrite.verbose=verbose; jpayne@68: } jpayne@68: jpayne@68: else if(parser.parse(arg, a, b)){ jpayne@68: //do nothing jpayne@68: }else if(b==null && new File(arg.split("@")[0]).exists()){ jpayne@68: in.add(arg); 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: overwrite=parser.overwrite; jpayne@68: out=parser.out1; jpayne@68: } jpayne@68: jpayne@68: checkFileExistence(in, out, overwrite, allowDupes); jpayne@68: jpayne@68: ArrayList models=loadModels(in); jpayne@68: GeneModel gm=mergeModels(models); jpayne@68: boolean errorState=writeModel(gm, out, overwrite); jpayne@68: jpayne@68: //Close the print stream if it was redirected jpayne@68: Shared.closeStream(outstream); jpayne@68: } jpayne@68: jpayne@68: public static boolean parseStatic(String arg, String a, String b){ jpayne@68: if(a.equals("kinnercds")){ jpayne@68: int k=Integer.parseInt(b); jpayne@68: GeneModel.setInnerK(k); jpayne@68: }else if(a.equals("kstartcds")){ jpayne@68: int k=Integer.parseInt(b); jpayne@68: GeneModel.setStartK(k); jpayne@68: }else if(a.equals("kstopcds")){ jpayne@68: int k=Integer.parseInt(b); jpayne@68: GeneModel.setStopK(k); jpayne@68: }else if(a.equals("kinnerrna")){ jpayne@68: int k=Integer.parseInt(b); jpayne@68: kInnerRNA=k; jpayne@68: }else if(a.equals("kstartrna")){ jpayne@68: int k=Integer.parseInt(b); jpayne@68: kStartRNA=k; jpayne@68: }else if(a.equals("kstoprna")){ jpayne@68: int k=Integer.parseInt(b); jpayne@68: kStopRNA=k; jpayne@68: }else if(a.equals("startleftoffset")){ jpayne@68: int x=Integer.parseInt(b); jpayne@68: GeneModel.setStartLeftOffset(x); jpayne@68: }else if(a.equals("startrightoffset")){ jpayne@68: int x=Integer.parseInt(b); jpayne@68: GeneModel.setStartRightOffset(x); jpayne@68: }else if(a.equals("stopleftoffset")){ jpayne@68: int x=Integer.parseInt(b); jpayne@68: GeneModel.setStopLeftOffset(x); jpayne@68: }else if(a.equals("stoprightoffset")){ jpayne@68: int x=Integer.parseInt(b); jpayne@68: GeneModel.setStopRightOffset(x); jpayne@68: }else if(a.equalsIgnoreCase("callcdsonly") || a.equalsIgnoreCase("cdsonly")){ jpayne@68: callCDS=Parse.parseBoolean(b); jpayne@68: calltRNA=call16S=call23S=call5S=call18S=!callCDS; jpayne@68: }else if(a.equalsIgnoreCase("call16sonly") || a.equalsIgnoreCase("16sonly")){ jpayne@68: call16S=Parse.parseBoolean(b); jpayne@68: calltRNA=call23S=call5S=call18S=callCDS=!call16S; jpayne@68: }else if(a.equalsIgnoreCase("call23sonly") || a.equalsIgnoreCase("23sonly")){ jpayne@68: call23S=Parse.parseBoolean(b); jpayne@68: calltRNA=call16S=call5S=call18S=callCDS=!call23S; jpayne@68: }else if(a.equalsIgnoreCase("call5sonly") || a.equalsIgnoreCase("5sonly")){ jpayne@68: call5S=Parse.parseBoolean(b); jpayne@68: calltRNA=call16S=call23S=call18S=callCDS=!call5S; jpayne@68: }else if(a.equalsIgnoreCase("calltrnaonly") || a.equalsIgnoreCase("trnaonly")){ jpayne@68: calltRNA=Parse.parseBoolean(b); jpayne@68: call16S=call23S=call5S=call18S=callCDS=!calltRNA; jpayne@68: }else if(a.equalsIgnoreCase("call18sonly") || a.equalsIgnoreCase("18sonly")){ jpayne@68: call18S=Parse.parseBoolean(b); jpayne@68: calltRNA=call16S=call23S=call5S=callCDS=!call18S; jpayne@68: } jpayne@68: jpayne@68: else if(a.equalsIgnoreCase("callcds") || a.equalsIgnoreCase("cds")){ jpayne@68: callCDS=Parse.parseBoolean(b); jpayne@68: }else if(a.equalsIgnoreCase("calltrna") || a.equalsIgnoreCase("trna")){ jpayne@68: calltRNA=Parse.parseBoolean(b); jpayne@68: }else if(a.equalsIgnoreCase("call16s") || a.equalsIgnoreCase("16s")){ jpayne@68: call16S=Parse.parseBoolean(b); jpayne@68: }else if(a.equalsIgnoreCase("call18s") || a.equalsIgnoreCase("18s")){ jpayne@68: call18S=Parse.parseBoolean(b); jpayne@68: }else if(a.equalsIgnoreCase("call23s") || a.equalsIgnoreCase("23s")){ jpayne@68: call23S=Parse.parseBoolean(b); jpayne@68: }else if(a.equalsIgnoreCase("call5s") || a.equalsIgnoreCase("5s")){ jpayne@68: call5S=Parse.parseBoolean(b); jpayne@68: }else if(a.equalsIgnoreCase("callrna") || a.equalsIgnoreCase("rna")){ jpayne@68: calltRNA=call16S=call18S=call5S=call23S=Parse.parseBoolean(b); jpayne@68: } jpayne@68: jpayne@68: else if(a.equalsIgnoreCase("normalize")){ jpayne@68: normalize=Parse.parseBoolean(b); jpayne@68: } jpayne@68: jpayne@68: else{ jpayne@68: return false; jpayne@68: } jpayne@68: return true; jpayne@68: } jpayne@68: jpayne@68: public static ArrayList loadModels(ArrayList fnames){ jpayne@68: ArrayList models=new ArrayList(fnames.size()); jpayne@68: ArrayList mults=new ArrayList(fnames.size()); jpayne@68: for(String s : fnames){ jpayne@68: double mult=1; jpayne@68: String fname=s; jpayne@68: if(s.indexOf('@')>=0){ jpayne@68: String[] split=s.split("@"); jpayne@68: fname=split[0]; jpayne@68: mult=Double.parseDouble(split[1]); jpayne@68: } jpayne@68: GeneModel pgm=GeneModelParser.loadModel(fname); jpayne@68: mults.add(mult); jpayne@68: models.add(pgm); jpayne@68: } jpayne@68: if(normalize){ jpayne@68: long max=0; jpayne@68: for(GeneModel gm : models){ jpayne@68: max=Tools.max(gm.basesProcessed, max); jpayne@68: } jpayne@68: for(GeneModel gm : models){ jpayne@68: if(max!=gm.basesProcessed){ jpayne@68: double mult=max/(double)(Tools.max(100, gm.basesProcessed)); jpayne@68: gm.multiplyBy(mult); jpayne@68: } jpayne@68: } jpayne@68: } jpayne@68: for(int i=0; i models){ jpayne@68: if(models.size()==1){return models.get(0);} jpayne@68: GeneModel pgmSum=new GeneModel(true); jpayne@68: for(GeneModel pgm : models){ jpayne@68: pgmSum.add(pgm); jpayne@68: } jpayne@68: return pgmSum; jpayne@68: } jpayne@68: jpayne@68: public static GeneModel loadAndMerge(ArrayList in) { jpayne@68: ArrayList models=loadModels(in); jpayne@68: return mergeModels(models); jpayne@68: } jpayne@68: jpayne@68: public static boolean writeModel(GeneModel pgm, String out, boolean overwrite){ jpayne@68: FileFormat ffout=FileFormat.testOutput(out, FileFormat.PGM, null, true, overwrite, false, false); jpayne@68: return writeModel(pgm, ffout); jpayne@68: } jpayne@68: jpayne@68: public static boolean writeModel(GeneModel pgm, FileFormat ffout){ jpayne@68: ByteStreamWriter bsw=ByteStreamWriter.makeBSW(ffout); jpayne@68: jpayne@68: ByteBuilder bb=new ByteBuilder(); jpayne@68: pgm.appendTo(bb); jpayne@68: jpayne@68: boolean errorState=false; jpayne@68: if(bsw!=null){ jpayne@68: bsw.addJob(bb); jpayne@68: errorState|=bsw.poisonAndWait(); jpayne@68: } jpayne@68: return errorState; jpayne@68: } jpayne@68: jpayne@68: /** Ensure files can be read and written */ jpayne@68: private static void checkFileExistence(ArrayList in, String out, boolean overwrite, boolean allowDupes){ jpayne@68: //Ensure output files can be written jpayne@68: if(!Tools.testOutputFiles(overwrite, false, false, out)){ jpayne@68: outstream.println((out==null)+", "+out); jpayne@68: throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out+"\n"); jpayne@68: } jpayne@68: ArrayList in2=new ArrayList(); jpayne@68: for(String s : in){ jpayne@68: in2.add(s.split("@")[0]); jpayne@68: } jpayne@68: in=null; jpayne@68: jpayne@68: for(int i=0; i foo=new ArrayList(); jpayne@68: foo.addAll(in2); jpayne@68: if(!Tools.testInputFiles(allowDupes, true, foo.toArray(new String[0]))){ jpayne@68: throw new RuntimeException("\nCan't read some input files.\n"); jpayne@68: } jpayne@68: jpayne@68: //Ensure that no file was specified multiple times jpayne@68: if(!allowDupes){ jpayne@68: foo.add(out); jpayne@68: if(!Tools.testForDuplicateFiles(true, foo.toArray(new String[0]))){ jpayne@68: throw new RuntimeException("\nSome file names were specified multiple times.\n"); jpayne@68: } jpayne@68: } jpayne@68: } jpayne@68: jpayne@68: /*--------------------------------------------------------------*/ jpayne@68: jpayne@68: private static PrintStream outstream=System.err; jpayne@68: public static boolean verbose=false; jpayne@68: /** Mix models equally */ jpayne@68: public static boolean normalize=false; jpayne@68: jpayne@68: }