Author: scooter
Date: 2012-10-10 15:33:48 -0700 (Wed, 10 Oct 2012)
New Revision: 30649

Added:
   csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/commands/ValueUtils.java
Modified:
   
csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/commands/ChemVizCommandHandler.java
   
csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/menus/ChemVizContextMenu.java
   csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/menus/DepictionMenus.java
   csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/model/Compound.java
   
csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/tasks/CreateCompoundTableTask.java
   csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/tasks/CreatePopupTask.java
   csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/ui/CompoundTable.java
Log:
Have many commands implemented -- moving forward!


Modified: 
csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/commands/ChemVizCommandHandler.java
===================================================================
--- 
csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/commands/ChemVizCommandHandler.java
        2012-10-10 21:14:28 UTC (rev 30648)
+++ 
csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/commands/ChemVizCommandHandler.java
        2012-10-10 22:33:48 UTC (rev 30649)
@@ -35,13 +35,17 @@
 import java.lang.RuntimeException;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 
 
 // Cytoscape imports
+import cytoscape.CyEdge;
 import cytoscape.CyNetwork;
 import cytoscape.CyNode;
 import cytoscape.Cytoscape;
@@ -60,7 +64,10 @@
 // chemViz imports
 import chemViz.model.ChemInfoProperties;
 import chemViz.model.Compound;
+import chemViz.model.Compound.AttriType;
 import chemViz.model.Compound.DescriptorType;
+import chemViz.tasks.CreatePopupTask;
+import chemViz.tasks.CreateCompoundTableTask;
 import chemViz.ui.ChemInfoSettingsDialog;
 
 enum Command {
@@ -72,22 +79,25 @@
                  "nodelist"),
        CLOSESTRUCTURES("close structures",
                        "Close the 2D structure grid",
-                       ""),
+                       "dialog"),
        CLOSETABLE("close table",
                   "Close the structure table",
-                  ""),
+                  "dialog"),
        GETDESC("get descriptors",
                "Return chemical descriptors for nodes or edges",
                
"attribute|descriptors|edge|edgelist|network=current|node|nodelist|smiles"),
+       LISTDESC("list descriptors",
+                "Return the list of available chemical descriptors",
+                ""),
        REMOVE("remove",
               "Remove 2D structures from nodes",
                                 "nodelist|node"),
        SHOWSTRUCTURES("show structures",
                       "Popup the 2D structures for a node/edge or group of 
nodes/edges",
-                      "node|nodelist|edge|edgelist"),
+                      "node|nodelist|edge|edgelist|labelattribute"),
        SHOWTABLE("show table",
                  "Show the structure table for a node/edge or group of 
nodes/edges",
-                 "edge|edgelist|node|nodelist|descriptors|attributes"),
+                 "edge|edgelist|node|nodelist|columnlist"),
        SETPARAM("set parameter",
                 "Set chemViz parameters",
                 
"fingerprinter=CDK|smilesAttributes|inchiAttributes|nodeSize=100|position=Centered|imageLabel");
@@ -113,21 +123,40 @@
  * Inner class to handle CyCommands
  */
 public class ChemVizCommandHandler extends AbstractCommandHandler {
-       ChemInfoProperties props;
+       static final String ALL = "all";
        static final String ATTRIBUTE = "attribute";
+       static final String COLUMNLIST = "columnlist";
        static final String CURRENT = "current";
+       static final String DIALOG = "dialog";
        static final String DESCRIPTORS = "descriptors";
        static final String EDGE = "edge";
        static final String EDGELIST = "edgelist";
+       static final String INCHI = "inchi";
+       static final String LABELATTRIBUTE = "labelattribute";
        static final String NETWORK = "network";
        static final String NODE = "node";
        static final String NODELIST = "nodelist";
+       static final String SELECTED = "selected";
        static final String SMILES = "smiles";
 
+       private ChemInfoProperties props;
+       private ChemInfoSettingsDialog dialog;
+       
+       // Dialogs that we may want to dispose of...
+       static Map<Integer, CreatePopupTask> popupTasks = null;
+       static int popupTaskCount = 0;
+       static Map<Integer, CreateCompoundTableTask> tableTasks = null; 
+       static int tableTaskCount = 0;
+
        public ChemVizCommandHandler (ChemInfoSettingsDialog settingsDialog) {
                super(CyCommandManager.reserveNamespace("chemViz"));
 
                props = settingsDialog.getProperties();
+               dialog = settingsDialog;
+
+               for (Command command: Command.values()) {
+                       addCommand(command.getCommand(), 
command.getDescription(), command.getArgString());
+               }
        }
 
        public CyCommandResult execute(String command, Collection<Tunable>args)
@@ -146,21 +175,36 @@
                }
 
                // Pull out common args
-               List<GraphObject> gObjList = getGraphObjectList(command, args);
+               List<GraphObject> gObjList = 
ValueUtils.getGraphObjectList(command, args);
+               String objectType = "node";     
+               if (gObjList != null && (gObjList.get(0) instanceof CyEdge))
+                       objectType = "edge";    
 
-               String smiles = null;
-               if (args.containsKey(SMILES))
-                       smiles = args.get(SMILES).toString();
+               String mstring = null;
+               AttriType mtype = AttriType.smiles;
+               if (args.containsKey(SMILES)) {
+                       mstring = args.get(SMILES).toString();
+               } else if (args.containsKey(INCHI)) {
+                       mstring = args.get(INCHI).toString();
+                       mtype = AttriType.inchi;
+               }
 
-               String attribute = null;
-               if (args.containsKey(ATTRIBUTE))
-                       attribute = args.get(ATTRIBUTE).toString();
+               List<String> smilesAttrList = null;
+               List<String> inchiAttrList = null;
 
