X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=forester%2Fjava%2Fsrc%2Forg%2Fforester%2Fphylogeny%2FPhylogenyNode.java;h=9e71765641aed27dc87691bc93bf0c7dec7c9113;hb=f507bf348ffed906d04bc76a614d6778d4cb5d64;hp=d8dd2988c0d7490a089e53efdc8da6a5fbe30774;hpb=e9760b195d1eedcf55ddc4c9bbe0f8a15fc18d6a;p=jalview.git diff --git a/forester/java/src/org/forester/phylogeny/PhylogenyNode.java b/forester/java/src/org/forester/phylogeny/PhylogenyNode.java index d8dd298..9e71765 100644 --- a/forester/java/src/org/forester/phylogeny/PhylogenyNode.java +++ b/forester/java/src/org/forester/phylogeny/PhylogenyNode.java @@ -23,7 +23,7 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA // // Contact: phylosoft @ gmail . com -// WWW: www.phylosoft.org/forester +// WWW: https://sites.google.com/site/cmzmasek/home/software/forester package org.forester.phylogeny; @@ -32,74 +32,115 @@ import java.util.List; import org.forester.io.parsers.nhx.NHXFormatException; import org.forester.io.parsers.nhx.NHXParser; +import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException; import org.forester.io.parsers.phyloxml.PhyloXmlUtil; import org.forester.phylogeny.data.BranchData; import org.forester.phylogeny.data.Confidence; import org.forester.phylogeny.data.NodeData; import org.forester.phylogeny.data.PhylogenyDataUtil; -import org.forester.phylogeny.iterators.ChildNodeIteratorForward; -import org.forester.phylogeny.iterators.PhylogenyNodeIterator; import org.forester.phylogeny.iterators.PreorderTreeIterator; import org.forester.util.ForesterUtil; -public final class PhylogenyNode implements PhylogenyNodeI, Comparable { +/** + * Warning. Implementation of method 'compareTo' only looks at + * node name. Thus, use of this class in SortedSets might lead + * to unexpected behavior. + * + */ +public final class PhylogenyNode implements Comparable { - private static int _node_count = 0; - private byte _indicator; - private int _id; - private int _sum_ext_nodes; - private float _x; - private float _y; - private double _distance_parent = PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT; + private static long NODE_COUNT = 0; + private BranchData _branch_data; private boolean _collapse; - private PhylogenyNode _parent; - private PhylogenyNode _link; private ArrayList _descendants; + private double _distance_parent = PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT; + private long _id; + private byte _indicator; + private PhylogenyNode _link; private NodeData _node_data; - private BranchData _branch_data; + private PhylogenyNode _parent; + private int _sum_ext_nodes; + private float _x; private float _x_secondary; + private float _y; private float _y_secondary; /** * Default constructor for PhylogenyNode. */ public PhylogenyNode() { - // init(); setId( PhylogenyNode.getNodeCount() ); PhylogenyNode.increaseNodeCount(); setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!) } - public void removeConnections() { - _parent = null; - _link = null; - _descendants = null; + public PhylogenyNode( final String node_name ) { + setId( PhylogenyNode.getNodeCount() ); + PhylogenyNode.increaseNodeCount(); + setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!) + if ( node_name != null ) { + getNodeData().setNodeName( node_name ); + } + } + + private PhylogenyNode( final String nhx, + final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction, + final boolean replace_underscores ) throws NHXFormatException, PhyloXmlDataFormatException { + NHXParser.parseNHX( nhx, this, taxonomy_extraction, replace_underscores, false, false, false ); + setId( PhylogenyNode.getNodeCount() ); + PhylogenyNode.increaseNodeCount(); + setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!). + } + + private PhylogenyNode( final String nhx, + final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction, + final boolean replace_underscores, + final boolean parse_extended_tags ) throws NHXFormatException, PhyloXmlDataFormatException { + NHXParser.parseNHX( nhx, + this, + taxonomy_extraction, + replace_underscores, + false, + false, + parse_extended_tags ); + setId( PhylogenyNode.getNodeCount() ); + PhylogenyNode.increaseNodeCount(); + setSumExtNodes( 1 ); // For ext node, this number is 1 (not 0!!). } /** * Adds PhylogenyNode n to the list of child nodes and sets the _parent of n * to this. - * + * * @param n * the PhylogenyNode to add */ - @Override - final public void addAsChild( final PhylogenyNodeI node ) { - final PhylogenyNode n = ( PhylogenyNode ) node; + final public void addAsChild( final PhylogenyNode node ) { + final PhylogenyNode n = node; addChildNode( n ); n.setParent( this ); } - /** - * Adds PhylogenyNode n to the list of child nodes. But does NOT set the - * _parent of n to this. - * - * @see addAsChild( PhylogenyNode n ) - * @param n - * the PhylogenyNode to add - */ - final private void addChildNode( final PhylogenyNode child ) { - getDescendants().add( child ); + public final int calculateDepth() { + PhylogenyNode n = this; + int steps = 0; + while ( n._parent != null ) { + steps++; + n = n._parent; + } + return steps; + } + + public final double calculateDistanceToRoot() { + PhylogenyNode n = this; + double d = 0.0; + while ( n._parent != null ) { + if ( n._distance_parent > 0.0 ) { + d += n._distance_parent; + } + n = n._parent; + } + return d; } @Override @@ -112,15 +153,12 @@ public final class PhylogenyNode implements PhylogenyNodeI, Comparable getAllDescendants() { + return _descendants; + } + /** * Returns a List containing references to all external children of this * PhylogenyNode. - * + * * @return List of references to external Nodes */ final public List getAllExternalDescendants() { @@ -244,7 +283,7 @@ public final class PhylogenyNode implements PhylogenyNodeI, Comparable getAllExternalDescendantsNames() { @@ -263,20 +302,15 @@ public final class PhylogenyNode implements PhylogenyNodeI, Comparable - * + * * @return the child node index of this node * @throws UnsupportedOperationException * if this node is a root node @@ -323,7 +357,7 @@ public final class PhylogenyNode implements PhylogenyNodeI, Comparable * [last modified Aug 14, 2006 by CMZ] - * + * * @return the child node index of this node * @throws UnsupportedOperationException * if this node is a root node @@ -351,7 +385,6 @@ public final class PhylogenyNode implements PhylogenyNodeI, Comparable * [last modified May 18, 2005 by CMZ] - * + * * @return the first child node of this node */ public final PhylogenyNode getFirstChildNode() { @@ -368,6 +401,13 @@ public final class PhylogenyNode implements PhylogenyNodeI, Comparable * [last modified May 18, 2005 by CMZ] - * + * * @return the last child node of this node */ public final PhylogenyNode getLastChildNode() { @@ -394,6 +434,10 @@ public final class PhylogenyNode implements PhylogenyNodeI, Comparable(); - // _parent = null; //TODO not needed? - // _id = 0; //TODO not needed? - //initializeData(); //TODO not needed? - //} - /** - * Deletes data of this PhylogenyNode. Links to the other Nodes in the - * Phylogeny, the ID and the sum of external nodes are NOT deleted. Field - * "_link" (_link to Nodes in other Phylogeny) IS deleted. - * - * @see #getLink() (Last modified: 12/20/03) - */ - // final private void initializeData() { - // _indicator = 0; - // _x = 0; - // _y = 0; - // //_node_name = ""; - // _distance_parent = PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT; - // _collapse = false; - // _link = null; - // _branch_data = null; - // _node_data = null; - // } /** * Returns whether this PhylogenyNode should be drawn as collapsed. + * Root can not be collapsed. */ final public boolean isCollapse() { - return _collapse; + return _collapse && _parent != null; } /** @@ -629,9 +631,13 @@ public final class PhylogenyNode implements PhylogenyNodeI, Comparable * [last modified June 01, 2005 by CMZ] - * + * * @return true if this node is the last child node of its _parent, false * otherwise */ @@ -710,11 +706,6 @@ public final class PhylogenyNode implements PhylogenyNodeI, Comparable 0 ) { - data = ForesterUtil.replaceIllegalNhCharacters( data ); - if ( simple_nh && ( data.length() > 10 ) ) { - data = data.substring( 0, 11 ); - } - if ( ForesterUtil.isContainsParanthesesableNhCharacter( data ) ) { - sb.append( '\'' ); - sb.append( data ); - sb.append( '\'' ); + else if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getSymbol() ) ) { + data = getNodeData().getSequence().getSymbol(); } - else { - sb.append( data ); + else if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getGeneName() ) ) { + data = getNodeData().getSequence().getGeneName(); } } + final StringBuilder sb = ForesterUtil.santitizeStringForNH( data ); if ( write_distance_to_parent && ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) ) { sb.append( ":" ); sb.append( getDistanceToParent() ); @@ -996,47 +954,22 @@ public final class PhylogenyNode implements PhylogenyNodeI, Comparable