File: FlowCell.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 (344 lines) | stat: -rwxr-xr-x 9,590 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package hiseq;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import shared.Tools;
import structures.FloatList;
import structures.Point;

public class FlowCell {
	
	public FlowCell(String fname){
		TileDump.loadDump(fname, this);
	}
	
	public FlowCell(int k_){k=k_;}
	
	public MicroTile getMicroTile(String id) {//This method is NOT threadsafe
		ihp.parse(id);
		return getMicroTile(ihp);
	}
	
	public MicroTile getMicroTile(IlluminaHeaderParser2 ihp){
		return getLane(ihp.lane()).getMicroTile(ihp.tile(), ihp.xPos(), ihp.yPos(), true);
	}
	
	public MicroTile getMicroTile(int lane, int tile, int x, int y){
		return getLane(lane).getMicroTile(tile, x, y, true);
	}
	
	public MicroTile getMicroTile(int lane, int tile, int x, int y, boolean create){
		return getLane(lane).getMicroTile(tile, x, y, create);
	}
	
	public Lane getLane(int lane){
		while(lanes.size()<=lane){lanes.add(new Lane(lanes.size()));}
		return lanes.get(lane);
	}

	public ArrayList<MicroTile> toList() {
		ArrayList<MicroTile> mtList=new ArrayList<MicroTile>();
		for(Lane lane : lanes){
			if(lane!=null){
				for(Tile tile : lane.tiles){
					if(tile!=null){
						for(ArrayList<MicroTile> ylist : tile.xlist){
							if(ylist!=null){
								for(MicroTile mt : ylist){
									if(mt!=null){
										mtList.add(mt);
									}
								}
							}
						}
					}
				}
			}
		}
		return mtList;
	}
	
	public ArrayList<MicroTile> calcStats(){
		ArrayList<MicroTile> mtList=toList();
		readsProcessed=basesProcessed=0;
		readsAligned=basesAligned=0;
		readErrors=baseErrors=0;
		for(MicroTile mt : mtList){
			mt.process();
			readsProcessed+=mt.readCount;
			basesProcessed+=mt.baseCount;
			readsAligned+=mt.alignedReadCount;
			basesAligned+=mt.alignedBaseCount;
			readErrors+=mt.readErrorCount;
			baseErrors+=mt.baseErrorCount;
		}
		final double mtDiv=Tools.max(1, mtList.size());
		avgReads=readsProcessed/mtDiv;
		avgAlignedReads=readsAligned/mtDiv;
		minCountToUse=(long)Tools.min(500, avgReads*0.25f);
		int toKeep=0;
		for(MicroTile mt : mtList){
			if(mt.readCount>=minCountToUse){toKeep++;}
		}
		
		FloatList avgQualityList=new FloatList(toKeep);
		FloatList avgUniqueList=new FloatList(toKeep);
		FloatList avgDepthList=new FloatList(toKeep);
		FloatList avgErrorFreeList=new FloatList(toKeep);
		FloatList avgPolyGList=new FloatList(toKeep);
		FloatList avgGList=new FloatList(toKeep);
		

		ArrayList<Point> readPoints=new ArrayList<Point>();
		ArrayList<Point> basePoints=new ArrayList<Point>();
		
		for(MicroTile mt : mtList){
			if(mt!=null && mt.readCount>=minCountToUse){
				double up=mt.uniquePercent();
				double rer=mt.readErrorRate();
				double ber=mt.baseErrorRate();
				avgQualityList.add((float)mt.averageReadQualityByProb());
				avgUniqueList.add((float)up);
				avgDepthList.add((float)mt.depth());
				avgErrorFreeList.add((float)mt.percentErrorFree());
				avgPolyGList.add((float)mt.polyGPercent());
				avgGList.add((float)mt.avgG());
				readPoints.add(new Point(up, rer));
				basePoints.add(new Point(up, Math.sqrt(ber)));
			}
		}
		
		if(readsAligned>1000) {
			Collections.sort(readPoints);
			Collections.sort(basePoints);
			uniqueToReadErrorRateFormula=Tools.linearRegression(readPoints, 0.001, 0.999);
			uniqueToBaseErrorRateFormula=Tools.linearRegression(basePoints, 0.001, 0.999);
//			System.err.println("Calculated "+Arrays.toString(uniqueToBaseErrorRateFormula)+" from "
//					+basePoints.size()+" points, "+basePoints.get(0)+"~"+basePoints.get(basePoints.size()-1));
		}else {
//			assert(false) : readsProcessed+", "+basesProcessed+", "+readsAligned+", "+readPoints.get(0);
			readPoints=basePoints=null;
			uniqueToReadErrorRateFormula=uniqueToBaseErrorRateFormula=null;
		}
		
		avgQuality=avgQualityList.mean();
		avgUnique=avgUniqueList.mean();
		avgDepth=avgDepthList.mean();
		avgErrorFree=avgErrorFreeList.mean();
		avgPolyG=avgPolyGList.mean();
		avgG=avgGList.mean();
		
		stdQuality=avgQualityList.stdev();
		stdUnique=avgUniqueList.stdev();
		stdDepth=avgDepthList.stdev();
		stdErrorFree=avgErrorFreeList.stdev();
		stdPolyG=avgPolyGList.stdev();
		stdG=avgGList.stdev();
		
		return mtList;
	}
	
