| 1 |
package hiseq; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Arrays; |
| 5 |
import java.util.Collections; |
| 6 |
|
| 7 |
import shared.Tools; |
| 8 |
import structures.FloatList; |
| 9 |
import structures.Point; |
| 10 |
|
| 11 |
public class FlowCell { |
| 12 |
|
| 13 |
public FlowCell(String fname){ |
| 14 |
TileDump.loadDump(fname, this); |
| 15 |
} |
| 16 |
|
| 17 |
public FlowCell(int k_){k=k_;} |
| 18 |
|
| 19 |
public MicroTile getMicroTile(String id) { |
| 20 |
ihp.parse(id); |
| 21 |
return getMicroTile(ihp); |
| 22 |
} |
| 23 |
|
| 24 |
public MicroTile getMicroTile(IlluminaHeaderParser2 ihp){ |
| 25 |
return getLane(ihp.lane()).getMicroTile(ihp.tile(), ihp.xPos(), ihp.yPos(), true); |
| 26 |
} |
| 27 |
|
| 28 |
public MicroTile getMicroTile(int lane, int tile, int x, int y){ |
| 29 |
return getLane(lane).getMicroTile(tile, x, y, true); |
| 30 |
} |
| 31 |
|
| 32 |
public MicroTile getMicroTile(int lane, int tile, int x, int y, boolean create){ |
| 33 |
return getLane(lane).getMicroTile(tile, x, y, create); |
| 34 |
} |
| 35 |
|
| 36 |
public Lane getLane(int lane){ |
| 37 |
while(lanes.size()<=lane){lanes.add(new Lane(lanes.size()));} |
| 38 |
return lanes.get(lane); |
| 39 |
} |
| 40 |
|
| 41 |
public ArrayList<MicroTile> toList() { |
| 42 |
ArrayList<MicroTile> mtList=new ArrayList<MicroTile>(); |
| 43 |
for(Lane lane : lanes){ |
| 44 |
if(lane!=null){ |
| 45 |
for(Tile tile : lane.tiles){ |
| 46 |
if(tile!=null){ |
| 47 |
for(ArrayList<MicroTile> ylist : tile.xlist){ |
| 48 |
if(ylist!=null){ |
| 49 |
for(MicroTile mt : ylist){ |
| 50 |
if(mt!=null){ |
| 51 |
mtList.add(mt); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
return mtList; |
| 61 |
} |
| 62 |
|
| 63 |
public ArrayList<MicroTile> calcStats(){ |
| 64 |
ArrayList<MicroTile> mtList=toList(); |
| 65 |
readsProcessed=basesProcessed=0; |
| 66 |
readsAligned=basesAligned=0; |
| 67 |
readErrors=baseErrors=0; |
| 68 |
for(MicroTile mt : mtList){ |
| 69 |
mt.process(); |
| 70 |
readsProcessed+=mt.readCount; |
| 71 |
basesProcessed+=mt.baseCount; |
| 72 |
readsAligned+=mt.alignedReadCount; |
| 73 |
basesAligned+=mt.alignedBaseCount; |
| 74 |
readErrors+=mt.readErrorCount; |
| 75 |
baseErrors+=mt.baseErrorCount; |
| 76 |
} |
| 77 |
final double mtDiv=Tools.max(1, mtList.size()); |
| 78 |
avgReads=readsProcessed/mtDiv; |
| 79 |
avgAlignedReads=readsAligned/mtDiv; |
| 80 |
minCountToUse=(long)Tools.min(500, avgReads*0.25f); |
| 81 |
int toKeep=0; |
| 82 |
for(MicroTile mt : mtList){ |
| 83 |
if(mt.readCount>=minCountToUse){toKeep++;} |
| 84 |
} |
| 85 |
|
| 86 |
FloatList avgQualityList=new FloatList(toKeep); |
| 87 |
FloatList avgUniqueList=new FloatList(toKeep); |
| 88 |
FloatList avgDepthList=new FloatList(toKeep); |
| 89 |
FloatList avgErrorFreeList=new FloatList(toKeep); |
| 90 |
FloatList avgPolyGList=new FloatList(toKeep); |
| 91 |
FloatList avgGList=new FloatList(toKeep); |
| 92 |
|
| 93 |
|
| 94 |
ArrayList<Point> readPoints=new ArrayList<Point>(); |
| 95 |
ArrayList<Point> basePoints=new ArrayList<Point>(); |
| 96 |
|
| 97 |
for(MicroTile mt : mtList){ |
| 98 |
if(mt!=null && mt.readCount>=minCountToUse){ |
| 99 |
double up=mt.uniquePercent(); |
| 100 |
double rer=mt.readErrorRate(); |
| 101 |
double ber=mt.baseErrorRate(); |
| 102 |
avgQualityList.add((float)mt.averageReadQualityByProb()); |
| 103 |
avgUniqueList.add((float)up); |
| 104 |
avgDepthList.add((float)mt.depth()); |
| 105 |
avgErrorFreeList.add((float)mt.percentErrorFree()); |
| 106 |
avgPolyGList.add((float)mt.polyGPercent()); |
| 107 |
avgGList.add((float)mt.avgG()); |
| 108 |
readPoints.add(new Point(up, rer)); |
| 109 |
basePoints.add(new Point(up, Math.sqrt(ber))); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
if(readsAligned>1000) { |
| 114 |
Collections.sort(readPoints); |
| 115 |
Collections.sort(basePoints); |
| 116 |
uniqueToReadErrorRateFormula=Tools.linearRegression(readPoints, 0.001, 0.999); |
| 117 |
uniqueToBaseErrorRateFormula=Tools.linearRegression(basePoints, 0.001, 0.999); |
| 118 |
|
| 119 |
|
| 120 |
}else { |
| 121 |
|
| 122 |
readPoints=basePoints=null; |
| 123 |
uniqueToReadErrorRateFormula=uniqueToBaseErrorRateFormula=null; |
| 124 |
} |
| 125 |
|
| 126 |
avgQuality=avgQualityList.mean(); |
| 127 |
avgUnique=avgUniqueList.mean(); |
| 128 |
avgDepth=avgDepthList.mean(); |
| 129 |
avgErrorFree=avgErrorFreeList.mean(); |
| 130 |
avgPolyG=avgPolyGList.mean(); |
| 131 |
avgG=avgGList.mean(); |
| 132 |
|
| 133 |
stdQuality=avgQualityList.stdev(); |
| 134 |
stdUnique=avgUniqueList.stdev(); |
| 135 |
stdDepth=avgDepthList.stdev(); |
| 136 |
stdErrorFree=avgErrorFreeList.stdev(); |
| 137 |
stdPolyG=avgPolyGList.stdev(); |
| 138 |
stdG=avgGList.stdev(); |
| 139 |
|
| 140 |
return mtList; |
| 141 |
} |
| 142 |
|
| 143 |
public FlowCell widenToTargetReads(int targetReads){ |
| 144 |
if(readsProcessed<1){ |
| 145 |
System.err.println("Warning: Zero reads processed."); |
| 146 |
return this; |
| 147 |
} |
| 148 |
if(readsProcessed<targetReads){ |
| 149 |
return this; |
| 150 |
} |
| 151 |
FlowCell fc=this; |
| 152 |
while(fc.avgReads<targetReads){ |
| 153 |
FlowCell fc2=fc.widen(true); |
| 154 |
fc2.calcStats(); |
| 155 |
if(fc2.avgReads<=fc.avgReads){ |
| 156 |
unwiden(); |
| 157 |
return fc; |
| 158 |
} |
| 159 |
fc=fc2; |
| 160 |
} |
| 161 |
return fc.setFrom(this); |
| 162 |
} |
| 163 |
|
| 164 |
public FlowCell widenToTargetAlignedReads(int targetAlignedReads){ |
| 165 |
if(readsAligned<1){ |
| 166 |
System.err.println("Warning: Zero aligned reads processed."); |
| 167 |
return this; |
| 168 |
} |
| 169 |
if(readsAligned<targetAlignedReads){ |
| 170 |
System.err.println("Warning: Below target aligned reads ("+readsAligned+"<"+targetAlignedReads+")"); |
| 171 |
return this; |
| 172 |
} |
| 173 |
FlowCell fc=this; |
| 174 |
while(fc.avgAlignedReads<targetAlignedReads){ |
| 175 |
FlowCell fc2=fc.widen(false); |
| 176 |
fc2.calcStats(); |
| 177 |
if(fc2.avgAlignedReads<=fc.avgAlignedReads){ |
| 178 |
unwiden(); |
| 179 |
return fc; |
| 180 |
} |
| 181 |
fc=fc2; |
| 182 |
} |
| 183 |
return fc.setFrom(this); |
| 184 |
} |
| 185 |
|
| 186 |
public void unwiden(){ |
| 187 |
if(Tile.xSize>Tile.ySize){Tile.ySize/=2;} |
| 188 |
else{Tile.xSize/=2;} |
| 189 |
} |
| 190 |
|
| 191 |
public FlowCell widen(boolean loud){ |
| 192 |
final int x=Tile.xSize, y=Tile.ySize; |
| 193 |
final int x2=x>=y ? x : 2*x; |
| 194 |
final int y2=x>=y ? 2*y : y; |
| 195 |
return widen(x2, y2, loud); |
| 196 |
} |
| 197 |
|
| 198 |
public FlowCell widen(int x2, int y2, boolean loud){ |
| 199 |
|
| 200 |
if(x2<=Tile.xSize && y2<=Tile.ySize) {return this;} |
| 201 |
Tile.xSize=Tools.max(x2, Tile.xSize); |
| 202 |
Tile.ySize=Tools.max(y2, Tile.ySize); |
| 203 |
if(loud) {System.err.println("Widening to "+Tile.xSize+"x"+Tile.ySize);} |
| 204 |
ArrayList<MicroTile> list=toList(); |
| 205 |
FlowCell fc=new FlowCell(k).setFrom(this); |
| 206 |
for(MicroTile mt : list){ |
| 207 |
MicroTile mt2=fc.getMicroTile(mt.lane, mt.tile, mt.x1, mt.y1); |
| 208 |
mt2.add(mt); |
| 209 |
} |
| 210 |
for(Lane lane : lanes) { |
| 211 |
if(lane!=null) { |
| 212 |
Lane lane2=fc.getLane(lane.lane); |
| 213 |
lane2.addLists(lane); |
| 214 |
} |
| 215 |
} |
| 216 |
return fc; |
| 217 |
} |
| 218 |
|
| 219 |
public void blur() { |
| 220 |
FlowCell fc=new FlowCell(k); |
| 221 |
fc.add(this); |
| 222 |
ArrayList<MicroTile> list=this.toList(); |
| 223 |
for(MicroTile mt : list) { |
| 224 |
if(mt.readCount<1) {continue;} |
| 225 |
int tile=mt.tile, lane=mt.lane, x1=mt.x1, x2=mt.x2, y1=mt.y1, y2=mt.y2; |
| 226 |
int x0=x1-1, y0=y1-1, x3=x2+1, y3=y2+1; |
| 227 |
|
| 228 |
MicroTile up=(y0<0 ? null : fc.getMicroTile(lane, tile, x1, y0, false)); |
| 229 |
MicroTile down=fc.getMicroTile(lane, tile, x1, y3, false); |
| 230 |
MicroTile left=(x0<0 ? null : fc.getMicroTile(lane, tile, x0, y1, false)); |
| 231 |
MicroTile right=fc.getMicroTile(lane, tile, x3, y1, false); |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
mt.multiplyBy(8); |
| 245 |
if(up!=null) {mt.add(up);} |
| 246 |
if(down!=null) {mt.add(down);} |
| 247 |
if(left!=null) {mt.add(left);} |
| 248 |
if(right!=null) {mt.add(right);} |
| 249 |
mt.multiplyBy(0.125); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
public void dump(String fname, boolean overwrite) { |
| 254 |
TileDump.write(this, fname, overwrite); |
| 255 |
} |
| 256 |
|
| 257 |
public void add(FlowCell fcb) { |
| 258 |
for(Lane b : fcb.lanes) { |
| 259 |
if(b!=null) { |
| 260 |
synchronized(b) { |
| 261 |
Lane a=getLane(b.lane); |
| 262 |
synchronized(a) { |
| 263 |
a.add(b); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
FlowCell setFrom(FlowCell fc) { |
| 271 |
readsProcessed=fc.readsProcessed; |
| 272 |
basesProcessed=fc.basesProcessed; |
| 273 |
readsAligned=fc.readsAligned; |
| 274 |
basesAligned=fc.basesAligned; |
| 275 |
readErrors=fc.readErrors; |
| 276 |
baseErrors=fc.baseErrors; |
| 277 |
|
| 278 |
name=fc.name; |
| 279 |
xMin=fc.xMin; |
| 280 |
xMax=fc.xMax; |
| 281 |
yMin=fc.yMin; |
| 282 |
yMax=fc.yMax; |
| 283 |
tMin=fc.tMin; |
| 284 |
tMax=fc.tMax; |
| 285 |
k=fc.k; |
| 286 |
|
| 287 |
uniqueToReadErrorRateFormula=fc.uniqueToReadErrorRateFormula==null ? null : |
| 288 |
Arrays.copyOf(fc.uniqueToReadErrorRateFormula, 2); |
| 289 |
uniqueToBaseErrorRateFormula=fc.uniqueToBaseErrorRateFormula==null ? null : |
| 290 |
Arrays.copyOf(fc.uniqueToBaseErrorRateFormula, 2); |
| 291 |
return this; |
| 292 |
} |
| 293 |
|
| 294 |
public double alignmentRate() { |
| 295 |
return readsAligned/(double)readsProcessed; |
| 296 |
} |
| 297 |
|
| 298 |
public double baseErrorRate() { |
| 299 |
return baseErrors/(double)(Tools.max(basesAligned, 1)); |
| 300 |
} |
| 301 |
|
| 302 |
public ArrayList<Lane> lanes=new ArrayList<Lane>(); |
| 303 |
|
| 304 |
long readsProcessed; |
| 305 |
long basesProcessed; |
| 306 |
long readsAligned; |
| 307 |
long basesAligned; |
| 308 |
long readErrors; |
| 309 |
long baseErrors; |
| 310 |
|
| 311 |
String name; |
| 312 |
|
| 313 |
int xMin=-1; |
| 314 |
int xMax=-1; |
| 315 |
int yMin=-1; |
| 316 |
int yMax=-1; |
| 317 |
int tMin=-1; |
| 318 |
int tMax=-1; |
| 319 |
int k=31; |
| 320 |
|
| 321 |
public double avgReads; |
| 322 |
public double avgAlignedReads; |
| 323 |
public double minCountToUse; |
| 324 |
|
| 325 |
public double avgQuality; |
| 326 |
public double avgUnique; |
| 327 |
public double avgDepth; |
| 328 |
public double avgErrorFree; |
| 329 |
public double avgPolyG; |
| 330 |
public double avgG; |
| 331 |
|
| 332 |
public double stdQuality; |
| 333 |
public double stdUnique; |
| 334 |
public double stdDepth; |
| 335 |
public double stdErrorFree; |
| 336 |
public double stdPolyG; |
| 337 |
public double stdG; |
| 338 |
|
| 339 |
public double[] uniqueToReadErrorRateFormula; |
| 340 |
public double[] uniqueToBaseErrorRateFormula; |
| 341 |
|
| 342 |
private IlluminaHeaderParser2 ihp=new IlluminaHeaderParser2(); |
| 343 |
|
| 344 |
} |