X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fphylogeny%2FPhylogeny.java;h=379a9338d23cf21eb5e833a6004319f29befaf0e;hb=c0439ed8b088887ffea2faf11bc7897333287cb3;hp=34eb487176b912240b70cc7347f0429df3b0849d;hpb=f507bf348ffed906d04bc76a614d6778d4cb5d64;p=jalview.git diff --git a/forester/java/src/org/forester/phylogeny/Phylogeny.java b/forester/java/src/org/forester/phylogeny/Phylogeny.java index 34eb487..379a933 100644 --- a/forester/java/src/org/forester/phylogeny/Phylogeny.java +++ b/forester/java/src/org/forester/phylogeny/Phylogeny.java @@ -56,7 +56,6 @@ import org.forester.phylogeny.iterators.PhylogenyNodeIterator; import org.forester.phylogeny.iterators.PostorderTreeIterator; import org.forester.phylogeny.iterators.PreorderTreeIterator; import org.forester.util.FailedConditionCheckException; -import org.forester.util.ForesterUtil; public class Phylogeny { @@ -116,8 +115,8 @@ public class Phylogeny { new_node.setParent( sibling_parent ); sibling.setParent( new_node ); sibling_parent.setChildNode( sibling_index, new_node ); - final double new_dist = sibling.getDistanceToParent() == PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ? PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT - : sibling.getDistanceToParent() / 2; + final double new_dist = sibling.getDistanceToParent() == PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT + ? PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT : sibling.getDistanceToParent() / 2; new_node.setDistanceToParent( new_dist ); sibling.setDistanceToParent( new_dist ); externalNodesHaveChanged(); @@ -138,12 +137,12 @@ public class Phylogeny { else { double max = -Double.MAX_VALUE; for( int i = 0; i < n.getNumberOfDescendants(); ++i ) { - final double l = calculateSubtreeHeight( n.getChildNode( i ), take_collapse_into_account ); + final double l = calculateSubtreeHeight( n.getChildNode( i ), take_collapse_into_account ); if ( l > max ) { max = l; } } - return max + ( n.getDistanceToParent() > 0 ? n.getDistanceToParent() : 0) ; + return max + ( n.getDistanceToParent() > 0 ? n.getDistanceToParent() : 0 ); } } @@ -260,13 +259,17 @@ public class Phylogeny { if ( p.getNumberOfDescendants() == 2 ) { final int pi = p.getChildNodeIndex(); if ( removed_node.isFirstChildNode() ) { - p.getChildNode( 1 ).setDistanceToParent( PhylogenyMethods.addPhylogenyDistances( p - .getDistanceToParent(), p.getChildNode( 1 ).getDistanceToParent() ) ); + p.getChildNode( 1 ) + .setDistanceToParent( PhylogenyMethods.addPhylogenyDistances( p.getDistanceToParent(), + p.getChildNode( 1 ) + .getDistanceToParent() ) ); pp.setChildNode( pi, p.getChildNode( 1 ) ); } else { - p.getChildNode( 0 ).setDistanceToParent( PhylogenyMethods.addPhylogenyDistances( p - .getDistanceToParent(), p.getChildNode( 0 ).getDistanceToParent() ) ); + p.getChildNode( 0 ) + .setDistanceToParent( PhylogenyMethods.addPhylogenyDistances( p.getDistanceToParent(), + p.getChildNode( 0 ) + .getDistanceToParent() ) ); pp.setChildNode( pi, p.getChildNode( 0 ) ); } } @@ -321,7 +324,7 @@ public class Phylogeny { */ public List getExternalNodes() { if ( _external_nodes_set == null ) { - _external_nodes_set = new ArrayList(); + _external_nodes_set = new ArrayList<>(); for( final PhylogenyNodeIterator it = iteratorPostorder(); it.hasNext(); ) { final PhylogenyNode n = it.next(); if ( n.isExternal() ) { @@ -332,7 +335,6 @@ public class Phylogeny { return _external_nodes_set; } - /** * Returns the first external PhylogenyNode. */ @@ -353,7 +355,7 @@ public class Phylogeny { * * @return the height for rooted, tree-shaped phylogenies */ - public double calculateHeight(final boolean take_collapse_into_account) { + public double calculateHeight( final boolean take_collapse_into_account ) { if ( isEmpty() ) { return 0.0; } @@ -436,7 +438,7 @@ public class Phylogeny { if ( isEmpty() ) { return null; } - final List nodes = new ArrayList(); + final List nodes = new ArrayList<>(); for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) { final PhylogenyNode n = iter.next(); if ( n.getName().equals( name ) ) { @@ -450,7 +452,7 @@ public class Phylogeny { if ( isEmpty() ) { return null; } - final List nodes = new ArrayList(); + final List nodes = new ArrayList<>(); for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) { final PhylogenyNode n = iter.next(); if ( n.getNodeData().isHasSequence() && n.getNodeData().getSequence().getName().equals( seq_name ) ) { @@ -464,7 +466,7 @@ public class Phylogeny { if ( isEmpty() ) { return null; } - final List nodes = new ArrayList(); + final List nodes = new ArrayList<>(); for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) { final PhylogenyNode n = iter.next(); if ( n.getNodeData().isHasSequence() && n.getNodeData().getSequence().getSymbol().equals( seq_name ) ) { @@ -478,7 +480,7 @@ public class Phylogeny { if ( isEmpty() ) { return null; } - final List nodes = new ArrayList(); + final List nodes = new ArrayList<>(); for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) { final PhylogenyNode n = iter.next(); if ( n.getNodeData().isHasSequence() && n.getNodeData().getSequence().getGeneName().equals( seq_name ) ) { @@ -492,7 +494,7 @@ public class Phylogeny { if ( isEmpty() ) { return null; } - final List nodes = new ArrayList(); + final List nodes = new ArrayList<>(); for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) { final PhylogenyNode n = iter.next(); if ( n.getNodeData().isHasTaxonomy() @@ -517,7 +519,7 @@ public class Phylogeny { if ( isEmpty() ) { return null; } - final List nodes = new ArrayList(); + final List nodes = new ArrayList<>(); for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) { final PhylogenyNode n = iter.next(); if ( PhylogenyMethods.getSpecies( n ).equals( specname ) ) { @@ -616,8 +618,8 @@ public class Phylogeny { public List getParalogousNodes( final PhylogenyNode n, final String[] taxonomyCodeRange ) { PhylogenyNode node = n; PhylogenyNode prev = null; - final List v = new ArrayList(); - final Map> map = new HashMap>(); + final List v = new ArrayList<>(); + final Map> map = new HashMap<>(); getTaxonomyMap( getRoot(), map ); if ( !node.isExternal() || isEmpty() ) { return null; @@ -634,12 +636,12 @@ public class Phylogeny { taxIdList = map.get( node ); if ( node.isDuplication() && isContains( taxIdList, taxonomyCodeRangeList ) ) { if ( node.getChildNode1() == prev ) { - v.addAll( getNodeByTaxonomyID( searchNodeSpeciesId, node.getChildNode2() - .getAllExternalDescendants() ) ); + v.addAll( getNodeByTaxonomyID( searchNodeSpeciesId, + node.getChildNode2().getAllExternalDescendants() ) ); } else { - v.addAll( getNodeByTaxonomyID( searchNodeSpeciesId, node.getChildNode1() - .getAllExternalDescendants() ) ); + v.addAll( getNodeByTaxonomyID( searchNodeSpeciesId, + node.getChildNode1().getAllExternalDescendants() ) ); } } } @@ -648,7 +650,7 @@ public class Phylogeny { public Collection getRelevantSequenceRelationTypes() { if ( _relevant_sequence_relation_types == null ) { - _relevant_sequence_relation_types = new Vector(); + _relevant_sequence_relation_types = new Vector<>(); } return _relevant_sequence_relation_types; } @@ -703,6 +705,27 @@ public class Phylogeny { return true; } + public boolean isCompletelyBinaryAllow3ChildrenAtRoot() { + if ( isEmpty() ) { + return false; + } + for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) { + final PhylogenyNode node = iter.next(); + if ( node.isRoot() ) { + if ( node.isInternal() + && ( ( node.getNumberOfDescendants() != 2 ) && ( node.getNumberOfDescendants() != 3 ) ) ) { + return false; + } + } + else { + if ( node.isInternal() && ( node.getNumberOfDescendants() != 2 ) ) { + return false; + } + } + } + return true; + } + /** * Checks whether a Phylogeny object is deleted (or empty). * @@ -972,7 +995,7 @@ public class Phylogeny { } else { node.setDistanceToParent( ( c.getDistanceToParent() >= 0.0 ? c.getDistanceToParent() : 0.0 ) - + ( node.getDistanceToParent() >= 0.0 ? node.getDistanceToParent() : 0.0 ) ); + + ( node.getDistanceToParent() >= 0.0 ? node.getDistanceToParent() : 0.0 ) ); } if ( c.getBranchDataDirectly() != null ) { node.setBranchData( ( BranchData ) c.getBranchDataDirectly().copy() ); @@ -1064,7 +1087,7 @@ public class Phylogeny { */ public void setRooted( final boolean b ) { _rooted = b; - } // setRooted( boolean ) + } public void setSequenceRelationQueries( final Collection sequencesByName ) { _sequenceRelationQueries = sequencesByName; @@ -1165,7 +1188,7 @@ public class Phylogeny { * @return List node with the same taxonomy identifier */ private List getNodeByTaxonomyID( final String taxonomyID, final List nodes ) { - final List retour = new ArrayList(); + final List retour = new ArrayList<>(); for( final PhylogenyNode node : nodes ) { if ( taxonomyID.equals( PhylogenyMethods.getTaxonomyIdentifier( node ) ) ) { retour.add( node ); @@ -1183,7 +1206,7 @@ public class Phylogeny { * @return species contains in all leaf under the param node */ private List getSubNodeTaxonomy( final PhylogenyNode node ) { - final List taxonomyList = new ArrayList(); + final List taxonomyList = new ArrayList<>(); final List childs = node.getAllExternalDescendants(); String speciesId = null; for( final PhylogenyNode phylogenyNode : childs ) {