Debian Codegraph @

current/jgi/MutateGenome.java

raw symbols SCIP
1 package jgi;
2
3 import java.util.ArrayList;
4 import java.util.Random;
5
6 import dna.AminoAcid;
7 import fileIO.ByteStreamWriter;
8 import fileIO.FileFormat;
9 import fileIO.ReadWrite;
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 stream.ConcurrentReadInputStream;
17 import stream.ConcurrentReadOutputStream;
18 import stream.FASTQ;
19 import stream.Read;
20 import structures.ByteBuilder;
21 import structures.ListNum;
22 import var2.Var;
23
24 /**
25 * @author Brian Bushnell
26 * @date June 1, 2016
27 *
28 */
29 public class MutateGenome {
30
31 public static void main(String[] args){
32 if(parsePloidy(args)!=1){
33 MutateGenome2.main(args);
34 return;
35 }
36 Timer t=new Timer();
37 MutateGenome x=new MutateGenome(args);
38 x.process(t);
39
40 //Close the print stream if it was redirected
41 Shared.closeStream(x.outstream);
42 }
43
44 public MutateGenome(String[] args){
45
46 {//Preparse block for help, config files, and outstream
47 PreParser pp=new PreParser(args, getClass(), false);
48 args=pp.args;
49 outstream=pp.outstream;
50 }
51
52 Shared.setBufferLen(1);
53 FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false;
54
55 Parser parser=new Parser();
56 parser.overwrite=true;
57 for(int i=0; i<args.length; i++){
58 String arg=args[i];
59 String[] split=arg.split("=");
60 String a=split[0].toLowerCase();
61 String b=split.length>1 ? split[1] : null;
62
63 if(parser.parse(arg, a, b)){
64 //do nothing
65 }else if(a.equals("parse_flag_goes_here")){
66 //Set a variable here
67 }else if(a.equals("period")){
68 period=Integer.parseInt(b);
69 }else if(a.equals("subrate") || a.equals("snprate")){
70 subRate=Float.parseFloat(b);
71 if(subRate>1){subRate/=100;}
72 }else if(a.equals("indelrate")){
73 indelRate=Float.parseFloat(b);
74 if(indelRate>1){indelRate/=100;}
75 }else if(a.equals("maxindel")){
76 maxIndel=Parse.parseIntKMG(b);
77 }else if(a.equals("indelspacing")){
78 indelSpacing=Parse.parseIntKMG(b);
79 }else if(a.equals("seed")){
80 seed=Long.parseLong(b);
81 }else if(a.equals("nohomopolymers") || a.equals("nohomo") || a.equals("banhomopolymers") || a.equals("banhomo")){
82 banHomopolymers=Parse.parseBoolean(b);
83 }else if(a.equals("prefix")){
84 prefix=b;
85 }else if(a.equals("vcf") || a.equals("outvcf") || a.equals("vcfout") || a.equals("vars") || a.equals("varsout") || a.equals("outvars")){
86 outVcf=b;
87 }else if(a.equals("id") || a.equals("identity")){
88 float x=Float.parseFloat(b);
89 if(x>1){x=x/100;}
90 x=1-x;
91 indelRate=x*0.01f;
92 subRate=x*0.99f;
93 }else if(a.equals("fraction") || a.equals("completeness")){
94 float x=Float.parseFloat(b);
95 if(x>1){x=x/100;}
96 genomeFraction=x;
97 }else{
98 outstream.println("Unknown parameter "+args[i]);
99 assert(false) : "Unknown parameter "+args[i];
100 // throw new RuntimeException("Unknown parameter "+args[i]);
101 }
102 }
103 errorRate=subRate+indelRate;
104 errorRate2=subRate+(indelRate*0.16666f*(maxIndel+1));
105
106 assert(subRate>=0 && subRate<=1) : "Substitution rate must be between 0 and 1, inclusive. Invalid value: "+subRate;
107 assert(indelRate>=0 && indelRate<=1) : "Indel rate must be between 0 and 1, inclusive. Invalid value: "+indelRate;
108 assert(errorRate>=0 && errorRate<=1) : "Total error rate must be between 0 and 1, inclusive. Invalid value: "+errorRate;
109
110 System.err.println(Tools.format("Target Identity: \t%.2f%%", (1-errorRate2)*100));
111 System.err.println(Tools.format("Substitution Rate: \t%.2f%%", subRate*100));
112 System.err.println(Tools.format("Indel Rate: \t%.2f%%", indelRate*100));
113
114 randy=Shared.threadLocalRandom(seed);
115
116 {//Process parser fields
117 Parser.processQuality();
118
119 maxReads=parser.maxReads;
120 in1=parser.in1;
121 out1=parser.out1;
122 overwrite=parser.overwrite;
123 append=parser.append;
124 }
125
126 //Ensure there is an input file
127 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
128
129 //Ensure output files can be written
130 if(!Tools.testOutputFiles(overwrite, append, false, out1, outVcf)){
131 outstream.println((out1==null)+", "+(outVcf==null)+", "+out1+", "+outVcf);
132 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+outVcf+"\n");
133 }
134
135 //Ensure input files can be read
136 if(!Tools.testInputFiles(false, true, in1)){
137 throw new RuntimeException("\nCan't read some input files.\n");
138 }
139
140 //Ensure that no file was specified multiple times
141 if(!Tools.testForDuplicateFiles(true, in1, out1, outVcf)){
142 throw new RuntimeException("\nSome file names were specified multiple times.\n");
143 }
144
145 ffout1=FileFormat.testOutput(out1, FileFormat.FASTA, null, true, overwrite, append, false);
146 ffoutVcf=FileFormat.testOutput(outVcf, FileFormat.VCF, null, true, overwrite, append, false);
147 ffin1=FileFormat.testInput(in1, FileFormat.FASTA, null, true, true);
148 }
149
150 private static int parsePloidy(String[] args){
151 int ploidy=1;
152 for(int i=0; i<args.length; i++){
153 String arg=args[i];
154 String[] split=arg.split("=");
155 String a=split[0].toLowerCase();
156 String b=split.length>1 ? split[1] : null;
157
158 if(a.equals("ploidy")){
159 ploidy=Integer.parseInt(b);
160 }
161 }
162 return ploidy;
163 }
164
165 void process(Timer t){
166
167 final ConcurrentReadInputStream cris;
168 {
169 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null);
170 cris.start();
171 }
172
173 final ConcurrentReadOutputStream ros;
174 if(out1!=null){
175 final int buff=4;
176
177 if(cris.paired() && (in1==null || !in1.contains(".sam"))){
178 outstream.println("Writing interleaved.");
179 }
180
181 assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name.";
182
183 ros=ConcurrentReadOutputStream.getStream(ffout1, null, buff, null, false);
184 ros.start();
185 }else{ros=null;}
186
187 {
188
189 ListNum<Read> ln=cris.nextList();
190 ArrayList<Read> reads=(ln!=null ? ln.list : null);
191
192 if(reads!=null && !reads.isEmpty()){
193 Read r=reads.get(0);
194 assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired());
195 }
196 ByteBuilder bb=new ByteBuilder();
197 ArrayList<String> headers=(ffoutVcf==null ? null : new ArrayList<String>());
198 ArrayList<SmallVar> vars=(ffoutVcf==null ? null : new ArrayList<SmallVar>());
199 ArrayList<SmallVar> varsTemp=(ffoutVcf==null ? null : new ArrayList<SmallVar>());
200
201 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
202 if(verbose){outstream.println("Fetched "+reads.size()+" reads.");}
203
204 for(int idx=0; idx<reads.size(); idx++){
205 final Read r1=reads.get(idx);
206
207 readsProcessed++;
208 basesProcessed+=r1.length();
209
210 processRead(r1, bb, varsTemp, headers);
211 if(vars!=null){vars.addAll(varsTemp);}
212 }
213
214 if(ros!=null){ros.add(reads, ln.id);}
215
216 cris.returnList(ln);
217 if(verbose){outstream.println("Returned a list.");}
218 ln=cris.nextList();
219 reads=(ln!=null ? ln.list : null);
220 }
221 if(ln!=null){
222 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
223 }
224
225 writeVars(vars, headers);
226 }
227
228 ReadWrite.closeStreams(cris, ros);
229 if(verbose){outstream.println("Finished.");}
230
231
232 {
233 t.stop();
234
235 //Calculate units per nanosecond
236 double brnano=basesRetained/(double)(t.elapsed);
237
238 //Add "k" and "m" for large numbers
239 long mutationsAdded=subsAdded+insAdded+delsAdded+junctionsAdded;
240 String brstring=(basesRetained<100000 ? ""+basesRetained : basesRetained<100000000 ? (basesRetained/1000)+"k" : (basesRetained/1000000)+"m");
241 String mastring=(mutationsAdded<100000 ? ""+mutationsAdded : mutationsAdded<100000000 ? (mutationsAdded/1000)+"k" : (mutationsAdded/1000000)+"m");
242 String sastring=(subsAdded<100000 ? ""+subsAdded : subsAdded<100000000 ? (subsAdded/1000)+"k" : (subsAdded/1000000)+"m");
243 String iastring=(insAdded<100000 ? ""+insAdded : insAdded<100000000 ? (insAdded/1000)+"k" : (insAdded/1000000)+"m");
244 String dastring=(delsAdded<100000 ? ""+delsAdded : delsAdded<100000000 ? (delsAdded/1000)+"k" : (delsAdded/1000000)+"m");
245 String jastring=(junctionsAdded<100000 ? ""+junctionsAdded : junctionsAdded<100000000 ? (junctionsAdded/1000)+"k" : (junctionsAdded/1000000)+"m");
246
247 //Format the strings so they have they are right-justified
248 while(brstring.length()<8){brstring=" "+brstring;}
249 while(mastring.length()<8){mastring=" "+mastring;}
250 while(sastring.length()<8){sastring=" "+sastring;}
251 while(iastring.length()<8){iastring=" "+iastring;}
252 while(dastring.length()<8){dastring=" "+dastring;}
253 while(jastring.length()<8){jastring=" "+jastring;}
254
255 outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8));
256 if(genomeFraction<1){outstream.println("Bases Retained: "+brstring+" \t"+Tools.format("%.2fm bases/sec", brnano*1000));}
257 outstream.println("Mutations Added: "+mastring+" \t"+Tools.format("%.2f%% Identity", 100f-mutationLengthAdded*100f/basesProcessed));
258 outstream.println("Subs Added: "+sastring);
259 outstream.println("Insertions Added: "+iastring);
260 outstream.println("Deletions Added: "+dastring);
261 outstream.println("Junctions Added: "+jastring);
262 }
263
264 t.stop();
265 }
266
267 public boolean isHomopolymerDel(byte[] bases, int pos, int len){
268 final byte b=bases[pos];
269 for(int i=1; i<len; i++){
270 if(bases[pos+i]!=b){return false;}
271 }
272 if(pos>0 && bases[pos-1]==b){return true;}
273 if(pos<bases.length-1 && bases[pos+1]==b){return true;}
274 return false;
275 }
276
277 public boolean isHomopolymerIns(byte[] bases, int pos, byte b){
278 if(b==bases[pos]){return true;}
279 if(pos>0 && b==bases[pos-1]){return true;}
280 return false;
281 }
282
283 public boolean isHomopolymerIns(byte[] bases, int pos, StringBuilder sb){
284 byte b=(byte) sb.charAt(0);
285 for(int i=1; i<sb.length(); i++) {
286 if(sb.charAt(i)!=b){return false;}
287 }
288 return isHomopolymerIns(bases, pos, b);
289 }
290
291 public void processRead(Read r, ByteBuilder bb, ArrayList<SmallVar> vars, ArrayList<String> headers){
292
293 if(r.aminoacid()) {
294 processReadAmino(r, bb, vars, headers);
295 return;
296 }
297
298 //Setup
299 bb.clear();
300 r.quality=null;
301 if(headers!=null){headers.add("<ID="+r.id+",length="+r.length()+">");}
302 if(vars!=null){vars.clear();}
303
304 //Handle genomeFraction
305 if(genomeFraction<1){
306 final byte[] bases0=r.bases;
307 int retain=(int)(bases0.length*(genomeFraction));
308 if(retain<bases0.length){
309 final int start=randy.nextInt(bases0.length);
310 int i=0, j=start;
311 for(; i<retain && j<bases0.length; i++, j++){
312 bb.append(bases0[j]);
313 }
314 j=0;
315
316 if(i<retain){
317 junctionsAdded++;
318 mutationLengthAdded++;
319 } //Chimeric junction
320
321 for(; i<retain; i++, j++){
322 bb.append(bases0[j]);
323 }
324 r.bases=bb.toBytes();
325 bb.clear();
326 }
327 }
328
329 //Handle mutations
330 final byte[] bases=r.bases;
331
332 if(period>-1){
333 int basesSinceMutation=0;
334 char prevChar='N';
335 for(int i=0; i<bases.length; i++){
336 final byte b0=bases[i];
337 byte b=b0;
338 if(basesSinceMutation>=period && AminoAcid.isFullyDefined(b)){
339 basesSinceMutation=0;
340 float x=randy.nextFloat()*errorRate;
341 if(x<subRate){
342 b=AminoAcid.numberToBase[((AminoAcid.baseToNumber[b]+randy.nextInt(3)+1)&3)];
343 bb.append(b);
344 if(vars!=null){vars.add(new SmallVar(SUB, i, i+1, Character.toString((char)b), Character.toString((char)b0), prevChar, r.id, r.numericID));}
345 subsAdded++;
346 mutationLengthAdded++;
347 }else if(randy.nextBoolean()){//del
348 if(banHomopolymers && isHomopolymerDel(bases, i, 1)) {
349 i--;
350 }else{
351 //do nothing
352 if(vars!=null){vars.add(new SmallVar(DEL, i, i+1, "", Character.toString((char)b0), prevChar, r.id, r.numericID));}
353 delsAdded++;
354 mutationLengthAdded++;
355 }
356 }else{//ins
357 b=AminoAcid.numberToBase[randy.nextInt(4)];
358 while(banHomopolymers && isHomopolymerIns(bases, i, b)) {
359 b=AminoAcid.numberToBase[randy.nextInt(4)];
360 }
361 bb.append(b);
362 if(vars!=null){vars.add(new SmallVar(INS, i, i, Character.toString((char)b), "", prevChar, r.id, r.numericID));}
363 i--;
364 insAdded++;
365 mutationLengthAdded++;
366 }
367 }else{
368 basesSinceMutation++;
369 bb.append(b);
370 }
371 prevChar=(char) b0;
372 }
373 }else{
374 char prevChar='N';
375 int lastIndel=-1;
376 for(int i=0; i<bases.length; i++){
377 final byte b0=bases[i];
378 byte b=b0;
379 float x=randy.nextFloat();
380 if(x<errorRate && AminoAcid.isFullyDefined(b)){
381 // System.err.println(x+", "+errorRate+", "+subRate);
382 if(x<subRate){
383 subsAdded++;
384 mutationLengthAdded++;
385 b=AminoAcid.numberToBase[((AminoAcid.baseToNumber[b]+randy.nextInt(3)+1)&3)];
386 bb.append(b);
387 if(vars!=null){vars.add(new SmallVar(SUB, i, i+1, Character.toString((char)b), Character.toString((char)b0), prevChar, r.id, r.numericID));}
388 }else if(i-lastIndel>indelSpacing){
389 int lim=Tools.min(maxIndel, bases.length-i-1);
390 if(lim>=1){
391 int len=1+(Tools.min(randy.nextInt(lim), randy.nextInt(lim), randy.nextInt(lim)));
392
393 if(randy.nextBoolean()){//del
394 if(banHomopolymers && isHomopolymerDel(bases, i, len)) {
395 i--;
396 }else{
397 delsAdded++;
398 mutationLengthAdded+=len;
399 //do nothing
400 if(vars!=null){vars.add(new SmallVar(DEL, i, i+len, "", new String(bases, i, len), prevChar, r.id, r.numericID));}
401 i=i+len-1;
402 }
403 }else{//ins
404 insAdded++;
405 mutationLengthAdded+=len;
406 if(len==1){
407 b=AminoAcid.numberToBase[randy.nextInt(4)];
408 while(banHomopolymers && isHomopolymerIns(bases, i, b)) {
409 b=AminoAcid.numberToBase[randy.nextInt(4)];
410 }
411 bb.append(b);
412 if(vars!=null){vars.add(new SmallVar(INS, i, i, Character.toString((char)b), "", prevChar, r.id, r.numericID));}
413 }else{
414 StringBuilder sb=new StringBuilder();
415 while(sb.length()==0 || (banHomopolymers && isHomopolymerIns(bases, i, sb))) {
416 sb.setLength(0);
417 while(sb.length()<len){
418 b=AminoAcid.numberToBase[randy.nextInt(4)];
419 sb.append((char)b);
420 }
421 }
422 if(vars!=null){vars.add(new SmallVar(INS, i, i, sb.toString(), "", prevChar, r.id, r.numericID));}
423 }
424 i--;
425 }
426 lastIndel=i;
427 }
428 }
429 }else{
430 bb.append(b);
431 }
432 prevChar=(char) b0;
433 }
434 }
435
436 condenseVars(vars);
437
438 //Modify read
439 r.bases=bb.toBytes();
440
441 if(prefix!=null){
442 r.id=prefix+r.numericID;
443 }
444 basesRetained+=r.bases.length;
445 }
446
447 public void processReadAmino(Read r, ByteBuilder bb, ArrayList<SmallVar> vars, ArrayList<String> headers){
448
449 assert(r.aminoacid());
450
451 //Setup
452 bb.clear();
453 r.quality=null;
454 if(headers!=null){headers.add("<ID="+r.id+",length="+r.length()+">");}
455 if(vars!=null){vars.clear();}
456
457 //Handle genomeFraction
458 if(genomeFraction<1){
459 final byte[] bases0=r.bases;
460 int retain=(int)(bases0.length*(genomeFraction));
461 if(retain<bases0.length){
462 final int start=randy.nextInt(bases0.length);
463 int i=0, j=start;
464 for(; i<retain && j<bases0.length; i++, j++){
465 bb.append(bases0[j]);
466 }
467 j=0;
468
469 if(i<retain){
470 junctionsAdded++;
471 mutationLengthAdded++;
472 } //Chimeric junction
473
474 for(; i<retain; i++, j++){
475 bb.append(bases0[j]);
476 }
477 r.bases=bb.toBytes();
478 bb.clear();
479 }
480 }
481
482 //Handle mutations
483 final byte[] bases=r.bases;
484
485 if(period>-1){
486 int basesSinceMutation=0;
487 char prevChar='X';
488 for(int i=0; i<bases.length; i++){
489 final byte b0=bases[i];
490 byte b=b0;
491 if(basesSinceMutation>=period && AminoAcid.isFullyDefinedAA(b)){
492 basesSinceMutation=0;
493 float x=randy.nextFloat()*errorRate;
494 if(x<subRate){
495 subsAdded++;
496 mutationLengthAdded++;
497 b=AminoAcid.numberToAcid[((AminoAcid.acidToNumber[b]+randy.nextInt(20)+1)%21)];
498 bb.append(b);
499 if(vars!=null){vars.add(new SmallVar(SUB, i, i+1, Character.toString((char)b), Character.toString((char)b0), prevChar, r.id, r.numericID));}
500 }else if(randy.nextBoolean()){//del
501 delsAdded++;
502 mutationLengthAdded++;
503 //do nothing
504 if(vars!=null){vars.add(new SmallVar(DEL, i, i+1, "", Character.toString((char)b0), prevChar, r.id, r.numericID));}
505 }else{//ins
506 insAdded++;
507 mutationLengthAdded++;
508 b=AminoAcid.numberToAcid[randy.nextInt(21)];
509 bb.append(b);
510 if(vars!=null){vars.add(new SmallVar(INS, i, i, Character.toString((char)b), "", prevChar, r.id, r.numericID));}
511 i--;
512 }
513 }else{
514 basesSinceMutation++;
515 bb.append(b);
516 }
517 prevChar=(char) b0;
518 }
519 }else{
520 char prevChar='N';
521 for(int i=0; i<bases.length; i++){
522 final byte b0=bases[i];
523 byte b=b0;
524 float x=randy.nextFloat();
525 if(x<errorRate && AminoAcid.isFullyDefinedAA(b)){
526 if(x<subRate){
527 subsAdded++;
528 mutationLengthAdded++;
529 b=AminoAcid.numberToAcid[((AminoAcid.acidToNumber[b]+randy.nextInt(20)+1)%21)];
530 bb.append(b);
531 if(vars!=null){vars.add(new SmallVar(SUB, i, i+1, Character.toString((char)b), Character.toString((char)b0), prevChar, r.id, r.numericID));}
532 }else if(randy.nextBoolean()){//del
533 delsAdded++;
534 mutationLengthAdded++;
535 //do nothing
536 if(vars!=null){vars.add(new SmallVar(DEL, i, i+1, "", Character.toString((char)b0), prevChar, r.id, r.numericID));}
537 }else{//ins
538 insAdded++;
539 mutationLengthAdded++;
540 b=AminoAcid.numberToAcid[randy.nextInt(21)];
541 bb.append(b);
542 if(vars!=null){vars.add(new SmallVar(INS, i, i, Character.toString((char)b), "", prevChar, r.id, r.numericID));}
543 i--;
544 }
545 }else{
546 bb.append(b);
547 }
548 prevChar=(char) b0;
549 }
550 }
551
552 condenseVars(vars);
553
554 //Modify read
555 r.bases=bb.toBytes();
556
557 if(prefix!=null){
558 r.id=prefix+r.numericID;
559 }
560 basesRetained+=r.bases.length;
561 }
562
563 /*--------------------------------------------------------------*/
564
565 private void condenseVars(ArrayList<SmallVar> vars){
566 if(vars==null || vars.size()<2){return;}
567
568 {//Pass 1: fuse indels into subs
569 SmallVar current=null;
570 for(int i=0; i<vars.size(); i++){
571 SmallVar next=vars.get(i);
572 if(next.type==SUB){
573 current=null;
574 }else if(current==null){
575 current=next;
576 }else if(current.stop==next.start && current.type!=next.type){
577 if(current.type==DEL){
578 assert(next.type==INS) : next.type;
579 //Change the del to a sub
580 current.type=SUB;
581 current.alt=next.alt;
582 current=null;
583 vars.set(i, null);
584 }else if(current.type==INS){
585 assert(next.type==DEL) : next.type;
586 //Change the ins to a sub
587 current.type=SUB;
588 current.ref=next.ref;
589 current.stop=next.stop;
590 current=null;
591 vars.set(i, null);
592 }else{
593 assert(false) : current.type;
594 }
595 }else{
596 current=next;
597 }
598 }
599 Tools.condenseStrict(vars);
600 if(vars.size()<2){return;}
601 }
602
603 {//Pass 2: lengthen indels
604 SmallVar current=null;
605 for(int i=0; i<vars.size(); i++){
606 SmallVar next=vars.get(i);
607 if(next.type==SUB){
608 current=null;
609 if(!next.valid()){vars.set(i, null);}
610 }else if(current==null){
611 current=next;
612 }else if(current.stop==next.start && current.type==next.type){
613 if(current.type==DEL){
614 assert(next.type==DEL) : next.type;
615 //Lengthen the deletion
616 current.stop=next.stop;
617 current.ref+=next.ref;
618 vars.set(i, null);
619 }else if(current.type==INS){
620 assert(next.type==INS) : next.type;
621 //Lengthen the insertion
622 current.alt+=next.alt;
623 vars.set(i, null);
624 }else{
625 assert(false) : current.type;
626 }
627 }else{
628 current=next;
629 }
630 }
631 Tools.condenseStrict(vars);
632 }
633 }
634
635 void writeVars(ArrayList<SmallVar> vars, ArrayList<String> headers){
636 if(ffoutVcf==null){return;}
637 ByteStreamWriter bsw=new ByteStreamWriter(ffoutVcf);
638 bsw.start();
639 ByteBuilder bb=new ByteBuilder();
640 bb.appendln("##fileformat=VCFv4.2");
641 bb.appendln("##BBMapVersion="+Shared.BBTOOLS_VERSION_STRING);
642 bb.appendln("##Program=MutateGenome");
643 for(String s : headers){
644 bb.append("##contig=").appendln(s);
645 }
646 bb.appendln("##FILTER=<ID=FAIL,Description=\"Fail\">");
647 bb.appendln("##FILTER=<ID=PASS,Description=\"Pass\">");
648 bb.appendln("##INFO=<ID=SN,Number=1,Type=Integer,Description=\"Scaffold Number\">");
649 bb.appendln("##INFO=<ID=STA,Number=1,Type=Integer,Description=\"Start\">");
650 bb.appendln("##INFO=<ID=STO,Number=1,Type=Integer,Description=\"Stop\">");
651 bb.appendln("##INFO=<ID=TYP,Number=1,Type=String,Description=\"Type\">");
652 bb.appendln("##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">");
653 bb.appendln("##FORMAT=<ID=SC,Number=1,Type=Float,Description=\"Score\">");
654 bb.appendln("##FORMAT=<ID=PF,Number=1,Type=String,Description=\"Pass Filter\">");
655 bb.appendln("#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\t"+(out1==null ? "sample" : ReadWrite.stripToCore(out1)));
656
657 bsw.print(bb);
658 bb.clear();
659
660 for(SmallVar v : vars){
661 v.toVcf(bb);
662 bb.nl();
663 if(bb.length()>=64000){
664 bsw.print(bb);
665 bb.clear();
666 }
667 }
668 if(bb.length()>=0){
669 bsw.print(bb);
670 bb.clear();
671 }
672 errorState=bsw.poisonAndWait()|errorState;
673 }
674
675 /*--------------------------------------------------------------*/
676
677 private static class SmallVar{
678
679 SmallVar(int type_, int start_, int stop_, String alt_, String ref_, char prevChar_, String rname_, long scafNum_){
680 type=type_;
681 start=start_;
682 stop=stop_;
683 alt=alt_;
684 ref=ref_;
685 prevChar=prevChar_;
686 rname=rname_;
687 scafNum=scafNum_;
688 }
689
690 boolean valid(){
691 return type!=SUB || !alt.equals(ref);
692 }
693
694 void toVcf(ByteBuilder bb){
695 bb.append(rname).append('\t');
696 if(type==SUB){
697 bb.append(start+1).append('\t');
698 bb.append('.').append('\t');
699 bb.append(ref).append('\t');
700 bb.append(alt).append('\t');
701 }else if(type==DEL || type==INS){
702 bb.append(start).append('\t');
703 bb.append('.').append('\t');
704 bb.append(prevChar).append(ref).append('\t');
705 bb.append(prevChar).append(alt).append('\t');
706 }else{assert(false);}
707 bb.append("60.00").append('\t');
708 bb.append("PASS").append('\t');
709 bb.append("SN=").append(scafNum).append(';');
710 bb.append("STA=").append(start).append(';');
711 bb.append("STO=").append(stop).append(';');
712 bb.append("TYP=").append(Var.typeArray[type]).append('\t');
713 bb.append("GT:SC:PF").append('\t');
714 bb.append(1).append(':');
715 bb.append("60.00").append(':');
716 bb.append("PASS");
717 }
718
719 int type;
720 final int start;
721 int stop;
722 String ref;
723 String alt;
724 final char prevChar;
725 final String rname;
726 final long scafNum;
727
728 }
729
730 /*--------------------------------------------------------------*/
731
732 private String in1=null;
733 private String out1=null;
734 private String outVcf=null;
735
736 private String prefix=null;
737
738 private final FileFormat ffin1;
739 private final FileFormat ffout1;
740 private final FileFormat ffoutVcf;
741
742 /*--------------------------------------------------------------*/
743
744 private long maxReads=-1;
745 // private long mutationsAdded=0;
746
747 private long mutationLengthAdded=0;
748 private long subsAdded=0;
749 private long insAdded=0;
750 private long delsAdded=0;
751 private long junctionsAdded=0;
752
753 private int period=-1;
754
755 private float genomeFraction=1;
756 private long basesRetained;
757
758 private long readsProcessed=0;
759 private long basesProcessed=0;
760
761 private float subRate=0;
762 private float indelRate=0;
763 private int maxIndel=1;
764 private int indelSpacing=10;
765 private boolean banHomopolymers=false;
766 private final float errorRate;
767 private final float errorRate2;
768
769 private final Random randy;
770 private long seed=-1;
771
772 /*--------------------------------------------------------------*/
773
774 /** True if an error was encountered */
775 public boolean errorState=false;
776 /** Overwrite existing output files */
777 private boolean overwrite=true;
778 /** Append to existing output files */
779 private boolean append=false;
780
781 private static final int SUB=Var.SUB, INS=Var.INS, DEL=Var.DEL;
782
783 private java.io.PrintStream outstream=System.err;
784 public static boolean verbose=false;
785
786 }