Author: chinmoy
Date: 2009-06-12 00:02:41 -0700 (Fri, 12 Jun 2009)
New Revision: 16943

Added:
   csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/Parser.class
   csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/Parser.java
   
csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/ParserPlugin$MyPluginAction.class
   csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/ParserPlugin.class
   csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/ParserPlugin.java
Log:
Working Parser object as well as Parser plugin

Added: csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/Parser.class
===================================================================
(Binary files differ)


Property changes on: 
csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/Parser.class
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/Parser.java
===================================================================
--- csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/Parser.java       
                        (rev 0)
+++ csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/Parser.java       
2009-06-12 07:02:41 UTC (rev 16943)
@@ -0,0 +1,165 @@
+package parser;
+import java.util.*;
+
+import cytoscape.CyEdge;
+import cytoscape.CyNetwork;
+import cytoscape.CyNode;
+import cytoscape.Cytoscape;
+
+public class Parser {
+
+
+       String treeString;
+       LinkedList<CyNode> allNodes;
+       LinkedList<CyEdge> allEdges;
+
+       public Parser(String tree)
+       {
+               treeString = tree;
+               allNodes = new LinkedList<CyNode>();
+               allEdges = new LinkedList<CyEdge>();
+
+       }
+
+       public void parse()
+       {
+               Stack <String> stack = new Stack<String>();     
+               LinkedList<String> list = new LinkedList<String>();
+               LinkedList<String> edgeList = new LinkedList<String>();
+
+               Iterator<String> iterator;
+               Iterator<String> edgeListIterator;
+
+               // Split the input string into a list
+               String [] substrings = treeString.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++)
+               {
+
+                       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("\\(+");
+
+                                               list.add(tempSub[1]);
+                                               break;
+
+
+                                       }
+                               }         
+                       }
+
+                       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(")");
+                       }
+
+
+               }
+
+
+               // 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();
+
+                               while(!stackTop.equals("("))
+                               {
+
+                                       try
+                                       {
+                                               Double branchLength = 
Double.parseDouble(stackTop);
+                                               // @DEVELOP_ME
+                                               // Find a way to store the 
branch length with the node
+                                               // so that the layout is 
actually representative of the 
+                                               // edge distances
+                                       }
+                                       catch(NumberFormatException f)
+                                       {
+                                               // Add a node
+
+                                               CyNode nodeA = 
Cytoscape.getCyNode(stackTop, true);
+                                               allNodes.add(nodeA);
+
+                                               // Store each node label into a 
list
+
+                                               edgeList.add(stackTop);
+                                       }
+
+                                       stackTop = stack.pop();
+                               }
+                               if(stackTop.equals("("))
+                               {
+                                       // Add a temporary parent node
+                                       String tempNodeLabel = 
"tempNode"+tempNodeIndex;
+                                       CyNode tempNode = 
Cytoscape.getCyNode(tempNodeLabel, true);
+                                       allNodes.add(tempNode);
+                                       tempNodeIndex++;
+
+                                       // Add edges between the temporary 
parent and the children
+                                       edgeListIterator = edgeList.iterator();
+                                       int tempEdgeIndex = 0;
+                                       while(edgeListIterator.hasNext())
+                                       {
+                                               Object tempEdgeListObj = 
edgeListIterator.next();
+                                               String tempEdgeListStr = 
(String) tempEdgeListObj;
+                                               String tempEdgeLabel = 
"edge"+tempEdgeIndex;
+                                               CyEdge edgeA = 
Cytoscape.getCyEdge(tempNodeLabel, tempEdgeLabel, tempEdgeListStr, "pp");
+                                               tempEdgeIndex++;
+                                               allEdges.add(edgeA);
+
+
+                                       }
+                                       edgeList.clear();
+
+                                       // Add the temporary Parent node back 
into the stack
+                                       stack.push(tempNodeLabel);
+                                       
+
+                               }
+                       }
+
+               }       
+
+       }
+
+       public LinkedList<CyNode> getNodeList(){
+               return allNodes;
+
+       }
+
+       public LinkedList<CyEdge> getEdgeList(){
+               return allEdges;
+       }
+}