+               if (args.containsKey(ATTRIBUTE)) {
+                       smilesAttrList.add(args.get(ATTRIBUTE).toString());
+                       inchiAttrList.add(args.get(ATTRIBUTE).toString());
+               } else {
+                       smilesAttrList = 
dialog.getCompoundAttributes(objectType,AttriType.smiles);
+                       inchiAttrList = 
dialog.getCompoundAttributes(objectType,AttriType.inchi);
+               }
+
                // Main command cascade
 
                //      ATTACH("attach",
                //             "Attach 2D structures to nodes",
-               //                               
"nodelist|node|attribute|smiles"),
+               //                               
"nodelist|node|attribute|inchi|smiles"),
                if (Command.ATTACH.equals(command)) {
 
                //      CALCULATE("calculate similarity",
@@ -169,29 +213,71 @@
                } else if (Command.CALCULATE.equals(command)) {
 
                //      CLOSESTRUCTURES("close structures",
-               //                      "Close the 2D structure grid",
-               //                      ""),
+               //                      "Close the 2D structure grid(s)",
+               //                      "dialog"),
                } else if (Command.CLOSESTRUCTURES.equals(command)) {
+                       if (popupTasks != null && popupTasks.size() > 0) {
+                               Set<Integer> dialogNumbers = 
getDialogNumbers(popupTasks.keySet(), args);
 
+                               for (Integer index: dialogNumbers) {
+                                       if (popupTasks.containsKey(index)) {
+                                               CreatePopupTask popup = 
popupTasks.get(index);
+                                               popup.closePopup();
+                                               popupTasks.remove(index);
+                                       }
+                               }
+                       }
+
                //      CLOSETABLE("close table",
                //                 "Close the structure table",
                //                 ""),
                } else if (Command.CLOSETABLE.equals(command)) {
+                       if (tableTasks != null && tableTasks.size() > 0) {
+                               Set<Integer> dialogNumbers = 
getDialogNumbers(tableTasks.keySet(), args);
 
+                               for (Integer index: dialogNumbers) {
+                                       if (tableTasks.containsKey(index)) {
+                                               CreateCompoundTableTask table = 
tableTasks.get(index);
+                                               table.closePopup();
+                                               tableTasks.remove(index);
+                                       }
+                               }
+                       }
+
+               //      LISTDESC("list descriptors",
+               //               "Return the list of available chemical 
descriptors",
+               //               ""),
+               } else if (Command.LISTDESC.equals(command)) {
+                       List<DescriptorType> descriptors = 
ValueUtils.getDescriptors(command, "all");
+                       List<String> descStrings = new ArrayList<String>();
+                       result.addMessage("Available descriptors: ");
+                       for (DescriptorType type: descriptors) {
+                               String shortName = type.getShortName();
+                               if (shortName.equals("image")) continue;
+
+                               String name = type.toString();
+                               result.addMessage(shortName+": "+name);
+                               descStrings.add(shortName);
+                       }
+                       result.addResult("descriptors", descStrings);
+
                //      GETDESC("get descriptors",
                //              "Return chemical descriptors for a node",
-               //              "descriptors|node|nodelist|smiles"),
+               //              
"edge|edgelist|descriptors|inchi|node|nodelist|inchi|smiles"),
                } else if (Command.GETDESC.equals(command)) {
-                       if (gObjList != null && smiles != null) 
-                               throw new RuntimeException("chemviz 
"+command+": can't have both smiles string and nodes");
+                       if (gObjList != null && mstring != null) 
+                               throw new RuntimeException("chemviz 
"+command+": can't have both smiles/inchi string and nodes/edges");
+                       if (gObjList == null && mstring == null) 
+                               throw new RuntimeException("chemviz 
"+command+": must have one of smiles/inchi string or nodes/edges");
 
                        if (!args.containsKey(DESCRIPTORS))
                                throw new RuntimeException("chemviz 
"+command+": descriptor list must be specified");
-                       List<DescriptorType> descriptors = 
getDescriptors(command, args.get(DESCRIPTORS).toString());
+                       List<DescriptorType> descriptors = 
ValueUtils.getDescriptors(command, args.get(DESCRIPTORS).toString());
 
-                       List<Compound> compoundList = getCompounds(gObjList, 
smiles, attribute);
+                       List<Compound> compoundList = 
ValueUtils.getCompounds(gObjList, mstring, mtype, smilesAttrList, 
inchiAttrList);
                        for (Compound compound: compoundList) {
                                for (DescriptorType type: descriptors) {
+                                       if 
(type.getShortName().equals("image")) continue;
                                        
result.addResult(compound.toString()+":"+type.getShortName(), 
compound.getDescriptor(type));
                                        result.addMessage("Compound 
"+compound.toString()+" "+type.toString()+" = 
"+compound.getDescriptor(type).toString());
                                }
@@ -204,18 +290,61 @@
                
                //      SHOWSTRUCTURES("show structures",
                //                     "Popup the 2D structures for a node or 
group of nodes",
-               //                     "node|nodelist"),
+               //                     "node|nodelist|edge|edgelist"),
                } else if (Command.SHOWSTRUCTURES.equals(command)) {
-               
+                       if (gObjList == null)
+                               throw new RuntimeException("chemviz 
"+command+": node/edge or nodelist/edgelist required");
+
+                       String labelAttribute = null;
+                       if (args.containsKey(LABELATTRIBUTE))
+                               labelAttribute = 
args.get(LABELATTRIBUTE).toString();
+
+               CreatePopupTask loader = new CreatePopupTask(gObjList, dialog, 
labelAttribute, dialog.getMaxCompounds());
+                       TaskManager.executeTask(loader, 
loader.getDefaultTaskConfig());
+
+                       if (popupTasks == null) popupTasks = new 
HashMap<Integer, CreatePopupTask>();
+                       result.addMessage("Showing structures: dialog 
#"+popupTaskCount);
+                       popupTasks.put(popupTaskCount++, loader);
+                       
                //      SHOWTABLE("show table",
-               //                "Show the structure table for a node or group 
of nodes",
-               //                "node|nodelist|descriptors|attributes"),
+               //                "Show the structure table for a node/edge or 
group of nodes/edges",
+               //                "edge|edgelist|node|nodelist|columnlist"),
                } else if (Command.SHOWTABLE.equals(command)) {
+                       if (gObjList == null)
+                               throw new RuntimeException("chemviz 
"+command+": node/edge or nodelist/edgelist required");
+
+                       List<String> columnList = null;
+                       if (args.containsKey(COLUMNLIST)) {
+                               String[] columnSpecs = 
((String)args.get(COLUMNLIST)).split(",");
+                               columnList = Arrays.asList(columnSpecs);
+                       }
+                       CreateCompoundTableTask loader = new 
CreateCompoundTableTask(gObjList, dialog, dialog.getMaxCompounds(), columnList);
+                       TaskManager.executeTask(loader, 
loader.getDefaultTaskConfig());
+
+                       if (tableTasks == null) tableTasks = new 
HashMap<Integer, CreateCompoundTableTask>();
+                       result.addMessage("Showing structure table: dialog 
#"+tableTaskCount);
+                       tableTasks.put(tableTaskCount++, loader);
                }
                
                return result;
        }
 
+       private Set<Integer> getDialogNumbers(Set<Integer> dialogNumbers, 
Map<String, Object> args) {
+               if (args.containsKey(DIALOG)) {
+                       Set<Integer> dn = new HashSet<Integer>();
+                       
+                       String dNumber = ((String)args.get(DIALOG)).trim();
+                       if (!dNumber.equals(ALL)) {
+                               String[] dList = dNumber.split(",");
+                               for (String d: dList) {
+                                       dn.add(new Integer(d.trim()));
+                               }
+                               return dn;
+                       }
+               }
+               return dialogNumbers;
+       }
+
        private void addCommand(String command, String description, String 
argString) {
                // Add the description first
                addDescription(command, description);
@@ -236,87 +365,6 @@
                }
        }
 
-       private List<GraphObject> getGraphObjectList(String command, 
Map<String,Object> args) {
-               if (!args.containsKey(NODE) && !args.containsKey(NODELIST) &&
-                   !args.containsKey(EDGE) && !args.containsKey(EDGE))
-                       return null;
-
-               if (args.containsKey(NODE) && args.containsKey(NODELIST))
-                       throw new RuntimeException("chemviz "+command+": can't 
have both 'node' and 'nodeList'");
-
-               if (args.containsKey(EDGE) && args.containsKey(EDGELIST))
-                       throw new RuntimeException("chemviz "+command+": can't 
have both 'edge' and 'edgeList'");
-
-               CyNetwork network = Cytoscape.getCurrentNetwork();
-
-               if (args.containsKey(NETWORK)) {
-                       String netName = args.get(NETWORK).toString();
-                       if (!netName.equals(CURRENT) && 
Cytoscape.getNetwork(netName) != null)
-                               network = Cytoscape.getNetwork(netName);
-               }
-
-               List<GraphObject> objList = new ArrayList<GraphObject>();
-               if (args.containsKey(NODE)) {
-                       objList.add(getNode(command, 
args.get(NODE).toString()));
-               } else if (args.containsKey(NODELIST)) {
-                       String nodes = args.get(NODELIST).toString();
-                       if (nodes == null || nodes.length() == 0) return null;
-                       String[] nodeArray  = nodes.split(",");
-                       for (String str: nodeArray)
-                               objList.add(getNode(command, str.trim()));
-               } else if (args.containsKey(EDGE)) {
-                       objList.add(getEdge(command, 
args.get(EDGE).toString()));
-               } else if (args.containsKey(EDGELIST)) {
-                       String edges = args.get(EDGELIST).toString();
-                       if (edges == null || edges.length() == 0) return null;
-                       String[] edgeArray  = edges.split(",");
-                       for (String str: edgeArray)
-                               objList.add(getEdge(command, str.trim()));
-               }
-               return objList;
-       }
-
-       private GraphObject getNode(String command, String nodeID) {
-               if (Cytoscape.getCyNode(nodeID, false) != null)
-                       return (GraphObject)Cytoscape.getCyNode(nodeID, false);
-
-               if (Cytoscape.getCyNode(nodeID) != null)
-                       return (GraphObject)Cytoscape.getCyNode(nodeID);
-
-               throw new RuntimeException("chemviz "+command+": can't find 
node '"+nodeID+"'");
-       }
-
-       private GraphObject getEdge(String command, String edgeID) {
-               throw new RuntimeException("chemviz "+command+": edge support 
isn't implemented yet");
-       }
-
-       private List<DescriptorType> getDescriptors(String command, String 
desc) {
-               if (desc == null || desc.length() == 0) 
-                       throw new RuntimeException("chemviz "+command+": 
descriptors list cannot be empty");
-
-               List<DescriptorType> fullList = Compound.getDescriptorList();
-               List<DescriptorType> resultList = new 
ArrayList<DescriptorType>();
-
-               String[] descArray = desc.split(",");
-               for (String descriptor: descArray) {
-                       if (getDescriptor(fullList,descriptor.trim()) == null)
-                               throw new RuntimeException("chemviz 
"+command+": descriptor '"+descriptor+"' isn't supported");
-                       
resultList.add(getDescriptor(fullList,descriptor.trim()));
-               }
-               return resultList;
-       }
-
-       private DescriptorType getDescriptor(List<DescriptorType> fullList, 
String desc) {
-               for (DescriptorType type: fullList)
-                       if (type.getShortName().equals(desc))
-                               return type;
-               return null;
-       }
-
-       private List<Compound> getCompounds(List<GraphObject> objList, String 
smiles, String attribute) {
-               return null;
-       }
-
        private void addArguments(String command) {
                if (props == null) {
                        addArgument(command);
@@ -334,48 +382,4 @@
                                addArgument(command, t.getName());
                }
        }
-
-       private void setTunables(ChemInfoProperties props, 
Collection<Tunable>args) throws Exception {
-               // Set the Tunables
-               for (Tunable t: args) {
-                       if (props.get(t.getName()) != null) {
-                               Tunable target = props.get(t.getName());
-                               Object value = t.getValue();
-                               try {
-                                       if ((target.getType() == Tunable.LIST) 
&&
-                                           (t.getType() == Tunable.STRING)) {
-                                               setListTunable(target, 
value.toString());
-                                       } else {
-                                               
target.setValue(value.toString());
-                                       }
-                                       target.updateValueListeners();
-                               } catch (Exception e) {
-                                       throw new Exception("Unable to parse 
value for "+
-                                                           t.getName()+": 
"+value.toString());
-                               }
-                       }
-               }
-       }
-
-       private void setListTunable(Tunable listTunable, String value) {
-               Object[] optionList = (Object [])listTunable.getLowerBound();
-               String[] inputList = value.split(",");
-               String v = "";
-               Integer first = null;
-               for (int i = 0; i < inputList.length; i++) {
-                       for (int j = 0; j < optionList.length; j++) {
-                               if 
(optionList[j].toString().equals(inputList[i])) {
-                                       v = v+","+j;
-                                       if (first == null) first = new 
Integer(j);
-                               }
-                       }
-               }
-               v = v.substring(1);
-               if (listTunable.checkFlag(Tunable.MULTISELECT)) {
-                       listTunable.setValue(v);
-               } else {
-                       listTunable.setValue(first);
-               }
-       }
-
 }

Added: csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/commands/ValueUtils.java
===================================================================
--- csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/commands/ValueUtils.java   
                        (rev 0)
+++ csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/commands/ValueUtils.java   
2012-10-10 22:33:48 UTC (rev 30649)
@@ -0,0 +1,354 @@
+/* vim: set ts=2: */
+/**
+ * Copyright (c) 2010 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions, and the following disclaimer.
+ *   2. Redistributions in binary form must reproduce the above
+ *      copyright notice, this list of conditions, and the following
+ *      disclaimer in the documentation and/or other materials provided
+ *      with the distribution.
+ *   3. Redistributions must acknowledge that this software was
+ *      originally developed by the UCSF Computer Graphics Laboratory
+ *      under support by the NIH National Center for Research Resources,
+ *      grant P41-RR01081.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
+ * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+package chemViz.commands;
+
+import java.lang.RuntimeException;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.HashMap;
+import java.util.Map;
+
+
+// Cytoscape imports
+import cytoscape.CyEdge;
+import cytoscape.CyNetwork;
+import cytoscape.CyNode;
+import cytoscape.Cytoscape;
+import cytoscape.command.AbstractCommandHandler;
+import cytoscape.command.CyCommandException;
+import cytoscape.command.CyCommandHandler;
+import cytoscape.command.CyCommandNamespace;
+import cytoscape.command.CyCommandManager;
+import cytoscape.command.CyCommandResult;
+import cytoscape.data.CyAttributes;
+import cytoscape.data.Semantics;
+import cytoscape.layout.Tunable;
+import cytoscape.task.util.TaskManager;
+
+import giny.model.GraphObject;
+
+// chemViz imports
+import chemViz.model.ChemInfoProperties;
+import chemViz.model.Compound;
+import chemViz.model.Compound.AttriType;
+import chemViz.model.Compound.DescriptorType;
+
+/**
+ * Inner class to handle CyCommands
+ */
+public class ValueUtils {
+       static final String ALL = "all";
+       static final String ATTRIBUTE = "attribute";
+       static final String CURRENT = "current";
+       static final String DESCRIPTORS = "descriptors";
+       static final String EDGE = "edge";
+       static final String EDGELIST = "edgelist";
+       static final String NETWORK = "network";
+       static final String NODE = "node";
+       static final String NODELIST = "nodelist";
+       static final String SELECTED = "selected";
+       static final String SMILES = "smiles";
+
+       static public List<GraphObject> getGraphObjectList(String command, 
Map<String,Object> args) {
+               if (!args.containsKey(NODE) && !args.containsKey(NODELIST) &&
+                   !args.containsKey(EDGE) && !args.containsKey(EDGE))
+                       return null;
+
+               if (args.containsKey(NODE) && args.containsKey(NODELIST))
+                       throw new RuntimeException("chemviz "+command+": can't 
have both 'node' and 'nodeList'");
+
+               if (args.containsKey(EDGE) && args.containsKey(EDGELIST))
+                       throw new RuntimeException("chemviz "+command+": can't 
have both 'edge' and 'edgeList'");
+
+               // Get the network
+               CyNetwork network = Cytoscape.getCurrentNetwork();
+               if (args.containsKey(NETWORK)) {
+                       String netName = args.get(NETWORK).toString();
+                       if (!netName.equals(CURRENT) && 
Cytoscape.getNetwork(netName) != null)
+                               network = Cytoscape.getNetwork(netName);
+               }
+
+               // OK, nodes or edges?
+               if (args.containsKey(NODE) || args.containsKey(NODELIST))
+                       return getNodeList(command, args, network);
+               else 
+                       return getEdgeList(command, args, network);
+       }
+
+       static public List<GraphObject> getNodeList(String command, 
Map<String,Object> args, CyNetwork network) {
+               List<GraphObject> objList = new ArrayList<GraphObject>();
+               if (args.containsKey(NODE)) {
+                       objList.add(getNode(command, 
args.get(NODE).toString()));
+               } else if (args.containsKey(NODELIST)) {
+                       String nodes = args.get(NODELIST).toString();
+                       if (nodes == null || nodes.length() == 0) return null;
+                       // Special case for "selected" nodes
+                       if (nodes.equals(SELECTED)) {
+                               objList.addAll(network.getSelectedNodes());
+                       } else {
+                               String[] nodeArray  = nodes.split(",");
+                               for (String str: nodeArray)
+                                       objList.add(getNode(command, 
str.trim()));
+                       }
+               }
+               return objList;
+       }
+
+       static public List<GraphObject> getEdgeList(String command, 
Map<String,Object> args, CyNetwork network) {
+               List<GraphObject> objList = new ArrayList<GraphObject>();
+               if (args.containsKey(EDGE)) {
+                       objList.add(getEdge(command, 
args.get(EDGE).toString()));
+               } else if (args.containsKey(EDGELIST)) {
+                       String edges = args.get(EDGELIST).toString();
+                       if (edges == null || edges.length() == 0) return null;
+                       // Special case for "selected" nodes
+                       if (edges.equals(SELECTED)) {
+                               objList.addAll(network.getSelectedEdges());
+                       } else {
+                               String[] edgeArray  = edges.split(",");
+                               for (String str: edgeArray)
+                                       objList.add(getEdge(command, 
str.trim()));
+                       }
+               }
+               return objList;
+       }
+
+       static public GraphObject getNode(String command, String nodeID) {
+               if (Cytoscape.getCyNode(nodeID, false) != null)
+                       return (GraphObject)Cytoscape.getCyNode(nodeID, false);
+
+               if (Cytoscape.getCyNode(nodeID) != null)
+                       return (GraphObject)Cytoscape.getCyNode(nodeID);
+
+               throw new RuntimeException("chemviz "+command+": can't find 
node '"+nodeID+"'");
+       }
+
+
+       static public GraphObject getEdge(String command, String edgeID) {
+               CyEdge edge = null;
+               String comp[] = edgeID.split("[()]");
+               CyNode source = Cytoscape.getCyNode(comp[0].trim(), false);
+               CyNode target = Cytoscape.getCyNode(comp[2].trim(), false);
+               if (source != null && target != null) {
+                       edge = Cytoscape.getCyEdge(source, target, 
Semantics.INTERACTION, comp[1].trim(), false);
+               }
+               if (edge == null)
+                       throw new RuntimeException("chemviz "+command+": can't 
find edge '"+edgeID+"'");
+               return edge;
+       }
+
+       static public List<DescriptorType> getDescriptors(String command, 
String desc) {
+               if (desc == null || desc.length() == 0) 
+                       throw new RuntimeException("chemviz "+command+": 
descriptors list cannot be empty");
+
+               List<DescriptorType> fullList = Compound.getDescriptorList();
+               List<DescriptorType> resultList = new 
ArrayList<DescriptorType>();
+
+               if (desc.trim().equals(ALL)) {
+                       return fullList;
+               }
+
+               String[] descArray = desc.split(",");
+               for (String descriptor: descArray) {
+                       if (getDescriptor(fullList,descriptor.trim()) == null)
+                               throw new RuntimeException("chemviz 
"+command+": descriptor '"+descriptor+"' isn't supported");
+                       
resultList.add(getDescriptor(fullList,descriptor.trim()));
+               }
+               return resultList;
+       }
+
+       static public DescriptorType getDescriptor(List<DescriptorType> 
fullList, String desc) {
+               for (DescriptorType type: fullList)
+                       if (type.getShortName().equals(desc))
+                               return type;
+               return null;
+       }
+
+       static public List<Compound> getCompounds(List<GraphObject> objList, 
String mstring, AttriType type, 
+                                                 List<String> sList, 
List<String> iList) {
+               List<Compound> compoundList = new ArrayList<Compound>();
+
+               // Handle special case of a bare smiles string
+               if (mstring != null) {
+                       Compound c = new Compound(null, null, mstring, type, 
false);
+                       compoundList.add(c);
+                       return compoundList;
+               }
+
+               for (GraphObject obj: objList) {
+                       if (obj instanceof CyNode)
+                               compoundList.addAll(getCompounds(obj, 
Cytoscape.getNodeAttributes(), sList, iList, false));
+                       else
+                               compoundList.addAll(getCompounds(obj, 
Cytoscape.getEdgeAttributes(), sList, iList, false));
+               }
+
+               return compoundList;
+       }
+
+       /**
+        * Returns all of the Compounds for a single graph object (Node or 
Edge) based on the SMILES
+        * and InChI attributes.
+        *
+        * @param go the graph object we're looking at
+        * @param attributes the appropriate set of attributes (nodeAttributes 
or edgeAttributes)
+        * @param sList the list of attributes that contain SMILES strings
+        * @param iList the list of attributes that contain InChI strings
+        * @param noStructures if 'true', the structures are fetched in the 
background
+        * @return the list of compounds.  If the compounds have not already 
been created, they are created
+        *         as a byproduct of this method.
+        */
+       public static List<Compound> getCompounds(GraphObject go, CyAttributes 
attributes, 
+                                                 List<String> sList, 
List<String> iList, 
+                                                 boolean noStructures) {
+               if ((sList == null || sList.size() == 0) 
+                   && (iList == null || iList.size() == 0))
+                       return null;
+               
+               List<Compound> cList = new ArrayList();
+
+               // Get the compound list from each attribute
+               for (String attr: sList) {
+                       cList.addAll(getCompounds(go, attributes, attr, 
AttriType.smiles, noStructures));
+               }
+
+               for (String attr: iList) {
+                       cList.addAll(getCompounds(go, attributes, attr, 
AttriType.inchi, noStructures));
+               }
+
+               return cList;
+       }
+
+       /**
+        * Returns all of the Compounds for a single graph object (Node or 
Edge) based on the designated
+        * attribute of the specific type
+        *
+        * @param go the graph object we're looking at
+        * @param attributes the appropriate set of attributes (nodeAttributes 
or edgeAttributes)
+        * @param attr the attribute that contains the compound descriptor
+        * @param type the type of the attribute (smiles or inchi)
+        * @param noStructures if 'true', the structures are fetched in the 
background
+        * @return the list of compounds.  If the compounds have not already 
been created, they are created
+        *         as a byproduct of this method.
+        */
+       public static List<Compound> getCompounds(GraphObject go, CyAttributes 
attributes, 
+                                                 String attr, AttriType type,
+                                                 boolean noStructures) {
+               byte atype = attributes.getType(attr);
+               List<Compound> cList = new ArrayList();
+                       
+               if (!attributes.hasAttribute(go.getIdentifier(), attr)) 
+                       return cList;
+               if (atype == CyAttributes.TYPE_STRING) {
+                       String cstring = 
attributes.getStringAttribute(go.getIdentifier(), attr);
+                       cList.addAll(getCompounds(go, attr, cstring, type, 
noStructures));
+               } else if (atype == CyAttributes.TYPE_SIMPLE_LIST) {
+                       List<String> stringList = 
attributes.getListAttribute(go.getIdentifier(), attr);
+                       for (String cstring: stringList) {
+                               cList.addAll(getCompounds(go, attr, cstring, 
type, noStructures));
+                       }
+               }
+               return cList;
+       }
+
+       public static List<Compound> getCompounds(GraphObject go, String attr, 
+                                                 String compoundString, 
AttriType type,
+                                                 boolean noStructures) {
+               List<Compound> cList = new ArrayList();
+
+               String[] cstrings = null;
+
+               if (type == AttriType.smiles) {
+                       cstrings = compoundString.split(",");
+               } else {
+                       cstrings = new String[1];
+                       cstrings[0] = compoundString;
+               }
+
+               for (int i = 0; i < cstrings.length; i++) {
+
+                       Compound c = Compound.getCompound(go, attr, 
cstrings[i], type);
+                       if (c == null)
+                               c = new Compound(go, attr, cstrings[i], type, 
noStructures);
+
+                       cList.add(c);
+                               return cList;
+               }
+
+               return cList;
+       }
+
+       static public void setTunables(ChemInfoProperties props, 
Collection<Tunable>args) throws Exception {
+               // Set the Tunables
+               for (Tunable t: args) {
+                       if (props.get(t.getName()) != null) {
+                               Tunable target = props.get(t.getName());
+                               Object value = t.getValue();
+                               try {
+                                       if ((target.getType() == Tunable.LIST) 
&&
+                                           (t.getType() == Tunable.STRING)) {
+                                               setListTunable(target, 
value.toString());
+                                       } else {
+                                               
target.setValue(value.toString());
+                                       }
+                                       target.updateValueListeners();
+                               } catch (Exception e) {
+                                       throw new Exception("Unable to parse 
value for "+
+                                                           t.getName()+": 
"+value.toString());
+                               }
+                       }
+               }
+       }
+
+       static public void setListTunable(Tunable listTunable, String value) {
+               Object[] optionList = (Object [])listTunable.getLowerBound();
+               String[] inputList = value.split(",");
+               String v = "";
+               Integer first = null;
+               for (int i = 0; i < inputList.length; i++) {
+                       for (int j = 0; j < optionList.length; j++) {
+                               if 
(optionList[j].toString().equals(inputList[i])) {
+                                       v = v+","+j;
+                                       if (first == null) first = new 
Integer(j);
+                               }
+                       }
+               }
+               v = v.substring(1);
+               if (listTunable.checkFlag(Tunable.MULTISELECT)) {
+                       listTunable.setValue(v);
+               } else {
+                       listTunable.setValue(first);
+               }
+       }
+}

Modified: 
csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/menus/ChemVizContextMenu.java
===================================================================
--- 
csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/menus/ChemVizContextMenu.java  
    2012-10-10 21:14:28 UTC (rev 30648)
+++ 
csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/menus/ChemVizContextMenu.java  
    2012-10-10 22:33:48 UTC (rev 30649)
@@ -35,6 +35,7 @@
 
 package chemViz.menus;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
 
@@ -54,9 +55,9 @@
 import cytoscape.CytoscapeInit;
 import cytoscape.data.CyAttributes;
 
+import chemViz.commands.ValueUtils;
 import chemViz.model.Compound;
 import chemViz.model.Compound.AttriType;
-import chemViz.tasks.CreateCompoundsTask;
 import chemViz.ui.ChemInfoSettingsDialog;
 
 /**
@@ -151,12 +152,9 @@
                        type = "edge";
                }
 
-               // This little bit of strangeness is to avoid duplicating bunch 
of code to handle
-               // the creation of compounds that's already in 
AbstractCompoundTask.  Here, we
-               // create a task, but just execute the run method directly 
(synchronously)...
-               CreateCompoundsTask t = new CreateCompoundsTask(go, attributes, 
settingsDialog);
-               t.run();
-               List<Compound> cList = t.getCompoundList();
+               List<Compound> cList = ValueUtils.getCompounds(go, attributes, 
+                                                                               
                                                                       
settingsDialog.getCompoundAttributes(type,AttriType.smiles),
+                                                                               
                                                                                
   settingsDialog.getCompoundAttributes(type,AttriType.inchi), false);
 
                Properties cytoProps = CytoscapeInit.getProperties();
                if (cList == null || cList.size() == 0) {

Modified: 
csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/menus/DepictionMenus.java
===================================================================
--- csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/menus/DepictionMenus.java  
2012-10-10 21:14:28 UTC (rev 30648)
+++ csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/menus/DepictionMenus.java  
2012-10-10 22:33:48 UTC (rev 30649)
@@ -252,7 +252,7 @@
         * @param dialog the settings dialog
         */
        private void createPopup(Collection<GraphObject>selection, 
ChemInfoSettingsDialog dialog) {
-    CreatePopupTask loader = new CreatePopupTask(new ArrayList(selection), 
dialog, dialog.getMaxCompounds());
+    CreatePopupTask loader = new CreatePopupTask(new ArrayList(selection), 
dialog, null, dialog.getMaxCompounds());
                TaskManager.executeTask(loader, loader.getDefaultTaskConfig());
        }
 

Modified: csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/model/Compound.java
===================================================================
--- csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/model/Compound.java        
2012-10-10 21:14:28 UTC (rev 30648)
+++ csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/model/Compound.java        
2012-10-10 22:33:48 UTC (rev 30649)
@@ -262,6 +262,7 @@
        private IFingerprinter fp;
        private int lastImageWidth = -1;
        private int lastImageHeight = -1;
+       private int index = 0;
        private boolean lastImageFailed = false;
        private DescriptorEngine descriptorEngine = null;
 
@@ -291,6 +292,7 @@
                } else {
                        mapList = new ArrayList();
                }
+               this.index = mapList.size();
                mapList.add(this);
                Compound.compoundMap.put(source, mapList);
                createStructure();
@@ -419,6 +421,13 @@
                return this.getSmiles().compareTo(o.getSmiles());
        }
 
+       public String toString() {
+               if (source == null)
+                       return maxString(moleculeString, 10);
+
+               return source.getIdentifier()+" ("+attribute+") ["+index+"]";
+       }
+
        /**
         * Handle requests for various compound descriptors.  This is used 
primarily by the CompoundTable, but
         * could be used by other clients as well.
@@ -784,4 +793,9 @@
                return bufferedImage;
        }
 
+       private String maxString(String str, int max) {
+               if (str.length() <= max) return str;
+
+               return str.substring(0,max-3)+"...";
+       }
 }

Modified: 
csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/tasks/CreateCompoundTableTask.java
===================================================================
--- 
csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/tasks/CreateCompoundTableTask.java
 2012-10-10 21:14:28 UTC (rev 30648)
+++ 
csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/tasks/CreateCompoundTableTask.java
 2012-10-10 22:33:48 UTC (rev 30649)
@@ -59,6 +59,8 @@
 public class CreateCompoundTableTask extends AbstractCompoundTask {
        Collection<GraphObject> selection;
        ChemInfoSettingsDialog settingsDialog;
+       CompoundTable   compoundTable = null;
+       List<String> columnList = null;
 
        /**
         * Creates the task.
@@ -74,6 +76,16 @@
                this.compoundCount = 0;
        }
 
+       public CreateCompoundTableTask(Collection<GraphObject> selection, 
ChemInfoSettingsDialog dialog, 
+                                      int maxCompounds, List<String> 
columnList) {
+               this.selection = selection;
+               this.settingsDialog = dialog;
+               this.canceled = false;
+               this.maxCompounds = maxCompounds;
+               this.compoundCount = 0;
+               this.columnList = columnList;
+       }
+
        public String getTitle() {
                return "Creating Table";
        }
@@ -101,8 +113,15 @@
                                                                                
                                                                        
settingsDialog.getCompoundAttributes(type,AttriType.smiles),
                                                                                
                                                                        
settingsDialog.getCompoundAttributes(type,AttriType.inchi));
                if (cList.size() > 0 && !canceled) {
-                       CompoundTable cTable = new CompoundTable(cList);
+                       compoundTable = new CompoundTable(cList, columnList);
                }
        }
 
+       public void closePopup() {
+               if (compoundTable != null) {
+                       compoundTable.dispose();
+                       compoundTable = null;
+               }
+       }
+
 }

Modified: 
csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/tasks/CreatePopupTask.java
===================================================================
--- csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/tasks/CreatePopupTask.java 
2012-10-10 21:14:28 UTC (rev 30648)
+++ csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/tasks/CreatePopupTask.java 
2012-10-10 22:33:48 UTC (rev 30649)
@@ -62,6 +62,7 @@
        List<GraphObject> objectList;
        ChemInfoSettingsDialog dialog;
        String labelAttribute;
+       CompoundPopup   compoundPopup = null;
 
        /**
         * Creates the task.
@@ -83,6 +84,7 @@
                this.maxCompounds = maxCompounds;
                this.compoundCount = 0;
                this.labelAttribute = dialog.getLabelAttribute();
+               this.compoundPopup = null;
        }
 
        /**
@@ -91,13 +93,18 @@
         * @param objects the graph objects that we're creating the popup for
         * @param dialog the settings dialog, which we use to pull the 
attribute names that contain the compound descriptors
         */
-  public CreatePopupTask(List<GraphObject>selection, ChemInfoSettingsDialog 
dialog, int maxCompounds) {
+  public CreatePopupTask(List<GraphObject>selection, ChemInfoSettingsDialog 
dialog, String labelAttribute, int maxCompounds) {
                this.objectList = selection;
                this.dialog = dialog;
                this.canceled = false;
                this.maxCompounds = maxCompounds;
                this.compoundCount = 0;
-               this.labelAttribute = dialog.getLabelAttribute();
+               if (labelAttribute == null)
+                       this.labelAttribute = dialog.getLabelAttribute();
+               else
+                       this.labelAttribute = labelAttribute;
+
+               this.compoundPopup = null;
        }
 
        public String getTitle() {
@@ -126,13 +133,20 @@
                                         
dialog.getCompoundAttributes(type,AttriType.inchi));
                if (cList.size() > 0 && !canceled) {
                        if (objectList.size() == 1) {
-                               CompoundPopup popup = new CompoundPopup(cList, 
objectList, null);
+                               compoundPopup = new CompoundPopup(cList, 
objectList, null);
                        } else {
                                if (labelAttribute.equals("ID"))
-                                       new CompoundPopup(cList, objectList, 
type+".ID");
+                                       compoundPopup = new 
CompoundPopup(cList, objectList, type+".ID");
                                else
-                                       new CompoundPopup(cList, objectList, 
labelAttribute);
+                                       compoundPopup = new 
CompoundPopup(cList, objectList, labelAttribute);
                        }
                }
        }
+
+       public void closePopup() {
+               if (compoundPopup != null) {
+                       compoundPopup.dispose();
+                       compoundPopup = null;
+               }
+       }
 }

Modified: csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/ui/CompoundTable.java
===================================================================
--- csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/ui/CompoundTable.java      
2012-10-10 21:14:28 UTC (rev 30648)
+++ csplugins/trunk/ucsf/scooter/chemViz/src/chemViz/ui/CompoundTable.java      
2012-10-10 22:33:48 UTC (rev 30649)
@@ -83,6 +83,7 @@
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ListSelectionEvent;
 import javax.swing.event.ListSelectionListener;
+import javax.swing.filechooser.FileNameExtensionFilter;
 import javax.swing.table.AbstractTableModel;
 import javax.swing.table.DefaultTableCellRenderer;
 import javax.swing.table.JTableHeader;
@@ -104,6 +105,7 @@
 import cytoscape.logger.CyLogger;
 import cytoscape.view.CyNetworkView;
 
+import chemViz.commands.ValueUtils;
 import chemViz.model.ChemInfoTableModel;
 import chemViz.model.Compound;
 import chemViz.model.Compound.DescriptorType;
@@ -136,7 +138,7 @@
        private List<CompoundColumn> columns;
        private List<Compound> compoundList;
 
-       public CompoundTable (List<Compound> compoundList) {
+       public CompoundTable (List<Compound> compoundList, List<String> 
columnList) {
                super(Cytoscape.getDesktop());
                network = Cytoscape.getCurrentNetwork();
                networkView = Cytoscape.getCurrentNetworkView();
@@ -145,9 +147,48 @@
                setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                this.rowMap = new HashMap();
 
-               // See if we have any table attributes stored
-               columns = 
TableAttributeHandler.getAttributes(Cytoscape.getCurrentNetwork());
+               GraphObject obj = compoundList.get(0).getSource();
+               String objType = "node.";
+               CyAttributes attributes = Cytoscape.getNodeAttributes();
 
+               if (obj instanceof CyEdge) {
+                       objType = "edge.";
+                       attributes = Cytoscape.getEdgeAttributes();
+               }
+
+               if (columnList != null && columnList.size() > 0) {
+                       columns = new ArrayList<CompoundColumn>();
+                       for (String s: columnList) {
+                               String[] tokens = s.trim().split("[:;]");
+                               if (tokens.length != 3 && tokens.length != 2)
+                                       throw new RuntimeException("Illegal 
column specification: "+s);
+                               if (tokens[0].equalsIgnoreCase("descriptor") || 
tokens[0].equalsIgnoreCase("desc")) {
+                                       DescriptorType type = 
ValueUtils.getDescriptor(Compound.getDescriptorList(),tokens[1]);
+                                       int columnWidth = -1;
+                                       if (tokens.length == 3) columnWidth = 
Integer.parseInt(tokens[2]);
+                                       columns.add(new CompoundColumn(type, 
columnWidth));
+                               } else if 
(tokens[0].equalsIgnoreCase("attribute") || tokens[0].equalsIgnoreCase("attr")) 
{
+                                       String attribute = tokens[1];
+                                       byte type = CyAttributes.TYPE_STRING;
+                                       if (!attribute.equals("ID")) {
+                                               type = 
attributes.getType(attribute);
+                                               if (type == 
CyAttributes.TYPE_UNDEFINED)
+                                                       continue;
+                                       }
+                                       int columnWidth = -1;
+                                       if (tokens.length == 3) columnWidth = 
Integer.parseInt(tokens[2]);
+
+                                       if (attribute.equals("ID"))
+                                               columns.add(new 
CompoundColumn("ID", "", CyAttributes.TYPE_STRING, columnWidth));
+                                       else
+                                               columns.add(new 
CompoundColumn(attribute, objType, type, columnWidth));
+                               }
+                       }
+               } else {
+                       // See if we have any table attributes stored
+                       columns = 
TableAttributeHandler.getAttributes(Cytoscape.getCurrentNetwork());
+               }
+
                // Create the table
                initTable();
 
@@ -312,6 +353,9 @@
                } else if (e.getActionCommand().equals("export")) {
                        // Get the file name
                        JFileChooser chooser = new JFileChooser();
+                       FileNameExtensionFilter filter = new 
FileNameExtensionFilter(
+                               "Text file formats", "txt", "tsv");
+                       chooser.setFileFilter(filter);
                        chooser.setDialogTitle("Export Table to File");
                        int returnVal = chooser.showSaveDialog(this);
                        if (returnVal == JFileChooser.APPROVE_OPTION) {
@@ -339,10 +383,16 @@
                        int row = sorter.modelIndex(viewRow);
                        Compound cmpd = compoundList.get(row);
                        for (int viewCol = 0; viewCol < columns.size(); 
viewCol++) {
-                               if (viewCol > 0)
-                                       writer.write("\t");
                                int col = 
table.convertColumnIndexToModel(viewCol);
-                               columns.get(col).output(writer, cmpd);
+                               CompoundColumn c = columns.get(col);
+
+                               // warning -- if the image column is the first 
column, then we'll get a
+                               // leading tab
+                               if (c.getColumnType() != ColumnType.DESCRIPTOR 
|| c.getDescriptor() != DescriptorType.IMAGE) {
+                                       if (viewCol > 0)
+                                               writer.write("\t");
+                                       c.output(writer, cmpd);
+                               }
                        }
                        writer.write("\n");
                }

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