Debian Codegraph @

current/bin/BinSketcher.java

raw symbols SCIP
1 package bin;
2
3 import java.io.PrintStream;
4 import java.util.ArrayList;
5 import java.util.concurrent.locks.ReadWriteLock;
6 import java.util.concurrent.locks.ReentrantReadWriteLock;
7
8 import json.JsonObject;
9 import json.JsonParser;
10 import shared.Shared;
11 import shared.Timer;
12 import shared.Tools;
13 import sketch.DisplayParams;
14 import sketch.SendSketch;
15 import sketch.Sketch;
16 import sketch.SketchMakerMini;
17 import sketch.SketchObject;
18 import sketch.SketchTool;
19 import stream.Read;
20 import tax.TaxTree;
21 import template.Accumulator;
22 import template.ThreadWaiter;
23
24 /**
25 * Handles sketches and taxonomic assignments for contigs and clusters.
26 *
27 * @author Brian Bushnell
28 * @date December 11, 2024
29 *
30 */
31 public class BinSketcher extends BinObject implements Accumulator<BinSketcher.ProcessThread> {
32
33 /*--------------------------------------------------------------*/
34 /*---------------- Initialization ----------------*/
35 /*--------------------------------------------------------------*/
36
37 public BinSketcher(int threads_, int minSize_){
38
39 threads=Tools.min(threads_, Shared.threads());
40 minSize=minSize_;
41
42 if(sketchClusters || sketchContigs || sketchOutput){
43 SketchObject.AUTOSIZE_LINEAR_DENSITY=sketchDensity;
44 SketchObject.AUTOSIZE_LINEAR=true;
45 SketchObject.AUTOSIZE=false;
46 SketchObject.SET_AUTOSIZE=true;
47 SketchObject.minSketchSize=5;
48
49 // SketchObject.AUTOSIZE=false;
50 // SketchObject.defaultParams.minKeyOccuranceCount=2;
51 SketchObject.defaultParams.parse("trackcounts", "trackcounts", null);
52 // SketchObject.defaultParams.minProb=0;
53 SketchObject.postParse();
54 SketchObject.defaultParams.maxRecords=2;
55 SketchObject.defaultParams.taxLevel=TaxTree.GENUS;
56 tool=new SketchTool(sketchSize, SketchObject.defaultParams);
57 }else{
58 tool=null;
59 }
60
61 }
62
63 /*--------------------------------------------------------------*/
64 /*---------------- Outer Methods ----------------*/
65 /*--------------------------------------------------------------*/
66
67 // void sketchBins(ArrayList<Bin> input, boolean force) {
68 // sketch(input, force);
69 // }
70
71 void sketch(ArrayList<? extends Sketchable> input, boolean force) {
72 ArrayList<Sketchable> updateList=new ArrayList<Sketchable>();
73 float mult=(force ? 1 : 2);
74 for(Sketchable s : input) {
75 if(s.size()>=minSize) {
76 synchronized(s) {
77 if(s.size()>mult*s.sketchedSize()) {
78 s.clearTax();
79 updateList.add(s);
80 }
81 }
82 }
83 }
84 if(updateList.isEmpty()) {return;}
85 spawnThreads(updateList);
86 }
87
88 /*--------------------------------------------------------------*/
89 /*---------------- Thread Management ----------------*/
90 /*--------------------------------------------------------------*/
91
92 /** Spawn process threads */
93 private void spawnThreads(ArrayList<? extends Sketchable> list){
94 Timer t=new Timer(outstream, true);
95 outstream.print("Sketching "+list.size()+" elements: \t");
96
97 //Do anything necessary prior to processing
98 long bases=0;
99 for(Sketchable s : list) {bases+=s.size();}
100
101 //Determine how many threads may be used
102 final int pthreads=(int)Tools.max(1, Tools.min(threads, Shared.threads(), list.size()/4, bases/40000));
103
104 //Fill a list with ProcessThreads
105 ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(pthreads);
106 for(int i=0; i<pthreads; i++){
107 alpt.add(new ProcessThread(list, i, pthreads));
108 }
109 assert(alpt.size()==pthreads);
110
111 //Start the threads and wait for them to finish
112 boolean success=ThreadWaiter.startAndWait(alpt, this);
113 errorState&=!success;
114
115 //Do anything necessary after processing
116 t.stopAndPrint();
117 }
118
119 @Override
120 public final void accumulate(ProcessThread pt){
121 // linesProcessed+=pt.linesProcessedT;
122 // bytesProcessed+=pt.bytesProcessedT;
123 // linesOut+=pt.linesOutT;
124 // bytesOut+=pt.bytesOutT;
125 errorState|=(!pt.success);
126 errorState|=(pt.errorStateT);
127 }
128
129 @Override
130 public final boolean success(){return !errorState;}
131
132 /*--------------------------------------------------------------*/
133 /*---------------- Inner Methods ----------------*/
134 /*--------------------------------------------------------------*/
135
136 /*--------------------------------------------------------------*/
137 /*---------------- Inner Classes ----------------*/
138 /*--------------------------------------------------------------*/
139
140 /** This class is static to prevent accidental writing to shared variables.
141 * It is safe to remove the static modifier. */
142 class ProcessThread extends Thread {
143
144 //Constructor
145 ProcessThread(final ArrayList<? extends Sketchable> contigs_, final int tid_, final int threads_){
146 contigs=contigs_;
147 tid=tid_;
148 threads=threads_;
149 params=new DisplayParams();
150 params.format=DisplayParams.FORMAT_JSON;
151 params.taxLevel=TaxTree.GENUS;
152 smm=new SketchMakerMini(tool, SketchObject.ONE_SKETCH, params);
153 }
154
155 //Called by start()
156 @Override
157 public void run(){
158 //Do anything necessary prior to processing
159
160 //Process the contigs
161 if(sketchInBulk && (1+contigs.size()/threads)>2) {
162 processInner_bulk();
163 }else {
164 processInner_oneByOne();
165 }
166
167 //Do anything necessary after processing
168
169 //Indicate successful exit status
170 success=true;
171 }
172
173 /** Iterate through the lines */
174 void processInner_oneByOne(){
175 // Timer t=new Timer();
176 for(int i=tid; i<contigs.size(); i+=threads) {
177 Sketchable c=contigs.get(i);
178 synchronized(c) {
179 assert(c.id()==i);
180 Sketch sketch=c.toSketch(smm, dummy);
181
182 String results=SendSketch.sendSketch(sketch, "refseq", params, 0);
183 if(results==null) {continue;}
184
185 JsonObject all=jp.parseJsonObject(results);
186 c.setFrom(all);
187 assert(c.sketchedSize()==c.size());
188 }
189 }
190 // t.stop("Thread "+tid+" time: ");
191 }
192
193 /** Iterate through the lines */
194 void processInner_bulk(){
195 final int incr=sectionSize*threads;
196 for(int i=tid; i<contigs.size(); i+=incr) {processSection(i, i+incr);}
197 }
198
199 /** Iterate through the lines */
200 void processSection(final int from, int to){
201 ArrayList<Sketch> sketches=new ArrayList<Sketch>(1+contigs.size()/threads);
202 for(int i=from; i<contigs.size() && i<to; i+=threads) {
203 Sketchable c=contigs.get(i);
204 synchronized(c) {
205 assert(c.id()==i);
206 Sketch sketch=c.toSketch(smm, dummy);
207 assert(sketch!=null) : "Handle null sketches.";
208 sketches.add(sketch);//Note: Could potentially be null?
209 }
210 }
211 // t.stopAndStart("Thread "+tid+" sketch time: ");
212 ArrayList<JsonObject> results=SendSketch.sendSketches(sketches, "refseq", params);
213 assert(results.size() == sketches.size()) : results.size()+", "+sketches.size();
214 for(int i=from, j=0; i<contigs.size() && i<to; i+=threads, j++) {
215 Sketchable c=contigs.get(i);
216 synchronized(c) {
217 assert(c.id()==i);
218 JsonObject jo=results.get(j);
219 c.setFrom(jo);
220 assert(jo==null || c.sketchedSize()==c.size());
221 }
222 }
223 }
224
225 // /** Number of reads processed by this thread */
226 // protected long linesProcessedT=0;
227 // /** Number of bases processed by this thread */
228 // protected long bytesProcessedT=0;
229 //
230 // /** Number of reads retained by this thread */
231 // protected long linesOutT=0;
232 // /** Number of bases retained by this thread */
233 // protected long bytesOutT=0;
234
235
236 final Read dummy=new Read(null, null, null, 0);
237 final JsonParser jp=new JsonParser();
238 final DisplayParams params;
239
240 protected boolean errorStateT=false;
241
242 /** True only if this thread has completed successfully */
243 boolean success=false;
244
245 /** Input */
246 private final ArrayList<? extends Sketchable> contigs;
247 /** Thread ID */
248 final int tid;
249 /** Thread ID */
250 final int threads;
251
252
253 final SketchMakerMini smm;
254 }
255
256 /*--------------------------------------------------------------*/
257 /*---------------- Fields ----------------*/
258 /*--------------------------------------------------------------*/
259
260 long linesProcessed=0;
261 long linesOut=0;
262 long bytesProcessed=0;
263 long bytesOut=0;
264
265
266 private final SketchTool tool;
267 // private final SketchMakerMini smm;
268 private final int threads;
269 final int minSize;
270
271 /*--------------------------------------------------------------*/
272 /*---------------- Static Fields ----------------*/
273 /*--------------------------------------------------------------*/
274
275 static int sectionSize=100;
276
277 /*--------------------------------------------------------------*/
278 /*---------------- Final Fields ----------------*/
279 /*--------------------------------------------------------------*/
280
281 @Override
282 public final ReadWriteLock rwlock() {return rwlock;}
283 private final ReadWriteLock rwlock=new ReentrantReadWriteLock();
284
285 /*--------------------------------------------------------------*/
286 /*---------------- Common Fields ----------------*/
287 /*--------------------------------------------------------------*/
288
289 /** Print status messages to this output stream */
290 private PrintStream outstream=System.err;
291 /** Print verbose messages */
292 public static boolean verbose=false;
293 /** True if an error was encountered */
294 public boolean errorState=false;
295
296 }