Copied: 
csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/ParserPlugin$MyPluginAction.class
 (from rev 16875, 
csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/ParserPlugin$MyPluginAction.class)
===================================================================
(Binary files differ)

Copied: 
csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/ParserPlugin.class 
(from rev 16875, 
csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/ParserPlugin.class)
===================================================================
(Binary files differ)

Copied: 
csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/ParserPlugin.java 
(from rev 16875, 
csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/ParserPlugin.java)
===================================================================
--- csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/ParserPlugin.java 
                        (rev 0)
+++ csplugins/trunk/soc/chinmoy/phylotree/ParserPlugin/parser/ParserPlugin.java 
2009-06-12 07:02:41 UTC (rev 16943)
@@ -0,0 +1,97 @@
+package parser;
+
+
+import java.awt.event.ActionEvent;
+import java.util.*;
+import java.io.*;
+import javax.swing.*;
+import cytoscape.CyEdge;
+import cytoscape.CyNetwork;
+import cytoscape.CyNode;
+import cytoscape.Cytoscape;
+import cytoscape.plugin.CytoscapePlugin;
+import cytoscape.util.CytoscapeAction;
+
+/**
+ * Plugin to parse a phylogenetic tree into a network
+ * 
+ */
+
+public class ParserPlugin extends CytoscapePlugin {
+
+       /**
+        * Parser a string representation of a PHYLIP phylogenetic tree
+        * into a Cytoscape network.
+        * Edit the String treeString to try different phylogenetic trees
+        * 
+        */
+       public ParserPlugin() {
+               // Create an Action, add the action to Cytoscape menu
+               MyPluginAction action = new MyPluginAction(this);
+               
Cytoscape.getDesktop().getCyMenus().addCytoscapeAction((CytoscapeAction) 
action);
+       }
+       
+       public class MyPluginAction extends CytoscapeAction {
+
+               public MyPluginAction(ParserPlugin myPlugin) {
+                       // Add the menu item under menu pulldown "Plugins"
+                       super("ParserPlugin");
+                       setPreferredMenu("Plugins");
+               }
+
+               public void actionPerformed(ActionEvent e) {
+                       
+                         String treeString; // The variable that will contain 
the string read from the file
+                       
+                       JFileChooser chooser = new JFileChooser();
+               int option = chooser.showOpenDialog(null);
+               if (option == JFileChooser.APPROVE_OPTION) {
+                 File file = chooser.getSelectedFile();
+                 
+                 
+              try{
+                
+                       BufferedReader in = new BufferedReader(new 
FileReader(file));
+                       treeString = in.readLine();
+                       parser.Parser p = new Parser(treeString); 
+                               p.parse();
+                               LinkedList<CyNode> nodes = p.getNodeList();
+                               LinkedList<CyEdge> edges = p.getEdgeList();
+                               
+       
+                           CyNetwork cyNetwork = 
Cytoscape.createNetwork("network", false);
+                               Iterator<CyNode> iterator1 = nodes.iterator(); 
+                               while(iterator1.hasNext())
+                               {
+                                       cyNetwork.addNode(iterator1.next());
+                                       
+                               }
+                                                       
+                               Iterator<CyEdge> iterator2 = edges.iterator(); 
+                               while(iterator2.hasNext())
+                               {
+                                       cyNetwork.addEdge(iterator2.next());
+                                       
+                               }       
+                        
+              }
+              
+              
+              catch(IOException l)
+              {
+               JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "Error 
reading file: " + file.getName()); 
+              }
+                 
+              catch(NullPointerException l)
+              {
+               JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "Error 
reading file: " + file.getName()); 
+              }
+                 
+               }
+                       
+               
+                       
+                 
+               }
+       }
+}


--~--~---------~--~----~------------~-------~--~----~
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