6c4d26797e88f2a445dbc80a9b273a9b96f17eb8
[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 import java.util.Set;\r
30 import java.util.SortedSet;\r
31 \r
32 import org.forester.phylogeny.Phylogeny;\r
33 import org.forester.phylogeny.PhylogenyBranch;\r
34 import org.forester.phylogeny.PhylogenyMethods;\r
35 import org.forester.phylogeny.PhylogenyNode;\r
36 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;\r
37 import org.forester.sdi.SDIutil.TaxonomyComparisonBase;\r
38 import org.forester.util.BasicDescriptiveStatistics;\r
39 \r
40 public class GSDIR {\r
41 \r
42     private int                              _min_duplications_sum;\r
43     private final BasicDescriptiveStatistics _duplications_sum_stats;\r
44     private final List<Phylogeny>            _min_duplications_sum_gene_trees;\r
45     protected int                            _speciations_sum;\r
46     protected int                            _duplications_sum;\r
47     private final List<PhylogenyNode>        _stripped_gene_tree_nodes;\r
48     private final List<PhylogenyNode>        _stripped_species_tree_nodes;\r
49     private final Set<PhylogenyNode>         _mapped_species_tree_nodes;\r
50     private final TaxonomyComparisonBase     _tax_comp_base;\r
51     private final SortedSet<String>          _scientific_names_mapped_to_reduced_specificity;\r
52 \r
53     public GSDIR( final Phylogeny gene_tree,\r
54                   final Phylogeny species_tree,\r
55                   final boolean strip_gene_tree,\r
56                   final boolean strip_species_tree ) throws SDIException {\r
57         _speciations_sum = 0;\r
58         _duplications_sum = 0;\r
59         final NodesLinkingResult nodes_linking_result = GSDI.linkNodesOfG( gene_tree,\r
60                                                                            species_tree,\r
61                                                                            null,\r
62                                                                            strip_gene_tree,\r
63                                                                            strip_species_tree );\r
64         _stripped_gene_tree_nodes = nodes_linking_result.getStrippedGeneTreeNodes();\r
65         _stripped_species_tree_nodes = nodes_linking_result.getStrippedSpeciesTreeNodes();\r
66         _mapped_species_tree_nodes = nodes_linking_result.getMappedSpeciesTreeNodes();\r
67         _scientific_names_mapped_to_reduced_specificity = nodes_linking_result\r
68                 .getScientificNamesMappedToReducedSpecificity();\r
69         _tax_comp_base = nodes_linking_result.getTaxCompBase();\r
70         final List<PhylogenyBranch> gene_tree_branches_post_order = new ArrayList<PhylogenyBranch>();\r
71         for( final PhylogenyNodeIterator it = gene_tree.iteratorPostorder(); it.hasNext(); ) {\r
72             final PhylogenyNode n = it.next();\r
73             if ( !n.isRoot() && !( n.getParent().isRoot() && n.isFirstChildNode() ) ) {\r
74                 gene_tree_branches_post_order.add( new PhylogenyBranch( n, n.getParent() ) );\r
75             }\r
76         }\r
77         _min_duplications_sum = Integer.MAX_VALUE;\r
78         _min_duplications_sum_gene_trees = new ArrayList<Phylogeny>();\r
79         _duplications_sum_stats = new BasicDescriptiveStatistics();\r
80         for( final PhylogenyBranch branch : gene_tree_branches_post_order ) {\r
81             _duplications_sum = 0;\r
82             _speciations_sum = 0;\r
83             gene_tree.reRoot( branch );\r
84             PhylogenyMethods.preOrderReId( species_tree );\r
85             //TEST, remove later\r
86             //            for( final PhylogenyNodeIterator it = _gene_tree.iteratorPostorder(); it.hasNext(); ) {\r
87             //                final PhylogenyNode g = it.next();\r
88             //                if ( g.isInternal() ) {\r
89             //                    g.setLink( null );\r
90             //                }\r
91             //            }\r
92             final GSDIsummaryResult gsdi_summary_result = new GSDIsummaryResult();\r
93             GSDI.geneTreePostOrderTraversal( gene_tree, true, gsdi_summary_result );\r
94             if ( _duplications_sum < _min_duplications_sum ) {\r
95                 _min_duplications_sum = _duplications_sum;\r
96                 _min_duplications_sum_gene_trees.clear();\r
97                 _min_duplications_sum_gene_trees.add( gene_tree.copy() );\r
98                 //_speciations_sum\r
99             }\r
100             else if ( _duplications_sum == _min_duplications_sum ) {\r
101                 _min_duplications_sum_gene_trees.add( gene_tree.copy() );\r
102             }\r
103             _duplications_sum_stats.addValue( _duplications_sum );\r
104         }\r
105     }\r
106 \r
107     public int getMinDuplicationsSum() {\r
108         return _min_duplications_sum;\r
109     }\r
110 \r
111     public List<Phylogeny> getMinDuplicationsSumGeneTrees() {\r
112         return _min_duplications_sum_gene_trees;\r
113     }\r
114 \r
115     public BasicDescriptiveStatistics getDuplicationsSumStats() {\r
116         return _duplications_sum_stats;\r
117     }\r
118 \r
119     public Set<PhylogenyNode> getMappedExternalSpeciesTreeNodes() {\r
120         return _mapped_species_tree_nodes;\r
121     }\r
122 \r
123     public final SortedSet<String> getReMappedScientificNamesFromGeneTree() {\r
124         return _scientific_names_mapped_to_reduced_specificity;\r
125     }\r
126 \r
127     public List<PhylogenyNode> getStrippedExternalGeneTreeNodes() {\r
128         return _stripped_gene_tree_nodes;\r
129     }\r
130 \r
131     public List<PhylogenyNode> getStrippedSpeciesTreeNodes() {\r
132         return _stripped_species_tree_nodes;\r
133     }\r
134 \r
135     public TaxonomyComparisonBase getTaxCompBase() {\r
136         return _tax_comp_base;\r
137     }\r
138 }\r