regex search
[jalview.git] / forester / java / src / org / forester / phylogeny / Phylogeny.java
index 5649a5b..115e885 100644 (file)
@@ -23,7 +23,7 @@
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 //
 // Contact: phylosoft @ gmail . com
-// WWW: www.phylosoft.org/forester
+// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
 
 package org.forester.phylogeny;
 
@@ -38,6 +38,7 @@ 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;
@@ -47,6 +48,8 @@ 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;
@@ -68,7 +71,7 @@ public class Phylogeny {
     private Confidence                                          _confidence;
     private Identifier                                          _identifier;
     private boolean                                             _rerootable;
-    private HashMap<Integer, PhylogenyNode>                     _id_to_node_map;
+    private HashMap<Long, PhylogenyNode>                        _id_to_node_map;
     private List<PhylogenyNode>                                 _external_nodes_set;
     private Collection<Sequence>                                _sequenceRelationQueries;
     private Collection<SequenceRelation.SEQUENCE_RELATION_TYPE> _relevant_sequence_relation_types;
@@ -304,6 +307,11 @@ public class Phylogeny {
         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
@@ -390,7 +398,7 @@ public class Phylogeny {
      * 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 int id ) throws NoSuchElementException {
+    public PhylogenyNode getNode( final long id ) throws NoSuchElementException {
         if ( isEmpty() ) {
             throw new NoSuchElementException( "attempt to get node in an empty phylogeny" );
         }
@@ -414,10 +422,10 @@ public class Phylogeny {
         }
         final List<PhylogenyNode> nodes = getNodes( name );
         if ( ( nodes == null ) || ( nodes.size() < 1 ) ) {
-            throw new IllegalArgumentException( "node named [" + name + "] not found" );
+            throw new IllegalArgumentException( "node named \"" + name + "\" not found" );
         }
         if ( nodes.size() > 1 ) {
-            throw new IllegalArgumentException( "node named [" + name + "] not unique" );
+            throw new IllegalArgumentException( "node named \"" + name + "\" not unique" );
         }
         return nodes.get( 0 );
     }
@@ -475,6 +483,34 @@ public class Phylogeny {
         return nodes;
     }
 
+    public List<PhylogenyNode> getNodesViaSequenceSymbol( final String seq_name ) {
+        if ( isEmpty() ) {
+            return null;
+        }
+        final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
+        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<PhylogenyNode> getNodesViaGeneName( final String seq_name ) {
+        if ( isEmpty() ) {
+            return null;
+        }
+        final List<PhylogenyNode> nodes = new ArrayList<PhylogenyNode>();
+        for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
+            final PhylogenyNode n = iter.next();
+            if ( n.getNodeData().isHasSequence() && n.getNodeData().getSequence().getGeneName().equals( seq_name ) ) {
+                nodes.add( n );
+            }
+        }
+        return nodes;
+    }
+
     public List<PhylogenyNode> getNodesViaTaxonomyCode( final String taxonomy_code ) {
         if ( isEmpty() ) {
             return null;
@@ -556,6 +592,22 @@ public class Phylogeny {
         return c;
     }
 
+    public int getNumberOfInternalNodes() {
+        if ( isEmpty() ) {
+            return 0;
+        }
+        int c = 0;
+        for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
+            if ( iter.next().isInternal() ) {
+                ++c;
+            }
+        }
+        if ( !isRooted() ) {
+            --c;
+        }
+        return c;
+    }
+
     /**
      * Returns the sum of external Nodes of this Phylogeny (int).
      */
@@ -725,7 +777,7 @@ public class Phylogeny {
             return;
         }
         _id_to_node_map = null;
-        int max = 0;
+        long max = 0;
         for( final PhylogenyNodeIterator it = iteratorPreorder(); it.hasNext(); ) {
             final PhylogenyNode node = it.next();
             if ( node.isRoot() ) {
@@ -795,40 +847,11 @@ public class Phylogeny {
      * @param id
      *            ID (int) of PhylogenyNode of this Phylogeny
      */
-    public void reRoot( final int id ) {
+    public void reRoot( final long id ) {
         reRoot( getNode( id ) );
     }
 
     /**
-     * Places the root of this Phylogeny on Branch b. The new root is always
-     * placed on the middle of the branch b.
-     * 
-     */
-    public void reRoot( final PhylogenyBranch b ) {
-        final PhylogenyNode n1 = b.getFirstNode();
-        final PhylogenyNode n2 = b.getSecondNode();
-        if ( n1.isExternal() ) {
-            reRoot( n1 );
-        }
-        else if ( n2.isExternal() ) {
-            reRoot( n2 );
-        }
-        else if ( ( n2 == n1.getChildNode1() ) || ( n2 == n1.getChildNode2() ) ) {
-            reRoot( n2 );
-        }
-        else if ( ( n1 == n2.getChildNode1() ) || ( n1 == n2.getChildNode2() ) ) {
-            reRoot( n1 );
-        }
-        else if ( ( n1.getParent() != null ) && n1.getParent().isRoot()
-                && ( ( n1.getParent().getChildNode1() == n2 ) || ( n1.getParent().getChildNode2() == n2 ) ) ) {
-            reRoot( n1 );
-        }
-        else {
-            throw new IllegalArgumentException( "reRoot( Branch b ): b is not a branch." );
-        }
-    }
-
-    /**
      * Places the root of this Phylogeny on the parent branch PhylogenyNode n.
      * The new root is always placed on the middle of the branch.
      * <p>
@@ -1024,7 +1047,7 @@ public class Phylogeny {
         _identifier = identifier;
     }
 
-    public void setIdToNodeMap( final HashMap<Integer, PhylogenyNode> idhash ) {
+    public void setIdToNodeMap( final HashMap<Long, PhylogenyNode> idhash ) {
         _id_to_node_map = idhash;
     }
 
@@ -1057,7 +1080,7 @@ public class Phylogeny {
 
     public void setRoot( final PhylogenyNode n ) {
         _root = n;
-    } // setRoot( PhylogenyNode )
+    }
 
     /**
      * Sets whether this Phylogeny is rooted or not.
@@ -1075,14 +1098,12 @@ public class Phylogeny {
     }
 
     public String toNewHampshire() {
-        return toNewHampshire( false, NH_CONVERSION_SUPPORT_VALUE_STYLE.NONE );
+        return toNewHampshire( NH_CONVERSION_SUPPORT_VALUE_STYLE.NONE );
     }
 
-    public String toNewHampshire( final boolean simple_nh,
-                                  final NH_CONVERSION_SUPPORT_VALUE_STYLE nh_conversion_support_style ) {
+    public String toNewHampshire( final NH_CONVERSION_SUPPORT_VALUE_STYLE nh_conversion_support_style ) {
         try {
-            return new PhylogenyWriter().toNewHampshire( this, simple_nh, true, nh_conversion_support_style )
-                    .toString();
+            return new PhylogenyWriter().toNewHampshire( this, true, nh_conversion_support_style ).toString();
         }
         catch ( final IOException e ) {
             throw new Error( "this should not have happend: " + e.getMessage() );
@@ -1152,7 +1173,7 @@ public class Phylogeny {
         return;
     } // unRoot()
 
-    private HashMap<Integer, PhylogenyNode> getIdToNodeMap() {
+    private HashMap<Long, PhylogenyNode> getIdToNodeMap() {
         return _id_to_node_map;
     }
 
@@ -1257,7 +1278,7 @@ public class Phylogeny {
         if ( isEmpty() ) {
             return;
         }
-        setIdToNodeMap( new HashMap<Integer, PhylogenyNode>() );
+        setIdToNodeMap( new HashMap<Long, PhylogenyNode>() );
         for( final PhylogenyNodeIterator iter = iteratorPreorder(); iter.hasNext(); ) {
             final PhylogenyNode node = iter.next();
             getIdToNodeMap().put( node.getId(), node );