File: Lane.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 (161 lines) | stat: -rwxr-xr-x 4,352 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
package hiseq;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicLongArray;

import fileIO.ByteStreamWriter;
import shared.Tools;
import structures.ByteBuilder;
import structures.LongList;

public class Lane implements Iterable<Tile> {
	
	public Lane(int lane_){
		lane=lane_;
	}
	
	public MicroTile getMicroTile(int tile, int x, int y){
		return getTile(tile).get(x, y, true);
	}
	
	public MicroTile getMicroTile(int tile, int x, int y, boolean create){
		return getTile(tile).get(x, y, create);
	}
	
	public Tile getTile(int index){
		while(tiles.size()<=index){tiles.add(null);}
		Tile t=tiles.get(index);
		if(t==null){
			t=new Tile(lane, index);
			tiles.set(index, t);
		}
		return t;
	}

	public void add(Lane b) {
		for(Tile tb : b.tiles) {
			if(tb!=null) {
				synchronized(tb) {
					Tile ta=getTile(tb.tile);
					synchronized(ta) {
						ta.add(tb);
					}
				}
			}
		}
		addLists(b);
	}
	
	public void addLists(Lane b) {
		for(int i=0; i<longLists.length; i++) {
			for(int j=0; j<longLists[i].length; j++) {
				Tools.add(longLists[i][j], b.longLists[i][j]);
			}
		}
	}

	public void print(ByteStreamWriter bsw, int k, double[] rerf, double[] berf) {
		double HG=calcHighDepthGenomic(k);
		for(Tile tile : tiles){
			if(tile!=null){
				bsw.print(tile.toText(k, HG, rerf, berf));
			}
		}
	}
	
	/**
	 * Calculates the rate of high-depth genomic kmers in the lane,
	 * using the observed rates of unique kmers and alignment errors.
	 * @param k Kmer length.
	 * @return HG Rate of high depth genomic kmers.
	 */
	public double calcHighDepthGenomic(int k) {
		long hits=0;
		long misses=0;
		long errors=0;
		long alignedBases=0;
		long mts=0;
		for(Tile t : this) {
			for(MicroTile mt : t) {
//				if(mt!=null) {
					hits+=mt.hits;
					misses+=mt.misses;
					errors+=mt.baseErrorCount;
					alignedBases+=mt.alignedBaseCount;
					mts++;
//				}
			}
		}
		if(mts<1 || alignedBases<1 || hits+misses<1) {return 0;}
		assert(alignedBases>0) : "No alignment data.";
		assert(hits+misses>0) : "No kmer data.";
		//PhiX error rate
		double E=errors/(double)alignedBases;
		//Per-base correctness chance
		double C=1-E;
		//Prob of kmer correctness
		double P=Math.pow(C, k);
		//Unique fraction
		double U=misses/(double)(hits+misses);
		//Non-unique fraction
		double NU=1-U;
		//High depth genomic kmer fraction
		double HG=Tools.mid(0.0001, 0.9999, NU/P); //Otherwise it can exceed 1 for high-depth things like PhiX.
		assert(HG>0 && HG<=1) : "\nE="+E+", P="+P+", U="+U+", NU="+NU+", HG="+HG
			+"\nmts="+mts+", hits="+hits+", misses="+misses+", errors="+errors+", bases="+alignedBases+", k="+k;
		return HG;
	}

	@Override
	public Iterator<Tile> iterator() {
		return new TileIterator();
	}
	
	private final class TileIterator implements Iterator<Tile> {

		TileIterator(){}
		
		@Override
		public boolean hasNext() {
			while(pos<tiles.size() && tiles.get(pos)==null) {pos++;}
			return pos<tiles.size();
		}

		@Override
		public Tile next() {
			Tile t=(hasNext() ? tiles.get(pos) : null);
			pos++;
			return t;
		}
		
		private int pos=0;
		
	}
	
	public ArrayList<Tile> tiles=new ArrayList<Tile>();

	public boolean isEmpty() {
		return tiles.isEmpty();
	}

//	public LongList[] depthSums=new LongList[] {new LongList(151), new LongList(151)};
//	public LongList[] depthCounts=new LongList[] {new LongList(151), new LongList(151)};
//	public LongList[] matchCounts=new LongList[] {new LongList(151), new LongList(151)};
//	public LongList[] subCounts=new LongList[] {new LongList(151), new LongList(151)};

	public AtomicLongArray[] depthSums=new AtomicLongArray[] 
			{new AtomicLongArray(500), new AtomicLongArray(500)};
	public AtomicLongArray[] depthCounts=new AtomicLongArray[] 
			{new AtomicLongArray(500), new AtomicLongArray(500)};
	public AtomicLongArray[] matchCounts=new AtomicLongArray[] 
			{new AtomicLongArray(500), new AtomicLongArray(500)};
	public AtomicLongArray[] subCounts=new AtomicLongArray[] 
			{new AtomicLongArray(500), new AtomicLongArray(500)};
	
	public AtomicLongArray[][] longLists=new AtomicLongArray[][] {
		depthSums, depthCounts, matchCounts, subCounts};
	
	public int lane;
	
}