1 package hmm; 2 3 import java.io.PrintStream; 4 import java.util.ArrayList; 5 import java.util.HashMap; 6 7 import fileIO.ByteFile; 8 import fileIO.ByteFile1; 9 import fileIO.ByteFile2; 10 import fileIO.ByteStreamWriter; 11 import fileIO.FileFormat; 12 import fileIO.ReadWrite; 13 import shared.Parse; 14 import shared.Parser; 15 import shared.PreParser; 16 import shared.Shared; 17 import shared.Timer; 18 import shared.Tools; 19 20 /** 21 * Loads output of HMMSearch. 22 * @author Brian Bushnell 23 * @date April 9, 2020 24 * 25 */ 26 public class HMMSearchReport { 27 28 /*--------------------------------------------------------------*/ 29 /*---------------- Initialization ----------------*/ 30 /*--------------------------------------------------------------*/ 31 32 /** 33 * Code entrance from the command line. 34 * @param args Command line arguments 35 */ main(String[] args)36 public static void main(String[] args){ 37 //Start a timer immediately upon code entrance. 38 Timer t=new Timer(); 39 40 //Create an instance of this class 41 HMMSearchReport x=new HMMSearchReport(args); 42 43 //Run the object 44 x.process(t); 45 46 //Close the print stream if it was redirected 47 Shared.closeStream(x.outstream); 48 } 49 50 /** 51 * Constructor. 52 * @param args Command line arguments 53 */ HMMSearchReport(String[] args)54 public HMMSearchReport(String[] args){ 55 56 {//Preparse block for help, config files, and outstream 57 PreParser pp=new PreParser(args, /*getClass()*/null, false); 58 args=pp.args; 59 outstream=pp.outstream; 60 } 61 62 //Set shared static variables prior to parsing 63 64 {//Parse the arguments 65 final Parser parser=parse(args); 66 parser.out1="stdout.txt"; 67 overwrite=parser.overwrite; 68 append=parser.append; 69 70 in=parser.in1; 71 72 // out=parser.out1; 73 } 74 75 fixExtensions(); //Add or remove .gz or .bz2 as needed 76 checkFileExistence(); //Ensure files can be read and written 77 checkStatics(); //Adjust file-related static fields as needed for this program 78 79 // ffout=FileFormat.testOutput(out, FileFormat.TXT, null, true, overwrite, append, false); 80 ffin=FileFormat.testInput(in, FileFormat.TXT, null, true, true); 81 } 82 83 /*--------------------------------------------------------------*/ 84 /*---------------- Initialization Helpers ----------------*/ 85 /*--------------------------------------------------------------*/ 86 87 /** Parse arguments from the command line */ parse(String[] args)88 private Parser parse(String[] args){ 89 90 Parser parser=new Parser(); 91 for(int i=0; i<args.length; i++){ 92 String arg=args[i]; 93 String[] split=arg.split("="); 94 String a=split[0].toLowerCase(); 95 String b=split.length>1 ? split[1] : null; 96 if(b!=null && b.equalsIgnoreCase("null")){b=null;} 97 98 if(a.equals("verbose")){ 99 verbose=Parse.parseBoolean(b); 100 ByteFile1.verbose=verbose; 101 ByteFile2.verbose=verbose; 102 ReadWrite.verbose=verbose; 103 }else if(parser.parse(arg, a, b)){ 104 //do nothing 105 }else{ 106 outstream.println("Unknown parameter "+args[i]); 107 assert(false) : "Unknown parameter "+args[i]; 108 // throw new RuntimeException("Unknown parameter "+args[i]); 109 } 110 } 111 112 return parser; 113 } 114 115 /** Add or remove .gz or .bz2 as needed */ fixExtensions()116 private void fixExtensions(){ 117 in=Tools.fixExtension(in); 118 if(in==null){throw new RuntimeException("Error - at least one input file is required.");} 119 } 120 121 /** Ensure files can be read and written */ checkFileExistence()122 private void checkFileExistence(){ 123 //Ensure output files can be written 124 // if(!Tools.testOutputFiles(overwrite, append, false, out)){ 125 // outstream.println((out==null)+", "+out); 126 // throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out+"\n"); 127 // } 128 129 //Ensure input files can be read 130 if(!Tools.testInputFiles(false, true, in)){ 131 throw new RuntimeException("\nCan't read some input files.\n"); 132 } 133 134 // //Ensure that no file was specified multiple times 135 // if(!Tools.testForDuplicateFiles(true, in, out)){ 136 // throw new RuntimeException("\nSome file names were specified multiple times.\n"); 137 // } 138 } 139 140 /** Adjust file-related static fields as needed for this program */ checkStatics()141 private static void checkStatics(){ 142 //Adjust the number of threads for input file reading 143 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ 144 ByteFile.FORCE_MODE_BF2=true; 145 } 146 } 147 148 /*--------------------------------------------------------------*/ 149 /*---------------- Outer Methods ----------------*/ 150 /*--------------------------------------------------------------*/ 151 process(Timer t)152 void process(Timer t){ 153 154 ByteFile bf=ByteFile.makeByteFile(ffin); 155 ByteStreamWriter bsw=null;//makeBSW(ffout); 156 157 // assert(false) : "Header goes here."; 158 if(bsw!=null){ 159 // assert(false) : "Header goes here."; 160 } 161 162 processInner(bf, bsw); 163 164 errorState|=bf.close(); 165 if(bsw!=null){errorState|=bsw.poisonAndWait();} 166 167 t.stop(); 168 169 outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8)); 170 171 // outstream.println(); 172 // outstream.println("Valid Lines: \t"+linesOut); 173 // outstream.println("Invalid Lines: \t"+(linesProcessed-linesOut)); 174 175 if(errorState){ 176 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); 177 } 178 } 179 180 /*--------------------------------------------------------------*/ 181 /*---------------- Inner Methods ----------------*/ 182 /*--------------------------------------------------------------*/ 183 processInner(ByteFile bf, ByteStreamWriter bsw)184 private void processInner(ByteFile bf, ByteStreamWriter bsw){ 185 ArrayList<HMMSearchLine> lines=load(bf); 186 for(HMMSearchLine line : lines){ 187 addToMap(line); 188 System.err.println(line); 189 } 190 } 191 addToMap(HMMSearchLine line)192 private void addToMap(HMMSearchLine line){ 193 ProteinSummary ps=map.get(line.name); 194 if(ps==null){ 195 ps=new ProteinSummary(line.name); 196 map.put(line.name, ps); 197 } 198 ps.add(line); 199 } 200 load(ByteFile bf)201 private ArrayList<HMMSearchLine> load(ByteFile bf){ 202 byte[] line=bf.nextLine(); 203 204 ArrayList<HMMSearchLine> lines=new ArrayList<HMMSearchLine>(); 205 while(line!=null){ 206 if(line.length>0){ 207 linesProcessed++; 208 bytesProcessed+=(line.length+1); 209 210 if(line[0]!='#'){ 211 HMMSearchLine hline=new HMMSearchLine(line); 212 lines.add(hline); 213 } 214 } 215 line=bf.nextLine(); 216 } 217 return lines; 218 } 219 makeBSW(FileFormat ff)220 private static ByteStreamWriter makeBSW(FileFormat ff){ 221 if(ff==null){return null;} 222 ByteStreamWriter bsw=new ByteStreamWriter(ff); 223 bsw.start(); 224 return bsw; 225 } 226 227 /*--------------------------------------------------------------*/ 228 /*---------------- Fields ----------------*/ 229 /*--------------------------------------------------------------*/ 230 231 private String in=null; 232 // private String out=null; 233 234 public HashMap<String, ProteinSummary> map=new HashMap<String, ProteinSummary>(); 235 236 /*--------------------------------------------------------------*/ 237 238 private long linesProcessed=0; 239 private long bytesProcessed=0; 240 // private long linesOut=0; 241 // private long bytesOut=0; 242 243 /*--------------------------------------------------------------*/ 244 /*---------------- Final Fields ----------------*/ 245 /*--------------------------------------------------------------*/ 246 247 private final FileFormat ffin; 248 // private final FileFormat ffout; 249 250 /*--------------------------------------------------------------*/ 251 /*---------------- Common Fields ----------------*/ 252 /*--------------------------------------------------------------*/ 253 254 private PrintStream outstream=System.err; 255 public static boolean verbose=false; 256 public boolean errorState=false; 257 private boolean overwrite=false; 258 private boolean append=false; 259 260 } 261