1   /*
2  * scrm is an implementation of the Sequential-Coalescent-with-Recombination Model.
3  *
4  * Copyright (C) 2013, 2014 Paul R. Staab, Sha (Joe) Zhu, Dirk Metzler and Gerton Lunter
5  *
6  * This file is part of scrm.
7  *
8  * scrm is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #include "forest.h"
24 
25 /******************************************************************
26  * Constructors & Initialization
27  *****************************************************************/
28 
Forest(Model * model,RandomGenerator * random_generator)29 Forest::Forest(Model* model, RandomGenerator* random_generator) {
30   this->initialize(model, random_generator);
31   dout << *model << std::endl;
32 }
33 
34 // Sets member variable to default values
initialize(Model * model,RandomGenerator * rg)35 void Forest::initialize(Model* model,
36                         RandomGenerator* rg) {
37 
38   model->resetTime();
39   model->resetSequencePosition();
40 
41   this->set_model(model);
42   this->set_random_generator(rg);
43 
44   current_rec_ = 0;
45   rec_bases_ = std::vector<double>(1, -1);
46   rec_bases_.reserve(1000);
47 
48   this->set_sample_size(0);
49 
50   this->coalescence_finished_ = true;
51 
52   this->contemporaries_ = ContemporariesContainer(model->population_number(),
53                                                   model->sample_size(),
54                                                   rg);
55 
56   tmp_event_time_ = -1;
57 }
58 
59 /**
60  * @brief Copy constructor for forest
61  *
62  * This creates a copy of an Forest. It only produces valid results
63  * when there is no ongoing coalescence event (e.g. when we are not
64  * inside of sampleNextGenealogy()).
65  *
66  * Also, both copies of it share the model and the random generator,
67  * so make sure that only one of them is sampling a new genealogy at
68  * a time.
69  *
70  * @param current_forest Forest that needs to be duplicated
71  */
Forest(const Forest & current_forest)72 Forest::Forest(const Forest &current_forest) {
73   if (!current_forest.coalescence_finished_) {
74     throw std::logic_error("Can not copy forest during an ongoing coalescence");
75   }
76 
77   // Share a model and a random generator
78   this->set_model(current_forest.model_);
79   this->set_random_generator(current_forest.random_generator());
80 
81   // Copy state information
82   this->set_sample_size(current_forest.sample_size());
83   this->rec_bases_ = current_forest.rec_bases_;
84   this->current_rec_ = current_forest.current_rec_;
85 
86   // Copy the nodes
87   this->nodes_ = NodeContainer(*current_forest.getNodes());
88 
89   // Rebuild invariants of the nodes, and determine new roots
90   this->set_local_root(NULL);
91   this->set_primary_root(NULL);
92   for (auto it = nodes()->iterator(); it.good(); ++it) {
93     updateAbove(*it, false, false);
94   }
95 
96   // Set initial values for temporary variables
97   this->contemporaries_ = ContemporariesContainer(model().population_number(),
98                                                   model().sample_size(),
99                                                   random_generator());
100 
101   this->tmp_event_time_ = this->getTMRCA(false); // Disable buffer for next genealogy.
102 
103   this->coalescence_finished_ = true;
104 
105   dout<<"  #################### check copied forest ###############"<<std::endl;
106   assert(this->printTree());
107   assert(this->printNodes());
108   assert(this->checkTree());
109   assert(this->checkLeafsOnLocalTree() );
110   dout<<"  #################### check copied forest finished ###############"<<std::endl<<std::endl;
111 }
112 
113 
114 /**
115  * function that cuts a subtree out of a tree of the forest and reinserts it as
116  * a separate tree.
117  *
118  * This is primarily used to cut the subtree below an recombination
119  * away.
120  *
121  * \param cut_point   A TreePoint marking the top of the subtree to cut.
122  * \return            The root of the now separated subtree.
123  */
cut(const TreePoint & cut_point)124 Node* Forest::cut(const TreePoint &cut_point) {
125   //The node above the cut_point in the old tree
126   Node* parent = cut_point.base_node()->parent();
127   assert( parent != NULL );
128 
129   //The new end of the old branch after the cut
130   Node* new_leaf = nodes()->createNode(cut_point.height());
131 
132   if ( !cut_point.base_node()->local() )
133     new_leaf->make_nonlocal(cut_point.base_node()->last_update());
134   else
135     new_leaf->make_nonlocal(current_rec());
136   assert( !new_leaf->local() );
137 
138   new_leaf->set_population(cut_point.base_node()->population());
139   new_leaf->set_length_below(0);
140   new_leaf->set_samples_below(0);
141 
142   new_leaf->set_parent(parent);
143   parent->change_child(cut_point.base_node(), new_leaf);
144   nodes()->add(new_leaf, cut_point.base_node());
145 
146   // Update all local nodes above.
147   updateAbove(parent, false, true);
148   dout << "* * New leaf of local tree: " << new_leaf << std::endl;
149 
150   // The node below the recombination point becomes local in all possible cases
151   // (if it already isn't...)
152   updateAbove(cut_point.base_node(), false, false);
153   cut_point.base_node()->make_local();
154 
155   // The new "root" of the newly formed tree
156   Node* new_root = nodes()->createNode(cut_point.height());
157   new_root->set_population(cut_point.base_node()->population());
158   cut_point.base_node()->set_parent(new_root);
159   new_root->set_first_child(cut_point.base_node());
160 
161   // Set invariants of new root
162   new_root->set_length_below(cut_point.base_node()->length_below() +
163                              cut_point.relative_height() );
164   new_root->set_samples_below(cut_point.base_node()->samples_below() );
165 
166   nodes()->add(new_root, new_leaf);
167 
168   dout << "* * New root of subtree: " << new_root << std::endl;
169   dout << "* * Done" << std::endl;
170 
171   assert( this->checkInvariants(cut_point.base_node()) );
172   assert( this->checkInvariants(parent) );
173   assert( this->checkInvariants(new_leaf) );
174   assert( this->checkInvariants(new_root) );
175   assert( new_leaf->height() == cut_point.height() );
176   assert( new_root->height() == cut_point.height() );
177 
178   return(new_root);
179 }
180 
181 
182 /**
183  * Function to update the invariants (local, samples_below, length_below)
184  * of a 'node' and all of its (grand-)parents. Also sets local_root_ if it
185  * encounters it. Never makes non-local nodes local, only the other way round.
186  *
187  *  \param node       The node at which the functions starts updating the
188  *                    invariants. Then updates it's parent and the parents
189  *                    parent.
190  *  \param above_local_root If true, it uses a faster algorithm that is only correct
191  *                    for nodes above the local root. Default false. Best don't touch
192  *                    this.
193  *  \param recursive  If false, only the given node is updated, but not its parent.
194  *                    Default true.
195  *  \param invariants_only If true, it only updates the nodes invariants, but
196  *                    does not make nodes non-local and change the local root.
197  */
updateAbove(Node * node,bool above_local_root,const bool & recursive,const bool & invariants_only)198 void Forest::updateAbove(Node* node, bool above_local_root,
199                          const bool &recursive, const bool &invariants_only) {
200 
201   //dout << "Updating: " << node << " above_local_root: " << above_local_root << std::endl;
202 
203   // Fast forward above local root because this part is fairly straight forward
204   if (above_local_root) {
205     // Assure that everything is non-local
206     if (node->local()) node->make_nonlocal(current_rec());
207 
208     // Update the primary root if needed
209     if ( node->is_root() ) {
210       if (node != primary_root()) {
211         set_primary_root(node);
212       }
213       return;
214     }
215     if ( recursive ) updateAbove(node->parent(), true, true);
216     return;
217   }
218 
219   node->set_last_change(current_rec());
220 
221   // Calculate new values for samples_below and length_below for the current
222   // node
223   Node *l_child = node->first_child();
224   Node *h_child = node->second_child();
225 
226   size_t samples_below = node->in_sample();
227   if (l_child != NULL) samples_below = l_child->samples_below();
228   if (h_child != NULL) samples_below += h_child->samples_below();
229   assert( samples_below <= this->sample_size() );
230 
231   double length_below = 0.0;
232   if (l_child != NULL) {
233     length_below += l_child->length_below();
234     if (l_child->local()) length_below += l_child->height_above();
235 
236     if (h_child != NULL) {
237       length_below += h_child->length_below();
238       if (h_child->local()) length_below += h_child->height_above();
239     }
240   }
241   assert( length_below >= 0 );
242 
243   // Update whether the node is local or not
244   if (!invariants_only) {
245     if (samples_below == 0) {
246       if ( node->local() ) node->make_nonlocal(current_rec());
247     }
248     else if ( samples_below == sample_size() ) {
249       if ( node->local() ) node->make_nonlocal(current_rec());
250 
251       // Are we the local root?
252       if (node->countChildren() == 2 &&
253           l_child->samples_below() > 0 && h_child->samples_below() > 0) {
254         set_local_root(node);
255       }
256       if ( node->is_root() && node != primary_root() ) {
257         set_primary_root(node);
258       }
259       above_local_root = true;
260     }
261   }
262 
263   // If nothing changed, we also don't need to update the tree further above...
264   if (samples_below == node->samples_below() &&
265       areSame(length_below, node->length_below()) ) {
266     return;
267   }
268 
269   // Update the node invariants
270   node->set_samples_below(samples_below);
271   node->set_length_below(length_below);
272 
273   // Go further up if possible
274   if ( recursive && !node->is_root() ) {
275     updateAbove(node->parent(), above_local_root, recursive, invariants_only);
276   }
277 }
278 
279 
280 /**
281  * Function that builds the initial tree at the very left end of the sequence.
282  *
283  * Also creates the sample nodes.
284  */
buildInitialTree()285 void Forest::buildInitialTree() {
286   dout << "===== BUILDING INITIAL TREE =====" << std::endl;
287   assert(this->nodes()->size() == 0);
288   assert(this->segment_count() == 0);
289   assert(this->rec_bases_.size() == 1);
290   assert(this->model().getCurrentSequencePosition() == 0.0);
291   assert(this->contemporaries()->empty());
292   this->set_next_base(0.0);
293   ++current_rec_;
294 
295   dout << "* Adding first node... ";
296   Node* first_node = nodes()->createNode(model().sample_time(0), 1);
297   first_node->set_population(model().sample_population(0));
298   this->nodes()->add(first_node);
299   this->set_local_root(first_node);
300   this->set_primary_root(first_node);
301   dout << "done." << std::endl;
302 
303   Node* last_added_node = NULL;
304   for (size_t i=1; i < this->model().sample_size(); i++) {
305     this->set_sample_size(i+1);
306 
307     dout << "* adding node ";
308     //Create a new separate little tree of and at height zero
309     Node* new_leaf = nodes()->createNode(model().sample_time(i), i+1);
310     new_leaf->set_population(model().sample_population(i));
311     dout << new_leaf << "(" << new_leaf->population() << ") "
312          << "at height " << new_leaf->height() << std::endl;
313     nodes()->add(new_leaf, last_added_node);
314     if (new_leaf->height() == 0.0) last_added_node = new_leaf;
315     dout << "* starting coalescences" << std::endl;
316 
317     // Coalesces the separate tree into the main tree
318     this->sampleCoalescences(new_leaf);
319     dout << "* * Tree:" << std::endl;
320 
321     assert(this->checkTree());
322     assert(this->checkLeafsOnLocalTree());
323     assert(this->printTree());
324     assert(this->printNodes());
325     assert(this->coalescence_finished());
326   }
327   this->sampleNextBase();
328   dout << "Next Sequence position: " << this->next_base() << std::endl;
329   this->calcSegmentSumStats();
330 }
331 
332 
333 /**
334  * Uniformly samples a TreePoint on the local tree.
335  *
336  * Its arguments are meant to be used only when the function iteratively calls
337  * itself. Just call it without any arguments if you want to sample a TreePoint.
338  *
339  * The function first samples a part of the total height of the tree and then
340  * goes down from the root, deciding at each node if that point is to the left
341  * or right, which should give us an O(log(#nodes)) algorithm.
342  *
343  * I checked the distribution of this function in multiple cases. -Paul
344  *
345  * \param node The current position in the tree when the functions goes down
346  *             iteratively.
347  *
348  * \param length_left The length that is left until we encounter the sampled
349  *              length.
350  *
351  * \return The sampled point on the tree.
352  */
samplePoint(Node * node,double length_left) const353 TreePoint Forest::samplePoint(Node* node, double length_left) const {
354   if (node == NULL) {
355     // Called without arguments => initialization
356     assert( this->checkTreeLength() );
357 
358     node = this->local_root();
359     length_left = random_generator()->sample() * getLocalTreeLength();
360     assert( 0 < length_left && length_left < getLocalTreeLength() );
361   }
362 
363   assert( node->local() || node == this->local_root() );
364   assert( length_left >= 0 );
365   assert( length_left < (node->length_below() + node->height_above()) );
366 
367   if ( node != this->local_root() ) {
368     if ( length_left < node->height_above() ) {
369       assert( node->local() );
370       return TreePoint(node, length_left, true);
371     }
372 
373     length_left -= node->height_above();
374     assert( length_left >= 0 );
375   }
376 
377   // At this point, we should have at least one local child
378   assert( node->first_child() != NULL );
379   assert( node->first_child()->local() || node->second_child()->local() );
380 
381   // If we have only one local child, then give it the full length we have left.
382   if ( !node->first_child()->local() ) {
383     return samplePoint(node->second_child(), length_left);
384   }
385   if ( node->second_child() == NULL || !node->second_child()->local() ) {
386     return samplePoint(node->first_child(), length_left);
387   }
388 
389   // If we have two local children, the look if we should go down left or right.
390   double tmp = node->first_child()->height_above() + node->first_child()->length_below();
391   if ( length_left <= tmp )
392     return samplePoint(node->first_child(), length_left);
393   else
394     return samplePoint(node->second_child(), length_left - tmp);
395 }
396 /* Alternative inefficient implementation
397 TreePoint Forest::samplePoint(Node* node, double length_left) {
398  length_left = random_generator()->sample() * local_tree_length();
399  for (auto ni = nodes()->iterator(); ni.good(); ++ni) {
400    if (!(*ni)->local()) continue;
401    if (length_left < (*ni)->height_above()) return TreePoint(*ni, length_left, true);
402    else length_left -= (*ni)->height_above();
403  }
404  assert(0);
405 }
406 */
407 
408 
409 
410 /**
411  * Function to modify the tree after we encountered a recombination on the
412  * sequence. Also samples a place for this recombination on the tree, marks the
413  * branch above as non-local (and updates invariants) if needed, cuts the
414  * subtree below away and starts a coalescence from it's root.
415  * @ingroup group_scrm_next
416  * @ingroup group_pf_update
417  */
sampleNextGenealogy()418 void Forest::sampleNextGenealogy() {
419   ++current_rec_; // Move to next recombination;
420 
421   if (current_base() == model().getCurrentSequencePosition()) {
422     // Don't implement a recombination if we are just here because rates changed
423     dout << std::endl << "Position: " << this->current_base() << ": Changing rates." << std::endl;
424     this->sampleNextBase();
425     this->calcSegmentSumStats();
426     dout << "Next Position: " << this->next_base() << std::endl;
427     return;
428   }
429 
430   assert( current_base() > model().getCurrentSequencePosition() );
431   assert( current_base() < model().getNextSequencePosition() );
432 
433   assert( tmp_event_time_ >= 0 );
434   this->contemporaries_.buffer(tmp_event_time_);
435 
436   dout << std::endl << "===== BUILDING NEXT GENEALOGY =====" << std::endl;
437   dout << "Segment Nr: " << current_rec() << " | "
438        << "Sequence position: " << this->current_base() << std::endl;
439   assert( this->current_base() <= this->model().loci_length() );
440 
441   // Sample the recombination point
442   TreePoint rec_point = this->samplePoint();
443   assert( rec_point.base_node()->local() );
444 
445   dout << "* Recombination at height " << rec_point.height() << " ";
446   dout << "(above " << rec_point.base_node() << ")"<< std::endl;
447 
448   dout << "* Cutting subtree below recombination " << std::endl;
449   this->cut(rec_point);
450   assert( rec_point.height() == rec_point.base_node()->parent_height() );
451   assert( this->printTree() );
452 
453   dout << "* Starting coalescence" << std::endl;
454   this->sampleCoalescences(rec_point.base_node()->parent());
455 
456   assert( this->checkLeafsOnLocalTree() );
457   assert( this->checkTree() );
458   assert( this->printTree() );
459   assert( this->printNodes() );
460   assert( this->coalescence_finished() );
461 
462   this->sampleNextBase();
463   dout << "Next Sequence position: " << this->next_base() << std::endl;
464   this->calcSegmentSumStats();
465 }
466 
467 
468 /**
469  * Function for doing a coalescence.
470  *
471  * \param start_node The node at which the coalescence starts. Must be the root
472  *                   of a tree.
473  */
sampleCoalescences(Node * start_node)474 void Forest::sampleCoalescences(Node *start_node) {
475   assert( start_node->is_root() );
476   // We can have one or active local nodes: If the coalescing node passes the
477   // local root, it also starts a coalescence.
478   if (start_node->height() > this->local_root()->height()) {
479     set_active_node(0, this->local_root());
480     set_active_node(1, start_node);
481   } else {
482     set_active_node(0, start_node);
483     set_active_node(1, this->local_root());
484   }
485   assert ( active_node(0)->height() <= active_node(1)->height() );
486 
487   // Initialize Temporary Variables
488   tmp_event_ = Event(active_node(0)->height());
489   coalescence_finished_ = false;
490 
491   // Only prune every second round
492   for (TimeIntervalIterator ti(this, active_node(0)); ti.good(); ++ti) {
493 
494     dout << "* * Time interval: " << (*ti).start_height() << " - "
495          << (*ti).end_height() << " (Last event at " << tmp_event_.time() << ")" << std::endl;
496 
497     // Assert that we don't accidentally jump in time
498     assert( tmp_event_.time() < 0 || tmp_event_.time() == (*ti).start_height() );
499 
500     // Update States & Rates (see their declaration for explanation);
501     states_[0] = getNodeState(active_node(0), (*ti).start_height());
502     states_[1] = getNodeState(active_node(1), (*ti).start_height());
503 
504     // Fixed time events (e.g pop splits/merges & single migration events first
505     if (model().hasFixedTimeEvent((*ti).start_height())) implementFixedTimeEvent(ti);
506 
507     // Calculate the rates of events in this time interval
508     assert( checkContemporaries((*ti).start_height()) );
509     calcRates(*ti);
510 
511     // Some debug checks
512     dout << "* * * Active Nodes: a0:" << active_node(0) << ":s" << states_[0]
513         << "(p" << active_node(0)->population() << ")"
514         << " a1:" << active_node(1) << ":s" << states_[1]
515         << "(p" << active_node(1)->population() << ")" << std::endl
516         << "* * * Total Rates: " << rates_[0] << " "
517         << rates_[1] << " " << rates_[2] << std::endl;
518 
519     assert( active_node(0) != active_node(1) );
520     assert( states_[0] != 0 || states_[1] != 0 );
521     assert( states_[0] != 1 || active_node(0)->is_root() );
522     assert( states_[1] != 1 || active_node(1)->is_root() );
523     assert( states_[0] == 1 || active_node(0)->parent_height() >= tmp_event_.time() );
524     assert( states_[1] == 1 || active_node(1)->parent_height() >= tmp_event_.time() );
525     assert( states_[0] != 2 || !active_node(0)->local() );
526     assert( states_[1] != 2 || !active_node(1)->local() );
527 
528     assert( active_node(0)->first_child() == NULL  || active_node(0)->first_child()->local() ||
529             active_node(0)->second_child() == NULL || active_node(0)->second_child()->local() );
530     assert( active_node(1)->first_child() == NULL  || active_node(1)->first_child()->local() ||
531             active_node(1)->second_child() == NULL || active_node(1)->second_child()->local() );
532 
533     // Sample the time at which the next event happens (if any)
534     sampleEvent(*ti, tmp_event_time_, tmp_event_);
535     dout << "* * * " << tmp_event_ << std::endl;
536     assert( tmp_event_.isNoEvent() || (*ti).start_height() <= tmp_event_.time() );
537     assert( tmp_event_.isNoEvent() || tmp_event_.time() <= (*ti).end_height() );
538 
539 
540     // Implement the event
541     if ( tmp_event_.isNoEvent() ) {
542       this->implementNoEvent(*ti, coalescence_finished_);
543     }
544 
545     else if ( tmp_event_.isPwCoalescence() ) {
546       this->implementPwCoalescence(active_node(0), active_node(1), tmp_event_.time());
547       this->coalescence_finished_ = true;
548     }
549 
550     else if ( tmp_event_.isRecombination() ) {
551       this->implementRecombination(tmp_event_, ti);
552     }
553 
554     else if ( tmp_event_.isMigration() ) {
555       this->implementMigration(tmp_event_, true, ti);
556     }
557 
558     else if ( tmp_event_.isCoalescence() ) {
559       this->implementCoalescence(tmp_event_, ti);
560       assert( checkInvariants(tmp_event_.node()) );
561       assert( this->printTree() );
562     }
563 
564     if (coalescence_finished()) return;
565   }
566 }
567 
568 
calcRates(const TimeInterval & ti)569 void Forest::calcRates(const TimeInterval &ti) {
570   rates_[0] = 0.0;
571   rates_[1] = 0.0;
572   rates_[2] = 0.0;
573   active_nodes_timelines_[0] = 0;
574   active_nodes_timelines_[1] = 0;
575 
576   // Set rate of first node
577   if (states_[0] == 1) {
578     // coalescing or migrating
579     rates_[0] += model().total_migration_rate(active_node(0)->population());
580     if (model().growth_rate(active_node(0)->population()) == 0.0)
581       rates_[0] += calcCoalescenceRate(active_node(0)->population(), ti);
582     else {
583       // exponential growth -- assign this node to timeline 1
584       rates_[1] += calcCoalescenceRate(active_node(0)->population(), ti);
585       active_nodes_timelines_[0] = 1;
586     }
587   }
588   else if (states_[0] == 2) {
589     // recombining
590     rates_[0] += calcRecombinationRate(active_node(0));
591   }
592 
593   // The second node is a bit more complicated
594   if (states_[1] == 1) {
595     // coalescing or migrating
596     rates_[0] += model().total_migration_rate(active_node(1)->population());
597     if (model().growth_rate(active_node(1)->population()) == 0.0) {
598       // No Growth => Normal time
599       rates_[0] += calcCoalescenceRate(active_node(1)->population(), ti);
600 
601       if (states_[0] == 1 && active_node(0)->population() == active_node(1)->population()) {
602         // Also add rates for pw coalescence
603         rates_[0] += calcPwCoalescenceRate(active_node(1)->population(), ti);
604       }
605     }
606     else {
607       // Growth => we need a exponential time
608       if (states_[0] == 1 && active_node(0)->population() == active_node(1)->population()) {
609         // Coalescing or migrating; and we can use the timeline of the first node
610         rates_[1] += calcCoalescenceRate(active_node(1)->population(), ti);
611         // And must add pw coalescence again
612         rates_[1] += calcPwCoalescenceRate(active_node(1)->population(), ti);
613         active_nodes_timelines_[1] = 1;
614       }
615       else {
616 	      // No chance of a pairwise coalescence, but there is growth.
617         // We might need our own timeline (This could be made more efficient if both populations have
618 	      // equal growth rates, but we ignore that for the moment).
619         rates_[2] += calcCoalescenceRate(active_node(1)->population(), ti);
620         active_nodes_timelines_[1] = 2;
621       }
622     }
623   }
624   else if (states_[1] == 2) {
625     // recombining
626     rates_[0] += calcRecombinationRate(active_node(1));
627   }
628 
629   assert(rates_[0] >= 0);
630   assert(rates_[1] >= 0);
631   assert(rates_[2] >= 0);
632 }
633 
634 
635 /**
636  * Samples if an event (coalescence, recombination or migration of active nodes)
637  * happens in the current TimeInterval or not.
638  *
639  * In particular requires that the 'temporary' forest members samples_, rates_
640  * and active_nodes_ are set correctly beforehand.
641  *
642  * \param ti The current time interval
643  * \returns the event that has happened (can also be a "NoEvent" event)
644  */
sampleEvent(const TimeInterval & ti,double & event_time,Event & return_event) const645 void Forest::sampleEvent(const TimeInterval &ti, double &event_time, Event &return_event) const {
646   event_time = -1;
647   size_t event_line = -1;
648 
649   // Sample on which time and time line the event happens (if any)
650   for (size_t i = 0; i < 3; ++i) {
651     if (rates_[i] == 0.0) continue;
652     selectFirstTime(random_generator()->sampleExpoExpoLimit(rates_[i], getTimeLineGrowth(i), ti.length()),
653                     i, event_time, event_line);
654   }
655 
656   // Correct the time from relative to the time interval to absolute
657   if (event_time != -1) event_time += ti.start_height();
658   assert( (event_time == -1) ||
659          (ti.start_height() <= event_time && event_time <= ti.end_height()) );
660 
661   assert( (event_line == -1 && event_time == -1) || (event_line != -1 && event_time != -1));
662   // Sample the event type
663   sampleEventType(event_time, event_line, ti, return_event);
664 }
665 
666 
667 /**
668  * Given that an event has happened, this function samples the events type.
669  *
670  * In particular requires that the 'temporary' forest members samples_, rates_,
671  * active_nodes_, and nodes_timelines_ are set correctly beforehand.
672  */
sampleEventType(const double time,const size_t time_line,const TimeInterval & ti,Event & event) const673 void Forest::sampleEventType(const double time, const size_t time_line,
674                              const TimeInterval &ti, Event &event) const {
675   event = Event(time);
676 
677   if ( time_line != -1 && rates_[time_line] == 0.0 ) {
678     throw std::logic_error("An event with rate 0 has happened!");
679   }
680 
681   // Situation where it is clear what happened:
682   if (time == -1) return;
683   if (time_line == 2) return event.setToCoalescence(active_node(1), 1);
684 
685   double sample = random_generator()->sample() * rates_[time_line];
686 
687   for (size_t i = 0; i < 2; ++i) {
688     // Only Nodes in state 1 or 2 can do something
689     if ( states_[i] == 0 ) continue;
690 
691     // Coalescence can occur on all time lines
692     if (states_[i] == 1 && active_nodes_timelines_[i] == time_line) {
693       sample -= calcCoalescenceRate(active_node(i)->population(), ti);
694       if (sample <= 0.0) return event.setToCoalescence(active_node(i), i);
695     }
696 
697     // Migration and Recombination only on time line 0
698     if (time_line != 0) continue;
699 
700     // Recombination
701     if (states_[i] == 2) {
702       sample -= calcRecombinationRate(active_nodes_[i]);
703       if (sample <= 0.0) return event.setToRecombination(active_node(i), i);
704       continue;
705     }
706 
707     // Migration
708     assert( states_[i] == 1 );
709     if ( sample < model().total_migration_rate(active_node(i)->population()) ) {
710       for ( size_t j = 0; j < model().population_number(); ++j) {
711         sample -= model().migration_rate(active_node(i)->population(), j);
712         if ( sample <= 0.0 ) return event.setToMigration(active_node(i), i, j);
713       }
714       throw std::logic_error("Error Sampling Type of Event");
715     }
716     sample -= model().total_migration_rate(active_node(i)->population());
717   }
718 
719   // If we are here, than we should have sampled a pw coalescence...
720   assert( states_[0] == 1 && states_[1] == 1 );
721   assert( active_nodes_[0]->population() == active_nodes_[1]->population() );
722   assert( sample <= calcPwCoalescenceRate(active_nodes_[0]->population(), ti) );
723   return event.setToPwCoalescence();
724 }
725 
726 
727 /**
728  * Looks if there was an event on a sampled time (e.g. this new_time != -1)
729  * and if so sets current_time to the event with the smallest time and increases
730  * the time_line counter.
731  *
732  * \param new_time An ExpoLimit Sample
733  * \param time_line The timeline that the sample was from
734  * \param current_time The variable that save the time of the nearest event
735  * \param time_line The variable that saves the timeline of the nearest event
736  * \return Nothing, but updates current_time and current_time_line
737  */
selectFirstTime(const double new_time,const size_t time_line,double & current_time,size_t & current_time_line) const738 void Forest::selectFirstTime(const double new_time, const size_t time_line,
739                              double &current_time, size_t &current_time_line) const {
740   if (new_time == -1) return;
741   if (current_time == -1 || new_time < current_time) {
742     current_time = new_time;
743     current_time_line = time_line;
744   }
745 }
746 
747 
748 /**
749  *  Function to determine the state of (the branch above) a node for an ongoing
750  *  coalescence.
751  *
752  *  The States are: 0 = off, 1 = potentially coalescing, 2 = potentially recombining
753  *
754  *  \param node the node for which to tell the start
755  *  \param current_time the time at which the coalescence is
756  *  \return The state of the node
757  */
getNodeState(Node const * node,const double current_time) const758 size_t Forest::getNodeState(Node const *node, const double current_time) const {
759   if (node->height() > current_time) return(0);
760   if (node->is_root()) return(1);
761   if (!node->local()) return(2);
762   dout << "Error getting node state." << std::endl;
763   dout << "Height: " << node->height()
764       << " current time: " << current_time
765       << " diff: " << node->height() - current_time << std::endl;
766   dout << "Node local: " << node->local() << std::endl;
767   dout << "Node root: " << node->is_root() << std::endl;
768   assert( false );
769   return(-1);
770 }
771 
772 
calcCoalescenceRate(const size_t pop,const TimeInterval & ti) const773 double Forest::calcCoalescenceRate(const size_t pop, const TimeInterval &ti) const {
774   // Rate for each pair is 1/(2N), as N is the diploid population size
775   return contemporaries_.size(pop) * model().inv_double_pop_size(pop, ti.start_height());
776 }
777 
778 
calcPwCoalescenceRate(const size_t pop,const TimeInterval & ti) const779 double Forest::calcPwCoalescenceRate(const size_t pop, const TimeInterval &ti) const {
780   // Rate a pair is 1/(2N), as N is the diploid population size
781   return model().inv_double_pop_size(pop, ti.start_height());
782 }
783 
784 
calcRecombinationRate(Node const * node) const785 double Forest::calcRecombinationRate(Node const* node) const {
786   assert(!node->local());
787   assert(this->current_base() >= model().getCurrentSequencePosition());
788   double last_update_pos = get_rec_base(node->last_update());
789 
790   if (last_update_pos >= model().getCurrentSequencePosition()) {
791     // Rec rate is constant for the relevant sequence part
792     return (this->current_base() - last_update_pos) * model().recombination_rate();
793   } else {
794     // Rec rate may change. Accumulate the total rate.
795 
796     double rate = model().recombination_rate() *
797         (this->current_base() - model().getCurrentSequencePosition());
798     size_t idx = model().get_position_index() - 1;
799 
800     while (model().change_position(idx) > last_update_pos) {
801       assert(idx > 0);
802       rate += model().recombination_rate(idx) * (model().change_position(idx+1)-model().change_position(idx));
803       --idx;
804     }
805 
806     rate += model().recombination_rate(idx) * (model().change_position(idx+1)-last_update_pos);
807     return rate;
808   }
809 }
810 
811 
812 /**
813  * Even if no event occurred in a time interval, there is some stuff that we
814  * might have to do. Mainly moving the "active" flag upwards if a active node
815  * was looking for recombinations, but none occurred. In that case, we also
816  * might finish the coalescences.
817  *
818  * \param ti The current time interval
819  * \param coalescences_finished temp variable to pass the information that the
820  *    coalescence has finished.
821  */
implementNoEvent(const TimeInterval & ti,bool & coalescence_finished)822 void Forest::implementNoEvent(const TimeInterval &ti, bool &coalescence_finished) {
823   if (ti.end_height() == DBL_MAX) {
824     std::stringstream message;
825     message << "Lines did not coalescence." << std::endl;
826     if (active_node(0)->population() != active_node(1)->population()) {
827       message << "The lines were in populations " << active_node(0)->population() + 1
828           << " and " << active_node(1)->population() + 1 << "." << std::endl
829           << "You should add on opportunity for migration between these populations."
830           << std::endl;
831     }
832 
833     else if (model().growth_rate(active_node(0)->population())) {
834         message << "Population " << active_node(0)->population() + 1
835                 << " has a negative growth factor for infinite time." << std::endl
836                 << "This can prevent coalescence. " << std::endl;
837     }
838 
839     throw std::logic_error(message.str());
840   }
841   if (states_[0] == 2) {
842     set_active_node(0, possiblyMoveUpwards(active_node(0), ti));
843     if (active_node(0)->local()) {
844       assert( states_[1] == 0 );
845       dout << "* * * Active Node 0 hit a local node. Done" << std::endl;
846       updateAbove(active_node(0));
847       coalescence_finished = true;
848       tmp_event_time_ = active_node(0)->height();
849       contemporaries_.replace(active_node(0),
850                               active_node(0)->first_child(),
851                               active_node(0)->second_child());
852       return;
853     }
854   }
855 
856   // There are no local node above the local root, which is the lowest node
857   // that active_node(1) can be.
858   if (states_[1] == 2) set_active_node(1, possiblyMoveUpwards(active_node(1), ti));
859 
860   if (active_node(0) == active_node(1)) {
861     dout << "* * * Active Nodes hit each other in " << active_node(0)
862         << ". Done." << std::endl;
863     updateAbove(active_node(0));
864     coalescence_finished = true;
865     // Update contemporaries for reuse
866     contemporaries_.replaceChildren(active_node(0));
867     tmp_event_time_ = active_node(0)->height();
868   }
869 }
870 
871 /**
872  * Modifies the forest to reflect the coalescences of a coalescing node into
873  * a point on a existing tree.
874  *
875  * Attention: The returned node does still require an update!
876  *
877  * \param coal_node The coalescing node
878  * \param coal_point The point on the tree into which coal_node coalesces.
879  *
880  */
implementCoalescence(const Event & event,TimeIntervalIterator & tii)881 void Forest::implementCoalescence(const Event &event, TimeIntervalIterator &tii) {
882   // Coalescence: sample target point and implement the coalescence
883   assert( event.node() == active_node(event.active_node_nr()) );
884 
885   Node* coal_node = event.node();
886   Node* target = contemporaries_.sample(coal_node->population());
887 
888   dout << "* * * Above node " << target << std::endl;
889   assert( target->height() < event.time() );
890   assert( coal_node->population() == target->population() );
891   assert( getEventNode() != NULL );
892   assert( getOtherNode() != NULL );
893 
894   Node* new_node;
895 
896   // ---------------------------------------------
897   // Actually implement the coalescence
898   // ---------------------------------------------
899 
900   // Look if we can reuse the root that coalesced for marking the point of
901   // coalescence
902   if ( coal_node->countChildren() == 1 && !coal_node->is_migrating() ){
903     assert( coal_node->local() );
904     new_node = coal_node;
905     coal_node = coal_node->first_child();
906     nodes()->move(new_node, event.time());
907     updateAbove(new_node, false, false);
908   } else {
909     // If not, create a new node
910     new_node = nodes()->createNode(event.time());
911     new_node->change_child(NULL, coal_node);
912     coal_node->set_parent(new_node);
913     nodes()->add(new_node);
914   }
915 
916   // Now we have:
917   // target:    Node in the target tree under the coalescence
918   // coal_node: Root of the coalescing tree
919   // new_node:  New parent of 'target' and 'coal_node'
920 
921   // Update new_node
922   new_node->set_population(coal_node->population());
923   new_node->change_child(NULL, target);
924   new_node->set_parent(target->parent());
925   if (!target->local()) {
926     new_node->make_nonlocal(target->last_update());
927     contemporaries_.add(new_node);
928   } else {
929     new_node->make_local();
930   }
931 
932   // Integrate it into the tree
933   target->set_parent(new_node);
934   new_node->parent()->change_child(target, new_node);
935 
936   // And update
937   coal_node->make_local();
938   updateAbove(coal_node, false, false);
939 
940   set_active_node(event.active_node_nr(), new_node);
941 
942 
943   // ---------------------------------------------
944   // Check if are can stop.
945   // ---------------------------------------------
946 
947   if ( getOtherNodesState() == 2 ) {
948     // If the coalescing node coalesced into the branch directly above
949     // a recombining node, we are done.
950     if ( getOtherNode()->parent() == getEventNode() ) {
951       dout << "* * * Recombining Node moved into coalesced node. Done." << std::endl;
952       getOtherNode()->make_local();
953       updateAbove(getOtherNode(), false, false);
954       updateAbove(getEventNode());
955       coalescence_finished_ = true;
956       return;
957     }
958 
959     // The branch below the event will be made local later anyway, so we don't
960     // have to care about marking it as updated.
961   }
962 
963   if ( target->local() ) {
964     // Only active_node(0) can coalescence into local nodes. active_node(1) is
965     // at least the local root and hence above all local nodes.
966     // If active_node(0) coalescences into the local tree, there are no more
967     // active nodes and we are done.
968     assert( event.active_node_nr() == 0 );
969     assert( states_[1] == 0 );
970 
971     dout << "* * * We hit the local tree. Done." << std::endl;
972     updateAbove(getEventNode());
973     coalescence_finished_ = true;
974     contemporaries_.replace(new_node, coal_node, target);
975     return;
976   }
977 
978   // If we hit an non-local branch:
979   // Begin next interval at the coalescence height and remove the branch
980   // below from contemporaries.
981   tii.splitCurrentInterval(getEventNode(), target);
982 }
983 
984 
985 
986 /**
987  * @brief Modifies the forest to reflect that two coalescing nodes coalesced together.
988  *
989  * @param root_1 The first coalescing node
990  * @param root_2 The second coalescing node
991  * @param time   The time at which the coalescence happens
992  */
implementPwCoalescence(Node * root_1,Node * root_2,const double time)993 void Forest::implementPwCoalescence(Node* root_1, Node* root_2, const double time) {
994   dout << "* * Both nodes coalesced together" << std::endl;
995   dout << "* * Implementing..." << std::flush;
996 
997   Node* new_root = NULL;
998   assert( root_1 != NULL );
999   assert( root_2 != NULL );
1000   assert( root_1->population() == root_2->population() );
1001 
1002   // Both roots are now local
1003   root_1->make_local();
1004   root_2->make_local();
1005 
1006   // both nodes may or may not mark the end of a single branch at the top of their tree,
1007   // which we don't need anymore.
1008   if (root_1->countChildren() == 1 && !root_1->is_migrating()) {
1009     if (root_2->countChildren() == 1 && !root_2->is_migrating()) {
1010       // both trees have a single branch => delete one
1011       root_2 = root_2->first_child();
1012       this->nodes()->remove(root_2->parent());
1013       root_2->set_parent(NULL);
1014       assert( root_2 != NULL );
1015       assert( root_2->is_root() );
1016     }
1017     // (now) only root_1 has a single branch => use as new root
1018     this->nodes()->move(root_1, time);
1019     new_root = root_1;
1020     root_1 = root_1->first_child();
1021     assert( root_1 != NULL );
1022   }
1023   else if (root_2->countChildren() == 1 && !root_2->is_migrating()) {
1024     // only root_2 has a single branch => use as new root
1025     this->nodes()->move(root_2, time);
1026     new_root = root_2;
1027     root_2 = root_2->first_child();
1028   }
1029   else {
1030     // No tree a has single branch on top => create a new root
1031     new_root = nodes()->createNode(time);
1032     this->nodes()->add(new_root);
1033   }
1034 
1035   root_1->set_parent(new_root);
1036   root_2->set_parent(new_root);
1037   new_root->set_second_child(root_1);
1038   new_root->set_first_child(root_2);
1039   new_root->set_population(root_1->population());
1040 
1041   updateAbove(root_1, false, false);
1042   updateAbove(root_2, false, false);
1043   updateAbove(new_root, false, false);
1044   dout << " done" << std::endl;
1045 
1046   assert( this->local_root()->height() == time );
1047   assert( root_1->local() );
1048   assert( root_2->local() );
1049   assert( !new_root->local() );
1050 }
1051 
1052 
implementRecombination(const Event & event,TimeIntervalIterator & ti)1053 void Forest::implementRecombination(const Event &event, TimeIntervalIterator &ti) {
1054   TreePoint event_point = TreePoint(event.node(), event.time(), false);
1055   set_active_node(event.active_node_nr(), cut(event_point));
1056 
1057   ti.recalculateInterval();
1058 
1059   assert( this->printTree() );
1060   assert( event.node()->local() );
1061 }
1062 
1063 
implementMigration(const Event & event,const bool & recalculate,TimeIntervalIterator & ti)1064 void Forest::implementMigration(const Event &event, const bool &recalculate, TimeIntervalIterator &ti) {
1065   dout << "* * Implementing migration event... " << std::flush;
1066   assert( event.node()->is_root() );
1067 
1068   // There is only little to do if we can reuse the event.node()
1069   if ( event.node()->is_unimportant() ||
1070        ( event.node()->height() == event.time() && event.node()->is_migrating() ) ) {
1071     dout << "Reusing: " << event.node() << "... " << std::flush;
1072     nodes()->move(event.node(), event.time());
1073     event.node()->set_population(event.mig_pop());
1074     updateAbove(event.node());
1075   } else {
1076     // Otherwise create a new node that marks the migration event,
1077     Node* mig_node = nodes()->createNode(event.time());
1078     dout << "Marker: " << mig_node << "... " << std::flush;
1079     nodes()->add(mig_node, event.node());
1080     mig_node->set_population(event.mig_pop());
1081 
1082     // integrate it into the tree
1083     event.node()->set_parent(mig_node);
1084     mig_node->set_first_child(event.node());
1085     updateAbove(event.node(), false, false);
1086     updateAbove(mig_node);
1087 
1088     // and set it active.
1089     this->set_active_node(event.active_node_nr(), mig_node);
1090     assert(mig_node->is_migrating());
1091 
1092     // Now make the event node local
1093     event.node()->make_local();
1094   }
1095   // Recalculate the interval
1096   if (recalculate) ti.recalculateInterval();
1097   dout << "done." << std::endl;
1098 
1099   assert( event.node()->local() );
1100 }
1101 
1102 
implementFixedTimeEvent(TimeIntervalIterator & ti)1103 void Forest::implementFixedTimeEvent(TimeIntervalIterator &ti) {
1104   dout << "* * Fixed time event" << std::endl;
1105   double sample;
1106   auto mig_events = model().single_mig_events();
1107 
1108   for (size_t i = 0; i < 2; ++i) {
1109     if (states_[i] != 1) continue;
1110 
1111     sample = random_generator()->sample();
1112     for (auto me : mig_events) {
1113       if (active_node(i)->population() == me.source_pop) {
1114         sample -= me.prob;
1115       }
1116 
1117       if (sample < 0) {
1118         dout << "* * * a" << i << ": Migration from "
1119              << me.source_pop << " to "
1120              << me.sink_pop << std::endl;
1121         tmp_event_ = Event((*ti).start_height());
1122         tmp_event_.setToMigration(active_node(i), i, me.sink_pop);
1123         implementMigration(tmp_event_, false, ti);
1124         sample = random_generator()->sample();
1125       }
1126     }
1127   }
1128 
1129   assert( printTree() );
1130 }
1131 
1132 /**
1133  * Helper function for doing a coalescence.
1134  * Moves the 'active' flag (i.e. the node stored in root_1 or root_2 in sampleCoalescence)
1135  * from a node to it's parent if the branch above the node
1136  * ends this the current time interval.
1137  *
1138  * This function is used the pass the active flag upwards in the tree if the
1139  * node is active, but neither coalescing nor a recombination happens on the
1140  * branch above, e.g. after a local-branch became active because it was hit by a
1141  * coalescence or a non-local branch was active and no recombination occurred.
1142  *
1143  * Also updates the active node if it moves up.
1144  *
1145  * \param node An active node
1146  * \param time_interval The time interval the coalescence is currently in.
1147  *
1148  * \return  Either the parent of 'node' if we need to move upwards or 'node'
1149  *          itself
1150  */
possiblyMoveUpwards(Node * node,const TimeInterval & time_interval)1151 Node* Forest::possiblyMoveUpwards(Node* node, const TimeInterval &time_interval) {
1152   if ( node->parent_height() == time_interval.end_height() ) {
1153     node->make_local();
1154     updateAbove(node, false, false);
1155     return node->parent();
1156   }
1157   return node;
1158 }
1159 
1160 
pruneNodeIfNeeded(Node * node,const bool prune_orphans)1161 bool Forest::pruneNodeIfNeeded(Node* node, const bool prune_orphans) {
1162   assert(node != NULL);
1163   if (!model().has_approximation()) return false;
1164   if (node->in_sample()) return false;
1165 
1166   if (node->is_root()) {
1167     // Orphaned nodes must go
1168     if (node->countChildren() == 0 && prune_orphans) {
1169       dout << "* * * PRUNING: Removing node " << node << " from tree (orphaned)" << std::endl;
1170       assert(node != local_root());
1171       // If we are removing the primary root, it is difficult to find the new
1172       // primary root. It should however be automatically set during the updates
1173       // of the current coalescence.
1174       if (node == primary_root()) set_primary_root(NULL);
1175       nodes()->remove(node);
1176       return true;
1177     }
1178     // Other roots stay
1179     return false;
1180   } else {
1181     // No root:
1182     if (nodeIsOld(node)) {
1183       // Old nodes have to go, no matter what
1184       dout << "* * * PRUNING: Removing branch above " << node << " from tree (old)" << std::endl;
1185       assert(!node->is_root());
1186 
1187       node->parent()->change_child(node, NULL);
1188       if (node->countChildren() == 0) nodes()->remove(node);
1189       else {
1190         // Remove link between `node` and its parent,
1191         // which effectively removes the branch,
1192         // and separates the subtree below from the main tree
1193         Node* parent = node->parent();
1194         node->set_parent(NULL);
1195         updateAbove(parent, false, true, true);
1196       }
1197       return true;
1198     }
1199 
1200     else if (node->countChildren() == 1 && !node->is_migrating()) {
1201       // Unneeded nodes
1202       dout << "* * * PRUNING: Removing node " << node << " from tree (unneeded)" << std::endl;
1203       assert(!node->is_migrating());
1204       assert(node->first_child()->last_update() == node->last_update());
1205       assert(node->first_child()->local() == node->local());
1206 
1207       Node* child = node->first_child();
1208       child->set_parent(node->parent());
1209       node->parent()->change_child(node, child);
1210       nodes()->remove(node);
1211       return true;
1212     }
1213   }
1214 
1215   return false;
1216 }
1217 
1218 
calcSegmentSumStats()1219 void Forest::calcSegmentSumStats() {
1220   for (size_t i = 0; i < model().countSummaryStatistics(); ++i) {
1221     model().getSummaryStatistic(i)->calculate(*this);
1222   }
1223 }
1224 
1225 
clearSumStats()1226 void Forest::clearSumStats() {
1227   for (size_t i = 0; i < model().countSummaryStatistics(); ++i) {
1228     model().getSummaryStatistic(i)->clear();
1229   }
1230 }
1231 
1232 
printLocusSumStats(std::ostream & output) const1233 void Forest::printLocusSumStats(std::ostream &output) const {
1234   for (size_t i = 0; i < model().countSummaryStatistics(); ++i) {
1235     model().getSummaryStatistic(i)->printLocusOutput(output);
1236   }
1237 }
1238 
1239 
printSegmentSumStats(std::ostream & output) const1240 void Forest::printSegmentSumStats(std::ostream &output) const {
1241   for (size_t i = 0; i < model().countSummaryStatistics(); ++i) {
1242     model().getSummaryStatistic(i)->printSegmentOutput(output);
1243   }
1244 }
1245 
1246 
clear()1247 void Forest::clear() {
1248   // Clear roots tracking
1249   set_local_root(NULL);
1250   set_primary_root(NULL);
1251 
1252   // Clear nodes
1253   nodes()->clear();
1254   contemporaries()->clear(true);
1255 
1256   // Reset Position & Segment Counts
1257   this->rec_bases_.clear();
1258   this->set_next_base(-1.0);
1259   this->current_rec_ = 0;
1260 
1261   // Clear Summary Statistics
1262   this->clearSumStats();
1263 
1264   // Reset Model
1265   writable_model()->resetTime();
1266   writable_model()->resetSequencePosition();
1267 }
1268 
1269 
1270 
readNewickNode(std::string & in_str,std::string::iterator & it,size_t parenthesis_balance,Node * const parent)1271 Node* Forest::readNewickNode( std::string &in_str, std::string::iterator &it, size_t parenthesis_balance, Node* const parent ){
1272   Node * node = nodes()->createNode( (double)0.0, (size_t)0 );
1273   node->set_parent ( parent );
1274   node->make_local();
1275   //this->nodes()->push_front( node );
1276   dout << "Node " << node
1277        << " starts from [ " << std::endl;
1278   for (  ; it != in_str.end(); ++it) {
1279     dout << "\""<<(*it) <<"\"" ;
1280     if        ( (*it) == '(' )  { // Start of a internal node, extract a new node
1281       parenthesis_balance++;
1282       Node* child_1 = this->readNewickNode ( in_str, it = (it+1),parenthesis_balance, node );
1283       node->set_first_child ( child_1 );
1284       this->nodes()->add( child_1 );
1285       if ( node->first_child() != NULL)
1286         node->set_height ( node->first_child()->height() + 40000*node->first_child()->bl()  ) ;
1287     } else if ( (*(it+1)) == ',' ){ //
1288       node->extract_bl_and_label(it);
1289       dout << " " << parent
1290            << " has first child node " << node
1291            << " with branch length "   << node->bl()
1292            << ", and with the label "  << node->label()
1293            << ", height "              << node->height()
1294            << " ]  Node " << node << " closed " << std::endl;
1295       //if ( !node->in_sample() ) this->contemporaries_.add(node);
1296       return node;
1297     } else if ( (*(it)) == ',' ){ //
1298       Node* child_2 = this->readNewickNode ( in_str, it=(it + 1), parenthesis_balance, node );
1299       node->set_second_child ( child_2 );
1300       this->nodes()->add( child_2 );
1301     } else if ( (*(it+1)) == ')'  ){
1302       // Before return, extract the branch length for the second node
1303       node->extract_bl_and_label(it);
1304       dout << " " << parent
1305            << " has second child node " << node
1306            << " with branch length "   << node->bl()
1307            << ", and with the label "  << node->label()
1308            << ", height "              << node->height()
1309            << " ]  Node " << node << " closed " << std::endl;
1310       //if ( !node->in_sample() ) this->contemporaries_.add(node);
1311       return node;
1312     } else if ( (*(it)) == ';' ) {
1313       dout <<" Node " << node << " closed " << std::endl;
1314       this->nodes()->add( node );
1315       node->make_nonlocal(current_rec());
1316       return node;
1317     } else {
1318       continue;
1319     }
1320   }
1321   assert(false);
1322   return NULL;
1323 }
1324 
1325 
readNewick(std::string & in_str)1326 void Forest::readNewick( std::string &in_str ){
1327   this->set_next_base(0.0);
1328   ++current_rec_;
1329   std::string::iterator it = in_str.begin();
1330   (void)this->readNewickNode( in_str, it );
1331   this->set_local_root( this->nodes()->last() );
1332   this->set_primary_root(this->nodes()->last() );
1333   dout << std::endl<<"there are "<< this->nodes()->size() << " nodes " << std::endl;
1334   (void)this->nodes()->sorted();
1335   for (auto it = nodes()->iterator(); it.good(); ++it) {
1336     updateAbove(*it, false, false);
1337   }
1338   assert(this->printNodes());
1339   assert(this->printTree());
1340   dout << "contemporaries_.size()"<<contemporaries_.size(0) <<std::endl;
1341   this->sampleNextBase();
1342   this->calcSegmentSumStats();
1343   this->tmp_event_time_ = this->local_root()->height();
1344 }
1345 
1346 
1347 // Must be called AFTER the tree was modified.
sampleNextBase()1348 void Forest::sampleNextBase() {
1349   double length = random_generator()->sampleExpoLimit(getLocalTreeLength() * model().recombination_rate(),
1350                                                       model().getNextSequencePosition() - current_base());
1351   if (length == -1) {
1352     // No recombination until the model changes
1353     set_next_base(model().getNextSequencePosition());
1354     if (next_base() < model().loci_length()) writable_model()->increaseSequencePosition();
1355   } else {
1356     // Recombination in the sequence segment
1357     set_next_base(current_base() + length);
1358   }
1359 
1360   assert(next_base() > current_base());
1361   assert(next_base() <= model().loci_length());
1362 }
1363 
1364