comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/clump/ReadKey.java @ 68:5028fdace37b

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