| 1 |
package assemble; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.HashMap; |
| 5 |
|
| 6 |
import dna.AminoAcid; |
| 7 |
import shared.Tools; |
| 8 |
import structures.ByteBuilder; |
| 9 |
import ukmer.Kmer; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public class Contig { |
| 18 |
|
| 19 |
public Contig(byte[] bases_){ |
| 20 |
this(bases_, null, 0); |
| 21 |
} |
| 22 |
|
| 23 |
public Contig(byte[] bases_, int id_){ |
| 24 |
this(bases_, null, id_); |
| 25 |
} |
| 26 |
|
| 27 |
public Contig(byte[] bases_, String name_, int id_){ |
| 28 |
bases=bases_; |
| 29 |
name=name_; |
| 30 |
id=id_; |
| 31 |
} |
| 32 |
|
| 33 |
public ByteBuilder toFasta(int wrap, ByteBuilder sb){ |
| 34 |
if(wrap<1){wrap=Integer.MAX_VALUE;} |
| 35 |
sb.append('>'); |
| 36 |
toHeader(sb); |
| 37 |
if(bases!=null){ |
| 38 |
int pos=0; |
| 39 |
while(pos<bases.length-wrap){ |
| 40 |
sb.append('\n'); |
| 41 |
sb.append(bases, pos, wrap); |
| 42 |
pos+=wrap; |
| 43 |
} |
| 44 |
if(pos<bases.length){ |
| 45 |
sb.append('\n'); |
| 46 |
sb.append(bases, pos, bases.length-pos); |
| 47 |
} |
| 48 |
} |
| 49 |
return sb; |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public String toString() { |
| 54 |
return name2(); |
| 55 |
} |
| 56 |
|
| 57 |
public String name() { |
| 58 |
return toHeader(new ByteBuilder()).toString(); |
| 59 |
} |
| 60 |
|
| 61 |
public String name2() { |
| 62 |
ByteBuilder bb=toHeader(new ByteBuilder()); |
| 63 |
bb.append(" flipped="+flipped+", used="+used+", associate="+associate); |
| 64 |
return bb.toString(); |
| 65 |
} |
| 66 |
|
| 67 |
private ByteBuilder toHeader(ByteBuilder bb){ |
| 68 |
if(name!=null){return bb.append(name);} |
| 69 |
bb.append("contig_").append(id); |
| 70 |
bb.append(",length=").append(length()); |
| 71 |
bb.append(",cov=").append(coverage, 1); |
| 72 |
bb.append(",min=").append(minCov); |
| 73 |
bb.append(",max=").append(maxCov); |
| 74 |
bb.append(",gc=").append(gc(), 3); |
| 75 |
|
| 76 |
bb.append(",left=").append(Tadpole.codeStrings[leftCode]); |
| 77 |
if(leftBranch()){ |
| 78 |
bb.append('_').append(leftRatio, 1); |
| 79 |
} |
| 80 |
bb.append(",right=").append(Tadpole.codeStrings[rightCode]); |
| 81 |
if(rightBranch()){ |
| 82 |
bb.append('_').append(rightRatio, 1); |
| 83 |
} |
| 84 |
if(leftEdges!=null){ |
| 85 |
bb.append(' ').append("leftEdges="); |
| 86 |
appendEdges(leftEdges, bb); |
| 87 |
} |
| 88 |
if(rightEdges!=null){ |
| 89 |
bb.append(' ').append("rightEdges="); |
| 90 |
appendEdges(rightEdges, bb); |
| 91 |
} |
| 92 |
return bb; |
| 93 |
} |
| 94 |
|
| 95 |
int appendEdges(ArrayList<Edge> edges, ByteBuilder bb){ |
| 96 |
int added=0; |
| 97 |
if(edges==null || edges.isEmpty()){return 0;} |
| 98 |
|
| 99 |
|
| 100 |
for(int i=0; i<edges.size(); i++){ |
| 101 |
Edge e=edges.get(i); |
| 102 |
if(e!=null){ |
| 103 |
if(added>0){bb.append(';');} |
| 104 |
e.appendTo(bb); |
| 105 |
added++; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
return added; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public float gc() { |
| 116 |
if(bases==null || bases.length<1){return 0;} |
| 117 |
int at=0, gc=0; |
| 118 |
for(byte b : bases){ |
| 119 |
int x=AminoAcid.baseToNumber[b]; |
| 120 |
if(x>-1){ |
| 121 |
if(x==0 || x==3){at++;} |
| 122 |
else{gc++;} |
| 123 |
} |
| 124 |
} |
| 125 |
if(gc<1){return 0;} |
| 126 |
return gc*1f/(at+gc); |
| 127 |
} |
| 128 |
|
| 129 |
public int length(){return bases.length;} |
| 130 |
|
| 131 |
long leftKmer(int k){ |
| 132 |
assert(k<32); |
| 133 |
long kmer=0; |
| 134 |
for(int i=0; i<k; i++){ |
| 135 |
byte b=bases[i]; |
| 136 |
byte x=AminoAcid.baseToNumber[b]; |
| 137 |
assert(x>=0); |
| 138 |
kmer=(kmer<<2)|x; |
| 139 |
} |
| 140 |
return kmer; |
| 141 |
} |
| 142 |
|
| 143 |
long rightKmer(int k){ |
| 144 |
assert(k<32); |
| 145 |
long kmer=0; |
| 146 |
for(int i=bases.length-k; i<bases.length; i++){ |
| 147 |
byte b=bases[i]; |
| 148 |
byte x=AminoAcid.baseToNumber[b]; |
| 149 |
assert(x>=0); |
| 150 |
kmer=(kmer<<2)|x; |
| 151 |
} |
| 152 |
return kmer; |
| 153 |
} |
| 154 |
|
| 155 |
Kmer leftKmer(Kmer kmer){ |
| 156 |
kmer.clearFast(); |
| 157 |
for(int i=0; i<kmer.kbig; i++){ |
| 158 |
kmer.addRight(bases[i]); |
| 159 |
} |
| 160 |
return kmer; |
| 161 |
} |
| 162 |
|
| 163 |
Kmer rightKmer(Kmer kmer){ |
| 164 |
kmer.clearFast(); |
| 165 |
for(int i=bases.length-kmer.kbig; i<bases.length; i++){ |
| 166 |
kmer.addRight(bases[i]); |
| 167 |
} |
| 168 |
return kmer; |
| 169 |
} |
| 170 |
|
| 171 |
boolean canonical(){ |
| 172 |
for(int i=0, j=bases.length-1; i<bases.length; i++, j--){ |
| 173 |
final byte a=AminoAcid.baseToComplementExtended[i], b=bases[j]; |
| 174 |
if(a<b){return true;} |
| 175 |
else if(b<a){return false;} |
| 176 |
} |
| 177 |
return true; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
void rcomp(){ |
| 186 |
AminoAcid.reverseComplementBasesInPlace(bases); |
| 187 |
{ |
| 188 |
int temp=leftCode; |
| 189 |
leftCode=rightCode; |
| 190 |
rightCode=temp; |
| 191 |
} |
| 192 |
{ |
| 193 |
float temp=leftRatio; |
| 194 |
leftRatio=rightRatio; |
| 195 |
rightRatio=temp; |
| 196 |
} |
| 197 |
{ |
| 198 |
ArrayList<Edge> temp=leftEdges; |
| 199 |
leftEdges=rightEdges; |
| 200 |
rightEdges=temp; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
boolean leftBranch(){return Tadpole.isBranchCode(leftCode);} |
| 205 |
boolean rightBranch(){return Tadpole.isBranchCode(rightCode);} |
| 206 |
|
| 207 |
boolean leftForwardBranch(){return leftCode==Tadpole.F_BRANCH;} |
| 208 |
boolean rightForwardBranch(){return rightCode==Tadpole.F_BRANCH;} |
| 209 |
|
| 210 |
boolean leftBackwardBranch(){return leftCode==Tadpole.B_BRANCH;} |
| 211 |
boolean rightBackwardBranch(){return rightCode==Tadpole.B_BRANCH;} |
| 212 |
|
| 213 |
final int leftEdgeCount(){ |
| 214 |
return leftEdges==null ? 0 : leftEdges.size(); |
| 215 |
} |
| 216 |
|
| 217 |
final int rightEdgeCount(){ |
| 218 |
return rightEdges==null ? 0 : rightEdges.size(); |
| 219 |
} |
| 220 |
|
| 221 |
public void addLeftEdge(Edge e) { |
| 222 |
assert(!hasLeftEdge(e.destination, e.bases, e.orientation)) : e+"\n"+leftEdges; |
| 223 |
Edge old=getLeftEdge(e.destination, e.orientation); |
| 224 |
if(old!=null){ |
| 225 |
if(e.depth>=old.depth && (old.depth==1 || old.length==e.length)){ |
| 226 |
old.bases=e.bases; |
| 227 |
old.length=e.length; |
| 228 |
old.depth+=e.depth; |
| 229 |
return; |
| 230 |
} |
| 231 |
} |
| 232 |
if(leftEdges==null){leftEdges=new ArrayList<Edge>(4);} |
| 233 |
leftEdges.add(e); |
| 234 |
} |
| 235 |
|
| 236 |
public void addRightEdge(Edge e) { |
| 237 |
assert(!hasRightEdge(e.destination, e.bases, e.orientation)) : e+"\n"+rightEdges; |
| 238 |
Edge old=getRightEdge(e.destination, e.orientation); |
| 239 |
if(old!=null){ |
| 240 |
if(e.depth>=old.depth && (old.depth==1 || old.length==e.length)){ |
| 241 |
old.bases=e.bases; |
| 242 |
old.length=e.length; |
| 243 |
old.depth+=e.depth; |
| 244 |
return; |
| 245 |
} |
| 246 |
} |
| 247 |
if(rightEdges==null){rightEdges=new ArrayList<Edge>(4);} |
| 248 |
rightEdges.add(e); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public void removeLeftEdge(int dest, boolean destRight){leftEdges=removeEdge(dest, destRight, leftEdges);} |
| 260 |
public void removeRightEdge(int dest, boolean destRight){rightEdges=removeEdge(dest, destRight, rightEdges);} |
| 261 |
|
| 262 |
public static ArrayList<Edge> removeEdge(int dest, boolean destRight, ArrayList<Edge> edges){ |
| 263 |
if(edges==null){return null;} |
| 264 |
int removed=0; |
| 265 |
for(int i=0; i<edges.size(); i++){ |
| 266 |
Edge e=edges.get(i); |
| 267 |
assert(e!=null); |
| 268 |
if(e.destination==dest && e.destRight()==destRight){ |
| 269 |
edges.set(i, null); |
| 270 |
removed++; |
| 271 |
} |
| 272 |
} |
| 273 |
if(removed>0){Tools.condenseStrict(edges);} |
| 274 |
return (edges.isEmpty() ? null : edges); |
| 275 |
} |
| 276 |
|
| 277 |
public Edge getLeftEdge(int dest, int orientation){ |
| 278 |
if(leftEdges==null){return null;} |
| 279 |
for(Edge e : leftEdges){ |
| 280 |
if(e.destination==dest && (orientation<0 || orientation==e.orientation)){ |
| 281 |
return e; |
| 282 |
} |
| 283 |
} |
| 284 |
return null; |
| 285 |
} |
| 286 |
|
| 287 |
public Edge getRightEdge(int dest, int orientation){ |
| 288 |
if(rightEdges==null){return null;} |
| 289 |
for(Edge e : rightEdges){ |
| 290 |
if(e.destination==dest && (orientation<0 || orientation==e.orientation)){ |
| 291 |
return e; |
| 292 |
} |
| 293 |
} |
| 294 |
return null; |
| 295 |
} |
| 296 |
|
| 297 |
public boolean hasLeftEdge(int dest, byte[] path, int orientation) { |
| 298 |
if(leftEdges!=null){ |
| 299 |
for(Edge e : leftEdges){ |
| 300 |
if(e.destination==dest && e.orientation==orientation){ |
| 301 |
if(path==null || Tools.equals(path, e.bases)){ |
| 302 |
return true; |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
return false; |
| 308 |
} |
| 309 |
|
| 310 |
public boolean hasRightEdge(int dest, byte[] path, int orientation) { |
| 311 |
if(rightEdges!=null){ |
| 312 |
for(Edge e : rightEdges){ |
| 313 |
if(e.destination==dest && e.orientation==orientation){ |
| 314 |
if(path==null || Tools.equals(path, e.bases)){ |
| 315 |
return true; |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
return false; |
| 321 |
} |
| 322 |
|
| 323 |
public final boolean flipped(){return flipped;} |
| 324 |
|
| 325 |
final void flip(ArrayList<Edge> inbound){ |
| 326 |
if(Tadpole.verbose){System.err.println("Flipping contig "+name());} |
| 327 |
flipped=!flipped; |
| 328 |
AminoAcid.reverseComplementBasesInPlace(bases); |
| 329 |
{ |
| 330 |
int temp=leftCode; |
| 331 |
leftCode=rightCode; |
| 332 |
rightCode=temp; |
| 333 |
} |
| 334 |
{ |
| 335 |
float temp=leftRatio; |
| 336 |
leftRatio=rightRatio; |
| 337 |
rightRatio=temp; |
| 338 |
} |
| 339 |
{ |
| 340 |
ArrayList<Edge> temp=leftEdges; |
| 341 |
leftEdges=rightEdges; |
| 342 |
rightEdges=temp; |
| 343 |
if(leftEdges!=null){ |
| 344 |
for(Edge e : leftEdges){ |
| 345 |
e.flipSource(); |
| 346 |
} |
| 347 |
} |
| 348 |
if(rightEdges!=null){ |
| 349 |
for(Edge e : rightEdges){ |
| 350 |
e.flipSource(); |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
if(inbound!=null){ |
| 355 |
for(Edge e : inbound){ |
| 356 |
assert(e.destination==id) : id+", "+e.destination+"\n"+e+"\n"+name()+"\n"; |
| 357 |
e.flipDest(); |
| 358 |
} |
| 359 |
} |
| 360 |
if(Tadpole.verbose){System.err.println("Flipped contig "+name());} |
| 361 |
} |
| 362 |
|
| 363 |
public final boolean used(){return used;} |
| 364 |
|
| 365 |
final void setUsed(HashMap<Integer, ArrayList<Edge>> map, ArrayList<Contig> allContigs){setUsed(map.get(id), allContigs);} |
| 366 |
|
| 367 |
final void setUsed(ArrayList<Edge> inbound, ArrayList<Contig> allContigs){ |
| 368 |
assert(!used); |
| 369 |
assert(!associate); |
| 370 |
used=true; |
| 371 |
removeAllEdges(inbound, allContigs); |
| 372 |
} |
| 373 |
|
| 374 |
public final boolean associate(){return associate;} |
| 375 |
|
| 376 |
final void setAssociate(HashMap<Integer, ArrayList<Edge>> map, ArrayList<Contig> allContigs){setAssociate(map.get(id), allContigs);} |
| 377 |
|
| 378 |
final void setAssociate(ArrayList<Edge> inbound, ArrayList<Contig> allContigs){ |
| 379 |
assert(!used); |
| 380 |
assert(!associate); |
| 381 |
associate=true; |
| 382 |
removeAllEdges(inbound, allContigs); |
| 383 |
} |
| 384 |
|
| 385 |
void removeAllEdges(ArrayList<Edge> inbound, ArrayList<Contig> allContigs){ |
| 386 |
leftEdges=null; |
| 387 |
rightEdges=null; |
| 388 |
|
| 389 |
if(inbound!=null){ |
| 390 |
for(Edge e : inbound){ |
| 391 |
if(e.destination==id){ |
| 392 |
Contig source=allContigs.get(e.origin); |
| 393 |
if(source!=this && !source.used() && !source.associate()){ |
| 394 |
source.removeLeftEdgesTo(id); |
| 395 |
source.removeRightEdgesTo(id); |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
private void removeLeftEdgesTo(int destNode) { |
| 403 |
leftEdges=removeEdgesTo(leftEdges, destNode); |
| 404 |
} |
| 405 |
|
| 406 |
private void removeRightEdgesTo(int destNode) { |
| 407 |
rightEdges=removeEdgesTo(rightEdges, destNode); |
| 408 |
} |
| 409 |
|
| 410 |
private static ArrayList<Edge> removeEdgesTo(ArrayList<Edge> edges, final int destNode) { |
| 411 |
if(edges==null || edges.isEmpty()){return null;} |
| 412 |
int cnt=0; |
| 413 |
for(int i=0; i<edges.size(); i++){ |
| 414 |
Edge e=edges.get(i); |
| 415 |
if(e.destination==destNode){ |
| 416 |
edges.set(i, null); |
| 417 |
cnt++; |
| 418 |
} |
| 419 |
} |
| 420 |
if(cnt==edges.size()){return null;} |
| 421 |
Tools.condenseStrict(edges); |
| 422 |
return edges; |
| 423 |
} |
| 424 |
|
| 425 |
final void renumber(final int newID, ArrayList<Edge> inbound){ |
| 426 |
if(id==newID){return;} |
| 427 |
if(leftEdges!=null){ |
| 428 |
for(Edge e : leftEdges){ |
| 429 |
assert(e.origin==id); |
| 430 |
e.origin=newID; |
| 431 |
} |
| 432 |
} |
| 433 |
if(rightEdges!=null){ |
| 434 |
for(Edge e : rightEdges){ |
| 435 |
assert(e.origin==id); |
| 436 |
e.origin=newID; |
| 437 |
} |
| 438 |
} |
| 439 |
if(inbound!=null){ |
| 440 |
for(Edge e : inbound){ |
| 441 |
assert(e.destination==id); |
| 442 |
e.destination=newID; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
id=newID; |
| 447 |
} |
| 448 |
|
| 449 |
public String name; |
| 450 |
public byte[] bases; |
| 451 |
public float coverage; |
| 452 |
public int minCov; |
| 453 |
public int maxCov; |
| 454 |
int leftCode; |
| 455 |
int rightCode; |
| 456 |
float leftRatio; |
| 457 |
float rightRatio; |
| 458 |
public int id; |
| 459 |
|
| 460 |
private boolean flipped=false; |
| 461 |
private boolean used=false; |
| 462 |
private boolean associate=false; |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
ArrayList<Edge> leftEdges; |
| 468 |
ArrayList<Edge> rightEdges; |
| 469 |
} |