1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
|
/*
Ray
Copyright (C) 2010, 2011, 2012 Sébastien Boisvert
http://DeNovoAssembler.SourceForge.Net/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You have received a copy of the GNU General Public License
along with this program (gpl-3.0.txt).
see <http://www.gnu.org/licenses/>
*/
#include "Vertex.h"
#include <code/Mock/common_functions.h>
#include <assert.h>
#include <vector>
#include <cstdlib>
#include <iostream>
using namespace std;
#define __NO_ORIGIN -999
Vertex::Vertex() {
constructor();
}
Vertex::~Vertex() {
}
void Vertex::constructor(){
m_coverage_lower=0;
setEdgeSet(0);
m_readsStartingHere=NULL;
m_directions=NULL;
m_assembled=__NO_ORIGIN;
m_color=0;
}
void Vertex::setEdgeSet(uint8_t edges) {
m_edges_lower = edges;
}
void Vertex::assemble(Rank origin){
if(origin>m_assembled)
m_assembled=origin;
#ifdef CONFIG_ASSERT
assert(m_assembled!=__NO_ORIGIN);
#endif
}
bool Vertex::isAssembled(){
return m_assembled!=__NO_ORIGIN;
}
bool Vertex::isAssembledByGreaterRank(Rank origin){
return origin<m_assembled;
}
void Vertex::setCoverageValue(CoverageDepth coverage) {
m_coverage_lower = coverage;
}
void Vertex::setCoverage(Kmer*a,CoverageDepth coverage){
if(*a==m_lowerKey){
CoverageDepth max=0;
max=max-1;// underflow.
if(m_coverage_lower==max){ // maximum value
return;
}
m_coverage_lower=coverage;
}
}
CoverageDepth Vertex::getVertexCoverage() const{
return getCoverage(&m_lowerKey);
}
CoverageDepth Vertex::getCoverage(const Kmer*a) const{
return m_coverage_lower;
}
vector<Kmer> Vertex::getIngoingEdges(const Kmer *a,int k) const{
return a->getIngoingEdges(getEdges(a),k);
}
vector<Kmer> Vertex::getOutgoingEdges(const Kmer*a,int k) const{
return a->getOutgoingEdges(getEdges(a),k);
}
void Vertex::addIngoingEdge_ClassicMethod(Kmer*vertex,Kmer*a,int k){
uint8_t s1First=a->getFirstSegmentFirstCode(k);
// add s1First to edges.
uint8_t newBits=(1<<(s1First));
setEdges(vertex,getEdges(vertex)|newBits);
}
void Vertex::deleteIngoingEdge(Kmer*vertex,Kmer*a,int k){
uint8_t s1First=a->getFirstSegmentFirstCode(k);
// delete s1First from edges.
uint8_t newBits=(1<<(s1First));
newBits=~newBits;
setEdges(vertex,getEdges(vertex)&newBits);
}
void Vertex::addIngoingEdge(Kmer*vertex,Kmer*a,int k){
addIngoingEdge_ClassicMethod(vertex,a,k);
}
void Vertex::addOutgoingEdge_ClassicMethod(Kmer*vertex,Kmer*a,int k){
// description of m_edges:
// outgoing ingoing
//
// G C T A G C T A
//
// 7 6 5 4 3 2 1 0
// put s2Last in m_edges
uint8_t s2Last=a->getSecondSegmentLastCode(k);
uint64_t newBits=1<<(4+s2Last);
setEdges(vertex,getEdges(vertex)|newBits);
}
void Vertex::deleteOutgoingEdge(Kmer*vertex,Kmer*a,int k){
uint8_t s2Last=a->getSecondSegmentLastCode(k);
uint64_t newBits=1<<(4+s2Last);
newBits=~newBits;
setEdges(vertex,getEdges(vertex)&newBits);
}
void Vertex::addOutgoingEdge(Kmer*vertex,Kmer*a,int k){
addOutgoingEdge_ClassicMethod(vertex,a,k);
}
void Vertex::setEdges(Kmer*a,uint8_t edges){
if(*a==m_lowerKey)
m_edges_lower=edges;
else
m_edges_lower=convertBitmap(edges);
}
uint8_t Vertex::getVertexEdges() const{
return getEdges(&m_lowerKey);
}
uint8_t Vertex::getEdges(const Kmer*a) const{
if(*a==m_lowerKey)
return m_edges_lower;
return convertBitmap(m_edges_lower);
}
void Vertex::addRead(Kmer*vertex,ReadAnnotation*e){
e->setNext(m_readsStartingHere);
m_readsStartingHere=e;
}
void Vertex::addDirection(Kmer*vertex,Direction*e){
#ifdef CONFIG_ASSERT
Vertex copy0=*this;
if(m_directions != NULL){
Direction copy1=*m_directions;
Direction*next=copy1.getNext();
assert(next ==NULL || next != NULL);
}
assert(e!=NULL);
Direction copy2=*e;
assert(copy2.getNext() == NULL);
#endif
e->setNext(m_directions);
m_directions=e;
#ifdef CONFIG_ASSERT
assert(m_directions != NULL);
#endif
}
ReadAnnotation*Vertex::getReads(Kmer*vertex){
return m_readsStartingHere;
}
vector<Direction> Vertex::getDirections(Kmer*vertex){
bool seekLower=false;
if(vertex->isEqual(&m_lowerKey)){
seekLower=true;
}
vector<Direction> a;
Direction*e=m_directions;
if(e==NULL)
return a;
//#define DEBUG_getDirections
#ifdef DEBUG_getDirections
cout<<"DEBUG_getDirections Initial Pointer= "<<e<<endl;
#endif
while(e!=NULL){
#ifdef CONFIG_ASSERT
assert(e!=NULL);
#endif
if(e->isLower()==seekLower){
a.push_back(*e);
}
// #define DEBUG_bug_20110921
#ifdef DEBUG_bug_20110921
if(e->getProgression() == 0){
cout<<"Direction "<<e<<" path "<<e->getWave()<<" position "<<e->getProgression()<<" lower "<<e->isLower()<<" next "<<e->getNext()<<endl;
}
#endif
e=e->getNext();
}
#ifdef CONFIG_ASSERT
assert(e==NULL);
#endif
return a;
}
void Vertex::clearDirections(Kmer*a){
m_directions=NULL;
}
void Vertex::write(Kmer*key, ostream*f,int kmerLength){
int coverage=getCoverage(key);
key->write(f);
f->write((char*)&coverage,sizeof(int));
vector<Kmer> parents=getIngoingEdges(key,kmerLength);
vector<Kmer> children=getOutgoingEdges(key,kmerLength);
int numberOfParents=parents.size();;
f->write((char*)&numberOfParents,sizeof(int));
for(int i=0;i<(int)parents.size();i++){
parents[i].write(f);
}
int numberOfChildren=children.size();
f->write((char*)&numberOfChildren,sizeof(int));
for(int i=0;i<(int)children.size();i++){
children[i].write(f);
}
}
void Vertex::writeAnnotations(Kmer*key,ostream*f,int kmerLength,bool color){
key->write(f);
Kmer complement=key->complementVertex(kmerLength,color);
bool isLower=(*key)<complement;
int annotations=0;
ReadAnnotation*ptr=m_readsStartingHere;
while(ptr!=NULL){
if(ptr->isLower()==isLower){
annotations++;
}
ptr=ptr->getNext();
}
ptr=m_readsStartingHere;
f->write((char*)&annotations,sizeof(int));
while(ptr!=NULL){
if(ptr->isLower()==isLower){
ptr->write(f);
}
ptr=ptr->getNext();
}
}
VirtualKmerColorHandle Vertex::getVirtualColor(){
return m_color;
}
void Vertex::setVirtualColor(VirtualKmerColorHandle handle){
m_color=handle;
}
Kmer Vertex::getKey(){
return m_lowerKey;
}
void Vertex::setKey(const Kmer & key){
m_lowerKey=key;
}
/*
* The algorithm to convert these maps:
*
* [Swap the 4 bits for children with the 4 bits for parents]
* swap 7 and 3
* swap 6 and 2
* swap 5 and 1
* swap 4 and 0
* [Swap nucleotides]
* swap 7 and 4
* swap 6 and 5
* swap 3 and 0
* swap 2 and 1
*
* children parents
* |--------|-------|
* |7 6 5 4 |3 2 1 0| bit index
* |T G C A |T G C A| nucleotide
* |--------|-------|
* < 0 0 1 0 1 0 0 1 > bit value
* ................
*
* see also the .h that has more documentation for this.
*/
uint8_t Vertex::convertBitmap(uint8_t bitMap) const{
/*
* [Swap the 4 bits for children with the 4 bits for parents]
* This is equivalent to swapping bits as described
* above.
*/
uint8_t newBits4_7=bitMap<<4;
uint8_t newBits0_3=bitMap>>4;
uint8_t baseMap=newBits4_7|newBits0_3;
/*
* Swap nucleotides
*/
baseMap=swapBits(baseMap,7,4);
baseMap=swapBits(baseMap,6,5);
baseMap=swapBits(baseMap,3,0);
baseMap=swapBits(baseMap,2,1);
return baseMap;
}
uint8_t Vertex::swapBits(uint8_t map,int bit1,int bit2) const {
int bit1Value=((uint64_t)map<<(63-bit1))>>63;
int bit2Value=((uint64_t)map<<(63-bit2))>>63;
#ifdef CONFIG_ASSERT
assert(bit1>=0);
assert(bit1<8);
assert(bit2>=0);
assert(bit2<8);
#endif
/*
* The bit are the same, no need to swap anything.
*/
if(bit1Value==bit2Value)
return map;
#ifdef CONFIG_ASSERT
if(bit1Value+bit2Value!=1){
cout<<"bit values: "<<bit1Value<<" "<<bit2Value<<endl;
}
assert(bit1Value==0||bit2Value==0);
assert(bit1Value==1||bit2Value==1);
assert(bit1Value+bit2Value==1);
#endif
if(bit1Value==1){
// set bit2 to 1
map|=(1<<bit2);
// and bit1 to 0
map&=~(1<<bit1);
}else if(bit2Value==1){
// set bit1 to 1
map|=(1<<bit1);
// and bit2 to 0
map&=~(1<<bit2);
}
#ifdef CONFIG_ASSERT
int newBit1Value=((uint64_t)map<<(63-bit1))>>63;
int newBit2Value=((uint64_t)map<<(63-bit2))>>63;
assert(newBit1Value==bit2Value);
assert(newBit2Value==bit1Value);
assert(newBit1Value+newBit2Value==1);
assert(newBit1Value==0||newBit2Value==0);
assert(newBit1Value==1||newBit2Value==1);
#endif /* ASSERT */
return map;
}
Direction*Vertex::getFirstDirection()const{
return m_directions;
}
int Vertex::load(const char * buffer) {
int position = 0;
position += m_lowerKey.load(buffer);
int bytes = sizeof(m_coverage_lower);
memcpy(&m_coverage_lower, buffer + position, bytes);
position += bytes;
bytes = sizeof(m_edges_lower);
memcpy(&m_edges_lower, buffer + position, bytes);
position += bytes;
return position;
}
int Vertex::dump(char * buffer) const {
int position = 0;
position += m_lowerKey.dump(buffer);
int bytes = sizeof(m_coverage_lower);
memcpy(buffer + position, &m_coverage_lower, bytes);
position += bytes;
uint8_t edges = getEdgeSet();
bytes = sizeof(edges);
memcpy(buffer + position, &edges, bytes);
position += bytes;
return position;
}
uint8_t Vertex::getEdgeSet() const {
return m_edges_lower;
}
int Vertex::getRequiredNumberOfBytes() const {
//cout << "DEBUG sizeof(m_coverage_lower) is " << sizeof(m_coverage_lower) << endl;
return m_lowerKey.getRequiredNumberOfBytes() + sizeof(m_coverage_lower) + sizeof(m_edges_lower);
}
void Vertex::print(int kmerLength, bool colorSpaceMode) const {
cout << " Vertex key= ";
cout << m_lowerKey.idToWord(kmerLength, colorSpaceMode);
cout << " " << getVertexCoverage();
cout << " parents: " << getIngoingEdges(&m_lowerKey, kmerLength).size();
cout << " children: " << getOutgoingEdges(&m_lowerKey, kmerLength).size();
cout << endl;
}
|