X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fsdi%2FGSDIR.java;h=c211c423148a5abdc00ee17103ccd054eb21d174;hb=eb56c16825aa7d894fe610cabb1b1613c90d5fc8;hp=8179811a7ae0bf1231584d81d273f6f7e1252122;hpb=9a9e40a5768c100cbbb70e32a2637c82890d2d53;p=jalview.git diff --git a/forester/java/src/org/forester/sdi/GSDIR.java b/forester/java/src/org/forester/sdi/GSDIR.java index 8179811..c211c42 100644 --- a/forester/java/src/org/forester/sdi/GSDIR.java +++ b/forester/java/src/org/forester/sdi/GSDIR.java @@ -26,59 +26,85 @@ package org.forester.sdi; import java.util.ArrayList; import java.util.List; +import java.util.Set; +import java.util.SortedSet; import org.forester.phylogeny.Phylogeny; import org.forester.phylogeny.PhylogenyBranch; import org.forester.phylogeny.PhylogenyMethods; import org.forester.phylogeny.PhylogenyNode; import org.forester.phylogeny.iterators.PhylogenyNodeIterator; +import org.forester.sdi.SDIutil.TaxonomyComparisonBase; import org.forester.util.BasicDescriptiveStatistics; -public class GSDIR extends GSDI { +public class GSDIR implements GSDII { - private int _min_duplications_sum; + private final int _min_duplications_sum; + private final int _speciations_sum; + + @Override + public int getSpeciationsSum() { + return _speciations_sum; + } private final BasicDescriptiveStatistics _duplications_sum_stats; private final List _min_duplications_sum_gene_trees; + private final List _stripped_gene_tree_nodes; + private final List _stripped_species_tree_nodes; + private final Set _mapped_species_tree_nodes; + private final TaxonomyComparisonBase _tax_comp_base; + private final SortedSet _scientific_names_mapped_to_reduced_specificity; - public GSDIR( final Phylogeny gene_tree, final Phylogeny species_tree, final boolean strip_gene_tree, final int x ) - throws SDIException { - super( gene_tree.copy(), species_tree, true, strip_gene_tree, true, 1 ); - _min_duplications_sum = Integer.MAX_VALUE; - _min_duplications_sum_gene_trees = new ArrayList(); - _duplications_sum_stats = new BasicDescriptiveStatistics(); - linkNodesOfG(); + public GSDIR( final Phylogeny gene_tree, + final Phylogeny species_tree, + final boolean strip_gene_tree, + final boolean strip_species_tree ) throws SDIException { + 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(); + _stripped_species_tree_nodes = nodes_linking_result.getStrippedSpeciesTreeNodes(); + _mapped_species_tree_nodes = nodes_linking_result.getMappedSpeciesTreeNodes(); + _scientific_names_mapped_to_reduced_specificity = nodes_linking_result + .getScientificNamesMappedToReducedSpecificity(); + _tax_comp_base = nodes_linking_result.getTaxCompBase(); final List gene_tree_branches_post_order = new ArrayList(); - for( final PhylogenyNodeIterator it = _gene_tree.iteratorPostorder(); it.hasNext(); ) { + for( final PhylogenyNodeIterator it = gene_tree.iteratorPostorder(); it.hasNext(); ) { final PhylogenyNode n = it.next(); - if ( !n.isRoot() ) { + if ( !n.isRoot() /*&& !( n.getParent().isRoot() && n.isFirstChildNode() )*/) { gene_tree_branches_post_order.add( new PhylogenyBranch( n, n.getParent() ) ); } } + int min_duplications_sum = Integer.MAX_VALUE; + int speciations_sum = 0; + _min_duplications_sum_gene_trees = new ArrayList(); + _duplications_sum_stats = new BasicDescriptiveStatistics(); for( final PhylogenyBranch branch : gene_tree_branches_post_order ) { - _duplications_sum = 0; - _speciation_or_duplication_events_sum = 0; - _speciations_sum = 0; - _gene_tree.reRoot( branch ); - PhylogenyMethods.preOrderReId( getSpeciesTree() ); + gene_tree.reRoot( branch ); + PhylogenyMethods.preOrderReId( species_tree ); //TEST, remove later - for( final PhylogenyNodeIterator it = _gene_tree.iteratorPostorder(); it.hasNext(); ) { - final PhylogenyNode g = it.next(); - if ( g.isInternal() ) { - g.setLink( null ); - } - } - geneTreePostOrderTraversal(); - if ( _duplications_sum < _min_duplications_sum ) { - _min_duplications_sum = _duplications_sum; + // for( final PhylogenyNodeIterator it = _gene_tree.iteratorPostorder(); it.hasNext(); ) { + // final PhylogenyNode g = it.next(); + // if ( g.isInternal() ) { + // g.setLink( null ); + // } + // } + final GSDIsummaryResult gsdi_result = GSDI.geneTreePostOrderTraversal( gene_tree, true ); + if ( gsdi_result.getDuplicationsSum() < min_duplications_sum ) { + min_duplications_sum = gsdi_result.getDuplicationsSum(); + speciations_sum = gsdi_result.getSpeciationsSum(); _min_duplications_sum_gene_trees.clear(); - _min_duplications_sum_gene_trees.add( getGeneTree().copy() ); + _min_duplications_sum_gene_trees.add( gene_tree.copy() ); + //_speciations_sum } - else if ( _duplications_sum == _min_duplications_sum ) { - _min_duplications_sum_gene_trees.add( getGeneTree().copy() ); + else if ( gsdi_result.getDuplicationsSum() == min_duplications_sum ) { + _min_duplications_sum_gene_trees.add( gene_tree.copy() ); } - _duplications_sum_stats.addValue( _duplications_sum ); + _duplications_sum_stats.addValue( gsdi_result.getDuplicationsSum() ); } - System.out.println( _duplications_sum_stats.getSummaryAsString() ); + _min_duplications_sum = min_duplications_sum; + _speciations_sum = speciations_sum; } public int getMinDuplicationsSum() { @@ -92,4 +118,59 @@ public class GSDIR extends GSDI { public BasicDescriptiveStatistics getDuplicationsSumStats() { return _duplications_sum_stats; } + + @Override + public Set getMappedExternalSpeciesTreeNodes() { + return _mapped_species_tree_nodes; + } + + @Override + public final SortedSet getReMappedScientificNamesFromGeneTree() { + return _scientific_names_mapped_to_reduced_specificity; + } + + @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; + } }