| 1 |
package barcode; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Collection; |
| 5 |
import java.util.HashMap; |
| 6 |
|
| 7 |
import shared.Shared; |
| 8 |
import shared.Timer; |
| 9 |
import shared.Tools; |
| 10 |
import structures.ByteBuilder; |
| 11 |
import template.Accumulator; |
| 12 |
import template.ThreadWaiter; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class PCRMatrixHDist extends PCRMatrix implements Accumulator<PCRMatrixHDist.PopThread> { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public PCRMatrixHDist(int length1_, int length2_, int delimiter_, boolean hdistSum_) { |
| 29 |
super(length1_, length2_, delimiter_, hdistSum_); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public static boolean parseStatic(String arg, String a, String b){ |
| 37 |
if(a.equals("maxhdist") || a.equals("hdist") || a.equals("maxhdist0") || a.equals("hdist0")){ |
| 38 |
maxHDist0=Integer.parseInt(b); |
| 39 |
}else if(a.equals("clearzone") || a.equals("cz") || a.equals("clearzone0") || a.equals("cz0")){ |
| 40 |
clearzone0=Integer.parseInt(b); |
| 41 |
}else if(a.equals("parse_flag_goes_here")){ |
| 42 |
|
| 43 |
}else{ |
| 44 |
return false; |
| 45 |
} |
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public boolean parse(String arg, String a, String b) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
public static void postParseStatic(){} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
@Override |
| 61 |
public Barcode findClosest(String s) { |
| 62 |
return length2<1 ? findClosestSingleHDist(s, maxHDist0, clearzone0) : |
| 63 |
findClosestDualHDist(s, maxHDist0, clearzone0); |
| 64 |
} |
| 65 |
|
| 66 |
public Barcode findClosest(String s, int maxHDist, int clearzone) { |
| 67 |
return length2<1 ? findClosestSingleHDist(s, maxHDist, clearzone) : |
| 68 |
findClosestDualHDist(s, maxHDist, clearzone); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void makeProbs() { |
| 73 |
throw new RuntimeException("This class does not support this method."); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void initializeData() {} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void refine(Collection<Barcode> codeCounts, long minCount) {} |
| 81 |
|
| 82 |
@Override |
| 83 |
public HashMap<String, String> makeAssignmentMap(Collection<Barcode> codeCounts, long minCount) { |
| 84 |
Timer t=new Timer(); |
| 85 |
assert(expectedList!=null && expectedList.size()>0) : expectedList; |
| 86 |
assert(codeCounts!=null); |
| 87 |
ArrayList<Barcode> list=highpass(codeCounts, minCount); |
| 88 |
HashMap<String, String> map=new HashMap<String, String>(Tools.max(200, list.size()/10)); |
| 89 |
totalCounted=totalAssigned=totalAssignedToExpected=0; |
| 90 |
final long ops=list.size()*(long)expectedList.size(); |
| 91 |
if(list.size()<2 || ops<100000 || Shared.threads()<2) { |
| 92 |
for(Barcode query : list) { |
| 93 |
final String s=query.name; |
| 94 |
assert(s.length()==counts.length); |
| 95 |
Barcode ref=findClosest(s); |
| 96 |
final long count=query.count(); |
| 97 |
totalCounted+=count; |
| 98 |
if(ref!=null) { |
| 99 |
totalAssigned+=count; |
| 100 |
if(ref.expected==1) { |
| 101 |
totalAssignedToExpected+=count; |
| 102 |
map.put(s, ref.name); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
}else { |
| 107 |
populateCountsMT(list, maxHDist0, clearzone0, map); |
| 108 |
} |
| 109 |
t.stop(); |
| 110 |
if(verbose) { |
| 111 |
if(verbose) { |
| 112 |
System.err.println("Pair Assignment Rate: \tTotal\tGood\tBad"); |
| 113 |
} |
| 114 |
System.err.println(String.format("Final Assignment Rate: \t%.4f\t%.4f\t%.6f", |
| 115 |
assignedFraction(), expectedFraction(), chimericFraction())+"\t"+t.timeInSeconds(2)+"s"); |
| 116 |
} |
| 117 |
return map; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
@Override |
| 125 |
public void populateCounts(ArrayList<Barcode> list, long minCount) { |
| 126 |
assert(minCount<2) : "TODO"; |
| 127 |
assert(expectedList!=null && expectedList.size()>0) : expectedList; |
| 128 |
assert(list!=null); |
| 129 |
final long ops=list.size()*(long)expectedList.size(); |
| 130 |
if(list.size()<2 || ops<100000 || Shared.threads()<2) { |
| 131 |
populateCountsST(list, maxHDist0, clearzone0); |
| 132 |
}else { |
| 133 |
populateCountsMT(list, maxHDist0, clearzone0, null); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
private void populateCountsST(ArrayList<Barcode> countList, |
| 138 |
int maxHDist, int clearzone) { |
| 139 |
for(Barcode query : countList) { |
| 140 |
final String s=query.name; |
| 141 |
assert(s.length()==counts.length); |
| 142 |
Barcode ref=findClosest(s, maxHDist, clearzone); |
| 143 |
add(s, ref, query.count()); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
private void populateCountsMT(ArrayList<Barcode> list, |
| 148 |
int maxHDist, int clearzone, HashMap<String, String> map) { |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
final int threads=Tools.mid(1, Tools.min(matrixThreads, Shared.threads()), list.size()/8); |
| 153 |
|
| 154 |
|
| 155 |
ArrayList<PopThread> alpt=new ArrayList<PopThread>(threads); |
| 156 |
for(int i=0; i<threads; i++){ |
| 157 |
alpt.add(new PopThread(list, maxHDist, clearzone, map, i, threads)); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
boolean success=ThreadWaiter.startAndWait(alpt, this); |
| 162 |
errorState&=!success; |
| 163 |
|
| 164 |
|
| 165 |
if(localCounts && map!=null) { |
| 166 |
for(PopThread pt : alpt) { |
| 167 |
synchronized(pt) {map.putAll(pt.map);} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public void populateUnexpected() {assert(false) : "Not valid.";} |
| 174 |
|
| 175 |
@Override |
| 176 |
public ByteBuilder toBytesProb(ByteBuilder bb) { |
| 177 |
throw new RuntimeException("This class does not support this method."); |
| 178 |
} |
| 179 |
|
| 180 |
protected boolean valid() {return true;} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
final class PopThread extends Thread { |
| 185 |
|
| 186 |
public PopThread(ArrayList<Barcode> list_, |
| 187 |
int maxHDist_, int clearzone_, HashMap<String, String> map_, int tid_, int threads_) { |
| 188 |
list=list_; |
| 189 |
maxHDist=maxHDist_; |
| 190 |
clearzone=clearzone_; |
| 191 |
tid=tid_; |
| 192 |
threads=threads_; |
| 193 |
map=(map_==null ? null : localCounts ? new HashMap<String, String>() : map_); |
| 194 |
countsT=(localCounts ? new long[length][5][5] : null); |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public void run() { |
| 199 |
for(int i=tid; i<list.size(); i+=threads) { |
| 200 |
Barcode query=list.get(i); |
| 201 |
final String s=query.name; |
| 202 |
assert(s.length()==length); |
| 203 |
Barcode ref=findClosest(s, maxHDist, clearzone); |
| 204 |
if(localCounts) { |
| 205 |
addT(s, ref, query.count()); |
| 206 |
if(map!=null && ref!=null && ref.expected==1) {map.put(s, ref.name);} |
| 207 |
}else { |
| 208 |
synchronized(counts) { |
| 209 |
add(s, ref, query.count()); |
| 210 |
if(map!=null && ref!=null && ref.expected==1) {map.put(s, ref.name);} |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
public void addT(String query, Barcode ref, long count) { |
| 217 |
assert(ref==null || ref.length()==countsT.length); |
| 218 |
for(int i=0; i<query.length(); i++) { |
| 219 |
final int q=query.charAt(i), r=(ref==null ? 'N' : ref.charAt(i)); |
| 220 |
final byte xq=baseToNumber[q], xr=baseToNumber[r]; |
| 221 |
countsT[i][xq][xr]+=count; |
| 222 |
} |
| 223 |
totalCountedT+=count; |
| 224 |
if(ref!=null) { |
| 225 |
ref.incrementSync(count); |
| 226 |
totalAssignedT+=count; |
| 227 |
totalAssignedToExpectedT+=ref.expected*count; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
final ArrayList<Barcode> list; |
| 232 |
final int maxHDist; |
| 233 |
final int clearzone; |
| 234 |
final int tid; |
| 235 |
final int threads; |
| 236 |
final HashMap<String, String> map; |
| 237 |
|
| 238 |
final long[][][] countsT; |
| 239 |
long totalCountedT; |
| 240 |
long totalAssignedT; |
| 241 |
long totalAssignedToExpectedT; |
| 242 |
} |
| 243 |
|
| 244 |
@Override |
| 245 |
public final void accumulate(PopThread t) { |
| 246 |
if(localCounts) { |
| 247 |
synchronized(t) { |
| 248 |
Tools.add(counts, t.countsT); |
| 249 |
totalCounted+=t.totalCountedT; |
| 250 |
totalAssigned+=t.totalAssignedT; |
| 251 |
totalAssignedToExpected+=t.totalAssignedToExpectedT; |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
@Override |
| 257 |
public boolean success() { |
| 258 |
return !errorState; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
static int maxHDist0=2; |
| 264 |
static int clearzone0=1; |
| 265 |
|
| 266 |
} |