	public FlowCell widenToTargetReads(int targetReads){
		if(readsProcessed<1){
			System.err.println("Warning: Zero reads processed.");
			return this;
		}
		if(readsProcessed<targetReads){
			return this;
		}
		FlowCell fc=this;
		while(fc.avgReads<targetReads){
			FlowCell fc2=fc.widen(true);
			fc2.calcStats();
			if(fc2.avgReads<=fc.avgReads){
				unwiden();
				return fc;
			}
			fc=fc2;
		}
		return fc.setFrom(this);
	}
	
	public FlowCell widenToTargetAlignedReads(int targetAlignedReads){
		if(readsAligned<1){
			System.err.println("Warning: Zero aligned reads processed.");
			return this;
		}
		if(readsAligned<targetAlignedReads){
			System.err.println("Warning: Below target aligned reads ("+readsAligned+"<"+targetAlignedReads+")");
			return this;
		}
		FlowCell fc=this;
		while(fc.avgAlignedReads<targetAlignedReads){
			FlowCell fc2=fc.widen(false);
			fc2.calcStats();
			if(fc2.avgAlignedReads<=fc.avgAlignedReads){
				unwiden();
				return fc;
			}
			fc=fc2;
		}
		return fc.setFrom(this);
	}
	
	public void unwiden(){
		if(Tile.xSize>Tile.ySize){Tile.ySize/=2;}
		else{Tile.xSize/=2;}
	}
	
	public FlowCell widen(boolean loud){
		final int x=Tile.xSize, y=Tile.ySize;
		final int x2=x>=y ? x : 2*x;
		final int y2=x>=y ? 2*y : y;
		return widen(x2, y2, loud);
	}
	
	public FlowCell widen(int x2, int y2, boolean loud){
//		assert(x2>Tile.xSize || y2>Tile.ySize);
		if(x2<=Tile.xSize && y2<=Tile.ySize) {return this;}
		Tile.xSize=Tools.max(x2, Tile.xSize);
		Tile.ySize=Tools.max(y2, Tile.ySize);
		if(loud) {System.err.println("Widening to "+Tile.xSize+"x"+Tile.ySize);}
		ArrayList<MicroTile> list=toList();
		FlowCell fc=new FlowCell(k).setFrom(this);
		for(MicroTile mt : list){
			MicroTile mt2=fc.getMicroTile(mt.lane, mt.tile, mt.x1, mt.y1);
			mt2.add(mt);
		}
		for(Lane lane : lanes) {
			if(lane!=null) {
				Lane lane2=fc.getLane(lane.lane);
				lane2.addLists(lane);
			}
		}
		return fc;
	}

