Debian Codegraph @

current/hiseq/Lane.java

raw symbols SCIP
1 package hiseq;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.concurrent.atomic.AtomicLongArray;
6
7 import fileIO.ByteStreamWriter;
8 import shared.Tools;
9 import structures.ByteBuilder;
10 import structures.LongList;
11
12 public class Lane implements Iterable<Tile> {
13
14 public Lane(int lane_){
15 lane=lane_;
16 }
17
18 public MicroTile getMicroTile(int tile, int x, int y){
19 return getTile(tile).get(x, y, true);
20 }
21
22 public MicroTile getMicroTile(int tile, int x, int y, boolean create){
23 return getTile(tile).get(x, y, create);
24 }
25
26 public Tile getTile(int index){
27 while(tiles.size()<=index){tiles.add(null);}
28 Tile t=tiles.get(index);
29 if(t==null){
30 t=new Tile(lane, index);
31 tiles.set(index, t);
32 }
33 return t;
34 }
35
36 public void add(Lane b) {
37 for(Tile tb : b.tiles) {
38 if(tb!=null) {
39 synchronized(tb) {
40 Tile ta=getTile(tb.tile);
41 synchronized(ta) {
42 ta.add(tb);
43 }
44 }
45 }
46 }
47 addLists(b);
48 }
49
50 public void addLists(Lane b) {
51 for(int i=0; i<longLists.length; i++) {
52 for(int j=0; j<longLists[i].length; j++) {
53 Tools.add(longLists[i][j], b.longLists[i][j]);
54 }
55 }
56 }
57
58 public void print(ByteStreamWriter bsw, int k, double[] rerf, double[] berf) {
59 double HG=calcHighDepthGenomic(k);
60 for(Tile tile : tiles){
61 if(tile!=null){
62 bsw.print(tile.toText(k, HG, rerf, berf));
63 }
64 }
65 }
66
67 /**
68 * Calculates the rate of high-depth genomic kmers in the lane,
69 * using the observed rates of unique kmers and alignment errors.
70 * @param k Kmer length.
71 * @return HG Rate of high depth genomic kmers.
72 */
73 public double calcHighDepthGenomic(int k) {
74 long hits=0;
75 long misses=0;
76 long errors=0;
77 long alignedBases=0;
78 long mts=0;
79 for(Tile t : this) {
80 for(MicroTile mt : t) {
81 // if(mt!=null) {
82 hits+=mt.hits;
83 misses+=mt.misses;
84 errors+=mt.baseErrorCount;
85 alignedBases+=mt.alignedBaseCount;
86 mts++;
87 // }
88 }
89 }
90 if(mts<1 || alignedBases<1 || hits+misses<1) {return 0;}
91 assert(alignedBases>0) : "No alignment data.";
92 assert(hits+misses>0) : "No kmer data.";
93 //PhiX error rate
94 double E=errors/(double)alignedBases;
95 //Per-base correctness chance
96 double C=1-E;
97 //Prob of kmer correctness
98 double P=Math.pow(C, k);
99 //Unique fraction
100 double U=misses/(double)(hits+misses);
101 //Non-unique fraction
102 double NU=1-U;
103 //High depth genomic kmer fraction
104 double HG=Tools.mid(0.0001, 0.9999, NU/P); //Otherwise it can exceed 1 for high-depth things like PhiX.
105 assert(HG>0 && HG<=1) : "\nE="+E+", P="+P+", U="+U+", NU="+NU+", HG="+HG
106 +"\nmts="+mts+", hits="+hits+", misses="+misses+", errors="+errors+", bases="+alignedBases+", k="+k;
107 return HG;
108 }
109
110 @Override
111 public Iterator<Tile> iterator() {
112 return new TileIterator();
113 }
114
115 private final class TileIterator implements Iterator<Tile> {
116
117 TileIterator(){}
118
119 @Override
120 public boolean hasNext() {
121 while(pos<tiles.size() && tiles.get(pos)==null) {pos++;}
122 return pos<tiles.size();
123 }
124
125 @Override
126 public Tile next() {
127 Tile t=(hasNext() ? tiles.get(pos) : null);
128 pos++;
129 return t;
130 }
131
132 private int pos=0;
133
134 }
135
136 public ArrayList<Tile> tiles=new ArrayList<Tile>();
137
138 public boolean isEmpty() {
139 return tiles.isEmpty();
140 }
141
142 // public LongList[] depthSums=new LongList[] {new LongList(151), new LongList(151)};
143 // public LongList[] depthCounts=new LongList[] {new LongList(151), new LongList(151)};
144 // public LongList[] matchCounts=new LongList[] {new LongList(151), new LongList(151)};
145 // public LongList[] subCounts=new LongList[] {new LongList(151), new LongList(151)};
146
147 public AtomicLongArray[] depthSums=new AtomicLongArray[]
148 {new AtomicLongArray(500), new AtomicLongArray(500)};
149 public AtomicLongArray[] depthCounts=new AtomicLongArray[]
150 {new AtomicLongArray(500), new AtomicLongArray(500)};
151 public AtomicLongArray[] matchCounts=new AtomicLongArray[]
152 {new AtomicLongArray(500), new AtomicLongArray(500)};
153 public AtomicLongArray[] subCounts=new AtomicLongArray[]
154 {new AtomicLongArray(500), new AtomicLongArray(500)};
155
156 public AtomicLongArray[][] longLists=new AtomicLongArray[][] {
157 depthSums, depthCounts, matchCounts, subCounts};
158
159 public int lane;
160
161 }