File: BinSketcher.java

package info (click to toggle)
bbmap 39.20%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,024 kB
  • sloc: java: 312,743; sh: 18,099; python: 5,247; ansic: 2,074; perl: 96; makefile: 39; xml: 38
file content (296 lines) | stat: -rwxr-xr-x 9,468 bytes parent folder | download | duplicates (2)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package bin;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import json.JsonObject;
import json.JsonParser;
import shared.Shared;
import shared.Timer;
import shared.Tools;
import sketch.DisplayParams;
import sketch.SendSketch;
import sketch.Sketch;
import sketch.SketchMakerMini;
import sketch.SketchObject;
import sketch.SketchTool;
import stream.Read;
import tax.TaxTree;
import template.Accumulator;
import template.ThreadWaiter;

/**
 * Handles sketches and taxonomic assignments for contigs and clusters.
 * 
 * @author Brian Bushnell
 * @date December 11, 2024
 *
 */
public class BinSketcher extends BinObject implements Accumulator<BinSketcher.ProcessThread> {
	
	/*--------------------------------------------------------------*/
	/*----------------        Initialization        ----------------*/
	/*--------------------------------------------------------------*/
	
	public BinSketcher(int threads_, int minSize_){
		
		threads=Tools.min(threads_, Shared.threads());
		minSize=minSize_;
		
		if(sketchClusters || sketchContigs || sketchOutput){
			SketchObject.AUTOSIZE_LINEAR_DENSITY=sketchDensity;
			SketchObject.AUTOSIZE_LINEAR=true;
			SketchObject.AUTOSIZE=false;
			SketchObject.SET_AUTOSIZE=true;
			SketchObject.minSketchSize=5;
			
//			SketchObject.AUTOSIZE=false;
//			SketchObject.defaultParams.minKeyOccuranceCount=2;
			SketchObject.defaultParams.parse("trackcounts", "trackcounts", null);
//			SketchObject.defaultParams.minProb=0;
			SketchObject.postParse();
			SketchObject.defaultParams.maxRecords=2;
			SketchObject.defaultParams.taxLevel=TaxTree.GENUS;
			tool=new SketchTool(sketchSize, SketchObject.defaultParams);
		}else{
			tool=null;
		}
		
	}
	
	/*--------------------------------------------------------------*/
	/*----------------         Outer Methods        ----------------*/
	/*--------------------------------------------------------------*/
	
//	void sketchBins(ArrayList<Bin> input, boolean force) {
//		sketch(input, force);
//	}
	
	void sketch(ArrayList<? extends Sketchable> input, boolean force) {
		ArrayList<Sketchable> updateList=new ArrayList<Sketchable>();
		float mult=(force ? 1 : 2);
		for(Sketchable s : input) {
			if(s.size()>=minSize) {
				synchronized(s) {
					if(s.size()>mult*s.sketchedSize()) {
						s.clearTax();
						updateList.add(s);
					}
				}
			}
		}
		if(updateList.isEmpty()) {return;}
		spawnThreads(updateList);
	}
	
	/*--------------------------------------------------------------*/
	/*----------------       Thread Management      ----------------*/
	/*--------------------------------------------------------------*/
	
	/** Spawn process threads */
	private void spawnThreads(ArrayList<? extends Sketchable> list){
		Timer t=new Timer(outstream, true);
		outstream.print("Sketching "+list.size()+" elements: \t");
		
		//Do anything necessary prior to processing
		long bases=0;
		for(Sketchable s : list) {bases+=s.size();}
		
		//Determine how many threads may be used
		final int pthreads=(int)Tools.max(1, Tools.min(threads, Shared.threads(), list.size()/4, bases/40000));
		
		//Fill a list with ProcessThreads
		ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(pthreads);
		for(int i=0; i<pthreads; i++){
			alpt.add(new ProcessThread(list, i, pthreads));
		}
		assert(alpt.size()==pthreads);
		
		//Start the threads and wait for them to finish
		boolean success=ThreadWaiter.startAndWait(alpt, this);
		errorState&=!success;
		
		//Do anything necessary after processing
		t.stopAndPrint();
	}
	
	@Override
	public final void accumulate(ProcessThread pt){
//		linesProcessed+=pt.linesProcessedT;
//		bytesProcessed+=pt.bytesProcessedT;
//		linesOut+=pt.linesOutT;
//		bytesOut+=pt.bytesOutT;
		errorState|=(!pt.success);
		errorState|=(pt.errorStateT);
	}
	
	@Override
	public final boolean success(){return !errorState;}
	
	/*--------------------------------------------------------------*/
	/*----------------         Inner Methods        ----------------*/
	/*--------------------------------------------------------------*/
	
	/*--------------------------------------------------------------*/
	/*----------------         Inner Classes        ----------------*/
	/*--------------------------------------------------------------*/
	
