| 1 |
package barcode; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Collection; |
| 5 |
import java.util.Collections; |
| 6 |
import java.util.HashMap; |
| 7 |
import java.util.Map.Entry; |
| 8 |
|
| 9 |
import align2.BandedAligner; |
| 10 |
import dna.AminoAcid; |
| 11 |
import shared.Tools; |
| 12 |
import structures.ByteBuilder; |
| 13 |
|
| 14 |
public class Barcode implements Comparable<Barcode> { |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public Barcode(String s){this(s, 0, 1, 0);} |
| 21 |
public Barcode(String s, long c){this(s, c, 1, 0);} |
| 22 |
public Barcode(String s, long c, int e){this(s, c, e, 0);} |
| 23 |
public Barcode(String s, long c, int e, int t){ |
| 24 |
name=s; |
| 25 |
count=c; |
| 26 |
expected=e; |
| 27 |
tile=t; |
| 28 |
assert(expected==0 || expected==1); |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public Barcode getBarcodeForPairnum(int pairnum, char delimiter) { |
| 37 |
return new Barcode(getStringForPairnum(pairnum, delimiter), count); |
| 38 |
} |
| 39 |
public String getStringForPairnum(int pairnum, char delimiter) { |
| 40 |
assert(pairnum==0 || pairnum==1); |
| 41 |
final int pos, pos2; |
| 42 |
if(delimiter>0){ |
| 43 |
pos=name.indexOf(delimiter); |
| 44 |
pos2=pos+1; |
| 45 |
assert(pos>=0) : pos+", "+Character.toString(delimiter)+", "+name; |
| 46 |
}else{ |
| 47 |
boolean even=((name.length()&1)==0); |
| 48 |
pos=(name.length()/2)+1; |
| 49 |
pos2=(even ? pos : pos+1); |
| 50 |
} |
| 51 |
String s=(pairnum==0 ? name.substring(0, pos) : name.substring(pos2, name.length())); |
| 52 |
return s; |
| 53 |
} |
| 54 |
|
| 55 |
public Barcode left(char delimiter){return getBarcodeForPairnum(0, delimiter);} |
| 56 |
public Barcode right(char delimiter){return getBarcodeForPairnum(1, delimiter);} |
| 57 |
public String leftString(char delimiter){return getStringForPairnum(0, delimiter);} |
| 58 |
public String rightString(char delimiter){return getStringForPairnum(1, delimiter);} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public int countUndefined() { |
| 63 |
int sum=0; |
| 64 |
for(int i=0; i<length(); i++){ |
| 65 |
char c=charAt(i); |
| 66 |
sum+=(Tools.isLetter(c) && !AminoAcid.isFullyDefined(c)) ? 1 : 0; |
| 67 |
} |
| 68 |
return sum; |
| 69 |
} |
| 70 |
|
| 71 |
public boolean isHomopolymer() {return isHomopolymer(name, name.charAt(0));} |
| 72 |
public boolean isHomopolymer(char h) {return isHomopolymer(name);} |
| 73 |
public static boolean isHomopolymer(String name) {return isHomopolymer(name, name.charAt(0));} |
| 74 |
public static boolean isHomopolymer(String name, char h) { |
| 75 |
for(int i=0; i<name.length(); i++) { |
| 76 |
char c=name.charAt(i); |
| 77 |
if(c!=h && Tools.isLetter(c)) {return false;} |
| 78 |
} |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
public byte checkHomopolymer() { |
| 84 |
int match=0; |
| 85 |
int nonletter=0; |
| 86 |
final byte mer=AminoAcid.baseToNumber[charAt(0)]; |
| 87 |
if(mer<0) {return mer;} |
| 88 |
for(int i=1; i<length(); i++){ |
| 89 |
char c=charAt(i); |
| 90 |
byte n=AminoAcid.baseToNumber[c]; |
| 91 |
if(n==mer) { |
| 92 |
match++; |
| 93 |
}else if(Tools.isLetter(c)) { |
| 94 |
return -1; |
| 95 |
}else { |
| 96 |
nonletter++; |
| 97 |
} |
| 98 |
} |
| 99 |
return (match>0 && nonletter<=1 ? mer : 0); |
| 100 |
} |
| 101 |
|
| 102 |
public static final boolean nonLetter(int a, int b) { |
| 103 |
return !Tools.isLetter(a) && !Tools.isLetter(b); |
| 104 |
} |
| 105 |
|
| 106 |
public int hdist(final Barcode b){return hdist(name, b.name);} |
| 107 |
public int hdist(final String b){return hdist(name, b);} |
| 108 |
public int hdist(final byte[] b){return hdist(name, b);} |
| 109 |
public int hdistL(final Barcode b){return hdistL(name, b.name);} |
| 110 |
public int hdistL(final String b){return hdistL(name, b);} |
| 111 |
public int hdistR(final Barcode b){return hdistR(name, b.name);} |
| 112 |
public int hdistR(final String b){return hdistR(name, b);} |
| 113 |
public static int hdist(final byte[] a, final byte[] b){ |
| 114 |
assert(a.length==b.length) : "'"+new String(a)+"', '"+new String(b)+"'"; |
| 115 |
final int min=Tools.min(a.length, b.length); |
| 116 |
int subs=0; |
| 117 |
for(int i=0; i<min; i++){ |
| 118 |
subs+=(a[i]==b[i] ? 0 : 1); |
| 119 |
|
| 120 |
} |
| 121 |
return subs; |
| 122 |
} |
| 123 |
public static int hdist(final String a, final String b){ |
| 124 |
|
| 125 |
assert(a.length()==b.length() || (Tools.endsWithLetter(a)!=Tools.endsWithLetter(b) |
| 126 |
&& Tools.startsWithLetter(a) && Tools.startsWithLetter(b))) |
| 127 |
: "'"+a+"', '"+b+"'"; |
| 128 |
final int min=Tools.min(a.length(), b.length()); |
| 129 |
int subs=0; |
| 130 |
for(int i=0; i<min; i++){ |
| 131 |
final char ca=a.charAt(i), cb=b.charAt(i); |
| 132 |
subs+=(ca==cb ? 0 : 1); |
| 133 |
|
| 134 |
} |
| 135 |
return subs; |
| 136 |
} |
| 137 |
public static int hdist(final String a, final byte[] b){ |
| 138 |
assert(a.length()==b.length || (Tools.endsWithLetter(a)!=Tools.endsWithLetter(b) |
| 139 |
&& Tools.startsWithLetter(a) && Tools.startsWithLetter(b))) |
| 140 |
: "'"+a+"', '"+b+"'"; |
| 141 |
final int min=Tools.min(a.length(), b.length); |
| 142 |
int subs=0; |
| 143 |
for(int i=0; i<min; i++){ |
| 144 |
final int ca=a.charAt(i), cb=b[i]; |
| 145 |
subs+=(ca==cb ? 0 : 1); |
| 146 |
|
| 147 |
} |
| 148 |
return subs; |
| 149 |
} |
| 150 |
public static int hdistL(final String a, final String b, int len1){ |
| 151 |
assert(a.length()==b.length() || (Tools.endsWithLetter(a)!=Tools.endsWithLetter(b) |
| 152 |
&& Tools.startsWithLetter(a) && Tools.startsWithLetter(b))) |
| 153 |
: "'"+a+"', '"+b+"'"; |
| 154 |
int subs=0; |
| 155 |
for(int i=0; i<len1; i++){ |
| 156 |
final char ca=a.charAt(i), cb=b.charAt(i); |
| 157 |
subs+=(ca==cb ? 0 : 1); |
| 158 |
} |
| 159 |
return subs; |
| 160 |
} |
| 161 |
public static int hdistR(final String a, final String b, int len2){ |
| 162 |
assert(a.length()==b.length() || (Tools.endsWithLetter(a)!=Tools.endsWithLetter(b) |
| 163 |
&& Tools.startsWithLetter(a) && Tools.startsWithLetter(b))) |
| 164 |
: "'"+a+"', '"+b+"'"; |
| 165 |
int subs=0; |
| 166 |
final int minlen=Tools.min(a.length(), b.length()); |
| 167 |
for(int i=minlen-1, min=minlen-len2; i>=min; i--){ |
| 168 |
final char ca=a.charAt(i), cb=b.charAt(i); |
| 169 |
subs+=(ca==cb ? 0 : 1); |
| 170 |
} |
| 171 |
return subs; |
| 172 |
} |
| 173 |
public static int hdistL(final String a, final String b){ |
| 174 |
assert(a.length()==b.length() || (Tools.endsWithLetter(a)!=Tools.endsWithLetter(b) |
| 175 |
&& Tools.startsWithLetter(a) && Tools.startsWithLetter(b))) |
| 176 |
: "'"+a+"', '"+b+"'"; |
| 177 |
int subs=0; |
| 178 |
for(int i=0, max=a.length(); i<max; i++){ |
| 179 |
final char ca=a.charAt(i), cb=b.charAt(i); |
| 180 |
if(!Tools.isLetter(ca)) {break;} |
| 181 |
subs+=(ca==cb ? 0 : 1); |
| 182 |
} |
| 183 |
return subs; |
| 184 |
} |
| 185 |
public static int hdistR(final String a, final String b){ |
| 186 |
assert(a.length()==b.length() || (Tools.endsWithLetter(a)!=Tools.endsWithLetter(b) |
| 187 |
&& Tools.startsWithLetter(a) && Tools.startsWithLetter(b))) |
| 188 |
: "'"+a+"', '"+b+"'"; |
| 189 |
final int minlen=Tools.min(a.length(), b.length()); |
| 190 |
int subs=0; |
| 191 |
for(int i=minlen-1; i>=0; i--){ |
| 192 |
final char ca=a.charAt(i), cb=b.charAt(i); |
| 193 |
if(!Tools.isLetter(ca)) { |
| 194 |
|
| 195 |
assert(!Tools.isDigit(ca)); |
| 196 |
break; |
| 197 |
} |
| 198 |
subs+=(ca==cb ? 0 : 1); |
| 199 |
} |
| 200 |
return subs; |
| 201 |
} |
| 202 |
|
| 203 |
public int edist(final Barcode b, BandedAligner bandy){return edist(b.name, bandy);} |
| 204 |
public int edist(final String b, BandedAligner bandy){ |
| 205 |
int dist=hdist(name, b); |
| 206 |
if(dist>1){dist=bandy.alignForward(getBytes(), b.getBytes(), 0, 0, length(), true);} |
| 207 |
return dist; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public static HashMap<String, Barcode> barcodesToMap(Collection<Barcode> codes) { |
| 214 |
HashMap<String, Barcode> countMap=new HashMap<String, Barcode>(); |
| 215 |
for(Barcode b : codes) {countMap.put(b.name, b);} |
| 216 |
return countMap; |
| 217 |
} |
| 218 |
|
| 219 |
public static ArrayList<Barcode> summateAssignments(HashMap<String, String> assignmentMap, |
| 220 |
ArrayList<Barcode> expectedCodeList, HashMap<String, Barcode> countMap) { |
| 221 |
ArrayList<String> list=new ArrayList<String>(expectedCodeList.size()); |
| 222 |
for(Barcode b : expectedCodeList) { |
| 223 |
if(b.expected==1) {list.add(b.name);} |
| 224 |
} |
| 225 |
return summateAssignments(assignmentMap, list, countMap); |
| 226 |
} |
| 227 |
|
| 228 |
public static ArrayList<Barcode> summateAssignments(HashMap<String, String> assignmentMap, |
| 229 |
Collection<String> expectedCodeList, Collection<Barcode> counts) { |
| 230 |
HashMap<String, Barcode> countMap=barcodesToMap(counts); |
| 231 |
return summateAssignments(assignmentMap, expectedCodeList, countMap); |
| 232 |
} |
| 233 |
|
| 234 |
public static ArrayList<Barcode> summateAssignments(HashMap<String, String> assignmentMap, |
| 235 |
Collection<String> expectedCodeList, HashMap<String, Barcode> countMap) { |
| 236 |
|
| 237 |
HashMap<String, Barcode> sumMap=new HashMap<String, Barcode>(); |
| 238 |
for(String s : expectedCodeList) { |
| 239 |
sumMap.put(s, new Barcode(s)); |
| 240 |
} |
| 241 |
for(Entry<String, String> e : assignmentMap.entrySet()) { |
| 242 |
String observed=e.getKey(), assigned=e.getValue(); |
| 243 |
Barcode sum=sumMap.get(assigned); |
| 244 |
if(sum==null) { |
| 245 |
sumMap.put(assigned, sum=new Barcode(assigned)); |
| 246 |
} |
| 247 |
Barcode count=countMap.get(observed); |
| 248 |
if(count!=null) { |
| 249 |
sum.increment(count); |
| 250 |
} |
| 251 |
} |
| 252 |
ArrayList<Barcode> list=new ArrayList<Barcode>(sumMap.size()); |
| 253 |
list.addAll(sumMap.values()); |
| 254 |
Collections.sort(list); |
| 255 |
return list; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
public static String parseBarcodeFromFname(String fname) { |
| 260 |
String[] split=Tools.dotPattern.split(fname); |
| 261 |
for(String s : split) { |
| 262 |
if(isBarcode(s)) {return s;} |
| 263 |
else if("UNKNOWN".equalsIgnoreCase(s)) {return s;} |
| 264 |
} |
| 265 |
|
| 266 |
return null; |
| 267 |
} |
| 268 |
|
| 269 |
public static final boolean isBarcodeSymbol(char x) {return AminoAcid.isACGTN(x) || x=='-' || x=='+';} |
| 270 |
public static final boolean isBarcodeSymbol(byte x) {return AminoAcid.isACGTN(x) || x=='-' || x=='+';} |
| 271 |
|
| 272 |
public static boolean isBarcode(String s) { |
| 273 |
int bases=0; |
| 274 |
int delimiters=0; |
| 275 |
if(s.length()<6) {return false;} |
| 276 |
for(int i=0; i<s.length(); i++) { |
| 277 |
char c=s.charAt(i); |
| 278 |
if(AminoAcid.isACGTN(c)) {bases++;} |
| 279 |
else if(c=='-' || c=='+') {delimiters++;} |
| 280 |
else {return false;} |
| 281 |
} |
| 282 |
return bases>=6 && delimiters<=1; |
| 283 |
} |
| 284 |
|
| 285 |
public static final boolean containsOnlyBarcodeSymbols(String s) { |
| 286 |
for(int i=0; i<s.length(); i++) { |
| 287 |
if(!isBarcodeSymbol(s.charAt(i))) {return false;} |
| 288 |
} |
| 289 |
return true; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public long count(){return count;} |
| 295 |
public void setCount(long x){count=x;} |
| 296 |
public int length() {return name.length();} |
| 297 |
public char charAt(int i) {return name.charAt(i);} |
| 298 |
public byte[] getBytes() {return name.getBytes();} |
| 299 |
|
| 300 |
public int length1() { |
| 301 |
for(int i=0; i<name.length(); i++) { |
| 302 |
if(!Tools.isLetter(name.charAt(i))) {return i;} |
| 303 |
} |
| 304 |
return name.length(); |
| 305 |
} |
| 306 |
public int length2() { |
| 307 |
int i=name.length()-1; |
| 308 |
while(i>=0 && Tools.isDigit(name.charAt(i))){ |
| 309 |
i--; |
| 310 |
} |
| 311 |
int len=0; |
| 312 |
while(i>=0 && Tools.isLetter(name.charAt(i))){ |
| 313 |
i--; |
| 314 |
len++; |
| 315 |
} |
| 316 |
return i<0 ? 0 : len; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public void increment() {count++;} |
| 324 |
public void increment(Barcode b) {count+=b.count;} |
| 325 |
public void increment(long x) {count+=x;} |
| 326 |
public synchronized void incrementSync(long x) {count+=x;} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
@Override |
| 333 |
public int hashCode(){ |
| 334 |
return name.hashCode()^(tile*65); |
| 335 |
} |
| 336 |
|
| 337 |
@Override |
| 338 |
public int compareTo(Barcode b) { |
| 339 |
if(count!=b.count){return count>b.count ? -1 : 1;} |
| 340 |
if(tile!=b.tile){return tile>b.tile ? 1 : -1;} |
| 341 |
return name.compareTo(b.name); |
| 342 |
} |
| 343 |
|
| 344 |
@Override |
| 345 |
public boolean equals(Object b) { |
| 346 |
|
| 347 |
return(equals((Barcode)b)); |
| 348 |
} |
| 349 |
public boolean equals(Barcode b) {return(b!=null && name.equals(b.name) && tile==b.tile);} |
| 350 |
|
| 351 |
@Override |
| 352 |
public String toString(){ |
| 353 |
return name+"\t"+count+(expected!=1 ? "\te"+expected : "")+ |
| 354 |
(frequency!=1 ? "\tf"+frequency : "")+(tile>0 ? "\tt"+tile : ""); |
| 355 |
} |
| 356 |
|
| 357 |
public ByteBuilder appendTo(ByteBuilder bb){ |
| 358 |
bb.append(name); |
| 359 |
if(tile>0) {bb.append(tile);} |
| 360 |
return bb.tab().append(count); |
| 361 |
} |
| 362 |
|
| 363 |
public ByteBuilder appendIndex(ByteBuilder bb, byte delimiter, int indexNum) { |
| 364 |
return appendIndex(bb, delimiter, indexNum, name); |
| 365 |
} |
| 366 |
|
| 367 |
public static ByteBuilder appendIndex(ByteBuilder bb, byte delimiter, int indexNum, String name) { |
| 368 |
for(int i=0, currentIndex=1; i<name.length() && currentIndex<=indexNum; i++) { |
| 369 |
final char c=name.charAt(i); |
| 370 |
if(c==delimiter) {currentIndex++;} |
| 371 |
else if(currentIndex==indexNum){bb.append(c);} |
| 372 |
} |
| 373 |
return bb; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
public static byte delimiter(String barcode){ |
| 381 |
if(barcode==null || barcode.length()<3) {return 0;} |
| 382 |
int letters=0, nonletters=0; |
| 383 |
byte delimiter=0; |
| 384 |
for(int i=0; i<barcode.length(); i++){ |
| 385 |
char c=barcode.charAt(i); |
| 386 |
if(Tools.isLetter(c)){ |
| 387 |
letters++; |
| 388 |
}else{ |
| 389 |
if(nonletters==0) {delimiter=(byte)c;} |
| 390 |
nonletters++; |
| 391 |
} |
| 392 |
} |
| 393 |
if(nonletters==1 && letters>1){ |
| 394 |
return delimiter; |
| 395 |
} |
| 396 |
return 0; |
| 397 |
} |
| 398 |
|
| 399 |
public static byte delimiter(byte[] barcode){ |
| 400 |
if(barcode==null || barcode.length<3) {return 0;} |
| 401 |
int letters=0, numbers=0, other=0; |
| 402 |
byte delimiter=0; |
| 403 |
for(int i=0; i<barcode.length; i++){ |
| 404 |
byte c=barcode[i]; |
| 405 |
if(Tools.isLetter(c)){ |
| 406 |
letters++; |
| 407 |
}else if(Tools.isDigit(c)){ |
| 408 |
numbers++; |
| 409 |
}else{ |
| 410 |
if(other==0) {delimiter=(byte)c;} |
| 411 |
other++; |
| 412 |
} |
| 413 |
} |
| 414 |
if(other==1 && letters>1){ |
| 415 |
return delimiter; |
| 416 |
} |
| 417 |
return 0; |
| 418 |
} |
| 419 |
|
| 420 |
public static int countPolymers(String code) { |
| 421 |
int delimiter=code.length(); |
| 422 |
for(int i=0; i<code.length(); i++) { |
| 423 |
if(!Tools.isLetter(code.charAt(i))){delimiter=i; break;} |
| 424 |
} |
| 425 |
|
| 426 |
int polymers=0; |
| 427 |
{ |
| 428 |
int same=0, len=0; |
| 429 |
char last=0; |
| 430 |
for(int i=0; i<delimiter; i++) { |
| 431 |
char c=code.charAt(i); |
| 432 |
same+=(c==last ? 1 : 0); |
| 433 |
len++; |
| 434 |
last=c; |
| 435 |
} |
| 436 |
polymers+=(len>1 && same>=len-1) ? 1 : 0; |
| 437 |
} |
| 438 |
{ |
| 439 |
int same=0, len=0; |
| 440 |
char last=0; |
| 441 |
for(int i=delimiter+1; i<code.length(); i++) { |
| 442 |
char c=code.charAt(i); |
| 443 |
same+=(c==last ? 1 : 0); |
| 444 |
len++; |
| 445 |
last=c; |
| 446 |
} |
| 447 |
polymers+=(len>1 && same>=len-1) ? 1 : 0; |
| 448 |
} |
| 449 |
return polymers; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
public final String name; |
| 457 |
private long count=0; |
| 458 |
public final int expected; |
| 459 |
public float frequency=1f; |
| 460 |
public int tile=0; |
| 461 |
|
| 462 |
} |