improving GSDI, under construction...
[jalview.git] / forester / java / src / org / forester / sdi / GSDIold.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 GSDIold 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 GSDIold( 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(), null );
122         geneTreePostOrderTraversal2();
123     }
124
125     GSDIold( final Phylogeny gene_tree, final Phylogeny species_tree, final boolean most_parsimonious_duplication_model )
126             throws SdiException {
127         this( gene_tree, species_tree, most_parsimonious_duplication_model, false, false );
128     }
129
130     // s is the node on the species tree g maps to.
131     private final void determineEvent( final PhylogenyNode s, final PhylogenyNode g ) {
132         Event event = null;
133         // Determine how many children map to same node as parent.
134         int sum_g_childs_mapping_to_s = 0;
135         for( int i = 0; i < g.getNumberOfDescendants(); ++i ) {
136             final PhylogenyNode c = g.getChildNode( i );
137             if ( c.getLink() == s ) {
138                 ++sum_g_childs_mapping_to_s;
139             }
140         }
141         // Determine the sum of traversals.
142         int traversals_sum = 0;
143         int max_traversals = 0;
144         PhylogenyNode max_traversals_node = null;
145         if ( !s.isExternal() ) {
146             for( int i = 0; i < s.getNumberOfDescendants(); ++i ) {
147                 final PhylogenyNode current_node = s.getChildNode( i );
148                 final int traversals = getTraversalCount( current_node );
149                 traversals_sum += traversals;
150                 if ( traversals > max_traversals ) {
151                     max_traversals = traversals;
152                     max_traversals_node = current_node;
153                 }
154             }
155         }
156         // System.out.println( " sum=" + traversals_sum );
157         // System.out.println( " max=" + max_traversals );
158         // System.out.println( " m=" + sum_g_childs_mapping_to_s );
159         if ( sum_g_childs_mapping_to_s > 0 ) {
160             if ( traversals_sum == 2 ) {
161                 event = createDuplicationEvent();
162                 System.out.print( g.toString() );
163                 System.out.println( " : ==2" );
164                 //  _transversal_counts.clear();
165             }
166             else if ( traversals_sum > 2 ) {
167                 if ( max_traversals <= 1 ) {
168                     if ( _most_parsimonious_duplication_model ) {
169                         event = createSpeciationEvent();
170                     }
171                     else {
172                         event = createSingleSpeciationOrDuplicationEvent();
173                     }
174                 }
175                 else {
176                     event = createDuplicationEvent();
177                     //     normalizeTcounts( s );
178                     //System.out.println( g.toString() );
179                     _transversal_counts.put( max_traversals_node, 1 );
180                     //  _transversal_counts.clear();
181                 }
182             }
183             else {
184                 event = createDuplicationEvent();
185                 //   _transversal_counts.clear();
186             }
187             //normalizeTcounts( s );
188         }
189         else {
190             event = createSpeciationEvent();
191         }
192         g.getNodeData().setEvent( event );
193     }
194
195     //    private final void determineEvent2( final PhylogenyNode s, final PhylogenyNode g ) {
196     //        Event event = null;
197     //        // Determine how many children map to same node as parent.
198     //        int sum_g_childs_mapping_to_s = 0;
199     //        for( int i = 0; i < g.getNumberOfDescendants(); ++i ) {
200     //            final PhylogenyNode c = g.getChildNode( i );
201     //            if ( c.getLink() == s ) {
202     //                ++sum_g_childs_mapping_to_s;
203     //            }
204     //        }
205     //        // Determine the sum of traversals.
206     //        int traversals_sum = 0;
207     //        int max_traversals = 0;
208     //        PhylogenyNode max_traversals_node = null;
209     //        if ( !s.isExternal() ) {
210     //            for( int i = 0; i < s.getNumberOfDescendants(); ++i ) {
211     //                final PhylogenyNode current_node = s.getChildNode( i );
212     //                final int traversals = getTraversalCount( current_node );
213     //                traversals_sum += traversals;
214     //                if ( traversals > max_traversals ) {
215     //                    max_traversals = traversals;
216     //                    max_traversals_node = current_node;
217     //                }
218     //            }
219     //        }
220     //        // System.out.println( " sum=" + traversals_sum );
221     //        // System.out.println( " max=" + max_traversals );
222     //        // System.out.println( " m=" + sum_g_childs_mapping_to_s );
223     //        if ( s.getNumberOfDescendants() == 2 ) {
224     //            if ( sum_g_childs_mapping_to_s == 0 ) {
225     //                event = createSpeciationEvent();
226     //            }
227     //            else {
228     //                event = createDuplicationEvent();
229     //            }
230     //        }
231     //        else {
232     //            if ( sum_g_childs_mapping_to_s == 2 ) {
233     //                event = createDuplicationEvent();
234     //            }
235     //            else if ( sum_g_childs_mapping_to_s == 1 ) {
236     //                event = createSingleSpeciationOrDuplicationEvent();
237     //            }
238     //            else {
239     //                event = createSpeciationEvent();
240     //            }
241     //        }
242     //        g.getNodeData().setEvent( event );
243     //    }
244     final void geneTreePostOrderTraversal2() {
245         PhylogenyNode g = null;
246         for( PhylogenyNodeIterator it = getGeneTree().iteratorPostorder(); it.hasNext(); ) {
247             g = it.next();
248             if ( !g.isExternal() ) {
249                 for( PhylogenyNodeIterator itw = getGeneTree().iteratorPostorder(); it.hasNext(); ) {
250                     PhylogenyNode gg = it.next();
251                     gg.setLink( null );
252                 }
253                 try {
254                     linkNodesOfG();
255                 }
256                 catch ( SdiException e ) {
257                     // TODO Auto-generated catch block
258                     e.printStackTrace();
259                 }
260                 _transversal_counts.clear();
261                 final PhylogenyNode[] linked_nodes = new PhylogenyNode[ g.getNumberOfDescendants() ];
262                 for( int i = 0; i < linked_nodes.length; ++i ) {
263                     if ( g.getChildNode( i ).getLink() == null ) {
264                         System.out.println( "link is null for " + g.getChildNode( i ) );
265                         System.exit( -1 );
266                     }
267                     linked_nodes[ i ] = g.getChildNode( i ).getLink();
268                 }
269                 final int[] min_max = obtainMinMaxIdIndices( linked_nodes );
270                 int min_i = min_max[ 0 ];
271                 int max_i = min_max[ 1 ];
272                 // initTransversalCounts();
273                 while ( linked_nodes[ min_i ] != linked_nodes[ max_i ] ) {
274                     increaseTraversalCount( linked_nodes[ max_i ] );
275                     //                if ( linked_nodes[ max_i ].isExternal() ) {
276                     //                    System.out.println( "i am ext!" );
277                     //                    PhylogenyNode n = linked_nodes[ max_i ];
278                     //                    while ( !n.isRoot() ) {
279                     //                        _transversal_counts.put( n, 0 );
280                     //                        n = n.getParent();
281                     //                    }
282                     //                }
283                     linked_nodes[ max_i ] = linked_nodes[ max_i ].getParent();
284                     final int[] min_max_ = obtainMinMaxIdIndices( linked_nodes );
285                     min_i = min_max_[ 0 ];
286                     max_i = min_max_[ 1 ];
287                 }
288                 final PhylogenyNode s = linked_nodes[ max_i ];
289                 g.setLink( s );
290                 // Determines whether dup. or spec.
291                 determineEvent( s, g );
292             }
293         }
294     }
295
296     final void geneTreePostOrderTraversal() {
297         PhylogenyNode prev;
298         PhylogenyNode g = null;
299         for( PhylogenyNodeIterator it = getGeneTree().iteratorPostorder(); it.hasNext(); ) {
300             prev = g;
301             g = it.next();
302             if ( !g.isExternal() ) {
303                 final PhylogenyNode[] linked_nodes = new PhylogenyNode[ g.getNumberOfDescendants() ];
304                 for( int i = 0; i < linked_nodes.length; ++i ) {
305                     if ( g.getChildNode( i ).getLink() == null ) {
306                         System.out.println( "link is null for " + g.getChildNode( i ) );
307                         System.exit( -1 );
308                     }
309                     linked_nodes[ i ] = g.getChildNode( i ).getLink();
310                 }
311                 final int[] min_max = obtainMinMaxIdIndices( linked_nodes );
312                 int min_i = min_max[ 0 ];
313                 int max_i = min_max[ 1 ];
314                 // initTransversalCounts();
315                 while ( linked_nodes[ min_i ] != linked_nodes[ max_i ] ) {
316                     increaseTraversalCount( linked_nodes[ max_i ] );
317                     //                if ( linked_nodes[ max_i ].isExternal() ) {
318                     //                    System.out.println( "i am ext!" );
319                     //                    PhylogenyNode n = linked_nodes[ max_i ];
320                     //                    while ( !n.isRoot() ) {
321                     //                        _transversal_counts.put( n, 0 );
322                     //                        n = n.getParent();
323                     //                    }
324                     //                }
325                     linked_nodes[ max_i ] = linked_nodes[ max_i ].getParent();
326                     final int[] min_max_ = obtainMinMaxIdIndices( linked_nodes );
327                     min_i = min_max_[ 0 ];
328                     max_i = min_max_[ 1 ];
329                 }
330                 final PhylogenyNode s = linked_nodes[ max_i ];
331                 g.setLink( s );
332                 // Determines whether dup. or spec.
333                 determineEvent( s, g );
334                 if ( false ) {
335                     System.out.println( "******" );
336                     _transversal_counts.clear();
337                 }
338             }
339         }
340     }
341
342     /**
343      * Traverses the subtree of PhylogenyNode g in postorder, calculating the
344      * mapping function M, and determines which nodes represent speciation
345      * events and which ones duplication events.
346      * <p>
347      * Preconditions: Mapping M for external nodes must have been calculated and
348      * the species tree must be labeled in preorder.
349      * <p>
350      * 
351      * @param g
352      *            starting node of a gene tree - normally the root
353      */
354     final void geneTreePostOrderTraversal( final PhylogenyNode g ) {
355         if ( !g.isExternal() ) {
356             for( int i = 0; i < g.getNumberOfDescendants(); ++i ) {
357                 geneTreePostOrderTraversal( g.getChildNode( i ) );
358             }
359             final PhylogenyNode[] linked_nodes = new PhylogenyNode[ g.getNumberOfDescendants() ];
360             for( int i = 0; i < linked_nodes.length; ++i ) {
361                 if ( g.getChildNode( i ).getLink() == null ) {
362                     System.out.println( "link is null for " + g.getChildNode( i ) );
363                     System.exit( -1 );
364                 }
365                 linked_nodes[ i ] = g.getChildNode( i ).getLink();
366             }
367             final int[] min_max = obtainMinMaxIdIndices( linked_nodes );
368             int min_i = min_max[ 0 ];
369             int max_i = min_max[ 1 ];
370             // initTransversalCounts();
371             while ( linked_nodes[ min_i ] != linked_nodes[ max_i ] ) {
372                 increaseTraversalCount( linked_nodes[ max_i ] );
373                 //                if ( linked_nodes[ max_i ].isExternal() ) {
374                 //                    System.out.println( "i am ext!" );
375                 //                    PhylogenyNode n = linked_nodes[ max_i ];
376                 //                    while ( !n.isRoot() ) {
377                 //                        _transversal_counts.put( n, 0 );
378                 //                        n = n.getParent();
379                 //                    }
380                 //                }
381                 linked_nodes[ max_i ] = linked_nodes[ max_i ].getParent();
382                 final int[] min_max_ = obtainMinMaxIdIndices( linked_nodes );
383                 min_i = min_max_[ 0 ];
384                 max_i = min_max_[ 1 ];
385             }
386             final PhylogenyNode s = linked_nodes[ max_i ];
387             g.setLink( s );
388             // Determines whether dup. or spec.
389             determineEvent( s, g );
390         }
391     }
392
393     private final Event createDuplicationEvent() {
394         final Event event = Event.createSingleDuplicationEvent();
395         ++_duplications_sum;
396         return event;
397     }
398
399     private final Event createSingleSpeciationOrDuplicationEvent() {
400         final Event event = Event.createSingleSpeciationOrDuplicationEvent();
401         ++_speciation_or_duplication_events_sum;
402         return event;
403     }
404
405     private final Event createSpeciationEvent() {
406         final Event event = Event.createSingleSpeciationEvent();
407         ++_speciations_sum;
408         return event;
409     }
410
411     public final int getSpeciationOrDuplicationEventsSum() {
412         return _speciation_or_duplication_events_sum;
413     }
414
415     public final int getSpeciationsSum() {
416         return _speciations_sum;
417     }
418
419     private final int getTraversalCount( final PhylogenyNode node ) {
420         if ( _transversal_counts.containsKey( node ) ) {
421             return _transversal_counts.get( node );
422         }
423         return 0;
424     }
425
426     private final void increaseTraversalCount( final PhylogenyNode node ) {
427         if ( _transversal_counts.containsKey( node ) ) {
428             _transversal_counts.put( node, _transversal_counts.get( node ) + 1 );
429         }
430         else {
431             _transversal_counts.put( node, 1 );
432         }
433         // System.out.println( "count for node " + node.getID() + " is now "
434         // + getTraversalCount( node ) );
435     }
436
437     private void normalizeTcounts( final PhylogenyNode s ) {
438         //        int min_traversals = Integer.MAX_VALUE;
439         //        for( int i = 0; i < s.getNumberOfDescendants(); ++i ) {
440         //            final PhylogenyNode current_node = s.getChildNode( i );
441         //            final int traversals = getTraversalCount( current_node );
442         //            if ( traversals < min_traversals ) {
443         //                min_traversals = traversals;
444         //            }
445         //        }
446         for( int i = 0; i < s.getNumberOfDescendants(); ++i ) {
447             final PhylogenyNode current_node = s.getChildNode( i );
448             // _transversal_counts.put( current_node, getTraversalCount( current_node ) - min_traversals );
449             if ( getTraversalCount( current_node ) > 1 ) {
450                 _transversal_counts.put( current_node, 1 );
451             }
452         }
453     }
454
455     /**
456      * This allows for linking of internal nodes of the species tree (as opposed
457      * to just external nodes, as in the method it overrides.
458      * @throws SdiException 
459      * 
460      */
461     @Override
462     //    final void linkNodesOfG() {
463     //        final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes = createTaxonomyToNodeMap();
464     //        if ( _strip_gene_tree ) {
465     //            stripGeneTree( speciestree_ext_nodes );
466     //            if ( ( _gene_tree == null ) || ( _gene_tree.getNumberOfExternalNodes() < 2 ) ) {
467     //                throw new IllegalArgumentException( "species tree does not contain any"
468     //                        + " nodes matching species in the gene tree" );
469     //            }
470     //        }
471     //        // Retrieve the reference to the PhylogenyNode with a matching species.
472     //        for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
473     //            final PhylogenyNode g = iter.next();
474     //            if ( !g.getNodeData().isHasTaxonomy() ) {
475     //                throw new IllegalArgumentException( "gene tree node " + g + " has no taxonomic data" );
476     //            }
477     //            final PhylogenyNode s = speciestree_ext_nodes.get( g.getNodeData().getTaxonomy() );
478     //            if ( s == null ) {
479     //                throw new IllegalArgumentException( "species " + g.getNodeData().getTaxonomy()
480     //                        + " not present in species tree" );
481     //            }
482     //            g.setLink( s );
483     //        }
484     //    }
485     final void linkNodesOfG() throws SdiException {
486         final Map<String, PhylogenyNode> species_to_node_map = new HashMap<String, PhylogenyNode>();
487         final List<PhylogenyNode> species_tree_ext_nodes = new ArrayList<PhylogenyNode>();
488         final TaxonomyComparisonBase tax_comp_base = determineTaxonomyComparisonBase( _gene_tree );
489         // System.out.println( "comp base is: " + tax_comp_base );
490         // Stringyfied taxonomy is the key, node is the value.
491         for( final PhylogenyNodeIterator iter = _species_tree.iteratorExternalForward(); iter.hasNext(); ) {
492             final PhylogenyNode s = iter.next();
493             species_tree_ext_nodes.add( s );
494             final String tax_str = taxonomyToString( s, tax_comp_base );
495             if ( !ForesterUtil.isEmpty( tax_str ) ) {
496                 if ( species_to_node_map.containsKey( tax_str ) ) {
497                     throw new SdiException( "taxonomy \"" + s + "\" is not unique in species tree" );
498                 }
499                 species_to_node_map.put( tax_str, s );
500             }
501         }
502         // Retrieve the reference to the node with a matching stringyfied taxonomy.
503         for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
504             final PhylogenyNode g = iter.next();
505             if ( !g.getNodeData().isHasTaxonomy() ) {
506                 if ( _strip_gene_tree ) {
507                     _stripped_gene_tree_nodes.add( g );
508                 }
509                 else {
510                     throw new SdiException( "gene tree node \"" + g + "\" has no taxonomic data" );
511                 }
512             }
513             else {
514                 final String tax_str = taxonomyToString( g, tax_comp_base );
515                 if ( ForesterUtil.isEmpty( tax_str ) ) {
516                     if ( _strip_gene_tree ) {
517                         _stripped_gene_tree_nodes.add( g );
518                     }
519                     else {
520                         throw new SdiException( "gene tree node \"" + g + "\" has no appropriate taxonomic data" );
521                     }
522                 }
523                 else {
524                     final PhylogenyNode s = species_to_node_map.get( tax_str );
525                     if ( s == null ) {
526                         if ( _strip_gene_tree ) {
527                             _stripped_gene_tree_nodes.add( g );
528                         }
529                         else {
530                             throw new SdiException( "taxonomy \"" + g.getNodeData().getTaxonomy()
531                                     + "\" not present in species tree" );
532                         }
533                     }
534                     else {
535                         g.setLink( s );
536                         _mapped_species_tree_nodes.add( s );
537                         //  System.out.println( "setting link of " + g + " to " + s );
538                     }
539                 }
540             }
541         } // for loop
542         if ( _strip_gene_tree ) {
543             for( final PhylogenyNode g : _stripped_gene_tree_nodes ) {
544                 _gene_tree.deleteSubtree( g, true );
545             }
546         }
547         if ( _strip_species_tree ) {
548             for( final PhylogenyNode s : species_tree_ext_nodes ) {
549                 if ( !_mapped_species_tree_nodes.contains( s ) ) {
550                     _species_tree.deleteSubtree( s, true );
551                 }
552             }
553         }
554     }
555
556     public Set<PhylogenyNode> getMappedExternalSpeciesTreeNodes() {
557         return _mapped_species_tree_nodes;
558     }
559
560     //    final private HashMap<Taxonomy, PhylogenyNode> createTaxonomyToNodeMap() {
561     //        final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes = new HashMap<Taxonomy, PhylogenyNode>();
562     //        for( final PhylogenyNodeIterator iter = _species_tree.iteratorLevelOrder(); iter.hasNext(); ) {
563     //            final PhylogenyNode n = iter.next();
564     //            if ( n.getNodeData().isHasTaxonomy() ) {
565     //                if ( speciestree_ext_nodes.containsKey( n.getNodeData().getTaxonomy() ) ) {
566     //                    throw new IllegalArgumentException( "taxonomy [" + n.getNodeData().getTaxonomy()
567     //                            + "] is not unique in species phylogeny" );
568     //                }
569     //                speciestree_ext_nodes.put( n.getNodeData().getTaxonomy(), n );
570     //            }
571     //        }
572     //        return speciestree_ext_nodes;
573     //    }
574     //    private final void stripGeneTree( final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes ) {
575     //        //  final Set<PhylogenyNode> to_delete = new HashSet<PhylogenyNode>();
576     //        for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
577     //            final PhylogenyNode g = iter.next();
578     //            if ( !g.getNodeData().isHasTaxonomy() ) {
579     //                throw new IllegalArgumentException( "gene tree node " + g + " has no taxonomic data" );
580     //            }
581     //            if ( !speciestree_ext_nodes.containsKey( g.getNodeData().getTaxonomy() ) ) {
582     //                _stripped_gene_tree_nodes.add( g );
583     //            }
584     //        }
585     //        for( final PhylogenyNode n : _stripped_gene_tree_nodes ) {
586     //            _gene_tree.deleteSubtree( n, true );
587     //        }
588     //    }
589     //    private final void stripGeneTree2( final HashMap<Taxonomy, PhylogenyNode> speciestree_ext_nodes ) {
590     //        //  final Set<PhylogenyNode> to_delete = new HashSet<PhylogenyNode>();
591     //        for( final PhylogenyNodeIterator iter = _gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
592     //            final PhylogenyNode g = iter.next();
593     //            if ( !g.getNodeData().isHasTaxonomy() ) {
594     //                _stripped_gene_tree_nodes.add( g );
595     //            }
596     //            else {
597     //                if ( !speciestree_ext_nodes.containsKey( g.getNodeData().getTaxonomy() ) ) {
598     //                    _stripped_gene_tree_nodes.add( g );
599     //                }
600     //            }
601     //        }
602     //        for( final PhylogenyNode n : _stripped_gene_tree_nodes ) {
603     //            _gene_tree.deleteSubtree( n, true );
604     //        }
605     //    }
606     public static TaxonomyComparisonBase determineTaxonomyComparisonBase( final Phylogeny gene_tree ) {
607         int with_id_count = 0;
608         int with_code_count = 0;
609         int with_sn_count = 0;
610         int max = 0;
611         for( final PhylogenyNodeIterator iter = gene_tree.iteratorExternalForward(); iter.hasNext(); ) {
612             final PhylogenyNode g = iter.next();
613             if ( g.getNodeData().isHasTaxonomy() ) {
614                 final Taxonomy tax = g.getNodeData().getTaxonomy();
615                 if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) {
616                     if ( ++with_id_count > max ) {
617                         max = with_id_count;
618                     }
619                 }
620                 if ( !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
621                     if ( ++with_code_count > max ) {
622                         max = with_code_count;
623                     }
624                 }
625                 if ( !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
626                     if ( ++with_sn_count > max ) {
627                         max = with_sn_count;
628                     }
629                 }
630             }
631         }
632         if ( max == 0 ) {
633             throw new IllegalArgumentException( "gene tree has no taxonomic data" );
634         }
635         else if ( max == 1 ) {
636             throw new IllegalArgumentException( "gene tree has only one node with taxonomic data" );
637         }
638         else if ( max == with_sn_count ) {
639             return SDI.TaxonomyComparisonBase.SCIENTIFIC_NAME;
640         }
641         else if ( max == with_id_count ) {
642             return SDI.TaxonomyComparisonBase.ID;
643         }
644         else {
645             return SDI.TaxonomyComparisonBase.CODE;
646         }
647     }
648
649     public List<PhylogenyNode> getStrippedExternalGeneTreeNodes() {
650         return _stripped_gene_tree_nodes;
651     }
652
653     @Override
654     public final String toString() {
655         final StringBuffer sb = new StringBuffer();
656         sb.append( "Most parsimonious duplication model: " + _most_parsimonious_duplication_model );
657         sb.append( ForesterUtil.getLineSeparator() );
658         sb.append( "Speciations sum                    : " + getSpeciationsSum() );
659         sb.append( ForesterUtil.getLineSeparator() );
660         sb.append( "Duplications sum                   : " + getDuplicationsSum() );
661         sb.append( ForesterUtil.getLineSeparator() );
662         if ( !_most_parsimonious_duplication_model ) {
663             sb.append( "Speciation or duplications sum     : " + getSpeciationOrDuplicationEventsSum() );
664             sb.append( ForesterUtil.getLineSeparator() );
665         }
666         sb.append( "mapping cost L                     : " + computeMappingCostL() );
667         return sb.toString();
668     }
669
670     static final int[] obtainMinMaxIdIndices( final PhylogenyNode[] linked_nodes ) {
671         int max_i = 0;
672         int min_i = 0;
673         int max_i_id = -Integer.MAX_VALUE;
674         int min_i_id = Integer.MAX_VALUE;
675         for( int i = 0; i < linked_nodes.length; ++i ) {
676             final int id_i = linked_nodes[ i ].getId();
677             if ( id_i > max_i_id ) {
678                 max_i = i;
679                 max_i_id = linked_nodes[ max_i ].getId();
680             }
681             if ( id_i < min_i_id ) {
682                 min_i = i;
683                 min_i_id = linked_nodes[ min_i ].getId();
684             }
685         }
686         return new int[] { min_i, max_i };
687     }
688     /**
689      * Updates the mapping function M after the root of the gene tree has been
690      * moved by one branch. It calculates M for the root of the gene tree and
691      * one of its two children.
692      * <p>
693      * To be used ONLY by method "SDIunrooted.fastInfer(Phylogeny,Phylogeny)".
694      * <p>
695      * (Last modfied: )
696      * 
697      * @param prev_root_was_dup
698      *            true if the previous root was a duplication, false otherwise
699      * @param prev_root_c1
700      *            child 1 of the previous root
701      * @param prev_root_c2
702      *            child 2 of the previous root
703      * @return number of duplications which have been assigned in gene tree
704      */
705     // int updateM( final boolean prev_root_was_dup,
706     // final PhylogenyNode prev_root_c1, final PhylogenyNode prev_root_c2 ) {
707     // final PhylogenyNode root = getGeneTree().getRoot();
708     // if ( ( root.getChildNode1() == prev_root_c1 )
709     // || ( root.getChildNode2() == prev_root_c1 ) ) {
710     // calculateMforNode( prev_root_c1 );
711     // }
712     // else {
713     // calculateMforNode( prev_root_c2 );
714     // }
715     // Event event = null;
716     // if ( prev_root_was_dup ) {
717     // event = Event.createSingleDuplicationEvent();
718     // }
719     // else {
720     // event = Event.createSingleSpeciationEvent();
721     // }
722     // root.getPhylogenyNodeData().setEvent( event );
723     // calculateMforNode( root );
724     // return getDuplications();
725     // } // updateM( boolean, PhylogenyNode, PhylogenyNode )
726     // Helper method for updateM( boolean, PhylogenyNode, PhylogenyNode )
727     // Calculates M for PhylogenyNode n, given that M for the two children
728     // of n has been calculated.
729     // (Last modified: 10/02/01)
730     // private void calculateMforNode( final PhylogenyNode n ) {
731     // if ( !n.isExternal() ) {
732     // boolean was_duplication = n.isDuplication();
733     // PhylogenyNode a = n.getChildNode1().getLink(), b = n
734     // .getChildNode2().getLink();
735     // while ( a != b ) {
736     // if ( a.getID() > b.getID() ) {
737     // a = a.getParent();
738     // }
739     // else {
740     // b = b.getParent();
741     // }
742     // }
743     // n.setLink( a );
744     // Event event = null;
745     // if ( ( a == n.getChildNode1().getLink() )
746     // || ( a == n.getChildNode2().getLink() ) ) {
747     // event = Event.createSingleDuplicationEvent();
748     // if ( !was_duplication ) {
749     // increaseDuplications();
750     // }
751     // }
752     // else {
753     // event = Event.createSingleSpeciationEvent();
754     // if ( was_duplication ) {
755     // decreaseDuplications();
756     // }
757     // }
758     // n.getPhylogenyNodeData().setEvent( event );
759     // }
760     // } // calculateMforNode( PhylogenyNode )
761 }