X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fsdi%2FGSDIR.java;h=7ca8dca5e490411d96f5dddf454e310b4e8f8587;hb=b819fa043cac2722618af63f0d4752ffa1a40890;hp=611ef4ac13aaa1cd20b5c9abbb376b7671bd8b05;hpb=03001eb3d298d53e97e4194787fa21bcbc43c5d8;p=jalview.git diff --git a/forester/java/src/org/forester/sdi/GSDIR.java b/forester/java/src/org/forester/sdi/GSDIR.java index 611ef4a..7ca8dca 100644 --- a/forester/java/src/org/forester/sdi/GSDIR.java +++ b/forester/java/src/org/forester/sdi/GSDIR.java @@ -39,15 +39,10 @@ import org.forester.util.BasicDescriptiveStatistics; public class GSDIR implements GSDII { - private final int _min_duplications_sum; - private final int _speciations_sum; - - @Override - public int getSpeciationsSum() { - return _speciations_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; + 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; @@ -57,10 +52,10 @@ public class GSDIR implements GSDII { public GSDIR( final Phylogeny gene_tree, final Phylogeny species_tree, final boolean strip_gene_tree, - final boolean strip_species_tree ) throws SDIException { + final boolean strip_species_tree, + final boolean transfer_taxonomy ) 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(); @@ -72,34 +67,45 @@ public class GSDIR implements GSDII { 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() && ( gene_tree.getRoot().getNumberOfDescendants() == 2 ) ) ) { gene_tree_branches_post_order.add( new PhylogenyBranch( n, n.getParent() ) ); } } + if ( gene_tree.getRoot().getNumberOfDescendants() == 2 ) { + gene_tree_branches_post_order.add( new PhylogenyBranch( gene_tree.getRoot().getChildNode1(), gene_tree + .getRoot().getChildNode2() ) ); + } 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 ) { - gene_tree.reRoot( branch ); + reRoot( branch, gene_tree ); 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 ); - // } - // } - final GSDIsummaryResult gsdi_result = GSDI.geneTreePostOrderTraversal( gene_tree, true ); + final GSDIsummaryResult gsdi_result = GSDI.geneTreePostOrderTraversal( gene_tree, + true, + min_duplications_sum ); + if ( gsdi_result == null ) { + continue; + } 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( gene_tree.copy() ); - //_speciations_sum + if ( transfer_taxonomy ) { + transferTaxonomy( gene_tree ); + } + _min_duplications_sum_gene_tree = gene_tree.copy(); } else if ( gsdi_result.getDuplicationsSum() == min_duplications_sum ) { - _min_duplications_sum_gene_trees.add( gene_tree.copy() ); + 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 ) { + if ( transfer_taxonomy ) { + transferTaxonomy( gene_tree ); + } + _min_duplications_sum_gene_tree = gene_tree.copy(); + } } _duplications_sum_stats.addValue( gsdi_result.getDuplicationsSum() ); } @@ -107,14 +113,6 @@ public class GSDIR implements GSDII { _speciations_sum = speciations_sum; } - public int getMinDuplicationsSum() { - return _min_duplications_sum; - } - - public List getMinDuplicationsSumGeneTrees() { - return _min_duplications_sum_gene_trees; - } - public BasicDescriptiveStatistics getDuplicationsSumStats() { return _duplications_sum_stats; } @@ -124,12 +122,25 @@ public class GSDIR implements GSDII { 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; } @@ -143,4 +154,70 @@ public class GSDIR implements GSDII { 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; + } + + /** + * Places the root of this Phylogeny on Branch b. The new root is always + * placed on the middle of the branch b. + * + */ + static final void reRoot( final PhylogenyBranch b, final Phylogeny phy ) { + final PhylogenyNode n1 = b.getFirstNode(); + final PhylogenyNode n2 = b.getSecondNode(); + if ( n1.isExternal() ) { + phy.reRoot( n1 ); + } + else if ( n2.isExternal() ) { + phy.reRoot( n2 ); + } + else if ( ( n2 == n1.getChildNode1() ) || ( n2 == n1.getChildNode2() ) ) { + phy.reRoot( n2 ); + } + else if ( ( n1 == n2.getChildNode1() ) || ( n1 == n2.getChildNode2() ) ) { + phy.reRoot( n1 ); + } + // else if ( ( n1.getParent() != null ) && n1.getParent().isRoot() + // && ( ( n1.getParent().getChildNode1() == n2 ) || ( n1.getParent().getChildNode2() == n2 ) ) ) { + // phy.reRoot( n1 ); + // + // } + else { + throw new IllegalArgumentException( "reRoot( Branch b ): b is not a branch." ); + } + } + + private final static void transferTaxonomy( final Phylogeny gt ) { + for( final PhylogenyNodeIterator it = gt.iteratorPostorder(); it.hasNext(); ) { + GSDI.transferTaxonomy( it.next() ); + } + } }