| 1 |
package clump; |
| 2 |
|
| 3 |
import java.io.Serializable; |
| 4 |
|
| 5 |
import barcode.Barcode; |
| 6 |
import hiseq.FlowcellCoordinate; |
| 7 |
import shared.KillSwitch; |
| 8 |
import shared.Tools; |
| 9 |
import stream.Read; |
| 10 |
import structures.IntList; |
| 11 |
|
| 12 |
class ReadKey implements Serializable, Comparable<ReadKey> { |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public static ReadKey makeKey(Read r, boolean setObject){ |
| 22 |
assert(r.obj==null); |
| 23 |
try { |
| 24 |
ReadKey rk=new ReadKey(r); |
| 25 |
if(setObject){r.obj=rk;} |
| 26 |
return rk; |
| 27 |
} catch (OutOfMemoryError e) { |
| 28 |
KillSwitch.memKill(e); |
| 29 |
throw new RuntimeException(e); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
private ReadKey(Read r){ |
| 34 |
this(r, 0, 0, true); |
| 35 |
} |
| 36 |
|
| 37 |
private ReadKey(Read r, long kmer_, int position_, boolean plus_){ |
| 38 |
kmer=kmer_; |
| 39 |
position=position_; |
| 40 |
clump=null; |
| 41 |
assert(!r.swapped()); |
| 42 |
flipped=false; |
| 43 |
kmerMinusStrand=!plus_; |
| 44 |
|
| 45 |
if(Clump.opticalOnly || clump.compareUMI){ |
| 46 |
FlowcellCoordinate fc=FlowcellCoordinate.getFC(); |
| 47 |
fc.setFrom(r.id); |
| 48 |
lane=fc.lane; |
| 49 |
tile=fc.tile; |
| 50 |
x=fc.x; |
| 51 |
y=fc.y; |
| 52 |
umi=fc.umi; |
| 53 |
} |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
protected ReadKey(){} |
| 58 |
|
| 59 |
public void set(long kmer_, int position_, boolean minus_){ |
| 60 |
setKmer(kmer_); |
| 61 |
setPosition(position_); |
| 62 |
|
| 63 |
kmerMinusStrand=minus_; |
| 64 |
} |
| 65 |
|
| 66 |
private long setKmer(long kmer_){ |
| 67 |
return kmer=kmer_; |
| 68 |
} |
| 69 |
|
| 70 |
private int setPosition(int position_){ |
| 71 |
return position=position_; |
| 72 |
} |
| 73 |
|
| 74 |
public Clump setClump(Clump clump_){ |
| 75 |
return clump=clump_; |
| 76 |
} |
| 77 |
|
| 78 |
private boolean setFlipped(boolean flipped_){ |
| 79 |
assert(flipped!=flipped_); |
| 80 |
return flipped=flipped_; |
| 81 |
} |
| 82 |
|
| 83 |
public void clear(){ |
| 84 |
setKmer(0); |
| 85 |
setPosition(0); |
| 86 |
setClump(null); |
| 87 |
kmerMinusStrand=false; |
| 88 |
} |
| 89 |
|
| 90 |
public void flip(Read r, int k){ |
| 91 |
assert(r.swapped()==flipped); |
| 92 |
r.reverseComplement(); |
| 93 |
r.setSwapped(!r.swapped()); |
| 94 |
setFlipped(!flipped); |
| 95 |
if(r.length()>=k){setPosition(r.length()-position+k-2);} |
| 96 |
assert(r.swapped()==flipped); |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public int compareTo(ReadKey b){ |
| 101 |
if(kmer!=b.kmer){return kmer>b.kmer ? -1 : 1;} |
| 102 |
if(kmerMinusStrand!=b.kmerMinusStrand){return kmerMinusStrand ? 1 : -1;} |
| 103 |
if(position!=b.position){return position<b.position ? 1 : -1;} |
| 104 |
if(Clump.opticalOnly){ |
| 105 |
if(lane!=b.lane){return lane-b.lane;} |
| 106 |
if(!spanTiles()){ |
| 107 |
if(tile!=b.tile){return tile-b.tile;} |
| 108 |
} |
| 109 |
if(Clump.sortYEarly()){ |
| 110 |
if(y!=b.y){return y-b.y;} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
return 0; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public boolean equals(Object b){ |
| 121 |
return equals((ReadKey)b, false); |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public int hashCode() { |
| 126 |
int x=(int)((kmer^position)&0xFFFFFFFFL); |
| 127 |
return kmerMinusStrand ? -x : x; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
public boolean physicallyContains(ReadKey b, int aLen, int bLen){ |
| 132 |
if(bLen>aLen){return false;} |
| 133 |
if(kmer!=b.kmer){return false;} |
| 134 |
final int dif=position-b.position; |
| 135 |
int bStart=dif, bStop=dif+bLen; |
| 136 |
return bStart>=0 && bStop<=aLen; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public boolean physicalAffix(ReadKey b, int aLen, int bLen){ |
| 142 |
if(bLen>aLen){return false;} |
| 143 |
if(kmer!=b.kmer){return false;} |
| 144 |
final int dif=position-b.position; |
| 145 |
int bStart=dif, bStop=dif+bLen; |
| 146 |
return (bStart==0 || bStop==aLen) && bStart>=0 && bStop<=aLen; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public boolean equals(ReadKey b, boolean containment){ |
| 155 |
if(b==null){return false;} |
| 156 |
if(kmer!=b.kmer){return false;} |
| 157 |
if(!containment && (kmerMinusStrand!=b.kmerMinusStrand || position!=b.position)){return false;} |
| 158 |
if(Clump.opticalOnly){ |
| 159 |
if(lane!=b.lane){return false;} |
| 160 |
if(!spanTiles()){ |
| 161 |
if(tile!=b.tile){return false;} |
| 162 |
} |
| 163 |
} |
| 164 |
return true; |
| 165 |
} |
| 166 |
public boolean equals(ReadKey b){ |
| 167 |
return equals(b, false); |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public String toString(){ |
| 172 |
return position+","+(kmerMinusStrand ? ",t" : ",f")+","+kmer+"\t"+lane+","+tile+","+x+","+y; |
| 173 |
} |
| 174 |
|
| 175 |
public float distance(ReadKey rkb){ |
| 176 |
if(lane!=rkb.lane){return FlowcellCoordinate.big;} |
| 177 |
|
| 178 |
long a=Tools.absdif(x, rkb.x), b=Tools.absdif(y, rkb.y); |
| 179 |
if(tile!=rkb.tile){ |
| 180 |
if(spanTiles()){ |
| 181 |
if(spanAdjacentOnly && Tools.absdif(tile, rkb.tile)>1){return FlowcellCoordinate.big;} |
| 182 |
if(spanTilesX && spanTilesY){ |
| 183 |
return Tools.min(a, b); |
| 184 |
}else if(spanTilesX){ |
| 185 |
return a; |
| 186 |
}else{ |
| 187 |
return b; |
| 188 |
} |
| 189 |
}else{ |
| 190 |
return FlowcellCoordinate.big; |
| 191 |
} |
| 192 |
} |
| 193 |
return (float)Math.sqrt(a*a+b*b); |
| 194 |
} |
| 195 |
|
| 196 |
public boolean near(ReadKey rkb, float dist){ |
| 197 |
return distance(rkb)<dist; |
| 198 |
} |
| 199 |
|
| 200 |
public boolean nearXY(ReadKey rkb, float dist){ |
| 201 |
if(lane!=rkb.lane){return false;} |
| 202 |
|
| 203 |
long a=Tools.absdif(x, rkb.x), b=Tools.absdif(y, rkb.y); |
| 204 |
return Tools.min(a,b)<=dist; |
| 205 |
} |
| 206 |
|
| 207 |
public boolean umiMatches(ReadKey rkb, int maxSubs) { |
| 208 |
if(umi==null || rkb.umi==null) {return false;} |
| 209 |
int hdist=Barcode.hdist(umi, rkb.umi); |
| 210 |
return hdist<=maxSubs; |
| 211 |
} |
| 212 |
|
| 213 |
public int flag; |
| 214 |
|
| 215 |
public long kmer; |
| 216 |
|
| 217 |
public int position; |
| 218 |
public boolean flipped; |
| 219 |
public boolean kmerMinusStrand; |
| 220 |
public Clump clump; |
| 221 |
public IntList vars; |
| 222 |
|
| 223 |
|
| 224 |
public int lane, tile, x, y; |
| 225 |
public String umi; |
| 226 |
|
| 227 |
public static final int overhead=overhead(); |
| 228 |
private static int overhead(){ |
| 229 |
return 16+ |
| 230 |
1*(8)+ |
| 231 |
1*(4)+ |
| 232 |
2*(4)+ |
| 233 |
2*(8)+ |
| 234 |
4*(4); |
| 235 |
} |
| 236 |
|
| 237 |
public static boolean spanTiles(){return spanTilesX || spanTilesY;} |
| 238 |
public static boolean spanTilesX=false; |
| 239 |
public static boolean spanTilesY=false; |
| 240 |
public static boolean spanAdjacentOnly=false; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
private static final long serialVersionUID = 1L; |
| 246 |
|
| 247 |
} |