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
|
package clump;
import java.io.Serializable;
import barcode.Barcode;
import hiseq.FlowcellCoordinate;
import shared.KillSwitch;
import shared.Tools;
import stream.Read;
import structures.IntList;
class ReadKey implements Serializable, Comparable<ReadKey> {
// public static ReadKey makeKeyIfNeeded(Read r){
// if(r.obj==null){
// return makeKey(r, true);
// }
// return (ReadKey)r.obj;
// }
public static ReadKey makeKey(Read r, boolean setObject){
assert(r.obj==null);
try {
ReadKey rk=new ReadKey(r);
if(setObject){r.obj=rk;}
return rk;
} catch (OutOfMemoryError e) {
KillSwitch.memKill(e);
throw new RuntimeException(e);
}
}
private ReadKey(Read r){
this(r, 0, 0, true);
}
private ReadKey(Read r, long kmer_, int position_, boolean plus_){
kmer=kmer_;
position=position_;
clump=null;
assert(!r.swapped());
flipped=false;
kmerMinusStrand=!plus_;
if(Clump.opticalOnly || clump.compareUMI){
FlowcellCoordinate fc=FlowcellCoordinate.getFC();
fc.setFrom(r.id);
lane=fc.lane;
tile=fc.tile;
x=fc.x;
y=fc.y;
umi=fc.umi;
}
// expectedErrors=r.expectedErrorsIncludingMate(true);
}
protected ReadKey(){}
public void set(long kmer_, int position_, boolean minus_){
setKmer(kmer_);
setPosition(position_);
// setClump(null);
kmerMinusStrand=minus_;
}
private long setKmer(long kmer_){
return kmer=kmer_;
}
private int setPosition(int position_){
return position=position_;
}
public Clump setClump(Clump clump_){
return clump=clump_;
}
private boolean setFlipped(boolean flipped_){
assert(flipped!=flipped_);
return flipped=flipped_;
}
public void clear(){
setKmer(0);
setPosition(0);
setClump(null);
kmerMinusStrand=false;
}
public void flip(Read r, int k){
assert(r.swapped()==flipped);
r.reverseComplement();
r.setSwapped(!r.swapped());
setFlipped(!flipped);
if(r.length()>=k){setPosition(r.length()-position+k-2);}
assert(r.swapped()==flipped);
}
@Override
public int compareTo(ReadKey b){
if(kmer!=b.kmer){return kmer>b.kmer ? -1 : 1;} //Bigger kmers first...
if(kmerMinusStrand!=b.kmerMinusStrand){return kmerMinusStrand ? 1 : -1;}
if(position!=b.position){return position<b.position ? 1 : -1;}
if(Clump.opticalOnly){
if(lane!=b.lane){return lane-b.lane;}
if(!spanTiles()){
if(tile!=b.tile){return tile-b.tile;}
}
if(Clump.sortYEarly()){ //Improves speed slightly
if(y!=b.y){return y-b.y;}
}
}
// if(expectedErrors!=b.expectedErrors){
// return expectedErrors>b.expectedErrors ? 1 : -1;//Higher quality first
// }
return 0;
}
@Override
public boolean equals(Object b){
return equals((ReadKey)b, false);
}
@Override
public int hashCode() {
int x=(int)((kmer^position)&0xFFFFFFFFL);
return kmerMinusStrand ? -x : x;
}
/** True if this physically contains b (ignoring mismatches) */
public boolean physicallyContains(ReadKey b, int aLen, int bLen){
if(bLen>aLen){return false;}
if(kmer!=b.kmer){return false;}
final int dif=position-b.position;
int bStart=dif, bStop=dif+bLen;
return bStart>=0 && bStop<=aLen;
}
/** True if this physically contains b as a prefix or suffix (ignoring mismatches).
* More restrictive than physicallyContains. */
public boolean physicalAffix(ReadKey b, int aLen, int bLen){
if(bLen>aLen){return false;}
if(kmer!=b.kmer){return false;}
final int dif=position-b.position;
int bStart=dif, bStop=dif+bLen;
return (bStart==0 || bStop==aLen) && bStart>=0 && bStop<=aLen;
}
/** Note that this is different than compareTo()==0
* That's to prevent sortYEarly comparison making things unequal.
* @param b
* @return True if equal
*/
public boolean equals(ReadKey b, boolean containment){
if(b==null){return false;}
if(kmer!=b.kmer){return false;}
if(!containment && (kmerMinusStrand!=b.kmerMinusStrand || position!=b.position)){return false;}
if(Clump.opticalOnly){
if(lane!=b.lane){return false;}
if(!spanTiles()){
if(tile!=b.tile){return false;}
}
}
return true;
}
public boolean equals(ReadKey b){
return equals(b, false);
}
@Override
public String toString(){
return position+","+(kmerMinusStrand ? ",t" : ",f")+","+kmer+"\t"+lane+","+tile+","+x+","+y;
}
public float distance(ReadKey rkb){
if(lane!=rkb.lane){return FlowcellCoordinate.big;}
long a=Tools.absdif(x, rkb.x), b=Tools.absdif(y, rkb.y);
if(tile!=rkb.tile){
if(spanTiles()){
if(spanAdjacentOnly && Tools.absdif(tile, rkb.tile)>1){return FlowcellCoordinate.big;}
if(spanTilesX && spanTilesY){
return Tools.min(a, b);
}else if(spanTilesX){
return a;
}else{
return b;
}
}else{
return FlowcellCoordinate.big;
}
}
return (float)Math.sqrt(a*a+b*b);
}
public boolean near(ReadKey rkb, float dist){
return distance(rkb)<dist;
}
public boolean nearXY(ReadKey rkb, float dist){
if(lane!=rkb.lane){return false;}
long a=Tools.absdif(x, rkb.x), b=Tools.absdif(y, rkb.y);
return Tools.min(a,b)<=dist;
}
public boolean umiMatches(ReadKey rkb, int maxSubs) {
if(umi==null || rkb.umi==null) {return false;}
int hdist=Barcode.hdist(umi, rkb.umi);
return hdist<=maxSubs;
}
public int flag;
public long kmer;
/** Position of rightmost base of kmer */
public int position;
public boolean flipped;
public boolean kmerMinusStrand;
public Clump clump;
public IntList vars;
// public float expectedErrors;
public int lane, tile, x, y;
public String umi;
public static final int overhead=overhead();
private static int overhead(){
return 16+ //self
1*(8)+ //kmer
1*(4)+ //int fields
2*(4)+ //booleans
2*(8)+ //pointers
4*(4); //flowcell coordinate
}
public static boolean spanTiles(){return spanTilesX || spanTilesY;}
public static boolean spanTilesX=false;
public static boolean spanTilesY=false;
public static boolean spanAdjacentOnly=false;
/**
*
*/
private static final long serialVersionUID = 1L;
}
|