\r
import java.util.ArrayList;\r
import java.util.List;\r
+import java.util.Set;\r
+import java.util.SortedSet;\r
\r
import org.forester.phylogeny.Phylogeny;\r
import org.forester.phylogeny.PhylogenyBranch;\r
import org.forester.phylogeny.PhylogenyMethods;\r
import org.forester.phylogeny.PhylogenyNode;\r
import org.forester.phylogeny.iterators.PhylogenyNodeIterator;\r
+import org.forester.sdi.SDIutil.TaxonomyComparisonBase;\r
import org.forester.util.BasicDescriptiveStatistics;\r
\r
-public class GSDIR extends GSDI {\r
+public class GSDIR implements GSDII {\r
\r
- private int _min_duplications_sum;\r
+ private final int _min_duplications_sum;\r
+ private final int _speciations_sum;\r
private final BasicDescriptiveStatistics _duplications_sum_stats;\r
- private final List<Phylogeny> _min_duplications_sum_gene_trees;\r
+ private Phylogeny _min_duplications_sum_gene_tree;\r
+ private final List<PhylogenyNode> _stripped_gene_tree_nodes;\r
+ private final List<PhylogenyNode> _stripped_species_tree_nodes;\r
+ private final Set<PhylogenyNode> _mapped_species_tree_nodes;\r
+ private final TaxonomyComparisonBase _tax_comp_base;\r
+ private final SortedSet<String> _scientific_names_mapped_to_reduced_specificity;\r
\r
public GSDIR( final Phylogeny gene_tree,\r
final Phylogeny species_tree,\r
final boolean strip_gene_tree,\r
final boolean strip_species_tree ) throws SDIException {\r
- super( gene_tree.copy(), species_tree, strip_gene_tree );\r
- linkNodesOfG( null, strip_gene_tree, strip_species_tree );\r
+ final NodesLinkingResult nodes_linking_result = GSDI.linkNodesOfG( gene_tree,\r
+ species_tree,\r
+ strip_gene_tree,\r
+ strip_species_tree );\r
+ _stripped_gene_tree_nodes = nodes_linking_result.getStrippedGeneTreeNodes();\r
+ _stripped_species_tree_nodes = nodes_linking_result.getStrippedSpeciesTreeNodes();\r
+ _mapped_species_tree_nodes = nodes_linking_result.getMappedSpeciesTreeNodes();\r
+ _scientific_names_mapped_to_reduced_specificity = nodes_linking_result\r
+ .getScientificNamesMappedToReducedSpecificity();\r
+ _tax_comp_base = nodes_linking_result.getTaxCompBase();\r
final List<PhylogenyBranch> gene_tree_branches_post_order = new ArrayList<PhylogenyBranch>();\r
- for( final PhylogenyNodeIterator it = _gene_tree.iteratorPostorder(); it.hasNext(); ) {\r
+ for( final PhylogenyNodeIterator it = gene_tree.iteratorPostorder(); it.hasNext(); ) {\r
final PhylogenyNode n = it.next();\r
- if ( !n.isRoot() && !( n.getParent().isRoot() && n.isFirstChildNode() ) ) {\r
+ if ( !n.isRoot() /*&& !( n.getParent().isRoot() && n.isFirstChildNode() )*/) {\r
gene_tree_branches_post_order.add( new PhylogenyBranch( n, n.getParent() ) );\r
}\r
}\r
- _min_duplications_sum = Integer.MAX_VALUE;\r
- _min_duplications_sum_gene_trees = new ArrayList<Phylogeny>();\r
+ int min_duplications_sum = Integer.MAX_VALUE;\r
+ int speciations_sum = 0;\r
_duplications_sum_stats = new BasicDescriptiveStatistics();\r
for( final PhylogenyBranch branch : gene_tree_branches_post_order ) {\r
- _duplications_sum = 0;\r
- _speciations_sum = 0;\r
- _gene_tree.reRoot( branch );\r
- PhylogenyMethods.preOrderReId( getSpeciesTree() );\r
+ gene_tree.reRoot( branch );\r
+ PhylogenyMethods.preOrderReId( species_tree );\r
//TEST, remove later\r
- // for( final PhylogenyNodeIterator it = _gene_tree.iteratorPostorder(); it.hasNext(); ) {\r
+ // for( final PhylogenyNodeIterator it = gene_tree.iteratorPostorder(); it.hasNext(); ) {\r
// final PhylogenyNode g = it.next();\r
// if ( g.isInternal() ) {\r
// g.setLink( null );\r
// }\r
// }\r
- geneTreePostOrderTraversal();\r
- if ( _duplications_sum < _min_duplications_sum ) {\r
- _min_duplications_sum = _duplications_sum;\r
- _min_duplications_sum_gene_trees.clear();\r
- _min_duplications_sum_gene_trees.add( getGeneTree().copy() );\r
+ final GSDIsummaryResult gsdi_result = GSDI.geneTreePostOrderTraversal( gene_tree,\r
+ true,\r
+ min_duplications_sum );\r
+ if ( gsdi_result == null ) {\r
+ continue;\r
}\r
- else if ( _duplications_sum == _min_duplications_sum ) {\r
- _min_duplications_sum_gene_trees.add( getGeneTree().copy() );\r
+ if ( gsdi_result.getDuplicationsSum() < min_duplications_sum ) {\r
+ min_duplications_sum = gsdi_result.getDuplicationsSum();\r
+ speciations_sum = gsdi_result.getSpeciationsSum();\r
+ _min_duplications_sum_gene_tree = gene_tree.copy();\r
}\r
- _duplications_sum_stats.addValue( _duplications_sum );\r
+ else if ( gsdi_result.getDuplicationsSum() == min_duplications_sum ) {\r
+ final List<Phylogeny> l = new ArrayList<Phylogeny>();\r
+ l.add( _min_duplications_sum_gene_tree );\r
+ l.add( gene_tree );\r
+ final int index = getIndexesOfShortestTree( l ).get( 0 );\r
+ if ( index == 1 ) {\r
+ _min_duplications_sum_gene_tree = gene_tree.copy();\r
+ }\r
+ }\r
+ _duplications_sum_stats.addValue( gsdi_result.getDuplicationsSum() );\r
}\r
+ _min_duplications_sum = min_duplications_sum;\r
+ _speciations_sum = speciations_sum;\r
+ }\r
+\r
+ public BasicDescriptiveStatistics getDuplicationsSumStats() {\r
+ return _duplications_sum_stats;\r
+ }\r
+\r
+ @Override\r
+ public Set<PhylogenyNode> getMappedExternalSpeciesTreeNodes() {\r
+ return _mapped_species_tree_nodes;\r
}\r
\r
public int getMinDuplicationsSum() {\r
return _min_duplications_sum;\r
}\r
\r
- public List<Phylogeny> getMinDuplicationsSumGeneTrees() {\r
- return _min_duplications_sum_gene_trees;\r
+ public Phylogeny getMinDuplicationsSumGeneTree() {\r
+ return _min_duplications_sum_gene_tree;\r
}\r
\r
- public BasicDescriptiveStatistics getDuplicationsSumStats() {\r
- return _duplications_sum_stats;\r
+ @Override\r
+ public final SortedSet<String> getReMappedScientificNamesFromGeneTree() {\r
+ return _scientific_names_mapped_to_reduced_specificity;\r
+ }\r
+\r
+ @Override\r
+ public int getSpeciationsSum() {\r
+ return _speciations_sum;\r
+ }\r
+\r
+ @Override\r
+ public List<PhylogenyNode> getStrippedExternalGeneTreeNodes() {\r
+ return _stripped_gene_tree_nodes;\r
+ }\r
+\r
+ @Override\r
+ public List<PhylogenyNode> getStrippedSpeciesTreeNodes() {\r
+ return _stripped_species_tree_nodes;\r
+ }\r
+\r
+ @Override\r
+ public TaxonomyComparisonBase getTaxCompBase() {\r
+ return _tax_comp_base;\r
+ }\r
+\r
+ public final static List<Integer> getIndexesOfShortestTree( final List<Phylogeny> assigned_trees ) {\r
+ final List<Integer> shortests = new ArrayList<Integer>();\r
+ boolean depth = true;\r
+ double x = Double.MAX_VALUE;\r
+ for( int i = 0; i < assigned_trees.size(); ++i ) {\r
+ final Phylogeny phy = assigned_trees.get( i );\r
+ if ( i == 0 ) {\r
+ if ( PhylogenyMethods.calculateMaxDistanceToRoot( phy ) > 0 ) {\r
+ depth = false;\r
+ }\r
+ }\r
+ final double d;\r
+ if ( depth ) {\r
+ d = PhylogenyMethods.calculateMaxDepth( phy );\r
+ }\r
+ else {\r
+ d = PhylogenyMethods.calculateMaxDistanceToRoot( phy );\r
+ }\r
+ if ( d < x ) {\r
+ x = d;\r
+ shortests.clear();\r
+ shortests.add( i );\r
+ }\r
+ else if ( d == x ) {\r
+ shortests.add( i );\r
+ }\r
+ }\r
+ return shortests;\r
}\r
}\r