2 // FORESTER -- software libraries and applications
\r
3 // for evolutionary biology research and applications.
\r
5 // Copyright (C) 2008-2013 Christian M. Zmasek
\r
6 // All rights reserved
\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
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
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
22 // Contact: phylosoft @ gmail . com
\r
23 // WWW: www.phylosoft.org
\r
25 package org.forester.sdi;
\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
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
40 public class GSDIR implements GSDII {
\r
42 private final int _min_duplications_sum;
\r
43 private final int _speciations_sum;
\r
47 public int getSpeciationsSum() {
\r
48 return _speciations_sum;
\r
51 private final BasicDescriptiveStatistics _duplications_sum_stats;
\r
52 private final List<Phylogeny> _min_duplications_sum_gene_trees;
\r
54 private final List<PhylogenyNode> _stripped_gene_tree_nodes;
\r
55 private final List<PhylogenyNode> _stripped_species_tree_nodes;
\r
56 private final Set<PhylogenyNode> _mapped_species_tree_nodes;
\r
57 private final TaxonomyComparisonBase _tax_comp_base;
\r
58 private final SortedSet<String> _scientific_names_mapped_to_reduced_specificity;
\r
60 public GSDIR( final Phylogeny gene_tree,
\r
61 final Phylogeny species_tree,
\r
62 final boolean strip_gene_tree,
\r
63 final boolean strip_species_tree ) throws SDIException {
\r
65 final NodesLinkingResult nodes_linking_result = GSDI.linkNodesOfG( gene_tree,
\r
69 strip_species_tree );
\r
70 _stripped_gene_tree_nodes = nodes_linking_result.getStrippedGeneTreeNodes();
\r
71 _stripped_species_tree_nodes = nodes_linking_result.getStrippedSpeciesTreeNodes();
\r
72 _mapped_species_tree_nodes = nodes_linking_result.getMappedSpeciesTreeNodes();
\r
73 _scientific_names_mapped_to_reduced_specificity = nodes_linking_result
\r
74 .getScientificNamesMappedToReducedSpecificity();
\r
75 _tax_comp_base = nodes_linking_result.getTaxCompBase();
\r
76 final List<PhylogenyBranch> gene_tree_branches_post_order = new ArrayList<PhylogenyBranch>();
\r
77 for( final PhylogenyNodeIterator it = gene_tree.iteratorPostorder(); it.hasNext(); ) {
\r
78 final PhylogenyNode n = it.next();
\r
79 if ( !n.isRoot() && !( n.getParent().isRoot() && n.isFirstChildNode() ) ) {
\r
80 gene_tree_branches_post_order.add( new PhylogenyBranch( n, n.getParent() ) );
\r
83 int min_duplications_sum = Integer.MAX_VALUE;
\r
84 int speciations_sum = 0;
\r
85 _min_duplications_sum_gene_trees = new ArrayList<Phylogeny>();
\r
86 _duplications_sum_stats = new BasicDescriptiveStatistics();
\r
87 for( final PhylogenyBranch branch : gene_tree_branches_post_order ) {
\r
89 gene_tree.reRoot( branch );
\r
90 PhylogenyMethods.preOrderReId( species_tree );
\r
91 //TEST, remove later
\r
92 // for( final PhylogenyNodeIterator it = _gene_tree.iteratorPostorder(); it.hasNext(); ) {
\r
93 // final PhylogenyNode g = it.next();
\r
94 // if ( g.isInternal() ) {
\r
95 // g.setLink( null );
\r
98 final GSDIsummaryResult gsdi_result = GSDI.geneTreePostOrderTraversal( gene_tree, true );
\r
99 if ( gsdi_result.getDuplicationsSum() < min_duplications_sum ) {
\r
100 min_duplications_sum = gsdi_result.getDuplicationsSum();
\r
101 speciations_sum = gsdi_result.getSpeciationsSum();
\r
102 _min_duplications_sum_gene_trees.clear();
\r
103 _min_duplications_sum_gene_trees.add( gene_tree.copy() );
\r
106 else if ( gsdi_result.getDuplicationsSum()== min_duplications_sum ) {
\r
107 _min_duplications_sum_gene_trees.add( gene_tree.copy() );
\r
109 _duplications_sum_stats.addValue( gsdi_result.getDuplicationsSum() );
\r
112 _min_duplications_sum = min_duplications_sum;
\r
113 _speciations_sum = speciations_sum;
\r
116 public int getMinDuplicationsSum() {
\r
117 return _min_duplications_sum;
\r
120 public List<Phylogeny> getMinDuplicationsSumGeneTrees() {
\r
121 return _min_duplications_sum_gene_trees;
\r
124 public BasicDescriptiveStatistics getDuplicationsSumStats() {
\r
125 return _duplications_sum_stats;
\r
129 public Set<PhylogenyNode> getMappedExternalSpeciesTreeNodes() {
\r
130 return _mapped_species_tree_nodes;
\r
134 public final SortedSet<String> getReMappedScientificNamesFromGeneTree() {
\r
135 return _scientific_names_mapped_to_reduced_specificity;
\r
139 public List<PhylogenyNode> getStrippedExternalGeneTreeNodes() {
\r
140 return _stripped_gene_tree_nodes;
\r
144 public List<PhylogenyNode> getStrippedSpeciesTreeNodes() {
\r
145 return _stripped_species_tree_nodes;
\r
149 public TaxonomyComparisonBase getTaxCompBase() {
\r
150 return _tax_comp_base;
\r