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