Debian Codegraph @

current/jgi/MakeChimeras.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.Random;
7
8 import fileIO.ByteFile;
9 import fileIO.ByteFile1;
10 import fileIO.ByteFile2;
11 import fileIO.FileFormat;
12 import fileIO.ReadWrite;
13 import fileIO.TextStreamWriter;
14 import shared.KillSwitch;
15 import shared.Parse;
16 import shared.Parser;
17 import shared.PreParser;
18 import shared.Shared;
19 import shared.Timer;
20 import shared.Tools;
21 import stream.ConcurrentGenericReadInputStream;
22 import stream.ConcurrentReadInputStream;
23 import stream.FASTQ;
24 import stream.FastaReadInputStream;
25 import stream.Read;
26 import structures.ListNum;
27
28
29 /**
30 * Fuses reads together randomly to make chimeric reads.
31 * @author Brian Bushnell
32 * @date Oct 7, 2014
33 *
34 */
35 public class MakeChimeras {
36
37 public static void main(String[] args){
38 Timer t=new Timer();
39 MakeChimeras x=new MakeChimeras(args);
40 x.process(t);
41
42 //Close the print stream if it was redirected
43 Shared.closeStream(x.outstream);
44 }
45
46 public MakeChimeras(String[] args){
47
48 {//Preparse block for help, config files, and outstream
49 PreParser pp=new PreParser(args, getClass(), false);
50 args=pp.args;
51 outstream=pp.outstream;
52 }
53
54 Shared.capBuffers(4);
55 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
56 ReadWrite.setZipThreads(Shared.threads());
57
58 FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false;
59
60 Parser parser=new Parser();
61 for(int i=0; i<args.length; i++){
62 String arg=args[i];
63 String[] split=arg.split("=");
64 String a=split[0].toLowerCase();
65 String b=split.length>1 ? split[1] : null;
66
67 if(parser.parse(arg, a, b)){
68 //do nothing
69 }else if(a.equals("verbose")){
70 verbose=Parse.parseBoolean(b);
71 ByteFile1.verbose=verbose;
72 ByteFile2.verbose=verbose;
73 stream.FastaReadInputStream.verbose=verbose;
74 ConcurrentGenericReadInputStream.verbose=verbose;
75 stream.FastqReadInputStream.verbose=verbose;
76 ReadWrite.verbose=verbose;
77 }else if(parser.in1==null && i==0 && Tools.looksLikeInputStream(arg)){
78 parser.in1=arg;
79 }else if(a.equals("forcelength")){
80 forceLength=Integer.parseInt(b);
81 }else if(a.equals("readsout") || a.equals("chimeras")){
82 readsOut=Parse.parseKMG(b);
83 }else{
84 outstream.println("Unknown parameter "+args[i]);
85 assert(false) : "Unknown parameter "+args[i];
86 // throw new RuntimeException("Unknown parameter "+args[i]);
87 }
88 }
89
90 {//Process parser fields
91 Parser.processQuality();
92
93 readsIn=parser.maxReads;
94
95 in1=parser.in1;
96 qfin1=parser.qfin1;
97
98 out1=parser.out1;
99
100 extin=parser.extin;
101 extout=parser.extout;
102 }
103
104 assert(FastaReadInputStream.settingsOK());
105
106 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
107 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
108 ByteFile.FORCE_MODE_BF2=true;
109 }
110
111 if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;}
112
113 if(!Tools.testOutputFiles(overwrite, append, false, out1)){
114 outstream.println((out1==null)+", "+out1);
115 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out1+"\n");
116 }
117
118 ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, false);
119
120 ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true);
121 }
122
123 void process(Timer t){
124 assert(readsOut>0) : "Please set the 'readsout' flag to a positive integer.";
125
126 ArrayList<Read> source=new ArrayList<Read>();
127 {
128 final ConcurrentReadInputStream cris;
129 {
130 cris=ConcurrentReadInputStream.getReadInputStream(readsIn, false, ffin1, null, qfin1, null);
131 if(verbose){outstream.println("Started cris");}
132 cris.start(); //4567
133 }
134 assert(!cris.paired());
135
136 long readsProcessed=0;
137 long basesProcessed=0;
138
139 {
140
141 ListNum<Read> ln=cris.nextList();
142 ArrayList<Read> reads=(ln!=null ? ln.list : null);
143
144 // outstream.println("Fetched "+reads);
145
146 if(reads!=null && !reads.isEmpty()){
147 Read r=reads.get(0);
148 assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired());
149 }
150
151 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
152
153 for(int idx=0; idx<reads.size(); idx++){
154 final Read r1=reads.get(idx);
155 assert(r1.mate==null);
156
157 final int initialLength1=r1.length();
158
159 if(initialLength1>0){
160 source.add(r1);
161 }
162
163 readsProcessed++;
164 basesProcessed+=initialLength1;
165 }
166
167 cris.returnList(ln);
168 ln=cris.nextList();
169 reads=(ln!=null ? ln.list : null);
170 }
171 if(ln!=null){
172 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
173 }
174 }
175
176 errorState|=ReadWrite.closeStream(cris);
177
178 t.stop();
179 outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8));
180 }
181
182
183
184 if(readsOut>=0){
185 t.start();
186
187 final TextStreamWriter tsw;
188 if(ffout1==null){
189 tsw=null;
190 }else{
191 tsw=new TextStreamWriter(ffout1);
192 tsw.start();
193 }
194
195 final Random randy=Shared.threadLocalRandom();
196
197 long readsProcessed=0;
198 long basesProcessed=0;
199 final int mod=source.size();
200 for(long i=0; i<readsOut; i++){
201 Read a=source.get(randy.nextInt(mod));
202 Read b=source.get(randy.nextInt(mod));
203 Read c=makeChimera(a, b, randy, i);
204 if(c==null){
205 i--;
206 }else{
207 if(tsw!=null && c!=null){
208 tsw.println(c);
209 readsProcessed++;
210 basesProcessed+=c.length();
211 }
212 }
213 }
214
215 if(tsw!=null){errorState|=tsw.poisonAndWait();}
216
217 t.stop();
218 outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8));
219 }
220
221 if(errorState){
222 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
223 }
224 }
225
226 /**
227 * @param a
228 * @param b
229 * @param randy
230 * @return
231 */
232 private Read makeChimera(Read a, Read b, Random randy, long numericID) {
233 final String id=a.id+" ~ "+b.id;
234
235 final Read a2, b2;
236 if(forceLength>0){
237 // if(a.length()>b.length()){
238 // Read c=a;
239 // a=b;
240 // b=c;
241 // }
242 a2=getPiece(a, randy, forceLength);
243 b2=getPiece(b, randy, b.length()-forceLength);
244 if(a2==null || b2==null){return null;}
245 }else{
246 a2=getPiece(a, randy);
247 b2=getPiece(b, randy);
248 }
249
250 a=b=null;
251
252 final byte[] abases=a2.bases, bbases=b2.bases, aquals=a2.quality, bquals=b2.quality;
253 final int alen=a2.length(), blen=b2.length();
254 final int len=a2.length()+b2.length();
255 byte[] bases=new byte[len];
256 byte[] quals=(aquals==null || bquals==null) ? null : new byte[len];
257
258 for(int i=0; i<alen; i++){
259 bases[i]=abases[i];
260 if(quals!=null){quals[i]=aquals[i];}
261 }
262 for(int i=alen, j=0; j<blen; i++, j++){
263 bases[i]=bbases[j];
264 if(quals!=null){quals[i]=bquals[j];}
265 }
266
267 Read r=new Read(bases, quals, id, numericID);
268 if(Tools.nextBoolean(randy)){r.reverseComplement();}
269 return r;
270 }
271
272 /**
273 * @param value
274 * @param randy
275 * @return
276 */
277 private static Read getPiece(Read a, Random randy) {
278 int len=randy.nextInt(a.length())+1;
279
280 final int start;
281 if(Tools.nextBoolean(randy)){
282 if(Tools.nextBoolean(randy)){
283 start=0;
284 }else{
285 start=a.length()-len;
286 }
287 }else{
288 int range=a.length()-len;
289 start=randy.nextInt(range+1);
290 }
291
292 byte[] bases=KillSwitch.copyOfRange(a.bases, start, start+len);
293 byte[] quals=a.quality==null ? null : KillSwitch.copyOfRange(a.quality, start, start+len);
294
295 Read r=new Read(bases, quals, a.id, a.numericID);
296 if(Tools.nextBoolean(randy)){r.reverseComplement();}
297 return r;
298 }
299
300 /**
301 * @param value
302 * @param randy
303 * @return
304 */
305 private Read getPiece(Read a, Random randy, int len) {
306 len=Tools.min(len, a.length());
307 if(len<1){return null;}
308
309 final int start;
310 if(Tools.nextBoolean(randy)){
311 if(Tools.nextBoolean(randy)){
312 start=0;
313 }else{
314 start=a.length()-len;
315 }
316 }else{
317 int range=a.length()-len;
318 start=randy.nextInt(range+1);
319 }
320
321 byte[] bases=KillSwitch.copyOfRange(a.bases, start, start+len);
322 byte[] quals=a.quality==null ? null : KillSwitch.copyOfRange(a.quality, start, start+len);
323
324 Read r=new Read(bases, quals, a.id, a.numericID);
325 if(Tools.nextBoolean(randy)){r.reverseComplement();}
326 return r;
327 }
328
329 /*--------------------------------------------------------------*/
330
331 /*--------------------------------------------------------------*/
332
333 private String in1=null;
334
335 private String qfin1=null;
336
337 private String out1=null;
338
339 private String extin=null;
340 private String extout=null;
341
342 private int forceLength=0;
343
344 /*--------------------------------------------------------------*/
345
346 private long readsIn=-1;
347 private long readsOut=-1;
348
349 /*--------------------------------------------------------------*/
350
351 private final FileFormat ffin1;
352
353 private final FileFormat ffout1;
354
355
356 /*--------------------------------------------------------------*/
357
358 private PrintStream outstream=System.err;
359 public static boolean verbose=false;
360 public boolean errorState=false;
361 private boolean overwrite=true;
362 private boolean append=false;
363
364 }