	/** This class is static to prevent accidental writing to shared variables.
	 * It is safe to remove the static modifier. */
	class ProcessThread extends Thread {
		
		//Constructor
		ProcessThread(final ArrayList<? extends Sketchable> contigs_, final int tid_, final int threads_){
			contigs=contigs_;
			tid=tid_;
			threads=threads_;
			params=new DisplayParams();
			params.format=DisplayParams.FORMAT_JSON;
			params.taxLevel=TaxTree.GENUS;
			smm=new SketchMakerMini(tool, SketchObject.ONE_SKETCH, params);
		}
		
		//Called by start()
		@Override
		public void run(){
			//Do anything necessary prior to processing
			
			//Process the contigs
			if(sketchInBulk && (1+contigs.size()/threads)>2) {
				processInner_bulk();
			}else {
				processInner_oneByOne();
			}
			
			//Do anything necessary after processing
			
			//Indicate successful exit status
			success=true;
		}
		
		/** Iterate through the lines */
		void processInner_oneByOne(){
//			Timer t=new Timer();
			for(int i=tid; i<contigs.size(); i+=threads) {
				Sketchable c=contigs.get(i);
				synchronized(c) {
					assert(c.id()==i);
					Sketch sketch=c.toSketch(smm, dummy);

					String results=SendSketch.sendSketch(sketch, "refseq", params, 0);
					if(results==null) {continue;}

					JsonObject all=jp.parseJsonObject(results);
					c.setFrom(all);
					assert(c.sketchedSize()==c.size());
				}
			}
//			t.stop("Thread "+tid+" time: ");
		}
		
		/** Iterate through the lines */
		void processInner_bulk(){
			final int incr=sectionSize*threads;
			for(int i=tid; i<contigs.size(); i+=incr) {processSection(i, i+incr);}
		}
		
		/** Iterate through the lines */
		void processSection(final int from, int to){
			ArrayList<Sketch> sketches=new ArrayList<Sketch>(1+contigs.size()/threads);
			for(int i=from; i<contigs.size() && i<to; i+=threads) {
				Sketchable c=contigs.get(i);
				synchronized(c) {
					assert(c.id()==i);
					Sketch sketch=c.toSketch(smm, dummy);
					assert(sketch!=null) : "Handle null sketches.";
					sketches.add(sketch);//Note:  Could potentially be null?
				}
			}
//			t.stopAndStart("Thread "+tid+" sketch time: ");
			ArrayList<JsonObject> results=SendSketch.sendSketches(sketches, "refseq", params);
			assert(results.size() == sketches.size()) : results.size()+", "+sketches.size();
			for(int i=from, j=0; i<contigs.size() && i<to; i+=threads, j++) {
				Sketchable c=contigs.get(i);
				synchronized(c) {
					assert(c.id()==i);
					JsonObject jo=results.get(j);
					c.setFrom(jo);
					assert(jo==null || c.sketchedSize()==c.size());
				}
			}
		}

//		/** Number of reads processed by this thread */
//		protected long linesProcessedT=0;
//		/** Number of bases processed by this thread */
//		protected long bytesProcessedT=0;
//		
//		/** Number of reads retained by this thread */
//		protected long linesOutT=0;
//		/** Number of bases retained by this thread */
//		protected long bytesOutT=0;
		

		final Read dummy=new Read(null, null, null, 0);
		final JsonParser jp=new JsonParser();
		final DisplayParams params;
		
		protected boolean errorStateT=false;
		
		/** True only if this thread has completed successfully */
		boolean success=false;
		
		/** Input */
		private final ArrayList<? extends Sketchable> contigs;
		/** Thread ID */
		final int tid;
		/** Thread ID */
		final int threads;
		

		final SketchMakerMini smm;
	}
	
	/*--------------------------------------------------------------*/
	/*----------------            Fields            ----------------*/
	/*--------------------------------------------------------------*/
	
	long linesProcessed=0;
	long linesOut=0;
	long bytesProcessed=0;
	long bytesOut=0;

	
	private final SketchTool tool;
//	private final SketchMakerMini smm;
	private final int threads;
	final int minSize;
	
	/*--------------------------------------------------------------*/
	/*----------------        Static Fields         ----------------*/
	/*--------------------------------------------------------------*/
	
	static int sectionSize=100;
	
	/*--------------------------------------------------------------*/
	/*----------------         Final Fields         ----------------*/
	/*--------------------------------------------------------------*/
	
	@Override
	public final ReadWriteLock rwlock() {return rwlock;}
	private final ReadWriteLock rwlock=new ReentrantReadWriteLock();
	
	/*--------------------------------------------------------------*/
	/*----------------        Common Fields         ----------------*/
	/*--------------------------------------------------------------*/
	
	/** Print status messages to this output stream */
	private PrintStream outstream=System.err;
	/** Print verbose messages */
	public static boolean verbose=false;
	/** True if an error was encountered */
	public boolean errorState=false;
	
}