File: BaseNode.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 (295 lines) | stat: -rwxr-xr-x 8,233 bytes parent folder | download | duplicates (2)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package consensus;

import java.util.Arrays;

import dna.AminoAcid;
import shared.Tools;
import stream.FASTQ;
import structures.ByteBuilder;

/**
 * A placeholder for a base.
 * Tracks counts of bases seen at that position.
 * Maintains edges to observed next bases.
 * 
 * @author Brian Bushnell
 * @date September 6, 2019
 *
 */
public class BaseNode extends BaseGraphPart implements Comparable<BaseNode> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7932097131372307182L;
	
	/*--------------------------------------------------------------*/
	/*----------------        Initialization        ----------------*/
	/*--------------------------------------------------------------*/

	public BaseNode(char refBase_, int type_, int rpos_){
		this((byte)refBase_, type_, rpos_);
	}
	
	public BaseNode(byte refBase_, int type_, int rpos_){
		super(type_);
		refBase=refBase_;
		rpos=rpos_;
		acgtWeight=(type==DEL ? null : new int[4]);
		acgtCount=(type==DEL ? null : new int[4]);
	}
	
	/*--------------------------------------------------------------*/
	/*----------------           Methods            ----------------*/
	/*--------------------------------------------------------------*/
	
	/** Add a traversal of the designated base and quality */
	public void add(byte base, int quality){
		int num=AminoAcid.baseToNumber[base];
		if(num>=0 && type!=DEL){
			acgtWeight[num]+=quality;
			acgtCount[num]++;
		}
		countSum++;
		weightSum+=quality;
	}
	
//	public byte consensus() {
//		assert(type!=DEL);
//		int maxPos=AminoAcid.baseToNumber0[refBase];
//		int max=acgtWeight[maxPos];
//		if(max*2>=weight && AminoAcid.isFullyDefined(refBase)){return refBase;}//Common case
//		if(maxPos<0){maxPos=0;}
//		
//		for(int i=0; i<acgtWeight.length; i++){
//			int weight=acgtWeight[i];
//			int depth=acgtCount[i];
//			if(weight>max && depth>=minDepth){
//				max=weight;
//				maxPos=i;
//			}
//		}
//		return AminoAcid.numberToBase[maxPos];
//	}
	
	public byte consensus(byte[] r) {
		assert(type!=DEL);
		if(onlyConvertNs && type==REF && refBase!='N'){
			r[0]=refBase;
			r[1]=20;
			return refBase;
		}
		int maxPos=AminoAcid.baseToNumber0[refBase];
		int maxWeight=acgtWeight[maxPos];
		int maxDepth=acgtCount[maxPos];
//		long sum=shared.Vector.sum(acgtWeight);
		
		if(acgtWeight[maxPos]*2<weightSum){//Uncommon case of ref being a minority
			for(int i=0; i<acgtWeight.length; i++){
				int x=acgtWeight[i];
				int y=acgtCount[i];
				if(x>maxWeight || (x==maxWeight && y>maxDepth)){
					maxWeight=x;
					maxDepth=y;
					maxPos=i;
				}
			}
		}
//		maxWeight=acgtWeight[maxPos];
//		maxDepth=acgtCount[maxPos];
		
		if(type==REF){
			final byte b=AminoAcid.numberToBase[maxPos];
			float af=maxDepth/(float)countSum;
			float maf=(refBase=='N' ? MAF_noref : MAF_sub);
			if(af<maf || maxDepth<minDepth){
				r[0]=refBase;
				r[1]=(refBase=='N' ? (byte)0 : (byte)2);
			}else{
				r[0]=b;
				double quality=Tools.mid(2, 41, 10*Math.log10(maxWeight/Tools.max(0.01f, weightSum)));
				r[1]=(byte)(quality+FASTQ.ASCII_OFFSET);
			}
			assert(r[0]!='N' || maxDepth<minDepth || af<maf) : "\n"+Arrays.toString(acgtCount)+", max="+maxDepth+", sum="+countSum+", maf="+maf+", af="+af+"\n"
					+ Arrays.toString(acgtWeight)+", max="+maxWeight+", weightSum="+weightSum+"\n";
		}else{
			assert(type==INS);
			r[0]=AminoAcid.numberToBase[maxPos];
			double quality=Tools.mid(2, 41, 10*Math.log10(maxWeight/Tools.max(0.01f, weightSum)));
			r[1]=(byte)(quality+FASTQ.ASCII_OFFSET);
		}
		return r[0];
	}
	
	public float baseProb(byte b){
		assert(acgtProb!=null) : this;
		int x=AminoAcid.baseToNumber[b];
		return x>=0 ? acgtProb[x] : 0.25f;
	}
	
	//inflection at x=.25,y=.25
