| 1 |
package cluster; |
| 2 |
|
| 3 |
import java.util.concurrent.atomic.AtomicLong; |
| 4 |
import java.util.concurrent.atomic.AtomicLongArray; |
| 5 |
|
| 6 |
import stream.Read; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
public class Cluster{ |
| 14 |
|
| 15 |
public Cluster(int id_, int k1_, int k2_, int arraylen1_, int arraylen2_){ |
| 16 |
|
| 17 |
id=id_; |
| 18 |
k1=k1_; |
| 19 |
k2=k2_; |
| 20 |
arraylen1=arraylen1_; |
| 21 |
arraylen2=arraylen2_; |
| 22 |
|
| 23 |
kmerArray1=new AtomicLongArray(arraylen1); |
| 24 |
kmerProbArray1=new float[arraylen1]; |
| 25 |
|
| 26 |
kmerArray2=new AtomicLongArray(arraylen2); |
| 27 |
kmerProbArray2=new float[arraylen2]; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public void recalculate(){ |
| 33 |
gc=(float)(gcCount.doubleValue()/baseCount.doubleValue()); |
| 34 |
|
| 35 |
if(k1>0){ |
| 36 |
long kmerCount=0; |
| 37 |
for(int i=0; i<arraylen1; i++){ |
| 38 |
kmerCount+=kmerArray1.get(i); |
| 39 |
} |
| 40 |
double extra=(0.05/arraylen1); |
| 41 |
double mult=(0.95/kmerCount); |
| 42 |
for(int i=0; i<arraylen1; i++){ |
| 43 |
kmerProbArray1[i]=(float)(kmerArray1.get(i)*mult+extra); |
| 44 |
} |
| 45 |
} |
| 46 |
if(k2>0){ |
| 47 |
long kmerCount=0; |
| 48 |
for(int i=0; i<arraylen2; i++){ |
| 49 |
kmerCount+=kmerArray2.get(i); |
| 50 |
} |
| 51 |
double extra=(0.05/arraylen2); |
| 52 |
double mult=(0.95/kmerCount); |
| 53 |
for(int i=0; i<arraylen2; i++){ |
| 54 |
kmerProbArray2[i]=(float)(kmerArray2.get(i)*mult+extra); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public void resetAtomics(){ |
| 60 |
for(int i=0; i<arraylen1; i++){ |
| 61 |
kmerArray1.set(i, 0); |
| 62 |
} |
| 63 |
for(int i=0; i<arraylen2; i++){ |
| 64 |
kmerArray2.set(i, 0); |
| 65 |
} |
| 66 |
depthsum1.set(0); |
| 67 |
depthsum2.set(0); |
| 68 |
readCount.set(0); |
| 69 |
baseCount.set(0); |
| 70 |
gcCount.set(0); |
| 71 |
} |
| 72 |
|
| 73 |
public void add(Read r){ |
| 74 |
if(r==null){return;} |
| 75 |
ReadTag rt=(ReadTag)r.obj; |
| 76 |
assert(rt!=null); |
| 77 |
final byte[] bases=r.bases; |
| 78 |
|
| 79 |
readCount.incrementAndGet(); |
| 80 |
baseCount.addAndGet(bases.length); |
| 81 |
gcCount.addAndGet(rt.gcCount); |
| 82 |
|
| 83 |
if(rt.strand==0){ |
| 84 |
depthsum1.addAndGet(rt.depth); |
| 85 |
}else{ |
| 86 |
depthsum2.addAndGet(rt.depth); |
| 87 |
} |
| 88 |
|
| 89 |
if(k1>0){ |
| 90 |
int[] kmers=rt.kmerArray1(k1); |
| 91 |
int kmer=-1, run=0; |
| 92 |
for(int i=0; i<kmers.length; i++){ |
| 93 |
int x=kmers[i]; |
| 94 |
if(x==kmer){ |
| 95 |
run++; |
| 96 |
}else{ |
| 97 |
if(run>0){kmerArray1.addAndGet(kmer, run);} |
| 98 |
kmer=x; |
| 99 |
run=1; |
| 100 |
} |
| 101 |
} |
| 102 |
if(run>0){kmerArray1.addAndGet(kmer, run);} |
| 103 |
} |
| 104 |
|
| 105 |
if(k2>0){ |
| 106 |
int[] kmers=rt.kmerArray2(k2); |
| 107 |
for(int kmer=0; kmer<kmers.length; kmer++){ |
| 108 |
int x=kmers[kmer]; |
| 109 |
if(x>0){kmerArray2.addAndGet(kmer, x);} |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
public float score(Read r) { |
| 115 |
if(r==null){return 0;} |
| 116 |
return r.mate==null ? scoreSingle(r) : scorePaired(r); |
| 117 |
} |
| 118 |
|
| 119 |
public float scoreSingle(Read r) { |
| 120 |
if(r==null){return 0;} |
| 121 |
ReadTag rt=(ReadTag)r.obj; |
| 122 |
|
| 123 |
assert(false) : "TODO"; |
| 124 |
float depthScore=scoreDepthSingle(rt); |
| 125 |
float gcScore=scoreGcSingle(rt); |
| 126 |
float kmerScore=scoreKmer1(rt); |
| 127 |
assert(false); |
| 128 |
float depthWeight=.2f; |
| 129 |
float gcWeight=.2f; |
| 130 |
float kmerWeight=.6f; |
| 131 |
|
| 132 |
return depthWeight*depthScore+gcWeight*gcScore+kmerWeight*kmerScore; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
private float scoreKmer1(ReadTag rt) { |
| 140 |
int[] kmers=rt.kmerArray1(k1); |
| 141 |
|
| 142 |
float score=0; |
| 143 |
if(scoreMode1==SCORE_MODE_AND){ |
| 144 |
float f=ClusterTools.andCount(kmers, kmerArray1); |
| 145 |
assert(false); |
| 146 |
}else if(scoreMode1==SCORE_MODE_MULT){ |
| 147 |
float f=ClusterTools.innerProduct(kmers, kmerProbArray1); |
| 148 |
assert(false); |
| 149 |
}else{ |
| 150 |
throw new RuntimeException(""+scoreMode1); |
| 151 |
} |
| 152 |
|
| 153 |
return score; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
private float scoreKmer2(ReadTag rt) { |
| 161 |
int[] kmers=rt.kmerArray2(k2); |
| 162 |
float[] probs=rt.kmerFreq2(k2); |
| 163 |
|
| 164 |
float score=0; |
| 165 |
if(scoreMode2==SCORE_MODE_AND){ |
| 166 |
float f=ClusterTools.andCount(kmers, kmerArray2); |
| 167 |
assert(false); |
| 168 |
}else if(scoreMode2==SCORE_MODE_MULT){ |
| 169 |
float f=ClusterTools.innerProduct(kmers, kmerProbArray2); |
| 170 |
assert(false); |
| 171 |
}else if(scoreMode2==SCORE_MODE_DIF){ |
| 172 |
float f=ClusterTools.absDif(probs, kmerProbArray2); |
| 173 |
assert(false); |
| 174 |
}else if(scoreMode2==SCORE_MODE_RMS){ |
| 175 |
float f=ClusterTools.rmsDif(probs, kmerProbArray2); |
| 176 |
assert(false); |
| 177 |
}else if(scoreMode2==SCORE_MODE_KS){ |
| 178 |
float f=ClusterTools.ksFunction(probs, kmerProbArray2); |
| 179 |
assert(false); |
| 180 |
}else{ |
| 181 |
throw new RuntimeException(""+scoreMode2); |
| 182 |
} |
| 183 |
|
| 184 |
return score; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
private float scoreGcSingle(ReadTag rt) { |
| 192 |
assert(false) : "TODO"; |
| 193 |
|
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
private float scoreDepthSingle(ReadTag rt) { |
| 202 |
assert(false) : "TODO"; |
| 203 |
|
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
public float scorePaired(Read r) { |
| 208 |
assert(false) : "TODO"; |
| 209 |
if(r==null){return 0;} |
| 210 |
ReadTag rt=(ReadTag)r.obj; |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public final int id; |
| 220 |
|
| 221 |
|
| 222 |
public final int k1; |
| 223 |
|
| 224 |
public final int k2; |
| 225 |
|
| 226 |
public final int arraylen1; |
| 227 |
public final int arraylen2; |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
public float gc; |
| 232 |
public int depth1, depth2; |
| 233 |
|
| 234 |
final AtomicLongArray kmerArray1; |
| 235 |
final float[] kmerProbArray1; |
| 236 |
|
| 237 |
final AtomicLongArray kmerArray2; |
| 238 |
final float[] kmerProbArray2; |
| 239 |
|
| 240 |
final AtomicLong depthsum1=new AtomicLong(0); |
| 241 |
final AtomicLong depthsum2=new AtomicLong(0); |
| 242 |
|
| 243 |
final AtomicLong readCount=new AtomicLong(0); |
| 244 |
final AtomicLong baseCount=new AtomicLong(0); |
| 245 |
|
| 246 |
final AtomicLong gcCount=new AtomicLong(0); |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public static final int SCORE_MODE_DIF=0; |
| 251 |
public static final int SCORE_MODE_RMS=1; |
| 252 |
public static final int SCORE_MODE_AND=2; |
| 253 |
public static final int SCORE_MODE_MULT=3; |
| 254 |
public static final int SCORE_MODE_KS=4; |
| 255 |
|
| 256 |
public static int scoreMode1=SCORE_MODE_MULT; |
| 257 |
public static int scoreMode2=SCORE_MODE_RMS; |
| 258 |
} |