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;
* @return distance between node1 and node2
*/
public double calculateDistance( final PhylogenyNode node1, final PhylogenyNode node2 ) {
- final PhylogenyNode lca =calculateLCA( node1, node2 );
+ final PhylogenyNode lca = calculateLCA( node1, node2 );
final PhylogenyNode n1 = node1;
final PhylogenyNode n2 = node2;
return ( PhylogenyMethods.getDistance( n1, lca ) + PhylogenyMethods.getDistance( n2, lca ) );
* @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() ) /*&& node1.getParent() != null */ ) {
+ if ( ( node1.getParent() == node2.getParent() ) ) {
return node1.getParent();
}
- int depth1 = node1.calculateDepth() ;
- int depth2 = node2.calculateDepth() ;
+ int depth1 = node1.calculateDepth();
+ int depth2 = node2.calculateDepth();
while ( ( depth1 > -1 ) && ( depth2 > -1 ) ) {
if ( depth1 > depth2 ) {
node1 = node1.getParent();
throw new IllegalArgumentException( "illegal attempt to calculate LCA of two nodes which do not share a common root" );
}
+ public static final void preOrderReId( final Phylogeny phy ) {
+ if ( phy.isEmpty() ) {
+ return;
+ }
+ phy.setIdToNodeMap( null );
+ int i = PhylogenyNode.getNodeCount();
+ for( final PhylogenyNodeIterator it = phy.iteratorPreorder(); it.hasNext(); ) {
+ it.next().setId( i++ );
+ }
+ PhylogenyNode.setNodeCount( i );
+ }
+
+ /**
+ * 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;
+ }
+
/**
* Returns all orthologs of the external PhylogenyNode n of this Phylogeny.
* Orthologs are returned as List of node references.
* of this Phylogeny, null if this Phylogeny is empty or if n is
* internal
*/
- public List<PhylogenyNode> getOrthologousNodes( final Phylogeny phy, final PhylogenyNode node ) {
+ public final static List<PhylogenyNode> getOrthologousNodes( final Phylogeny phy, final PhylogenyNode node ) {
final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
+ PhylogenyMethods.preOrderReId( phy );
final PhylogenyNodeIterator it = phy.iteratorExternalForward();
while ( it.hasNext() ) {
final PhylogenyNode temp_node = it.next();
- if ( ( temp_node != node ) && isAreOrthologous( node, temp_node ) ) {
+ if ( ( temp_node != node ) && !calculateLCAonTreeWithIdsInPreOrder( node, temp_node ).isDuplication() ) {
nodes.add( temp_node );
}
}
return nodes;
}
- public static boolean isAreOrthologous( final PhylogenyNode node1, final PhylogenyNode node2 ) {
- return !calculateLCA( node1, node2 ).isDuplication();
+ public static final HashMap<String, PhylogenyNode> createNameToExtNodeMap( final Phylogeny phy ) {
+ final HashMap<String, PhylogenyNode> nodes = new HashMap<String, PhylogenyNode>();
+ for( final PhylogenyNodeIterator iter = phy.iteratorExternalForward(); iter.hasNext(); ) {
+ final PhylogenyNode n = iter.next();
+ nodes.put( n.getName(), n );
+ }
+ return nodes;
}
public final static Phylogeny[] readPhylogenies( final PhylogenyParser parser, final File file ) throws IOException {
return PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT;
}
- // Helper for getUltraParalogousNodes( PhylogenyNode ).
- public static boolean areAllChildrenDuplications( final PhylogenyNode n ) {
+ public final static boolean isAllDecendentsAreDuplications( final PhylogenyNode n ) {
if ( n.isExternal() ) {
- return false;
+ return true;
}
else {
if ( n.isDuplication() ) {
- //FIXME test me!
for( final PhylogenyNode desc : n.getDescendants() ) {
- if ( !areAllChildrenDuplications( desc ) ) {
+ if ( !isAllDecendentsAreDuplications( desc ) ) {
return false;
}
}
* @param n
* external PhylogenyNode whose strictly speciation related Nodes
* are to be returned
- * @return Vector of references to all strictly speciation related Nodes of
+ * @return References to all strictly speciation related Nodes of
* PhylogenyNode n of this Phylogeny, null if this Phylogeny is
* empty or if n is internal
*/
public static List<PhylogenyNode> getSuperOrthologousNodes( final PhylogenyNode n ) {
// FIXME
- PhylogenyNode node = n, deepest = null;
+ PhylogenyNode node = n;
+ PhylogenyNode deepest = null;
final List<PhylogenyNode> v = new ArrayList<PhylogenyNode>();
if ( !node.isExternal() ) {
return null;
// FIXME test me
PhylogenyNode node = n;
if ( !node.isExternal() ) {
- return null;
+ throw new IllegalArgumentException( "attempt to get ultra-paralogous nodes of internal node" );
}
- while ( !node.isRoot() && node.getParent().isDuplication() && areAllChildrenDuplications( node.getParent() ) ) {
+ while ( !node.isRoot() && node.getParent().isDuplication() && isAllDecendentsAreDuplications( node.getParent() ) ) {
node = node.getParent();
}
final List<PhylogenyNode> nodes = node.getAllExternalDescendants();
return nodes;
}
- public static String inferCommonPartOfScientificNameOfDescendants( final PhylogenyNode node ) {
- final List<PhylogenyNode> descs = node.getDescendants();
- String sn = null;
- for( final PhylogenyNode n : descs ) {
- if ( !n.getNodeData().isHasTaxonomy()
- || ForesterUtil.isEmpty( n.getNodeData().getTaxonomy().getScientificName() ) ) {
- return null;
- }
- else if ( sn == null ) {
- sn = n.getNodeData().getTaxonomy().getScientificName().trim();
- }
- else {
- String sn_current = n.getNodeData().getTaxonomy().getScientificName().trim();
- if ( !sn.equals( sn_current ) ) {
- boolean overlap = false;
- while ( ( sn.indexOf( ' ' ) >= 0 ) || ( sn_current.indexOf( ' ' ) >= 0 ) ) {
- if ( ForesterUtil.countChars( sn, ' ' ) > ForesterUtil.countChars( sn_current, ' ' ) ) {
- sn = sn.substring( 0, sn.lastIndexOf( ' ' ) ).trim();
- }
- else {
- sn_current = sn_current.substring( 0, sn_current.lastIndexOf( ' ' ) ).trim();
- }
- if ( sn.equals( sn_current ) ) {
- overlap = true;
- break;
- }
- }
- if ( !overlap ) {
- return null;
- }
- }
- }
- }
- return sn;
- }
-
public static boolean isHasExternalDescendant( final PhylogenyNode node ) {
for( int i = 0; i < node.getNumberOfDescendants(); ++i ) {
if ( node.getChildNode( i ).isExternal() ) {
* a reference Phylogeny
* @param to_be_stripped
* Phylogeny to be stripped
- * @return number of external nodes removed from to_be_stripped
+ * @return nodes removed from to_be_stripped
*/
- public static int taxonomyBasedDeletionOfExternalNodes( final Phylogeny reference, final Phylogeny to_be_stripped ) {
+ public static List<PhylogenyNode> taxonomyBasedDeletionOfExternalNodes( final Phylogeny reference,
+ final Phylogeny to_be_stripped ) {
final Set<String> ref_ext_taxo = new HashSet<String>();
for( final PhylogenyNodeIterator it = reference.iteratorExternalForward(); it.hasNext(); ) {
final PhylogenyNode n = it.next();
ref_ext_taxo.add( n.getNodeData().getTaxonomy().getTaxonomyCode() );
}
}
- System.out.println( " ref_ext_tax:" );
- for( final String string : ref_ext_taxo ) {
- System.out.println( string );
- }
final ArrayList<PhylogenyNode> nodes_to_delete = new ArrayList<PhylogenyNode>();
for( final PhylogenyNodeIterator it = to_be_stripped.iteratorExternalForward(); it.hasNext(); ) {
final PhylogenyNode n = it.next();
nodes_to_delete.add( n );
}
}
- System.out.println( " to delete:" );
- for( final PhylogenyNode string : nodes_to_delete ) {
- System.out.println( string.getNodeData().getTaxonomy().getTaxonomyCode() );
- }
- for( final PhylogenyNode phylogenyNode : nodes_to_delete ) {
- to_be_stripped.deleteSubtree( phylogenyNode, true );
+ 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.size();
+ return nodes_to_delete;
}
/**
TAXONOMY_ID;
}
- public static enum TAXONOMY_EXTRACTION {
- NO, YES, PFAM_STYLE_ONLY;
- }
-
public static enum DESCENDANT_SORT_PRIORITY {
TAXONOMY, SEQUENCE, NODE_NAME;
}