//	public float baseScore(byte b){
//		float prob=baseProb(b);
//		return prob>0.25f ? prob : 5*prob-1;
//	}

	//inflection at x=.25,y=0.  Range is 1 to -1.
	public float baseScore(byte b){
		float prob=baseProb(b);
//		return prob>0.25f ? (prob-0.25f)*slope : 4*prob-1;
		
//		byte maxBase=maxBase();
//		byte secondBase=secondBase();
//		float prob1=baseProb(maxBase);
//		float prob2=baseProb(secondBase);
		
		return prob;
	}
	
	byte minBase(){
		int idx=0, count=acgtCount[0];
		for(int i=1; i<4; i++){
			if(acgtCount[i]<count){
				idx=i;
				count=acgtCount[i];
			}
		}
		return AminoAcid.numberToBase[idx];
	}
	
	byte maxBase(){
		int idx=0, count=acgtCount[0];
		for(int i=1; i<4; i++){
			if(acgtCount[i]>count){
				idx=i;
				count=acgtCount[i];
			}
		}
		return AminoAcid.numberToBase[idx];
	}
	
	byte secondBase(){
		int idx=0, count=acgtCount[0];
		int idx2=-1, count2=-1;
		for(int i=1; i<4; i++){
			int c=acgtCount[i];
			if(c>count){
				idx2=idx;
				count2=count;
				idx=i;
				count=c;
			}else if(c>count2){
				idx2=i;
				count2=c;
			}
		}
		return AminoAcid.numberToBase[idx2];
	}
	
	/** Add a traversal of the designated quality */
	void increment(byte base, int quality){add(base, quality);}
	
	@Override
	public final String partString(){return "Node";}
	
	@Override
	public ByteBuilder toText(){
		return toTextCount();
	}
	
	public ByteBuilder toTextWeight(){
		ByteBuilder bb=new ByteBuilder();
		bb.append('(');
		bb.append(partString()).comma().append(rpos).comma().append(typeString());
		if(acgtCount==null){bb.append("[]");}
		else {
			for(int i=0; i<4; i++){bb.comma().append(acgtWeight[i]);}
		}
		bb.space();
		if(refEdge!=null){bb.comma().append("REF:").append(refEdge.weightSum);}
		if(insEdge!=null){bb.comma().append("INS:").append(insEdge.weightSum);}
		if(delEdge!=null){bb.comma().append("DEL:").append(delEdge.weightSum);}
		bb.append(')');
		return bb;
	}
	
	public ByteBuilder toTextCount(){
		ByteBuilder bb=new ByteBuilder();
		bb.append('(');
		bb.append(partString()).comma().append(rpos).comma().append(typeString());
		if(acgtCount==null){bb.append("[]");}
		else {
			for(int i=0; i<4; i++){bb.comma().append(acgtCount[i]);}
		}
		bb.space();
		if(refEdge!=null){bb.comma().append("REF:").append(refEdge.countSum);}
		if(insEdge!=null){bb.comma().append("INS:").append(insEdge.countSum);}
		if(delEdge!=null){bb.comma().append("DEL:").append(delEdge.countSum);}
		bb.append(')');
		return bb;
	}
	
	@Override
	public int compareTo(BaseNode b) {
		int dif=weightSum-b.weightSum;
		if(dif!=0){return dif;}
		dif=countSum-b.countSum;
		if(dif!=0){return dif;}
		return type-b.type;
	}
	
	void calcProbs(){
		assert(acgtProb==null);
		acgtProb=new float[4];
		float mult=1f/Tools.max(1, shared.Vector.sum(acgtCount));
		for(int i=0; i<acgtProb.length; i++){
			acgtProb[i]=acgtCount[i]*mult;
		}
	}

	//Difference between first and second most common bases, as a fraction of the total traversals
	public float alleleDif() {
		int max=0, second=0, sum=0;
		for(int x : acgtCount){
			if(x>max){
				second=max;
				max=x;
			}else if(x>second){
				second=x;
			}
			sum+=x;
		}
//		return (max-second)/(float)Tools.max(1, sum);
		return (max-0.5f*second)/(float)Tools.max(1, sum);
//		return max/(float)Tools.max(1, sum);
	}
	
	/*--------------------------------------------------------------*/
	/*----------------            Fields            ----------------*/
	/*--------------------------------------------------------------*/
	
	/** Number of times this node has been traversed */
	public int countSum;
	/** Sum of scores of traversals.  Generally, sum of quality scores of this or adjacent bases.
	 * Probably equal to sum of acgt. */
	public int weightSum;
	
	public final int rpos;
	
	public final byte refBase;
	public final int[] acgtWeight;
	public final int[] acgtCount;
	public float acgtProb[];
	
	//These fields are not really necessary
	public BaseNode refEdge;
	public BaseNode insEdge;
	public BaseNode delEdge;
	
	/*--------------------------------------------------------------*/
	/*----------------           Statics            ----------------*/
	/*--------------------------------------------------------------*/
	
	private static final float slope=(4f/3f);
	
}