| 1 |
package consensus; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
|
| 5 |
import dna.AminoAcid; |
| 6 |
import shared.Tools; |
| 7 |
import stream.FASTQ; |
| 8 |
import structures.ByteBuilder; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public class BaseNode extends BaseGraphPart implements Comparable<BaseNode> { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
private static final long serialVersionUID = 7932097131372307182L; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public BaseNode(char refBase_, int type_, int rpos_){ |
| 31 |
this((byte)refBase_, type_, rpos_); |
| 32 |
} |
| 33 |
|
| 34 |
public BaseNode(byte refBase_, int type_, int rpos_){ |
| 35 |
super(type_); |
| 36 |
refBase=refBase_; |
| 37 |
rpos=rpos_; |
| 38 |
acgtWeight=(type==DEL ? null : new int[4]); |
| 39 |
acgtCount=(type==DEL ? null : new int[4]); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public void add(byte base, int quality){ |
| 48 |
int num=AminoAcid.baseToNumber[base]; |
| 49 |
if(num>=0 && type!=DEL){ |
| 50 |
acgtWeight[num]+=quality; |
| 51 |
acgtCount[num]++; |
| 52 |
} |
| 53 |
countSum++; |
| 54 |
weightSum+=quality; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public byte consensus(byte[] r) { |
| 76 |
assert(type!=DEL); |
| 77 |
if(onlyConvertNs && type==REF && refBase!='N'){ |
| 78 |
r[0]=refBase; |
| 79 |
r[1]=20; |
| 80 |
return refBase; |
| 81 |
} |
| 82 |
int maxPos=AminoAcid.baseToNumber0[refBase]; |
| 83 |
int maxWeight=acgtWeight[maxPos]; |
| 84 |
int maxDepth=acgtCount[maxPos]; |
| 85 |
|
| 86 |
|
| 87 |
if(acgtWeight[maxPos]*2<weightSum){ |
| 88 |
for(int i=0; i<acgtWeight.length; i++){ |
| 89 |
int x=acgtWeight[i]; |
| 90 |
int y=acgtCount[i]; |
| 91 |
if(x>maxWeight || (x==maxWeight && y>maxDepth)){ |
| 92 |
maxWeight=x; |
| 93 |
maxDepth=y; |
| 94 |
maxPos=i; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
if(type==REF){ |
| 102 |
final byte b=AminoAcid.numberToBase[maxPos]; |
| 103 |
float af=maxDepth/(float)countSum; |
| 104 |
float maf=(refBase=='N' ? MAF_noref : MAF_sub); |
| 105 |
if(af<maf || maxDepth<minDepth){ |
| 106 |
r[0]=refBase; |
| 107 |
r[1]=(refBase=='N' ? (byte)0 : (byte)2); |
| 108 |
}else{ |
| 109 |
r[0]=b; |
| 110 |
double quality=Tools.mid(2, 41, 10*Math.log10(maxWeight/Tools.max(0.01f, weightSum))); |
| 111 |
r[1]=(byte)(quality+FASTQ.ASCII_OFFSET); |
| 112 |
} |
| 113 |
assert(r[0]!='N' || maxDepth<minDepth || af<maf) : "\n"+Arrays.toString(acgtCount)+", max="+maxDepth+", sum="+countSum+", maf="+maf+", af="+af+"\n" |
| 114 |
+ Arrays.toString(acgtWeight)+", max="+maxWeight+", weightSum="+weightSum+"\n"; |
| 115 |
}else{ |
| 116 |
assert(type==INS); |
| 117 |
r[0]=AminoAcid.numberToBase[maxPos]; |
| 118 |
double quality=Tools.mid(2, 41, 10*Math.log10(maxWeight/Tools.max(0.01f, weightSum))); |
| 119 |
r[1]=(byte)(quality+FASTQ.ASCII_OFFSET); |
| 120 |
} |
| 121 |
return r[0]; |
| 122 |
} |
| 123 |
|
| 124 |
public float baseProb(byte b){ |
| 125 |
assert(acgtProb!=null) : this; |
| 126 |
int x=AminoAcid.baseToNumber[b]; |
| 127 |
return x>=0 ? acgtProb[x] : 0.25f; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public float baseScore(byte b){ |
| 138 |
float prob=baseProb(b); |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
return prob; |
| 147 |
} |
| 148 |
|
| 149 |
byte minBase(){ |
| 150 |
int idx=0, count=acgtCount[0]; |
| 151 |
for(int i=1; i<4; i++){ |
| 152 |
if(acgtCount[i]<count){ |
| 153 |
idx=i; |
| 154 |
count=acgtCount[i]; |
| 155 |
} |
| 156 |
} |
| 157 |
return AminoAcid.numberToBase[idx]; |
| 158 |
} |
| 159 |
|
| 160 |
byte maxBase(){ |
| 161 |
int idx=0, count=acgtCount[0]; |
| 162 |
for(int i=1; i<4; i++){ |
| 163 |
if(acgtCount[i]>count){ |
| 164 |
idx=i; |
| 165 |
count=acgtCount[i]; |
| 166 |
} |
| 167 |
} |
| 168 |
return AminoAcid.numberToBase[idx]; |
| 169 |
} |
| 170 |
|
| 171 |
byte secondBase(){ |
| 172 |
int idx=0, count=acgtCount[0]; |
| 173 |
int idx2=-1, count2=-1; |
| 174 |
for(int i=1; i<4; i++){ |
| 175 |
int c=acgtCount[i]; |
| 176 |
if(c>count){ |
| 177 |
idx2=idx; |
| 178 |
count2=count; |
| 179 |
idx=i; |
| 180 |
count=c; |
| 181 |
}else if(c>count2){ |
| 182 |
idx2=i; |
| 183 |
count2=c; |
| 184 |
} |
| 185 |
} |
| 186 |
return AminoAcid.numberToBase[idx2]; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
void increment(byte base, int quality){add(base, quality);} |
| 191 |
|
| 192 |
@Override |
| 193 |
public final String partString(){return "Node";} |
| 194 |
|
| 195 |
@Override |
| 196 |
public ByteBuilder toText(){ |
| 197 |
return toTextCount(); |
| 198 |
} |
| 199 |
|
| 200 |
public ByteBuilder toTextWeight(){ |
| 201 |
ByteBuilder bb=new ByteBuilder(); |
| 202 |
bb.append('('); |
| 203 |
bb.append(partString()).comma().append(rpos).comma().append(typeString()); |
| 204 |
if(acgtCount==null){bb.append("[]");} |
| 205 |
else { |
| 206 |
for(int i=0; i<4; i++){bb.comma().append(acgtWeight[i]);} |
| 207 |
} |
| 208 |
bb.space(); |
| 209 |
if(refEdge!=null){bb.comma().append("REF:").append(refEdge.weightSum);} |
| 210 |
if(insEdge!=null){bb.comma().append("INS:").append(insEdge.weightSum);} |
| 211 |
if(delEdge!=null){bb.comma().append("DEL:").append(delEdge.weightSum);} |
| 212 |
bb.append(')'); |
| 213 |
return bb; |
| 214 |
} |
| 215 |
|
| 216 |
public ByteBuilder toTextCount(){ |
| 217 |
ByteBuilder bb=new ByteBuilder(); |
| 218 |
bb.append('('); |
| 219 |
bb.append(partString()).comma().append(rpos).comma().append(typeString()); |
| 220 |
if(acgtCount==null){bb.append("[]");} |
| 221 |
else { |
| 222 |
for(int i=0; i<4; i++){bb.comma().append(acgtCount[i]);} |
| 223 |
} |
| 224 |
bb.space(); |
| 225 |
if(refEdge!=null){bb.comma().append("REF:").append(refEdge.countSum);} |
| 226 |
if(insEdge!=null){bb.comma().append("INS:").append(insEdge.countSum);} |
| 227 |
if(delEdge!=null){bb.comma().append("DEL:").append(delEdge.countSum);} |
| 228 |
bb.append(')'); |
| 229 |
return bb; |
| 230 |
} |
| 231 |
|
| 232 |
@Override |
| 233 |
public int compareTo(BaseNode b) { |
| 234 |
int dif=weightSum-b.weightSum; |
| 235 |
if(dif!=0){return dif;} |
| 236 |
dif=countSum-b.countSum; |
| 237 |
if(dif!=0){return dif;} |
| 238 |
return type-b.type; |
| 239 |
} |
| 240 |
|
| 241 |
void calcProbs(){ |
| 242 |
assert(acgtProb==null); |
| 243 |
acgtProb=new float[4]; |
| 244 |
float mult=1f/Tools.max(1, shared.Vector.sum(acgtCount)); |
| 245 |
for(int i=0; i<acgtProb.length; i++){ |
| 246 |
acgtProb[i]=acgtCount[i]*mult; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
public float alleleDif() { |
| 252 |
int max=0, second=0, sum=0; |
| 253 |
for(int x : acgtCount){ |
| 254 |
if(x>max){ |
| 255 |
second=max; |
| 256 |
max=x; |
| 257 |
}else if(x>second){ |
| 258 |
second=x; |
| 259 |
} |
| 260 |
sum+=x; |
| 261 |
} |
| 262 |
|
| 263 |
return (max-0.5f*second)/(float)Tools.max(1, sum); |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public int countSum; |
| 273 |
|
| 274 |
|
| 275 |
public int weightSum; |
| 276 |
|
| 277 |
public final int rpos; |
| 278 |
|
| 279 |
public final byte refBase; |
| 280 |
public final int[] acgtWeight; |
| 281 |
public final int[] acgtCount; |
| 282 |
public float acgtProb[]; |
| 283 |
|
| 284 |
|
| 285 |
public BaseNode refEdge; |
| 286 |
public BaseNode insEdge; |
| 287 |
public BaseNode delEdge; |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
private static final float slope=(4f/3f); |
| 294 |
|
| 295 |
} |