( c.size() );
for( final PhylogenyNode phylogenyNode : c ) {
n.add( phylogenyNode.getName() );
}
return n;
}
final public BranchData getBranchData() {
if ( _branch_data == null ) {
_branch_data = new BranchData();
}
return _branch_data;
}
/**
* This return child node n of this node.
*
* @param n
* the index of the child to get
* @return the child node with index n
* @throws IllegalArgumentException
* if n is out of bounds
*/
final public PhylogenyNode getChildNode( final int i ) {
if ( isExternal() ) {
throw new UnsupportedOperationException( "attempt to get the child node of an external node." );
}
if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
+ getNumberOfDescendants() + " child nodes" );
}
return getDescendants().get( i );
}
/**
* Convenience method. Returns the first child PhylogenyNode of this
* PhylogenyNode.
*/
final public PhylogenyNode getChildNode1() {
return getChildNode( 0 );
}
/**
* Convenience method. Returns the second child PhylogenyNode of this
* PhylogenyNode.
*
* [last modified May 18, 2005 by CMZ]
*/
final public PhylogenyNode getChildNode2() {
return getChildNode( 1 );
}
/**
* This gets the child node index of this node.
*
*
* @return the child node index of this node
* @throws UnsupportedOperationException
* if this node is a root node
*/
final public int getChildNodeIndex() {
return getChildNodeIndex( getParent() );
}
/**
* This gets the child node index of this node, given that parent is its
* parent
*
* [last modified Aug 14, 2006 by CMZ]
*
* @return the child node index of this node
* @throws UnsupportedOperationException
* if this node is a root node
*/
final public int getChildNodeIndex( final PhylogenyNode parent ) {
if ( isRoot() ) {
throw new UnsupportedOperationException( "Cannot get the child index for a root node." );
}
for( int i = 0; i < parent.getNumberOfDescendants(); ++i ) {
if ( parent.getChildNode( i ) == this ) {
return i;
}
}
throw new RuntimeException( "Unexpected exception: Could not determine the child index for node: " + this );
}
final public List getDescendants() {
if ( _descendants == null ) {
_descendants = new ArrayList();
}
return _descendants;
}
/**
* Returns the length of the branch leading to the _parent of this
* PhylogenyNode (double).
*/
final public double getDistanceToParent() {
return _distance_parent;
}
/**
* Convenience method. Returns the first child node of this node.
*
* [last modified May 18, 2005 by CMZ]
*
* @return the first child node of this node
*/
public final PhylogenyNode getFirstChildNode() {
return getChildNode( 0 );
}
/**
* Returns the ID (int) of this PhylogenyNode.
*/
final public long getId() {
return _id;
}
/**
* Returns the _indicator value of this PhylogenyNode.
*/
public final byte getIndicator() {
return _indicator;
}
/**
* Convenience method. Returns the last child node of this node.
*
* [last modified May 18, 2005 by CMZ]
*
* @return the last child node of this node
*/
public final PhylogenyNode getLastChildNode() {
return getChildNode( getNumberOfDescendants() - 1 );
}
/**
* Returns a refernce to the linked PhylogenyNode of this PhylogenyNode.
* Currently, this method is only used for the speciation-_duplication
* assignment algorithms.
*/
public final PhylogenyNode getLink() {
return _link;
}
final public String getName() {
return getNodeData().getNodeName();
}
/**
* Returns a refernce to the next external PhylogenyNode of this
* PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
* external node is available.
*/
public final PhylogenyNode getNextExternalNode() {
if ( isInternal() ) {
throw new UnsupportedOperationException( "attempt to get next external node of an internal node" );
}
else if ( isLastExternalNode() ) {
return null;
}
int index = getChildNodeIndex();
PhylogenyNode previous_node = this;
PhylogenyNode current_node = getParent();
while ( !current_node.isRoot()
&& ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isLastChildNode() ) ) {
index = current_node.getChildNodeIndex();
previous_node = current_node;
current_node = current_node.getParent();
}
current_node = current_node.getChildNode( index + 1 );
while ( current_node.isInternal() ) {
current_node = current_node.getFirstChildNode();
}
return current_node;
}
public final PhylogenyNode getNextExternalNodeWhileTakingIntoAccountCollapsedNodes() {
//TODO work on me ~~
if ( isInternal() && !isCollapse() ) {
throw new UnsupportedOperationException( "attempt to get next external node of an uncollapsed internal node" );
}
if ( isRoot() ) {
return null;
}
if ( getParent().isCollapse() ) {
throw new UnsupportedOperationException( "attempt to get next external node of node with a collapsed parent" );
}
// This checks if last node.
PhylogenyNode n = this;
boolean last = true;
while ( !n.isRoot() ) {
if ( !n.isLastChildNode() ) {
last = false;
break;
}
n = n.getParent();
}
if ( last ) {
return null;
}
int index = getChildNodeIndex();
PhylogenyNode previous_node = this;
PhylogenyNode current_node = getParent();
while ( !current_node.isRoot()
&& ( current_node.isCollapse() || ( current_node.getNumberOfDescendants() == 1 ) || previous_node
.isLastChildNode() ) ) {
index = current_node.getChildNodeIndex();
previous_node = current_node;
current_node = current_node.getParent();
}
if ( index < ( current_node.getNumberOfDescendants() - 1 ) ) {
current_node = current_node.getChildNode( index + 1 );
}
while ( current_node.isInternal() && !current_node.isCollapse() ) {
current_node = current_node.getFirstChildNode();
}
return current_node;
}
public final NodeData getNodeData() {
if ( _node_data == null ) {
_node_data = new NodeData();
}
return _node_data;
}
final public int getNumberOfDescendants() {
if ( _descendants == null ) {
return 0;
}
return _descendants.size();
}
/**
* Returns the total number of external Nodes originating from this
* PhylogenyNode (int).
*/
final public int getNumberOfExternalNodes() {
return _sum_ext_nodes;
}
final public int getNumberOfParents() {
return 1;
}
/**
* Returns a refernce to the parent PhylogenyNode of this PhylogenyNode.
*/
final public PhylogenyNode getParent() {
return _parent;
}
/**
* Returns a refernce to the next external PhylogenyNode of this
* PhylogenyNode. TODO should be in Phylogeny. Returns null if no next
* external node is available.
*/
final public PhylogenyNode getPreviousExternalNode() {
if ( isInternal() ) {
throw new UnsupportedOperationException( "Cannot get the previous external node for an internal node." );
}
else if ( isRoot() /* TODO && tree is rooted */) {
throw new UnsupportedOperationException( "Cannot get the previous external node for a root node." );
}
else if ( isFirstExternalNode() ) {
throw new UnsupportedOperationException( "Attempt to get previous external node of the first external node." );
}
int index = getChildNodeIndex();
PhylogenyNode previous_node = this;
PhylogenyNode current_node = getParent();
while ( !current_node.isRoot()
&& ( ( current_node.getNumberOfDescendants() == 1 ) || previous_node.isFirstChildNode() ) ) {
index = current_node.getChildNodeIndex();
previous_node = current_node;
current_node = current_node.getParent();
}
current_node = current_node.getChildNode( index - 1 );
while ( current_node.isInternal() ) {
current_node = current_node.getLastChildNode();
}
return current_node;
}
/**
* Used for drawing of Trees.
*/
final public float getXcoord() {
return _x;
}
final public float getXSecondary() {
return _x_secondary;
}
/**
* Used for drawing of Trees.
*/
final public float getYcoord() {
return _y;
}
final public float getYSecondary() {
return _y_secondary;
}
@Override
final public int hashCode() {
final NodeData data = getNodeData();
if ( ( getName().length() < 1 ) && !data.isHasSequence() && !data.isHasTaxonomy() ) {
return super.hashCode();
}
int result = getName().hashCode();
if ( data.isHasSequence() ) {
result ^= data.getSequence().hashCode();
}
if ( data.isHasTaxonomy() ) {
result ^= data.getTaxonomy().hashCode();
}
return result;
}
/**
* Returns whether this PhylogenyNode should be drawn as collapsed.
*/
final public boolean isCollapse() {
return _collapse;
}
/**
* Returns true if this PhylogenyNode represents a _duplication event, false
* otherwise.
*/
final public boolean isDuplication() {
return getNodeData().isHasEvent() && getNodeData().getEvent().isDuplication();
}
public boolean isEmpty() {
return ( ( _node_data == null ) || _node_data.isEmpty() );
}
/**
* Checks whether this PhylogenyNode is external (tip).
*
* @return true if this PhylogenyNode is external, false otherwise
*/
final public boolean isExternal() {
if ( _descendants == null ) {
return true;
}
return ( getNumberOfDescendants() < 1 );
}
final public boolean isFirstChildNode() {
if ( isRoot() /* and tree is rooted TODO */) {
throw new UnsupportedOperationException( "Cannot determine whether the root is the first child node of its _parent." );
}
return ( getChildNodeIndex() == 0 );
}
final public boolean isFirstExternalNode() {
if ( isInternal() ) {
return false;
}
PhylogenyNode node = this;
while ( !node.isRoot() ) {
if ( !node.isFirstChildNode() ) {
return false;
}
node = node.getParent();
}
return true;
}
/**
* Returns whether a _duplication or speciation event has been assigned for
* this PhylogenyNode.
*/
final public boolean isHasAssignedEvent() {
if ( !getNodeData().isHasEvent() ) {
return false;
}
if ( ( getNodeData().getEvent() ).isUnassigned() ) {
return false;
}
return true;
}
/**
* Checks whether this PhylogenyNode is internal (tip).
*
* @return true if this PhylogenyNode is external, false otherwise
*/
final public boolean isInternal() {
return ( !isExternal() );
}
/**
* Returns true if this node is the last child node of its _parent.
*
* [last modified June 01, 2005 by CMZ]
*
* @return true if this node is the last child node of its _parent, false
* otherwise
*/
final public boolean isLastChildNode() {
if ( isRoot() /* and tree is rooted TODO */) {
throw new UnsupportedOperationException( "Cannot determine whether the root is the last child node of its _parent." );
}
return ( getChildNodeIndex() == ( getParent().getNumberOfDescendants() - 1 ) );
}
final public boolean isLastExternalNode() {
if ( isInternal() ) {
return false;
}
PhylogenyNode node = this;
while ( !node.isRoot() ) {
if ( !node.isLastChildNode() ) {
return false;
}
node = node.getParent();
}
return true;
}
/**
* Checks whether this PhylogenyNode is a root.
*
* @return true if this PhylogenyNode is the root, false otherwise
*/
final public boolean isRoot() {
return _parent == null;
}
final public boolean isSpeciation() {
return getNodeData().isHasEvent() && getNodeData().getEvent().isSpeciation();
}
// ---------------------------------------------------------
// Basic printing
// ---------------------------------------------------------
/**
* Prints to the console the subtree originating from this PhylogenyNode in
* preorder.
*/
public void preorderPrint() {
System.out.println( this + "\n" );
if ( isInternal() ) {
for( int i = 0; i < getNumberOfDescendants(); ++i ) {
getChildNode( i ).preorderPrint();
}
}
}
final public void removeChildNode( final int i ) {
if ( isExternal() ) {
throw new UnsupportedOperationException( "cannot get the child node for a external node." );
}
if ( ( i >= getNumberOfDescendants() ) || ( i < 0 ) ) {
throw new IllegalArgumentException( "attempt to get child node " + i + " of a node with "
+ getNumberOfDescendants() + " child nodes." );
}
getDescendants().remove( i );
}
final public void removeChildNode( final PhylogenyNode remove_me ) {
removeChildNode( remove_me.getChildNodeIndex() );
}
public void removeConnections() {
_parent = null;
_link = null;
_descendants = null;
}
final public void setBranchData( final BranchData branch_data ) {
_branch_data = branch_data;
}
/**
* Sets the first child PhylogenyNode of this PhylogenyNode to n.
*/
final public void setChild1( final PhylogenyNode n ) {
setChildNode( 0, n );
}
/**
* Sets the second child PhylogenyNode of this PhylogenyNode to n.
*/
final public void setChild2( final PhylogenyNode n ) {
setChildNode( 1, n );
}
/**
* Inserts PhylogenyNode n at the specified position i into the list of
* child nodes. This does not allow null slots in the list of child nodes:
* If i is larger than the number of child nodes, n is just added to the
* list, not place at index i.
*
* @param i
* the index of position where to add the child
* @param n
* the PhylogenyNode to add
*/
final public void setChildNode( final int i, final PhylogenyNode node ) {
node.setParent( this );
if ( getNumberOfDescendants() <= i ) {
addChildNode( node );
}
else {
getDescendants().set( i, node );
}
}
/**
* Sets whether this PhylogenyNode should be drawn as collapsed.
*/
final public void setCollapse( final boolean b ) {
_collapse = b;
}
/**
* Sets the length of the branch leading to the _parent of this
* PhylogenyNode to double d.
*/
final public void setDistanceToParent( final double d ) {
_distance_parent = d;
}
/**
* Sets the _indicator value of this PhylogenyNode to i.
*/
final public void setIndicator( final byte i ) {
_indicator = i;
}
/**
* Sets the linked PhylogenyNode of this PhylogenyNode to n. Currently, this
* method is only used for the speciation-_duplication assignment
* algorithms.
*/
final public void setLink( final PhylogenyNode n ) {
_link = n;
}
/**
* Sets the name of this node.
*/
final public void setName( final String node_name ) {
getNodeData().setNodeName( node_name );
}
/**
* Sets the _parent PhylogenyNode of this PhylogenyNode to n.
*/
final public void setParent( final PhylogenyNode n ) {
_parent = n;
}
/**
* Sets the total number of external Nodes originating from this
* PhylogenyNode to i (int).
*/
final public void setSumExtNodes( final int i ) {
if ( i < 0 ) {
throw new IllegalArgumentException( "attempt to set sum of external nodes to less than one" );
}
_sum_ext_nodes = i;
}
/**
* Used for drawing of Trees.
*/
final public void setXcoord( final float x ) {
_x = x;
}
final public void setXSecondary( final float x_secondary ) {
_x_secondary = x_secondary;
}
// -----------
/**
* Used for drawing of Trees.
*/
final public void setYcoord( final float y ) {
_y = y;
}
final public void setYSecondary( final float y_secondary ) {
_y_secondary = y_secondary;
}
/**
* Swaps the the two childern of a PhylogenyNode node of this Phylogeny.
*/
public final void swapChildren() throws RuntimeException {
if ( isExternal() ) {
throw new RuntimeException( "attempt to swap descendants of external node" );
}
if ( getNumberOfDescendants() != 2 ) {
throw new RuntimeException( "attempt to swap descendants of node with " + getNumberOfDescendants()
+ " descendants" );
}
final PhylogenyNode a = getChildNode( 0 );
final PhylogenyNode b = getChildNode( 1 );
setChildNode( 0, b );
setChildNode( 1, a );
}
// ---------------------------------------------------------
// Writing of Nodes to Strings
// ---------------------------------------------------------
final public String toNewHampshire( final boolean write_distance_to_parent,
final NH_CONVERSION_SUPPORT_VALUE_STYLE svs ) {
String data = "";
if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.AS_INTERNAL_NODE_NAMES ) && !isExternal() ) {
if ( getBranchData().isHasConfidences()
&& ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
data = Confidence.FORMATTER.format( ForesterUtil
.round( getBranchData().getConfidence( 0 ).getValue(),
PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) );
}
}
else if ( !ForesterUtil.isEmpty( getName() ) ) {
data = getName();
}
else if ( getNodeData().isHasTaxonomy() ) {
if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
data = getNodeData().getTaxonomy().getTaxonomyCode();
}
else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
data = getNodeData().getTaxonomy().getScientificName();
}
else if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getCommonName() ) ) {
data = getNodeData().getTaxonomy().getCommonName();
}
}
else if ( getNodeData().isHasSequence() ) {
if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
data = getNodeData().getSequence().getName();
}
else if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getSymbol() ) ) {
data = getNodeData().getSequence().getSymbol();
}
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() );
}
if ( ( svs == NH_CONVERSION_SUPPORT_VALUE_STYLE.IN_SQUARE_BRACKETS ) && !isExternal()
&& getBranchData().isHasConfidences()
&& ( getBranchData().getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
sb.append( "[" );
sb.append( Confidence.FORMATTER.format( ForesterUtil
.round( getBranchData().getConfidence( 0 ).getValue(),
PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ) );
sb.append( "]" );
}
return sb.toString();
}
/**
* Converts this PhylogenyNode to a New Hampshire X (NHX) String
* representation.
*/
final public String toNewHampshireX() {
final StringBuilder sb = new StringBuilder();
final StringBuffer s_nhx = new StringBuffer();
if ( !ForesterUtil.isEmpty( getName() ) ) {
sb.append( ForesterUtil.santitizeStringForNH( getName() ) );
}
if ( getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
sb.append( ":" );
sb.append( getDistanceToParent() );
}
if ( getNodeDataDirectly() != null ) {
s_nhx.append( getNodeDataDirectly().toNHX() );
}
if ( getBranchDataDirectly() != null ) {
s_nhx.append( getBranchDataDirectly().toNHX() );
}
if ( s_nhx.length() > 0 ) {
sb.append( "[&&NHX" );
sb.append( s_nhx );
sb.append( "]" );
}
return sb.toString();
}
@Override
final public String toString() {
final StringBuilder sb = new StringBuilder();
if ( !ForesterUtil.isEmpty( getName() ) ) {
sb.append( getName() );
sb.append( " " );
}
if ( getNodeData().isHasTaxonomy() ) {
if ( !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getScientificName() ) ) {
sb.append( getNodeData().getTaxonomy().getScientificName() );
sb.append( " " );
}
else if ( ( sb.length() <= 1 ) && !ForesterUtil.isEmpty( getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
sb.append( getNodeData().getTaxonomy().getTaxonomyCode() );
sb.append( " " );
}
else if ( getNodeData().getTaxonomy().getIdentifier() != null ) {
sb.append( getNodeData().getTaxonomy().getIdentifier().toString() );
sb.append( " " );
}
}
if ( getNodeData().isHasSequence() ) {
if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getName() ) ) {
sb.append( getNodeData().getSequence().getName() );
sb.append( " " );
}
if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getSymbol() ) ) {
sb.append( getNodeData().getSequence().getSymbol() );
sb.append( " " );
}
if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getGeneName() ) ) {
sb.append( getNodeData().getSequence().getGeneName() );
sb.append( " " );
}
if ( getNodeData().getSequence().getAccession() != null ) {
sb.append( getNodeData().getSequence().getAccession().toString() );
sb.append( " " );
}
if ( !ForesterUtil.isEmpty( getNodeData().getSequence().getMolecularSequence() ) ) {
sb.append( getNodeData().getSequence().getMolecularSequence() );
sb.append( " " );
}
}
if ( sb.length() <= 1 ) {
sb.append( "[" );
sb.append( getId() );
sb.append( "]" );
}
return sb.toString().trim();
}
/**
* Sets the Id of this PhylogenyNode to i. In most cases, this number
* should not be set to values lower than getNodeCount() -- which this method
* does not allow.
*/
synchronized final protected void setId( final long i ) {
if ( i < getNodeCount() ) {
throw new IllegalArgumentException( "attempt to set node id to a value less than total node count (thus violating the uniqueness of node ids)" );
}
_id = i;
}
final BranchData getBranchDataDirectly() {
return _branch_data;
}
final NodeData getNodeDataDirectly() {
return _node_data;
}
final void setChildNodeOnly( final int i, final PhylogenyNode node ) {
if ( getNumberOfDescendants() <= i ) {
addChildNode( node );
}
else {
getDescendants().set( i, node );
}
}
/**
* Sets the indicators of all the children of this PhylogenyNode to zero.
*/
final void setIndicatorsToZero() {
for( final PreorderTreeIterator it = new PreorderTreeIterator( this ); it.hasNext(); ) {
it.next().setIndicator( ( byte ) 0 );
}
}
/**
* 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 static PhylogenyNode createInstanceFromNhxString( final String nhx ) throws NHXFormatException,
PhyloXmlDataFormatException {
return new PhylogenyNode( nhx, NHXParser.TAXONOMY_EXTRACTION.NO, false );
}
public static PhylogenyNode createInstanceFromNhxString( final String nhx,
final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction )
throws NHXFormatException, PhyloXmlDataFormatException {
return new PhylogenyNode( nhx, taxonomy_extraction, false );
}
public static PhylogenyNode createInstanceFromNhxString( final String nhx,
final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction,
final boolean replace_underscores )
throws NHXFormatException, PhyloXmlDataFormatException {
return new PhylogenyNode( nhx, taxonomy_extraction, replace_underscores );
}
/**
* Returns the total number of all Nodes created so far.
*
* @return total number of Nodes (long)
*/
synchronized final public static long getNodeCount() {
return NODE_COUNT;
}
/**
* Decreases the total number of all Nodes created so far by one.
*/
final static synchronized void decreaseNodeCount() {
--NODE_COUNT;
}
/**
* Sets the total number of all Nodes created so far to i.
*/
synchronized final static void setNodeCount( final long i ) {
PhylogenyNode.NODE_COUNT = i;
}
/**
* Increases the total number of all Nodes created so far by one.
*/
synchronized final private static void increaseNodeCount() {
++NODE_COUNT;
}
public enum NH_CONVERSION_SUPPORT_VALUE_STYLE {
AS_INTERNAL_NODE_NAMES, IN_SQUARE_BRACKETS, NONE;
}
}
org/forester/phylogeny/iterators/ 0000775 0000000 0000000 00000000000 14125307352 016232 5 ustar root root org/forester/phylogeny/iterators/PostorderTreeIterator.java 0000664 0000000 0000000 00000007223 14125307352 023414 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.iterators;
import java.util.NoSuchElementException;
import java.util.Stack;
import org.forester.phylogeny.Phylogeny;
import org.forester.phylogeny.PhylogenyNode;
/*
* *
*/
public final class PostorderTreeIterator implements PhylogenyNodeIterator {
final private Phylogeny _tree;
final private PhylogenyNode _root;
private boolean _has_next;
final private Stack _stack;
/**
* @param t
* Phylogeny for which a Iterator is to be constructed.
*/
public PostorderTreeIterator( final Phylogeny tree ) throws IllegalArgumentException {
if ( tree.isEmpty() ) {
throw new IllegalArgumentException( "Attempt to use PostorderTreeIterator on an empty phylogeny." );
}
_tree = tree;
_root = getTree().getRoot();
_stack = new Stack();
reset();
}
final private PhylogenyNode getRoot() {
return _root;
}
final private Stack getStack() {
return _stack;
}
final private Phylogeny getTree() {
return _tree;
}
@Override
final public boolean hasNext() {
return _has_next;
}
/**
* Advances the Iterator by one.
*/
@Override
final public PhylogenyNode next() throws NoSuchElementException {
if ( !hasNext() ) {
throw new NoSuchElementException( "Attempt to call \"next()\" on iterator which has no more next elements." );
}
while ( true ) {
final PostOrderStackObject si = getStack().pop();
final PhylogenyNode node = si.getNode();
final int phase = si.getPhase();
if ( phase > node.getNumberOfDescendants() ) {
setHasNext( node != getRoot() );
return node;
}
else {
getStack().push( new PostOrderStackObject( node, ( phase + 1 ) ) );
if ( node.isInternal() ) {
getStack().push( new PostOrderStackObject( node.getChildNode( phase - 1 ), 1 ) );
}
}
}
}
@Override
final public void remove() {
throw new UnsupportedOperationException();
}
@Override
final public void reset() {
setHasNext( true );
getStack().clear();
getStack().push( new PostOrderStackObject( getTree().getRoot(), 1 ) );
}
final private void setHasNext( final boolean has_next ) {
_has_next = has_next;
}
}
org/forester/phylogeny/iterators/LevelOrderTreeIterator.java 0000664 0000000 0000000 00000010642 14125307352 023475 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.iterators;
import java.util.NoSuchElementException;
import org.forester.datastructures.Queue;
import org.forester.phylogeny.Phylogeny;
import org.forester.phylogeny.PhylogenyNode;
/*
* An iterator to iterate a Phylogeny in level order.
*
* Created: 10/23/2005 by Christian M. Zmasek. Last modified: 10/23/2005 by
* Christian M. Zmasek.
*
* @author Christian M. Zmasek
*
* @version 1.000
*/
public class LevelOrderTreeIterator implements PhylogenyNodeIterator {
// Instance variables
// ------------------
private final Queue _queue;
private final PhylogenyNode _root;
// Constructors
// ------------
/**
* Creates a new LevelOrderTreeIterator for iterating over all the nodes of
* Phylogeny phylogeny
*
* @param phylogeny
* the Phylogeny to iterate over
* @throws IllegalArgumentException
* if phylogeny is empty
*/
public LevelOrderTreeIterator( final Phylogeny phylogeny ) throws IllegalArgumentException {
this( phylogeny.getRoot() );
if ( phylogeny.isEmpty() ) {
throw new IllegalArgumentException( "Attempt to use LevelOrderTreeIterator on an empty phylogeny." );
}
}
/**
* Creates a new LevelOrderTreeIterator for iterating over all the child
* nodes of PhylogenyNode node (including node itself).
*
* @param node
* the parent of the nodes to iterate over
*/
public LevelOrderTreeIterator( final PhylogenyNode node ) {
_queue = new Queue();
_root = node;
reset();
}
// Private methods
// ---------------
/**
* Returns the queue upon which this iterator is based.
*
*/
private Queue getQueue() {
return _queue;
}
/**
* Returns the root of the phylogeny this iterators parses over.
*
* @return the root of the phylogeny this iterators parses over.
*/
private PhylogenyNode getRoot() {
return _root;
}
// Public methods
// --------------
/**
* Returns true is this iterator has at least one more element, false
* otherwise.
*
* @return true is this iterator has at least one more element, false
* otherwise
*/
@Override
public boolean hasNext() {
return !getQueue().isEmpty();
}
/**
* Returns the next PhylogenyNode.
*
* @return the next PhylogenyNode
* @throws NoSuchElementException
* if iteration is complete
*/
@Override
public PhylogenyNode next() throws NoSuchElementException {
if ( !hasNext() ) {
throw new NoSuchElementException( "Attempt to call \"next()\" on iterator which has no more next elements." );
}
final PhylogenyNode node = ( PhylogenyNode ) getQueue().dequeue();
for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
getQueue().enqueue( node.getChildNode( i ) );
}
return node;
}
/**
* Not supported.
*
*/
@Override
public void remove() {
throw new UnsupportedOperationException();
}
/**
* Resets the iterator.
*/
@Override
public void reset() {
getQueue().clear();
getQueue().enqueue( getRoot() );
}
} // enod of class LevelOrderTreeIterator
org/forester/phylogeny/iterators/PhylogenyNodeIterator.java 0000664 0000000 0000000 00000003167 14125307352 023402 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.iterators;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.forester.phylogeny.PhylogenyNode;
/*
* @author Christian Zmasek
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public interface PhylogenyNodeIterator extends Iterator {
@Override
public boolean hasNext();
@Override
public PhylogenyNode next() throws NoSuchElementException;
public void reset();
}
org/forester/phylogeny/iterators/PreorderTreeIterator.java 0000664 0000000 0000000 00000006230 14125307352 023212 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.iterators;
import java.util.NoSuchElementException;
import java.util.Stack;
import org.forester.phylogeny.Phylogeny;
import org.forester.phylogeny.PhylogenyNode;
public final class PreorderTreeIterator implements PhylogenyNodeIterator {
final private Phylogeny _tree;
final private Stack _stack;
/**
* @param tree
* Phylogeny for which a Iterator is to be constructed.
*/
public PreorderTreeIterator( final Phylogeny tree ) throws IllegalArgumentException {
if ( tree.isEmpty() ) {
throw new IllegalArgumentException( "Attempt to use PreorderTreeIterator on empty tree." );
}
_stack = new Stack();
_tree = tree;
reset();
}
public PreorderTreeIterator( final PhylogenyNode node ) throws IllegalArgumentException {
_stack = new Stack();
_tree = null;
reset( node );
}
/*
* (non-Javadoc)
*
* @see java.util.Iterator#hasNext()
*/
@Override
public final boolean hasNext() {
return !_stack.isEmpty();
}
/**
* Advances the Iterator by one.
*/
@Override
public final PhylogenyNode next() throws NoSuchElementException {
if ( !hasNext() ) {
throw new NoSuchElementException( "Attempt to call \"next()\" on iterator which has no more next elements." );
}
final PhylogenyNode node = _stack.pop();
if ( !node.isExternal() ) {
for( int i = node.getNumberOfDescendants() - 1; i >= 0; --i ) {
_stack.push( node.getChildNode( i ) );
}
}
return node;
}
/**
* Not supported.
*
*/
@Override
public final void remove() {
throw new UnsupportedOperationException();
}
@Override
public final void reset() {
_stack.clear();
_stack.push( _tree.getRoot() );
}
private final void reset( final PhylogenyNode node ) {
_stack.clear();
_stack.push( node );
}
}
org/forester/phylogeny/iterators/ExternalForwardIterator.java 0000664 0000000 0000000 00000006765 14125307352 023734 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.iterators;
import java.util.NoSuchElementException;
import org.forester.phylogeny.Phylogeny;
import org.forester.phylogeny.PhylogenyNode;
public class ExternalForwardIterator implements PhylogenyNodeIterator {
private PhylogenyNode _current_node;
private final PhylogenyNode _last_ext_node;
private final PhylogenyNode _first_ext_node;
/**
* Constructor for ExternalForwardIterator.
*
* @param tree
* the tree on which to iterate over all external nodes.
*/
public ExternalForwardIterator( final Phylogeny phylogeny ) throws IllegalArgumentException {
if ( phylogeny.isEmpty() ) {
throw new IllegalArgumentException( "attempt to use ExternalForwardIterator on an empty phylogeny" );
}
PhylogenyNode n = phylogeny.getRoot();
while ( !n.isExternal() ) {
n = n.getLastChildNode();
}
_last_ext_node = n;
_first_ext_node = phylogeny.getFirstExternalNode();
reset();
}
private PhylogenyNode getCurrentNode() {
return _current_node;
}
private PhylogenyNode getFirstExtNode() {
return _first_ext_node;
}
private PhylogenyNode getLastExtNode() {
return _last_ext_node;
}
/*
* (non-Javadoc)
*
* @see java.util.Iterator#hasNext()
*/
@Override
public boolean hasNext() {
return getCurrentNode() != null;
}
/*
* (non-Javadoc)
*
* @see java.util.Iterator#next()
*/
@Override
public PhylogenyNode next() throws NoSuchElementException {
if ( !hasNext() ) {
throw new NoSuchElementException( "attempt to call \"next()\" on iterator which has no more next elements" );
}
final PhylogenyNode n = getCurrentNode();
if ( n == getLastExtNode() ) {
setCurrentNode( null );
}
else {
setCurrentNode( n.getNextExternalNode() );
}
return n;
}
/**
* Not supported.
*
*/
@Override
public void remove() {
throw new UnsupportedOperationException();
}
/**
* DOCUMENT ME!
*/
@Override
public void reset() {
setCurrentNode( getFirstExtNode() );
}
private void setCurrentNode( final PhylogenyNode current_node ) {
_current_node = current_node;
}
} // end of class ExternalForwardIterator
org/forester/phylogeny/iterators/PostOrderStackObject.java 0000664 0000000 0000000 00000003171 14125307352 023135 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.iterators;
import org.forester.phylogeny.PhylogenyNode;
/*
* @author Christian M. Zmasek
*
* @version 1.00 -- last modified: 06/15/00
*/
public final class PostOrderStackObject {
final private PhylogenyNode _node;
final private int _phase;
public PostOrderStackObject( final PhylogenyNode n, final int i ) {
_node = n;
_phase = i;
}
final public PhylogenyNode getNode() {
return _node;
}
final public int getPhase() {
return _phase;
}
}
org/forester/phylogeny/PhylogenyMethods.java 0000664 0000000 0000000 00000252240 14125307352 020370 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.forester.io.parsers.FastaParser;
import org.forester.io.parsers.PhylogenyParser;
import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
import org.forester.io.parsers.util.PhylogenyParserException;
import org.forester.msa.Msa;
import org.forester.phylogeny.data.Accession;
import org.forester.phylogeny.data.Annotation;
import org.forester.phylogeny.data.BranchColor;
import org.forester.phylogeny.data.BranchWidth;
import org.forester.phylogeny.data.Confidence;
import org.forester.phylogeny.data.DomainArchitecture;
import org.forester.phylogeny.data.Event;
import org.forester.phylogeny.data.Identifier;
import org.forester.phylogeny.data.PhylogenyDataUtil;
import org.forester.phylogeny.data.Sequence;
import org.forester.phylogeny.data.Taxonomy;
import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
import org.forester.phylogeny.factories.PhylogenyFactory;
import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
import org.forester.util.BasicDescriptiveStatistics;
import org.forester.util.DescriptiveStatistics;
import org.forester.util.ForesterUtil;
public class PhylogenyMethods {
private PhylogenyMethods() {
// Hidden constructor.
}
@Override
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
public static boolean extractFastaInformation( final Phylogeny phy ) {
boolean could_extract = false;
for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
final PhylogenyNode node = iter.next();
if ( !ForesterUtil.isEmpty( node.getName() ) ) {
final Matcher name_m = FastaParser.FASTA_DESC_LINE.matcher( node.getName() );
if ( name_m.lookingAt() ) {
could_extract = true;
final String acc_source = name_m.group( 1 );
final String acc = name_m.group( 2 );
final String seq_name = name_m.group( 3 );
final String tax_sn = name_m.group( 4 );
if ( !ForesterUtil.isEmpty( acc_source ) && !ForesterUtil.isEmpty( acc ) ) {
ForesterUtil.ensurePresenceOfSequence( node );
node.getNodeData().getSequence( 0 ).setAccession( new Accession( acc, acc_source ) );
}
if ( !ForesterUtil.isEmpty( seq_name ) ) {
ForesterUtil.ensurePresenceOfSequence( node );
node.getNodeData().getSequence( 0 ).setName( seq_name );
}
if ( !ForesterUtil.isEmpty( tax_sn ) ) {
ForesterUtil.ensurePresenceOfTaxonomy( node );
node.getNodeData().getTaxonomy( 0 ).setScientificName( tax_sn );
}
}
}
}
return could_extract;
}
public static DescriptiveStatistics calculateBranchLengthStatistics( final Phylogeny phy ) {
final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
final PhylogenyNode n = iter.next();
if ( !n.isRoot() && ( n.getDistanceToParent() >= 0.0 ) ) {
stats.addValue( n.getDistanceToParent() );
}
}
return stats;
}
public static List calculateConfidenceStatistics( final Phylogeny phy ) {
final List stats = new ArrayList();
for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
final PhylogenyNode n = iter.next();
if ( !n.isExternal() && !n.isRoot() ) {
if ( n.getBranchData().isHasConfidences() ) {
for( int i = 0; i < n.getBranchData().getConfidences().size(); ++i ) {
final Confidence c = n.getBranchData().getConfidences().get( i );
if ( ( i > ( stats.size() - 1 ) ) || ( stats.get( i ) == null ) ) {
stats.add( i, new BasicDescriptiveStatistics() );
}
if ( !ForesterUtil.isEmpty( c.getType() ) ) {
if ( !ForesterUtil.isEmpty( stats.get( i ).getDescription() ) ) {
if ( !stats.get( i ).getDescription().equalsIgnoreCase( c.getType() ) ) {
throw new IllegalArgumentException( "support values in node [" + n.toString()
+ "] appear inconsistently ordered" );
}
}
stats.get( i ).setDescription( c.getType() );
}
stats.get( i ).addValue( ( ( c != null ) && ( c.getValue() >= 0 ) ) ? c.getValue() : 0 );
}
}
}
}
return stats;
}
/**
* Calculates the distance between PhylogenyNodes node1 and node2.
*
*
* @param node1
* @param node2
* @return distance between node1 and node2
*/
public static double calculateDistance( final PhylogenyNode node1, final PhylogenyNode node2 ) {
final PhylogenyNode lca = calculateLCA( node1, node2 );
final PhylogenyNode n1 = node1;
final PhylogenyNode n2 = node2;
return ( PhylogenyMethods.getDistance( n1, lca ) + PhylogenyMethods.getDistance( n2, lca ) );
}
/**
* Returns the LCA of PhylogenyNodes node1 and node2.
*
*
* @param node1
* @param node2
* @return LCA of node1 and node2
*/
public final static PhylogenyNode calculateLCA( PhylogenyNode node1, PhylogenyNode node2 ) {
if ( node1 == null ) {
throw new IllegalArgumentException( "first argument (node) is null" );
}
if ( node2 == null ) {
throw new IllegalArgumentException( "second argument (node) is null" );
}
if ( node1 == node2 ) {
return node1;
}
if ( ( node1.getParent() == node2.getParent() ) ) {
return node1.getParent();
}
int depth1 = node1.calculateDepth();
int depth2 = node2.calculateDepth();
while ( ( depth1 > -1 ) && ( depth2 > -1 ) ) {
if ( depth1 > depth2 ) {
node1 = node1.getParent();
depth1--;
}
else if ( depth2 > depth1 ) {
node2 = node2.getParent();
depth2--;
}
else {
if ( node1 == node2 ) {
return node1;
}
node1 = node1.getParent();
node2 = node2.getParent();
depth1--;
depth2--;
}
}
throw new IllegalArgumentException( "illegal attempt to calculate LCA of two nodes which do not share a common root" );
}
/**
* Returns the LCA of PhylogenyNodes node1 and node2.
* Precondition: ids are in pre-order (or level-order).
*
*
* @param node1
* @param node2
* @return LCA of node1 and node2
*/
public final static PhylogenyNode calculateLCAonTreeWithIdsInPreOrder( PhylogenyNode node1, PhylogenyNode node2 ) {
if ( node1 == null ) {
throw new IllegalArgumentException( "first argument (node) is null" );
}
if ( node2 == null ) {
throw new IllegalArgumentException( "second argument (node) is null" );
}
while ( node1 != node2 ) {
if ( node1.getId() > node2.getId() ) {
node1 = node1.getParent();
}
else {
node2 = node2.getParent();
}
}
return node1;
}
public static short calculateMaxBranchesToLeaf( final PhylogenyNode node ) {
if ( node.isExternal() ) {
return 0;
}
short max = 0;
for( PhylogenyNode d : node.getAllExternalDescendants() ) {
short steps = 0;
while ( d != node ) {
if ( d.isCollapse() ) {
steps = 0;
}
else {
steps++;
}
d = d.getParent();
}
if ( max < steps ) {
max = steps;
}
}
return max;
}
public static int calculateMaxDepth( final Phylogeny phy ) {
int max = 0;
for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
final PhylogenyNode node = iter.next();
final int steps = node.calculateDepth();
if ( steps > max ) {
max = steps;
}
}
return max;
}
public static double calculateMaxDistanceToRoot( final Phylogeny phy ) {
double max = 0.0;
for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
final PhylogenyNode node = iter.next();
final double d = node.calculateDistanceToRoot();
if ( d > max ) {
max = d;
}
}
return max;
}
public static PhylogenyNode calculateNodeWithMaxDistanceToRoot( final Phylogeny phy ) {
double max = 0.0;
PhylogenyNode max_node = phy.getFirstExternalNode();
for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
final PhylogenyNode node = iter.next();
final double d = node.calculateDistanceToRoot();
if ( d > max ) {
max = d;
max_node = node;
}
}
return max_node;
}
public static int calculateNumberOfExternalNodesWithoutTaxonomy( final PhylogenyNode node ) {
final List descs = node.getAllExternalDescendants();
int x = 0;
for( final PhylogenyNode n : descs ) {
if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
x++;
}
}
return x;
}
public static DescriptiveStatistics calculateNumberOfDescendantsPerNodeStatistics( final Phylogeny phy ) {
final DescriptiveStatistics stats = new BasicDescriptiveStatistics();
for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
final PhylogenyNode n = iter.next();
if ( !n.isExternal() ) {
stats.addValue( n.getNumberOfDescendants() );
}
}
return stats;
}
public final static void collapseSubtreeStructure( final PhylogenyNode n ) {
final List eds = n.getAllExternalDescendants();
final List d = new ArrayList();
for( final PhylogenyNode ed : eds ) {
d.add( calculateDistanceToAncestor( n, ed ) );
}
for( int i = 0; i < eds.size(); ++i ) {
n.setChildNode( i, eds.get( i ) );
eds.get( i ).setDistanceToParent( d.get( i ) );
}
}
public static int countNumberOfOneDescendantNodes( final Phylogeny phy ) {
int count = 0;
for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
final PhylogenyNode n = iter.next();
if ( !n.isExternal() && ( n.getNumberOfDescendants() == 1 ) ) {
count++;
}
}
return count;
}
public static int countNumberOfPolytomies( final Phylogeny phy ) {
int count = 0;
for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
final PhylogenyNode n = iter.next();
if ( !n.isExternal() && ( n.getNumberOfDescendants() > 2 ) ) {
count++;
}
}
return count;
}
public static final HashMap createNameToExtNodeMap( final Phylogeny phy ) {
final HashMap nodes = new HashMap();
final List ext = phy.getExternalNodes();
for( final PhylogenyNode n : ext ) {
nodes.put( n.getName(), n );
}
return nodes;
}
public static void deleteExternalNodesNegativeSelection( final Set to_delete, final Phylogeny phy ) {
for( final Long id : to_delete ) {
phy.deleteSubtree( phy.getNode( id ), true );
}
phy.clearHashIdToNodeMap();
phy.externalNodesHaveChanged();
}
public static void deleteExternalNodesNegativeSelection( final String[] node_names_to_delete, final Phylogeny p )
throws IllegalArgumentException {
for( final String element : node_names_to_delete ) {
if ( ForesterUtil.isEmpty( element ) ) {
continue;
}
List nodes = null;
nodes = p.getNodes( element );
final Iterator it = nodes.iterator();
while ( it.hasNext() ) {
final PhylogenyNode n = it.next();
if ( !n.isExternal() ) {
throw new IllegalArgumentException( "attempt to delete non-external node \"" + element + "\"" );
}
p.deleteSubtree( n, true );
}
}
p.clearHashIdToNodeMap();
p.externalNodesHaveChanged();
}
public static List deleteExternalNodesPositiveSelection( final String[] node_names_to_keep,
final Phylogeny p ) {
final PhylogenyNodeIterator it = p.iteratorExternalForward();
final String[] to_delete = new String[ p.getNumberOfExternalNodes() ];
int i = 0;
Arrays.sort( node_names_to_keep );
while ( it.hasNext() ) {
final String curent_name = it.next().getName();
if ( Arrays.binarySearch( node_names_to_keep, curent_name ) < 0 ) {
to_delete[ i++ ] = curent_name;
}
}
PhylogenyMethods.deleteExternalNodesNegativeSelection( to_delete, p );
final List deleted = new ArrayList();
for( final String n : to_delete ) {
if ( !ForesterUtil.isEmpty( n ) ) {
deleted.add( n );
}
}
return deleted;
}
public static void deleteExternalNodesPositiveSelectionT( final List species_to_keep, final Phylogeny phy ) {
final Set to_delete = new HashSet();
for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
final PhylogenyNode n = it.next();
if ( n.getNodeData().isHasTaxonomy() ) {
if ( !species_to_keep.contains( n.getNodeData().getTaxonomy() ) ) {
to_delete.add( n.getId() );
}
}
else {
throw new IllegalArgumentException( "node " + n.getId() + " has no taxonomic data" );
}
}
deleteExternalNodesNegativeSelection( to_delete, phy );
}
final public static void deleteInternalNodesWithOnlyOneDescendent( final Phylogeny phy ) {
final ArrayList to_delete = new ArrayList();
for( final PhylogenyNodeIterator iter = phy.iteratorPostorder(); iter.hasNext(); ) {
final PhylogenyNode n = iter.next();
if ( ( !n.isExternal() ) && ( n.getNumberOfDescendants() == 1 ) ) {
to_delete.add( n );
}
}
for( final PhylogenyNode d : to_delete ) {
PhylogenyMethods.removeNode( d, phy );
}
phy.clearHashIdToNodeMap();
phy.externalNodesHaveChanged();
}
final public static void deleteNonOrthologousExternalNodes( final Phylogeny phy, final PhylogenyNode n ) {
if ( n.isInternal() ) {
throw new IllegalArgumentException( "node is not external" );
}
final ArrayList to_delete = new ArrayList();
for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
final PhylogenyNode i = it.next();
if ( !PhylogenyMethods.getEventAtLCA( n, i ).isSpeciation() ) {
to_delete.add( i );
}
}
for( final PhylogenyNode d : to_delete ) {
phy.deleteSubtree( d, true );
}
phy.clearHashIdToNodeMap();
phy.externalNodesHaveChanged();
}
public final static List> divideIntoSubTrees( final Phylogeny phy,
final double min_distance_to_root ) {
if ( min_distance_to_root <= 0 ) {
throw new IllegalArgumentException( "attempt to use min distance to root of: " + min_distance_to_root );
}
final List> l = new ArrayList>();
setAllIndicatorsToZero( phy );
for( final PhylogenyNodeIterator it = phy.iteratorExternalForward(); it.hasNext(); ) {
final PhylogenyNode n = it.next();
if ( n.getIndicator() != 0 ) {
continue;
}
l.add( divideIntoSubTreesHelper( n, min_distance_to_root ) );
if ( l.isEmpty() ) {
throw new RuntimeException( "this should not have happened" );
}
}
return l;
}
public static List getAllDescendants( final PhylogenyNode node ) {
final List descs = new ArrayList();
final Set encountered = new HashSet();
if ( !node.isExternal() ) {
final List exts = node.getAllExternalDescendants();
for( PhylogenyNode current : exts ) {
descs.add( current );
while ( current != node ) {
current = current.getParent();
if ( encountered.contains( current.getId() ) ) {
continue;
}
descs.add( current );
encountered.add( current.getId() );
}
}
}
return descs;
}
/**
*
* Convenience method
*
* @param node
* @return
*/
public static Color getBranchColorValue( final PhylogenyNode node ) {
if ( node.getBranchData().getBranchColor() == null ) {
return null;
}
return node.getBranchData().getBranchColor().getValue();
}
/**
* Convenience method
*/
public static double getBranchWidthValue( final PhylogenyNode node ) {
if ( !node.getBranchData().isHasBranchWidth() ) {
return BranchWidth.BRANCH_WIDTH_DEFAULT_VALUE;
}
return node.getBranchData().getBranchWidth().getValue();
}
/**
* Convenience method
*/
public static double getConfidenceValue( final PhylogenyNode node ) {
if ( !node.getBranchData().isHasConfidences() ) {
return Confidence.CONFIDENCE_DEFAULT_VALUE;
}
return node.getBranchData().getConfidence( 0 ).getValue();
}
/**
* Convenience method
*/
public static double[] getConfidenceValuesAsArray( final PhylogenyNode node ) {
if ( !node.getBranchData().isHasConfidences() ) {
return new double[ 0 ];
}
final double[] values = new double[ node.getBranchData().getConfidences().size() ];
int i = 0;
for( final Confidence c : node.getBranchData().getConfidences() ) {
values[ i++ ] = c.getValue();
}
return values;
}
final public static Event getEventAtLCA( final PhylogenyNode n1, final PhylogenyNode n2 ) {
return calculateLCA( n1, n2 ).getNodeData().getEvent();
}
/**
* Returns taxonomy t if all external descendants have
* the same taxonomy t, null otherwise.
*
*/
public static Taxonomy getExternalDescendantsTaxonomy( final PhylogenyNode node ) {
final List descs = node.getAllExternalDescendants();
Taxonomy tax = null;
for( final PhylogenyNode n : descs ) {
if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
return null;
}
else if ( tax == null ) {
tax = n.getNodeData().getTaxonomy();
}
else if ( n.getNodeData().getTaxonomy().isEmpty() || !tax.isEqual( n.getNodeData().getTaxonomy() ) ) {
return null;
}
}
return tax;
}
public static PhylogenyNode getFurthestDescendant( final PhylogenyNode node ) {
final List children = node.getAllExternalDescendants();
PhylogenyNode farthest = null;
double longest = -Double.MAX_VALUE;
for( final PhylogenyNode child : children ) {
if ( PhylogenyMethods.getDistance( child, node ) > longest ) {
farthest = child;
longest = PhylogenyMethods.getDistance( child, node );
}
}
return farthest;
}
// public static PhylogenyMethods getInstance() {
// if ( PhylogenyMethods._instance == null ) {
// PhylogenyMethods._instance = new PhylogenyMethods();
// }
// return PhylogenyMethods._instance;
// }
/**
* Returns the largest confidence value found on phy.
*/
static public double getMaximumConfidenceValue( final Phylogeny phy ) {
double max = -Double.MAX_VALUE;
for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
final double s = PhylogenyMethods.getConfidenceValue( iter.next() );
if ( ( s != Confidence.CONFIDENCE_DEFAULT_VALUE ) && ( s > max ) ) {
max = s;
}
}
return max;
}
static public int getMinimumDescendentsPerInternalNodes( final Phylogeny phy ) {
int min = Integer.MAX_VALUE;
int d = 0;
PhylogenyNode n;
for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
n = it.next();
if ( n.isInternal() ) {
d = n.getNumberOfDescendants();
if ( d < min ) {
min = d;
}
}
}
return min;
}
/**
* Convenience method for display purposes.
* Not intended for algorithms.
*/
public static String getSpecies( final PhylogenyNode node ) {
if ( !node.getNodeData().isHasTaxonomy() ) {
return "";
}
else if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getScientificName() ) ) {
return node.getNodeData().getTaxonomy().getScientificName();
}
if ( !ForesterUtil.isEmpty( node.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
return node.getNodeData().getTaxonomy().getTaxonomyCode();
}
else {
return node.getNodeData().getTaxonomy().getCommonName();
}
}
/**
* Convenience method for display purposes.
* Not intended for algorithms.
*/
public static String getTaxonomyIdentifier( final PhylogenyNode node ) {
if ( !node.getNodeData().isHasTaxonomy() || ( node.getNodeData().getTaxonomy().getIdentifier() == null ) ) {
return "";
}
return node.getNodeData().getTaxonomy().getIdentifier().getValue();
}
public final static boolean isAllDecendentsAreDuplications( final PhylogenyNode n ) {
if ( n.isExternal() ) {
return true;
}
else {
if ( n.isDuplication() ) {
for( final PhylogenyNode desc : n.getDescendants() ) {
if ( !isAllDecendentsAreDuplications( desc ) ) {
return false;
}
}
return true;
}
else {
return false;
}
}
}
public static boolean isHasExternalDescendant( final PhylogenyNode node ) {
for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
if ( node.getChildNode( i ).isExternal() ) {
return true;
}
}
return false;
}
/*
* This is case insensitive.
*
*/
public synchronized static boolean isTaxonomyHasIdentifierOfGivenProvider( final Taxonomy tax,
final String[] providers ) {
if ( ( tax.getIdentifier() != null ) && !ForesterUtil.isEmpty( tax.getIdentifier().getProvider() ) ) {
final String my_tax_prov = tax.getIdentifier().getProvider();
for( final String provider : providers ) {
if ( provider.equalsIgnoreCase( my_tax_prov ) ) {
return true;
}
}
return false;
}
else {
return false;
}
}
public static void midpointRoot( final Phylogeny phylogeny ) {
if ( ( phylogeny.getNumberOfExternalNodes() < 2 ) || ( calculateMaxDistanceToRoot( phylogeny ) <= 0 ) ) {
return;
}
int counter = 0;
final int total_nodes = phylogeny.getNodeCount();
while ( true ) {
if ( ++counter > total_nodes ) {
throw new RuntimeException( "this should not have happened: midpoint rooting does not converge" );
}
PhylogenyNode a = null;
double da = 0;
double db = 0;
for( int i = 0; i < phylogeny.getRoot().getNumberOfDescendants(); ++i ) {
final PhylogenyNode f = getFurthestDescendant( phylogeny.getRoot().getChildNode( i ) );
final double df = getDistance( f, phylogeny.getRoot() );
if ( df > 0 ) {
if ( df > da ) {
db = da;
da = df;
a = f;
}
else if ( df > db ) {
db = df;
}
}
}
final double diff = da - db;
if ( diff < 0.000001 ) {
break;
}
double x = da - ( diff / 2.0 );
while ( ( x > a.getDistanceToParent() ) && !a.isRoot() ) {
x -= ( a.getDistanceToParent() > 0 ? a.getDistanceToParent() : 0 );
a = a.getParent();
}
phylogeny.reRoot( a, x );
}
phylogeny.recalculateNumberOfExternalDescendants( true );
}
public static void normalizeBootstrapValues( final Phylogeny phylogeny,
final double max_bootstrap_value,
final double max_normalized_value ) {
for( final PhylogenyNodeIterator iter = phylogeny.iteratorPreorder(); iter.hasNext(); ) {
final PhylogenyNode node = iter.next();
if ( node.isInternal() ) {
final double confidence = getConfidenceValue( node );
if ( confidence != Confidence.CONFIDENCE_DEFAULT_VALUE ) {
if ( confidence >= max_bootstrap_value ) {
setBootstrapConfidence( node, max_normalized_value );
}
else {
setBootstrapConfidence( node, ( confidence * max_normalized_value ) / max_bootstrap_value );
}
}
}
}
}
public static List obtainAllNodesAsList( final Phylogeny phy ) {
final List nodes = new ArrayList();
if ( phy.isEmpty() ) {
return nodes;
}
for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
nodes.add( iter.next() );
}
return nodes;
}
/**
* Returns a map of distinct taxonomies of
* all external nodes of node.
* If at least one of the external nodes has no taxonomy,
* null is returned.
*
*/
public static Map obtainDistinctTaxonomyCounts( final PhylogenyNode node ) {
final List descs = node.getAllExternalDescendants();
final Map tax_map = new HashMap();
for( final PhylogenyNode n : descs ) {
if ( !n.getNodeData().isHasTaxonomy() || n.getNodeData().getTaxonomy().isEmpty() ) {
return null;
}
final Taxonomy t = n.getNodeData().getTaxonomy();
if ( tax_map.containsKey( t ) ) {
tax_map.put( t, tax_map.get( t ) + 1 );
}
else {
tax_map.put( t, 1 );
}
}
return tax_map;
}
/**
* Arranges the order of childern for each node of this Phylogeny in such a
* way that either the branch with more children is on top (right) or on
* bottom (left), dependent on the value of boolean order.
*
* @param order
* decides in which direction to order
* @param pri
*/
public static void orderAppearance( final PhylogenyNode n,
final boolean order,
final boolean order_ext_alphabetically,
final DESCENDANT_SORT_PRIORITY pri ) {
if ( n.isExternal() ) {
return;
}
else {
PhylogenyNode temp = null;
if ( ( n.getNumberOfDescendants() == 2 )
&& ( n.getChildNode1().getNumberOfExternalNodes() != n.getChildNode2().getNumberOfExternalNodes() )
&& ( ( n.getChildNode1().getNumberOfExternalNodes() < n.getChildNode2().getNumberOfExternalNodes() ) == order ) ) {
temp = n.getChildNode1();
n.setChild1( n.getChildNode2() );
n.setChild2( temp );
}
else if ( order_ext_alphabetically ) {
boolean all_ext = true;
for( final PhylogenyNode i : n.getDescendants() ) {
if ( !i.isExternal() ) {
all_ext = false;
break;
}
}
if ( all_ext ) {
PhylogenyMethods.sortNodeDescendents( n, pri );
}
}
for( int i = 0; i < n.getNumberOfDescendants(); ++i ) {
orderAppearance( n.getChildNode( i ), order, order_ext_alphabetically, pri );
}
}
}
public static void postorderBranchColorAveragingExternalNodeBased( final Phylogeny p ) {
for( final PhylogenyNodeIterator iter = p.iteratorPostorder(); iter.hasNext(); ) {
final PhylogenyNode node = iter.next();
double red = 0.0;
double green = 0.0;
double blue = 0.0;
int n = 0;
if ( node.isInternal() ) {
//for( final PhylogenyNodeIterator iterator = node.iterateChildNodesForward(); iterator.hasNext(); ) {
for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
final PhylogenyNode child_node = node.getChildNode( i );
final Color child_color = getBranchColorValue( child_node );
if ( child_color != null ) {
++n;
red += child_color.getRed();
green += child_color.getGreen();
blue += child_color.getBlue();
}
}
setBranchColorValue( node,
new Color( ForesterUtil.roundToInt( red / n ),
ForesterUtil.roundToInt( green / n ),
ForesterUtil.roundToInt( blue / n ) ) );
}
}
}
public static final void preOrderReId( final Phylogeny phy ) {
if ( phy.isEmpty() ) {
return;
}
phy.setIdToNodeMap( null );
long i = PhylogenyNode.getNodeCount();
for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
it.next().setId( i++ );
}
PhylogenyNode.setNodeCount( i );
}
public final static Phylogeny[] readPhylogenies( final PhylogenyParser parser, final File file ) throws IOException {
final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
final Phylogeny[] trees = factory.create( file, parser );
if ( ( trees == null ) || ( trees.length == 0 ) ) {
throw new PhylogenyParserException( "Unable to parse phylogeny from file: " + file );
}
return trees;
}
public final static Phylogeny[] readPhylogenies( final PhylogenyParser parser, final List files )
throws IOException {
final List tree_list = new ArrayList();
for( final File file : files ) {
final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
final Phylogeny[] trees = factory.create( file, parser );
if ( ( trees == null ) || ( trees.length == 0 ) ) {
throw new PhylogenyParserException( "Unable to parse phylogeny from file: " + file );
}
tree_list.addAll( Arrays.asList( trees ) );
}
return tree_list.toArray( new Phylogeny[ tree_list.size() ] );
}
public static void removeNode( final PhylogenyNode remove_me, final Phylogeny phylogeny ) {
if ( remove_me.isRoot() ) {
if ( remove_me.getNumberOfDescendants() == 1 ) {
final PhylogenyNode desc = remove_me.getDescendants().get( 0 );
desc.setDistanceToParent( addPhylogenyDistances( remove_me.getDistanceToParent(),
desc.getDistanceToParent() ) );
desc.setParent( null );
phylogeny.setRoot( desc );
phylogeny.clearHashIdToNodeMap();
}
else {
throw new IllegalArgumentException( "attempt to remove a root node with more than one descendants" );
}
}
else if ( remove_me.isExternal() ) {
phylogeny.deleteSubtree( remove_me, false );
phylogeny.clearHashIdToNodeMap();
phylogeny.externalNodesHaveChanged();
}
else {
final PhylogenyNode parent = remove_me.getParent();
final List descs = remove_me.getDescendants();
parent.removeChildNode( remove_me );
for( final PhylogenyNode desc : descs ) {
parent.addAsChild( desc );
desc.setDistanceToParent( addPhylogenyDistances( remove_me.getDistanceToParent(),
desc.getDistanceToParent() ) );
}
remove_me.setParent( null );
phylogeny.clearHashIdToNodeMap();
phylogeny.externalNodesHaveChanged();
}
}
private static enum NDF {
NodeName( "NN" ),
TaxonomyCode( "TC" ),
TaxonomyCommonName( "CN" ),
TaxonomyScientificName( "TS" ),
TaxonomyIdentifier( "TI" ),
TaxonomySynonym( "SY" ),
SequenceName( "SN" ),
GeneName( "GN" ),
SequenceSymbol( "SS" ),
SequenceAccession( "SA" ),
Domain( "DO" ),
Annotation( "AN" ),
CrossRef( "XR" ),
BinaryCharacter( "BC" ),
MolecularSequence( "MS" );
private final String _text;
NDF( final String text ) {
_text = text;
}
public static NDF fromString( final String text ) {
for( final NDF n : NDF.values() ) {
if ( text.startsWith( n._text ) ) {
return n;
}
}
return null;
}
}
public static List searchData( final String query,
final Phylogeny phy,
final boolean case_sensitive,
final boolean partial,
final boolean regex,
final boolean search_domains,
final double domains_confidence_threshold ) {
final List nodes = new ArrayList();
if ( phy.isEmpty() || ( query == null ) ) {
return nodes;
}
if ( ForesterUtil.isEmpty( query ) ) {
return nodes;
}
String my_query = query;
NDF ndf = null;
if ( ( my_query.length() > 2 ) && ( my_query.indexOf( ":" ) == 2 ) ) {
ndf = NDF.fromString( my_query );
if ( ndf != null ) {
my_query = my_query.substring( 3 );
}
}
for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
final PhylogenyNode node = iter.next();
boolean match = false;
if ( ( ( ndf == null ) || ( ndf == NDF.NodeName ) )
&& match( node.getName(), my_query, case_sensitive, partial, regex ) ) {
match = true;
}
else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomyCode ) )
&& node.getNodeData().isHasTaxonomy()
&& match( node.getNodeData().getTaxonomy().getTaxonomyCode(),
my_query,
case_sensitive,
partial,
regex ) ) {
match = true;
}
else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomyCommonName ) )
&& node.getNodeData().isHasTaxonomy()
&& match( node.getNodeData().getTaxonomy().getCommonName(),
my_query,
case_sensitive,
partial,
regex ) ) {
match = true;
}
else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomyScientificName ) )
&& node.getNodeData().isHasTaxonomy()
&& match( node.getNodeData().getTaxonomy().getScientificName(),
my_query,
case_sensitive,
partial,
regex ) ) {
match = true;
}
else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomyIdentifier ) )
&& node.getNodeData().isHasTaxonomy()
&& ( node.getNodeData().getTaxonomy().getIdentifier() != null )
&& match( node.getNodeData().getTaxonomy().getIdentifier().getValue(),
my_query,
case_sensitive,
partial,
regex ) ) {
match = true;
}
else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomySynonym ) ) && node.getNodeData().isHasTaxonomy()
&& !node.getNodeData().getTaxonomy().getSynonyms().isEmpty() ) {
final List syns = node.getNodeData().getTaxonomy().getSynonyms();
I: for( final String syn : syns ) {
if ( match( syn, my_query, case_sensitive, partial, regex ) ) {
match = true;
break I;
}
}
}
if ( !match && ( ( ndf == null ) || ( ndf == NDF.SequenceName ) ) && node.getNodeData().isHasSequence()
&& match( node.getNodeData().getSequence().getName(), my_query, case_sensitive, partial, regex ) ) {
match = true;
}
if ( !match && ( ( ndf == null ) || ( ndf == NDF.GeneName ) ) && node.getNodeData().isHasSequence()
&& match( node.getNodeData().getSequence().getGeneName(), my_query, case_sensitive, partial, regex ) ) {
match = true;
}
if ( !match && ( ( ndf == null ) || ( ndf == NDF.SequenceSymbol ) ) && node.getNodeData().isHasSequence()
&& match( node.getNodeData().getSequence().getSymbol(), my_query, case_sensitive, partial, regex ) ) {
match = true;
}
if ( !match
&& ( ( ndf == null ) || ( ndf == NDF.SequenceAccession ) )
&& node.getNodeData().isHasSequence()
&& ( node.getNodeData().getSequence().getAccession() != null )
&& match( node.getNodeData().getSequence().getAccession().getValue(),
my_query,
case_sensitive,
partial,
regex ) ) {
match = true;
}
if ( !match && ( ( ( ndf == null ) && search_domains ) || ( ndf == NDF.Domain ) )
&& node.getNodeData().isHasSequence()
&& ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
final DomainArchitecture da = node.getNodeData().getSequence().getDomainArchitecture();
I: for( int i = 0; i < da.getNumberOfDomains(); ++i ) {
if ( ( da.getDomain( i ).getConfidence() <= domains_confidence_threshold )
&& ( match( da.getDomain( i ).getName(), my_query, case_sensitive, partial, regex ) ) ) {
match = true;
break I;
}
}
}
if ( !match && ( ( ndf == null ) || ( ndf == NDF.Annotation ) ) && node.getNodeData().isHasSequence()
&& ( node.getNodeData().getSequence().getAnnotations() != null ) ) {
for( final Annotation ann : node.getNodeData().getSequence().getAnnotations() ) {
if ( match( ann.getDesc(), my_query, case_sensitive, partial, regex ) ) {
match = true;
break;
}
if ( match( ann.getRef(), my_query, case_sensitive, partial, regex ) ) {
match = true;
break;
}
}
}
if ( !match && ( ( ndf == null ) || ( ndf == NDF.CrossRef ) ) && node.getNodeData().isHasSequence()
&& ( node.getNodeData().getSequence().getCrossReferences() != null ) ) {
for( final Accession x : node.getNodeData().getSequence().getCrossReferences() ) {
if ( match( x.getComment(), my_query, case_sensitive, partial, regex ) ) {
match = true;
break;
}
if ( match( x.getSource(), my_query, case_sensitive, partial, regex ) ) {
match = true;
break;
}
if ( match( x.getValue(), my_query, case_sensitive, partial, regex ) ) {
match = true;
break;
}
}
}
if ( !match && ( ( ndf == null ) || ( ndf == NDF.BinaryCharacter ) )
&& ( node.getNodeData().getBinaryCharacters() != null ) ) {
Iterator it = node.getNodeData().getBinaryCharacters().getPresentCharacters().iterator();
I: while ( it.hasNext() ) {
if ( match( it.next(), my_query, case_sensitive, partial, regex ) ) {
match = true;
break I;
}
}
it = node.getNodeData().getBinaryCharacters().getGainedCharacters().iterator();
I: while ( it.hasNext() ) {
if ( match( it.next(), my_query, case_sensitive, partial, regex ) ) {
match = true;
break I;
}
}
}
if ( !match
&& ( ndf == NDF.MolecularSequence )
&& node.getNodeData().isHasSequence()
&& match( node.getNodeData().getSequence().getMolecularSequence(),
my_query,
case_sensitive,
true,
regex ) ) {
match = true;
}
if ( match ) {
nodes.add( node );
}
}
return nodes;
}
public static List searchDataLogicalAnd( final String[] queries,
final Phylogeny phy,
final boolean case_sensitive,
final boolean partial,
final boolean search_domains,
final double domains_confidence_threshold ) {
final List nodes = new ArrayList();
if ( phy.isEmpty() || ( queries == null ) || ( queries.length < 1 ) ) {
return nodes;
}
for( final PhylogenyNodeIterator iter = phy.iteratorPreorder(); iter.hasNext(); ) {
final PhylogenyNode node = iter.next();
boolean all_matched = true;
for( String query : queries ) {
if ( query == null ) {
continue;
}
query = query.trim();
NDF ndf = null;
if ( ( query.length() > 2 ) && ( query.indexOf( ":" ) == 2 ) ) {
ndf = NDF.fromString( query );
if ( ndf != null ) {
query = query.substring( 3 );
}
}
boolean match = false;
if ( ForesterUtil.isEmpty( query ) ) {
continue;
}
if ( ( ( ndf == null ) || ( ndf == NDF.NodeName ) )
&& match( node.getName(), query, case_sensitive, partial, false ) ) {
match = true;
}
else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomyCode ) )
&& node.getNodeData().isHasTaxonomy()
&& match( node.getNodeData().getTaxonomy().getTaxonomyCode(),
query,
case_sensitive,
partial,
false ) ) {
match = true;
}
else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomyCommonName ) )
&& node.getNodeData().isHasTaxonomy()
&& match( node.getNodeData().getTaxonomy().getCommonName(),
query,
case_sensitive,
partial,
false ) ) {
match = true;
}
else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomyScientificName ) )
&& node.getNodeData().isHasTaxonomy()
&& match( node.getNodeData().getTaxonomy().getScientificName(),
query,
case_sensitive,
partial,
false ) ) {
match = true;
}
else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomyIdentifier ) )
&& node.getNodeData().isHasTaxonomy()
&& ( node.getNodeData().getTaxonomy().getIdentifier() != null )
&& match( node.getNodeData().getTaxonomy().getIdentifier().getValue(),
query,
case_sensitive,
partial,
false ) ) {
match = true;
}
else if ( ( ( ndf == null ) || ( ndf == NDF.TaxonomySynonym ) ) && node.getNodeData().isHasTaxonomy()
&& !node.getNodeData().getTaxonomy().getSynonyms().isEmpty() ) {
final List syns = node.getNodeData().getTaxonomy().getSynonyms();
I: for( final String syn : syns ) {
if ( match( syn, query, case_sensitive, partial, false ) ) {
match = true;
break I;
}
}
}
if ( !match && ( ( ndf == null ) || ( ndf == NDF.SequenceName ) ) && node.getNodeData().isHasSequence()
&& match( node.getNodeData().getSequence().getName(), query, case_sensitive, partial, false ) ) {
match = true;
}
if ( !match
&& ( ( ndf == null ) || ( ndf == NDF.GeneName ) )
&& node.getNodeData().isHasSequence()
&& match( node.getNodeData().getSequence().getGeneName(), query, case_sensitive, partial, false ) ) {
match = true;
}
if ( !match && ( ( ndf == null ) || ( ndf == NDF.SequenceSymbol ) )
&& node.getNodeData().isHasSequence()
&& match( node.getNodeData().getSequence().getSymbol(), query, case_sensitive, partial, false ) ) {
match = true;
}
if ( !match
&& ( ( ndf == null ) || ( ndf == NDF.SequenceAccession ) )
&& node.getNodeData().isHasSequence()
&& ( node.getNodeData().getSequence().getAccession() != null )
&& match( node.getNodeData().getSequence().getAccession().getValue(),
query,
case_sensitive,
partial,
false ) ) {
match = true;
}
if ( !match && ( ( ( ndf == null ) && search_domains ) || ( ndf == NDF.Domain ) )
&& node.getNodeData().isHasSequence()
&& ( node.getNodeData().getSequence().getDomainArchitecture() != null ) ) {
final DomainArchitecture da = node.getNodeData().getSequence().getDomainArchitecture();
I: for( int i = 0; i < da.getNumberOfDomains(); ++i ) {
if ( ( da.getDomain( i ).getConfidence() <= domains_confidence_threshold )
&& match( da.getDomain( i ).getName(), query, case_sensitive, partial, false ) ) {
match = true;
break I;
}
}
}
if ( !match && ( ( ndf == null ) || ( ndf == NDF.Annotation ) ) && node.getNodeData().isHasSequence()
&& ( node.getNodeData().getSequence().getAnnotations() != null ) ) {
for( final Annotation ann : node.getNodeData().getSequence().getAnnotations() ) {
if ( match( ann.getDesc(), query, case_sensitive, partial, false ) ) {
match = true;
break;
}
if ( match( ann.getRef(), query, case_sensitive, partial, false ) ) {
match = true;
break;
}
}
}
if ( !match && ( ( ndf == null ) || ( ndf == NDF.CrossRef ) ) && node.getNodeData().isHasSequence()
&& ( node.getNodeData().getSequence().getCrossReferences() != null ) ) {
for( final Accession x : node.getNodeData().getSequence().getCrossReferences() ) {
if ( match( x.getComment(), query, case_sensitive, partial, false ) ) {
match = true;
break;
}
if ( match( x.getSource(), query, case_sensitive, partial, false ) ) {
match = true;
break;
}
if ( match( x.getValue(), query, case_sensitive, partial, false ) ) {
match = true;
break;
}
}
}
if ( !match && ( ( ndf == null ) || ( ndf == NDF.BinaryCharacter ) )
&& ( node.getNodeData().getBinaryCharacters() != null ) ) {
Iterator it = node.getNodeData().getBinaryCharacters().getPresentCharacters().iterator();
I: while ( it.hasNext() ) {
if ( match( it.next(), query, case_sensitive, partial, false ) ) {
match = true;
break I;
}
}
it = node.getNodeData().getBinaryCharacters().getGainedCharacters().iterator();
I: while ( it.hasNext() ) {
if ( match( it.next(), query, case_sensitive, partial, false ) ) {
match = true;
break I;
}
}
}
if ( !match
&& ( ndf == NDF.MolecularSequence )
&& node.getNodeData().isHasSequence()
&& match( node.getNodeData().getSequence().getMolecularSequence(),
query,
case_sensitive,
true,
false ) ) {
match = true;
}
if ( !match ) {
all_matched = false;
break;
}
}
if ( all_matched ) {
nodes.add( node );
}
}
return nodes;
}
public static void setAllIndicatorsToZero( final Phylogeny phy ) {
for( final PhylogenyNodeIterator it = phy.iteratorPostorder(); it.hasNext(); ) {
it.next().setIndicator( ( byte ) 0 );
}
}
/**
* Convenience method.
* Sets value for the first confidence value (created if not present, values overwritten otherwise).
*/
public static void setBootstrapConfidence( final PhylogenyNode node, final double bootstrap_confidence_value ) {
setConfidence( node, bootstrap_confidence_value, "bootstrap" );
}
public static void setBranchColorValue( final PhylogenyNode node, final Color color ) {
if ( node.getBranchData().getBranchColor() == null ) {
node.getBranchData().setBranchColor( new BranchColor() );
}
node.getBranchData().getBranchColor().setValue( color );
}
/**
* Convenience method
*/
public static void setBranchWidthValue( final PhylogenyNode node, final double branch_width_value ) {
node.getBranchData().setBranchWidth( new BranchWidth( branch_width_value ) );
}
/**
* Convenience method.
* Sets value for the first confidence value (created if not present, values overwritten otherwise).
*/
public static void setConfidence( final PhylogenyNode node, final double confidence_value ) {
setConfidence( node, confidence_value, "" );
}
/**
* Convenience method.
* Sets value for the first confidence value (created if not present, values overwritten otherwise).
*/
public static void setConfidence( final PhylogenyNode node, final double confidence_value, final String type ) {
Confidence c = null;
if ( node.getBranchData().getNumberOfConfidences() > 0 ) {
c = node.getBranchData().getConfidence( 0 );
}
else {
c = new Confidence();
node.getBranchData().addConfidence( c );
}
c.setType( type );
c.setValue( confidence_value );
}
public static void setScientificName( final PhylogenyNode node, final String scientific_name ) {
if ( !node.getNodeData().isHasTaxonomy() ) {
node.getNodeData().setTaxonomy( new Taxonomy() );
}
node.getNodeData().getTaxonomy().setScientificName( scientific_name );
}
/**
* Convenience method to set the taxonomy code of a phylogeny node.
*
*
* @param node
* @param taxonomy_code
* @throws PhyloXmlDataFormatException
*/
public static void setTaxonomyCode( final PhylogenyNode node, final String taxonomy_code )
throws PhyloXmlDataFormatException {
if ( !node.getNodeData().isHasTaxonomy() ) {
node.getNodeData().setTaxonomy( new Taxonomy() );
}
node.getNodeData().getTaxonomy().setTaxonomyCode( taxonomy_code );
}
final static public void sortNodeDescendents( final PhylogenyNode node, final DESCENDANT_SORT_PRIORITY pri ) {
Comparator c;
switch ( pri ) {
case SEQUENCE:
c = new PhylogenyNodeSortSequencePriority();
break;
case NODE_NAME:
c = new PhylogenyNodeSortNodeNamePriority();
break;
default:
c = new PhylogenyNodeSortTaxonomyPriority();
}
final List descs = node.getDescendants();
Collections.sort( descs, c );
int i = 0;
for( final PhylogenyNode desc : descs ) {
node.setChildNode( i++, desc );
}
}
/**
* Removes from Phylogeny to_be_stripped all external Nodes which are
* associated with a species NOT found in Phylogeny reference.
*
* @param reference
* a reference Phylogeny
* @param to_be_stripped
* Phylogeny to be stripped
* @return nodes removed from to_be_stripped
*/
public static List taxonomyBasedDeletionOfExternalNodes( final Phylogeny reference,
final Phylogeny to_be_stripped ) {
final Set ref_ext_taxo = new HashSet();
for( final PhylogenyNodeIterator it = reference.iteratorExternalForward(); it.hasNext(); ) {
final PhylogenyNode n = it.next();
if ( !n.getNodeData().isHasTaxonomy() ) {
throw new IllegalArgumentException( "no taxonomic data in node: " + n );
}
if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) {
ref_ext_taxo.add( n.getNodeData().getTaxonomy().getScientificName() );
}
if ( !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getTaxonomyCode() ) ) {
ref_ext_taxo.add( n.getNodeData().getTaxonomy().getTaxonomyCode() );
}
if ( ( n.getNodeData().getTaxonomy().getIdentifier() != null )
&& !ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getIdentifier().getValue() ) ) {
ref_ext_taxo.add( n.getNodeData().getTaxonomy().getIdentifier().getValuePlusProvider() );
}
}
final ArrayList nodes_to_delete = new ArrayList();
for( final PhylogenyNodeIterator it = to_be_stripped.iteratorExternalForward(); it.hasNext(); ) {
final PhylogenyNode n = it.next();
if ( !n.getNodeData().isHasTaxonomy() ) {
nodes_to_delete.add( n );
}
else if ( !( ref_ext_taxo.contains( n.getNodeData().getTaxonomy().getScientificName() ) )
&& !( ref_ext_taxo.contains( n.getNodeData().getTaxonomy().getTaxonomyCode() ) )
&& !( ( n.getNodeData().getTaxonomy().getIdentifier() != null ) && ref_ext_taxo.contains( n
.getNodeData().getTaxonomy().getIdentifier().getValuePlusProvider() ) ) ) {
nodes_to_delete.add( n );
}
}
for( final PhylogenyNode n : nodes_to_delete ) {
to_be_stripped.deleteSubtree( n, true );
}
to_be_stripped.clearHashIdToNodeMap();
to_be_stripped.externalNodesHaveChanged();
return nodes_to_delete;
}
final static public void transferInternalNamesToBootstrapSupport( final Phylogeny phy ) {
final PhylogenyNodeIterator it = phy.iteratorPostorder();
while ( it.hasNext() ) {
final PhylogenyNode n = it.next();
if ( !n.isExternal() && !ForesterUtil.isEmpty( n.getName() ) ) {
double value = -1;
try {
value = Double.parseDouble( n.getName() );
}
catch ( final NumberFormatException e ) {
throw new IllegalArgumentException( "failed to parse number from [" + n.getName() + "]: "
+ e.getLocalizedMessage() );
}
if ( value >= 0.0 ) {
n.getBranchData().addConfidence( new Confidence( value, "bootstrap" ) );
n.setName( "" );
}
}
}
}
final static public boolean isInternalNamesLookLikeConfidences( final Phylogeny phy ) {
final PhylogenyNodeIterator it = phy.iteratorPostorder();
while ( it.hasNext() ) {
final PhylogenyNode n = it.next();
if ( !n.isExternal() && !n.isRoot() ) {
if ( !ForesterUtil.isEmpty( n.getName() ) ) {
double value = -1;
try {
value = Double.parseDouble( n.getName() );
}
catch ( final NumberFormatException e ) {
return false;
}
if ( ( value < 0.0 ) || ( value > 100 ) ) {
return false;
}
}
}
}
return true;
}
final static public void transferInternalNodeNamesToConfidence( final Phylogeny phy, final String confidence_type ) {
final PhylogenyNodeIterator it = phy.iteratorPostorder();
while ( it.hasNext() ) {
transferInternalNodeNameToConfidence( confidence_type, it.next() );
}
}
private static void transferInternalNodeNameToConfidence( final String confidence_type, final PhylogenyNode n ) {
if ( !n.isExternal() && !n.getBranchData().isHasConfidences() ) {
if ( !ForesterUtil.isEmpty( n.getName() ) ) {
double d = -1.0;
try {
d = Double.parseDouble( n.getName() );
}
catch ( final Exception e ) {
d = -1.0;
}
if ( d >= 0.0 ) {
n.getBranchData().addConfidence( new Confidence( d, confidence_type ) );
n.setName( "" );
}
}
}
}
final static public void transferNodeNameToField( final Phylogeny phy,
final PhylogenyNodeField field,
final boolean external_only ) throws PhyloXmlDataFormatException {
final PhylogenyNodeIterator it = phy.iteratorPostorder();
while ( it.hasNext() ) {
final PhylogenyNode n = it.next();
if ( external_only && n.isInternal() ) {
continue;
}
final String name = n.getName().trim();
if ( !ForesterUtil.isEmpty( name ) ) {
switch ( field ) {
case TAXONOMY_CODE:
n.setName( "" );
setTaxonomyCode( n, name );
break;
case TAXONOMY_SCIENTIFIC_NAME:
n.setName( "" );
if ( !n.getNodeData().isHasTaxonomy() ) {
n.getNodeData().setTaxonomy( new Taxonomy() );
}
n.getNodeData().getTaxonomy().setScientificName( name );
break;
case TAXONOMY_COMMON_NAME:
n.setName( "" );
if ( !n.getNodeData().isHasTaxonomy() ) {
n.getNodeData().setTaxonomy( new Taxonomy() );
}
n.getNodeData().getTaxonomy().setCommonName( name );
break;
case SEQUENCE_SYMBOL:
n.setName( "" );
if ( !n.getNodeData().isHasSequence() ) {
n.getNodeData().setSequence( new Sequence() );
}
n.getNodeData().getSequence().setSymbol( name );
break;
case SEQUENCE_NAME:
n.setName( "" );
if ( !n.getNodeData().isHasSequence() ) {
n.getNodeData().setSequence( new Sequence() );
}
n.getNodeData().getSequence().setName( name );
break;
case TAXONOMY_ID_UNIPROT_1: {
if ( !n.getNodeData().isHasTaxonomy() ) {
n.getNodeData().setTaxonomy( new Taxonomy() );
}
String id = name;
final int i = name.indexOf( '_' );
if ( i > 0 ) {
id = name.substring( 0, i );
}
else {
n.setName( "" );
}
n.getNodeData().getTaxonomy()
.setIdentifier( new Identifier( id, PhyloXmlUtil.UNIPROT_TAX_PROVIDER ) );
break;
}
case TAXONOMY_ID_UNIPROT_2: {
if ( !n.getNodeData().isHasTaxonomy() ) {
n.getNodeData().setTaxonomy( new Taxonomy() );
}
String id = name;
final int i = name.indexOf( '_' );
if ( i > 0 ) {
id = name.substring( i + 1, name.length() );
}
else {
n.setName( "" );
}
n.getNodeData().getTaxonomy()
.setIdentifier( new Identifier( id, PhyloXmlUtil.UNIPROT_TAX_PROVIDER ) );
break;
}
case TAXONOMY_ID: {
if ( !n.getNodeData().isHasTaxonomy() ) {
n.getNodeData().setTaxonomy( new Taxonomy() );
}
n.getNodeData().getTaxonomy().setIdentifier( new Identifier( name ) );
break;
}
}
}
}
}
static double addPhylogenyDistances( final double a, final double b ) {
if ( ( a >= 0.0 ) && ( b >= 0.0 ) ) {
return a + b;
}
else if ( a >= 0.0 ) {
return a;
}
else if ( b >= 0.0 ) {
return b;
}
return PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
}
static double calculateDistanceToAncestor( final PhylogenyNode anc, PhylogenyNode desc ) {
double d = 0;
boolean all_default = true;
while ( anc != desc ) {
if ( desc.getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
d += desc.getDistanceToParent();
if ( all_default ) {
all_default = false;
}
}
desc = desc.getParent();
}
if ( all_default ) {
return PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
}
return d;
}
/**
* Deep copies the phylogeny originating from this node.
*/
static PhylogenyNode copySubTree( final PhylogenyNode source ) {
if ( source == null ) {
return null;
}
else {
final PhylogenyNode newnode = source.copyNodeData();
if ( !source.isExternal() ) {
for( int i = 0; i < source.getNumberOfDescendants(); ++i ) {
newnode.setChildNode( i, PhylogenyMethods.copySubTree( source.getChildNode( i ) ) );
}
}
return newnode;
}
}
/**
* Shallow copies the phylogeny originating from this node.
*/
static PhylogenyNode copySubTreeShallow( final PhylogenyNode source ) {
if ( source == null ) {
return null;
}
else {
final PhylogenyNode newnode = source.copyNodeDataShallow();
if ( !source.isExternal() ) {
for( int i = 0; i < source.getNumberOfDescendants(); ++i ) {
newnode.setChildNode( i, PhylogenyMethods.copySubTreeShallow( source.getChildNode( i ) ) );
}
}
return newnode;
}
}
private final static List divideIntoSubTreesHelper( final PhylogenyNode node,
final double min_distance_to_root ) {
final List l = new ArrayList();
final PhylogenyNode r = moveTowardsRoot( node, min_distance_to_root );
for( final PhylogenyNode ext : r.getAllExternalDescendants() ) {
if ( ext.getIndicator() != 0 ) {
throw new RuntimeException( "this should not have happened" );
}
ext.setIndicator( ( byte ) 1 );
l.add( ext );
}
return l;
}
/**
* Calculates the distance between PhylogenyNodes n1 and n2.
* PRECONDITION: n1 is a descendant of n2.
*
* @param n1
* a descendant of n2
* @param n2
* @return distance between n1 and n2
*/
private static double getDistance( PhylogenyNode n1, final PhylogenyNode n2 ) {
double d = 0.0;
while ( n1 != n2 ) {
if ( n1.getDistanceToParent() > 0.0 ) {
d += n1.getDistanceToParent();
}
n1 = n1.getParent();
}
return d;
}
private static boolean match( final String s,
final String query,
final boolean case_sensitive,
final boolean partial,
final boolean regex ) {
if ( ForesterUtil.isEmpty( s ) || ForesterUtil.isEmpty( query ) ) {
return false;
}
String my_s = s.trim();
String my_query = query.trim();
if ( !case_sensitive && !regex ) {
my_s = my_s.toLowerCase();
my_query = my_query.toLowerCase();
}
if ( regex ) {
Pattern p = null;
try {
if ( case_sensitive ) {
p = Pattern.compile( my_query );
}
else {
p = Pattern.compile( my_query, Pattern.CASE_INSENSITIVE );
}
}
catch ( final PatternSyntaxException e ) {
return false;
}
if ( p != null ) {
return p.matcher( my_s ).find();
}
else {
return false;
}
}
else if ( partial ) {
return my_s.indexOf( my_query ) >= 0;
}
else {
Pattern p = null;
try {
p = Pattern.compile( "(\\b|_)" + Pattern.quote( my_query ) + "(\\b|_)" );
}
catch ( final PatternSyntaxException e ) {
return false;
}
if ( p != null ) {
return p.matcher( my_s ).find();
}
else {
return false;
}
}
}
private final static PhylogenyNode moveTowardsRoot( final PhylogenyNode node, final double min_distance_to_root ) {
PhylogenyNode n = node;
PhylogenyNode prev = node;
while ( min_distance_to_root < n.calculateDistanceToRoot() ) {
prev = n;
n = n.getParent();
}
return prev;
}
public static enum DESCENDANT_SORT_PRIORITY {
NODE_NAME, SEQUENCE, TAXONOMY;
}
public static enum PhylogenyNodeField {
CLADE_NAME,
SEQUENCE_NAME,
SEQUENCE_SYMBOL,
TAXONOMY_CODE,
TAXONOMY_COMMON_NAME,
TAXONOMY_ID,
TAXONOMY_ID_UNIPROT_1,
TAXONOMY_ID_UNIPROT_2,
TAXONOMY_SCIENTIFIC_NAME;
}
public static void addMolecularSeqsToTree( final Phylogeny phy, final Msa msa ) {
for( int s = 0; s < msa.getNumberOfSequences(); ++s ) {
final org.forester.sequence.MolecularSequence seq = msa.getSequence( s );
final PhylogenyNode node = phy.getNode( seq.getIdentifier() );
final org.forester.phylogeny.data.Sequence new_seq = new Sequence();
new_seq.setMolecularSequenceAligned( true );
new_seq.setMolecularSequence( seq.getMolecularSequenceAsString() );
new_seq.setName( seq.getIdentifier() );
try {
new_seq.setType( PhyloXmlUtil.SEQ_TYPE_PROTEIN );
}
catch ( final PhyloXmlDataFormatException ignore ) {
// do nothing
}
node.getNodeData().addSequence( new_seq );
}
}
final private static class PhylogenyNodeSortTaxonomyPriority implements Comparator {
@Override
public int compare( final PhylogenyNode n1, final PhylogenyNode n2 ) {
if ( n1.getNodeData().isHasTaxonomy() && n2.getNodeData().isHasTaxonomy() ) {
if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getScientificName() ) )
&& ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getScientificName() ) ) ) {
return n1.getNodeData().getTaxonomy().getScientificName().toLowerCase()
.compareTo( n2.getNodeData().getTaxonomy().getScientificName().toLowerCase() );
}
if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getTaxonomyCode() ) )
&& ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
return n1.getNodeData().getTaxonomy().getTaxonomyCode()
.compareTo( n2.getNodeData().getTaxonomy().getTaxonomyCode() );
}
}
if ( n1.getNodeData().isHasSequence() && n2.getNodeData().isHasSequence() ) {
if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getName() ) )
&& ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getName() ) ) ) {
return n1.getNodeData().getSequence().getName().toLowerCase()
.compareTo( n2.getNodeData().getSequence().getName().toLowerCase() );
}
if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getGeneName() ) )
&& ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getGeneName() ) ) ) {
return n1.getNodeData().getSequence().getGeneName()
.compareTo( n2.getNodeData().getSequence().getGeneName() );
}
if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getSymbol() ) )
&& ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getSymbol() ) ) ) {
return n1.getNodeData().getSequence().getSymbol()
.compareTo( n2.getNodeData().getSequence().getSymbol() );
}
}
if ( ( !ForesterUtil.isEmpty( n1.getName() ) ) && ( !ForesterUtil.isEmpty( n2.getName() ) ) ) {
return n1.getName().toLowerCase().compareTo( n2.getName().toLowerCase() );
}
return 0;
}
}
final private static class PhylogenyNodeSortSequencePriority implements Comparator {
@Override
public int compare( final PhylogenyNode n1, final PhylogenyNode n2 ) {
if ( n1.getNodeData().isHasSequence() && n2.getNodeData().isHasSequence() ) {
if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getName() ) )
&& ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getName() ) ) ) {
return n1.getNodeData().getSequence().getName().toLowerCase()
.compareTo( n2.getNodeData().getSequence().getName().toLowerCase() );
}
if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getGeneName() ) )
&& ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getGeneName() ) ) ) {
return n1.getNodeData().getSequence().getGeneName()
.compareTo( n2.getNodeData().getSequence().getGeneName() );
}
if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getSymbol() ) )
&& ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getSymbol() ) ) ) {
return n1.getNodeData().getSequence().getSymbol()
.compareTo( n2.getNodeData().getSequence().getSymbol() );
}
}
if ( n1.getNodeData().isHasTaxonomy() && n2.getNodeData().isHasTaxonomy() ) {
if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getScientificName() ) )
&& ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getScientificName() ) ) ) {
return n1.getNodeData().getTaxonomy().getScientificName().toLowerCase()
.compareTo( n2.getNodeData().getTaxonomy().getScientificName().toLowerCase() );
}
if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getTaxonomyCode() ) )
&& ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
return n1.getNodeData().getTaxonomy().getTaxonomyCode()
.compareTo( n2.getNodeData().getTaxonomy().getTaxonomyCode() );
}
}
if ( ( !ForesterUtil.isEmpty( n1.getName() ) ) && ( !ForesterUtil.isEmpty( n2.getName() ) ) ) {
return n1.getName().toLowerCase().compareTo( n2.getName().toLowerCase() );
}
return 0;
}
}
final private static class PhylogenyNodeSortNodeNamePriority implements Comparator {
@Override
public int compare( final PhylogenyNode n1, final PhylogenyNode n2 ) {
if ( ( !ForesterUtil.isEmpty( n1.getName() ) ) && ( !ForesterUtil.isEmpty( n2.getName() ) ) ) {
return n1.getName().toLowerCase().compareTo( n2.getName().toLowerCase() );
}
if ( n1.getNodeData().isHasTaxonomy() && n2.getNodeData().isHasTaxonomy() ) {
if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getScientificName() ) )
&& ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getScientificName() ) ) ) {
return n1.getNodeData().getTaxonomy().getScientificName().toLowerCase()
.compareTo( n2.getNodeData().getTaxonomy().getScientificName().toLowerCase() );
}
if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getTaxonomy().getTaxonomyCode() ) )
&& ( !ForesterUtil.isEmpty( n2.getNodeData().getTaxonomy().getTaxonomyCode() ) ) ) {
return n1.getNodeData().getTaxonomy().getTaxonomyCode()
.compareTo( n2.getNodeData().getTaxonomy().getTaxonomyCode() );
}
}
if ( n1.getNodeData().isHasSequence() && n2.getNodeData().isHasSequence() ) {
if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getName() ) )
&& ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getName() ) ) ) {
return n1.getNodeData().getSequence().getName().toLowerCase()
.compareTo( n2.getNodeData().getSequence().getName().toLowerCase() );
}
if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getGeneName() ) )
&& ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getGeneName() ) ) ) {
return n1.getNodeData().getSequence().getGeneName()
.compareTo( n2.getNodeData().getSequence().getGeneName() );
}
if ( ( !ForesterUtil.isEmpty( n1.getNodeData().getSequence().getSymbol() ) )
&& ( !ForesterUtil.isEmpty( n2.getNodeData().getSequence().getSymbol() ) ) ) {
return n1.getNodeData().getSequence().getSymbol()
.compareTo( n2.getNodeData().getSequence().getSymbol() );
}
}
return 0;
}
}
}
org/forester/phylogeny/PhylogenyBranch.java 0000664 0000000 0000000 00000012554 14125307352 020164 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// Copyright (C) 2000-2001 Washington University School of Medicine
// and Howard Hughes Medical Institute
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny;
import org.forester.phylogeny.data.PhylogenyData;
/*
* @author Christian M. Zmasek
*/
public class PhylogenyBranch {
private final PhylogenyNode _node_1;
private final PhylogenyNode _node_2;
private PhylogenyData _data;
private final boolean _is_directed;
private boolean _towards_1;
public PhylogenyBranch( final PhylogenyNode first_node, final PhylogenyNode second_node ) {
if ( ( first_node == null ) || ( second_node == null ) ) {
throw new IllegalArgumentException( "Attempt to create a branch with a null node" );
}
_node_1 = first_node;
_node_2 = second_node;
_is_directed = false;
}
public PhylogenyBranch( final PhylogenyNode first_node,
final PhylogenyNode second_node,
final boolean direction_towards_first ) {
if ( ( first_node == null ) || ( second_node == null ) ) {
throw new IllegalArgumentException( "Attempt to create a branch with a null node" );
}
_node_1 = first_node;
_node_2 = second_node;
_is_directed = true;
_towards_1 = direction_towards_first;
}
@Override
public boolean equals( final Object obj ) {
if ( this == obj ) {
return true;
}
if ( obj == null ) {
return false;
}
if ( getClass() != obj.getClass() ) {
return false;
}
final PhylogenyBranch other = ( PhylogenyBranch ) obj;
return hashCode() == other.hashCode();
}
public PhylogenyNode getConnectedNode( final PhylogenyNode node ) throws IllegalArgumentException {
if ( node == _node_1 ) {
return _node_2;
}
else if ( node == _node_2 ) {
return _node_1;
}
else {
throw new IllegalArgumentException( "Attempt to get " + "connected node on branch with node which is "
+ "not connected by the branch" );
}
}
public PhylogenyData getData() {
return _data;
}
public PhylogenyNode getFirstNode() {
return _node_1;
}
public PhylogenyNode getSecondNode() {
return _node_2;
}
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
final int node_1_hc = _node_1.hashCode();
final int node_2_hc = _node_2.hashCode();
int hc_1 = 0;
int hc_2 = 0;
if ( !_is_directed ) {
if ( node_1_hc > node_2_hc ) {
hc_1 = node_2_hc;
hc_2 = node_1_hc;
}
else {
hc_1 = node_1_hc;
hc_2 = node_2_hc;
}
}
else {
if ( _towards_1 ) {
hc_1 = node_2_hc;
hc_2 = node_1_hc;
}
else {
hc_1 = node_1_hc;
hc_2 = node_2_hc;
}
}
result = ( PRIME * result ) + ( ( _data == null ) ? 0 : _data.hashCode() );
result = ( PRIME * result ) + ( _is_directed ? 1231 : 1237 );
result = ( PRIME * result ) + hc_1;
result = ( PRIME * result ) + hc_2;
return result;
}
public boolean isDirected() {
return _is_directed;
}
public boolean isDirectionTowards( final PhylogenyNode node ) throws RuntimeException {
if ( !isDirected() ) {
throw new RuntimeException( "Attempt to get direction of undirected branch" );
}
return ( ( node == _node_1 ) && _towards_1 );
}
public void setDirectionTowards( final PhylogenyNode node ) {
_towards_1 = node == _node_1;
}
@Override
public String toString() {
if ( isDirected() ) {
if ( isDirectionTowards( getFirstNode() ) ) {
return ( getSecondNode().getName() + " -> " + getFirstNode().getName() );
}
else {
return ( getFirstNode().getName() + " -> " + getSecondNode().getName() );
}
}
else {
return ( getFirstNode().getName() + " -- " + getSecondNode().getName() );
}
}
}
org/forester/phylogeny/data/ 0000775 0000000 0000000 00000000000 14125307352 015127 5 ustar root root org/forester/phylogeny/data/Uri.java 0000664 0000000 0000000 00000007644 14125307352 016544 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.net.URI;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
public class Uri implements PhylogenyData {
final private URI _uri;
final private String _description;
final private String _type;
public Uri( final String uri_str, final String description, final String type ) {
if ( uri_str == null ) {
throw new IllegalArgumentException( "attempt to create Uri from null" );
}
_uri = URI.create( uri_str );
_description = description;
_type = type;
}
public Uri( final URI uri ) {
if ( uri == null ) {
throw new IllegalArgumentException( "attempt to create Uri from null URI" );
}
_uri = uri;
_description = "";
_type = "";
}
public Uri( final URI uri, final String description, final String type ) {
if ( uri == null ) {
throw new IllegalArgumentException( "attempt to create Uri from null URI" );
}
_uri = uri;
_description = description;
_type = type;
}
@Override
public StringBuffer asSimpleText() {
return new StringBuffer( getValue().toString() );
}
@Override
public StringBuffer asText() {
final StringBuffer sb = new StringBuffer();
sb.append( "[" );
sb.append( getDescription() );
sb.append( " " );
sb.append( getType() );
sb.append( "] " );
sb.append( getValue().toString() );
return sb;
}
@Override
public PhylogenyData copy() {
return new Uri( getValue().toString(), new String( getDescription() ), new String( getType() ) );
}
public String getDescription() {
return _description;
}
public String getType() {
return _type;
}
public URI getValue() {
return _uri;
}
@Override
public boolean isEqual( final PhylogenyData data ) {
throw new UnsupportedOperationException();
}
@Override
public StringBuffer toNHX() {
throw new UnsupportedOperationException();
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.URI,
getValue().toString(),
PhyloXmlMapping.TYPE_ATTR,
getType(),
PhyloXmlMapping.URI_DESC_ATTR,
getDescription(),
indentation );
}
@Override
public String toString() {
return asSimpleText().toString();
}
}
org/forester/phylogeny/data/Property.java 0000664 0000000 0000000 00000022161 14125307352 017620 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// Copyright (C) 2000-2001 Washington University School of Medicine
// and Howard Hughes Medical Institute
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.util.StringTokenizer;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.util.ForesterUtil;
public class Property implements PhylogenyData {
private String _value;
private final String _ref;
private final String _unit;
private final String _datatype;
private final AppliesTo _applies_to;
private final String _id_ref;
public Property( final String ref,
final String value,
final String unit,
final String datatype,
final AppliesTo applies_to ) {
this( ref, value, unit, datatype, applies_to, "" );
}
// Only used by method createFromNhxString.
private Property( final String ref,
final String value,
final String unit,
final String datatype,
final AppliesTo applies_to,
final boolean dummy ) {
_ref = ref;
_unit = unit;
_datatype = datatype;
_applies_to = applies_to;
_id_ref = "";
setValue( value );
}
public Property( final String ref,
final String value,
final String unit,
final String datatype,
final AppliesTo applies_to,
final String id_ref ) {
if ( !ForesterUtil.isEmpty( ref ) && ( ref.indexOf( ":" ) < 1 ) ) {
throw new IllegalArgumentException( "property reference [" + ref
+ "] is not in the expected format (missing a \":\")" );
}
if ( !ForesterUtil.isEmpty( unit ) && ( unit.indexOf( ":" ) < 1 ) ) {
throw new IllegalArgumentException( "property unit [" + unit
+ "] is not in the expected format (missing a \":\")" );
}
if ( !ForesterUtil.isEmpty( datatype ) && ( datatype.indexOf( ":" ) < 1 ) ) {
throw new IllegalArgumentException( "property datatype [" + unit
+ "] is not in the expected format (missing a \":\")" );
}
_ref = ref;
_unit = unit;
_datatype = datatype;
_applies_to = applies_to;
_id_ref = id_ref;
setValue( value );
}
@Override
public StringBuffer asSimpleText() {
return new StringBuffer( getValue() );
}
@Override
public StringBuffer asText() {
final StringBuffer sb = new StringBuffer();
sb.append( getRef() );
sb.append( ": " );
sb.append( getValue() );
if ( !ForesterUtil.isEmpty( getUnit() ) ) {
sb.append( " " );
sb.append( getUnit() );
}
return sb;
}
@Override
public PhylogenyData copy() {
return new Property( getRef(), getValue(), getUnit(), getDataType(), getAppliesTo(), getIdRef() );
}
public AppliesTo getAppliesTo() {
return _applies_to;
}
public String getDataType() {
return _datatype;
}
public String getIdRef() {
return _id_ref;
}
public String getRef() {
return _ref;
}
public String getUnit() {
return _unit;
}
public String getValue() {
return _value;
}
@Override
public boolean isEqual( final PhylogenyData data ) {
if ( data == null ) {
return false;
}
return ( ( Property ) data ).getValue().equals( getValue() )
&& ( ( Property ) data ).getUnit().equals( getUnit() )
&& ( ( Property ) data ).getRef().equals( getRef() );
}
public void setValue( final String value ) {
_value = value;
}
@Override
public StringBuffer toNHX() {
throw new UnsupportedOperationException();
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.PROPERTY,
getValue(),
PhyloXmlMapping.PROPERTY_REF,
getRef(),
PhyloXmlMapping.PROPERTY_UNIT,
getUnit(),
PhyloXmlMapping.PROPERTY_DATATYPE,
getDataType(),
PhyloXmlMapping.PROPERTY_APPLIES_TO,
getAppliesTo().toString(),
PhyloXmlMapping.ID_REF,
getIdRef(),
indentation );
}
@Override
public String toString() {
return asText().toString();
}
public static Property createFromNhxString( final String nhx ) throws IllegalArgumentException {
final StringTokenizer st = new StringTokenizer( nhx, "=" );
final int tokens = st.countTokens();
final String error = "error in NHX property tag format: "
+ "expected: X[N|B|C|S|T|P|O]==[=[=], got: \"" + nhx + "\" instead";
if ( ( tokens != 4 ) && ( tokens != 5 ) ) {
throw new IllegalArgumentException( error );
}
final String first = st.nextToken();
AppliesTo applies_to = null;
if ( first.equals( "XN" ) ) {
applies_to = AppliesTo.NODE;
}
else if ( first.equals( "XB" ) ) {
applies_to = AppliesTo.PARENT_BRANCH;
}
else if ( first.equals( "XC" ) ) {
applies_to = AppliesTo.CLADE;
}
else if ( first.equals( "XS" ) ) {
applies_to = AppliesTo.ANNOTATION;
}
else if ( first.equals( "XT" ) ) {
applies_to = AppliesTo.OTHER;
}
else if ( first.equals( "XP" ) ) {
applies_to = AppliesTo.PHYLOGENY;
}
else if ( first.equals( "XO" ) ) {
applies_to = AppliesTo.OTHER;
}
else {
throw new IllegalArgumentException( error );
}
String datatype = st.nextToken();
if ( datatype.equals( "S" ) ) {
datatype = "xsd:string";
}
else if ( datatype.equals( "L" ) ) {
datatype = "xsd:long";
}
else if ( datatype.equals( "D" ) ) {
datatype = "xsd:decimal";
}
else if ( datatype.equals( "B" ) ) {
datatype = "xsd:boolean";
}
else if ( datatype.equals( "U" ) ) {
datatype = "xsd:anyURI";
}
final String ref = st.nextToken();
final String value = st.nextToken();
String unit = "";
if ( tokens == 5 ) {
unit = st.nextToken();
}
return new Property( ref, value, unit, datatype, applies_to, true );
}
public static enum AppliesTo {
PHYLOGENY {
@Override
public String toString() {
return "phylogeny";
}
},
CLADE {
@Override
public String toString() {
return "clade";
}
},
NODE {
@Override
public String toString() {
return "node";
}
},
ANNOTATION {
@Override
public String toString() {
return "annotation";
}
},
PARENT_BRANCH {
@Override
public String toString() {
return "parent_branch";
}
},
OTHER {
@Override
public String toString() {
return "other";
}
}
}
}
org/forester/phylogeny/data/Annotation.java 0000664 0000000 0000000 00000024712 14125307352 020112 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.io.writers.PhylogenyWriter;
import org.forester.util.ForesterUtil;
public class Annotation implements PhylogenyData, MultipleUris, Comparable {
private Confidence _confidence;
private String _desc;
private String _evidence;
private PropertiesMap _properties;
private final String _ref_source;
private final String _ref_value;
private String _source;
private String _type;
private List _uris;
public Annotation() {
_ref_value = "";
_ref_source = "";
init();
}
public Annotation( final String ref ) {
if ( ForesterUtil.isEmpty( ref ) ) {
throw new IllegalArgumentException( "annotation reference is empty or null" );
}
final String s[] = ref.split( ":" );
if ( ( s.length != 2 ) || ForesterUtil.isEmpty( s[ 0 ] ) || ForesterUtil.isEmpty( s[ 1 ] ) ) {
throw new IllegalArgumentException( "illegal format for annotation reference: [" + ref + "]" );
}
_ref_source = s[ 0 ];
_ref_value = s[ 1 ];
init();
}
public Annotation( final String ref_source, final String ref_value ) {
if ( ForesterUtil.isEmpty( ref_source ) || ForesterUtil.isEmpty( ref_value ) ) {
throw new IllegalArgumentException( "illegal format for annotation reference" );
}
_ref_source = ref_source;
_ref_value = ref_value;
init();
}
@Override
public void addUri( final Uri uri ) {
if ( getUris() == null ) {
setUris( new ArrayList() );
}
getUris().add( uri );
}
@Override
public StringBuffer asSimpleText() {
return new StringBuffer( !ForesterUtil.isEmpty( getRef() ) ? getRef() : getDesc() );
}
@Override
public StringBuffer asText() {
final StringBuffer sb = new StringBuffer();
if ( !ForesterUtil.isEmpty( getDesc() ) && !ForesterUtil.isEmpty( getRef() ) ) {
sb.append( getDesc() );
sb.append( " (" );
sb.append( getRef() );
sb.append( ")" );
}
else if ( !ForesterUtil.isEmpty( getDesc() ) ) {
sb.append( getDesc() );
}
else if ( !ForesterUtil.isEmpty( getRef() ) ) {
sb.append( getRef() );
}
return sb;
}
@Override
public int compareTo( final Annotation o ) {
if ( equals( o ) ) {
return 0;
}
if ( getRef().equals( o.getRef() ) ) {
return getDesc().compareTo( o.getDesc() );
}
return getRef().compareTo( o.getRef() );
}
@Override
public PhylogenyData copy() {
final Annotation ann = new Annotation( getRefSource(), getRefValue() );
if ( getConfidence() != null ) {
ann.setConfidence( ( Confidence ) getConfidence().copy() );
}
else {
ann.setConfidence( null );
}
ann.setType( getType() );
ann.setDesc( getDesc() );
ann.setEvidence( getEvidence() );
ann.setSource( new String( getSource() ) );
if ( getProperties() != null ) {
ann.setProperties( ( PropertiesMap ) getProperties().copy() );
}
else {
ann.setProperties( null );
}
if ( getUris() != null ) {
ann.setUris( new ArrayList() );
for( final Uri uri : getUris() ) {
if ( uri != null ) {
ann.getUris().add( uri );
}
}
}
return ann;
}
@Override
public boolean equals( final Object o ) {
if ( this == o ) {
return true;
}
else if ( o == null ) {
return false;
}
else if ( o.getClass() != this.getClass() ) {
throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
+ o.getClass() + "]" );
}
else {
return isEqual( ( Annotation ) o );
}
}
public Confidence getConfidence() {
return _confidence;
}
public String getDesc() {
return _desc;
}
public String getEvidence() {
return _evidence;
}
public PropertiesMap getProperties() {
return _properties;
}
public String getRef() {
if ( ForesterUtil.isEmpty( _ref_source ) ) {
return "";
}
final StringBuilder sb = new StringBuilder();
sb.append( _ref_source );
sb.append( ':' );
sb.append( _ref_value );
return sb.toString();
}
public final String getRefSource() {
return _ref_source;
}
public final String getRefValue() {
return _ref_value;
}
public String getSource() {
return _source;
}
public String getType() {
return _type;
}
@Override
public Uri getUri( final int index ) {
return getUris().get( index );
}
@Override
public List getUris() {
return _uris;
}
@Override
public boolean isEqual( final PhylogenyData data ) {
final Annotation other = ( Annotation ) data;
return getDesc().equalsIgnoreCase( other.getDesc() ) && getType().equals( other.getType() )
&& getSource().equals( other.getSource() ) && getRef().equals( other.getRef() );
}
public void setConfidence( final Confidence confidence ) {
_confidence = confidence;
}
public void setDesc( final String desc ) {
_desc = desc;
}
public void setEvidence( final String evidence ) {
_evidence = evidence;
}
public void setProperties( final PropertiesMap property ) {
_properties = property;
}
// public void setRef( final String ref ) {
// _ref = ref;
// }
public void setSource( final String source ) {
_source = source;
}
public void setType( final String type ) {
_type = type;
}
@Override
public void setUris( final List uris ) {
_uris = uris;
}
@Override
public StringBuffer toNHX() {
throw new UnsupportedOperationException();
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
if ( ( getConfidence() != null ) || ( getProperties() != null )
|| ( ( getUris() != null ) && !getUris().isEmpty() ) || !ForesterUtil.isEmpty( getDesc() ) ) {
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendOpen( writer,
PhyloXmlMapping.ANNOTATION,
PhyloXmlMapping.ANNOTATION_REF_ATTR,
getRef(),
PhyloXmlMapping.ANNOTATION_EVIDENCE_ATTR,
getEvidence(),
PhyloXmlMapping.ANNOTATION_TYPE_ATTR,
getType(),
PhyloXmlMapping.ANNOTATION_SOURCE_ATTR,
getSource() );
if ( !ForesterUtil.isEmpty( getDesc() ) ) {
PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.ANNOTATION_DESC, getDesc(), indentation );
}
if ( getConfidence() != null ) {
getConfidence().toPhyloXML( writer, level, indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
}
if ( getProperties() != null ) {
getProperties().toPhyloXML( writer, level, indentation );
}
if ( getUris() != null ) {
for( final Uri uri : getUris() ) {
if ( uri != null ) {
uri.toPhyloXML( writer, level, indentation );
}
}
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.ANNOTATION );
}
else {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.ANNOTATION,
PhyloXmlMapping.ANNOTATION_REF_ATTR,
getRef(),
PhyloXmlMapping.ANNOTATION_EVIDENCE_ATTR,
getEvidence(),
PhyloXmlMapping.ANNOTATION_TYPE_ATTR,
getType(),
PhyloXmlMapping.ANNOTATION_SOURCE_ATTR,
getSource(),
indentation );
}
}
@Override
public String toString() {
return asText().toString();
}
private void init() {
_desc = "";
_type = "";
_source = "";
_evidence = "";
_confidence = null;
_properties = null;
setUris( null );
}
}
org/forester/phylogeny/data/NodeData.java 0000664 0000000 0000000 00000041635 14125307352 017462 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// Copyright (C) 2000-2001 Washington University School of Medicine
// and Howard Hughes Medical Institute
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
import org.forester.phylogeny.data.Property.AppliesTo;
import org.forester.util.ForesterUtil;
public class NodeData implements PhylogenyData {
private String _node_name;
private Event _event;
private List _sequences;
private List _taxonomies;
private List _distributions;
private Date _date;
private BinaryCharacters _binary_characters;
private PropertiesMap _properties;
private List _references;
private List _vector;
private NodeVisualData _node_visual_data;
public NodeData() {
init();
}
private void init() {
_node_name = "";
_event = null;
_sequences = null;
_taxonomies = null;
_distributions = null;
_date = null;
_binary_characters = null;
_properties = null;
_references = null;
_vector = null;
_node_visual_data = null;
}
public void addDistribution( final Distribution distribution ) {
if ( _distributions == null ) {
_distributions = new ArrayList();
}
_distributions.add( distribution );
}
public void addReference( final Reference reference ) {
if ( _references == null ) {
_references = new ArrayList();
}
_references.add( reference );
}
public void addSequence( final Sequence sequence ) {
if ( _sequences == null ) {
_sequences = new ArrayList();
}
_sequences.add( sequence );
}
public void addTaxonomy( final Taxonomy taxonomy ) {
if ( _taxonomies == null ) {
_taxonomies = new ArrayList();
}
_taxonomies.add( taxonomy );
}
@Override
public StringBuffer asSimpleText() {
throw new UnsupportedOperationException();
}
@Override
public StringBuffer asText() {
throw new UnsupportedOperationException();
}
@Override
public PhylogenyData copy() {
final NodeData new_data = new NodeData();
new_data.setNodeName( getNodeName() );
if ( ( getSequences() != null ) && ( getSequences().size() > 0 ) ) {
new_data.setSequences( new ArrayList() );
for( final Sequence s : getSequences() ) {
if ( s != null ) {
new_data.addSequence( ( Sequence ) s.copy() );
}
}
}
if ( isHasEvent() ) {
new_data.setEvent( ( Event ) getEvent().copy() );
}
if ( ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) ) {
new_data.setTaxonomies( new ArrayList() );
for( final Taxonomy t : getTaxonomies() ) {
if ( t != null ) {
new_data.addTaxonomy( ( Taxonomy ) t.copy() );
}
}
}
if ( isHasBinaryCharacters() ) {
new_data.setBinaryCharacters( ( BinaryCharacters ) getBinaryCharacters().copy() );
}
if ( ( getReferences() != null ) && ( getReferences().size() > 0 ) ) {
new_data.setReferences( new ArrayList() );
for( final Reference r : getReferences() ) {
if ( r != null ) {
new_data.addReference( ( Reference ) r.copy() );
}
}
}
if ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) {
new_data.setDistributions( new ArrayList() );
for( final Distribution d : getDistributions() ) {
if ( d != null ) {
new_data.addDistribution( ( Distribution ) d.copy() );
}
}
}
if ( ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
new_data.setNodeVisualData( ( NodeVisualData ) getNodeVisualData().copy() );
}
if ( isHasDate() ) {
new_data.setDate( ( Date ) getDate().copy() );
}
if ( isHasProperties() ) {
new_data.setProperties( ( PropertiesMap ) getProperties().copy() );
}
return new_data;
}
public BinaryCharacters getBinaryCharacters() {
return _binary_characters;
}
public Date getDate() {
return _date;
}
/**
* Convenience method -- always returns the first Distribution.
*
* @return Distribution
*/
public Distribution getDistribution() {
return getDistribution( 0 );
}
public Distribution getDistribution( final int index ) {
if ( _distributions == null ) {
return null;
}
return _distributions.get( index );
}
public List getDistributions() {
return _distributions;
}
public Event getEvent() {
return _event;
}
public PropertiesMap getProperties() {
return _properties;
}
/**
* Convenience method -- always returns the first Reference.
*
* @return Reference
*
*/
public Reference getReference() {
return getReference( 0 );
}
public Reference getReference( final int index ) {
if ( _references == null ) {
return null;
}
return _references.get( index );
}
public List getReferences() {
return _references;
}
/**
* Convenience method -- always returns the first Sequence.
*
* @return Sequence
*/
public Sequence getSequence() {
return getSequence( 0 );
}
public Sequence getSequence( final int index ) {
if ( _sequences == null ) {
return null;
}
return _sequences.get( index );
}
public List getSequences() {
return _sequences;
}
public List getTaxonomies() {
return _taxonomies;
}
/**
* Convenience method -- always returns the first Taxonomy.
*
* @return Taxonomy
*
*/
public Taxonomy getTaxonomy() {
return getTaxonomy( 0 );
}
public Taxonomy getTaxonomy( final int index ) {
if ( _taxonomies == null ) {
return null;
}
return _taxonomies.get( index );
}
@Override
public boolean isEqual( final PhylogenyData data ) {
throw new NoSuchMethodError();
}
public boolean isHasBinaryCharacters() {
return getBinaryCharacters() != null;
}
public boolean isEmpty() {
return ( ForesterUtil.isEmpty( _node_name ) && !isHasSequence() && !isHasTaxonomy() && !isHasBinaryCharacters()
&& !isHasDate() && !isHasDistribution() && !isHasEvent() && !isHasProperties() && !isHasReference() && ( ( _vector == null ) || _vector
.isEmpty() ) );
}
public boolean isHasDate() {
return ( getDate() != null )
&& ( !ForesterUtil.isEmpty( getDate().getDesc() ) || !ForesterUtil.isNull( getDate().getMax() )
|| !ForesterUtil.isNull( getDate().getMin() ) || !ForesterUtil.isNull( getDate().getValue() ) || !ForesterUtil
.isEmpty( getDate().getUnit() ) );
}
public boolean isHasDistribution() {
return ( ( ( getDistributions() != null ) && ( getDistributions().size() > 0 ) ) && ( ( !ForesterUtil
.isEmpty( getDistribution().getDesc() ) )
|| ( ( getDistribution().getPoints() != null ) && ( getDistribution().getPoints().size() > 0 ) ) || ( ( getDistribution()
.getPolygons() != null ) && ( getDistribution().getPolygons().size() > 0 ) ) ) );
}
public boolean isHasEvent() {
return getEvent() != null;
}
public boolean isHasProperties() {
return ( getProperties() != null ) && ( getProperties().size() > 0 );
}
public boolean isHasReference() {
return ( ( getReferences() != null ) && ( getReferences().size() > 0 ) )
&& ( !ForesterUtil.isEmpty( getReference().getDoi() ) || !ForesterUtil.isEmpty( getReference()
.getDescription() ) );
}
public boolean isHasSequence() {
return ( getSequences() != null ) && ( getSequences().size() > 0 ) && ( getSequences().get( 0 ) != null );
}
public boolean isHasTaxonomy() {
return ( getTaxonomies() != null ) && ( getTaxonomies().size() > 0 ) && ( getTaxonomies().get( 0 ) != null );
}
public void setBinaryCharacters( final BinaryCharacters binary_characters ) {
_binary_characters = binary_characters;
}
public void setDate( final Date date ) {
_date = date;
}
/**
* Convenience method -- always sets the first Distribution.
*
*/
public void setDistribution( final Distribution distribution ) {
if ( _distributions == null ) {
_distributions = new ArrayList();
}
if ( _distributions.size() == 0 ) {
_distributions.add( distribution );
}
else {
_distributions.set( 0, distribution );
}
}
public void setDistribution( final int index, final Distribution distribution ) {
if ( _distributions == null ) {
_distributions = new ArrayList();
}
_distributions.set( index, distribution );
}
private void setDistributions( final List distributions ) {
_distributions = distributions;
}
public void setEvent( final Event event ) {
_event = event;
}
public void setProperties( final PropertiesMap custom_data ) {
_properties = custom_data;
}
public void setReference( final int index, final Reference reference ) {
if ( _references == null ) {
_references = new ArrayList();
}
_references.set( index, reference );
}
/**
* Convenience method -- always sets the first Reference.
*
*/
public void setReference( final Reference reference ) {
if ( _references == null ) {
_references = new ArrayList();
}
if ( _references.size() == 0 ) {
_references.add( reference );
}
else {
_references.set( 0, reference );
}
}
private void setReferences( final List references ) {
_references = references;
}
public void setSequence( final int index, final Sequence sequence ) {
if ( _sequences == null ) {
_sequences = new ArrayList();
}
_sequences.set( index, sequence );
}
/**
* Convenience method -- always sets the first Sequence.
*
*/
public void setSequence( final Sequence sequence ) {
if ( _sequences == null ) {
_sequences = new ArrayList();
}
if ( _sequences.size() == 0 ) {
_sequences.add( sequence );
}
else {
_sequences.set( 0, sequence );
}
}
private void setSequences( final List sequences ) {
_sequences = sequences;
}
private void setTaxonomies( final List taxonomies ) {
_taxonomies = taxonomies;
}
public void setTaxonomy( final int index, final Taxonomy taxonomy ) {
if ( _taxonomies == null ) {
_taxonomies = new ArrayList();
}
_taxonomies.set( index, taxonomy );
}
/**
* Convenience method -- always sets the first Taxonomy.
*
*/
public void setTaxonomy( final Taxonomy taxonomy ) {
if ( _taxonomies == null ) {
_taxonomies = new ArrayList();
}
if ( _taxonomies.size() == 0 ) {
_taxonomies.add( taxonomy );
}
else {
_taxonomies.set( 0, taxonomy );
}
}
@Override
public StringBuffer toNHX() {
final StringBuffer sb = new StringBuffer();
if ( isHasTaxonomy() ) {
sb.append( getTaxonomy().toNHX() );
}
if ( isHasSequence() ) {
sb.append( getSequence().toNHX() );
}
if ( isHasEvent() ) {
sb.append( getEvent().toNHX() );
}
return sb;
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
if ( isHasTaxonomy() ) {
for( final Taxonomy t : getTaxonomies() ) {
if ( !t.isEmpty() ) {
t.toPhyloXML( writer, level, indentation );
}
}
}
if ( isHasSequence() ) {
for( final Sequence s : getSequences() ) {
if ( !s.isEmpty() ) {
s.toPhyloXML( writer, level, indentation );
}
}
}
if ( isHasEvent() ) {
getEvent().toPhyloXML( writer, level, indentation );
}
if ( isHasBinaryCharacters() ) {
getBinaryCharacters().toPhyloXML( writer, level, indentation );
}
if ( isHasDistribution() ) {
for( final Distribution d : getDistributions() ) {
d.toPhyloXML( writer, level, indentation );
}
}
if ( isHasDate() ) {
getDate().toPhyloXML( writer, level, indentation );
}
if ( isHasReference() ) {
for( final Reference r : getReferences() ) {
r.toPhyloXML( writer, level, indentation );
}
}
if ( isHasProperties() ) {
getProperties().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
}
if ( ( level == 0 ) && ( getNodeVisualData() != null ) && !getNodeVisualData().isEmpty() ) {
getNodeVisualData().toPhyloXML( writer, level, indentation.substring( 0, indentation.length() - 2 ) );
}
if ( ( getVector() != null )
&& !getVector().isEmpty()
&& ( ( getProperties() == null ) || getProperties()
.getPropertiesWithGivenReferencePrefix( PhyloXmlUtil.VECTOR_PROPERTY_REF ).isEmpty() ) ) {
final List ps = vectorToProperties( getVector() );
final String my_indent = indentation.substring( 0, indentation.length() - 2 );
for( final Property p : ps ) {
p.toPhyloXML( writer, level, my_indent );
}
}
}
private List vectorToProperties( final List vector ) {
final List properties = new ArrayList();
for( int i = 0; i < vector.size(); ++i ) {
properties.add( new Property( PhyloXmlUtil.VECTOR_PROPERTY_REF + i,
String.valueOf( vector.get( i ) ),
"",
PhyloXmlUtil.VECTOR_PROPERTY_TYPE,
AppliesTo.NODE ) );
}
return properties;
}
public void setVector( final List vector ) {
_vector = vector;
}
public List getVector() {
return _vector;
}
public String getNodeName() {
return _node_name;
}
public void setNodeName( final String node_name ) {
_node_name = node_name;
}
public void setNodeVisualData( final NodeVisualData node_visual_data ) {
_node_visual_data = node_visual_data;
}
public NodeVisualData getNodeVisualData() {
return _node_visual_data;
}
}
org/forester/phylogeny/data/Sequence.java 0000664 0000000 0000000 00000042567 14125307352 017560 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import org.forester.io.parsers.nhx.NHXtags;
import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
import org.forester.io.writers.PhylogenyWriter;
import org.forester.sequence.MolecularSequence;
import org.forester.sequence.MolecularSequence.TYPE;
import org.forester.util.ForesterUtil;
public class Sequence implements PhylogenyData, MultipleUris, Comparable {
private Accession _accession;
private SortedSet _annotations;
private DomainArchitecture _da;
private String _gene_name;
private String _location;
private String _mol_sequence;
private boolean _mol_sequence_is_aligned;
private String _name;
private List _seq_relations;
private String _source_id;
private String _symbol;
private String _type;
private List _uris;
private SortedSet _xrefs;
public Sequence() {
init();
}
public Sequence( final MolecularSequence mol_seq ) {
init();
setMolecularSequence( mol_seq.getMolecularSequenceAsString() );
setName( mol_seq.getIdentifier() );
String type;
if ( mol_seq.getType() == TYPE.AA ) {
type = "protein";
}
else if ( mol_seq.getType() == TYPE.DNA ) {
type = "dna";
}
else if ( mol_seq.getType() == TYPE.RNA ) {
type = "rna";
}
else {
throw new IllegalArgumentException( "unknown sequence type " + mol_seq.getType() );
}
try {
setType( type );
}
catch ( final PhyloXmlDataFormatException e ) {
throw new IllegalArgumentException( "don't know how to handle type " + mol_seq.getType() );
}
}
public void addAnnotation( final Annotation annotation ) {
getAnnotations().add( annotation );
}
public void addCrossReference( final Accession cross_reference ) {
if ( getCrossReferences() == null ) {
setCrossReferences( new TreeSet() );
}
getCrossReferences().add( cross_reference );
}
public void addSequenceRelation( final SequenceRelation sr ) {
getSequenceRelations().add( sr );
}
@Override
public void addUri( final Uri uri ) {
if ( getUris() == null ) {
setUris( new ArrayList() );
}
getUris().add( uri );
}
@Override
public StringBuffer asSimpleText() {
final StringBuffer sb = new StringBuffer();
if ( getAccession() != null ) {
sb.append( "[" );
sb.append( getAccession() );
sb.append( "] " );
}
if ( !ForesterUtil.isEmpty( getName() ) ) {
sb.append( getName() );
sb.append( " " );
}
if ( !ForesterUtil.isEmpty( getLocation() ) ) {
sb.append( getLocation() );
}
return sb;
}
@Override
public StringBuffer asText() {
return asSimpleText();
}
@Override
public int compareTo( final Sequence o ) {
if ( ( !ForesterUtil.isEmpty( getName() ) ) && ( !ForesterUtil.isEmpty( o.getName() ) ) ) {
return getName().compareTo( o.getName() );
}
if ( ( !ForesterUtil.isEmpty( getSymbol() ) ) && ( !ForesterUtil.isEmpty( o.getSymbol() ) ) ) {
return getSymbol().compareTo( o.getSymbol() );
}
if ( ( !ForesterUtil.isEmpty( getGeneName() ) ) && ( !ForesterUtil.isEmpty( o.getGeneName() ) ) ) {
return getGeneName().compareTo( o.getGeneName() );
}
if ( ( getAccession() != null ) && ( o.getAccession() != null )
&& !ForesterUtil.isEmpty( getAccession().getValue() )
&& !ForesterUtil.isEmpty( o.getAccession().getValue() ) ) {
return getAccession().getValue().compareTo( o.getAccession().getValue() );
}
if ( ( !ForesterUtil.isEmpty( getMolecularSequence() ) )
&& ( !ForesterUtil.isEmpty( o.getMolecularSequence() ) ) ) {
return getMolecularSequence().compareTo( o.getMolecularSequence() );
}
return 0;
}
/**
* Not a deep copy.
*
*/
@Override
public PhylogenyData copy() {
final Sequence seq = new Sequence();
seq.setAnnotations( getAnnotations() );
seq.setName( getName() );
seq.setGeneName( getGeneName() );
try {
seq.setSymbol( getSymbol() );
}
catch ( final PhyloXmlDataFormatException e ) {
e.printStackTrace();
}
seq.setMolecularSequence( getMolecularSequence() );
seq.setMolecularSequenceAligned( isMolecularSequenceAligned() );
seq.setLocation( getLocation() );
if ( getAccession() != null ) {
seq.setAccession( ( Accession ) getAccession().copy() );
}
else {
seq.setAccession( null );
}
try {
seq.setType( getType() );
}
catch ( final PhyloXmlDataFormatException e ) {
e.printStackTrace();
}
if ( getUris() != null ) {
seq.setUris( new ArrayList() );
for( final Uri uri : getUris() ) {
if ( uri != null ) {
seq.getUris().add( uri );
}
}
}
if ( getDomainArchitecture() != null ) {
seq.setDomainArchitecture( ( DomainArchitecture ) getDomainArchitecture().copy() );
}
else {
seq.setDomainArchitecture( null );
}
if ( getCrossReferences() != null ) {
seq.setCrossReferences( new TreeSet() );
for( final Accession x : getCrossReferences() ) {
if ( x != null ) {
seq.getCrossReferences().add( x );
}
}
}
return seq;
}
@Override
public boolean equals( final Object o ) {
if ( this == o ) {
return true;
}
else if ( o == null ) {
return false;
}
else if ( o.getClass() != this.getClass() ) {
throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
+ o.getClass() + "]" );
}
else {
return isEqual( ( Sequence ) o );
}
}
public Accession getAccession() {
return _accession;
}
public Annotation getAnnotation( final int i ) {
return ( Annotation ) getAnnotations().toArray()[ i ];
}
public SortedSet getAnnotations() {
if ( _annotations == null ) {
_annotations = new TreeSet();
}
return _annotations;
}
public SortedSet getCrossReferences() {
return _xrefs;
}
public DomainArchitecture getDomainArchitecture() {
return _da;
}
public String getGeneName() {
return _gene_name;
}
public String getLocation() {
return _location;
}
public String getMolecularSequence() {
return _mol_sequence;
}
public String getName() {
return _name;
}
public List getSequenceRelations() {
if ( _seq_relations == null ) {
_seq_relations = new ArrayList();
}
return _seq_relations;
}
public String getSourceId() {
return _source_id;
}
public String getSymbol() {
return _symbol;
}
public String getType() {
return _type;
}
@Override
public Uri getUri( final int index ) {
return getUris().get( index );
}
@Override
public List getUris() {
return _uris;
}
@Override
public int hashCode() {
if ( getAccession() != null ) {
return getAccession().hashCode();
}
int result = getName().hashCode();
if ( getSymbol().length() > 0 ) {
result ^= getName().hashCode();
}
if ( getGeneName().length() > 0 ) {
result ^= getGeneName().hashCode();
}
if ( getMolecularSequence().length() > 0 ) {
result ^= getMolecularSequence().hashCode();
}
return result;
}
public boolean hasSequenceRelations() {
return _seq_relations.size() > 0;
}
public void init() {
setName( "" );
setGeneName( "" );
setMolecularSequence( "" );
setMolecularSequenceAligned( false );
setLocation( "" );
setAccession( null );
try {
setSymbol( "" );
}
catch ( final PhyloXmlDataFormatException e ) {
e.printStackTrace();
}
try {
setType( "" );
}
catch ( final PhyloXmlDataFormatException e ) {
e.printStackTrace();
}
setDomainArchitecture( null );
setUris( null );
setSequenceRelations( null );
setSourceId( null );
setCrossReferences( null );
setAnnotations( null );
}
public boolean isEmpty() {
return ( getAccession() == null ) && ForesterUtil.isEmpty( getName() ) && ForesterUtil.isEmpty( getSymbol() )
&& ForesterUtil.isEmpty( getGeneName() ) && ForesterUtil.isEmpty( getType() )
&& ForesterUtil.isEmpty( getLocation() ) && ForesterUtil.isEmpty( getSourceId() )
&& ForesterUtil.isEmpty( getMolecularSequence() ) && ( getDomainArchitecture() == null )
&& ForesterUtil.isEmpty( _annotations ) && ForesterUtil.isEmpty( _uris )
&& ForesterUtil.isEmpty( _seq_relations )
&& ( ( getCrossReferences() == null ) || getCrossReferences().isEmpty() );
}
@Override
public boolean isEqual( final PhylogenyData data ) {
if ( this == data ) {
return true;
}
final Sequence s = ( Sequence ) data;
if ( ( getAccession() != null ) && ( s.getAccession() != null ) ) {
return getAccession().isEqual( s.getAccession() );
}
return s.getMolecularSequence().equals( getMolecularSequence() ) && s.getName().equals( getName() )
&& s.getSymbol().equals( getSymbol() ) && s.getGeneName().equals( getGeneName() );
}
public boolean isMolecularSequenceAligned() {
return _mol_sequence_is_aligned;
}
public void setAccession( final Accession accession ) {
_accession = accession;
}
public void setDomainArchitecture( final DomainArchitecture ds ) {
_da = ds;
}
public void setGeneName( final String gene_name ) {
_gene_name = gene_name;
}
public void setLocation( final String description ) {
_location = description;
}
public void setMolecularSequence( final String mol_sequence ) {
_mol_sequence = mol_sequence;
}
public void setMolecularSequenceAligned( final boolean aligned ) {
_mol_sequence_is_aligned = aligned;
}
public void setName( final String name ) {
_name = name;
}
public void setSourceId( final String source_id ) {
_source_id = source_id;
}
public void setSymbol( final String symbol ) throws PhyloXmlDataFormatException {
if ( !ForesterUtil.isEmpty( symbol ) && !PhyloXmlUtil.SEQUENCE_SYMBOL_PATTERN.matcher( symbol ).matches() ) {
throw new PhyloXmlDataFormatException( "illegal sequence symbol: [" + symbol + "]" );
}
_symbol = symbol;
}
public void setType( final String type ) throws PhyloXmlDataFormatException {
if ( !ForesterUtil.isEmpty( type ) && !PhyloXmlUtil.SEQUENCE_TYPES.contains( type ) ) {
throw new PhyloXmlDataFormatException( "illegal sequence type: [" + type + "]" );
}
_type = type;
}
@Override
public void setUris( final List uris ) {
_uris = uris;
}
@Override
public StringBuffer toNHX() {
final StringBuffer sb = new StringBuffer();
if ( getName().length() > 0 ) {
sb.append( ":" );
sb.append( NHXtags.GENE_NAME );
sb.append( ForesterUtil.replaceIllegalNhxCharacters( getName() ) );
}
if ( getAccession() != null ) {
getAccession().toNHX();
}
return sb;
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
if ( isEmpty() ) {
return;
}
final String my_ind = indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE;
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.SEQUENCE, PhyloXmlMapping.SEQUENCE_TYPE, getType() );
if ( !ForesterUtil.isEmpty( getSymbol() ) ) {
PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.SEQUENCE_SYMBOL, getSymbol(), indentation );
}
if ( ( getAccession() != null ) && !ForesterUtil.isEmpty( getAccession().getValue() ) ) {
getAccession().toPhyloXML( writer, level, indentation );
}
if ( !ForesterUtil.isEmpty( getName() ) ) {
PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.SEQUENCE_NAME, getName(), indentation );
}
if ( !ForesterUtil.isEmpty( getGeneName() ) ) {
PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.SEQUENCE_GENE_NAME, getGeneName(), indentation );
}
if ( !ForesterUtil.isEmpty( getLocation() ) ) {
PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.SEQUENCE_LOCATION, getLocation(), indentation );
}
if ( !ForesterUtil.isEmpty( getMolecularSequence() ) ) {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.SEQUENCE_MOL_SEQ,
getMolecularSequence(),
PhyloXmlMapping.SEQUENCE_MOL_SEQ_ALIGNED_ATTR,
String.valueOf( isMolecularSequenceAligned() ),
indentation );
}
if ( ( getUris() != null ) && !getUris().isEmpty() ) {
for( final Uri uri : getUris() ) {
if ( uri != null ) {
uri.toPhyloXML( writer, level, indentation );
}
}
}
if ( ( getAnnotations() != null ) && !getAnnotations().isEmpty() ) {
for( final PhylogenyData annotation : getAnnotations() ) {
annotation.toPhyloXML( writer, level, my_ind );
}
}
if ( ( getCrossReferences() != null ) && !getCrossReferences().isEmpty() ) {
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( my_ind );
PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.SEQUENCE_X_REFS );
for( final PhylogenyData x : getCrossReferences() ) {
x.toPhyloXML( writer, level, my_ind );
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( my_ind );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.SEQUENCE_X_REFS );
}
if ( getDomainArchitecture() != null ) {
getDomainArchitecture().toPhyloXML( writer, level, my_ind );
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.SEQUENCE );
}
@Override
public String toString() {
return asText().toString();
}
private void setAnnotations( final SortedSet annotations ) {
_annotations = annotations;
}
private void setCrossReferences( final TreeSet cross_references ) {
_xrefs = cross_references;
}
private void setSequenceRelations( final List seq_relations ) {
_seq_relations = seq_relations;
}
}
org/forester/phylogeny/data/ProteinDomain.java 0000664 0000000 0000000 00000014405 14125307352 020546 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.util.ForesterUtil;
public class ProteinDomain implements PhylogenyData {
final public static double CONFIDENCE_DEFAULT = -1;
final public static String IDENTIFIER_DEFAULT = "";
final private String _name;
final private int _from;
final private int _to;
final private String _id;
final private double _confidence;
public ProteinDomain( final String name, final int from, final int to ) {
this( name, from, to, ProteinDomain.IDENTIFIER_DEFAULT, ProteinDomain.CONFIDENCE_DEFAULT );
}
public ProteinDomain( final String name, final int from, final int to, final double confidence ) {
this( name, from, to, ProteinDomain.IDENTIFIER_DEFAULT, confidence );
}
public ProteinDomain( final String name, final int from, final int to, final String id ) {
this( name, from, to, id, ProteinDomain.CONFIDENCE_DEFAULT );
}
public ProteinDomain( final String name, final int from, final int to, final String id, final double confidence ) {
if ( ( from >= to ) || ( to < 0 ) ) {
throw new IllegalArgumentException( "attempt to create protein domain from " + from + " to " + to );
}
_name = name;
_from = from;
_to = to;
_id = id;
_confidence = confidence;
}
@Override
public StringBuffer asSimpleText() {
return new StringBuffer( getName() );
}
@Override
public StringBuffer asText() {
final StringBuffer sb = new StringBuffer( getName() );
sb.append( " [" );
sb.append( getLength() );
if ( !ForesterUtil.isEmpty( getId() ) ) {
sb.append( " " );
sb.append( getId() );
}
if ( getConfidence() >= 0 ) {
sb.append( " " );
sb.append( getConfidence() );
}
sb.append( "]" );
return sb;
}
@Override
public PhylogenyData copy() {
if ( getId() == null ) {
return new ProteinDomain( getName(), getFrom(), getTo(), getConfidence() );
}
return new ProteinDomain( getName(), getFrom(), getTo(), getId(), getConfidence() );
}
public double getConfidence() {
return _confidence;
}
public int getFrom() {
return _from;
}
public String getId() {
return _id;
}
public int getLength() {
return ( ( getTo() - getFrom() ) + 1 );
}
public String getName() {
return _name;
}
public int getTo() {
return _to;
}
@Override
public boolean isEqual( final PhylogenyData protein_domain ) {
if ( protein_domain == null ) {
return false;
}
if ( !( protein_domain instanceof ProteinDomain ) ) {
return false;
}
else if ( ( ( ProteinDomain ) protein_domain ).getLength() != getLength() ) {
return false;
}
else if ( !( ( ProteinDomain ) protein_domain ).getName().equals( getName() ) ) {
return false;
}
return true;
}
@Override
public StringBuffer toNHX() {
throw new UnsupportedOperationException();
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
if ( getId() != null ) {
PhylogenyDataUtil.appendOpen( writer,
PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_DOMAIN,
PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_FROM,
getFrom() + "",
PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_TO,
getTo() + "",
PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_CONFIDENCE,
getConfidence() + "",
PhyloXmlMapping.IDENTIFIER,
getId() );
}
else {
PhylogenyDataUtil.appendOpen( writer,
PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_DOMAIN,
PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_FROM,
getFrom() + "",
PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_TO,
getTo() + "",
PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_PROT_DOMAIN_CONFIDENCE,
getConfidence() + "" );
}
writer.write( getName() );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_DOMAIN );
}
@Override
public String toString() {
return asText().toString();
}
}
org/forester/phylogeny/data/Event.java 0000664 0000000 0000000 00000031662 14125307352 017063 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.util.StringTokenizer;
import org.forester.io.parsers.nhx.NHXFormatException;
import org.forester.io.parsers.nhx.NHXtags;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.io.writers.PhylogenyWriter;
import org.forester.util.ForesterUtil;
public class Event implements PhylogenyData {
public final static int DEFAULT_VALUE = -1;
private static final String NHX_SEPARATOR = ">";
private int _duplications;
private int _speciations;
private int _gene_losses;
private EventType _event_type;
private Confidence _confidence;
public Event() {
_duplications = DEFAULT_VALUE;
_speciations = DEFAULT_VALUE;
_gene_losses = DEFAULT_VALUE;
_event_type = EventType.unassigned;
}
public Event( final EventType type ) {
_duplications = DEFAULT_VALUE;
_speciations = DEFAULT_VALUE;
_gene_losses = DEFAULT_VALUE;
_event_type = type;
}
public Event( final int duplications, final int speciations, final int gene_losses ) {
_duplications = duplications;
_speciations = speciations;
_gene_losses = gene_losses;
_event_type = EventType.mixed;
}
public Event( final int duplications, final int speciations, final int gene_losses, final String type ) {
_duplications = duplications;
_speciations = speciations;
_gene_losses = gene_losses;
_event_type = EventType.valueOf( type );
}
public Event( final String nhx ) throws NHXFormatException {
if ( ForesterUtil.isEmpty( nhx ) ) {
_duplications = DEFAULT_VALUE;
_speciations = DEFAULT_VALUE;
_gene_losses = DEFAULT_VALUE;
_event_type = EventType.unassigned;
}
else {
final StringTokenizer st = new StringTokenizer( nhx, NHX_SEPARATOR );
if ( st.countTokens() != 4 ) {
throw new NHXFormatException( "malformed NHX format for event [" + nhx + "]" );
}
final String duplications = ( String ) st.nextElement();
final String speciations = ( String ) st.nextElement();
final String losses = ( String ) st.nextElement();
final String event_type = ( String ) st.nextElement();
int d = 0;
int s = 0;
int l = 0;
try {
d = Integer.parseInt( duplications );
s = Integer.parseInt( speciations );
l = Integer.parseInt( losses );
_duplications = d;
_speciations = s;
_gene_losses = l;
_event_type = EventType.valueOf( event_type );
}
catch ( final Exception e ) {
throw new NHXFormatException( "malformed NHX format for event [" + nhx + "]:" + e.getMessage() );
}
}
}
@Override
public StringBuffer asSimpleText() {
final StringBuffer sb = new StringBuffer();
if ( isUnassigned() ) {
}
else if ( isSpeciationOrDuplication() ) {
sb.append( "?" );
}
else if ( isOther() || isRoot() || isTransfer() || isFusion() ) {
sb.append( getEventType().toString() );
}
else {
if ( getNumberOfDuplications() > 0 ) {
if ( getNumberOfDuplications() > 1 ) {
sb.append( getNumberOfDuplications() );
}
sb.append( "D" );
}
if ( getNumberOfSpeciations() > 0 ) {
if ( getNumberOfSpeciations() > 1 ) {
sb.append( getNumberOfSpeciations() );
}
sb.append( "S" );
}
if ( getNumberOfGeneLosses() > 0 ) {
if ( getNumberOfGeneLosses() > 1 ) {
sb.append( getNumberOfGeneLosses() );
}
sb.append( "L" );
}
}
return sb;
}
@Override
public StringBuffer asText() {
final StringBuffer sb = new StringBuffer();
if ( isUnassigned() || isSpeciationOrDuplication() || isOther() || isRoot() || isTransfer() || isFusion() ) {
sb.append( getEventType().toString() );
}
else {
if ( isDuplication() ) {
if ( getNumberOfDuplications() == 1 ) {
sb.append( "duplication" );
}
else {
sb.append( "duplications [" + getNumberOfDuplications() + "]" );
}
}
else if ( isSpeciation() ) {
if ( getNumberOfSpeciations() == 1 ) {
sb.append( "speciation" );
}
else {
sb.append( "speciations [" + getNumberOfSpeciations() + "]" );
}
}
else if ( isGeneLoss() ) {
if ( getNumberOfGeneLosses() == 1 ) {
sb.append( "gene-loss" );
}
else {
sb.append( "gene-losses [" + getNumberOfGeneLosses() + "]" );
}
}
else {
sb.append( "duplications [" + getNumberOfDuplications() + "] " );
sb.append( "speciations [" + getNumberOfSpeciations() + "] " );
sb.append( "gene-losses [" + getNumberOfGeneLosses() + "]" );
}
}
return sb;
}
@Override
public PhylogenyData copy() {
if ( isUnassigned() ) {
return new Event();
}
else if ( _event_type != EventType.mixed ) {
return new Event( _event_type );
}
else {
return new Event( _duplications, _speciations, _gene_losses );
}
}
public Confidence getConfidence() {
return _confidence;
}
public EventType getEventType() {
return _event_type;
}
public int getNumberOfDuplications() {
return _duplications;
}
public int getNumberOfGeneLosses() {
return _gene_losses;
}
public int getNumberOfSpeciations() {
return _speciations;
}
/**
* Returns true if this event contains one or more duplications events only
*
* @return true if this event contains one or more duplications events only
*/
public boolean isDuplication() {
return ( _duplications > 0 ) && ( _gene_losses < 1 ) && ( _speciations < 1 );
}
@Override
public boolean isEqual( final PhylogenyData event ) {
if ( ( event == null ) || !( event instanceof Event ) ) {
return false;
}
final Event e = ( Event ) event;
if ( getEventType().compareTo( e.getEventType() ) != 0 ) {
return false;
}
if ( getNumberOfDuplications() != e.getNumberOfDuplications() ) {
return false;
}
if ( getNumberOfSpeciations() != e.getNumberOfSpeciations() ) {
return false;
}
if ( getNumberOfGeneLosses() != e.getNumberOfGeneLosses() ) {
return false;
}
return true;
}
public boolean isFusion() {
return _event_type == EventType.fusion;
}
/**
* Returns true if this event contains one or more gene loss events only
*
* @return true if this event contains one or more gene loss events only
*/
public boolean isGeneLoss() {
return ( _duplications < 1 ) && ( _gene_losses > 0 ) && ( _speciations < 1 );
}
public boolean isOther() {
return _event_type == EventType.other;
}
public boolean isRoot() {
return _event_type == EventType.root;
}
/**
* Returns true if this event contains one or more speciation events only
*
* @return true if this event contains one or more speciation events only
*/
public boolean isSpeciation() {
return ( _duplications < 1 ) && ( _gene_losses < 1 ) && ( _speciations > 0 );
}
public boolean isSpeciationOrDuplication() {
return _event_type == EventType.speciation_or_duplication;
}
public boolean isTransfer() {
return _event_type == EventType.transfer;
}
public boolean isUnassigned() {
return ( _duplications == DEFAULT_VALUE ) && ( _event_type == EventType.unassigned );
}
public void setConfidence( final Confidence confidence ) {
_confidence = confidence;
}
public void setDuplications( final int duplications ) {
_duplications = duplications;
_event_type = EventType.mixed;
}
public void setGeneLosses( final int gene_losses ) {
_gene_losses = gene_losses;
_event_type = EventType.mixed;
}
public void setSpeciations( final int speciations ) {
_speciations = speciations;
_event_type = EventType.mixed;
}
@Override
public StringBuffer toNHX() {
final StringBuffer sb = new StringBuffer();
if ( !isUnassigned() && ( isSpeciationOrDuplication() || isDuplication() || isSpeciation() ) ) {
sb.append( ":" );
sb.append( NHXtags.IS_DUPLICATION );
if ( isSpeciationOrDuplication() ) {
sb.append( "?" );
}
else if ( isDuplication() ) {
sb.append( "Y" );
}
else if ( isSpeciation() ) {
sb.append( "N" );
}
}
return sb;
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.EVENTS );
if ( ( getEventType() != EventType.unassigned ) && ( getEventType() != EventType.mixed ) ) {
PhylogenyDataUtil
.appendElement( writer, PhyloXmlMapping.EVENT_TYPE, getEventType().toString(), indentation );
}
if ( getNumberOfDuplications() > 0 ) {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.EVENT_DUPLICATIONS,
getNumberOfDuplications() + "",
indentation );
}
if ( getNumberOfSpeciations() > 0 ) {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.EVENT_SPECIATIONS,
getNumberOfSpeciations() + "",
indentation );
}
if ( getNumberOfGeneLosses() > 0 ) {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.EVENT_LOSSES,
getNumberOfGeneLosses() + "",
indentation );
}
if ( getConfidence() != null ) {
getConfidence().toPhyloXML( writer, level, indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.EVENTS );
}
@Override
public String toString() {
return asText().toString();
}
public static Event createSingleDuplicationEvent() {
return new Event( 1, 0, 0 );
}
public static Event createSingleSpeciationEvent() {
return new Event( 0, 1, 0 );
}
public static Event createSingleSpeciationOrDuplicationEvent() {
return new Event( EventType.speciation_or_duplication );
}
public static enum EventType {
transfer, fusion, root, speciation_or_duplication, other, mixed, unassigned
}
}
org/forester/phylogeny/data/Confidence.java 0000664 0000000 0000000 00000015403 14125307352 020032 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import org.forester.io.parsers.nhx.NHXtags;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
import org.forester.util.ForesterUtil;
public class Confidence implements PhylogenyData, Comparable {
public final static int CONFIDENCE_DEFAULT_VALUE = -9999;
private double _value;
private double _sd;
private String _type;
public final static NumberFormat FORMATTER;
static {
final DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator( '.' );
FORMATTER = new DecimalFormat( "#.#########", dfs );
FORMATTER.setMaximumFractionDigits( PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT );
}
public Confidence() {
init();
}
public Confidence( final double value, final String type ) {
setValue( value );
setType( type );
setStandardDeviation( CONFIDENCE_DEFAULT_VALUE );
}
public Confidence( final double value, final String type, final double sd ) {
setValue( value );
setType( type );
setStandardDeviation( sd );
}
@Override
public StringBuffer asSimpleText() {
return new StringBuffer().append( ForesterUtil.FORMATTER_6.format( getValue() ) );
}
@Override
public StringBuffer asText() {
final StringBuffer sb = new StringBuffer();
if ( !ForesterUtil.isEmpty( getType() ) ) {
sb.append( "[" );
sb.append( getType() );
sb.append( "] " );
}
sb.append( ForesterUtil.FORMATTER_6.format( getValue() ) );
if ( getStandardDeviation() != CONFIDENCE_DEFAULT_VALUE ) {
sb.append( " (sd=" );
sb.append( getStandardDeviation() );
sb.append( ")" );
}
return sb;
}
@Override
public int compareTo( final Confidence confidence ) {
if ( this == confidence ) {
return 0;
}
return getType().compareToIgnoreCase( confidence.getType() );
}
@Override
public PhylogenyData copy() {
return new Confidence( getValue(), getType(), getStandardDeviation() );
}
public String getType() {
return _type;
}
public double getValue() {
return _value;
}
public double getStandardDeviation() {
return _sd;
}
public void init() {
setValue( CONFIDENCE_DEFAULT_VALUE );
setType( "" );
setStandardDeviation( CONFIDENCE_DEFAULT_VALUE );
}
@Override
public boolean isEqual( final PhylogenyData confidence ) {
if ( confidence == null ) {
return false;
}
if ( !( confidence instanceof Confidence ) ) {
return false;
}
final Confidence s = ( Confidence ) confidence;
if ( s.getValue() != getValue() ) {
return false;
}
if ( !s.getType().equals( getType() ) ) {
return false;
}
return true;
}
public void setType( final String type ) {
_type = type;
}
public void setValue( final double value ) {
_value = value;
}
public void setStandardDeviation( final double sd ) {
_sd = sd;
}
@Override
public StringBuffer toNHX() {
final StringBuffer sb = new StringBuffer();
sb.append( NHXtags.SUPPORT );
sb.append( FORMATTER.format( ForesterUtil.round( getValue(),
PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ) );
return sb;
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
if ( getValue() == CONFIDENCE_DEFAULT_VALUE ) {
return;
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
if ( getStandardDeviation() != CONFIDENCE_DEFAULT_VALUE ) {
PhylogenyDataUtil
.appendElement( writer,
PhyloXmlMapping.CONFIDENCE,
FORMATTER.format( ForesterUtil
.round( getValue(), PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ),
PhyloXmlMapping.CONFIDENCE_TYPE_ATTR,
ForesterUtil.isEmpty( getType() ) ? "unknown" : getType(),
PhyloXmlMapping.CONFIDENCE_SD_ATTR,
String.valueOf( ForesterUtil
.round( getStandardDeviation(),
PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ) );
}
else {
PhylogenyDataUtil
.appendElement( writer,
PhyloXmlMapping.CONFIDENCE,
FORMATTER.format( ForesterUtil
.round( getValue(), PhyloXmlUtil.ROUNDING_DIGITS_FOR_PHYLOXML_DOUBLE_OUTPUT ) ),
PhyloXmlMapping.CONFIDENCE_TYPE_ATTR,
ForesterUtil.isEmpty( getType() ) ? "unknown" : getType() );
}
}
@Override
public String toString() {
return asText().toString();
}
}
org/forester/phylogeny/data/Identifier.java 0000664 0000000 0000000 00000011301 14125307352 020050 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.util.ForesterUtil;
public final class Identifier implements PhylogenyData {
final private String _value;
final private String _provider;
final private String _value_provider;
public Identifier() {
_value = "";
_provider = "";
_value_provider = "";
}
public Identifier( final String value ) {
_value = value;
_provider = "";
_value_provider = value;
}
public Identifier( final String value, final String provider ) {
_value = value;
_provider = provider;
if ( provider != null ) {
_value_provider = value + provider;
}
else {
_value_provider = value;
}
}
@Override
public StringBuffer asSimpleText() {
return new StringBuffer( getValue() );
}
@Override
public StringBuffer asText() {
final StringBuffer sb = new StringBuffer();
if ( !ForesterUtil.isEmpty( getProvider() ) ) {
sb.append( "[" );
sb.append( getProvider() );
sb.append( "] " );
}
sb.append( getValue() );
return sb;
}
public String getValuePlusProvider() {
return _value_provider;
}
@Override
public PhylogenyData copy() {
return new Identifier( getValue(), getProvider() );
}
@Override
public boolean equals( final Object o ) {
if ( this == o ) {
return true;
}
else if ( o == null ) {
return false;
}
else if ( o.getClass() != this.getClass() ) {
throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
+ o.getClass() + "]" );
}
else {
return isEqual( ( Identifier ) o );
}
}
public String getProvider() {
return _provider;
}
public String getValue() {
return _value;
}
@Override
public int hashCode() {
return _value_provider.hashCode();
}
@Override
public boolean isEqual( final PhylogenyData data ) {
if ( this == data ) {
return true;
}
if ( ( data == null ) || ( getValue() == null ) ) {
return false;
}
final Identifier a = ( Identifier ) data;
if ( ( getProvider() != null ) && ( a.getProvider() != null ) ) {
return ( a.getValue().equals( getValue() ) && a.getProvider().equals( getProvider() ) );
}
return ( a.getValue().equals( getValue() ) );
}
@Override
public StringBuffer toNHX() {
throw new UnsupportedOperationException();
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
if ( !org.forester.util.ForesterUtil.isEmpty( getProvider() ) ) {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.IDENTIFIER,
getValue(),
PhyloXmlMapping.IDENTIFIER_PROVIDER_ATTR,
getProvider(),
indentation );
}
else {
PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.IDENTIFIER, getValue(), indentation );
}
}
@Override
public String toString() {
return asText().toString();
}
}
org/forester/phylogeny/data/Date.java 0000664 0000000 0000000 00000013551 14125307352 016654 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// Copyright (C) 2000-2001 Washington University School of Medicine
// and Howard Hughes Medical Institute
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.util.ForesterUtil;
public class Date implements PhylogenyData {
private String _desc;
private BigDecimal _value;
private BigDecimal _min;
private BigDecimal _max;
private String _unit;
public Date() {
_desc = "";
_value = null;
_min = null;
_max = null;
_unit = "";
}
public Date( final String desc ) {
if ( desc == null ) {
throw new IllegalArgumentException( "illegaly empty of null fields in constructor" );
}
_desc = desc;
_value = null;
_min = null;
_max = null;
_unit = "";
}
public Date( final String desc,
final BigDecimal value,
final BigDecimal min,
final BigDecimal max,
final String unit ) {
if ( ( desc == null ) || ( unit == null ) ) {
throw new IllegalArgumentException( "illegaly empty of null fields in constructor" );
}
_desc = desc;
_value = value;
_min = min;
_max = max;
_unit = unit;
}
@Override
public StringBuffer asSimpleText() {
if ( getValue() != null ) {
return new StringBuffer( getDesc() + " [" + getValue().toPlainString() + " " + getUnit() + "]" );
}
else {
return new StringBuffer( getDesc() );
}
}
@Override
public StringBuffer asText() {
return asSimpleText();
}
@Override
public PhylogenyData copy() {
return new Date( getDesc(),
getValue() == null ? null : new BigDecimal( getValue().toPlainString() ),
getMin() == null ? null : new BigDecimal( getMin().toPlainString() ),
getMax() == null ? null : new BigDecimal( getMax().toPlainString() ),
getUnit() );
}
public String getDesc() {
return _desc;
}
public BigDecimal getMax() {
return _max;
}
public BigDecimal getMin() {
return _min;
}
public String getUnit() {
return _unit;
}
public BigDecimal getValue() {
return _value;
}
@Override
public boolean isEqual( final PhylogenyData data ) {
throw new UnsupportedOperationException();
}
public void setDesc( final String desc ) {
_desc = desc;
}
public void setMax( final BigDecimal max ) {
_max = max;
}
public void setMin( final BigDecimal min ) {
_min = min;
}
public void setUnit( final String unit ) {
_unit = unit;
}
public void setValue( final BigDecimal value ) {
_value = value;
}
@Override
public StringBuffer toNHX() {
throw new UnsupportedOperationException();
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.CLADE_DATE, PhyloXmlMapping.CLADE_DATE_UNIT, getUnit() );
if ( !ForesterUtil.isEmpty( getDesc() ) ) {
PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.CLADE_DATE_DESC, getDesc(), indentation );
}
if ( getValue() != null ) {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.CLADE_DATE_VALUE,
getValue().toPlainString(),
indentation );
}
if ( getMin() != null ) {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.CLADE_DATE_MIN,
getMin().toPlainString(),
indentation );
}
if ( getMax() != null ) {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.CLADE_DATE_MAX,
getMax().toPlainString(),
indentation );
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.CLADE_DATE );
}
@Override
public String toString() {
return asSimpleText().toString();
}
} org/forester/phylogeny/data/BinaryCharacters.java 0000664 0000000 0000000 00000030740 14125307352 021222 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.io.writers.PhylogenyWriter;
import org.forester.util.ForesterUtil;
public class BinaryCharacters implements PhylogenyData {
public final static int COUNT_DEFAULT = -1;
private final SortedSet _present;
private final SortedSet _gained;
private final SortedSet _lost;
private final int _present_count;
private final int _gained_count;
private final int _lost_count;
private String _type;
public BinaryCharacters() {
_present = new TreeSet();
_gained = new TreeSet();
_lost = new TreeSet();
_present_count = COUNT_DEFAULT;
_gained_count = COUNT_DEFAULT;
_lost_count = COUNT_DEFAULT;
}
public BinaryCharacters( final SortedSet present_characters,
final SortedSet gained_characters,
final SortedSet lost_characters,
final String type ) {
_present = present_characters;
_gained = gained_characters;
_lost = lost_characters;
_type = type;
_present_count = COUNT_DEFAULT;
_gained_count = COUNT_DEFAULT;
_lost_count = COUNT_DEFAULT;
}
public BinaryCharacters( final SortedSet present_characters,
final SortedSet gained_characters,
final SortedSet lost_characters,
final String type,
final int present_count,
final int gained_count,
final int lost_count ) {
_present = present_characters;
_gained = gained_characters;
_lost = lost_characters;
_type = type;
_present_count = present_count;
_gained_count = gained_count;
_lost_count = lost_count;
validate();
}
private void addCharacters( final String indentation, final Writer w, final String[] present ) throws IOException {
for( final String string : present ) {
PhylogenyDataUtil.appendElement( w, PhyloXmlMapping.BINARY_CHARACTER, string, indentation );
}
}
public void addGainedCharacter( final String binary_character ) {
if ( getLostCharacters().contains( binary_character ) ) {
throw new IllegalArgumentException( "attempt to add binary character [" + binary_character
+ "] to gained characters but is already listed as lost" );
}
getGainedCharacters().add( binary_character );
}
public void addLostCharacter( final String binary_character ) {
if ( getPresentCharacters().contains( binary_character ) ) {
throw new IllegalArgumentException( "attempt to add binary character [" + binary_character
+ "] to lost characters but is already listed as present" );
}
if ( getGainedCharacters().contains( binary_character ) ) {
throw new IllegalArgumentException( "attempt to add binary character [" + binary_character
+ "] to lost characters but is already listed as gained" );
}
getLostCharacters().add( binary_character );
}
public void addPresentCharacter( final String binary_character ) {
if ( getLostCharacters().contains( binary_character ) ) {
throw new IllegalArgumentException( "attempt to add binary character [" + binary_character
+ "] to present characters but is already listed as lost" );
}
getPresentCharacters().add( binary_character );
}
@Override
public StringBuffer asSimpleText() {
return asText();
}
@Override
public StringBuffer asText() {
validate();
final StringBuffer sb = new StringBuffer();
sb.append( "present [" );
sb.append( getPresentCount() );
sb.append( "]: " );
sb.append( getPresentCharactersAsStringBuffer() );
sb.append( ForesterUtil.LINE_SEPARATOR );
sb.append( "gained [ " );
sb.append( getGainedCount() );
sb.append( "]: " );
sb.append( getGainedCharactersAsStringBuffer() );
sb.append( ForesterUtil.LINE_SEPARATOR );
sb.append( "lost [" );
sb.append( getLostCount() );
sb.append( "]: " );
sb.append( getLostCharactersAsStringBuffer() );
return sb;
}
@Override
/**
* Not a deep copy.
*
*/
public PhylogenyData copy() {
validate();
return new BinaryCharacters( getPresentCharacters(),
getGainedCharacters(),
getLostCharacters(),
getType(),
getPresentCount(),
getGainedCount(),
getLostCount() );
}
public SortedSet getGainedCharacters() {
return _gained;
}
public String[] getGainedCharactersAsStringArray() {
return sortedSetToStringArray( getGainedCharacters() );
}
public StringBuffer getGainedCharactersAsStringBuffer() {
return sortedSetToStringBuffer( getGainedCharacters(), " " );
}
public int getGainedCount() {
return _gained_count;
}
public SortedSet getLostCharacters() {
return _lost;
}
public String[] getLostCharactersAsStringArray() {
return sortedSetToStringArray( getLostCharacters() );
}
public StringBuffer getLostCharactersAsStringBuffer() {
return sortedSetToStringBuffer( getLostCharacters(), " " );
}
public int getLostCount() {
return _lost_count;
}
public SortedSet getPresentCharacters() {
return _present;
}
public String[] getPresentCharactersAsStringArray() {
return sortedSetToStringArray( getPresentCharacters() );
}
public StringBuffer getPresentCharactersAsStringBuffer() {
return sortedSetToStringBuffer( getPresentCharacters(), " " );
}
public int getPresentCount() {
return _present_count;
}
public String getType() {
return _type;
}
@Override
public boolean isEqual( final PhylogenyData data ) {
throw new UnsupportedOperationException();
}
public void setType( final String type ) {
_type = type;
}
@Override
public StringBuffer toNHX() {
throw new UnsupportedOperationException();
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
validate();
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendOpen( writer,
PhyloXmlMapping.BINARY_CHARACTERS,
PhyloXmlMapping.BINARY_CHARACTERS_TYPE_ATTR,
getType(),
PhyloXmlMapping.BINARY_CHARACTERS_GAINED_COUNT_ATTR,
getGainedCount() != COUNT_DEFAULT ? String.valueOf( getGainedCount() ) : "",
PhyloXmlMapping.BINARY_CHARACTERS_LOST_COUNT_ATTR,
getLostCount() != COUNT_DEFAULT ? String.valueOf( getLostCount() ) : "",
PhyloXmlMapping.BINARY_CHARACTERS_PRESENT_COUNT_ATTR,
getPresentCount() != COUNT_DEFAULT ? String.valueOf( getPresentCount() ) : "" );
final String my_ind = indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE;
if ( getGainedCharacters().size() > 0 ) {
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( my_ind );
PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.BINARY_CHARACTERS_GAINED );
addCharacters( my_ind, writer, getGainedCharactersAsStringArray() );
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( my_ind );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.BINARY_CHARACTERS_GAINED );
}
if ( getLostCharacters().size() > 0 ) {
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( my_ind );
PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.BINARY_CHARACTERS_LOST );
addCharacters( my_ind, writer, getLostCharactersAsStringArray() );
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( my_ind );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.BINARY_CHARACTERS_LOST );
}
if ( getPresentCharacters().size() > 0 ) {
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( my_ind );
PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.BINARY_CHARACTERS_PRESENT );
addCharacters( my_ind, writer, getPresentCharactersAsStringArray() );
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( my_ind );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.BINARY_CHARACTERS_PRESENT );
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.BINARY_CHARACTERS );
}
@Override
public String toString() {
return asText().toString();
}
private void validate() {
if ( ( getPresentCount() != COUNT_DEFAULT ) && ( getPresentCharacters().size() > 0 )
&& ( getPresentCount() != getPresentCharacters().size() ) ) {
throw new RuntimeException( "present characters size and count are unequal" );
}
if ( ( getGainedCount() != COUNT_DEFAULT ) && ( getGainedCharacters().size() > 0 )
&& ( getGainedCount() != getGainedCharacters().size() ) ) {
throw new RuntimeException( "gained characters size and count are unequal" );
}
if ( ( getLostCount() != COUNT_DEFAULT ) && ( getLostCharacters().size() > 0 )
&& ( getLostCount() != getLostCharacters().size() ) ) {
throw new RuntimeException( "lost characters size and count are unequal" );
}
}
private static String[] sortedSetToStringArray( final SortedSet set ) {
final String[] chars = new String[ set.size() ];
final Iterator it = set.iterator();
int i = 0;
while ( it.hasNext() ) {
chars[ i++ ] = it.next();
}
return chars;
}
private static StringBuffer sortedSetToStringBuffer( final SortedSet set, final String separator ) {
final StringBuffer sb = new StringBuffer();
final Iterator it = set.iterator();
while ( it.hasNext() ) {
sb.append( it.next() );
if ( it.hasNext() ) {
sb.append( separator );
}
}
return sb;
}
}
org/forester/phylogeny/data/BranchData.java 0000664 0000000 0000000 00000010735 14125307352 017767 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// Copyright (C) 2000-2001 Washington University School of Medicine
// and Howard Hughes Medical Institute
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
public class BranchData implements PhylogenyData {
private BranchColor _branch_color;
private List _confidences;
private BranchWidth _branch_width;
public BranchData() {
// Doing nothing.
}
public void addConfidence( final Confidence confidence ) {
getConfidences().add( confidence );
}
@Override
public StringBuffer asSimpleText() {
throw new UnsupportedOperationException();
}
@Override
public StringBuffer asText() {
throw new UnsupportedOperationException();
}
@Override
public PhylogenyData copy() {
final BranchData new_bd = new BranchData();
if ( isHasBranchColor() ) {
new_bd.setBranchColor( ( BranchColor ) getBranchColor().copy() );
}
if ( isHasBranchWidth() ) {
new_bd.setBranchWidth( ( BranchWidth ) getBranchWidth().copy() );
}
if ( isHasConfidences() ) {
for( final Confidence confidence : getConfidences() ) {
new_bd.addConfidence( ( Confidence ) confidence.copy() );
}
}
return new_bd;
}
public BranchColor getBranchColor() {
return _branch_color;
}
public BranchWidth getBranchWidth() {
return _branch_width;
}
public Confidence getConfidence( final int index ) {
return getConfidences().get( index );
}
public List getConfidences() {
if ( _confidences == null ) {
_confidences = new ArrayList();
}
return _confidences;
}
public int getNumberOfConfidences() {
return getConfidences().size();
}
@Override
public boolean isEqual( final PhylogenyData data ) {
throw new UnsupportedOperationException();
}
public boolean isHasBranchColor() {
return getBranchColor() != null;
}
public boolean isHasBranchWidth() {
return getBranchWidth() != null;
}
public boolean isHasConfidences() {
return getNumberOfConfidences() > 0;
}
public void setBranchColor( final BranchColor branch_color ) {
_branch_color = branch_color;
}
public void setBranchWidth( final BranchWidth branch_width ) {
_branch_width = branch_width;
}
@Override
public StringBuffer toNHX() {
final StringBuffer sb = new StringBuffer();
if ( isHasConfidences() && ( getConfidence( 0 ).getValue() != Confidence.CONFIDENCE_DEFAULT_VALUE ) ) {
sb.append( ":" );
sb.append( getConfidence( 0 ).toNHX() );
}
return sb;
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
if ( isHasConfidences() ) {
for( final Confidence confidence : getConfidences() ) {
confidence.toPhyloXML( writer, level, indentation );
}
}
if ( isHasBranchWidth() ) {
getBranchWidth().toPhyloXML( writer, level, indentation );
}
if ( isHasBranchColor() ) {
getBranchColor().toPhyloXML( writer, level, indentation );
}
}
}
org/forester/phylogeny/data/Taxonomy.java 0000664 0000000 0000000 00000040423 14125307352 017613 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.forester.io.parsers.nhx.NHXtags;
import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
import org.forester.util.ForesterUtil;
public class Taxonomy implements PhylogenyData, MultipleUris, Comparable {
private String _scientific_name;
private String _common_name;
private List _synonyms;
private String _authority;
private Identifier _identifier;
private String _taxonomy_code;
private String _rank;
private List _uris;
private List _lineage;
public Taxonomy() {
init();
}
@Override
public StringBuffer asSimpleText() {
return asText();
}
@Override
public Uri getUri( final int index ) {
return getUris().get( index );
}
@Override
public void addUri( final Uri uri ) {
if ( getUris() == null ) {
setUris( new ArrayList() );
}
getUris().add( uri );
}
@Override
public StringBuffer asText() {
final StringBuffer sb = new StringBuffer();
if ( getIdentifier() != null ) {
sb.append( "[" );
sb.append( getIdentifier().asSimpleText() );
sb.append( "]" );
}
if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
if ( sb.length() > 0 ) {
sb.append( " " );
}
sb.append( "[" );
sb.append( getTaxonomyCode() );
sb.append( "]" );
}
if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
if ( sb.length() > 0 ) {
sb.append( " " );
}
sb.append( getScientificName() );
if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
sb.append( " (" );
sb.append( getAuthority() );
sb.append( ")" );
}
}
if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
if ( sb.length() > 0 ) {
sb.append( " " );
}
sb.append( getCommonName() );
}
return sb;
}
@Override
public PhylogenyData copy() {
final Taxonomy t = new Taxonomy();
try {
t.setTaxonomyCode( getTaxonomyCode() );
}
catch ( final PhyloXmlDataFormatException e ) {
e.printStackTrace();
}
t.setScientificName( getScientificName() );
t.setCommonName( getCommonName() );
t.setAuthority( getAuthority() );
for( final String syn : getSynonyms() ) {
t.getSynonyms().add( syn );
}
if ( getIdentifier() != null ) {
t.setIdentifier( ( Identifier ) getIdentifier().copy() );
}
else {
t.setIdentifier( null );
}
try {
t.setRank( new String( getRank() ) );
}
catch ( final PhyloXmlDataFormatException e ) {
e.printStackTrace();
}
if ( getUris() != null ) {
t.setUris( new ArrayList() );
for( final Uri uri : getUris() ) {
if ( uri != null ) {
t.getUris().add( uri );
}
}
}
if ( getLineage() != null ) {
t.setLineage( new ArrayList() );
for( final String l : getLineage() ) {
if ( l != null ) {
t.getLineage().add( l );
}
}
}
return t;
}
@Override
public boolean equals( final Object o ) {
if ( this == o ) {
return true;
}
else if ( o == null ) {
return false;
}
else if ( o.getClass() != this.getClass() ) {
throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
+ o.getClass() + "]" );
}
else {
return isEqual( ( Taxonomy ) o );
}
}
public String getAuthority() {
return _authority;
}
public String getCommonName() {
return _common_name;
}
public Identifier getIdentifier() {
return _identifier;
}
public String getRank() {
return _rank;
}
public String getScientificName() {
return _scientific_name;
}
public List getSynonyms() {
if ( _synonyms == null ) {
_synonyms = new ArrayList();
}
return _synonyms;
}
public String getTaxonomyCode() {
return _taxonomy_code;
}
@Override
public List getUris() {
return _uris;
}
@Override
public int hashCode() {
if ( ( getIdentifier() != null ) && !ForesterUtil.isEmpty( getIdentifier().getValue() ) ) {
return getIdentifier().hashCode();
}
else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
return getTaxonomyCode().hashCode();
}
else if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
return ( getScientificName().toLowerCase() + getAuthority().toLowerCase() ).hashCode();
}
return getScientificName().toLowerCase().hashCode();
}
else {
return getCommonName().toLowerCase().hashCode();
}
}
public void init() {
setScientificName( "" );
setCommonName( "" );
setIdentifier( null );
try {
setRank( "" );
}
catch ( final PhyloXmlDataFormatException e ) {
e.printStackTrace();
}
try {
setTaxonomyCode( "" );
}
catch ( final PhyloXmlDataFormatException e ) {
e.printStackTrace();
}
setAuthority( "" );
setSynonyms( null );
setUris( null );
setLineage( null );
}
public boolean isEmpty() {
return ( ( getIdentifier() == null ) && ForesterUtil.isEmpty( getTaxonomyCode() )
&& ForesterUtil.isEmpty( getCommonName() ) && ForesterUtil.isEmpty( getScientificName() ) && ForesterUtil
.isEmpty( _lineage ) );
}
/**
*
* If this and taxonomy 'data' has an identifier, comparison will be based on that.
* Otherwise, if this and taxonomy 'data' has a code, comparison will be based on that.
* Otherwise, if Taxonomy 'data' has a scientific name, comparison will be
* based on that (case insensitive!).
* Otherwise, if Taxonomy 'data' has a common name, comparison will be
* based on that (case insensitive!).
* (Note. This is important and should not be change without a very good reason.)
*
*/
@Override
public boolean isEqual( final PhylogenyData data ) {
if ( this == data ) {
return true;
}
final Taxonomy tax = ( Taxonomy ) data;
if ( ( getIdentifier() != null ) && ( tax.getIdentifier() != null )
&& !ForesterUtil.isEmpty( getIdentifier().getValue() )
&& !ForesterUtil.isEmpty( tax.getIdentifier().getValue() ) ) {
return getIdentifier().isEqual( tax.getIdentifier() );
}
else if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) && !ForesterUtil.isEmpty( tax.getTaxonomyCode() ) ) {
return getTaxonomyCode().equals( tax.getTaxonomyCode() );
}
else if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( tax.getScientificName() ) ) {
if ( !ForesterUtil.isEmpty( getAuthority() ) && !ForesterUtil.isEmpty( tax.getAuthority() ) ) {
return ( getScientificName().equalsIgnoreCase( tax.getScientificName() ) )
&& ( getAuthority().equalsIgnoreCase( tax.getAuthority() ) );
}
return getScientificName().equalsIgnoreCase( tax.getScientificName() );
}
else if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( tax.getCommonName() ) ) {
return getCommonName().equalsIgnoreCase( tax.getCommonName() );
}
//throw new RuntimeException( "comparison not possible with empty fields" );
return false;
}
public void setAuthority( final String authority ) {
_authority = authority;
}
public void setCommonName( final String common_name ) {
_common_name = common_name;
}
public void setIdentifier( final Identifier identifier ) {
_identifier = identifier;
}
public void setRank( final String rank ) throws PhyloXmlDataFormatException {
if ( !ForesterUtil.isEmpty( rank ) && !PhyloXmlUtil.TAXONOMY_RANKS_SET.contains( rank ) ) {
throw new PhyloXmlDataFormatException( "illegal rank: [" + rank + "]" );
}
_rank = rank;
}
public void setScientificName( final String scientific_name ) {
_scientific_name = scientific_name;
}
private void setSynonyms( final List synonyms ) {
_synonyms = synonyms;
}
public void setTaxonomyCode( String taxonomy_code ) throws PhyloXmlDataFormatException {
if ( !ForesterUtil.isEmpty( taxonomy_code )
&& !PhyloXmlUtil.TAXOMONY_CODE_PATTERN.matcher( taxonomy_code ).matches() ) {
throw new PhyloXmlDataFormatException( "illegal taxonomy code: [" + taxonomy_code + "]" );
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//TODO FIXME (added on 13-11-18) remove me eventually
if ( taxonomy_code.equals( "ACIBL" ) ) {
taxonomy_code = "KORVE";
}
else if ( taxonomy_code.equals( "PYRKO" ) ) {
taxonomy_code = "THEKO";
}
//TODO FIXME (added on 13-11-18) remove me eventually
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_taxonomy_code = taxonomy_code;
}
@Override
public void setUris( final List uris ) {
_uris = uris;
}
@Override
public StringBuffer toNHX() {
final StringBuffer sb = new StringBuffer();
if ( getIdentifier() != null ) {
sb.append( ':' + NHXtags.TAXONOMY_ID );
sb.append( ForesterUtil.replaceIllegalNhxCharacters( getIdentifier().getValue() ) );
}
final StringBuffer species = new StringBuffer();
if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
species.append( ForesterUtil.replaceIllegalNhxCharacters( getTaxonomyCode() ) );
}
if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
ForesterUtil.appendSeparatorIfNotEmpty( species, '|' );
species.append( ForesterUtil.replaceIllegalNhxCharacters( getScientificName() ) );
}
if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
ForesterUtil.appendSeparatorIfNotEmpty( species, '|' );
species.append( ForesterUtil.replaceIllegalNhxCharacters( getCommonName() ) );
}
if ( species.length() > 0 ) {
sb.append( ':' + NHXtags.SPECIES_NAME );
sb.append( species );
}
return sb;
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
if ( isEmpty() ) {
return;
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.TAXONOMY );
if ( ( getIdentifier() != null ) && !ForesterUtil.isEmpty( getIdentifier().getValue() ) ) {
getIdentifier().toPhyloXML( writer, level, indentation );
}
if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) ) {
PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_CODE, getTaxonomyCode(), indentation );
}
if ( !ForesterUtil.isEmpty( getScientificName() ) ) {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.TAXONOMY_SCIENTIFIC_NAME,
getScientificName(),
indentation );
}
if ( !ForesterUtil.isEmpty( getAuthority() ) ) {
PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_AUTHORITY, getAuthority(), indentation );
}
if ( !ForesterUtil.isEmpty( getCommonName() ) ) {
PhylogenyDataUtil
.appendElement( writer, PhyloXmlMapping.TAXONOMY_COMMON_NAME, getCommonName(), indentation );
}
if ( _synonyms != null ) {
for( final String syn : getSynonyms() ) {
if ( !ForesterUtil.isEmpty( syn ) ) {
PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_SYNONYM, syn, indentation );
}
}
}
if ( !ForesterUtil.isEmpty( getRank() ) ) {
PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.TAXONOMY_RANK, getRank(), indentation );
}
if ( getUris() != null ) {
for( final Uri uri : getUris() ) {
if ( uri != null ) {
uri.toPhyloXML( writer, level, indentation );
}
}
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.TAXONOMY );
}
@Override
public String toString() {
return asText().toString();
}
@Override
public int compareTo( final Taxonomy o ) {
if ( equals( o ) ) {
return 0;
}
if ( ( getIdentifier() != null ) && ( o.getIdentifier() != null )
&& !ForesterUtil.isEmpty( getIdentifier().getValue() )
&& !ForesterUtil.isEmpty( o.getIdentifier().getValue() ) ) {
final int x = getIdentifier().getValuePlusProvider().compareTo( o.getIdentifier().getValuePlusProvider() );
if ( x != 0 ) {
return x;
}
}
if ( !ForesterUtil.isEmpty( getScientificName() ) && !ForesterUtil.isEmpty( o.getScientificName() ) ) {
return getScientificName().compareToIgnoreCase( o.getScientificName() );
}
if ( !ForesterUtil.isEmpty( getCommonName() ) && !ForesterUtil.isEmpty( o.getCommonName() ) ) {
return getCommonName().compareToIgnoreCase( o.getCommonName() );
}
if ( !ForesterUtil.isEmpty( getTaxonomyCode() ) && !ForesterUtil.isEmpty( o.getTaxonomyCode() ) ) {
return getTaxonomyCode().compareToIgnoreCase( o.getTaxonomyCode() );
}
if ( ( getIdentifier() != null ) && ( o.getIdentifier() != null )
&& !ForesterUtil.isEmpty( getIdentifier().getValue() )
&& !ForesterUtil.isEmpty( o.getIdentifier().getValue() ) ) {
return getIdentifier().getValuePlusProvider().compareTo( o.getIdentifier().getValuePlusProvider() );
}
return 1;
}
public void setLineage( final List lineage ) {
_lineage = lineage;
}
public List getLineage() {
return _lineage;
}
}
org/forester/phylogeny/data/SequenceRelation.java 0000664 0000000 0000000 00000013345 14125307352 021246 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.Map;
public class SequenceRelation implements PhylogenyData {
//public final static Map typesToNames = new LinkedHashMap();
public final static Map typesToNames = new LinkedHashMap();
public final static String SEQUENCE_RELATION_TYPE_ORTHOLOGY = "orthology";
public final static String SEQUENCE_RELATION_TYPE_ONE_TO_ONE_ORTHOLOGY = "one_to_one_orthology";
public final static String SEQUENCE_RELATION_TYPE_SUPER_ORTHOLOGY = "super_orthology";
public final static String SEQUENCE_RELATION_TYPE_PARALOGY = "paralogy";
public final static String SEQUENCE_RELATION_TYPE_ULTRA_PARALOGY = "ultra_paralogy";
public final static String SEQUENCE_RELATION_TYPE_XENOLOGY = "xenology";
public final static String SEQUENCE_RELATION_TYPE_UNKNOWN = "unknown";
public final static String SEQUENCE_RELATION_TYPE_OTHER = "other";
private Sequence ref0;
private Sequence ref1;
private SEQUENCE_RELATION_TYPE type;
private Double distance;
private Confidence confidence;
static {
typesToNames.put( SEQUENCE_RELATION_TYPE.orthology, SEQUENCE_RELATION_TYPE_ORTHOLOGY );
typesToNames.put( SEQUENCE_RELATION_TYPE.one_to_one_orthology, SEQUENCE_RELATION_TYPE_ONE_TO_ONE_ORTHOLOGY );
typesToNames.put( SEQUENCE_RELATION_TYPE.super_orthology, SEQUENCE_RELATION_TYPE_SUPER_ORTHOLOGY );
typesToNames.put( SEQUENCE_RELATION_TYPE.paralogy, SEQUENCE_RELATION_TYPE_PARALOGY );
typesToNames.put( SEQUENCE_RELATION_TYPE.ultra_paralogy, SEQUENCE_RELATION_TYPE_ULTRA_PARALOGY );
typesToNames.put( SEQUENCE_RELATION_TYPE.xenology, SEQUENCE_RELATION_TYPE_XENOLOGY );
typesToNames.put( SEQUENCE_RELATION_TYPE.unknown, SEQUENCE_RELATION_TYPE_UNKNOWN );
typesToNames.put( SEQUENCE_RELATION_TYPE.other, SEQUENCE_RELATION_TYPE_OTHER );
}
@Override
public StringBuffer asSimpleText() {
// TODO Auto-generated method stub
return null;
}
@Override
public StringBuffer asText() {
// TODO Auto-generated method stub
return null;
}
@Override
public PhylogenyData copy() {
// TODO Auto-generated method stub
return null;
}
public Confidence getConfidence() {
return confidence;
}
public Double getDistance() {
return distance;
}
public Sequence getRef0() {
return ref0;
}
public Sequence getRef1() {
return ref1;
}
public SEQUENCE_RELATION_TYPE getType() {
return type;
}
@Override
public boolean isEqual( final PhylogenyData data ) {
// TODO Auto-generated method stub
return false;
}
public void setConfidence( final Confidence confidence ) {
this.confidence = confidence;
}
public void setDistance( final Double distance ) {
this.distance = distance;
}
public void setRef0( final Sequence ref0 ) {
this.ref0 = ref0;
}
public void setRef1( final Sequence ref1 ) {
this.ref1 = ref1;
}
public void setType( final SEQUENCE_RELATION_TYPE type ) {
this.type = type;
}
@Override
public StringBuffer toNHX() {
throw new UnsupportedOperationException();
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
// TODO Auto-generated method stub
}
public static String getPrintableNameByType( final SEQUENCE_RELATION_TYPE type ) {
String s = typesToNames.get( type );
if ( s != null ) {
s = s.replace( '_', ' ' );
if ( ( s.length() > 15 ) && s.toLowerCase().endsWith( "ology" ) ) {
s = s.substring( 0, s.length() - 5 ) + ".";
}
}
return s;
}
public static enum SEQUENCE_RELATION_TYPE {
orthology, one_to_one_orthology, super_orthology, paralogy, ultra_paralogy, xenology, unknown, other;
}
}
org/forester/phylogeny/data/NodeDataField.java 0000664 0000000 0000000 00000003134 14125307352 020416 0 ustar root root
package org.forester.phylogeny.data;
public enum NodeDataField {
NODE_NAME,
EVENT,
SEQUENCE_NAME,
GENE_NAME,
SEQUENCE_SYMBOL,
SEQUENCE_MOL_SEQ_FASTA,
SEQUENCE_ACC,
TAXONOMY_SCIENTIFIC_NAME,
TAXONOMY_CODE,
UNKNOWN,
GO_TERM_IDS,
SEQ_ANNOTATIONS,
DOMAINS_ALL,
DOMAINS_COLLAPSED_PER_PROTEIN;
@Override
public String toString() {
switch ( this ) {
case DOMAINS_ALL:
return "Domains";
case DOMAINS_COLLAPSED_PER_PROTEIN:
return "Domains (collapsed per protein)";
case EVENT:
return "Events";
case GENE_NAME:
return "Gene Names";
case GO_TERM_IDS:
return "GO Term IDs";
case NODE_NAME:
return "Node Names";
case SEQ_ANNOTATIONS:
return "Sequence Annotations";
case SEQUENCE_ACC:
return "Sequence Accessors";
case SEQUENCE_MOL_SEQ_FASTA:
return "Molecular Sequences (Fasta)";
case SEQUENCE_NAME:
return "Sequence Names";
case SEQUENCE_SYMBOL:
return "Sequence Symbols";
case TAXONOMY_CODE:
return "Taxonomy Codes";
case TAXONOMY_SCIENTIFIC_NAME:
return "Scientific Names";
case UNKNOWN:
return "User Selected Data Fields";
default:
throw new IllegalArgumentException();
}
}
} org/forester/phylogeny/data/PhylogenyDataUtil.java 0000664 0000000 0000000 00000037576 14125307352 021422 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.awt.Graphics;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import org.forester.io.writers.PhylogenyWriter;
import org.forester.util.ForesterUtil;
public final class PhylogenyDataUtil {
/** Value of -99.0 is used as default value. */
public final static double BRANCH_LENGTH_DEFAULT = -1024.0;
public static void appendClose( final Writer w, final String element_name ) throws IOException {
w.write( "" );
w.write( element_name );
w.write( ">" );
}
public static void appendElement( final Writer w, final String element_name, final String value )
throws IOException {
appendOpen( w, element_name );
w.write( replaceIllegalXmlCharacters( value ) );
appendClose( w, element_name );
}
public static void appendElement( final Writer w,
final String element_name,
final String value,
final String indentation ) throws IOException {
w.write( ForesterUtil.LINE_SEPARATOR );
w.write( indentation );
w.write( PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
// Something like this replacement needs to be done in a more systematic manner.
appendElement( w, element_name, value );
}
public static void appendElement( final Writer w,
final String element_name,
final String value,
final String attribute_name,
final String attribute_value ) throws IOException {
appendOpen( w, element_name, attribute_name, attribute_value );
w.write( replaceIllegalXmlCharacters( value ) );
appendClose( w, element_name );
}
public static void appendElement( final Writer w,
final String element_name,
final String value,
final String attribute_name,
final String attribute_value,
final String indentation ) throws IOException {
w.write( ForesterUtil.LINE_SEPARATOR );
w.write( indentation );
w.write( PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
appendOpen( w, element_name, attribute_name, attribute_value );
w.write( replaceIllegalXmlCharacters( value ) );
appendClose( w, element_name );
}
public static void appendElement( final Writer w,
final String element_name,
final String value,
final String attribute1_name,
final String attribute1_value,
final String attribute2_name,
final String attribute2_value,
final String indentation ) throws IOException {
w.write( ForesterUtil.LINE_SEPARATOR );
w.write( indentation );
w.write( PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
appendOpen( w, element_name, attribute1_name, attribute1_value, attribute2_name, attribute2_value );
w.write( replaceIllegalXmlCharacters( value ) );
appendClose( w, element_name );
}
public static void appendElement( final Writer w,
final String element_name,
final String value,
final String attribute1_name,
final String attribute1_value,
final String attribute2_name,
final String attribute2_value ) throws IOException {
appendOpen( w, element_name, attribute1_name, attribute1_value, attribute2_name, attribute2_value );
w.write( replaceIllegalXmlCharacters( value ) );
appendClose( w, element_name );
}
public static void appendElement( final Writer w,
final String element_name,
final String attribute1_name,
final String attribute1_value,
final String attribute2_name,
final String attribute2_value,
final String attribute3_name,
final String attribute3_value,
final String attribute4_name,
final String attribute4_value,
final String indentation ) throws IOException {
w.write( ForesterUtil.LINE_SEPARATOR );
w.write( indentation );
appendOpen( w,
element_name,
attribute1_name,
attribute1_value,
attribute2_name,
attribute2_value,
attribute3_name,
attribute3_value,
attribute4_name,
attribute4_value );
appendClose( w, element_name );
}
public static void appendElement( final Writer w,
final String element_name,
final String value,
final String attribute1_name,
final String attribute1_value,
final String attribute2_name,
final String attribute2_value,
final String attribute3_name,
final String attribute3_value,
final String attribute4_name,
final String attribute4_value,
final String attribute5_name,
final String attribute5_value,
final String indentation ) throws IOException {
w.write( ForesterUtil.LINE_SEPARATOR );
w.write( indentation );
w.write( PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
appendOpen( w,
element_name,
attribute1_name,
attribute1_value,
attribute2_name,
attribute2_value,
attribute3_name,
attribute3_value,
attribute4_name,
attribute4_value,
attribute5_name,
attribute5_value );
w.write( replaceIllegalXmlCharacters( value ) );
appendClose( w, element_name );
}
public static void appendOpen( final Writer w, final String element_name ) throws IOException {
w.write( "<" );
w.write( element_name );
w.write( ">" );
}
public static void appendOpen( final Writer w,
final String element_name,
final String attribute_name,
final String attribute_value ) throws IOException {
w.write( "<" );
w.write( element_name );
if ( !ForesterUtil.isEmpty( attribute_value ) ) {
w.write( " " );
w.write( attribute_name );
w.write( "=\"" );
w.write( attribute_value );
w.write( "\"" );
}
w.write( ">" );
}
public static void appendOpen( final Writer w,
final String element_name,
final String attribute1_name,
final String attribute1_value,
final String attribute2_name,
final String attribute2_value ) throws IOException {
w.write( "<" );
w.write( element_name );
if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
w.write( " " );
w.write( attribute1_name );
w.write( "=\"" );
w.write( attribute1_value );
w.write( "\"" );
}
if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
w.write( " " );
w.write( attribute2_name );
w.write( "=\"" );
w.write( attribute2_value );
w.write( "\"" );
}
w.write( ">" );
}
public static void appendOpen( final Writer w,
final String element_name,
final String attribute1_name,
final String attribute1_value,
final String attribute2_name,
final String attribute2_value,
final String attribute3_name,
final String attribute3_value ) throws IOException {
w.write( "<" );
w.write( element_name );
if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
w.write( " " );
w.write( attribute1_name );
w.write( "=\"" );
w.write( attribute1_value );
w.write( "\"" );
}
if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
w.write( " " );
w.write( attribute2_name );
w.write( "=\"" );
w.write( attribute2_value );
w.write( "\"" );
}
if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
w.write( " " );
w.write( attribute3_name );
w.write( "=\"" );
w.write( attribute3_value );
w.write( "\"" );
}
w.write( ">" );
}
public static void appendOpen( final Writer w,
final String element_name,
final String attribute1_name,
final String attribute1_value,
final String attribute2_name,
final String attribute2_value,
final String attribute3_name,
final String attribute3_value,
final String attribute4_name,
final String attribute4_value ) throws IOException {
w.write( "<" );
w.write( element_name );
if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
w.write( " " );
w.write( attribute1_name );
w.write( "=\"" );
w.write( attribute1_value );
w.write( "\"" );
}
if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
w.write( " " );
w.write( attribute2_name );
w.write( "=\"" );
w.write( attribute2_value );
w.write( "\"" );
}
if ( !ForesterUtil.isEmpty( attribute3_value ) ) {
w.write( " " );
w.write( attribute3_name );
w.write( "=\"" );
w.write( attribute3_value );
w.write( "\"" );
}
if ( !ForesterUtil.isEmpty( attribute4_value ) ) {
w.write( " " );
w.write( attribute4_name );
w.write( "=\"" );
w.write( attribute4_value );
w.write( "\"" );
}
w.write( ">" );
}
public static void appendOpen( final Writer w,
final String element_name,
final String attribute1_name,
final String attribute1_value,
final String attribute2_name,
final String attribute2_value,
final String attribute3_name,
final String attribute3_value,
final String attribute4_name,
final String attribute4_value,
final String attribute5_name,
final String attribute5_value ) throws IOException {
w.write( "<" );
w.write( element_name );
if ( !ForesterUtil.isEmpty( attribute1_value ) ) {
w.write( " " );
w.write( attribute1_name );
w.write( "=\"" );
w.write( attribute1_value );
w.write( "\"" );
}
if ( !ForesterUtil.isEmpty( attribute2_value ) ) {
w.write( " " );
w.write( attribute2_name );
w.write( "=\"" );
w.write( attribute2_value );
w.write( "\"" );
}
if ( !ForesterUtil.isEmpty( attribute3_value ) ) {
w.write( " " );
w.write( attribute3_name );
w.write( "=\"" );
w.write( attribute3_value );
w.write( "\"" );
}
if ( !ForesterUtil.isEmpty( attribute4_value ) ) {
w.write( " " );
w.write( attribute4_name );
w.write( "=\"" );
w.write( attribute4_value );
w.write( "\"" );
}
if ( !ForesterUtil.isEmpty( attribute5_value ) ) {
w.write( " " );
w.write( attribute5_name );
w.write( "=\"" );
w.write( attribute5_value );
w.write( "\"" );
}
w.write( ">" );
}
/**
* Creates a deep copy of ArrayList of PhylogenyData objects.
*
* @param list
* an ArrayList of PhylogenyData objects
* @return a deep copy of ArrayList list
*/
public static ArrayList copy( final ArrayList list ) {
final ArrayList l = new ArrayList( list.size() );
for( int i = 0; i < list.size(); ++i ) {
l.add( ( list.get( i ) ).copy() );
}
return l;
}
public static void drawLine( final double x1, final double y1, final double x2, final double y2, final Graphics g ) {
g.drawLine( org.forester.util.ForesterUtil.roundToInt( x1 ),
org.forester.util.ForesterUtil.roundToInt( y1 ),
org.forester.util.ForesterUtil.roundToInt( x2 ),
org.forester.util.ForesterUtil.roundToInt( y2 ) );
}
public static String replaceIllegalXmlCharacters( final String value ) {
String v = value.replaceAll( "&", "&" );
v = v.replaceAll( "<", "<" );
v = v.replaceAll( ">", ">" );
v = v.replaceAll( "'", "'" );
v = v.replaceAll( "\"", """ );
return v;
}
}
org/forester/phylogeny/data/Point.java 0000664 0000000 0000000 00000013644 14125307352 017073 0 ustar root root
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.util.ForesterUtil;
public class Point implements PhylogenyData {
private final String _geodetic_datum;
private final BigDecimal _lat;
private final BigDecimal _long;
private final BigDecimal _alt;
private final String _alt_unit;
public static final String UNKNOWN_GEODETIC_DATUM = "?";
public Point() {
this( UNKNOWN_GEODETIC_DATUM, null, null, null, "" );
}
public Point( final String geodetic_datum, final BigDecimal lat, final BigDecimal longitude ) {
this( geodetic_datum, lat, longitude, null, "" );
}
public boolean isEmpty() {
return ( _lat == null ) && ( _long == null ) && ( _alt == null );
}
public Point( final String geodetic_datum,
final BigDecimal lat,
final BigDecimal longitude,
final BigDecimal alt,
final String alt_unit ) {
if ( ForesterUtil.isEmpty( geodetic_datum ) ) {
throw new IllegalArgumentException( "illegal attempt to use empty geodetic datum" );
}
if ( ( alt != null ) && ForesterUtil.isEmpty( alt_unit ) ) {
throw new IllegalArgumentException( "altitude must hava a unit" );
}
_geodetic_datum = geodetic_datum;
_lat = lat;
_long = longitude;
_alt = alt;
_alt_unit = alt_unit;
}
@Override
public StringBuffer asSimpleText() {
if ( isEmpty() ) {
return new StringBuffer();
}
else if ( getAltitude() == null ) {
return new StringBuffer( "[" + getLatitude().toPlainString() + ", " + getLongitude() + "]" );
}
else {
return new StringBuffer( "[" + getLatitude().toPlainString() + ", " + getLongitude() + ", " + getAltitude()
+ getAltiudeUnit() + "]" );
}
}
@Override
public StringBuffer asText() {
return asSimpleText();
}
@Override
public PhylogenyData copy() {
return new Point( getGeodeticDatum(),
getLatitude() == null ? null : new BigDecimal( getLatitude().toPlainString() ),
getLongitude() == null ? null : new BigDecimal( getLongitude().toPlainString() ),
getAltitude() == null ? null : new BigDecimal( getAltitude().toPlainString() ),
getAltiudeUnit() );
}
public BigDecimal getAltitude() {
return _alt;
}
public String getAltiudeUnit() {
return _alt_unit;
}
public String getGeodeticDatum() {
return _geodetic_datum;
}
public BigDecimal getLatitude() {
return _lat;
}
public BigDecimal getLongitude() {
return _long;
}
@Override
public boolean isEqual( final PhylogenyData point ) {
throw new UnsupportedOperationException();
}
@Override
public StringBuffer toNHX() {
throw new UnsupportedOperationException();
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
if ( isEmpty() ) {
return;
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
if ( getAltitude() != null ) {
PhylogenyDataUtil.appendOpen( writer,
PhyloXmlMapping.POINT,
PhyloXmlMapping.POINT_GEODETIC_DATUM,
getGeodeticDatum(),
PhyloXmlMapping.POINT_ALTITUDE_UNIT_ATTR,
getAltiudeUnit() );
}
else {
PhylogenyDataUtil.appendOpen( writer,
PhyloXmlMapping.POINT,
PhyloXmlMapping.POINT_GEODETIC_DATUM,
getGeodeticDatum() );
}
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.POINT_LATITUDE,
getLatitude().toPlainString(),
indentation );
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.POINT_LONGITUDE,
getLongitude().toPlainString(),
indentation );
if ( getAltitude() != null ) {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.POINT_ALTITUDE,
getAltitude().toPlainString(),
indentation );
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.POINT );
}
@Override
public String toString() {
return asSimpleText().toString();
}
static public final boolean isSeemsEmpty( final Point p ) {
return ( ( ( p.getAltitude() == null ) || ( p.getAltitude().compareTo( BigDecimal.ZERO ) <= 0 ) )
&& ( ( p.getLongitude() == null ) || ( p.getLongitude().compareTo( BigDecimal.ZERO ) <= 0 ) )
&& ( ( p.getLatitude() == null ) || ( p.getLatitude().compareTo( BigDecimal.ZERO ) <= 0 ) )
&& ( ForesterUtil.isEmpty( p.getGeodeticDatum() ) || p.getGeodeticDatum()
.equalsIgnoreCase( UNKNOWN_GEODETIC_DATUM ) ) && ( ForesterUtil.isEmpty( p.getAltiudeUnit() ) || p
.getAltiudeUnit().equalsIgnoreCase( "?" ) ) );
}
}
org/forester/phylogeny/data/BranchColor.java 0000664 0000000 0000000 00000006305 14125307352 020172 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.awt.Color;
import java.io.IOException;
import java.io.Writer;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.util.ForesterUtil;
public class BranchColor implements PhylogenyData {
private Color _color;
public BranchColor() {
_color = null;
}
public BranchColor( final Color color ) {
_color = color;
}
@Override
public StringBuffer asSimpleText() {
return new StringBuffer( getValue().toString() );
}
@Override
public StringBuffer asText() {
return new StringBuffer( getValue().toString() );
}
@Override
/**
* Not a deep copy.
*
*/
public PhylogenyData copy() {
final BranchColor bc = new BranchColor();
bc.setValue( getValue() );
return bc;
}
public Color getValue() {
return _color;
}
@Override
public boolean isEqual( final PhylogenyData data ) {
return getValue().equals( ( ( BranchColor ) data ).getValue() );
}
public void setValue( final Color color ) {
_color = color;
}
@Override
public StringBuffer toNHX() {
throw new UnsupportedOperationException();
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.COLOR );
PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.COLOR_RED, getValue().getRed() + "", indentation );
PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.COLOR_GREEN, getValue().getGreen() + "", indentation );
PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.COLOR_BLUE, getValue().getBlue() + "", indentation );
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.COLOR );
}
@Override
public String toString() {
return asText().toString();
}
}
org/forester/phylogeny/data/Distribution.java 0000664 0000000 0000000 00000014512 14125307352 020454 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// Copyright (C) 2000-2001 Washington University School of Medicine
// and Howard Hughes Medical Institute
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.io.writers.PhylogenyWriter;
import org.forester.util.ForesterUtil;
public class Distribution implements PhylogenyData {
private final String _desc;
private final List _points;
private final List _polygons;
public Distribution( final String desc ) {
_desc = desc;
_points = null;
_polygons = null;
}
public Distribution( final String desc, final List points ) {
_desc = null;
_points = points;
_polygons = null;
}
public Distribution( final String desc, final List points, final List polygons ) {
_desc = desc;
_points = points;
_polygons = polygons;
}
public boolean isEmpty() {
if ( ForesterUtil.isEmpty( _desc ) && ( ( getPoints() != null ) && ( getPoints().size() == 1 ) )
&& ForesterUtil.isEmpty( _polygons ) ) {
if ( Point.isSeemsEmpty( getPoints().get( 0 ) ) ) {
return true;
}
}
return ForesterUtil.isEmpty( _desc ) && ForesterUtil.isEmpty( _points ) && ForesterUtil.isEmpty( _polygons );
}
@Override
public StringBuffer asSimpleText() {
final StringBuffer sb = new StringBuffer();
if ( isEmpty() ) {
return sb;
}
sb.append( "Distribution: " );
if ( !ForesterUtil.isEmpty( getDesc() ) ) {
sb.append( ForesterUtil.LINE_SEPARATOR );
sb.append( " Description: " );
sb.append( getDesc() );
}
int i = 0;
if ( getPoints() != null ) {
for( final Point point : getPoints() ) {
if ( ( point != null ) && !Point.isSeemsEmpty( point ) ) {
sb.append( ForesterUtil.LINE_SEPARATOR );
sb.append( " Point " + i + ": " );
sb.append( point.asSimpleText() );
i++;
}
}
}
i = 0;
if ( getPolygons() != null ) {
for( final Polygon polygon : getPolygons() ) {
if ( polygon != null ) {
sb.append( ForesterUtil.LINE_SEPARATOR );
sb.append( " Polygon " + i + ":" );
sb.append( ForesterUtil.LINE_SEPARATOR );
sb.append( polygon.asSimpleText() );
i++;
}
}
}
return sb;
}
@Override
public StringBuffer asText() {
return asSimpleText();
}
@Override
public PhylogenyData copy() {
List new_points = null;
List new_polygons = null;
if ( getPoints() != null ) {
new_points = new ArrayList();
for( final Point point : getPoints() ) {
new_points.add( ( Point ) point.copy() );
}
}
if ( getPolygons() != null ) {
new_polygons = new ArrayList();
for( final Polygon polygon : getPolygons() ) {
new_polygons.add( ( Polygon ) polygon.copy() );
}
}
return new Distribution( getDesc(), new_points, new_polygons );
}
public String getDesc() {
return _desc;
}
public List getPoints() {
return _points;
}
public List getPolygons() {
return _polygons;
}
@Override
public boolean isEqual( final PhylogenyData data ) {
throw new UnsupportedOperationException();
}
@Override
public StringBuffer toNHX() {
throw new UnsupportedOperationException();
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
if ( isEmpty() ) {
return;
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.DISTRIBUTION );
if ( !ForesterUtil.isEmpty( getDesc() ) ) {
PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.DISTRIBUTION_DESC, getDesc(), indentation );
}
final String ind = indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE;
if ( getPoints() != null ) {
for( final Point point : getPoints() ) {
if ( ( point != null ) && !Point.isSeemsEmpty( point ) ) {
point.toPhyloXML( writer, level, ind );
}
}
}
if ( getPolygons() != null ) {
for( final Polygon polygon : getPolygons() ) {
if ( polygon != null ) {
polygon.toPhyloXML( writer, level, ind );
}
}
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.DISTRIBUTION );
}
@Override
public String toString() {
return asSimpleText().toString();
}
}
org/forester/phylogeny/data/MultipleUris.java 0000664 0000000 0000000 00000002516 14125307352 020434 0 ustar root root // $Id:
// forester -- software libraries and applications
// for genomics and evolutionary biology research.
//
// Copyright (C) 2010 Christian M Zmasek
// Copyright (C) 2010 Sanford-Burnham Medical Research Institute
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.util.List;
public interface MultipleUris {
public List getUris();
public void setUris( final List uris );
public Uri getUri( final int index );
public void addUri( final Uri uri );
}
org/forester/phylogeny/data/Polygon.java 0000664 0000000 0000000 00000006601 14125307352 017424 0 ustar root root // $Id:
// forester -- software libraries and applications
// for genomics and evolutionary biology research.
//
// Copyright (C) 2010 Christian M Zmasek
// Copyright (C) 2010 Sanford-Burnham Medical Research Institute
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.io.writers.PhylogenyWriter;
import org.forester.util.ForesterUtil;
public class Polygon implements PhylogenyData {
private final List _points;
public Polygon( final List points ) {
_points = points;
}
@Override
public StringBuffer asSimpleText() {
final StringBuffer sb = new StringBuffer();
boolean first = true;
for( final Point point : getPoints() ) {
if ( first ) {
first = false;
}
else {
sb.append( ForesterUtil.LINE_SEPARATOR );
}
sb.append( point.asSimpleText() );
}
return sb;
}
@Override
public StringBuffer asText() {
return asSimpleText();
}
@Override
public PhylogenyData copy() {
final List new_points = new ArrayList();
for( final Point point : getPoints() ) {
new_points.add( ( Point ) point.copy() );
}
return new Polygon( new_points );
}
public List getPoints() {
return _points;
}
public boolean isEmpty() {
return ForesterUtil.isEmpty( _points );
}
@Override
public boolean isEqual( final PhylogenyData data ) {
throw new UnsupportedOperationException();
}
@Override
public StringBuffer toNHX() {
throw new UnsupportedOperationException();
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
if ( isEmpty() ) {
return;
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.POLYGON );
for( final Point point : getPoints() ) {
point.toPhyloXML( writer, level, indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE );
writer.write( indentation );
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.POLYGON );
}
}
org/forester/phylogeny/data/Accession.java 0000664 0000000 0000000 00000017644 14125307352 017715 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: www.phylosoft.org
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import org.forester.io.parsers.nhx.NHXtags;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.util.ForesterUtil;
public final class Accession implements PhylogenyData, Comparable {
final private String _comment;
final private String _source;
final private String _source_value;
final private String _value;
public enum Source {
NCBI, REFSEQ, UNIPROT, GI, EMBL, ENSEMBL, UNKNOWN;
@Override
public String toString() {
switch ( this ) {
case NCBI:
return "ncbi";
case REFSEQ:
return "refseq";
case UNIPROT:
return "uniprot";
case GI:
return "gi";
case EMBL:
return "embl";
case ENSEMBL:
return "ensembl";
case UNKNOWN:
return "unknown";
default:
throw new IllegalArgumentException();
}
}
}
public Accession( final String value ) {
_value = value;
_source = "";
_comment = "";
_source_value = value;
}
public Accession( final String value, final String source ) {
_value = value;
_source = source;
_comment = "";
if ( source != null ) {
_source_value = source + value;
}
else {
_source_value = value;
}
}
public Accession( final String value, final Source source ) {
_value = value;
_source = source.toString();
_comment = "";
_source_value = source + value;
}
public Accession( final String value, final String source, final String comment ) {
_value = value;
_source = source;
_comment = comment;
if ( source != null ) {
_source_value = source + value;
}
else {
_source_value = value;
}
}
@Override
public StringBuffer asSimpleText() {
return new StringBuffer( getValue() );
}
@Override
public StringBuffer asText() {
final StringBuffer sb = new StringBuffer();
if ( !ForesterUtil.isEmpty( getSource() ) ) {
sb.append( getSource() );
sb.append( ": " );
}
sb.append( getValue() );
if ( !ForesterUtil.isEmpty( getComment() ) ) {
sb.append( " (" );
sb.append( getComment() );
sb.append( ")" );
}
return sb;
}
@Override
public int compareTo( final Accession o ) {
if ( equals( o ) ) {
return 0;
}
return _source_value.compareTo( o._source_value );
}
@Override
public PhylogenyData copy() {
return new Accession( getValue(), getSource() );
}
@Override
public boolean equals( final Object o ) {
if ( this == o ) {
return true;
}
else if ( o == null ) {
return false;
}
else if ( o.getClass() != this.getClass() ) {
throw new IllegalArgumentException( "attempt to check [" + this.getClass() + "] equality to " + o + " ["
+ o.getClass() + "]" );
}
else {
return isEqual( ( Accession ) o );
}
}
public String getComment() {
return _comment;
}
public String getSource() {
return _source;
}
public String getValue() {
return _value;
}
@Override
public int hashCode() {
return _source_value.hashCode();
}
@Override
public boolean isEqual( final PhylogenyData data ) {
if ( this == data ) {
return true;
}
if ( ( data == null ) || ( getValue() == null ) ) {
return false;
}
final Accession a = ( Accession ) data;
if ( ( getSource() != null ) && ( a.getSource() != null ) ) {
return ( a.getValue().equals( getValue() ) && a.getSource().equals( getSource() ) );
}
return ( a.getValue().equals( getValue() ) );
}
@Override
public StringBuffer toNHX() {
final StringBuffer sb = new StringBuffer();
sb.append( ":" );
sb.append( NHXtags.SEQUENCE_ACCESSION );
sb.append( ForesterUtil.replaceIllegalNhxCharacters( getValue() ) );
return sb;
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
if ( ForesterUtil.isEmpty( getSource() ) ) {
if ( ForesterUtil.isEmpty( getComment() ) ) {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.ACCESSION,
getValue(),
PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
"unknown",
indentation );
}
else {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.ACCESSION,
getValue(),
PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
"unknown",
PhyloXmlMapping.ACCESSION_COMMENT_ATTR,
getComment(),
indentation );
}
}
else {
if ( ForesterUtil.isEmpty( getComment() ) ) {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.ACCESSION,
getValue(),
PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
getSource(),
indentation );
}
else {
PhylogenyDataUtil.appendElement( writer,
PhyloXmlMapping.ACCESSION,
getValue(),
PhyloXmlMapping.ACCESSION_SOURCE_ATTR,
getSource(),
PhyloXmlMapping.ACCESSION_COMMENT_ATTR,
getComment(),
indentation );
}
}
}
@Override
public String toString() {
return asText().toString();
}
}
org/forester/phylogeny/data/Reference.java 0000664 0000000 0000000 00000007562 14125307352 017702 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: www.phylosoft.org
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.io.parsers.phyloxml.PhyloXmlUtil;
import org.forester.util.ForesterUtil;
public class Reference implements PhylogenyData {
String _desc;
String _doi;
public Reference( final String desc ) {
_desc = desc;
_doi = "";
}
public Reference( final String desc, final String doi ) {
_desc = desc;
_doi = doi;
}
@Override
public StringBuffer asSimpleText() {
return new StringBuffer( getDescription() );
}
@Override
public StringBuffer asText() {
final StringBuffer sb = new StringBuffer();
if ( !ForesterUtil.isEmpty( getDoi() ) ) {
sb.append( "[doi:" );
sb.append( getDoi() );
sb.append( "] " );
}
sb.append( getDescription() );
return sb;
}
@Override
public PhylogenyData copy() {
return new Reference( getDescription(), getDoi() );
}
public String getDoi() {
return _doi;
}
public String getDescription() {
return _desc;
}
@Override
public boolean isEqual( final PhylogenyData data ) {
if ( ( data == null ) || ( getDescription() == null ) ) {
return false;
}
return ( ( Reference ) data ).getDescription().equals( getDescription() )
&& ( ( Reference ) data ).getDoi().equals( getDoi() );
}
public void setDoi( final String doi ) throws PhyloXmlDataFormatException {
if ( !ForesterUtil.isEmpty( doi ) && !PhyloXmlUtil.LIT_REF_DOI_PATTERN.matcher( doi ).matches() ) {
throw new PhyloXmlDataFormatException( "illegal doi: [" + doi + "]" );
}
_doi = doi;
}
public void setValue( final String value ) {
_desc = value;
}
@Override
public StringBuffer toNHX() {
throw new UnsupportedOperationException();
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendOpen( writer, PhyloXmlMapping.REFERENCE, PhyloXmlMapping.REFERENCE_DOI_ATTR, getDoi() );
if ( !ForesterUtil.isEmpty( getDescription() ) ) {
PhylogenyDataUtil.appendElement( writer, PhyloXmlMapping.REFERENCE_DESC, getDescription(), indentation );
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.REFERENCE );
}
@Override
public String toString() {
return asText().toString();
}
} org/forester/phylogeny/data/PhylogenyData.java 0000664 0000000 0000000 00000004513 14125307352 020545 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
/*
* Interface for data for annotating a Phylogeny.
*/
public interface PhylogenyData {
public StringBuffer asSimpleText();
public StringBuffer asText();
/**
* Creates a new PhylogenyData object with identical values as this
* PhylogenyData.
* This ~should~ return a deep copy, but not there yet.
*
*
* @return a ~deep~ copy of this PhylogenyData
*/
public PhylogenyData copy();
/**
* Compares this PhylogenyData to PhylogenyData data. In general, this
* should return true if and only if all fiels are exactly identical.
*
* @param PhylogenyData
* the PhylogenyData to compare to
* @return in general, true if and only if all fiels are exactly identical,
* false otherwise
*/
public boolean isEqual( final PhylogenyData data );
public StringBuffer toNHX();
/**
* Writes a phyloXML representation of this phylogeny data.
*
* @param writer
* @param level
* @param indentation
* @throws IOException
*/
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException;
} org/forester/phylogeny/data/DomainArchitecture.java 0000664 0000000 0000000 00000020266 14125307352 021552 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.StringTokenizer;
import java.util.TreeMap;
import org.forester.io.parsers.nhx.NHXtags;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.io.writers.PhylogenyWriter;
import org.forester.util.ForesterUtil;
public class DomainArchitecture implements PhylogenyData {
public final static String NHX_SEPARATOR = ">";
private static final BigDecimal INCREASE_KEY = new BigDecimal( "0.00001" );
private SortedMap _domains;
private int _total_length;
public DomainArchitecture() {
init();
}
public DomainArchitecture( final List domains, final int total_length ) {
init();
for( final PhylogenyData phylogenyData : domains ) {
final ProteinDomain pd = ( ProteinDomain ) phylogenyData;
addDomain( pd );
}
_total_length = total_length;
}
public DomainArchitecture( final String da_str ) {
init();
int total_length = 0;
int to = -1;
try {
final StringTokenizer st = new StringTokenizer( da_str, DomainArchitecture.NHX_SEPARATOR );
final String length_str = ( String ) st.nextElement();
total_length = new Integer( length_str ).intValue();
while ( st.hasMoreElements() ) {
final String from_str = ( String ) st.nextElement();
final String to_str = ( String ) st.nextElement();
final String support_str = ( String ) st.nextElement();
final String name = ( String ) st.nextElement();
to = new Integer( to_str ).intValue();
final int from = new Integer( from_str ).intValue();
final double support = new Double( support_str ).doubleValue();
final ProteinDomain pd = new ProteinDomain( name, from, to, support );
addDomain( pd );
}
}
catch ( final Exception e ) {
throw new IllegalArgumentException( "malformed format for domain structure \"" + da_str + "\": "
+ e.getMessage() );
}
if ( to > total_length ) {
throw new IllegalArgumentException( "total length of domain structure is too short" );
}
_total_length = total_length;
}
public void addDomain( final ProteinDomain pd ) {
BigDecimal key = new BigDecimal( "" + pd.getFrom() );
while ( _domains.containsKey( key ) ) {
key = new BigDecimal( "" + ( key.doubleValue() + DomainArchitecture.INCREASE_KEY.doubleValue() ) );
}
_domains.put( key, pd );
}
@Override
public StringBuffer asSimpleText() {
final StringBuffer sb = new StringBuffer();
for( int i = 0; i < getDomains().size(); ++i ) {
if ( i > 0 ) {
sb.append( "~" );
}
sb.append( getDomain( i ).asSimpleText() );
}
return sb;
}
@Override
public StringBuffer asText() {
final StringBuffer sb = new StringBuffer();
for( int i = 0; i < getDomains().size(); ++i ) {
if ( i > 0 ) {
sb.append( "~" );
}
sb.append( getDomain( i ).asText() );
}
return sb;
}
@Override
public PhylogenyData copy() {
final List domains = new ArrayList( getDomains().size() );
for( int i = 0; i < getDomains().size(); ++i ) {
domains.add( getDomain( i ).copy() );
}
return new DomainArchitecture( domains, getTotalLength() );
}
public ProteinDomain getDomain( final int i ) {
return ( ProteinDomain ) _domains.values().toArray()[ i ];
}
public SortedMap getDomains() {
return _domains;
}
public int getNumberOfDomains() {
return _domains.size();
}
public int getTotalLength() {
return _total_length;
}
private void init() {
_domains = new TreeMap();
_total_length = 0;
}
/**
* Returns true if the names and the order of the domains match (domain and
* linker lengths are ignored).
*
*
*/
@Override
public boolean isEqual( final PhylogenyData domain_architecture ) {
if ( domain_architecture == null ) {
return false;
}
if ( !( domain_architecture instanceof DomainArchitecture ) ) {
return false;
}
final DomainArchitecture d = ( DomainArchitecture ) domain_architecture;
if ( getDomains().size() != d.getDomains().size() ) {
return false;
}
for( int i = 0; i < getDomains().size(); ++i ) {
if ( !getDomain( i ).getName().equals( d.getDomain( i ).getName() ) ) {
return false;
}
}
return true;
}
public void setTotalLength( final int total_length ) {
_total_length = total_length;
}
@Override
public StringBuffer toNHX() {
final StringBuffer sb = new StringBuffer();
sb.append( ":" );
sb.append( NHXtags.DOMAIN_STRUCTURE );
sb.append( getTotalLength() );
if ( getDomains() != null ) {
for( int i = 0; i < getDomains().size(); ++i ) {
sb.append( DomainArchitecture.NHX_SEPARATOR );
sb.append( getDomain( i ).getFrom() );
sb.append( DomainArchitecture.NHX_SEPARATOR );
sb.append( getDomain( i ).getTo() );
sb.append( DomainArchitecture.NHX_SEPARATOR );
sb.append( getDomain( i ).getConfidence() );
sb.append( DomainArchitecture.NHX_SEPARATOR );
sb.append( ForesterUtil.replaceIllegalNhxCharacters( getDomain( i ).getName() ) );
}
}
return sb;
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendOpen( writer,
PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECURE,
PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECTURE_LENGTH,
getTotalLength() + "" );
if ( getDomains() != null ) {
final String ind = indentation + PhylogenyWriter.PHYLO_XML_INTENDATION_BASE;
for( int i = 0; i < getDomains().size(); ++i ) {
getDomain( i ).toPhyloXML( writer, level, ind );
}
}
writer.write( ForesterUtil.LINE_SEPARATOR );
writer.write( indentation );
PhylogenyDataUtil.appendClose( writer, PhyloXmlMapping.SEQUENCE_DOMAIN_ARCHITECURE );
}
@Override
public String toString() {
return asText().toString();
}
}
org/forester/phylogeny/data/BranchWidth.java 0000664 0000000 0000000 00000005055 14125307352 020174 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import org.forester.io.parsers.phyloxml.PhyloXmlMapping;
import org.forester.util.ForesterUtil;
public class BranchWidth implements PhylogenyData {
public final static double BRANCH_WIDTH_DEFAULT_VALUE = 1.0;
private final double _value;
public BranchWidth() {
_value = BRANCH_WIDTH_DEFAULT_VALUE;
}
public BranchWidth( final double value ) {
_value = value;
}
@Override
public StringBuffer asSimpleText() {
return new StringBuffer( getValue() + "" );
}
@Override
public StringBuffer asText() {
return asSimpleText();
}
@Override
public PhylogenyData copy() {
return new BranchWidth( getValue() );
}
public double getValue() {
return _value;
}
@Override
public boolean isEqual( final PhylogenyData data ) {
return getValue() == ( ( BranchWidth ) data ).getValue();
}
@Override
public StringBuffer toNHX() {
throw new UnsupportedOperationException();
}
@Override
public void toPhyloXML( final Writer w, final int level, final String indentation ) throws IOException {
w.write( ForesterUtil.LINE_SEPARATOR );
w.write( indentation );
PhylogenyDataUtil.appendElement( w, PhyloXmlMapping.WIDTH, getValue() + "" );
}
@Override
public String toString() {
return asText().toString();
}
}
org/forester/phylogeny/data/PropertiesMap.java 0000664 0000000 0000000 00000014657 14125307352 020601 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.data;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import org.forester.util.ForesterUtil;
public class PropertiesMap implements PhylogenyData {
private final SortedMap _properties;
public PropertiesMap() {
_properties = new TreeMap();
}
public int size() {
return _properties.size();
}
public void addProperty( final Property property ) throws IllegalArgumentException {
if ( getProperties().containsKey( property.getRef() ) ) {
throw new IllegalArgumentException( "ref [" + property.getRef() + "] is already present" );
}
getProperties().put( property.getRef(), property );
}
@Override
public StringBuffer asSimpleText() {
final StringBuffer sb = new StringBuffer();
boolean first = true;
for( final String ref : getPropertyRefs() ) {
if ( first ) {
first = false;
}
else {
sb.append( " " );
}
sb.append( getProperty( ref ).asText() );
}
return sb;
}
@Override
public StringBuffer asText() {
return asSimpleText();
}
@Override
public PhylogenyData copy() {
final PropertiesMap new_one = new PropertiesMap();
for( final String r : getProperties().keySet() ) {
new_one.addProperty( getProperties().get( r ) );
}
return new_one;
}
public SortedMap getProperties() {
return _properties;
}
public Property[] getPropertiesArray() {
final Property[] a = new Property[ getProperties().size() ];
int i = 0;
for( final String ref : getProperties().keySet() ) {
a[ i++ ] = getProperties().get( ref );
}
return a;
}
public List getPropertiesWithGivenReferencePrefix( final String ref_prefix )
throws IllegalArgumentException {
if ( ForesterUtil.isEmpty( ref_prefix ) ) {
throw new IllegalArgumentException( "reference prefix is null or empty" );
}
final String my_ref_prefix = new String( ref_prefix.trim() );
final List props = new ArrayList();
for( final String ref : getProperties().keySet() ) {
if ( ref.startsWith( my_ref_prefix ) ) {
props.add( getProperty( ref ) );
}
}
return props;
}
public Property getProperty( final String ref ) throws IllegalArgumentException {
if ( getProperties().containsKey( ref ) ) {
return getProperties().get( ref );
}
else {
throw new IllegalArgumentException( "reference [" + ref + "] is not present" );
}
}
/**
* Returns all property refs of this PhylogenyNode as String array.
*/
public String[] getPropertyRefs() {
if ( getProperties() == null ) {
return new String[ 0 ];
}
final Property[] properties = getPropertiesArray();
final String[] refs = new String[ properties.length ];
for( int i = 0; i < properties.length; ++i ) {
refs[ i ] = properties[ i ].getRef();
}
return refs;
}
@Override
public boolean isEqual( final PhylogenyData data ) {
throw new UnsupportedOperationException();
}
public boolean refExists( final String ref ) {
if ( getProperties() != null ) {
for( final String r : getProperties().keySet() ) {
if ( r.equalsIgnoreCase( ref ) ) {
return true;
}
}
}
return false;
}
public Property removeProperty( final String ref ) throws IllegalArgumentException {
if ( getProperties().containsKey( ref ) ) {
return getProperties().remove( ref );
}
else {
throw new IllegalArgumentException( "reference [" + ref + "] is not present" );
}
}
public List removePropertiesWithGivenReferencePrefix( final String ref_prefix )
throws IllegalArgumentException {
if ( ForesterUtil.isEmpty( ref_prefix ) ) {
throw new IllegalArgumentException( "reference prefix is null or empty" );
}
final String my_ref_prefix = new String( ref_prefix.trim() );
final List to_remove = new ArrayList();
for( final String ref : getProperties().keySet() ) {
if ( ref.startsWith( my_ref_prefix ) ) {
to_remove.add( ref );
}
}
for( final String ref : to_remove ) {
getProperties().remove( ref );
}
return to_remove;
}
@Override
public StringBuffer toNHX() {
throw new UnsupportedOperationException();
}
@Override
public void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
if ( getProperties() != null ) {
for( final String ref : getProperties().keySet() ) {
getProperties().get( ref ).toPhyloXML( writer, level, indentation );
}
}
}
@Override
public String toString() {
return asSimpleText().toString();
}
}
org/forester/phylogeny/data/NodeVisualData.java 0000664 0000000 0000000 00000043062 14125307352 020642 0 ustar root root
package org.forester.phylogeny.data;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import org.forester.phylogeny.data.Property.AppliesTo;
import org.forester.util.ForesterUtil;
public final class NodeVisualData implements PhylogenyData {
public static final String APTX_VISUALIZATION_REF = "style:";
public static final int DEFAULT_SIZE = -1;
public static final String FONT_COLOR_REF = APTX_VISUALIZATION_REF + "font_color";
public static final String FONT_COLOR_TYPE = "xsd:token";
public static final String FONT_REF = APTX_VISUALIZATION_REF + "font";
public static final String FONT_SIZE_REF = APTX_VISUALIZATION_REF + "font_size";
public static final String FONT_SIZE_TYPE = "xsd:unsignedByte";
public static final String FONT_STYLE_BOLD = "bold";
public static final String FONT_STYLE_BOLD_ITALIC = "bold_italic";
public static final String FONT_STYLE_ITALIC = "italic";
public static final String FONT_STYLE_PLAIN = "plain";
public static final String FONT_STYLE_REF = APTX_VISUALIZATION_REF + "font_style";
public static final String FONT_STYLE_TYPE = "xsd:token";
public static final String FONT_TYPE = "xsd:token";
public static final String NODE_COLOR_REF = APTX_VISUALIZATION_REF + "node_color";
public static final String NODE_COLOR_TYPE = "xsd:token";
public static final String NODE_FILL_GRADIENT = "gradient";
public static final String NODE_FILL_NONE = "none";
public static final String NODE_FILL_SOLID = "solid";
public static final String NODE_FILL_TYPE_REF = APTX_VISUALIZATION_REF + "node_fill_type";
public static final String NODE_FILL_TYPE_TYPE = "xsd:token";
public static final String NODE_SHAPE_CIRCLE = "circle";
public static final String NODE_SHAPE_RECTANGLE = "rectangle";
public static final String NODE_SHAPE_REF = APTX_VISUALIZATION_REF + "node_shape";
public static final String NODE_SHAPE_TYPE = "xsd:token";
public static final String NODE_SIZE_REF = APTX_VISUALIZATION_REF + "node_size";
public static final String NODE_SIZE_TYPE = "xsd:float";
public static final String NODE_TRANSPARENCY_REF = APTX_VISUALIZATION_REF + "node_transparency";
public static final String NODE_TRANSPARENCY_TYPE = "xsd:float";
private static final byte DEFAULT_FONT_SIZE = -1;
private static final int DEFAULT_TRANSPARENCY = -1;
private NodeFill _fill_type;
private Font _font;
private Color _font_color;
private String _font_name;
private byte _font_size;
private FontType _font_style;
private Color _node_color;
private NodeShape _shape;
private float _size;
private float _transparency;
public NodeVisualData() {
init();
}
public NodeVisualData( final String font_name,
final FontType font_style,
final byte font_size,
final Color font_color,
final NodeShape shape,
final NodeFill fill_type,
final Color node_color,
final float size,
final float transparency ) {
setFontName( font_name );
setFontStyle( font_style );
setFontSize( font_size );
setFontColor( font_color );
setShape( shape );
setFillType( fill_type );
setNodeColor( node_color );
setSize( size );
setTransparency( transparency );
}
@Override
public final StringBuffer asSimpleText() {
return asText();
}
@Override
public final StringBuffer asText() {
final StringBuffer sb = new StringBuffer();
return sb;
}
@Override
public final PhylogenyData copy() {
return new NodeVisualData( !ForesterUtil.isEmpty( getFontName() ) ? new String( getFontName() ) : null,
getFontStyle(),
getFontSize(),
getFontColor() != null ? new Color( getFontColor().getRed(), getFontColor()
.getGreen(), getFontColor().getBlue() ) : null,
getShape(),
getFillType(),
getNodeColor() != null ? new Color( getNodeColor().getRed(), getNodeColor()
.getGreen(), getNodeColor().getBlue() ) : null,
getSize(),
getTransparency() );
}
public final NodeFill getFillType() {
return _fill_type;
}
public final Font getFont() {
if ( _font != null ) {
return _font;
}
else if ( !ForesterUtil.isEmpty( getFontName() ) ) {
_font = new Font( getFontName(), getFontStyleInt(), getFontSize() );
return _font;
}
return null;
}
public final Color getFontColor() {
return _font_color;
}
public final String getFontName() {
return _font_name;
}
public final byte getFontSize() {
return _font_size;
}
public final FontType getFontStyle() {
return _font_style;
}
public final int getFontStyleInt() {
if ( getFontStyle() == FontType.BOLD ) {
return Font.BOLD;
}
else if ( getFontStyle() == FontType.ITALIC ) {
return Font.ITALIC;
}
else if ( getFontStyle() == FontType.BOLD_ITALIC ) {
return Font.BOLD + Font.ITALIC;
}
return Font.PLAIN;
}
public final Color getNodeColor() {
return _node_color;
}
public final NodeShape getShape() {
return _shape;
}
public final float getSize() {
return _size;
}
public final float getTransparency() {
return _transparency;
}
public final boolean isEmpty() {
return ( ForesterUtil.isEmpty( getFontName() ) && ( getFontStyle() == FontType.PLAIN )
&& ( getFontSize() == DEFAULT_FONT_SIZE ) && ( getFontColor() == null )
&& ( getShape() == NodeShape.DEFAULT ) && ( getFillType() == NodeFill.DEFAULT )
&& ( getNodeColor() == null ) && ( getSize() == DEFAULT_SIZE ) && ( getTransparency() == DEFAULT_TRANSPARENCY ) );
}
@Override
public final boolean isEqual( final PhylogenyData data ) {
throw new UnsupportedOperationException();
}
public void parseProperty( final Property prop ) {
if ( prop.getRef().equals( FONT_REF ) ) {
setFontName( prop.getValue().trim() );
}
else if ( prop.getRef().equals( FONT_SIZE_REF ) ) {
int s = -1;
try {
s = Integer.parseInt( prop.getValue() );
}
catch ( final NumberFormatException e ) {
return;
}
if ( ( s >= 0 ) && ( s < Byte.MAX_VALUE ) ) {
setFontSize( s );
}
}
else if ( prop.getRef().equals( FONT_STYLE_REF ) ) {
setFontStyle( prop.getValue() );
}
else if ( prop.getRef().equals( FONT_COLOR_REF ) ) {
try {
setFontColor( Color.decode( prop.getValue() ) );
}
catch ( final NumberFormatException e ) {
return;
}
}
else if ( prop.getRef().equals( NODE_SIZE_REF ) ) {
float s = -1.0f;
try {
s = Float.parseFloat( prop.getValue() );
}
catch ( final NumberFormatException e ) {
return;
}
if ( s >= 0 ) {
setSize( s );
}
}
else if ( prop.getRef().equals( NODE_COLOR_REF ) ) {
try {
setNodeColor( Color.decode( prop.getValue() ) );
}
catch ( final NumberFormatException e ) {
return;
}
}
else if ( prop.getRef().equals( NODE_SHAPE_REF ) ) {
setShape( prop.getValue() );
}
else if ( prop.getRef().equals( NODE_FILL_TYPE_REF ) ) {
setFillType( prop.getValue() );
}
}
public final void setFillType( final NodeFill fill_type ) {
_fill_type = fill_type;
}
public final void setFillType( final String fill ) {
if ( fill.equalsIgnoreCase( NODE_FILL_NONE ) ) {
setFillType( NodeFill.NONE );
}
else if ( fill.equalsIgnoreCase( NODE_FILL_SOLID ) ) {
setFillType( NodeFill.SOLID );
}
else if ( fill.equalsIgnoreCase( NODE_FILL_GRADIENT ) ) {
setFillType( NodeFill.GRADIENT );
}
else {
setFillType( NodeFill.DEFAULT );
}
}
public final void setFontColor( final Color font_color ) {
_font_color = font_color;
}
public final void setFontName( final String font_name ) {
if ( !ForesterUtil.isEmpty( font_name ) ) {
_font_name = font_name;
}
else {
_font_name = null;
}
_font = null;
}
public final void setFontSize( final int font_size ) {
if ( ( font_size != DEFAULT_FONT_SIZE ) && ( font_size < 0 ) ) {
throw new IllegalArgumentException( "negative font size: " + font_size );
}
else if ( font_size > Byte.MAX_VALUE ) {
throw new IllegalArgumentException( "font size is too large: " + font_size );
}
_font_size = ( byte ) font_size;
_font = null;
}
public final void setFontStyle( final FontType font_style ) {
_font_style = font_style;
_font = null;
}
public final void setFontStyle( final int font_style ) {
if ( ( font_style == ( Font.BOLD + Font.ITALIC ) ) ) {
setFontStyle( FontType.BOLD_ITALIC );
}
else if ( font_style == Font.ITALIC ) {
setFontStyle( FontType.ITALIC );
}
else if ( font_style == Font.BOLD ) {
setFontStyle( FontType.BOLD );
}
else {
setFontStyle( FontType.PLAIN );
}
}
public final void setFontStyle( final String font_style ) {
if ( font_style.equalsIgnoreCase( FONT_STYLE_BOLD ) ) {
setFontStyle( FontType.BOLD );
}
else if ( font_style.equalsIgnoreCase( FONT_STYLE_ITALIC ) ) {
setFontStyle( FontType.ITALIC );
}
else if ( font_style.equalsIgnoreCase( FONT_STYLE_BOLD_ITALIC ) ) {
setFontStyle( FontType.BOLD_ITALIC );
}
else {
setFontStyle( FontType.PLAIN );
}
}
public final void setNodeColor( final Color node_color ) {
_node_color = node_color;
}
public final void setShape( final NodeShape shape ) {
_shape = shape;
}
public final void setShape( final String shape ) {
if ( shape.equalsIgnoreCase( NODE_SHAPE_CIRCLE ) ) {
setShape( NodeShape.CIRCLE );
}
else if ( shape.equalsIgnoreCase( NODE_SHAPE_RECTANGLE ) ) {
setShape( NodeShape.RECTANGLE );
}
else {
setShape( NodeShape.DEFAULT );
}
}
public final void setSize( final float size ) {
if ( ( size != DEFAULT_SIZE ) && ( size < 0 ) ) {
throw new IllegalArgumentException( "negative size: " + size );
}
_size = size;
}
public final void setTransparency( final float transparency ) {
_transparency = transparency;
}
@Override
public final StringBuffer toNHX() {
throw new UnsupportedOperationException();
}
@Override
public final void toPhyloXML( final Writer writer, final int level, final String indentation ) throws IOException {
for( final Property p : toProperties() ) {
p.toPhyloXML( writer, level, indentation );
}
}
@Override
public final String toString() {
return asText().toString();
}
private String colorToHex( final Color c ) {
return String.format( "#%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue() );
}
private final void init() {
setFontName( null );
setFontStyle( FontType.PLAIN );
setFontSize( DEFAULT_FONT_SIZE );
setFontColor( null );
setShape( NodeShape.DEFAULT );
setFillType( NodeFill.DEFAULT );
setNodeColor( null );
setSize( DEFAULT_SIZE );
setTransparency( DEFAULT_TRANSPARENCY );
_font = null;
}
private final List toProperties() {
final List properties = new ArrayList();
if ( !ForesterUtil.isEmpty( getFontName() ) ) {
properties.add( new Property( FONT_REF, getFontName(), "", FONT_TYPE, AppliesTo.NODE ) );
}
if ( getFontSize() != DEFAULT_FONT_SIZE ) {
properties.add( new Property( FONT_SIZE_REF,
String.valueOf( getFontSize() ),
"",
FONT_SIZE_TYPE,
AppliesTo.NODE ) );
}
if ( getFontStyle() != FontType.PLAIN ) {
String font_style = "";
if ( getFontStyle() == FontType.ITALIC ) {
font_style = FONT_STYLE_ITALIC;
}
else if ( getFontStyle() == FontType.BOLD ) {
font_style = FONT_STYLE_BOLD;
}
else if ( getFontStyle() == FontType.BOLD_ITALIC ) {
font_style = FONT_STYLE_BOLD_ITALIC;
}
else {
throw new RuntimeException( "unknown font style" + getShape() );
}
properties.add( new Property( FONT_STYLE_REF, font_style, "", FONT_STYLE_TYPE, AppliesTo.NODE ) );
}
if ( getFontColor() != null ) {
properties.add( new Property( FONT_COLOR_REF,
colorToHex( getFontColor() ),
"",
FONT_COLOR_TYPE,
AppliesTo.NODE ) );
}
if ( getShape() != NodeShape.DEFAULT ) {
String shape = null;
if ( getShape() == NodeShape.RECTANGLE ) {
shape = NODE_SHAPE_RECTANGLE;
}
else if ( getShape() == NodeShape.CIRCLE ) {
shape = NODE_SHAPE_CIRCLE;
}
else {
throw new RuntimeException( "unknown node shape" + getShape() );
}
properties.add( new Property( NODE_SHAPE_REF, shape, "", NODE_SHAPE_TYPE, AppliesTo.NODE ) );
}
if ( getSize() != DEFAULT_SIZE ) {
properties.add( new Property( NODE_SIZE_REF,
String.valueOf( getSize() ),
"",
NODE_SIZE_TYPE,
AppliesTo.NODE ) );
}
if ( getNodeColor() != null ) {
properties.add( new Property( NODE_COLOR_REF,
colorToHex( getNodeColor() ),
"",
NODE_COLOR_TYPE,
AppliesTo.NODE ) );
}
if ( getFillType() != NodeFill.DEFAULT ) {
String fill = null;
if ( getFillType() == NodeFill.GRADIENT ) {
fill = NODE_FILL_GRADIENT;
}
else if ( getFillType() == NodeFill.NONE ) {
fill = NODE_FILL_NONE;
}
else if ( getFillType() == NodeFill.SOLID ) {
fill = NODE_FILL_SOLID;
}
else {
throw new RuntimeException( "unknown fill type " + getFillType() );
}
properties.add( new Property( NODE_FILL_TYPE_REF, fill, "", NODE_FILL_TYPE_TYPE, AppliesTo.NODE ) );
}
if ( getTransparency() != DEFAULT_TRANSPARENCY ) {
properties.add( new Property( NODE_TRANSPARENCY_REF,
String.valueOf( getTransparency() ),
"",
NODE_TRANSPARENCY_TYPE,
AppliesTo.NODE ) );
}
return properties;
}
public enum FontType {
BOLD, BOLD_ITALIC, ITALIC, PLAIN
}
public enum NodeFill {
DEFAULT, GRADIENT, NONE, SOLID
}
public enum NodeShape {
CIRCLE, DEFAULT, RECTANGLE
}
}
org/forester/phylogeny/factories/ 0000775 0000000 0000000 00000000000 14125307352 016175 5 ustar root root org/forester/phylogeny/factories/PhylogenyFactory.java 0000664 0000000 0000000 00000003501 14125307352 022345 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.factories;
import java.io.IOException;
import org.forester.phylogeny.Phylogeny;
/*
* Interface for Phylogeny factories.
*
* @author Christian M. Zmasek
*/
public interface PhylogenyFactory {
/**
* This must create a Phylogeny from source (e.g. an XML file, an alignment,
* pairwise distances) by using creator (e.g. an XML file parser, an
* algorithm implementation).
*
* @param source
* a source to create a Phylogeny from
* @param creator
* a means to create a Phylogeny
* @return a Phylogeny[] based on argument source
* @throws IOException
*/
public Phylogeny[] create( Object source, Object creator ) throws IOException;
}
org/forester/phylogeny/factories/ParserBasedPhylogenyFactory.java 0000664 0000000 0000000 00000006401 14125307352 024463 0 ustar root root // $Id:
// $
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny.factories;
import java.io.IOException;
import org.forester.io.parsers.PhylogenyParser;
import org.forester.io.parsers.phyloxml.PhyloXmlParser;
import org.forester.phylogeny.Phylogeny;
import org.forester.util.ForesterUtil;
public class ParserBasedPhylogenyFactory implements PhylogenyFactory {
private final static PhylogenyFactory _instance;
static {
try {
_instance = new ParserBasedPhylogenyFactory();
}
catch ( final Throwable e ) {
throw new RuntimeException( e.getMessage() );
}
}
private ParserBasedPhylogenyFactory() {
// Private constructor.
}
@Override
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
@Override
public synchronized Phylogeny[] create( final Object source, final Object parser ) throws IOException {
if ( !( parser instanceof PhylogenyParser ) ) {
throw new IllegalArgumentException( "attempt to use object of type other than PhylogenyParser as creator for ParserBasedPhylogenyFactory" );
}
final PhylogenyParser my_parser = ( PhylogenyParser ) parser;
my_parser.setSource( source );
return my_parser.parse();
}
public synchronized Phylogeny[] create( final Object source, final Object parser, final String schema_location )
throws IOException {
if ( !( parser instanceof PhylogenyParser ) ) {
throw new IllegalArgumentException( "attempt to use object of type other than PhylogenyParser as creator for ParserBasedPhylogenyFactory." );
}
if ( !( parser instanceof PhyloXmlParser ) ) {
throw new IllegalArgumentException( "attempt to use schema location with other than phyloXML parser" );
}
final PhyloXmlParser xml_parser = ( PhyloXmlParser ) parser;
if ( !ForesterUtil.isEmpty( schema_location ) ) {
xml_parser.setValidateAgainstSchema( schema_location );
}
xml_parser.setSource( source );
return xml_parser.parse();
}
public static PhylogenyFactory getInstance() {
return _instance;
}
}
org/forester/phylogeny/Phylogeny.java 0000664 0000000 0000000 00000133572 14125307352 017052 0 ustar root root // $Id:
// FORESTER -- software libraries and applications
// for evolutionary biology research and applications.
//
// Copyright (C) 2008-2009 Christian M. Zmasek
// Copyright (C) 2008-2009 Burnham Institute for Medical Research
// Copyright (C) 2000-2001 Washington University School of Medicine
// and Howard Hughes Medical Institute
// All rights reserved
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
//
// Contact: phylosoft @ gmail . com
// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
package org.forester.phylogeny;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Vector;
import org.forester.io.parsers.nhx.NHXParser;
import org.forester.io.writers.PhylogenyWriter;
import org.forester.phylogeny.PhylogenyNode.NH_CONVERSION_SUPPORT_VALUE_STYLE;
import org.forester.phylogeny.data.BranchData;
import org.forester.phylogeny.data.Confidence;
import org.forester.phylogeny.data.Identifier;
import org.forester.phylogeny.data.PhylogenyDataUtil;
import org.forester.phylogeny.data.Sequence;
import org.forester.phylogeny.data.SequenceRelation;
import org.forester.phylogeny.data.SequenceRelation.SEQUENCE_RELATION_TYPE;
import org.forester.phylogeny.factories.ParserBasedPhylogenyFactory;
import org.forester.phylogeny.factories.PhylogenyFactory;
import org.forester.phylogeny.iterators.ExternalForwardIterator;
import org.forester.phylogeny.iterators.LevelOrderTreeIterator;
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 {
public final static boolean ALLOW_MULTIPLE_PARENTS_DEFAULT = false;
private PhylogenyNode _root;
private boolean _rooted;
private boolean _allow_multiple_parents;
private String _name;
private String _type;
private String _description;
private String _distance_unit;
private Confidence _confidence;
private Identifier _identifier;
private boolean _rerootable;
private HashMap _id_to_node_map;
private List _external_nodes_set;
private Collection _sequenceRelationQueries;
private Collection _relevant_sequence_relation_types;
/**
* Default Phylogeny constructor. Constructs an empty Phylogeny.
*/
public Phylogeny() {
init();
}
/**
* Adds this Phylogeny to the list of child nodes of PhylogenyNode parent
* and sets the parent of this to parent.
*
* @param n
* the PhylogenyNode to add
*/
public void addAsChild( final PhylogenyNode parent ) {
if ( isEmpty() ) {
throw new IllegalArgumentException( "Attempt to add an empty tree." );
}
if ( !isRooted() ) {
throw new IllegalArgumentException( "Attempt to add an unrooted tree." );
}
parent.addAsChild( getRoot() );
externalNodesHaveChanged();
}
public void addAsSibling( final PhylogenyNode sibling ) {
if ( isEmpty() ) {
throw new IllegalArgumentException( "Attempt to add an empty tree." );
}
if ( !isRooted() ) {
throw new IllegalArgumentException( "Attempt to add an unrooted tree." );
}
final int sibling_index = sibling.getChildNodeIndex();
final PhylogenyNode new_node = new PhylogenyNode();
final PhylogenyNode sibling_parent = sibling.getParent();
new_node.setChild1( sibling );
new_node.setChild2( getRoot() );
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;
new_node.setDistanceToParent( new_dist );
sibling.setDistanceToParent( new_dist );
externalNodesHaveChanged();
}
/**
* This calculates the height of the subtree emanating at n for rooted,
* tree-shaped phylogenies
*
* @param n
* the root-node of a subtree
* @return the height of the subtree emanating at n
*/
public double calculateSubtreeHeight( final PhylogenyNode n ) {
if ( n.isExternal() || n.isCollapse() ) {
return ForesterUtil.isLargerOrEqualToZero( n.getDistanceToParent() );
}
else {
double max = -Double.MAX_VALUE;
for( int i = 0; i < n.getNumberOfDescendants(); ++i ) {
final double l = calculateSubtreeHeight( n.getChildNode( i ) );
if ( l > max ) {
max = l;
}
}
return max + ForesterUtil.isLargerOrEqualToZero( n.getDistanceToParent() );
}
}
public void clearHashIdToNodeMap() {
setIdToNodeMap( null );
}
/**
* Returns a deep copy of this Phylogeny.
* ]
* (The resulting Phylogeny has its references in the external nodes
* corrected, if they are lacking/obsolete in this.)
*/
public Phylogeny copy() {
return copy( _root );
}
/**
* Returns a deep copy of this Phylogeny.
*
* (The resulting Phylogeny has its references in the external nodes
* corrected, if they are lacking/obsolete in this.)
*/
public Phylogeny copy( final PhylogenyNode source ) {
final Phylogeny tree = new Phylogeny();
if ( isEmpty() ) {
tree.init();
return tree;
}
tree._rooted = _rooted;
tree._name = new String( _name );
tree._description = new String( _description );
tree._type = new String( _type );
tree._rerootable = _rerootable;
tree._distance_unit = new String( _distance_unit );
if ( _confidence != null ) {
tree._confidence = ( Confidence ) _confidence.copy();
}
if ( _identifier != null ) {
tree._identifier = ( Identifier ) _identifier.copy();
}
tree.setAllowMultipleParents( isAllowMultipleParents() );
tree._root = PhylogenyMethods.copySubTree( source );
return tree;
}
/**
* Returns a shallow copy of this Phylogeny.
*
* (The resulting Phylogeny has its references in the external nodes
* corrected, if they are lacking/obsolete in this.)
*/
public Phylogeny copyShallow() {
return copyShallow( _root );
}
public Phylogeny copyShallow( final PhylogenyNode source ) {
final Phylogeny tree = new Phylogeny();
if ( isEmpty() ) {
tree.init();
return tree;
}
tree._rooted = _rooted;
tree._name = _name;
tree._description = _description;
tree._type = _type;
tree._rerootable = _rerootable;
tree._distance_unit = _distance_unit;
tree._confidence = _confidence;
tree._identifier = _identifier;
tree.setAllowMultipleParents( isAllowMultipleParents() );
tree._root = PhylogenyMethods.copySubTreeShallow( source );
return tree;
}
/**
* Need to call clearHashIdToNodeMap() afterwards (not done automatically
* to allow client multiple deletions in linear time).
* Need to call 'recalculateNumberOfExternalDescendants(boolean)' after this
* if tree is to be displayed.
*
* @param remove_us the parent node of the subtree to be deleted
*/
public void deleteSubtree( final PhylogenyNode remove_us, final boolean collapse_resulting_node_with_one_desc ) {
if ( isEmpty() || ( remove_us.isRoot() && ( getNumberOfExternalNodes() != 1 ) ) ) {
return;
}
if ( remove_us.isRoot() && ( getNumberOfExternalNodes() == 1 ) ) {
init();
}
else if ( !collapse_resulting_node_with_one_desc ) {
remove_us.getParent().removeChildNode( remove_us );
}
else {
final PhylogenyNode removed_node = remove_us;
final PhylogenyNode p = remove_us.getParent();
if ( p.isRoot() ) {
if ( p.getNumberOfDescendants() == 2 ) {
if ( removed_node.isFirstChildNode() ) {
setRoot( getRoot().getChildNode( 1 ) );
getRoot().setParent( null );
}
else {
setRoot( getRoot().getChildNode( 0 ) );
getRoot().setParent( null );
}
}
else {
p.removeChildNode( removed_node.getChildNodeIndex() );
}
}
else {
final PhylogenyNode pp = removed_node.getParent().getParent();
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() ) );
pp.setChildNode( pi, p.getChildNode( 1 ) );
}
else {
p.getChildNode( 0 ).setDistanceToParent( PhylogenyMethods.addPhylogenyDistances( p
.getDistanceToParent(), p.getChildNode( 0 ).getDistanceToParent() ) );
pp.setChildNode( pi, p.getChildNode( 0 ) );
}
}
else {
p.removeChildNode( removed_node.getChildNodeIndex() );
}
}
}
remove_us.removeConnections();
externalNodesHaveChanged();
}
public void externalNodesHaveChanged() {
_external_nodes_set = null;
}
public String[] getAllExternalNodeNames() {
int i = 0;
if ( isEmpty() ) {
return null;
}
final String[] names = new String[ getNumberOfExternalNodes() ];
for( final PhylogenyNodeIterator iter = iteratorExternalForward(); iter.hasNext(); ) {
names[ i++ ] = new String( iter.next().getName() );
}
return names;
}
public Confidence getConfidence() {
return _confidence;
}
public String getDescription() {
return _description;
}
public String getDistanceUnit() {
return _distance_unit;
}
public final static Phylogeny createInstanceFromNhxString( final String nhx ) throws IOException {
final PhylogenyFactory factory = ParserBasedPhylogenyFactory.getInstance();
return factory.create( nhx, new NHXParser() )[ 0 ];
}
/**
*
* Warning. The order of the returned nodes is random
* -- and hence cannot be relied on.
*
* @return Unordered set of PhylogenyNode
*/
public List getExternalNodes() {
if ( _external_nodes_set == null ) {
_external_nodes_set = new ArrayList();
for( final PhylogenyNodeIterator it = iteratorPostorder(); it.hasNext(); ) {
final PhylogenyNode n = it.next();
if ( n.isExternal() ) {
_external_nodes_set.add( n );
}
}
}
return _external_nodes_set;
}
/**
* 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() {
if ( isEmpty() ) {
throw new FailedConditionCheckException( "attempt to obtain first external node of empty phylogeney" );
}
PhylogenyNode node = getRoot();
while ( node.isInternal() ) {
node = node.getFirstChildNode();
}
return node;
}
/**
* This calculates the height for rooted, tree-shaped phylogenies. The
* height is the longest distance from the root to an external node. Please
* note. Child nodes of collapsed nodes are ignored -- which is useful for
* display purposes but might be misleading for other applications.
*
* @return the height for rooted, tree-shaped phylogenies
*/
public double getHeight() {
if ( isEmpty() ) {
return 0.0;
}
return calculateSubtreeHeight( getRoot() );
}
public Identifier getIdentifier() {
return _identifier;
}
/**
* Returns the name of this Phylogeny.
*/
public String getName() {
return _name;
}
/**
* Finds the PhylogenyNode of this Phylogeny which has a matching ID number.
* @return PhylogenyNode with matching ID, null if not found
*/
public PhylogenyNode getNode( final long id ) throws NoSuchElementException {
if ( isEmpty() ) {
throw new NoSuchElementException( "attempt to get node in an empty phylogeny" );
}
if ( ( getIdToNodeMap() == null ) || getIdToNodeMap().isEmpty() ) {
reHashIdToNodeMap();
}
return getIdToNodeMap().get( id );
}
/**
* Returns a PhylogenyNode of this Phylogeny which has a matching name.
* Throws an Exception if seqname is not present in this or not unique.
*
* @param name
* name (String) of PhylogenyNode to find
* @return PhylogenyNode with matchin name
*/
public PhylogenyNode getNode( final String name ) {
if ( isEmpty() ) {
return null;
}
final List nodes = getNodes( name );
if ( ( nodes == null ) || ( nodes.size() < 1 ) ) {
throw new IllegalArgumentException( "node named \"" + name + "\" not found" );
}
if ( nodes.size() > 1 ) {
throw new IllegalArgumentException( "node named \"" + name + "\" not unique" );
}
return nodes.get( 0 );
}
/**
* This is time-inefficient since it runs a iterator each time it is called.
*
*/
public int getNodeCount() {
if ( isEmpty() ) {
return 0;
}
int c = 0;
for( final PhylogenyNodeIterator it = iteratorPreorder(); it.hasNext(); it.next() ) {
++c;
}
return c;
}
/**
* Returns a List with references to all Nodes of this Phylogeny which have
* a matching name.
*
* @param name
* name (String) of Nodes to find
* @return Vector of references to Nodes of this Phylogeny with matching
* names
* @see #getNodesWithMatchingSpecies(String)
*/
public List getNodes( final String name ) {
if ( isEmpty() ) {
return null;
}
final List nodes = new ArrayList();
for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
final PhylogenyNode n = iter.next();
if ( n.getName().equals( name ) ) {
nodes.add( n );
}
}
return nodes;
}
public List getNodesViaSequenceName( final String seq_name ) {
if ( isEmpty() ) {
return null;
}
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 ) ) {
nodes.add( n );
}
}
return nodes;
}
public List getNodesViaSequenceSymbol( final String seq_name ) {
if ( isEmpty() ) {
return null;
}
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 ) ) {
nodes.add( n );
}
}
return nodes;
}
public List