Author: chinmoy
Date: 2009-06-26 10:32:04 -0700 (Fri, 26 Jun 2009)
New Revision: 17109

Modified:
   
csplugins/trunk/soc/chinmoy/phylotree/src/org/cytoscape/phylotree/parser/PhylipTreeImpl.java
   
csplugins/trunk/soc/chinmoy/phylotree/src/org/cytoscape/phylotree/parser/Phylotree.java
Log:


Modified: 
csplugins/trunk/soc/chinmoy/phylotree/src/org/cytoscape/phylotree/parser/PhylipTreeImpl.java
===================================================================
--- 
csplugins/trunk/soc/chinmoy/phylotree/src/org/cytoscape/phylotree/parser/PhylipTreeImpl.java
        2009-06-26 15:33:11 UTC (rev 17108)
+++ 
csplugins/trunk/soc/chinmoy/phylotree/src/org/cytoscape/phylotree/parser/PhylipTreeImpl.java
        2009-06-26 17:32:04 UTC (rev 17109)
@@ -13,269 +13,326 @@
 
 
 public class PhylipTreeImpl implements Phylotree {
- 
- private String treeStr;
- private LinkedList<PhylotreeNode> nodeList = null;
 
- 
- // Constructors
- 
- /*
- * Reads a PHYLIP string
- */ 
- 
- public PhylipTreeImpl(String pTreeStr){
-  this.treeStr = pTreeStr;
-  nodeList = new LinkedList<PhylotreeNode>();
-  parse();
- }
- 
- /*
- * Reads a PHYLIP file
- */ 
- 
- public PhylipTreeImpl(File pTreeFile){
- 
-  treeStr = getTreeTextFromFile(pTreeFile);
-  parse();
- }
+       private String treeStr; // The tree in string format
+       private List<PhylotreeNode> nodeList = null;   // List of all nodes in 
the tree
 
- private String getTreeTextFromFile(File pTreeFile){
-  String retStr = null;
-  
-  try{
-            
-            BufferedReader reader = new BufferedReader(new 
FileReader(pTreeFile));
-            retStr = reader.readLine();
-         return retStr;
-  }      
-      catch(IOException l)
-      {
-       return null;
-      }
-      
-      catch(NullPointerException l)
-      {
-      return null;
-      }
-      
-    
-  
-  //open the TreeFile, read its content
-  // retStr = read the text from file
-  
- 
- }
- 
- private void parse(){
-  //This is the core of the parser
-  //Parse the treeStr, and populate the data items,
-  
-  
-   Stack <String> stack = new Stack<String>(); 
-   LinkedList<String> list = new LinkedList<String>();
-   LinkedList<PhylipNode> childNodeList = new LinkedList<PhylipNode>();
-   LinkedList<Double> branchLengthList = new LinkedList<Double>();
-   
-   LinkedList<PhylipEdge> allEdges = new LinkedList<PhylipEdge>();
-   
-   Iterator<String> iterator;
-   Iterator<PhylipNode> childNodeListIterator;
-   Iterator<Double> branchLengthListIterator;
-   Double branchLength = 0.0;
-   // Split the input string into a list
-   String [] substrings = treeStr.split(":|,|;");
-   for(int i =0; i<substrings.length;i++)
-   {
-    substrings[i] = substrings[i].trim();
-   }
 
-   // Parse the input into a list
-   for(int i = 0; i<substrings.length; i++)
-   {
+       // Constructors
 
-    if (substrings[i].charAt(0) == '(')
-    {
-     list.add("(");
-     for (int k = 1; k<substrings[i].length(); k++)
-     {
-      if(substrings[i].charAt(k) == '(')
-      {
-       list.add("("); 
-      }
-      else
-      {
-       String[] tempSub = substrings[i].split("\\(+");
+       /*
+        * Constructor that accepts a string as an argument
+        */ 
+       public PhylipTreeImpl(String pTreeStr){
+               this.treeStr = pTreeStr;
+               nodeList = new LinkedList<PhylotreeNode>();
+               parse();
+       }
 
-       list.add(tempSub[1]);
-       break;
+       /*
+        * Constructor that accepts a file as an argument
+        */ 
+       public PhylipTreeImpl(File pTreeFile){
 
+               treeStr = getTreeTextFromFile(pTreeFile);
+               parse();
+       }
 
-      }
-     }         
-    }
+       /*      
+        * Reads a PHYLIP file and returns the tree in string format
+        */
+       private String getTreeTextFromFile(File pTreeFile){
+               String retStr = null;
 
-    else if(substrings[i].charAt(0) != '(' && substrings[i].charAt(0) != ')')
-    {
-     String[] tempSub2 = substrings[i].split("\\)");
-     list.add(tempSub2[0]);
-    }
-    if(substrings[i].charAt(substrings[i].length()-1)== ')')
-    {
-     list.add(")");
-    }
+               try
+               {
+                       // Read the file to obtain the tree
+                       BufferedReader reader = new BufferedReader(new 
FileReader(pTreeFile));
+                       retStr = reader.readLine();
+                       return retStr;
+               }      
+               catch(IOException l)
+               {
+                       // Error reading file
+                       return null;
+               }
 
+               catch(NullPointerException l)
+               {
+                       // File not found
+                       return null;
+               }
 
-   }
+       }
 
-   
-   // Parse the list into a node and edge lists using a stack
-   
-   iterator = list.iterator();
-   int tempNodeIndex = 0;
-   while(iterator.hasNext())
-   {
-    Object tempObj = iterator.next();
-    String tempStr = (String) tempObj;
-    
-    if(!tempStr.equals(")"))
-     {
-     stack.push(tempStr);
-     // Ignore
-     }
-    if(tempStr.equals(")"))
-    {
-     String stackTop = stack.pop();
+       /*
+        * parse()
+        * Traverses the tree string 'treeStr'and creates appropriate nodes and 
edges
+        */
+       private void parse()
+       {
+               Stack <String> stack = new Stack<String>();  
+               Stack<PhylipNode> parentNodeStack = new Stack<PhylipNode>();
+               List<String> list = new LinkedList<String>();
+               List<PhylipNode> childNodeList = new LinkedList<PhylipNode>();
+               // ChildNodeList records the children of a node for the purpose 
of creating edges.
+               // It is cleared after the edges for each node are created. 
 
-     while(!stackTop.equals("("))
-     {
+               List<Double> branchLengthList = new LinkedList<Double>(); 
+               // Reflects the ChildNodeList but records the edge lengths 
instead of the child nodes. 
 
-      try
-      {
-       branchLength = Double.parseDouble(stackTop);
-      }
-      catch(NumberFormatException f)
-      {
-       // Add a node
 
-       PhylipNode nodeA = new PhylipNode(stackTop);
-       nodeList.add(nodeA);
-       // Store each node label into a list
+               // Iterators for lists
+               Iterator<String> iterator;
+               Iterator<PhylipNode> childNodeListIterator;
+               Iterator<Double> branchLengthListIterator;
 
-       childNodeList.add(nodeA);
-       branchLengthList.add(branchLength);
-      }
+               Double branchLength = 0.0;
 
-      stackTop = stack.pop();
-     }
-     if(stackTop.equals("("))
-     {
-      // Add a temporary parent node
-      String tempNodeLabel = "tempNode"+tempNodeIndex;
-      PhylipNode tempNode = new PhylipNode(tempNodeLabel);
-     
-      if(stack.isEmpty())
-      {
-        nodeList.add(tempNode);
-        
-      }
-      
-      tempNodeIndex++;
+               // Split the tree string into a list
+               String [] substrings = treeStr.split(":|,|;");
 
-      // Add edges between the temporary parent and the children
-      childNodeListIterator = childNodeList.iterator();
-      branchLengthListIterator = branchLengthList.iterator();
-      int tempEdgeIndex = 0;
-      while(childNodeListIterator.hasNext())
-      {
-       PhylipNode childNode = childNodeListIterator.next();
-       String tempEdgeLabel = "edge"+tempEdgeIndex;
-       
-       PhylipEdge edge = new PhylipEdge(tempNode, childNode);
-       Double edgeLength = branchLengthListIterator.next();
-       branchLengthListIterator.remove();
-       edge.setEdgeLength(edgeLength);
-        
System.out.println(edge.getSourceNode().getName()+"<-->"+edge.getTargetNode().getName()+":"+edgeLength);
-       tempEdgeIndex++;
-       allEdges.add(edge);  // temporarily
-       
-      }
-      childNodeList.clear();
 
-      // Add the temporary Parent node back into the stack
-      stack.push(tempNodeLabel);
-      
+               // Parse the input into a list ignoring ',' but adding names, 
lengths and parentheses
 
-     }
-    }
+               for(int i = 0; i<substrings.length; i++)
+               {
+                       substrings[i] = substrings[i].trim();
 
-   } 
+                       // For every parenthesis encountered, add it to the list
+                       if (substrings[i].charAt(0) == '(')
+                       {
+                               list.add("(");
+                               for (int k = 1; k<substrings[i].length(); k++)
+                               {
+                                       if(substrings[i].charAt(k) == '(')
+                                       {
+                                               list.add("("); 
+                                       }
+                                       else
+                                       {
+                                               // Split the remainder of the 
string around the '(' if a name is encountered
+                                               // Add the name to the list 
+                                               String[] tempSub = 
substrings[i].split("\\(+");
 
- 
-  }
-  
-  
- 
- 
- // interface methods
- public LinkedList<PhylotreeNode> getNodeList(){
-  return nodeList;
- }
- 
- //Get the edges for given node
- public List<PhylotreeEdge> getEdges(PhylotreeNode pNode){
-  Vector<PhylotreeEdge> retValue = new Vector<PhylotreeEdge>();
+                                               list.add(tempSub[1]);
+                                               break;
 
-  // a node could have multiple edges
-  //retValue.add(connectedNode1);
-  //retValue.add(connectedNode2);
-  //retValue.add(connectedNodeN);
-  
-  return retValue;
- }
- 
- public List getEdgeAttribute(PhylotreeEdge pEdge){
-  return null;
- }
- 
- 
- // inner classes
- class PhylipNode implements PhylotreeNode {
-  private String nodeName = null;
-  
-  public PhylipNode(String pNodeName){
-   this.nodeName = pNodeName;
-  }
-  
-  public String getName(){
-   return nodeName;
-  }
- }
- 
- 
- class PhylipEdge implements PhylotreeEdge {
-  private PhylipNode sourceNode = null;
-  private PhylipNode targetNode = null;
-  private double edgeLength = 0.0;
-  
-  public PhylipEdge(PhylipNode pSourceNode, PhylipNode pTargetNode){
-   this.sourceNode = pSourceNode;
-   this.targetNode = pTargetNode;
-  }
-  
-  public void setEdgeLength(double length){
-   edgeLength = length; 
-  }
-  
-  public double getEdgeLength() {
-   return edgeLength;
-  }
-  public PhylipNode getSourceNode() {
-   return sourceNode;
-  }
-  
-  public PhylipNode getTargetNode(){
-   return targetNode;
-  }
- }
+
+                                       }
+                               }         
+                       }
+
+                       // For every name/length encountered, splice off the 
')' and add it to the list
+                       else if(substrings[i].charAt(0) != '(' && 
substrings[i].charAt(0) != ')')
+                       {
+                               String[] tempSub2 = substrings[i].split("\\)");
+                               list.add(tempSub2[0]);
+                       }
+
+                       // For every ')' encountered add it to the list
+                       if(substrings[i].charAt(substrings[i].length()-1)== ')')
+                       {
+                               list.add(")");
+                       }
+               }
+
+
+               // Traverse the list into node and edge lists using a stack
+
+               iterator = list.iterator();
+               int parentNodeIndex = 0;
+               while(iterator.hasNext())
+               {
+                       String tempStr = iterator.next(); 
+
+                       if(!tempStr.equals(")"))
+                       {
+                               stack.push(tempStr);
+                               // Ignore
+                       }
+                       if(tempStr.equals(")"))
+                       {
+                               // Pop off items from the stack till '(' is 
encountered.
+                               // Appropriately populate node and edge lists 
with names/branch lenghts
+
+                               String stackTop = stack.pop();
+
+                               while(!stackTop.equals("("))
+                               {
+
+                                       try 
+                                       {
+                                               // If the item popped off is a 
branch length, store it in the branchLength variable
+                                               // so that it may be associated 
with the edge later
+                                               branchLength = 
Double.parseDouble(stackTop);
+                                       }
+                                       catch(NumberFormatException f)
+                                       {
+                                               // Failure to parseDouble() 
indicates that the item is a node name
+                                               // Add a PhylipNode
+
+                                               PhylipNode nodeA;
+                                               if(!parentNodeStack.isEmpty() 
&& stackTop.equals(parentNodeStack.peek().getName()))
+                                               {
+                                                       // This scenario 
indicates that the stackTop is a parentNode which has previous associated edges
+                                                       nodeA = 
parentNodeStack.pop();
+                                                       nodeList.add(nodeA);    
+
+                                               }
+                                               else{
+                                                       // This scenario 
indicates that stackTop is a previously unencountered node
+                                                       nodeA = new 
PhylipNode(stackTop);
+                                                       nodeList.add(nodeA);
+                                               }
+                                               // Also store the node in the 
childNodeList and its branch length in the branchLengthList
+                                               // for the purpose of adding 
edges from the parent node when it is created
+
+                                               childNodeList.add(nodeA);
+                                               
branchLengthList.add(branchLength);
+                                               branchLength = 0.0;
+                                       }
+
+                                       stackTop = stack.pop();
+                               }
+                               if(stackTop.equals("("))
+                               {
+                                       // '(' indicates that a set of child 
nodes has been parsed
+                                       // Add a parent node representing the 
common ancestor of all the child nodes
+
+                                       String parentNodeLabel = 
"parentNode"+parentNodeIndex;
+                                       PhylipNode parentNode = new 
PhylipNode(parentNodeLabel);
+                                       // Add a parent node even when there 
are only two nodes in the entire tree
+                                       if(stack.isEmpty())
+                                       {
+                                               nodeList.add(parentNode);
+                                       }
+
+                                       parentNodeIndex++;
+
+                                       // Add edges between the parent node 
and the children
+                                       // Initialize iterators
+                                       childNodeListIterator = 
childNodeList.iterator();
+                                       branchLengthListIterator = 
branchLengthList.iterator();
+
+                                       while(childNodeListIterator.hasNext())
+                                       {
+                                               PhylipNode childNode = 
childNodeListIterator.next();
+
+                                               // Create the edge
+                                               PhylipEdge edge = new 
PhylipEdge(parentNode,childNode);
+
+                                               // Associate the edge length 
with the edge
+                                               Double edgeLength = 
branchLengthListIterator.next();
+                                               
branchLengthListIterator.remove();
+                                               edge.setEdgeLength(edgeLength);
+
+                                               // Record the edge in each 
node's respective list of edges
+                                               
//System.out.println(parentNode.nodeEdges);
+                                               parentNode.nodeEdges.add(edge);
+                                               childNode.nodeEdges.add(edge);
+
+                                               //Used during development
+                                               
//System.out.println(edge.getSourceNode().getName()+"<-->"+edge.getTargetNode().getName()+":"+edgeLength);
+
+
+                                       }
+                                       childNodeList.clear();
+
+                                       // Add the label of the parent node 
back into the stack so it is connected to the rest of the tree
+                                       // Also add the parentNode into a 
separate stack so that its associated edges are preserved
+
+                                       stack.push(parentNodeLabel);
+                                       parentNodeStack.push(parentNode);
+
+                               }
+                       }
+
+               } 
+
+
+       }
+
+
+
+
+       // Interface methods
+       public List<PhylotreeNode> getNodeList(){
+               return nodeList;
+       }
+
+       // Get the edges for given node
+       public List<PhylotreeEdge> getEdges(PhylotreeNode pNode){
+
+               // Obtain a list of edges for the node
+               PhylipNode phylipNode = (PhylipNode)pNode;
+               List<PhylipEdge> edges = phylipNode.nodeEdges;
+
+               // Add each edge to the vector to be returned
+               Vector<PhylotreeEdge> retValue = new Vector<PhylotreeEdge>();
+               Iterator<PhylipEdge> iterator = edges.iterator();
+               while(iterator.hasNext())
+               {
+                       // A node could have multiple edges
+                       PhylipEdge pEdge = iterator.next();
+                       retValue.add(pEdge);
+               }
+               return retValue;
+       }
+
+/*     Return a list of all the edge attributes
+ *  The order of attributes is {edgeLength,}
+ */
+       public List<Object> getEdgeAttribute(PhylotreeEdge pEdge){
+
+               List<Object> edgeAttributes = new LinkedList<Object> ();
+
+               PhylipEdge phyEdge = (PhylipEdge)pEdge;
+
+               edgeAttributes.add(phyEdge.getEdgeLength());
+               return edgeAttributes;
+       }
+
+
+       // Inner classes
+       class PhylipNode implements PhylotreeNode {
+               private String nodeName = null;
+               public List<PhylipEdge> nodeEdges; // List of all edges 
connected to this node
+
+               public PhylipNode(String pNodeName){
+                       this.nodeName = pNodeName;
+                       this.nodeEdges = new LinkedList<PhylipEdge>();
+               }
+
+               public String getName(){
+                       return nodeName;
+               }
+       }
+
+
+       class PhylipEdge implements PhylotreeEdge {
+               private PhylipNode sourceNode = null;
+               private PhylipNode targetNode = null;
+               private double edgeLength = 0.0;
+
+               public PhylipEdge(PhylipNode pSourceNode, PhylipNode 
pTargetNode){
+                       this.sourceNode = pSourceNode;
+                       this.targetNode = pTargetNode;
+               }
+
+               public void setEdgeLength(double length){
+                       edgeLength = length; 
+               }
+
+               public double getEdgeLength() {
+                       return edgeLength;
+               }
+               public PhylipNode getSourceNode() {
+                       return sourceNode;
+               } 
+
+               public PhylipNode getTargetNode(){
+                       return targetNode;
+               }
+       }
 }

Modified: 
csplugins/trunk/soc/chinmoy/phylotree/src/org/cytoscape/phylotree/parser/Phylotree.java
===================================================================
--- 
csplugins/trunk/soc/chinmoy/phylotree/src/org/cytoscape/phylotree/parser/Phylotree.java
     2009-06-26 15:33:11 UTC (rev 17108)
+++ 
csplugins/trunk/soc/chinmoy/phylotree/src/org/cytoscape/phylotree/parser/Phylotree.java
     2009-06-26 17:32:04 UTC (rev 17109)
@@ -20,6 +20,6 @@
         *  @param layout DOCUMENT ME!
         *  @return  List of Edges (Node pairs)
         */     
-       public List getEdgeAttribute(PhylotreeEdge pEdge);
+       public List<Object> getEdgeAttribute(PhylotreeEdge pEdge);
        
 }


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to