comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/bbmap-39.01-1/current/kmer/KmerNode2D.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 kmer;
2
3 import java.util.Arrays;
4 import java.util.concurrent.atomic.AtomicLong;
5
6 import fileIO.ByteStreamWriter;
7 import shared.KillSwitch;
8 import shared.Shared;
9 import shared.Tools;
10 import structures.ByteBuilder;
11
12 /**
13 * Allows multiple values per kmer.
14 * @author Brian Bushnell
15 * @date Nov 7, 2014
16 *
17 */
18 public class KmerNode2D extends KmerNode {
19
20 /*--------------------------------------------------------------*/
21 /*---------------- Initialization ----------------*/
22 /*--------------------------------------------------------------*/
23
24 public KmerNode2D(long pivot_){
25 super(pivot_);
26 }
27
28 public KmerNode2D(long pivot_, int value_){
29 super(pivot_);
30 assert(value_>=0 || value_==-1);
31 values=new int[] {value_, -1};
32 numValues=1;
33 }
34
35 public KmerNode2D(long pivot_, int[] vals_, int vlen){
36 super(pivot_);
37 values=vals_;
38 numValues=vlen;
39 assert(values!=null || vlen==0);
40 assert(values==null || (vlen<=values.length && vlen>=0));
41 // assert(countValues(values)==vlen) : countValues(values)+", "+vlen; //TODO: Slow assertion //123
42 }
43
44 @Override
45 public final KmerNode makeNode(long pivot_, int value_){
46 return new KmerNode2D(pivot_, value_);
47 }
48
49 @Override
50 public final KmerNode makeNode(long pivot_, int[] values_, int vlen){
51 return new KmerNode2D(pivot_, values_, vlen);
52 }
53
54 /*--------------------------------------------------------------*/
55 /*---------------- Public Methods ----------------*/
56 /*--------------------------------------------------------------*/
57
58 // public final int set_Test(final long kmer, final int v[]){
59 // assert(TESTMODE);
60 // final int x;
61 // if(TWOD()){
62 // int[] old=getValues(kmer, null);
63 // assert(old==null || contains(kmer, old));
64 // x=set0(kmer, v);
65 // assert(old==null || contains(kmer, old));
66 // assert(contains(kmer, v));
67 // }else{
68 // int old=getValue(kmer);
69 // assert(old==0 || old==-1 || contains(kmer, old));
70 // x=set0(kmer, v);
71 // assert(contains(kmer, v)) : "old="+old+", v="+v+", kmer="+kmer+", get(kmer)="+getValue(kmer);
72 // assert(v[0]==old || !contains(kmer, old));
73 // }
74 // return x;
75 // }
76
77 /** Returns number of nodes added */
78 @Override
79 public int set(long kmer, int vals[], int vlen){
80 if(pivot<0){pivot=kmer; insertValue(vals, vlen); return 1;} //Allows initializing empty nodes to -1
81 if(kmer<pivot){
82 if(left==null){left=new KmerNode2D(kmer, vals, vlen); return 1;}
83 return left.set(kmer, vals, vlen);
84 }else if(kmer>pivot){
85 if(right==null){right=new KmerNode2D(kmer, vals, vlen); return 1;}
86 return right.set(kmer, vals, vlen);
87 }else{
88 insertValue(vals, vlen);
89 }
90 return 0;
91 }
92
93 /*--------------------------------------------------------------*/
94 /*---------------- Nonpublic Methods ----------------*/
95 /*--------------------------------------------------------------*/
96
97 @Override
98 protected int value(){return values==null ? 0 : values[0];}
99
100 @Override
101 protected int[] values(int[] singleton){
102 return values;
103 }
104
105 @Override
106 public int set(int value_){
107 insertValue(value_);
108 return value_;
109 }
110
111 @Override
112 protected int set(int[] values_, int vlen){
113 int ret=(values==null ? 1 : 0);
114 insertValue(values_, vlen);
115 return ret;
116 }
117
118 @Override
119 int numValues(){
120 // assert(countValues(values)==numValues) : countValues(values)+", "+numValues; //TODO: Slow assertion //123
121 return numValues;
122 // asdf
123 // if(values==null){return 0;}
124 // for(int i=0; i<values.length; i++){
125 // if(values[i]==-1){return i;}
126 // }
127 // return values.length;
128 }
129
130 /*--------------------------------------------------------------*/
131 /*---------------- Private Methods ----------------*/
132 /*--------------------------------------------------------------*/
133
134 /** Returns number of values added */
135 private int insertValue(int v){
136 return insertIntoList(v);
137 }
138
139 /** Returns number of values added */
140 private int insertValue(int[] vals, int vlen){
141 // assert(countValues(vals)==vlen) : countValues(vals)+", "+vlen; //TODO: Slow assertion //123
142 assert(vals!=null || vlen==0);
143 assert(vals==null || (vlen<=vals.length && vlen>=0));
144 if(values==null){
145 values=vals;
146 numValues=vlen;
147 return 1;
148 }
149 for(int v : vals){
150 if(v<0){break;}
151 insertIntoList(v);
152 }
153 return 0;
154 }
155
156 private final int countValues(int[] vals){
157 if(vals==null) {return 0;}
158 int count=0;
159 for(int v : vals){
160 if(v>=0){
161 count++;
162 }else{
163 break;
164 }
165 }
166 return count;
167 }
168
169 private final int insertIntoList(final int v){
170 // assert(countValues(values)==numValues) : countValues(values)+", "+numValues; //TODO: Slow assertion //123
171 assert(v>=0);
172
173 if(values==null){
174 values=new int[] {v, -1};
175 numValues=1;
176 return 1;
177 }
178
179 for(int i=numValues-1, lim=Tools.max(0, numValues-slowAddLimit); i>=lim; i--){//This is the slow bit
180 if(values[i]==v){return 0;}
181 if(values[i]<0){
182 values[i]=v;
183 numValues++;
184 return 1;
185 }
186 }
187 //At this point the size is big, or the element was not found
188
189 if(numValues>=values.length){//resize
190 assert(numValues==values.length);
191 final int oldSize=values.length;
192 final int newSize=(int)Tools.min(Shared.MAX_ARRAY_LEN, oldSize*2L);
193 assert(newSize>values.length) : "Overflow.";
194 values=KillSwitch.copyOf(values, newSize);
195 Arrays.fill(values, oldSize, newSize, -1);
196 }
197
198 //quick add
199 assert(values[numValues]<0);
200 values[numValues]=v;
201 numValues++;
202
203 // assert(countValues(values)==numValues) : countValues(values)+", "+numValues; //TODO: Slow assertion //123
204 return 1;
205 }
206
207 /*--------------------------------------------------------------*/
208 /*---------------- Resizing and Rebalancing ----------------*/
209 /*--------------------------------------------------------------*/
210
211 @Override
212 boolean canResize() {
213 return false;
214 }
215
216 @Override
217 public boolean canRebalance() {
218 return true;
219 }
220
221 @Deprecated
222 @Override
223 public int arrayLength() {
224 throw new RuntimeException("Unsupported.");
225 }
226
227 @Deprecated
228 @Override
229 void resize() {
230 throw new RuntimeException("Unsupported.");
231 }
232
233 @Deprecated
234 @Override
235 public void rebalance() {
236 throw new RuntimeException("Please call rebalance(ArrayList<KmerNode>) instead, with an empty list.");
237 }
238
239 /*--------------------------------------------------------------*/
240 /*---------------- Info Dumping ----------------*/
241 /*--------------------------------------------------------------*/
242
243 @Override
244 public final boolean dumpKmersAsBytes(ByteStreamWriter bsw, int k, int mincount, int maxcount, AtomicLong remaining){
245 if(values==null){return true;}
246 if(remaining!=null && remaining.decrementAndGet()<0){return true;}
247 bsw.printlnKmer(pivot, values, k);
248 if(left!=null){left.dumpKmersAsBytes(bsw, k, mincount, maxcount, remaining);}
249 if(right!=null){right.dumpKmersAsBytes(bsw, k, mincount, maxcount, remaining);}
250 return true;
251 }
252
253 @Override
254 public final boolean dumpKmersAsBytes_MT(final ByteStreamWriter bsw, final ByteBuilder bb, final int k, final int mincount, int maxcount, AtomicLong remaining){
255 if(values==null){return true;}
256 if(remaining!=null && remaining.decrementAndGet()<0){return true;}
257 toBytes(pivot, values, k, bb);
258 bb.nl();
259 if(bb.length()>=16000){
260 ByteBuilder bb2=new ByteBuilder(bb);
261 synchronized(bsw){bsw.addJob(bb2);}
262 bb.clear();
263 }
264 if(left!=null){left.dumpKmersAsBytes_MT(bsw, bb, k, mincount, maxcount, remaining);}
265 if(right!=null){right.dumpKmersAsBytes_MT(bsw, bb, k, mincount, maxcount, remaining);}
266 return true;
267 }
268
269 @Override
270 protected final StringBuilder dumpKmersAsText(StringBuilder sb, int k, int mincount, int maxcount){
271 if(values==null){return sb;}
272 if(sb==null){sb=new StringBuilder(32);}
273 sb.append(AbstractKmerTable.toText(pivot, values, k)).append('\n');
274 if(left!=null){left.dumpKmersAsText(sb, k, mincount, maxcount);}
275 if(right!=null){right.dumpKmersAsText(sb, k, mincount, maxcount);}
276 return sb;
277 }
278
279 @Override
280 protected final ByteBuilder dumpKmersAsText(ByteBuilder bb, int k, int mincount, int maxcount){
281 if(values==null){return bb;}
282 if(bb==null){bb=new ByteBuilder(32);}
283 bb.append(AbstractKmerTable.toBytes(pivot, values, k)).append('\n');
284 if(left!=null){left.dumpKmersAsText(bb, k, mincount, maxcount);}
285 if(right!=null){right.dumpKmersAsText(bb, k, mincount, maxcount);}
286 return bb;
287 }
288
289 @Override
290 final boolean TWOD(){return true;}
291
292 /*--------------------------------------------------------------*/
293 /*---------------- Invalid Methods ----------------*/
294 /*--------------------------------------------------------------*/
295
296 /*--------------------------------------------------------------*/
297 /*---------------- Fields ----------------*/
298 /*--------------------------------------------------------------*/
299
300 int[] values;
301 private int numValues;
302 private static final int slowAddLimit=4;
303
304 }