in progress
[jalview.git] / forester / java / src / org / forester / sdi / TaxonomyAssigner.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 org.forester.phylogeny.Phylogeny;
29 import org.forester.phylogeny.PhylogenyNode;
30 import org.forester.phylogeny.data.Taxonomy;
31
32 public class TaxonomyAssigner extends SDI {
33
34     public TaxonomyAssigner( final Phylogeny gene_tree, final Phylogeny species_tree ) throws SdiException {
35         super( gene_tree, species_tree );
36         getSpeciesTree().preOrderReId();
37         linkNodesOfG();
38         geneTreePostOrderTraversal( getGeneTree().getRoot() );
39     }
40
41     void geneTreePostOrderTraversal( final PhylogenyNode g ) {
42         if ( !g.isExternal() ) {
43             for( int i = 0; i < g.getNumberOfDescendants(); ++i ) {
44                 geneTreePostOrderTraversal( g.getChildNode( i ) );
45             }
46             final PhylogenyNode[] linked_nodes = new PhylogenyNode[ g.getNumberOfDescendants() ];
47             for( int i = 0; i < linked_nodes.length; ++i ) {
48                 linked_nodes[ i ] = g.getChildNode( i ).getLink();
49             }
50             final int[] min_max = GSDI.obtainMinMaxIdIndices( linked_nodes );
51             int min_i = min_max[ 0 ];
52             int max_i = min_max[ 1 ];
53             while ( linked_nodes[ min_i ] != linked_nodes[ max_i ] ) {
54                 linked_nodes[ max_i ] = linked_nodes[ max_i ].getParent();
55                 final int[] min_max_ = GSDI.obtainMinMaxIdIndices( linked_nodes );
56                 min_i = min_max_[ 0 ];
57                 max_i = min_max_[ 1 ];
58             }
59             final PhylogenyNode s = linked_nodes[ max_i ];
60             g.setLink( s );
61             if ( s.getNodeData().isHasTaxonomy() ) {
62                 g.getNodeData().setTaxonomy( ( Taxonomy ) s.getNodeData().getTaxonomy().copy() );
63             }
64         }
65     }
66
67     public static void execute( final Phylogeny gene_tree, final Phylogeny species_tree ) throws SdiException {
68         new TaxonomyAssigner( gene_tree, species_tree );
69     }
70 }