rio - gsdir work...
[jalview.git] / forester / java / src / org / forester / sdi / GSDIR.java
1 // $Id:\r
2 // FORESTER -- software libraries and applications\r
3 // for evolutionary biology research and applications.\r
4 //\r
5 // Copyright (C) 2008-2013 Christian M. Zmasek\r
6 // All rights reserved\r
7 //\r
8 // This library is free software; you can redistribute it and/or\r
9 // modify it under the terms of the GNU Lesser General Public\r
10 // License as published by the Free Software Foundation; either\r
11 // version 2.1 of the License, or (at your option) any later version.\r
12 //\r
13 // This library is distributed in the hope that it will be useful,\r
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
16 // Lesser General Public License for more details.\r
17 //\r
18 // You should have received a copy of the GNU Lesser General Public\r
19 // License along with this library; if not, write to the Free Software\r
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA\r
21 //\r
22 // Contact: phylosoft @ gmail . com\r
23 // WWW: www.phylosoft.org\r
24 \r
25 package org.forester.sdi;\r
26 \r
27 import java.util.ArrayList;\r
28 import java.util.List;\r
29 \r
30 import org.forester.phylogeny.Phylogeny;\r
31 import org.forester.phylogeny.PhylogenyMethods;\r
32 import org.forester.phylogeny.PhylogenyNode;\r
33 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;\r
34 import org.forester.util.BasicDescriptiveStatistics;\r
35 \r
36 public class GSDIR extends GSDI {\r
37 \r
38     private int                   _min_duplications_sum;\r
39     private final BasicDescriptiveStatistics                  _duplications_sum_stats;\r
40     private final List<Phylogeny> _min_duplications_sum_gene_trees;\r
41 \r
42     public GSDIR( final Phylogeny gene_tree, final Phylogeny species_tree, final boolean strip_gene_tree, final int x )\r
43             throws SDIException {\r
44         super( gene_tree, species_tree, true, strip_gene_tree, true, 1 );\r
45         _min_duplications_sum = Integer.MAX_VALUE;\r
46         _min_duplications_sum_gene_trees = new ArrayList<Phylogeny>();\r
47         _duplications_sum_stats = new  BasicDescriptiveStatistics();\r
48         linkNodesOfG();\r
49         final List<PhylogenyNode> gene_tree_nodes_post_order = new ArrayList<PhylogenyNode>();\r
50         for( final PhylogenyNodeIterator it = gene_tree.iteratorPostorder(); it.hasNext(); ) {\r
51             gene_tree_nodes_post_order.add( it.next() );\r
52         }\r
53         for( final PhylogenyNode root : gene_tree_nodes_post_order ) {\r
54             _gene_tree.reRoot( root );\r
55             PhylogenyMethods.preOrderReId( getSpeciesTree() );\r
56             //TEST, remove later\r
57             for( final PhylogenyNodeIterator it = getGeneTree().iteratorPostorder(); it.hasNext(); ) {\r
58                 final PhylogenyNode g = it.next();\r
59                 if ( g.isInternal() ) {\r
60                     g.setLink( null );\r
61                 }\r
62             }\r
63             geneTreePostOrderTraversal();\r
64             _duplications_sum_stats.addValue( getMinDuplicationsSum() );\r
65             if ( getDuplicationsSum() < getMinDuplicationsSum() ) {\r
66                 _min_duplications_sum = getDuplicationsSum();\r
67                 _min_duplications_sum_gene_trees.clear();\r
68                 _min_duplications_sum_gene_trees.add( getGeneTree().copy() );\r
69             }\r
70             else if ( getDuplicationsSum() == getMinDuplicationsSum() ) {\r
71                 _min_duplications_sum_gene_trees.add( getGeneTree().copy() );\r
72             }\r
73         }\r
74     }\r
75 \r
76     public int getMinDuplicationsSum() {\r
77         return _min_duplications_sum;\r
78     }\r
79 \r
80     public List<Phylogeny> getMinDuplicationsSumGeneTrees() {\r
81         return _min_duplications_sum_gene_trees;\r
82     }\r
83 \r
84     public BasicDescriptiveStatistics getDuplicationsSumStats() {\r
85         return _duplications_sum_stats;\r
86     }\r
87 }\r