7ce94fcafe42a75a724db4ec1e1449dfdc7a2ef3
[jalview.git] / forester / java / src / org / forester / sdi / GSDI.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: www.phylosoft.org/forester
25
26 package org.forester.sdi;
27
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34
35 import org.forester.phylogeny.Phylogeny;
36 import org.forester.phylogeny.PhylogenyNode;
37 import org.forester.phylogeny.data.Event;
38 import org.forester.phylogeny.data.Taxonomy;
39 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
40 import org.forester.util.ForesterUtil;
41
42 /*
43  * Implements our algorithm for speciation - duplication inference (SDI). <p>
44  * The initialization is accomplished by: </p> <ul> <li>method
45  * "linkExtNodesOfG()" of class SDI: setting the links for the external nodes of
46  * the gene tree <li>"preorderReID(int)" from class Phylogeny: numbering of
47  * nodes of the species tree in preorder <li>the optional stripping of the
48  * species tree is accomplished by method "stripTree(Phylogeny,Phylogeny)" of
49  * class Phylogeny </ul> <p> The recursion part is accomplished by this class'
50  * method "geneTreePostOrderTraversal(PhylogenyNode)". <p> Requires JDK 1.5 or
51  * greater.
52  * 
53  * @see SDI#linkNodesOfG()
54  * 
55  * @see Phylogeny#preorderReID(int)
56  * 
57  * @see
58  * PhylogenyMethods#taxonomyBasedDeletionOfExternalNodes(Phylogeny,Phylogeny)
59  * 
60  * @see #geneTreePostOrderTraversal(PhylogenyNode)
61  * 
62  * @author Christian M. Zmasek
63  */
64 public final class GSDI extends SDI {
65
66     private final HashMap<PhylogenyNode, Integer> _transversal_counts;
67     private final boolean                         _most_parsimonious_duplication_model;
68     private final boolean                         _strip_gene_tree;
69     private final boolean                         _strip_species_tree;
70     private int                                   _speciation_or_duplication_events_sum;
71     private int                                   _speciations_sum;
72     private final List<PhylogenyNode>             _stripped_gene_tree_nodes;
73     private final List<PhylogenyNode>             _stripped_species_tree_nodes;
74     private final Set<PhylogenyNode>              _mapped_species_tree_nodes;
75
76     /**
77      * Constructor which sets the gene tree and the species tree to be compared.
78      * species_tree is the species tree to which the gene tree gene_tree will be
79      * compared to - with method "infer(boolean)". Both Trees must be completely
80      * binary and rooted. The actual inference is accomplished with method
81      * "infer(boolean)". The mapping cost L can then be calculated with method
82      * "computeMappingCost()".
83      * <p>
84      * 
85      * @see #infer(boolean)
86      * @see SDI#computeMappingCostL()
87      * @param gene_tree
88      *            reference to a rooted gene tree to which assign duplication vs
89      *            speciation, must have species names in the species name fields
90      *            for all external nodes
91      * @param species_tree
92      *            reference to a rooted binary species tree which might get
93      *            stripped in the process, must have species names in the
94      *            species name fields for all external nodes
95      * 
96      * @param most_parsimonious_duplication_model
97      *            set to true to assign nodes as speciations which would
98      *            otherwise be assiged as unknown because of polytomies in the
99      *            species tree.
100      * @throws SdiException 
101      * 
102      */
103     public GSDI( final Phylogeny gene_tree,
104                  final Phylogeny species_tree,
105                  final boolean most_parsimonious_duplication_model,
106                  final boolean strip_gene_tree,
107                  final boolean strip_species_tree ) throws SdiException {
108         super( gene_tree, species_tree );
109         _speciation_or_duplication_events_sum = 0;
110         _speciations_sum = 0;
111         _most_parsimonious_duplication_model = most_parsimonious_duplication_model;
112         _transversal_counts = new HashMap<PhylogenyNode, Integer>();
113         _duplications_sum = 0;
114         _strip_gene_tree = strip_gene_tree;
115         _strip_species_tree = strip_species_tree;
116         _stripped_gene_tree_nodes = new ArrayList<PhylogenyNode>();
117         _stripped_species_tree_nodes = new ArrayList<PhylogenyNode>();
118         _mapped_species_tree_nodes = new HashSet<PhylogenyNode>();
119         getSpeciesTree().preOrderReId();
120         linkNodesOfG();
121         geneTreePostOrderTraversal( getGeneTree().getRoot() );
122     }
123
124     GSDI( final Phylogeny gene_tree, final Phylogeny species_tree, final boolean most_parsimonious_duplication_model )
125             throws SdiException {
126         this( gene_tree, species_tree, most_parsimonious_duplication_model, false, false );
127     }
128
129     private final Event createDuplicationEvent() {
130         final Event event = Event.createSingleDuplicationEvent();
131         ++_duplications_sum;
132         return event;
133     }
134
135     private final Event createSingleSpeciationOrDuplicationEvent() {
136         final Event event = Event.createSingleSpeciationOrDuplicationEvent();
137         ++_speciation_or_duplication_events_sum;
138         return event;
139     }
140
141     private final Event createSpeciationEvent() {
142         final Event event = Event.createSingleSpeciationEvent();
143         ++_speciations_sum;
144         return event;
145     }
146
147     // s is the node on the species tree g maps to.
148     private final void determineEvent( final PhylogenyNode s, final PhylogenyNode g ) {
149         Event event = null;
150         // Determine how many children map to same node as parent.
151         int sum_g_childs_mapping_to_s = 0;
152         for( int i = 0; i < g.getNumberOfDescendants(); ++i ) {
153             final PhylogenyNode c = g.getChildNode( i );
154             if ( c.getLink() == s ) {
155                 ++sum_g_childs_mapping_to_s;
156             }
157         }
158         // Determine the sum of traversals.
159         int traversals_sum = 0;
160         int max_traversals = 0;
161        
162         PhylogenyNode max_traversals_node = null;
163         if ( !s.isExternal() ) {
164             
165             for( int i = 0; i < s.getNumberOfDescendants(); ++i ) {
166                 final PhylogenyNode current_node = s.getChildNode( i );
167                 final int traversals = getTraversalCount( current_node );
168                 traversals_sum += traversals;
169                 if ( traversals > max_traversals ) {
170                     max_traversals = traversals;
171                     max_traversals_node = current_node;
172                 }
173                
174             }
175         }
176         // System.out.println( " sum=" + traversals_sum );
177         // System.out.println( " max=" + max_traversals );
178         // System.out.println( " m=" + sum_g_childs_mapping_to_s );
179         if ( sum_g_childs_mapping_to_s > 0 ) {
180             if ( traversals_sum == 2 ) {
181                 event = createDuplicationEvent();
182                 System.out.print( g.toString() );
183                 System.out.println( " : ==2" );
184                 //  _transversal_counts.clear();
185             }
186             else if ( traversals_sum > 2 ) {
187                 if ( max_traversals <= 1 ) {
188                     if ( _most_parsimonious_duplication_model ) {
189                         event = createSpeciationEvent();
190                     }
191                     else {
192                         event = createSingleSpeciationOrDuplicationEvent();
193                     }
194                 }
195                 else {
196                     event = createDuplicationEvent();
197                     //System.out.println( g.toString() );
198                     _transversal_counts.put( max_traversals_node, 1 );
199                     //  _transversal_counts.clear();
200                 }
201             }
202             else {
203                 event = createDuplicationEvent();
204                 //   _transversal_counts.clear();
205             }
206             normalizeTcounts( s );
207         }
208         else {
209             event = createSpeciationEvent();
210         }
211         g.getNodeData().setEvent( event );
212     }
213
214     private void normalizeTcounts( final PhylogenyNode s ) {
215         int min_traversals = Integer.MAX_VALUE;
216         for( int i = 0; i < s.getNumberOfDescendants(); ++i ) {
217             final PhylogenyNode current_node = s.getChildNode( i );
218             final int traversals = getTraversalCount( current_node );
219           
220             if ( traversals < min_traversals ) {
221                 min_traversals = traversals;
222                 
223             }
224         }
225         for( int i = 0; i < s.getNumberOfDescendants(); ++i ) {
226             final PhylogenyNode current_node = s.getChildNode( i );
227             _transversal_counts.put( current_node, getTraversalCount( current_node  ) - min_traversals );
228         }
229     }
230
231     /**
232      * Traverses the subtree of PhylogenyNode g in postorder, calculating the
233      * mapping function M, and determines which nodes represent speciation
234      * events and which ones duplication events.
235      * <p>
236      * Preconditions: Mapping M for external nodes must have been calculated and
237      * the species tree must be labeled in preorder.
238      * <p>
239      * 
240      * @param g
241      *            starting node of a gene tree - normally the root
242      */
243     final void geneTreePostOrderTraversal( final PhylogenyNode g ) {
244         if ( !g.isExternal() ) {
245             boolean all_ext = true;
246             for( int i = 0; i < g.getNumberOfDescendants(); ++i ) {
247                 if ( g.getChildNode( i ).isInternal() ) {
248                     all_ext = false;
249                     break;
250                 }
251             }
252             if ( all_ext ) {
253                 //_transversal_counts.clear();
254             }
255             for( int i = 0; i < g.getNumberOfDescendants(); ++i ) {
256                 geneTreePostOrderTraversal( g.getChildNode( i ) );
257             }
258             final PhylogenyNode[] linked_nodes = new PhylogenyNode[ g.getNumberOfDescendants() ];
259             for( int i = 0; i < linked_nodes.length; ++i ) {
260                 if ( g.getChildNode( i ).getLink() == null ) {
261                     System.out.println( "link is null for " + g.getChildNode( i ) );
262                     System.exit( -1 );
263                 }
264                 linked_nodes[ i ] = g.getChildNode( i ).getLink();
265             }
266             final int[] min_max = obtainMinMaxIdIndices( linked_nodes );
267             int min_i = min_max[ 0 ];
268             int max_i = min_max[ 1 ];
269             // initTransversalCounts();
270             while ( linked_nodes[ min_i ] != linked_nodes[ max_i ] ) {
271                 increaseTraversalCount( linked_nodes[ max_i ] );
272                 linked_nodes[ max_i ] = linked_nodes[ max_i ].getParent();
273                 final int[] min_max_ = obtainMinMaxIdIndices( linked_nodes );
274                 min_i = min_max_[ 0 ];
275                 max_i = min_max_[ 1 ];
276             }
277             final PhylogenyNode s = linked_nodes[ max_i ];
278             g.setLink( s );
279             // Determines whether dup. or spec.
280             determineEvent( s, g );
281         }
282     }
283
284     public final int getSpeciationOrDuplicationEventsSum() {
285         return _speciation_or_duplication_events_sum;
286     }
287
288     public final int getSpeciationsSum() {
289         return _speciations_sum;
290     }
291
292     private final int getTraversalCount( final PhylogenyNode node ) {
293         if ( _transversal_counts.containsKey( node ) ) {
294             return _transversal_counts.get( node );
295         }
296         return 0;
297     }
298
299     private final void increaseTraversalCount( final PhylogenyNode node ) {
300         if ( _transversal_counts.containsKey( node ) ) {
301             _transversal_counts.put( node, _transversal_counts.get( node ) + 1 );
302         }
303         else {
304             _transversal_counts.put( node, 1 );
305         }
306         // System.out.println( "count for node " + node.getID() + " is now "
307         // + getTraversalCount( node ) );
308     }
309
310     /**
311      * This allows for linking of internal nodes of the species tree (as opposed
312      * to just external nodes, as in the method it overrides.
313      * @throws SdiException 
314      * 
315      */
316     @Override
317     //    final void linkNodesOfG() {
318     //        final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes = createTaxonomyToNodeMap();
319     //        if ( _strip_gene_tree ) {
320     //            stripGeneTree( speciestree_ext_nodes );
321     //            if ( ( _gene_tree == null ) || ( _gene_tree.getNumberOfExternalNodes() < 2 ) ) {
322     //                throw new IllegalArgumentException( "species tree does not contain any"
323     //                        + " nodes matching species in the gene tree" );
324     //            }
325     //        }
326     //        // Retrieve the reference to the PhylogenyNode with a matching species.
327     //        for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
328     //            final PhylogenyNode g = iter.next();
329     //            if ( !g.getNodeData().isHasTaxonomy() ) {
330     //                throw new IllegalArgumentException( "gene tree node " + g + " has no taxonomic data" );
331     //            }
332     //            final PhylogenyNode s = speciestree_ext_nodes.get( g.getNodeData().getTaxonomy() );
333     //            if ( s == null ) {
334     //                throw new IllegalArgumentException( "species " + g.getNodeData().getTaxonomy()
335     //                        + " not present in species tree" );
336     //            }
337     //            g.setLink( s );
338     //        }
339     //    }
340     final void linkNodesOfG() throws SdiException {
341         final Map<String, PhylogenyNode> species_to_node_map = new HashMap<String, PhylogenyNode>();
342         final List<PhylogenyNode> species_tree_ext_nodes = new ArrayList<PhylogenyNode>();
343         final TaxonomyComparisonBase tax_comp_base = determineTaxonomyComparisonBase( _gene_tree );
344         // System.out.println( "comp base is: " + tax_comp_base );
345         // Stringyfied taxonomy is the key, node is the value.
346         for( final PhylogenyNodeIterator iter = _species_tree.iteratorExternalForward(); iter.hasNext(); ) {
347             final PhylogenyNode s = iter.next();
348             species_tree_ext_nodes.add( s );
349             final String tax_str = taxonomyToString( s, tax_comp_base );
350             if ( !ForesterUtil.isEmpty( tax_str ) ) {
351                 if ( species_to_node_map.containsKey( tax_str ) ) {
352                     throw new SdiException( "taxonomy \"" + s + "\" is not unique in species tree" );
353                 }
354                 species_to_node_map.put( tax_str, s );
355             }
356         }
357         // Retrieve the reference to the node with a matching stringyfied taxonomy.
358         for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
359             final PhylogenyNode g = iter.next();
360             if ( !g.getNodeData().isHasTaxonomy() ) {
361                 if ( _strip_gene_tree ) {
362                     _stripped_gene_tree_nodes.add( g );
363                 }
364                 else {
365                     throw new SdiException( "gene tree node \"" + g + "\" has no taxonomic data" );
366                 }
367             }
368             else {
369                 final String tax_str = taxonomyToString( g, tax_comp_base );
370                 if ( ForesterUtil.isEmpty( tax_str ) ) {
371                     if ( _strip_gene_tree ) {
372                         _stripped_gene_tree_nodes.add( g );
373                     }
374                     else {
375                         throw new SdiException( "gene tree node \"" + g + "\" has no appropriate taxonomic data" );
376                     }
377                 }
378                 else {
379                     final PhylogenyNode s = species_to_node_map.get( tax_str );
380                     if ( s == null ) {
381                         if ( _strip_gene_tree ) {
382                             _stripped_gene_tree_nodes.add( g );
383                         }
384                         else {
385                             throw new SdiException( "taxonomy \"" + g.getNodeData().getTaxonomy()
386                                     + "\" not present in species tree" );
387                         }
388                     }
389                     else {
390                         g.setLink( s );
391                         _mapped_species_tree_nodes.add( s );
392                         //  System.out.println( "setting link of " + g + " to " + s );
393                     }
394                 }
395             }
396         } // for loop
397         if ( _strip_gene_tree ) {
398             for( final PhylogenyNode g : _stripped_gene_tree_nodes ) {
399                 _gene_tree.deleteSubtree( g, true );
400             }
401         }
402         if ( _strip_species_tree ) {
403             for( final PhylogenyNode s : species_tree_ext_nodes ) {
404                 if ( !_mapped_species_tree_nodes.contains( s ) ) {
405                     _species_tree.deleteSubtree( s, true );
406                 }
407             }
408         }
409     }
410
411     public Set<PhylogenyNode> getMappedExternalSpeciesTreeNodes() {
412         return _mapped_species_tree_nodes;
413     }
414
415     //    final private HashMap<Taxonomy, PhylogenyNode> createTaxonomyToNodeMap() {
416     //        final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes = new HashMap<Taxonomy, PhylogenyNode>();
417     //        for( final PhylogenyNodeIterator iter = _species_tree.iteratorLevelOrder(); iter.hasNext(); ) {
418     //            final PhylogenyNode n = iter.next();
419     //            if ( n.getNodeData().isHasTaxonomy() ) {
420     //                if ( speciestree_ext_nodes.containsKey( n.getNodeData().getTaxonomy() ) ) {
421     //                    throw new IllegalArgumentException( "taxonomy [" + n.getNodeData().getTaxonomy()
422     //                            + "] is not unique in species phylogeny" );
423     //                }
424     //                speciestree_ext_nodes.put( n.getNodeData().getTaxonomy(), n );
425     //            }
426     //        }
427     //        return speciestree_ext_nodes;
428     //    }
429     //    private final void stripGeneTree( final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes ) {
430     //        //  final Set<PhylogenyNode> to_delete = new HashSet<PhylogenyNode>();
431     //        for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
432     //            final PhylogenyNode g = iter.next();
433     //            if ( !g.getNodeData().isHasTaxonomy() ) {
434     //                throw new IllegalArgumentException( "gene tree node " + g + " has no taxonomic data" );
435     //            }
436     //            if ( !speciestree_ext_nodes.containsKey( g.getNodeData().getTaxonomy() ) ) {
437     //                _stripped_gene_tree_nodes.add( g );
438     //            }
439     //        }
440     //        for( final PhylogenyNode n : _stripped_gene_tree_nodes ) {
441     //            _gene_tree.deleteSubtree( n, true );
442     //        }
443     //    }
444     //    private final void stripGeneTree2( final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes ) {
445     //        //  final Set<PhylogenyNode> to_delete = new HashSet<PhylogenyNode>();
446     //        for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
447     //            final PhylogenyNode g = iter.next();
448     //            if ( !g.getNodeData().isHasTaxonomy() ) {
449     //                _stripped_gene_tree_nodes.add( g );
450     //            }
451     //            else {
452     //                if ( !speciestree_ext_nodes.containsKey( g.getNodeData().getTaxonomy() ) ) {
453     //                    _stripped_gene_tree_nodes.add( g );
454     //                }
455     //            }
456     //        }
457     //        for( final PhylogenyNode n : _stripped_gene_tree_nodes ) {
458     //            _gene_tree.deleteSubtree( n, true );
459     //        }
460     //    }
461     public static TaxonomyComparisonBase determineTaxonomyComparisonBase( final Phylogeny gene_tree ) {
462         int with_id_count = 0;
463         int with_code_count = 0;
464         int with_sn_count = 0;
465         int max = 0;
466         for( final PhylogenyNodeIterator iter = gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
467             final PhylogenyNode g = iter.next();
468             if ( g.getNodeData().isHasTaxonomy() ) {
469                 final Taxonomy tax = g.getNodeData().getTaxonomy();
470                 if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) {
471                     if ( ++with_id_count > max ) {
472                         max = with_id_count;
473                     }
474                 }
475                 if ( !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
476                     if ( ++with_code_count > max ) {
477                         max = with_code_count;
478                     }
479                 }
480                 if ( !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
481                     if ( ++with_sn_count > max ) {
482                         max = with_sn_count;
483                     }
484                 }
485             }
486         }
487         if ( max == 0 ) {
488             throw new IllegalArgumentException( "gene tree has no taxonomic data" );
489         }
490         else if ( max == 1 ) {
491             throw new IllegalArgumentException( "gene tree has only one node with taxonomic data" );
492         }
493         else if ( max == with_sn_count ) {
494             return SDI.TaxonomyComparisonBase.SCIENTIFIC_NAME;
495         }
496         else if ( max == with_id_count ) {
497             return SDI.TaxonomyComparisonBase.ID;
498         }
499         else {
500             return SDI.TaxonomyComparisonBase.CODE;
501         }
502     }
503
504     public List<PhylogenyNode> getStrippedExternalGeneTreeNodes() {
505         return _stripped_gene_tree_nodes;
506     }
507
508     @Override
509     public final String toString() {
510         final StringBuffer sb = new StringBuffer();
511         sb.append( "Most parsimonious duplication model: " + _most_parsimonious_duplication_model );
512         sb.append( ForesterUtil.getLineSeparator() );
513         sb.append( "Speciations sum                    : " + getSpeciationsSum() );
514         sb.append( ForesterUtil.getLineSeparator() );
515         sb.append( "Duplications sum                   : " + getDuplicationsSum() );
516         sb.append( ForesterUtil.getLineSeparator() );
517         if ( !_most_parsimonious_duplication_model ) {
518             sb.append( "Speciation or duplications sum     : " + getSpeciationOrDuplicationEventsSum() );
519             sb.append( ForesterUtil.getLineSeparator() );
520         }
521         sb.append( "mapping cost L                     : " + computeMappingCostL() );
522         return sb.toString();
523     }
524
525     static final int[] obtainMinMaxIdIndices( final PhylogenyNode[] linked_nodes ) {
526         int max_i = 0;
527         int min_i = 0;
528         int max_i_id = -Integer.MAX_VALUE;
529         int min_i_id = Integer.MAX_VALUE;
530         for( int i = 0; i < linked_nodes.length; ++i ) {
531             final int id_i = linked_nodes[ i ].getId();
532             if ( id_i > max_i_id ) {
533                 max_i = i;
534                 max_i_id = linked_nodes[ max_i ].getId();
535             }
536             if ( id_i < min_i_id ) {
537                 min_i = i;
538                 min_i_id = linked_nodes[ min_i ].getId();
539             }
540         }
541         return new int[] { min_i, max_i };
542     }
543     /**
544      * Updates the mapping function M after the root of the gene tree has been
545      * moved by one branch. It calculates M for the root of the gene tree and
546      * one of its two children.
547      * <p>
548      * To be used ONLY by method "SDIunrooted.fastInfer(Phylogeny,Phylogeny)".
549      * <p>
550      * (Last modfied: )
551      * 
552      * @param prev_root_was_dup
553      *            true if the previous root was a duplication, false otherwise
554      * @param prev_root_c1
555      *            child 1 of the previous root
556      * @param prev_root_c2
557      *            child 2 of the previous root
558      * @return number of duplications which have been assigned in gene tree
559      */
560     // int updateM( final boolean prev_root_was_dup,
561     // final PhylogenyNode prev_root_c1, final PhylogenyNode prev_root_c2 ) {
562     // final PhylogenyNode root = getGeneTree().getRoot();
563     // if ( ( root.getChildNode1() == prev_root_c1 )
564     // || ( root.getChildNode2() == prev_root_c1 ) ) {
565     // calculateMforNode( prev_root_c1 );
566     // }
567     // else {
568     // calculateMforNode( prev_root_c2 );
569     // }
570     // Event event = null;
571     // if ( prev_root_was_dup ) {
572     // event = Event.createSingleDuplicationEvent();
573     // }
574     // else {
575     // event = Event.createSingleSpeciationEvent();
576     // }
577     // root.getPhylogenyNodeData().setEvent( event );
578     // calculateMforNode( root );
579     // return getDuplications();
580     // } // updateM( boolean, PhylogenyNode, PhylogenyNode )
581     // Helper method for updateM( boolean, PhylogenyNode, PhylogenyNode )
582     // Calculates M for PhylogenyNode n, given that M for the two children
583     // of n has been calculated.
584     // (Last modified: 10/02/01)
585     // private void calculateMforNode( final PhylogenyNode n ) {
586     // if ( !n.isExternal() ) {
587     // boolean was_duplication = n.isDuplication();
588     // PhylogenyNode a = n.getChildNode1().getLink(), b = n
589     // .getChildNode2().getLink();
590     // while ( a != b ) {
591     // if ( a.getID() > b.getID() ) {
592     // a = a.getParent();
593     // }
594     // else {
595     // b = b.getParent();
596     // }
597     // }
598     // n.setLink( a );
599     // Event event = null;
600     // if ( ( a == n.getChildNode1().getLink() )
601     // || ( a == n.getChildNode2().getLink() ) ) {
602     // event = Event.createSingleDuplicationEvent();
603     // if ( !was_duplication ) {
604     // increaseDuplications();
605     // }
606     // }
607     // else {
608     // event = Event.createSingleSpeciationEvent();
609     // if ( was_duplication ) {
610     // decreaseDuplications();
611     // }
612     // }
613     // n.getPhylogenyNodeData().setEvent( event );
614     // }
615     // } // calculateMforNode( PhylogenyNode )
616 }