| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class BinSketcher extends BinObject implements Accumulator<BinSketcher.ProcessThread> { |
| 32 |
|
| 33 |
|
| 34 |
|
| 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 |
|
| 50 |
|
| 51 |
SketchObject.defaultParams.parse("trackcounts", "trackcounts", null); |
| 52 |
|
| 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 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 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 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 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 |
|
| 98 |
long bases=0; |
| 99 |
for(Sketchable s : list) {bases+=s.size();} |
| 100 |
|
| 101 |
|
| 102 |
final int pthreads=(int)Tools.max(1, Tools.min(threads, Shared.threads(), list.size()/4, bases/40000)); |
| 103 |
|
| 104 |
|
| 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 |
|
| 112 |
boolean success=ThreadWaiter.startAndWait(alpt, this); |
| 113 |
errorState&=!success; |
| 114 |
|
| 115 |
|
| 116 |
t.stopAndPrint(); |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public final void accumulate(ProcessThread pt){ |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
errorState|=(!pt.success); |
| 126 |
errorState|=(pt.errorStateT); |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public final boolean success(){return !errorState;} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
class ProcessThread extends Thread { |
| 143 |
|
| 144 |
|
| 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 |
|
| 156 |
@Override |
| 157 |
public void run(){ |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
if(sketchInBulk && (1+contigs.size()/threads)>2) { |
| 162 |
processInner_bulk(); |
| 163 |
}else { |
| 164 |
processInner_oneByOne(); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
success=true; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
void processInner_oneByOne(){ |
| 175 |
|
| 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 |
|
| 191 |
} |
| 192 |
|
| 193 |
|
| 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 |
|
| 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); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 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 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 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 |
|
| 243 |
boolean success=false; |
| 244 |
|
| 245 |
|
| 246 |
private final ArrayList<? extends Sketchable> contigs; |
| 247 |
|
| 248 |
final int tid; |
| 249 |
|
| 250 |
final int threads; |
| 251 |
|
| 252 |
|
| 253 |
final SketchMakerMini smm; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 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 |
|
| 268 |
private final int threads; |
| 269 |
final int minSize; |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
static int sectionSize=100; |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
@Override |
| 282 |
public final ReadWriteLock rwlock() {return rwlock;} |
| 283 |
private final ReadWriteLock rwlock=new ReentrantReadWriteLock(); |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
private PrintStream outstream=System.err; |
| 291 |
|
| 292 |
public static boolean verbose=false; |
| 293 |
|
| 294 |
public boolean errorState=false; |
| 295 |
|
| 296 |
} |