X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fsdi%2FGSDIR.java;h=80a0d3aa7e2cc5a489eb2f2d89ea846e8b29737a;hb=0bcaf88108ac7d33e5a76df7fa49bf22d01c6798;hp=6c4d26797e88f2a445dbc80a9b273a9b96f17eb8;hpb=9bad57b1ba0f75075ab8c6bda1dedb906f8c6280;p=jalview.git diff --git a/forester/java/src/org/forester/sdi/GSDIR.java b/forester/java/src/org/forester/sdi/GSDIR.java index 6c4d267..80a0d3a 100644 --- a/forester/java/src/org/forester/sdi/GSDIR.java +++ b/forester/java/src/org/forester/sdi/GSDIR.java @@ -37,13 +37,12 @@ import org.forester.phylogeny.iterators.PhylogenyNodeIterator; import org.forester.sdi.SDIutil.TaxonomyComparisonBase; import org.forester.util.BasicDescriptiveStatistics; -public class GSDIR { +public class GSDIR implements GSDII { - private int _min_duplications_sum; + private final int _min_duplications_sum; + private final int _speciations_sum; private final BasicDescriptiveStatistics _duplications_sum_stats; - private final List _min_duplications_sum_gene_trees; - protected int _speciations_sum; - protected int _duplications_sum; + private Phylogeny _min_duplications_sum_gene_tree; private final List _stripped_gene_tree_nodes; private final List _stripped_species_tree_nodes; private final Set _mapped_species_tree_nodes; @@ -54,11 +53,8 @@ public class GSDIR { final Phylogeny species_tree, final boolean strip_gene_tree, final boolean strip_species_tree ) throws SDIException { - _speciations_sum = 0; - _duplications_sum = 0; final NodesLinkingResult nodes_linking_result = GSDI.linkNodesOfG( gene_tree, species_tree, - null, strip_gene_tree, strip_species_tree ); _stripped_gene_tree_nodes = nodes_linking_result.getStrippedGeneTreeNodes(); @@ -70,69 +66,118 @@ public class GSDIR { final List gene_tree_branches_post_order = new ArrayList(); for( final PhylogenyNodeIterator it = gene_tree.iteratorPostorder(); it.hasNext(); ) { final PhylogenyNode n = it.next(); - if ( !n.isRoot() && !( n.getParent().isRoot() && n.isFirstChildNode() ) ) { + if ( !n.isRoot() /*&& !( n.getParent().isRoot() && n.isFirstChildNode() )*/) { gene_tree_branches_post_order.add( new PhylogenyBranch( n, n.getParent() ) ); } } - _min_duplications_sum = Integer.MAX_VALUE; - _min_duplications_sum_gene_trees = new ArrayList(); + int min_duplications_sum = Integer.MAX_VALUE; + int speciations_sum = 0; _duplications_sum_stats = new BasicDescriptiveStatistics(); for( final PhylogenyBranch branch : gene_tree_branches_post_order ) { - _duplications_sum = 0; - _speciations_sum = 0; gene_tree.reRoot( branch ); PhylogenyMethods.preOrderReId( species_tree ); //TEST, remove later - // for( final PhylogenyNodeIterator it = _gene_tree.iteratorPostorder(); it.hasNext(); ) { + // for( final PhylogenyNodeIterator it = gene_tree.iteratorPostorder(); it.hasNext(); ) { // final PhylogenyNode g = it.next(); // if ( g.isInternal() ) { // g.setLink( null ); // } // } - final GSDIsummaryResult gsdi_summary_result = new GSDIsummaryResult(); - GSDI.geneTreePostOrderTraversal( gene_tree, true, gsdi_summary_result ); - if ( _duplications_sum < _min_duplications_sum ) { - _min_duplications_sum = _duplications_sum; - _min_duplications_sum_gene_trees.clear(); - _min_duplications_sum_gene_trees.add( gene_tree.copy() ); - //_speciations_sum + final GSDIsummaryResult gsdi_result = GSDI.geneTreePostOrderTraversal( gene_tree, + true, + min_duplications_sum ); + if ( gsdi_result == null ) { + continue; } - else if ( _duplications_sum == _min_duplications_sum ) { - _min_duplications_sum_gene_trees.add( gene_tree.copy() ); + if ( gsdi_result.getDuplicationsSum() < min_duplications_sum ) { + min_duplications_sum = gsdi_result.getDuplicationsSum(); + speciations_sum = gsdi_result.getSpeciationsSum(); + _min_duplications_sum_gene_tree = gene_tree.copy(); } - _duplications_sum_stats.addValue( _duplications_sum ); + else if ( gsdi_result.getDuplicationsSum() == min_duplications_sum ) { + final List l = new ArrayList(); + l.add( _min_duplications_sum_gene_tree ); + l.add( gene_tree ); + final int index = getIndexesOfShortestTree( l ).get( 0 ); + if ( index == 1 ) { + _min_duplications_sum_gene_tree = gene_tree.copy(); + } + } + _duplications_sum_stats.addValue( gsdi_result.getDuplicationsSum() ); } - } - - public int getMinDuplicationsSum() { - return _min_duplications_sum; - } - - public List getMinDuplicationsSumGeneTrees() { - return _min_duplications_sum_gene_trees; + _min_duplications_sum = min_duplications_sum; + _speciations_sum = speciations_sum; } public BasicDescriptiveStatistics getDuplicationsSumStats() { return _duplications_sum_stats; } + @Override public Set getMappedExternalSpeciesTreeNodes() { return _mapped_species_tree_nodes; } + public int getMinDuplicationsSum() { + return _min_duplications_sum; + } + + public Phylogeny getMinDuplicationsSumGeneTree() { + return _min_duplications_sum_gene_tree; + } + + @Override public final SortedSet getReMappedScientificNamesFromGeneTree() { return _scientific_names_mapped_to_reduced_specificity; } + @Override + public int getSpeciationsSum() { + return _speciations_sum; + } + + @Override public List getStrippedExternalGeneTreeNodes() { return _stripped_gene_tree_nodes; } + @Override public List getStrippedSpeciesTreeNodes() { return _stripped_species_tree_nodes; } + @Override public TaxonomyComparisonBase getTaxCompBase() { return _tax_comp_base; } + + public final static List getIndexesOfShortestTree( final List assigned_trees ) { + final List shortests = new ArrayList(); + boolean depth = true; + double x = Double.MAX_VALUE; + for( int i = 0; i < assigned_trees.size(); ++i ) { + final Phylogeny phy = assigned_trees.get( i ); + if ( i == 0 ) { + if ( PhylogenyMethods.calculateMaxDistanceToRoot( phy ) > 0 ) { + depth = false; + } + } + final double d; + if ( depth ) { + d = PhylogenyMethods.calculateMaxDepth( phy ); + } + else { + d = PhylogenyMethods.calculateMaxDistanceToRoot( phy ); + } + if ( d < x ) { + x = d; + shortests.clear(); + shortests.add( i ); + } + else if ( d == x ) { + shortests.add( i ); + } + } + return shortests; + } }