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