Debian Codegraph @

current/prok/FilterSilva.java

raw symbols SCIP
1 package prok;
2
3 import java.io.File;
4 import java.io.PrintStream;
5 import java.util.ArrayList;
6
7 import fileIO.ByteFile;
8 import fileIO.ByteFile1;
9 import fileIO.ByteFile2;
10 import fileIO.FileFormat;
11 import fileIO.ReadWrite;
12 import shared.Parse;
13 import shared.Parser;
14 import shared.PreParser;
15 import shared.Shared;
16 import shared.Timer;
17 import shared.Tools;
18 import stream.ConcurrentGenericReadInputStream;
19 import stream.ConcurrentReadInputStream;
20 import stream.ConcurrentReadOutputStream;
21 import stream.FASTQ;
22 import stream.FastaReadInputStream;
23 import stream.Read;
24 import structures.ListNum;
25 import tax.TaxNode;
26 import tax.TaxTree;
27 import tracker.ReadStats;
28
29 /**
30 * Removes unwanted sequences from Silva, particularly bacteria flagged as euks due to name misidentification.
31 *
32 * @author Brian Bushnell
33 * @date January 27, 2020
34 *
35 */
36 public class FilterSilva {
37
38 public static void main(String[] args){
39 //Start a timer immediately upon code entrance.
40 Timer t=new Timer();
41
42 //Create an instance of this class
43 FilterSilva x=new FilterSilva(args);
44
45 //Run the object
46 x.process(t);
47
48 //Close the print stream if it was redirected
49 Shared.closeStream(x.outstream);
50 }
51
52 public FilterSilva(String[] args){
53
54 {//Preparse block for help, config files, and outstream
55 PreParser pp=new PreParser(args, getClass(), false);
56 args=pp.args;
57 outstream=pp.outstream;
58 }
59
60 //Set shared static variables
61 Shared.capBuffers(4); //Only for singlethreaded programs
62 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
63 ReadWrite.setZipThreads(Shared.threads());
64 FASTQ.TEST_INTERLEAVED=FASTQ.FORCE_INTERLEAVED=false;
65 TaxTree.SILVA_MODE=true;
66
67 Parser parser=new Parser();
68 for(int i=0; i<args.length; i++){
69 String arg=args[i];
70 String[] split=arg.split("=");
71 String a=split[0].toLowerCase();
72 String b=split.length>1 ? split[1] : null;
73 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
74
75 if(a.equals("verbose")){
76 verbose=Parse.parseBoolean(b);
77 ByteFile1.verbose=verbose;
78 ByteFile2.verbose=verbose;
79 stream.FastaReadInputStream.verbose=verbose;
80 ConcurrentGenericReadInputStream.verbose=verbose;
81 stream.FastqReadInputStream.verbose=verbose;
82 ReadWrite.verbose=verbose;
83 }else if(a.equals("tree") || a.equals("treefile")){
84 treeFile=b;
85 }else if(parser.parse(arg, a, b)){
86 //do nothing
87 }
88
89 else if(parser.in1==null && i==0 && Tools.looksLikeInputStream(arg)){
90 parser.in1=arg;
91 }else if(parser.out1==null && i==1 && !arg.contains("=")){
92 parser.out1=arg;
93 }else{
94 outstream.println("Unknown parameter "+args[i]);
95 assert(false) : "Unknown parameter "+args[i];
96 // throw new RuntimeException("Unknown parameter "+args[i]);
97 }
98 }
99
100 {//Process parser fields
101 Parser.processQuality();
102
103 maxReads=parser.maxReads;
104
105 overwrite=ReadStats.overwrite=parser.overwrite;
106 append=ReadStats.append=parser.append;
107
108 in1=parser.in1;
109
110 out1=parser.out1;
111
112 extin=parser.extin;
113 extout=parser.extout;
114 }
115
116 assert(FastaReadInputStream.settingsOK());
117
118 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
119 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2){
120 ByteFile.FORCE_MODE_BF2=false;
121 ByteFile.FORCE_MODE_BF1=true;
122 }
123
124 if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;}
125
126 if(!Tools.testOutputFiles(overwrite, append, false, out1)){
127 outstream.println((out1==null)+", "+out1);
128 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n");
129 }
130
131 ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, false);
132
133 ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true);
134
135 tree=TaxTree.loadTaxTree(treeFile, outstream, true, false);
136 }
137
138 void process(Timer t){
139
140 final ConcurrentReadInputStream cris;
141 {
142 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null);
143 cris.start();
144 if(verbose){outstream.println("Started cris");}
145 }
146 boolean paired=cris.paired();
147 // if(verbose){
148 if(!ffin1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));}
149 // }
150
151 final ConcurrentReadOutputStream ros;
152 if(out1!=null){
153 final int buff=4;
154
155 assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name.";
156
157 ros=ConcurrentReadOutputStream.getStream(ffout1, null, null, null, buff, null, false);
158 ros.start();
159 }else{ros=null;}
160
161 long readsProcessed=0, readsOut=0;
162 long basesProcessed=0, basesOut=0;
163
164 {
165
166 ListNum<Read> ln=cris.nextList();
167 ArrayList<Read> reads=(ln!=null ? ln.list : null);
168
169 // outstream.println("Fetched "+reads);
170
171 if(reads!=null && !reads.isEmpty()){
172 Read r=reads.get(0);
173 assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired());
174 }
175
176 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
177
178 final ArrayList<Read> listOut=new ArrayList<Read>(reads.size());
179 for(int idx=0; idx<reads.size(); idx++){
180 final Read r1=reads.get(idx);
181
182 final int initialLength1=r1.length();
183 final boolean keep=process(r1);
184 if(keep){
185 listOut.add(r1);
186 readsOut++;
187 basesOut+=r1.length();
188 }
189
190 readsProcessed++;
191 basesProcessed+=initialLength1;
192 }
193
194 if(ros!=null){ros.add(listOut, ln.id);}
195
196 cris.returnList(ln);
197 ln=cris.nextList();
198 reads=(ln!=null ? ln.list : null);
199 }
200 if(ln!=null){
201 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
202 }
203 }
204
205 errorState|=ReadStats.writeAll();
206
207 errorState|=ReadWrite.closeStreams(cris, ros);
208
209 t.stop();
210 outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8));
211 outstream.println(Tools.readsBasesOut(readsProcessed, basesProcessed, readsOut, basesOut, 8, false));
212
213 if(errorState){
214 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
215 }
216 }
217
218 /*--------------------------------------------------------------*/
219
220 private boolean process(Read r){
221 TaxNode tn=tree.parseNodeFromHeader(r.id, true);
222 if(tn==null){return false;}
223 if(tree.isEukaryote(tn.id)){
224 if(r.id.contains(";Chloroplast;") || r.id.contains("Mitochondria")){return false;}
225 if(r.id.contains("Bacteria;") || r.id.contains("Archaea;")){return false;}
226 //Unclear how to remove 16S from euks at this point; best to do it later.
227 //Might be worth adding the SSU type to the sequence header, though, e.g. 16S/18S/m16S/m18S/c16S
228 }
229 return true;
230 }
231
232 /*--------------------------------------------------------------*/
233
234 private String in1=null;
235
236 private String out1=null;
237
238 private String extin=null;
239 private String extout=null;
240
241 private String treeFile="auto";
242
243 /*--------------------------------------------------------------*/
244
245 private long maxReads=-1;
246
247 /*--------------------------------------------------------------*/
248
249 private final FileFormat ffin1;
250
251 private final FileFormat ffout1;
252
253 private final TaxTree tree;
254
255 /*--------------------------------------------------------------*/
256
257 private PrintStream outstream=System.err;
258 public static boolean verbose=false;
259 public boolean errorState=false;
260 private boolean overwrite=true;
261 private boolean append=false;
262
263 }