File: GenerateMultiChrom.java

package info (click to toggle)
bbmap 39.20%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,024 kB
  • sloc: java: 312,743; sh: 18,099; python: 5,247; ansic: 2,074; perl: 96; makefile: 39; xml: 38
file content (166 lines) | stat: -rwxr-xr-x 3,981 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package pacbio;

import java.io.File;
import java.util.Random;

import dna.AminoAcid;
import dna.ChromosomeArray;
import dna.Data;
import dna.FastaToChromArrays2;
import fileIO.ReadWrite;
import shared.Shared;
import shared.Tools;

/**
 * @author Brian Bushnell
 * @date Jul 16, 2012
 *
 */
public class GenerateMultiChrom {
	
	public static void main(String[] args){
		
		ChromosomeArray cha=null;
		
		try {
			int genomeIn=Integer.parseInt(args[0]);
			Data.setGenome(genomeIn);
			cha=Data.getChromosome(1);
			Data.unload(1, true);
		} catch (NumberFormatException e) {
			//ignore
		}
		
		if(cha==null){
			String inname=args[0];
			cha=ChromosomeArray.read(inname, 1);
		}
		
		assert(cha!=null);
		
		int copies=Integer.parseInt(args[1]);
		int build=Integer.parseInt(args[2]);
		
		int mincontig=-1;
		int maxcontig=-1;
		int buffer=-1;
		if(args.length>3){
			mincontig=Integer.parseInt(args[3]);
			maxcontig=Integer.parseInt(args[4]);
			buffer=Integer.parseInt(args[5]);
			System.out.println("Multichrom will be overlayed with blocks of "+buffer+" 'N'");
		}
		
		
//		String pattern=ROOT_GENOME+GENOME_BUILD+"/chr"+chrom+".chromC";
		
		File f=new File(Data.ROOT_GENOME+build);
		if(!f.exists()){f.mkdirs();}
		
		for(int i=1; i<=copies; i++){
			ChromosomeArray chb=makeSynthetic(cha, i);
			if(buffer>0){
				addN(chb, mincontig, maxcontig, buffer);
			}
			ReadWrite.write(chb, Data.ROOT_GENOME+build+"/chr"+i+Data.chromExtension(), false);
		}
		FastaToChromArrays2.writeInfo(build, copies, Data.name, "multiple_"+Data.GENOME_BUILD, false, false);
		
	}
	
	private static void addN(ChromosomeArray cha, int minContig, int maxContig, int buffer){
		
		final int spread=maxContig-minContig+1;
		final Random randy=new Random(cha.chromosome);
		final int lim=cha.maxIndex-Tools.max(maxContig, minContig+buffer);

		int contig=0;
		int nextContig=minContig+randy.nextInt(spread);
		
		for(int i=0; i<lim; i++){
			byte b=cha.get(i);
			if(b=='N'){contig=0;}
			else{
				contig++;
				if(contig>=nextContig){
					contig=0;
					int lim2=i+buffer;
					while(i<lim2){
						cha.set(i, 'N');
						i++;
					}
					nextContig=minContig+(randy.nextInt(spread)+randy.nextInt(spread))/2;
				}
			}
		}
	}

	/**
	 * @param cha
	 * @param i
	 * @return
	 */
	private static ChromosomeArray makeSynthetic(ChromosomeArray cha, int chrom) {
//		assert(false) : cha.array.length+", "+cha.maxIndex;
		ChromosomeArray chb=new ChromosomeArray(chrom, Shared.PLUS, cha.minIndex, cha.array.length+40);
		chb.maxIndex=-1;
		
		int dif=0;
		final int MIN_DIF=-12;
		final int MAX_DIF=12;
		final int INDEL_PERCENT=10;
		final int SUB_PERCENT=1;
		final int ERROR_PERCENT=INDEL_PERCENT+SUB_PERCENT;
		final int ERROR_LENGTH=3;
		
		Random randy=new Random(chrom);
		
		int a=cha.minIndex;
		int b=chb.minIndex;
		
		while(a<=cha.array.length){
			byte c=cha.get(a);
			int x=(c=='N' ? 100 : randy.nextInt(100));
			if(x>=ERROR_PERCENT){ //No error
				chb.set(b, c);
				a++;
				b++;
			}else if(x>=INDEL_PERCENT){//sub
				byte e=c;
				while(e==c){
					e=AminoAcid.numberToBase[randy.nextInt(4)];
				}
				chb.set(b, e);
				a++;
				b++;
			}else{//indel
				boolean ins=Tools.nextBoolean(randy);
				int len=Tools.min(randy.nextInt(ERROR_LENGTH), randy.nextInt(ERROR_LENGTH), randy.nextInt(ERROR_LENGTH+1))+1;
				if(ins && dif+len>MAX_DIF){
					ins=false;
				}else if(!ins && dif-len<MIN_DIF){
					ins=true;
				}
				
				if(ins){
					for(int i=0; i<len; i++){
						boolean same=randy.nextFloat()<0.6f; //Additional 60% chance that inserted base will be a duplicate of an existing base
						int n=randy.nextInt(4);
						byte e=(same ? c : AminoAcid.numberToBase[n]);
						chb.set(b, e);
						b++;
						dif++;
					}
				}else{
					a+=len;
					dif-=len;
				}
			}
		}
		
		return chb;
	}
	
	
	
}