X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fphylogeny%2FPhylogeny.java;h=86aae064d89cc3201d8275d7fb98706112d88c5f;hb=d605114bdf420c6cb680b02bb10ea25f09db769c;hp=09901711297537b04db31c3f7f8d3b9e7176cd80;hpb=4a71774e563925de62c37378a6c32c3275f2e287;p=jalview.git diff --git a/forester/java/src/org/forester/phylogeny/Phylogeny.java b/forester/java/src/org/forester/phylogeny/Phylogeny.java index 0990171..86aae06 100644 --- a/forester/java/src/org/forester/phylogeny/Phylogeny.java +++ b/forester/java/src/org/forester/phylogeny/Phylogeny.java @@ -37,6 +37,8 @@ import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Vector; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.forester.io.parsers.nhx.NHXParser; import org.forester.io.writers.PhylogenyWriter; @@ -116,8 +118,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 +140,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 +262,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 ) ); } } @@ -333,28 +339,6 @@ public class Phylogeny { } /** - * Returns the number of duplications of this Phylogeny (int). A return - * value of -1 indicates that the number of duplications is unknown. - */ - // public int getNumberOfDuplications() { - // return _number_of_duplications; - // } // getNumberOfDuplications() - /** - * Sets the number of duplications of this Phylogeny (int). A value of -1 - * indicates that the number of duplications is unknown. - * - * @param clean_nh - * set to true for clean NH format - */ - // public void setNumberOfDuplications( int i ) { - // if ( i < 0 ) { - // _number_of_duplications = -1; - // } - // else { - // _number_of_duplications = i; - // } - // } // setNumberOfDuplications( int ) - /** * Returns the first external PhylogenyNode. */ public PhylogenyNode getFirstExternalNode() { @@ -374,7 +358,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; } @@ -467,6 +451,23 @@ public class Phylogeny { return nodes; } + public List getNodes( final Pattern p ) { + if ( isEmpty() ) { + return null; + } + final List nodes = new ArrayList(); + for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) { + final PhylogenyNode n = iter.next(); + if ( n.getName() != null ) { + final Matcher m = p.matcher( n.getName() ); + if ( m.find() ) { + nodes.add( n ); + } + } + } + return nodes; + } + public List getNodesViaSequenceName( final String seq_name ) { if ( isEmpty() ) { return null; @@ -655,12 +656,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() ) ); } } } @@ -669,7 +670,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; } @@ -724,6 +725,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). * @@ -993,7 +1015,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() ); @@ -1085,7 +1107,7 @@ public class Phylogeny { */ public void setRooted( final boolean b ) { _rooted = b; - } // setRooted( boolean ) + } public void setSequenceRelationQueries( final Collection sequencesByName ) { _sequenceRelationQueries = sequencesByName;