	public void blur() {
		FlowCell fc=new FlowCell(k);
		fc.add(this);
		ArrayList<MicroTile> list=this.toList();
		for(MicroTile mt : list) {
			if(mt.readCount<1) {continue;}
			int tile=mt.tile, lane=mt.lane, x1=mt.x1, x2=mt.x2, y1=mt.y1, y2=mt.y2;
			int x0=x1-1, y0=y1-1, x3=x2+1, y3=y2+1;
//			MicroTile center=fc.getMicroTile(lane, tile, x1, y1, true);
			MicroTile up=(y0<0 ? null : fc.getMicroTile(lane, tile, x1, y0, false));
			MicroTile down=fc.getMicroTile(lane, tile, x1, y3, false);
			MicroTile left=(x0<0 ? null : fc.getMicroTile(lane, tile, x0, y1, false));
			MicroTile right=fc.getMicroTile(lane, tile, x3, y1, false);
//			mt.add(center);
//			mt.add(center);
//			mt.add(center);
//			mt.add(center);
//			mt.add(center);
//			mt.add(center);
//			mt.add(center);
//			if(up!=null) {mt.add(up);}
//			if(down!=null) {mt.add(down);}
//			if(left!=null) {mt.add(left);}
//			if(right!=null) {mt.add(right);}
			
			mt.multiplyBy(8);
			if(up!=null) {mt.add(up);}
			if(down!=null) {mt.add(down);}
			if(left!=null) {mt.add(left);}
			if(right!=null) {mt.add(right);}
			mt.multiplyBy(0.125); //Counts should end up around 50% above original
		}
	}
	
	public void dump(String fname, boolean overwrite) {
		TileDump.write(this, fname, overwrite);
	}
	
	public void add(FlowCell fcb) {
		for(Lane b : fcb.lanes) {
			if(b!=null) {
				synchronized(b) {
					Lane a=getLane(b.lane);
					synchronized(a) {
						a.add(b);
					}
				}
			}
		}
	}
	
	FlowCell setFrom(FlowCell fc) {
		readsProcessed=fc.readsProcessed;
		basesProcessed=fc.basesProcessed;
		readsAligned=fc.readsAligned;
		basesAligned=fc.basesAligned;
		readErrors=fc.readErrors;
		baseErrors=fc.baseErrors;
		
		name=fc.name;
		xMin=fc.xMin;
		xMax=fc.xMax;
		yMin=fc.yMin;
		yMax=fc.yMax;
		tMin=fc.tMin;
		tMax=fc.tMax;
		k=fc.k;

		uniqueToReadErrorRateFormula=fc.uniqueToReadErrorRateFormula==null ? null : 
			Arrays.copyOf(fc.uniqueToReadErrorRateFormula, 2);
		uniqueToBaseErrorRateFormula=fc.uniqueToBaseErrorRateFormula==null ? null : 
			Arrays.copyOf(fc.uniqueToBaseErrorRateFormula, 2);
		return this;
	}

	public double alignmentRate() {
		return readsAligned/(double)readsProcessed;
	}
	
	public double baseErrorRate() {
		return baseErrors/(double)(Tools.max(basesAligned, 1));
	}
	
	public ArrayList<Lane> lanes=new ArrayList<Lane>();
	
	long readsProcessed;
	long basesProcessed;
	long readsAligned;
	long basesAligned;
	long readErrors;
	long baseErrors;
	
	String name;

	int xMin=-1;
	int xMax=-1;
	int yMin=-1;
	int yMax=-1;
	int tMin=-1;
	int tMax=-1;
	int k=31;

	public double avgReads;
	public double avgAlignedReads;
	public double minCountToUse;
	
	public double avgQuality;
	public double avgUnique;
	public double avgDepth;
	public double avgErrorFree;
	public double avgPolyG;
	public double avgG;
	
	public double stdQuality;
	public double stdUnique;
	public double stdDepth;
	public double stdErrorFree;
	public double stdPolyG;
	public double stdG;
	
	public double[] uniqueToReadErrorRateFormula;
	public double[] uniqueToBaseErrorRateFormula;
	
	private IlluminaHeaderParser2 ihp=new IlluminaHeaderParser2();
	
}