| 1 |
package tracker; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
|
| 5 |
import dna.AminoAcid; |
| 6 |
import shared.KillSwitch; |
| 7 |
import shared.Tools; |
| 8 |
import stream.Read; |
| 9 |
import structures.LongList; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public class PolymerTracker { |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public PolymerTracker(){ |
| 25 |
reset(); |
| 26 |
} |
| 27 |
|
| 28 |
public void reset(){ |
| 29 |
Arrays.fill(maxACGTN, 0); |
| 30 |
for(int i=0; i<5; i++){ |
| 31 |
countsACGTN[i]=new LongList(); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public void addPair(Read r){ |
| 40 |
if(r==null){return;} |
| 41 |
add(r.bases); |
| 42 |
add(r.mate); |
| 43 |
} |
| 44 |
|
| 45 |
public void add(Read r){ |
| 46 |
if(r==null){return;} |
| 47 |
add(r.bases); |
| 48 |
} |
| 49 |
|
| 50 |
public void add(PolymerTracker pt){ |
| 51 |
for(int i=0; i<5; i++){ |
| 52 |
LongList list=countsACGTN[i]; |
| 53 |
LongList ptList=pt.countsACGTN[i]; |
| 54 |
for(int len=0; len<ptList.size; len++){ |
| 55 |
long count=ptList.get(len); |
| 56 |
list.increment(len, count); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public void add(byte[] bases){ |
| 62 |
if(bases==null || bases.length<1){return;} |
| 63 |
if(PER_SEQUENCE){ |
| 64 |
addPerSequence(bases); |
| 65 |
}else{ |
| 66 |
addPerPolymer(bases); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public LongList[] accumulate(){ |
| 75 |
if(cumulativeACGTN!=null){return cumulativeACGTN;} |
| 76 |
LongList[] sums=new LongList[5]; |
| 77 |
for(int i=0; i<5; i++){ |
| 78 |
LongList list=countsACGTN[i]; |
| 79 |
LongList sumList=new LongList(list.size); |
| 80 |
for(int len=list.size-1; len>=0; len--){ |
| 81 |
sumList.set(len, sumList.get(len+1)+list.get(len)); |
| 82 |
} |
| 83 |
sums[i]=sumList; |
| 84 |
} |
| 85 |
cumulativeACGTN=sums; |
| 86 |
return sums; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
public String toHistogram(){ |
| 91 |
StringBuilder sb=new StringBuilder(); |
| 92 |
sb.append("#Length\tA\tC\tG\tT\tN\n"); |
| 93 |
|
| 94 |
final int maxIndex=longest(); |
| 95 |
for(int len=0; len<maxIndex; len++){ |
| 96 |
sb.append(len); |
| 97 |
for(int i=0; i<5; i++){ |
| 98 |
long count=countsACGTN[i].get(len); |
| 99 |
sb.append('\t').append(count); |
| 100 |
} |
| 101 |
sb.append('\n'); |
| 102 |
} |
| 103 |
return sb.toString(); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
public String toHistogramCumulative(){ |
| 108 |
LongList[] sums=accumulate(); |
| 109 |
|
| 110 |
StringBuilder sb=new StringBuilder(); |
| 111 |
sb.append("#Length\tA\tC\tG\tT\tN\n"); |
| 112 |
|
| 113 |
final int maxIndex=longest(); |
| 114 |
for(int len=0; len<maxIndex; len++){ |
| 115 |
sb.append(len); |
| 116 |
for(int i=0; i<5; i++){ |
| 117 |
long count=sums[i].get(len); |
| 118 |
sb.append('\t').append(count); |
| 119 |
} |
| 120 |
sb.append('\n'); |
| 121 |
} |
| 122 |
return sb.toString(); |
| 123 |
} |
| 124 |
|
| 125 |
public double calcRatio(byte base1, byte base2, int length){ |
| 126 |
long count1=getCount(base1, length); |
| 127 |
long count2=getCount(base2, length); |
| 128 |
return count1/Tools.max(1.0, count2); |
| 129 |
} |
| 130 |
|
| 131 |
public long getCount(byte base, int length) { |
| 132 |
int x=AminoAcid.baseToNumberACGTN[base]; |
| 133 |
return countsACGTN[x].get(length); |
| 134 |
} |
| 135 |
|
| 136 |
public double calcRatioCumulative(byte base1, byte base2, int length){ |
| 137 |
long count1=getCountCumulative(base1, length); |
| 138 |
long count2=getCountCumulative(base2, length); |
| 139 |
return count1/Tools.max(1.0, count2); |
| 140 |
} |
| 141 |
|
| 142 |
public long getCountCumulative(byte base, int length) { |
| 143 |
int x=AminoAcid.baseToNumberACGTN[base]; |
| 144 |
return accumulate()[x].get(length); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
private void addPerSequence(byte[] bases){ |
| 154 |
Arrays.fill(maxACGTN, 0); |
| 155 |
byte prev=-1; |
| 156 |
int current=0; |
| 157 |
for(byte b : bases){ |
| 158 |
if(b==prev){ |
| 159 |
current++; |
| 160 |
}else{ |
| 161 |
recordMax(prev, current); |
| 162 |
prev=b; |
| 163 |
current=1; |
| 164 |
} |
| 165 |
} |
| 166 |
recordMax(prev, current); |
| 167 |
|
| 168 |
for(int i=0; i<maxACGTN.length; i++){ |
| 169 |
countsACGTN[i].increment(maxACGTN[i], 1); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
private void addPerPolymer(byte[] bases){ |
| 175 |
byte prev=-1; |
| 176 |
int current=0; |
| 177 |
for(byte b : bases){ |
| 178 |
if(b==prev){ |
| 179 |
current++; |
| 180 |
}else{ |
| 181 |
recordCounts(prev, current); |
| 182 |
prev=b; |
| 183 |
current=1; |
| 184 |
} |
| 185 |
} |
| 186 |
recordCounts(prev, current); |
| 187 |
} |
| 188 |
|
| 189 |
private void recordMax(byte base, int len){ |
| 190 |
if(base<0){return;} |
| 191 |
int x=AminoAcid.baseToNumberACGTN[base]; |
| 192 |
if(x<0){return;} |
| 193 |
maxACGTN[x]=Tools.max(maxACGTN[x], len); |
| 194 |
} |
| 195 |
|
| 196 |
private void recordCounts(byte base, int len){ |
| 197 |
if(base<0){return;} |
| 198 |
int x=AminoAcid.baseToNumberACGTN[base]; |
| 199 |
if(x<0){return;} |
| 200 |
countsACGTN[x].increment(len, 1); |
| 201 |
} |
| 202 |
|
| 203 |
private int longest(){ |
| 204 |
int max=0; |
| 205 |
for(LongList list : countsACGTN){ |
| 206 |
max=Tools.max(list.size(), max); |
| 207 |
} |
| 208 |
return max; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
private final int[] maxACGTN=KillSwitch.allocInt1D(5); |
| 216 |
final LongList[] countsACGTN=new LongList[5]; |
| 217 |
private LongList[] cumulativeACGTN; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public static boolean PER_SEQUENCE=true; |
| 224 |
public static boolean CUMULATIVE=true; |
| 225 |
|
| 226 |
} |