Debian Codegraph @

current/jgi/KeepBestCopy.java

raw symbols SCIP
1 package jgi;
2
3 import java.io.File;
4 import java.io.PrintStream;
5 import java.util.ArrayList;
6 import java.util.LinkedHashMap;
7 import java.util.Map.Entry;
8
9 import fileIO.ByteFile;
10 import fileIO.ByteFile1;
11 import fileIO.ByteFile2;
12 import fileIO.FileFormat;
13 import fileIO.ReadWrite;
14 import shared.Parse;
15 import shared.Parser;
16 import shared.PreParser;
17 import shared.Shared;
18 import shared.Timer;
19 import shared.Tools;
20 import stream.ConcurrentGenericReadInputStream;
21 import stream.ConcurrentReadInputStream;
22 import stream.ConcurrentReadOutputStream;
23 import stream.FASTQ;
24 import stream.FastaReadInputStream;
25 import stream.Read;
26 import structures.ListNum;
27 import tax.GiToTaxid;
28 import tracker.ReadStats;
29
30 /**
31 * Designed to keep the best copy of an SSU per organism.
32 * @author Brian Bushnell
33 * @date Oct 4, 2019
34 *
35 */
36 public class KeepBestCopy {
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 KeepBestCopy x=new KeepBestCopy(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 KeepBestCopy(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
66 Parser parser=new Parser();
67 for(int i=0; i<args.length; i++){
68 String arg=args[i];
69 String[] split=arg.split("=");
70 String a=split[0].toLowerCase();
71 String b=split.length>1 ? split[1] : null;
72 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
73
74 if(a.equals("verbose")){
75 verbose=Parse.parseBoolean(b);
76 ByteFile1.verbose=verbose;
77 ByteFile2.verbose=verbose;
78 stream.FastaReadInputStream.verbose=verbose;
79 ConcurrentGenericReadInputStream.verbose=verbose;
80 stream.FastqReadInputStream.verbose=verbose;
81 ReadWrite.verbose=verbose;
82 }else if(a.equals("maxlen")){
83 maxLen=Integer.parseInt(b);
84 }else if(parser.parse(arg, a, b)){
85 //do nothing
86 }
87
88 else if(parser.in1==null && i==0 && Tools.looksLikeInputStream(arg)){
89 parser.in1=arg;
90 }else if(parser.out1==null && i==1 && !arg.contains("=")){
91 parser.out1=arg;
92 }else{
93 outstream.println("Unknown parameter "+args[i]);
94 assert(false) : "Unknown parameter "+args[i];
95 // throw new RuntimeException("Unknown parameter "+args[i]);
96 }
97 }
98
99 {//Process parser fields
100 Parser.processQuality();
101
102 maxReads=parser.maxReads;
103
104 overwrite=ReadStats.overwrite=parser.overwrite;
105 append=ReadStats.append=parser.append;
106
107 in1=parser.in1;
108 qfin1=parser.qfin1;
109
110 out1=parser.out1;
111 qfout1=parser.qfout1;
112
113 extin=parser.extin;
114 extout=parser.extout;
115 }
116
117 assert(FastaReadInputStream.settingsOK());
118
119 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
120 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2){
121 ByteFile.FORCE_MODE_BF2=false;
122 ByteFile.FORCE_MODE_BF1=true;
123 }
124
125 if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;}
126
127 if(!Tools.testOutputFiles(overwrite, append, false, out1)){
128 outstream.println((out1==null)+", "+out1);
129 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n");
130 }
131
132 ffout1=FileFormat.testOutput(out1, FileFormat.FASTA, extout, true, overwrite, append, false);
133
134 ffin1=FileFormat.testInput(in1, FileFormat.FASTA, extin, true, true);
135 }
136
137 ConcurrentReadInputStream makeCris(){
138 final ConcurrentReadInputStream cris;
139 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null, qfin1, null);
140 cris.start();
141 if(verbose){outstream.println("Started cris");}
142 return cris;
143 }
144
145 void process(Timer t){
146
147 final ConcurrentReadInputStream cris=makeCris();
148 boolean paired=cris.paired();
149 if(!ffin1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));}
150
151 long readsProcessed=0, readsOut=0;
152 long basesProcessed=0, basesOut=0;
153
154 {
155
156 ListNum<Read> ln=cris.nextList();
157 ArrayList<Read> reads=(ln!=null ? ln.list : null);
158
159 // outstream.println("Fetched "+reads);
160
161 if(reads!=null && !reads.isEmpty()){
162 Read r=reads.get(0);
163 assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired());
164 }
165
166 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
167
168 final ArrayList<Read> listOut=new ArrayList<Read>(reads.size());
169 for(int idx=0; idx<reads.size(); idx++){
170 final Read r1=reads.get(idx);
171
172 final int initialLength1=r1.length();
173 final boolean keep=process(r1);
174
175 readsProcessed++;
176 basesProcessed+=initialLength1;
177 }
178
179 cris.returnList(ln);
180 ln=cris.nextList();
181 reads=(ln!=null ? ln.list : null);
182 }
183 if(ln!=null){
184 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
185 }
186 }
187
188 errorState|=ReadWrite.closeStreams(cris);
189
190 final ConcurrentReadOutputStream ros;
191 if(out1!=null){
192 final int buff=4;
193
194 assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name.";
195
196 ros=ConcurrentReadOutputStream.getStream(ffout1, null, qfout1, null, buff, null, false);
197 ros.start();
198 }else{ros=null;}
199
200 {
201 ArrayList<Read> list=new ArrayList<Read>(200);
202 long ln=0;
203 for(Entry<Integer, Read> e : map.entrySet()){
204 Read r=e.getValue();
205 list.add(r);
206 readsOut++;
207 basesOut+=r.length();
208 if(list.size()>=200){
209 if(ros!=null){ros.add(list, ln);}
210 ln++;
211 list=new ArrayList<Read>(200);
212 }
213 }
214 }
215
216 errorState|=ReadStats.writeAll();
217
218 errorState|=ReadWrite.closeStream(ros);
219
220 t.stop();
221 outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8));
222 outstream.println(Tools.readsBasesOut(readsProcessed, basesProcessed, readsOut, basesOut, 8, false));
223
224 if(errorState){
225 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
226 }
227 }
228
229 /*--------------------------------------------------------------*/
230
231 private boolean process(Read r){
232 int tid=GiToTaxid.parseTaxidNumber(r.id, '|');
233 if(tid<0){return false;}
234 Integer key=tid;
235 Read old=map.get(key);
236 if(old==null || isBetterThan(r, old)){
237 map.put(key, r);
238 return true;
239 }
240 return false;
241 }
242
243 private boolean isBetterThan(Read r, Read old){
244 if(old==null){return true;}
245 int oldNs=r.countNocalls();
246 int Ns=r.countUndefined();
247 int oldDef=old.length()-oldNs;
248 int def=r.length()-Ns;
249 if(old.length()>maxLen && r.length()<old.length()){return true;}
250 if(r.length()>maxLen && old.length()<r.length()){return false;}
251 return def>oldDef || (def==oldDef && Ns<oldNs);
252 }
253
254 /*--------------------------------------------------------------*/
255
256 private String in1=null;
257
258 private String qfin1=null;
259
260 private String out1=null;
261
262 private String qfout1=null;
263
264 private String extin=null;
265 private String extout=null;
266
267 /*--------------------------------------------------------------*/
268
269 int maxLen=1600;
270
271 private long maxReads=-1;
272
273 private LinkedHashMap<Integer, Read> map=new LinkedHashMap<Integer, Read>();
274
275 /*--------------------------------------------------------------*/
276
277 private final FileFormat ffin1;
278 private final FileFormat ffout1;
279
280 /*--------------------------------------------------------------*/
281
282 private PrintStream outstream=System.err;
283 public static boolean verbose=false;
284 public boolean errorState=false;
285 private boolean overwrite=true;
286 private boolean append=false;
287
288 }