Debian Codegraph @

current/driver/FilterAssemblySummary.java

raw symbols SCIP
1 package driver;
2
3 import java.io.File;
4 import java.io.PrintStream;
5
6 import fileIO.FileFormat;
7 import fileIO.ReadWrite;
8 import fileIO.TextFile;
9 import fileIO.TextStreamWriter;
10 import shared.Parse;
11 import shared.Parser;
12 import shared.PreParser;
13 import shared.Shared;
14 import shared.Timer;
15 import shared.Tools;
16 import tax.TaxFilter;
17
18 /**
19 * @author Brian Bushnell
20 * @date May 1, 2016
21 *
22 */
23 public class FilterAssemblySummary {
24
25 public static void main(String[] args){
26 Timer t=new Timer();
27 FilterAssemblySummary x=new FilterAssemblySummary(args);
28 x.process(t);
29
30 //Close the print stream if it was redirected
31 Shared.closeStream(x.outstream);
32 }
33
34 public FilterAssemblySummary(String[] args){
35
36 {//Preparse block for help, config files, and outstream
37 PreParser pp=new PreParser(args, getClass(), false);
38 args=pp.args;
39 outstream=pp.outstream;
40 }
41
42 Shared.capBuffers(4);
43 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
44 ReadWrite.setZipThreads(Shared.threads());
45
46 Parser parser=new Parser();
47 for(int i=0; i<args.length; i++){
48 String arg=args[i];
49 String[] split=arg.split("=");
50 String a=split[0].toLowerCase();
51 String b=split.length>1 ? split[1] : null;
52
53 if(parser.parse(arg, a, b)){
54 //do nothing
55 }else if(a.equals("verbose")){
56 verbose=Parse.parseBoolean(b);
57 ReadWrite.verbose=verbose;
58 }else if(parser.in1==null && i==0 && Tools.looksLikeInputStream(arg)){
59 parser.in1=arg;
60 }else if(TaxFilter.validArgument(a)){
61 //do nothing
62 }else{
63 outstream.println("Unknown parameter "+args[i]);
64 assert(false) : "Unknown parameter "+args[i];
65 // throw new RuntimeException("Unknown parameter "+args[i]);
66 }
67 }
68
69 {//Process parser fields
70 Parser.processQuality();
71
72 maxReads=parser.maxReads;
73
74 overwrite=parser.overwrite;
75 append=parser.append;
76
77 in1=parser.in1;
78
79 out1=parser.out1;
80 }
81
82 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
83
84 if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;}
85
86 if(!Tools.testOutputFiles(overwrite, append, false, out1)){
87 outstream.println((out1==null)+", "+out1);
88 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n");
89 }
90
91 ffout1=FileFormat.testOutput(out1, FileFormat.TEXT, null, true, overwrite, append, false);
92
93 ffin1=FileFormat.testInput(in1, FileFormat.TEXT, null, true, true);
94
95 //Make the actual filter
96 filter=TaxFilter.makeFilter(args);
97 }
98
99 void process(Timer t){
100
101 final TextFile tf;
102 {
103 tf=new TextFile(ffin1);
104 if(verbose){outstream.println("Started tf");}
105 }
106
107 final TextStreamWriter tsw;
108 {
109 tsw=new TextStreamWriter(ffout1);
110 tsw.start();
111 if(verbose){outstream.println("Started tsw");}
112 }
113
114 long linesProcessed=0;
115 long linesRetained=0;
116 long charsProcessed=0;
117
118 {
119 String line;
120 while((maxReads<0 || linesProcessed<maxReads) && (line=tf.nextLine())!=null){
121 linesProcessed++;
122 charsProcessed+=line.length();
123 String result=processLine(line);
124 if(result!=null){
125 linesRetained++;
126 if(tsw!=null){tsw.println(result);}
127 }
128 }
129 }
130
131 errorState|=tsw.poisonAndWait();
132 errorState|=tf.close();
133
134 t.stop();
135
136 String kpstring=(linesRetained<100000 ? ""+linesRetained : linesRetained<100000000 ? (linesRetained/1000)+"k" : (linesRetained/1000000)+"m");
137 while(kpstring.length()<8){kpstring=" "+kpstring;}
138 outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, charsProcessed, 8));
139 outstream.println("Lines Retained: "+kpstring);
140
141
142
143 if(errorState){
144 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
145 }
146 }
147
148
149 private String processLine(String line){
150 // System.out.println("Processing line "+line);
151 if(line.startsWith("#")){return null;}
152 String[] split=line.split("\t");
153 assert(split.length>6) : split.length+"\n"+"'"+line+"'";
154 String id=split[6];
155 int number=Integer.parseInt(id);
156 // System.out.println("Found number "+number+" from "+id+"; node: "+filter.tree().getNode(number));
157 boolean b=filter.passesFilter((int)number);
158 // System.out.println("passesFilter? "+b);
159 return b ? line : null;
160 }
161
162
163 /*--------------------------------------------------------------*/
164
165 /*--------------------------------------------------------------*/
166
167 private String in1=null;
168 private String out1=null;
169
170 /*--------------------------------------------------------------*/
171
172 private long maxReads=-1;
173
174 /*--------------------------------------------------------------*/
175
176 private final FileFormat ffin1;
177 private final FileFormat ffout1;
178
179 /** The actual filter */
180 private final TaxFilter filter;
181
182
183 /*--------------------------------------------------------------*/
184
185 private PrintStream outstream=System.err;
186 public static boolean verbose=false;
187 public boolean errorState=false;
188 private boolean overwrite=true;
189 private boolean append=false;
190
191 }