00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <Sequence/Coalescent/SimTypes.hpp>
00025 #include <cassert>
00026 #include <iostream>
00027 #include <cstdlib>
00028
00029 namespace Sequence
00030 {
00031 segment::segment() : beg(0),end(0),desc(0)
00036 {
00037 }
00038
00039 segment::segment(const int & b,
00040 const int & e,
00041 const int & d = 0) : beg(b),end(e),desc(d)
00048 {
00049 assert ( end >= beg );
00050 }
00051
00052 chromosome::chromosome() : segs(NULL),
00053 pop(0),
00054 nsegs(0)
00059 {
00060 }
00061
00062 chromosome::chromosome( const std::vector<segment> & initial_segs,
00063 const int & population ) :
00064 segs((segment *)malloc(initial_segs.size()*
00065 sizeof(segment))),
00066 pop(population),
00067 nsegs(int(initial_segs.size()))
00074 {
00075 std::copy(initial_segs.begin(),initial_segs.end(),segs);
00076 }
00077
00078 chromosome::~chromosome()
00082 {
00083 if(nsegs>0) free(segs);
00084 }
00085
00086 chromosome::chromosome( const chromosome & ch ) :
00087 segs((segment *)malloc(ch.nsegs*sizeof(segment))),
00088 pop(ch.pop),
00089 nsegs(ch.nsegs)
00093 {
00094 std::copy(ch.segs,ch.segs+ch.nsegs,segs);
00095 }
00096
00097 chromosome & chromosome::operator=(const chromosome & ch)
00101 {
00102 if(this == &ch) return *this;
00103 if(ch.nsegs>this->nsegs)
00104 {
00105 this->segs = (segment *)realloc(this->segs,ch.nsegs*sizeof(segment));
00106 }
00107 std::copy(ch.segs,ch.segs+ch.nsegs,segs);
00108 this->nsegs = ch.nsegs;
00109 this->pop = ch.pop;
00110 return *this;
00111 }
00112
00113
00114 void chromosome::swap_with( chromosome & ch )
00124 {
00125 std::swap(this->segs,ch.segs);
00126 std::swap(this->nsegs,ch.nsegs);
00127 std::swap(this->pop,ch.pop);
00128 }
00129
00130 void chromosome::assign_allocated_segs( segment * newsegs,
00131 const int & new_nsegs )
00137 {
00138 free(segs);
00139 segs=newsegs;
00140 nsegs=new_nsegs;
00141 }
00142
00143 chromosome::iterator chromosome::begin()
00147 {
00148 return segs;
00149 }
00150
00151 chromosome::iterator chromosome::end()
00155 {
00156 return segs+nsegs;
00157 }
00158
00159 chromosome::const_iterator chromosome::begin() const
00163 {
00164 return segs;
00165 }
00166
00167 chromosome::const_iterator chromosome::end() const
00171 {
00172 return segs+nsegs;
00173 }
00174
00175 int chromosome::links() const
00181 {
00182 return( (segs+nsegs-1)->end - segs->beg );
00183 }
00184
00185 node::node(const double & t,
00186 const int & a) : time(t),abv(a)
00191 {
00192 }
00193
00194 marginal::marginal(const int & b, const int & ns,
00195 const int & nn,
00196 const std::vector<node> & t) :
00197 beg(b),nsam(ns),nnodes(nn),tree(t)
00204 {
00205 }
00206
00207 marginal::iterator marginal::begin()
00211 {
00212 return tree.begin();
00213 }
00214
00215 marginal::iterator marginal::end()
00219 {
00220 return tree.end();
00221 }
00222
00223 marginal::const_iterator marginal::begin() const
00227 {
00228 return tree.begin();
00229 }
00230
00231 marginal::const_iterator marginal::end() const
00235 {
00236 return tree.end();
00237 }
00238
00239 marginal::reference marginal::operator[](const std::vector<node>::size_type &i)
00244 {
00245 assert( int(i) <= 2*nsam-2 );
00246 return tree[i];
00247 }
00248
00249 marginal::const_reference marginal::operator[](const std::vector<node>::size_type &i) const
00254 {
00255 assert( int(i) <= 2*nsam-2 );
00256 return tree[i];
00257 }
00258
00259 bool marginal::operator<(const marginal & rhs) const
00263 {
00264 return this->beg < rhs.beg;
00265 }
00266
00267 std::ostream & operator<<(std::ostream & s,const chromosome & c)
00273 {
00274 s << '(';
00275 chromosome::const_iterator seg = c.begin();
00276 for( ; seg < c.end() ;
00277 ++seg)
00278 {
00279 s << seg->beg << ':' << seg->end << ',';
00280 }
00281 s << ' ' << c.nsegs << ' ' << c.links() << ')';
00282 return s;
00283 }
00284
00285 std::ostream & operator<<(std::ostream & s, const marginal & m)
00290 {
00291 s << "marginal tree begins at: " << m.beg << '\n';
00292 int seg=0;
00293 for( ;seg < m.nnodes ; ++seg)
00294 {
00295 s << seg << ' '
00296 << (m.begin()+seg)->time << ' '
00297 << (m.begin()+seg)->abv << '\n';
00298 }
00299 s << seg << ' '
00300 << (m.begin()+seg)->time << ' '
00301 << (m.begin()+seg)->abv;
00302 return s;
00303 }
00304
00305 struct newick_stream_marginal_tree_impl
00309 {
00310 marginal::const_iterator mi;
00311 const int nsam;
00312 std::vector<int> left,right;
00313 std::vector<node> tree;
00314 void init();
00315 std::ostream & parens( const int & noden,
00316 std::ostream & o) const;
00317 newick_stream_marginal_tree_impl(const marginal & m);
00318 newick_stream_marginal_tree_impl(const marginal * m);
00319 newick_stream_marginal_tree_impl(arg::const_iterator m);
00320 newick_stream_marginal_tree_impl(arg::iterator m);
00321 };
00322
00323 newick_stream_marginal_tree_impl::newick_stream_marginal_tree_impl(const marginal & m) :
00324 mi(m.begin()),nsam(m.nsam),
00325 left(2*nsam-1,-1),right(left),
00326 tree(std::vector<node>())
00327 {
00328 init();
00329 }
00330
00331 newick_stream_marginal_tree_impl::newick_stream_marginal_tree_impl(const marginal * m) :
00332 mi(m->begin()),nsam(m->nsam),
00333 left(2*nsam-1,-1),right(left),
00334 tree(std::vector<node>())
00335 {
00336 init();
00337 }
00338
00339 newick_stream_marginal_tree_impl::newick_stream_marginal_tree_impl(arg::const_iterator m) :
00340 mi(m->begin()),nsam(m->nsam),
00341 left(2*nsam-1,-1),right(left),
00342 tree(std::vector<node>())
00343 {
00344 init();
00345 }
00346
00347 newick_stream_marginal_tree_impl::newick_stream_marginal_tree_impl(arg::iterator m) :
00348 mi(m->begin()),nsam(m->nsam),
00349 left(2*nsam-1,-1),right(left),
00350 tree(std::vector<node>())
00351 {
00352 init();
00353 }
00354
00355 std::ostream &
00356 newick_stream_marginal_tree_impl::parens( const int & noden,
00357 std::ostream & o) const
00361 {
00362 double time;
00363
00364 if( left[noden] == -1 )
00365 {
00366 assert( (mi+((mi+noden)->abv))->time >= 0. );
00367 o << noden+1 << ':' <<(mi+((mi+noden)->abv))->time;
00368 }
00369 else
00370 {
00371 o << '(';
00372 parens( left[noden], o ) ;
00373 o << ',';
00374 parens( right[noden], o ) ;
00375 if( (mi+noden)->abv == -1 )
00376 {
00377 o << ");";
00378 }
00379 else
00380 {
00381 time = (mi + (mi+noden)->abv )->time - (mi+noden)->time ;
00382 assert(time >= 0.);
00383 o << "):"<<time;
00384 }
00385 }
00386 return o;
00387 }
00388
00389 void newick_stream_marginal_tree_impl::init()
00393 {
00394 for(int i=0;i<2*nsam-2;++i)
00395 {
00396 if( left[ (mi+i)->abv ] == -1 )
00397 {
00398 left[(mi+i)->abv] = i;
00399 }
00400 else
00401 {
00402 right[ (mi+i)->abv] = i;
00403 }
00404 }
00405
00406 }
00407
00408 newick_stream_marginal_tree::newick_stream_marginal_tree( const marginal & m ) :
00409 impl( new newick_stream_marginal_tree_impl(m) )
00410 {
00411 }
00412
00413 newick_stream_marginal_tree::newick_stream_marginal_tree( const marginal * m ) :
00414 impl( new newick_stream_marginal_tree_impl(m) )
00415 {
00416 }
00417
00418 newick_stream_marginal_tree::newick_stream_marginal_tree( arg::const_iterator m ) :
00419 impl( new newick_stream_marginal_tree_impl(m) )
00420 {
00421 }
00422
00423 newick_stream_marginal_tree::newick_stream_marginal_tree( arg::iterator m ) :
00424 impl( new newick_stream_marginal_tree_impl(m) )
00425 {
00426 }
00427
00428 newick_stream_marginal_tree::~newick_stream_marginal_tree( )
00429 {
00430 delete impl;
00431 }
00432
00433 std::vector<node> newick_stream_marginal_tree::get_tree() const
00438 {
00439 return impl->tree;
00440 }
00441
00442 std::ostream & newick_stream_marginal_tree::print( std::ostream & o ) const
00446 {
00447 impl->parens( 2*(impl->nsam)-2,o);
00448 return o;
00449 }
00450
00451 std::istream & newick_stream_marginal_tree::read( std::istream & i )
00456 {
00457 return i;
00458 }
00459
00460 std::ostream & operator<<(std::ostream & o, const newick_stream_marginal_tree & n)
00464 {
00465 return n.print(o);
00466 }
00467
00468 std::istream & operator>>(std::istream & i, newick_stream_marginal_tree & n)
00472 {
00473 return n.read(i);
00474 }
00475 }