| 1 |
package bin; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.concurrent.locks.ReadWriteLock; |
| 6 |
|
| 7 |
import shared.KillSwitch; |
| 8 |
import shared.LineParserS1; |
| 9 |
import shared.LineParserS4; |
| 10 |
import shared.Shared; |
| 11 |
import shared.Tools; |
| 12 |
import stream.ConcurrentReadInputStream; |
| 13 |
import stream.Read; |
| 14 |
import structures.IntLongHashMap; |
| 15 |
import structures.ListNum; |
| 16 |
import template.Accumulator; |
| 17 |
import template.ThreadWaiter; |
| 18 |
import tracker.EntropyTracker; |
| 19 |
|
| 20 |
public class SpectraCounter implements Accumulator<SpectraCounter.LoadThread> { |
| 21 |
|
| 22 |
public SpectraCounter(PrintStream outstream_, boolean parseDepth_, |
| 23 |
boolean parseTID_, IntLongHashMap sizeMap_) { |
| 24 |
outstream=outstream_; |
| 25 |
parseDepth=parseDepth_; |
| 26 |
parseTID=parseTID_; |
| 27 |
sizeMap=sizeMap_; |
| 28 |
if(calcEntropy) { |
| 29 |
if(AdjustEntropy.kLoaded!=4 || AdjustEntropy.wLoaded!=150) { |
| 30 |
AdjustEntropy.load(4, 150); |
| 31 |
} |
| 32 |
assert(AdjustEntropy.kLoaded==4 && AdjustEntropy.wLoaded==150) : |
| 33 |
AdjustEntropy.kLoaded+", "+calcEntropy; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
public void makeSpectra(ArrayList<Contig> contigs, ConcurrentReadInputStream cris, int minlen){ |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
int threads=Tools.mid(1, cris==null ? contigs.size()/4 : 128, Shared.threads()); |
| 45 |
|
| 46 |
ArrayList<LoadThread> alpt=new ArrayList<LoadThread>(threads); |
| 47 |
for(int i=0; i<threads; i++){ |
| 48 |
LoadThread lt=new LoadThread(contigs, cris, minlen, i, threads); |
| 49 |
alpt.add(lt); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
boolean success=ThreadWaiter.startAndWait(alpt, this); |
| 54 |
errorState&=!success; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public synchronized void accumulate(LoadThread t) { |
| 62 |
synchronized(t) { |
| 63 |
errorState|=(t.success); |
| 64 |
contigsLoaded+=t.contigsLoadedT; |
| 65 |
basesLoaded+=t.basesLoadedT; |
| 66 |
contigsRetained+=t.contigsRetainedT; |
| 67 |
basesRetained+=t.basesRetainedT; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public ReadWriteLock rwlock() {return null;} |
| 73 |
|
| 74 |
@Override |
| 75 |
public synchronized boolean success() {return errorState;} |
| 76 |
|
| 77 |
class LoadThread extends Thread { |
| 78 |
|
| 79 |
LoadThread(ArrayList<Contig> contigs_, ConcurrentReadInputStream cris_, |
| 80 |
int minlen_, int tid_, int threads_) { |
| 81 |
contigs=contigs_; |
| 82 |
cris=cris_; |
| 83 |
minlen=minlen_; |
| 84 |
tid=tid_; |
| 85 |
threads=threads_; |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public void run() { |
| 90 |
synchronized(this) { |
| 91 |
runInner(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
private void runInner() { |
| 96 |
if(cris==null) { |
| 97 |
runOnContigs(); |
| 98 |
}else { |
| 99 |
runOnCris(); |
| 100 |
} |
| 101 |
success=true; |
| 102 |
} |
| 103 |
|
| 104 |
void runOnContigs() { |
| 105 |
for(int i=tid; i<contigs.size(); i+=threads) { |
| 106 |
Contig c=contigs.get(i); |
| 107 |
processContig(c); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
void runOnCris() { |
| 112 |
|
| 113 |
ListNum<Read> ln=cris.nextList(); |
| 114 |
|
| 115 |
|
| 116 |
while(ln!=null && ln.size()>0){ |
| 117 |
ArrayList<Contig> localContigs=new ArrayList<Contig>(ln.size()); |
| 118 |
for(Read r : ln) { |
| 119 |
Contig c=loadContig(r); |
| 120 |
if(c!=null) { |
| 121 |
processContig(c); |
| 122 |
localContigs.add(c); |
| 123 |
} |
| 124 |
} |
| 125 |
synchronized(contigs) {contigs.addAll(localContigs);} |
| 126 |
|
| 127 |
|
| 128 |
cris.returnList(ln); |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
ln=cris.nextList(); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
if(ln!=null){ |
| 137 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
Contig processRead(Read r) { |
| 142 |
Contig c=loadContig(r); |
| 143 |
if(c==null) {return null;} |
| 144 |
processContig(c); |
| 145 |
return c; |
| 146 |
} |
| 147 |
|
| 148 |
Contig loadContig(Read r) { |
| 149 |
contigsLoadedT++; |
| 150 |
basesLoadedT+=r.length(); |
| 151 |
int tid=-1; |
| 152 |
if(parseTID) { |
| 153 |
tid=DataLoader.parseTaxID(r.name()); |
| 154 |
if(tid>0) { |
| 155 |
synchronized(sizeMap) { |
| 156 |
sizeMap.increment(tid, r.length()); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
if(r.length()<minlen) {return null;} |
| 161 |
contigsRetainedT++; |
| 162 |
basesRetainedT+=r.length(); |
| 163 |
Contig c=new Contig(r.name(), r.bases, (int)r.numericID); |
| 164 |
synchronized(c) { |
| 165 |
c.labelTaxid=tid; |
| 166 |
} |
| 167 |
return c; |
| 168 |
} |
| 169 |
|
| 170 |
void processContig(Contig c) { |
| 171 |
synchronized(c) { |
| 172 |
|
| 173 |
contigsProcessedT++; |
| 174 |
basesProcessedT+=c.size(); |
| 175 |
c.loadCounts(); |
| 176 |
if(c.numDepths()>1) {c.fillNormDepth();} |
| 177 |
if(calcEntropy) { |
| 178 |
c.entropy=et.averageEntropy(c.bases, false); |
| 179 |
c.entropy=AdjustEntropy.compensate(c.gc(), c.entropy); |
| 180 |
} |
| 181 |
if(calcStrandedness) { |
| 182 |
c.dimers=new int[16]; |
| 183 |
c.strandedness=EntropyTracker.strandedness(c.bases, c.dimers, 2); |
| 184 |
} |
| 185 |
if(countTrimers) { |
| 186 |
c.trimers=new int[64]; |
| 187 |
EntropyTracker.countKmers(c.bases, c.trimers, 3); |
| 188 |
} |
| 189 |
if(parseDepth) { |
| 190 |
boolean b=DataLoader.parseAndSetDepth(c, lps, lpt); |
| 191 |
if(!b) { |
| 192 |
KillSwitch.kill("Could not parse depth from header "+c.name+ |
| 193 |
"\nThis program needs a sam file, a cov file, or labeled contigs."); |
| 194 |
} |
| 195 |
assert(b) : "Could not parse depth from "+c.name; |
| 196 |
} |
| 197 |
|
| 198 |
assert(c.tetramers!=null && c.numTetramers>0); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
final int tid; |
| 203 |
final int threads; |
| 204 |
final int minlen; |
| 205 |
final ArrayList<Contig> contigs; |
| 206 |
final ConcurrentReadInputStream cris; |
| 207 |
final EntropyTracker et=new EntropyTracker(entropyK, entropyWindow, false); |
| 208 |
|
| 209 |
boolean success=false; |
| 210 |
int contigsProcessedT=0; |
| 211 |
long basesProcessedT=0; |
| 212 |
LineParserS1 lps=new LineParserS1('_'); |
| 213 |
LineParserS4 lpt=new LineParserS4(",,=,"); |
| 214 |
|
| 215 |
int contigsLoadedT=0; |
| 216 |
long basesLoadedT=0; |
| 217 |
int contigsRetainedT=0; |
| 218 |
long basesRetainedT=0; |
| 219 |
} |
| 220 |
|
| 221 |
public PrintStream outstream=System.err; |
| 222 |
|
| 223 |
public final boolean parseDepth; |
| 224 |
public final boolean parseTID; |
| 225 |
public final IntLongHashMap sizeMap; |
| 226 |
|
| 227 |
public int contigsLoaded=0; |
| 228 |
public long basesLoaded=0; |
| 229 |
public int contigsRetained=0; |
| 230 |
public long basesRetained=0; |
| 231 |
|
| 232 |
public boolean errorState=false; |
| 233 |
public static boolean calcEntropy=true; |
| 234 |
public static boolean calcStrandedness=true; |
| 235 |
public static boolean countTrimers=true; |
| 236 |
public static int entropyK=4; |
| 237 |
public static int entropyWindow=150; |
| 238 |
|
| 239 |
} |