780aba9c24247ea35013d4c361980cfba8d0ef76
[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         PhylogenyNode max_traversals_node = null;
162         if ( !s.isExternal() ) {
163             for( int i = 0; i < s.getNumberOfDescendants(); ++i ) {
164                 final PhylogenyNode current_node = s.getChildNode( i );
165                 final int traversals = getTraversalCount( current_node );
166                 traversals_sum += traversals;
167                 if ( traversals > max_traversals ) {
168                     max_traversals = traversals;
169                     max_traversals_node = current_node;
170                 }
171             }
172         }
173         // System.out.println( " sum=" + traversals_sum );
174         // System.out.println( " max=" + max_traversals );
175         // System.out.println( " m=" + sum_g_childs_mapping_to_s );
176         if ( sum_g_childs_mapping_to_s > 0 ) {
177             if ( traversals_sum == 2 ) {
178                 event = createDuplicationEvent();
179                 System.out.print( g.toString() );
180                 System.out.println( " : ==2" );
181                 //  _transversal_counts.clear();
182             }
183             else if ( traversals_sum > 2 ) {
184                 if ( max_traversals <= 1 ) {
185                     if ( _most_parsimonious_duplication_model ) {
186                         event = createSpeciationEvent();
187                     }
188                     else {
189                         event = createSingleSpeciationOrDuplicationEvent();
190                     }
191                 }
192                 else {
193                     event = createDuplicationEvent();
194                     //System.out.println( g.toString() );
195                     _transversal_counts.put( max_traversals_node, 1 );
196                     //  _transversal_counts.clear();
197                 }
198             }
199             else {
200                 event = createDuplicationEvent();
201                 //   _transversal_counts.clear();
202             }
203             normalizeTcounts( s );
204         }
205         else {
206             event = createSpeciationEvent();
207         }
208         g.getNodeData().setEvent( event );
209     }
210
211     private void normalizeTcounts( final PhylogenyNode s ) {
212         int min_traversals = Integer.MAX_VALUE;
213         for( int i = 0; i < s.getNumberOfDescendants(); ++i ) {
214             final PhylogenyNode current_node = s.getChildNode( i );
215             final int traversals = getTraversalCount( current_node );
216             if ( traversals < min_traversals ) {
217                 min_traversals = traversals;
218             }
219         }
220         for( int i = 0; i < s.getNumberOfDescendants(); ++i ) {
221             final PhylogenyNode current_node = s.getChildNode( i );
222             _transversal_counts.put( current_node, getTraversalCount( current_node ) - min_traversals );
223         }
224     }
225
226     /**
227      * Traverses the subtree of PhylogenyNode g in postorder, calculating the
228      * mapping function M, and determines which nodes represent speciation
229      * events and which ones duplication events.
230      * <p>
231      * Preconditions: Mapping M for external nodes must have been calculated and
232      * the species tree must be labeled in preorder.
233      * <p>
234      * 
235      * @param g
236      *            starting node of a gene tree - normally the root
237      */
238     final void geneTreePostOrderTraversal( final PhylogenyNode g ) {
239         if ( !g.isExternal() ) {
240             boolean all_ext = true;
241             for( int i = 0; i < g.getNumberOfDescendants(); ++i ) {
242                 if ( g.getChildNode( i ).isInternal() ) {
243                     all_ext = false;
244                     break;
245                 }
246             }
247             if ( all_ext ) {
248                 //_transversal_counts.clear();
249             }
250             for( int i = 0; i < g.getNumberOfDescendants(); ++i ) {
251                 geneTreePostOrderTraversal( g.getChildNode( i ) );
252             }
253             final PhylogenyNode[] linked_nodes = new PhylogenyNode[ g.getNumberOfDescendants() ];
254             for( int i = 0; i < linked_nodes.length; ++i ) {
255                 if ( g.getChildNode( i ).getLink() == null ) {
256                     System.out.println( "link is null for " + g.getChildNode( i ) );
257                     System.exit( -1 );
258                 }
259                 linked_nodes[ i ] = g.getChildNode( i ).getLink();
260             }
261             final int[] min_max = obtainMinMaxIdIndices( linked_nodes );
262             int min_i = min_max[ 0 ];
263             int max_i = min_max[ 1 ];
264             // initTransversalCounts();
265             while ( linked_nodes[ min_i ] != linked_nodes[ max_i ] ) {
266                 increaseTraversalCount( linked_nodes[ max_i ] );
267                 linked_nodes[ max_i ] = linked_nodes[ max_i ].getParent();
268                 final int[] min_max_ = obtainMinMaxIdIndices( linked_nodes );
269                 min_i = min_max_[ 0 ];
270                 max_i = min_max_[ 1 ];
271             }
272             final PhylogenyNode s = linked_nodes[ max_i ];
273             g.setLink( s );
274             // Determines whether dup. or spec.
275             determineEvent( s, g );
276         }
277     }
278
279     public final int getSpeciationOrDuplicationEventsSum() {
280         return _speciation_or_duplication_events_sum;
281     }
282
283     public final int getSpeciationsSum() {
284         return _speciations_sum;
285     }
286
287     private final int getTraversalCount( final PhylogenyNode node ) {
288         if ( _transversal_counts.containsKey( node ) ) {
289             return _transversal_counts.get( node );
290         }
291         return 0;
292     }
293
294     private final void increaseTraversalCount( final PhylogenyNode node ) {
295         if ( _transversal_counts.containsKey( node ) ) {
296             _transversal_counts.put( node, _transversal_counts.get( node ) + 1 );
297         }
298         else {
299             _transversal_counts.put( node, 1 );
300         }
301         // System.out.println( "count for node " + node.getID() + " is now "
302         // + getTraversalCount( node ) );
303     }
304
305     /**
306      * This allows for linking of internal nodes of the species tree (as opposed
307      * to just external nodes, as in the method it overrides.
308      * @throws SdiException 
309      * 
310      */
311     @Override
312     //    final void linkNodesOfG() {
313     //        final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes = createTaxonomyToNodeMap();
314     //        if ( _strip_gene_tree ) {
315     //            stripGeneTree( speciestree_ext_nodes );
316     //            if ( ( _gene_tree == null ) || ( _gene_tree.getNumberOfExternalNodes() < 2 ) ) {
317     //                throw new IllegalArgumentException( "species tree does not contain any"
318     //                        + " nodes matching species in the gene tree" );
319     //            }
320     //        }
321     //        // Retrieve the reference to the PhylogenyNode with a matching species.
322     //        for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
323     //            final PhylogenyNode g = iter.next();
324     //            if ( !g.getNodeData().isHasTaxonomy() ) {
325     //                throw new IllegalArgumentException( "gene tree node " + g + " has no taxonomic data" );
326     //            }
327     //            final PhylogenyNode s = speciestree_ext_nodes.get( g.getNodeData().getTaxonomy() );
328     //            if ( s == null ) {
329     //                throw new IllegalArgumentException( "species " + g.getNodeData().getTaxonomy()
330     //                        + " not present in species tree" );
331     //            }
332     //            g.setLink( s );
333     //        }
334     //    }
335     final void linkNodesOfG() throws SdiException {
336         final Map<String, PhylogenyNode> species_to_node_map = new HashMap<String, PhylogenyNode>();
337         final List<PhylogenyNode> species_tree_ext_nodes = new ArrayList<PhylogenyNode>();
338         final TaxonomyComparisonBase tax_comp_base = determineTaxonomyComparisonBase( _gene_tree );
339         // System.out.println( "comp base is: " + tax_comp_base );
340         // Stringyfied taxonomy is the key, node is the value.
341         for( final PhylogenyNodeIterator iter = _species_tree.iteratorExternalForward(); iter.hasNext(); ) {
342             final PhylogenyNode s = iter.next();
343             species_tree_ext_nodes.add( s );
344             final String tax_str = taxonomyToString( s, tax_comp_base );
345             if ( !ForesterUtil.isEmpty( tax_str ) ) {
346                 if ( species_to_node_map.containsKey( tax_str ) ) {
347                     throw new SdiException( "taxonomy \"" + s + "\" is not unique in species tree" );
348                 }
349                 species_to_node_map.put( tax_str, s );
350             }
351         }
352         // Retrieve the reference to the node with a matching stringyfied taxonomy.
353         for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
354             final PhylogenyNode g = iter.next();
355             if ( !g.getNodeData().isHasTaxonomy() ) {
356                 if ( _strip_gene_tree ) {
357                     _stripped_gene_tree_nodes.add( g );
358                 }
359                 else {
360                     throw new SdiException( "gene tree node \"" + g + "\" has no taxonomic data" );
361                 }
362             }
363             else {
364                 final String tax_str = taxonomyToString( g, tax_comp_base );
365                 if ( ForesterUtil.isEmpty( tax_str ) ) {
366                     if ( _strip_gene_tree ) {
367                         _stripped_gene_tree_nodes.add( g );
368                     }
369                     else {
370                         throw new SdiException( "gene tree node \"" + g + "\" has no appropriate taxonomic data" );
371                     }
372                 }
373                 else {
374                     final PhylogenyNode s = species_to_node_map.get( tax_str );
375                     if ( s == null ) {
376                         if ( _strip_gene_tree ) {
377                             _stripped_gene_tree_nodes.add( g );
378                         }
379                         else {
380                             throw new SdiException( "taxonomy \"" + g.getNodeData().getTaxonomy()
381                                     + "\" not present in species tree" );
382                         }
383                     }
384                     else {
385                         g.setLink( s );
386                         _mapped_species_tree_nodes.add( s );
387                         //  System.out.println( "setting link of " + g + " to " + s );
388                     }
389                 }
390             }
391         } // for loop
392         if ( _strip_gene_tree ) {
393             for( final PhylogenyNode g : _stripped_gene_tree_nodes ) {
394                 _gene_tree.deleteSubtree( g, true );
395             }
396         }
397         if ( _strip_species_tree ) {
398             for( final PhylogenyNode s : species_tree_ext_nodes ) {
399                 if ( !_mapped_species_tree_nodes.contains( s ) ) {
400                     _species_tree.deleteSubtree( s, true );
401                 }
402             }
403         }
404     }
405
406     public Set<PhylogenyNode> getMappedExternalSpeciesTreeNodes() {
407         return _mapped_species_tree_nodes;
408     }
409
410     //    final private HashMap<Taxonomy, PhylogenyNode> createTaxonomyToNodeMap() {
411     //        final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes = new HashMap<Taxonomy, PhylogenyNode>();
412     //        for( final PhylogenyNodeIterator iter = _species_tree.iteratorLevelOrder(); iter.hasNext(); ) {
413     //            final PhylogenyNode n = iter.next();
414     //            if ( n.getNodeData().isHasTaxonomy() ) {
415     //                if ( speciestree_ext_nodes.containsKey( n.getNodeData().getTaxonomy() ) ) {
416     //                    throw new IllegalArgumentException( "taxonomy [" + n.getNodeData().getTaxonomy()
417     //                            + "] is not unique in species phylogeny" );
418     //                }
419     //                speciestree_ext_nodes.put( n.getNodeData().getTaxonomy(), n );
420     //            }
421     //        }
422     //        return speciestree_ext_nodes;
423     //    }
424     //    private final void stripGeneTree( final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes ) {
425     //        //  final Set<PhylogenyNode> to_delete = new HashSet<PhylogenyNode>();
426     //        for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
427     //            final PhylogenyNode g = iter.next();
428     //            if ( !g.getNodeData().isHasTaxonomy() ) {
429     //                throw new IllegalArgumentException( "gene tree node " + g + " has no taxonomic data" );
430     //            }
431     //            if ( !speciestree_ext_nodes.containsKey( g.getNodeData().getTaxonomy() ) ) {
432     //                _stripped_gene_tree_nodes.add( g );
433     //            }
434     //        }
435     //        for( final PhylogenyNode n : _stripped_gene_tree_nodes ) {
436     //            _gene_tree.deleteSubtree( n, true );
437     //        }
438     //    }
439     //    private final void stripGeneTree2( final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes ) {
440     //        //  final Set<PhylogenyNode> to_delete = new HashSet<PhylogenyNode>();
441     //        for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
442     //            final PhylogenyNode g = iter.next();
443     //            if ( !g.getNodeData().isHasTaxonomy() ) {
444     //                _stripped_gene_tree_nodes.add( g );
445     //            }
446     //            else {
447     //                if ( !speciestree_ext_nodes.containsKey( g.getNodeData().getTaxonomy() ) ) {
448     //                    _stripped_gene_tree_nodes.add( g );
449     //                }
450     //            }
451     //        }
452     //        for( final PhylogenyNode n : _stripped_gene_tree_nodes ) {
453     //            _gene_tree.deleteSubtree( n, true );
454     //        }
455     //    }
456     public static TaxonomyComparisonBase determineTaxonomyComparisonBase( final Phylogeny gene_tree ) {
457         int with_id_count = 0;
458         int with_code_count = 0;
459         int with_sn_count = 0;
460         int max = 0;
461         for( final PhylogenyNodeIterator iter = gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
462             final PhylogenyNode g = iter.next();
463             if ( g.getNodeData().isHasTaxonomy() ) {
464                 final Taxonomy tax = g.getNodeData().getTaxonomy();
465                 if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) {
466                     if ( ++with_id_count > max ) {
467                         max = with_id_count;
468                     }
469                 }
470                 if ( !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
471                     if ( ++with_code_count > max ) {
472                         max = with_code_count;
473                     }
474                 }
475                 if ( !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
476                     if ( ++with_sn_count > max ) {
477                         max = with_sn_count;
478                     }
479                 }
480             }
481         }
482         if ( max == 0 ) {
483             throw new IllegalArgumentException( "gene tree has no taxonomic data" );
484         }
485         else if ( max == 1 ) {
486             throw new IllegalArgumentException( "gene tree has only one node with taxonomic data" );
487         }
488         else if ( max == with_sn_count ) {
489             return SDI.TaxonomyComparisonBase.SCIENTIFIC_NAME;
490         }
491         else if ( max == with_id_count ) {
492             return SDI.TaxonomyComparisonBase.ID;
493         }
494         else {
495             return SDI.TaxonomyComparisonBase.CODE;
496         }
497     }
498
499     public List<PhylogenyNode> getStrippedExternalGeneTreeNodes() {
500         return _stripped_gene_tree_nodes;
501     }
502
503     @Override
504     public final String toString() {
505         final StringBuffer sb = new StringBuffer();
506         sb.append( "Most parsimonious duplication model: " + _most_parsimonious_duplication_model );
507         sb.append( ForesterUtil.getLineSeparator() );
508         sb.append( "Speciations sum                    : " + getSpeciationsSum() );
509         sb.append( ForesterUtil.getLineSeparator() );
510         sb.append( "Duplications sum                   : " + getDuplicationsSum() );
511         sb.append( ForesterUtil.getLineSeparator() );
512         if ( !_most_parsimonious_duplication_model ) {
513             sb.append( "Speciation or duplications sum     : " + getSpeciationOrDuplicationEventsSum() );
514             sb.append( ForesterUtil.getLineSeparator() );
515         }
516         sb.append( "mapping cost L                     : " + computeMappingCostL() );
517         return sb.toString();
518     }
519
520     static final int[] obtainMinMaxIdIndices( final PhylogenyNode[] linked_nodes ) {
521         int max_i = 0;
522         int min_i = 0;
523         int max_i_id = -Integer.MAX_VALUE;
524         int min_i_id = Integer.MAX_VALUE;
525         for( int i = 0; i < linked_nodes.length; ++i ) {
526             final int id_i = linked_nodes[ i ].getId();
527             if ( id_i > max_i_id ) {
528                 max_i = i;
529                 max_i_id = linked_nodes[ max_i ].getId();
530             }
531             if ( id_i < min_i_id ) {
532                 min_i = i;
533                 min_i_id = linked_nodes[ min_i ].getId();
534             }
535         }
536         return new int[] { min_i, max_i };
537     }
538     /**
539      * Updates the mapping function M after the root of the gene tree has been
540      * moved by one branch. It calculates M for the root of the gene tree and
541      * one of its two children.
542      * <p>
543      * To be used ONLY by method "SDIunrooted.fastInfer(Phylogeny,Phylogeny)".
544      * <p>
545      * (Last modfied: )
546      * 
547      * @param prev_root_was_dup
548      *            true if the previous root was a duplication, false otherwise
549      * @param prev_root_c1
550      *            child 1 of the previous root
551      * @param prev_root_c2
552      *            child 2 of the previous root
553      * @return number of duplications which have been assigned in gene tree
554      */
555     // int updateM( final boolean prev_root_was_dup,
556     // final PhylogenyNode prev_root_c1, final PhylogenyNode prev_root_c2 ) {
557     // final PhylogenyNode root = getGeneTree().getRoot();
558     // if ( ( root.getChildNode1() == prev_root_c1 )
559     // || ( root.getChildNode2() == prev_root_c1 ) ) {
560     // calculateMforNode( prev_root_c1 );
561     // }
562     // else {
563     // calculateMforNode( prev_root_c2 );
564     // }
565     // Event event = null;
566     // if ( prev_root_was_dup ) {
567     // event = Event.createSingleDuplicationEvent();
568     // }
569     // else {
570     // event = Event.createSingleSpeciationEvent();
571     // }
572     // root.getPhylogenyNodeData().setEvent( event );
573     // calculateMforNode( root );
574     // return getDuplications();
575     // } // updateM( boolean, PhylogenyNode, PhylogenyNode )
576     // Helper method for updateM( boolean, PhylogenyNode, PhylogenyNode )
577     // Calculates M for PhylogenyNode n, given that M for the two children
578     // of n has been calculated.
579     // (Last modified: 10/02/01)
580     // private void calculateMforNode( final PhylogenyNode n ) {
581     // if ( !n.isExternal() ) {
582     // boolean was_duplication = n.isDuplication();
583     // PhylogenyNode a = n.getChildNode1().getLink(), b = n
584     // .getChildNode2().getLink();
585     // while ( a != b ) {
586     // if ( a.getID() > b.getID() ) {
587     // a = a.getParent();
588     // }
589     // else {
590     // b = b.getParent();
591     // }
592     // }
593     // n.setLink( a );
594     // Event event = null;
595     // if ( ( a == n.getChildNode1().getLink() )
596     // || ( a == n.getChildNode2().getLink() ) ) {
597     // event = Event.createSingleDuplicationEvent();
598     // if ( !was_duplication ) {
599     // increaseDuplications();
600     // }
601     // }
602     // else {
603     // event = Event.createSingleSpeciationEvent();
604     // if ( was_duplication ) {
605     // decreaseDuplications();
606     // }
607     // }
608     // n.getPhylogenyNodeData().setEvent( event );
609     // }
610     // } // calculateMforNode( PhylogenyNode )
611 }