Author: scooter
Date: 2012-07-05 17:29:19 -0700 (Thu, 05 Jul 2012)
New Revision: 29779

Added:
   
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/commands/ExpandNodesCommand.java
   
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/tasks/ExpandNodesTask.java
Modified:
   
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/actions/ExpandNodesAction.java
   
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/dialogs/AnnotateNetworkDialog.java
   
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/tasks/BridgeDBUtils.java
Log:
Added expand nodes functionality.  Requires a new version of the metanode 
plugin to work right.


Modified: 
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/actions/ExpandNodesAction.java
===================================================================
--- 
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/actions/ExpandNodesAction.java
       2012-07-06 00:10:56 UTC (rev 29778)
+++ 
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/actions/ExpandNodesAction.java
       2012-07-06 00:29:19 UTC (rev 29779)
@@ -40,7 +40,10 @@
 import cytoscape.Cytoscape;
 import cytoscape.CyNetwork;
 import cytoscape.logger.CyLogger;
+import cytoscape.task.util.TaskManager;
 
+import bindingDB.tasks.ExpandNodesTask;
+
 public class ExpandNodesAction extends AbstractAction {
        private CyLogger logger = null;
 
@@ -51,5 +54,7 @@
 
        public void actionPerformed (ActionEvent e)
        {
+                       ExpandNodesTask expand = new ExpandNodesTask(logger);
+                       TaskManager.executeTask(expand, 
expand.getDefaultTaskConfig());
        }
 }

Added: 
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/commands/ExpandNodesCommand.java
===================================================================
--- 
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/commands/ExpandNodesCommand.java
                             (rev 0)
+++ 
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/commands/ExpandNodesCommand.java
     2012-07-06 00:29:19 UTC (rev 29779)
@@ -0,0 +1,151 @@
+/* vim: set ts=2: */
+/**
+ * Copyright (c) 2012 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 bindingDB.commands;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import cytoscape.CyEdge;
+import cytoscape.CyNode;
+import cytoscape.Cytoscape;
+import cytoscape.command.CyCommandResult;
+import cytoscape.data.CyAttributes;
+import cytoscape.data.Semantics;
+import cytoscape.groups.CyGroup;
+import cytoscape.groups.CyGroupManager;
+import cytoscape.logger.CyLogger;
+
+
+public class ExpandNodesCommand {
+       /**
+        * This command will create metanodes from each protein that has hits 
and add nodes
+        * representing each hit.
+        */
+       static public CyCommandResult expandNodes(CyLogger logger, CyNode node) 
{
+               CyAttributes nodeAttributes = Cytoscape.getNodeAttributes();
+               CyCommandResult result = new CyCommandResult();
+
+               if (!nodeAttributes.hasAttribute(node.getIdentifier(), 
AnnotateNetworkCommand.HIT_ATTR)) {
+                       result.addMessage("Node "+node.getIdentifier()+" 
doesn't have any hits");
+                       return result;
+               }
+
+               Integer hitCount = 
nodeAttributes.getIntegerAttribute(node.getIdentifier(),
+                                                          
AnnotateNetworkCommand.HIT_ATTR);
+
+               if (hitCount.intValue() == 0) {
+                       result.addMessage("Node "+node.getIdentifier()+" 
doesn't have any hits");
+                       return result;
+               }
+
+               // Get all of our lists
+               List<String>monomerIDList = getStringList(nodeAttributes, node, 
AnnotateNetworkCommand.ID_ATTR);
+               List<String>smilesList = getStringList(nodeAttributes, node, 
AnnotateNetworkCommand.SMILES_ATTR);
+               List<String>typeList = getStringList(nodeAttributes, node, 
AnnotateNetworkCommand.AFF_TYPE_ATTR);
+               List<String>affinityStrList = getStringList(nodeAttributes, 
node, AnnotateNetworkCommand.AFFINITY_STR_ATTR);
+               List<Double>affinityList = getDoubleList(nodeAttributes, node, 
AnnotateNetworkCommand.AFFINITY_ATTR);
+
+               // Create a list of new and edges
+               List<CyNode>nodeList = new ArrayList<CyNode>();
+
+               for (int index = 0; index < monomerIDList.size(); index++) {
+                       String id = monomerIDList.get(index);
+                       String smiles = smilesList.get(index);
+                       String type = typeList.get(index);
+                       String affinityStr = affinityStrList.get(index);
+                       Double affinity = affinityList.get(index);
+                       CyNode newNode = createNode(id, smiles);
+                       CyEdge newEdge = createEdge(node, newNode, type, 
affinityStr, affinity);
+                       nodeList.add(newNode);
+               }
+
+               // Now, create the settings we want the metanode to use
+               String settings = 
"hideMetanode=false;createMembershipEdges=false;dontExpandEmpty=true";
+               nodeAttributes.setAttribute(node.getIdentifier(), 
"__metanodeSettings", settings);
+
+               // OK, now turn the node into a metanode
+               CyGroup group = CyGroupManager.createGroup(node, nodeList, 
"metaNode", Cytoscape.getCurrentNetwork());
+
+               // Collapse the group
+               group.setState(2);
+               // ... and expand it
+               group.setState(1);
+
+               result.addMessage("Created "+nodeList.size()+" nodes and added 
them to "+node.getIdentifier());
+
+               return result;
+       }
+
+       static CyNode createNode(String id, String smiles) {
+               CyNode newNode = Cytoscape.getCyNode(id, true);
+               CyAttributes nodeAttribute = Cytoscape.getNodeAttributes();
+               List<String>smList = new ArrayList<String>();
+               smList.add(smiles);
+               nodeAttribute.setListAttribute(id, 
AnnotateNetworkCommand.SMILES_ATTR, smList);
+               Cytoscape.getCurrentNetwork().addNode(newNode);
+               return newNode;
+       }
+
+       static CyEdge createEdge(CyNode parent, CyNode child, String type, 
String affStr, Double aff) {
+               CyEdge edge = Cytoscape.getCyEdge(parent, child, 
Semantics.INTERACTION, type, true);
+               CyAttributes edgeAttributes = Cytoscape.getEdgeAttributes();
+               edgeAttributes.setAttribute(edge.getIdentifier(), 
AnnotateNetworkCommand.AFFINITY_STR_ATTR, affStr);
+               edgeAttributes.setAttribute(edge.getIdentifier(), 
AnnotateNetworkCommand.AFFINITY_ATTR, aff);
+               Cytoscape.getCurrentNetwork().addEdge(edge);
+               return edge;
+       }
+
+       static List<String> getStringList(CyAttributes attrs, CyNode node, 
String attrName) {
+               if (!attrs.hasAttribute(node.getIdentifier(), attrName))
+                       return new ArrayList<String>();
+
+               return 
(List<String>)attrs.getListAttribute(node.getIdentifier(), attrName);
+       }
+
+       static List<Double> getDoubleList(CyAttributes attrs, CyNode node, 
String attrName) {
+               if (!attrs.hasAttribute(node.getIdentifier(), attrName))
+                       return new ArrayList<Double>();
+
+               return 
(List<Double>)attrs.getListAttribute(node.getIdentifier(), attrName);
+       }
+
+}

Modified: 
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/dialogs/AnnotateNetworkDialog.java
===================================================================
--- 
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/dialogs/AnnotateNetworkDialog.java
   2012-07-06 00:10:56 UTC (rev 29778)
+++ 
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/dialogs/AnnotateNetworkDialog.java
   2012-07-06 00:29:19 UTC (rev 29779)
@@ -51,6 +51,7 @@
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JSlider;
+import javax.swing.SwingWorker;
 
 import javax.swing.border.Border;
 import javax.swing.border.EtchedBorder;
@@ -82,16 +83,20 @@
        JComboBox speciesList;
        JSlider cutoffSlider;
        JLabel speciesLabel;
-       JFrame busyDialog = null;
 
+       // Default species list
+       static final String[] defaultSpecies = {"Human"};
+       // Default type list
+       static final String[] defaultTypes = {"Uniprot/TrEMBL"};
+
+       static final String[] loadingString = {"Loading..."};
+
        public AnnotateNetworkDialog(CyLogger logger) { 
                super(Cytoscape.getDesktop(), "Annotate network"); 
                this.logger = logger;
 
-               showBusyDialog();
                haveCyThesaurus = BridgeDBUtils.haveCyThesaurus();
                initComponents();
-               hideBusyDialog();
                this.pack();
                this.setSize(WIDTH, HEIGHT);
        }
@@ -129,17 +134,18 @@
                        attrPanel.add(typeLabel);
                        typeLabel.setBounds(LEFT, y, 
typeLabel.getPreferredSize().width, 25);
 
-                       typeList = new JComboBox(getTypeList("Human"));
-                       typeList.setSelectedItem("Uniprot/TrEMBL");
                        if (haveCyThesaurus) {
+                               typeList = new JComboBox(getTypeList("Human"));
                                typeList.setToolTipText("If the attribute type 
is not Uniprot/TrEMBL, it will be converted");
                                typeList.addActionListener(this);
                        } else {
+                               typeList = new JComboBox(defaultTypes);
+                               typeList.setSelectedItem("Uniprot/TrEMBL");
                                typeList.setToolTipText("CyThesaurus is 
unavailable -- only Uniprot/TrEMBL is supported");
-                               typeList.setEnabled(false);
                        }
+                       typeList.setEnabled(false);
                        attrPanel.add(typeList);
-                       typeList.setBounds(COLUMN1, y, 
typeList.getPreferredSize().width, 25);
+                       typeList.setBounds(COLUMN1, y, 150, 25);
                }
 
                // Show the species.  This is only enabled if the Idenfier type 
is not Uniprot/trembl
@@ -210,23 +216,18 @@
                        if 
(typeList.getSelectedItem().equals("Uniprot/TrEMBL")) {
                                speciesList.setEnabled(false);
                                speciesLabel.setEnabled(false);
+                       } else if (typeList.getSelectedItem().equals("Guess")) {
+                               // TODO: At some point, we need to add the 
guess type functionality....
                        } else {
                                speciesList.setEnabled(true);
                                speciesLabel.setEnabled(true);
                        }
                        return;
+
+               // Species changed?
                } else if (e.getSource() == speciesList) {
-                       showBusyDialog();
                        // We changed the species -- update typelist
-                       String[] types = 
getTypeList((String)speciesList.getSelectedItem());
-                       DefaultComboBoxModel model = 
(DefaultComboBoxModel)typeList.getModel();
-                       typeList.removeActionListener(this);
-                       model.removeAllElements();
-                       for (String t: types) {
-                               model.addElement(t);
-                       }
-                       typeList.addActionListener(this);
-                       hideBusyDialog();
+                       
updateTypesList(getTypeList((String)speciesList.getSelectedItem()));
                        return;
                }
 
@@ -276,16 +277,18 @@
                return attrList.toArray(new String[1]);
        }
 
-       private String[] getTypeList(String species) {
-               String[] defaultType = {"Uniprot/TrEMBL"};
+       private String[] getTypeList(final String species) {
                if (!haveCyThesaurus) {
-                       return defaultType;
+                       return defaultTypes;
                }
 
-               String[] types = BridgeDBUtils.getSupportedTypes(logger, 
species);
-               if (types == null)
-                       return defaultType;
-               return types;
+               TypesLoader loader = new TypesLoader(species);
+               loader.execute();
+
+               if (typeList != null)
+                       typeList.setEnabled(false);
+
+               return loadingString;
        }
 
        private String[] getSpeciesList() {
@@ -297,22 +300,46 @@
                return species;
        }
 
-       private void showBusyDialog() {
-               if (busyDialog == null)
-                       initBusyDialog();
-               busyDialog.setVisible(true);
+       private void updateTypesList(String[] types) {
+               typeList.removeActionListener(this);
+               DefaultComboBoxModel model = 
(DefaultComboBoxModel)typeList.getModel();
+               model.removeAllElements();
+                       
+               for (String t: types) {
+                       model.addElement(t);
+                       if (t.equals("Uniprot/TrEMBL")) {
+                               typeList.setSelectedItem(t);
+                       }
+               }
+               typeList.addActionListener(this);
        }
-       private void hideBusyDialog() {
-               if (busyDialog != null)
-                       busyDialog.setVisible(false);
+
+       class TypesLoader extends SwingWorker<String[], Void> {
+               String species;
+
+               public TypesLoader(String species) {
+                       this.species = species;
+               }
+
+               @Override
+               public String[] doInBackground() {
+                       return BridgeDBUtils.getSupportedTypes(logger, species);
+               }
+
+               @Override
+               public void done() {
+                       String[] types = defaultTypes;
+                       boolean gotResult = true;
+                       try {
+                               types = get();
+                       } catch (InterruptedException ignore) {
+                       } catch (java.util.concurrent.ExecutionException e) {
+                               logger.error("Unable to load types for species 
"+species+": "+e.getMessage());
+                               gotResult = false;
+                       }
+
+                       updateTypesList(types);
+                       typeList.setEnabled(true);
+               }
        }
-       private void initBusyDialog() {
-               busyDialog = new JFrame("Working...");
-               JPanel contentPane = new JPanel(new BorderLayout());
-               JLabel label = new JLabel("Contacting BridgeDB...");
-               contentPane.add(label, BorderLayout.CENTER);
-               busyDialog.setContentPane(contentPane);
-               busyDialog.pack();
-       }
-
 }

Modified: 
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/tasks/BridgeDBUtils.java
===================================================================
--- 
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/tasks/BridgeDBUtils.java
     2012-07-06 00:10:56 UTC (rev 29778)
+++ 
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/tasks/BridgeDBUtils.java
     2012-07-06 00:29:19 UTC (rev 29779)
@@ -53,10 +53,15 @@
        static final String BASE_URL = "http://webservice.bridgedb.org/";;
 
        static Map<String,String> resources = new HashMap<String,String>();
+       static Map<String,String[]> typeMap = new HashMap<String,String[]>();
        static String selectedResource = null;
        static boolean haveCyThesaurus = false;
+       static String[] speciesCache = null;
 
        static public String[] getSpeciesList(CyLogger logger) {
+               if (speciesCache != null) 
+                       return speciesCache;
+
                List<String> speciesList = new ArrayList<String>();
                try {
                        URL speciesURL = new URL(SPECIES_URL);
@@ -76,6 +81,8 @@
 
                String[] speciesArray = speciesList.toArray(new String[0]);
                Arrays.sort(speciesArray);
+               speciesCache = speciesArray;
+
                // System.out.println("Species: 
"+Arrays.toString(speciesArray));
                return speciesArray;
        }
@@ -84,6 +91,10 @@
                if (haveCyThesaurus) {
                        if (!resources.containsKey(species))
                                return null;
+
+                       if (typeMap.containsKey(species))
+                               return typeMap.get(species);
+
                        selectResource(resources.get(species));
 
                        try {
@@ -93,7 +104,6 @@
                                        Set<String> idTypes = (Set<String>) 
result.getResult();
                                        String[] types = idTypes.toArray(new 
String[0]);
                                        Arrays.sort(types);
-                                       // System.out.println("Types for 
"+species+": "+Arrays.toString(types));
                                        return types;
                                }
                        } catch (Exception e) {
@@ -132,9 +142,10 @@
                Map<String,Object>args = new HashMap<String, Object>();
                String connstring = "idmapper-bridgerest:"+BASE_URL+species;
                args.put("connstring", connstring);
-               args.put("classpath", 
"org.bridgedb.webservice,bridgerest.BridgeRest");
+               args.put("classpath", 
"org.bridgedb.webservice.bridgerest.BridgeRest");
                try {
-                       CyCommandManager.execute("idmapping", "register 
resource", args);
+                       // System.out.println("Registering resource 
"+connstring);
+                       CyCommandResult result = 
CyCommandManager.execute("idmapping", "register resource", args);
                } catch (Exception e) {}
 
                resources.put(species, connstring);
@@ -151,7 +162,8 @@
                Map<String,Object>args = new HashMap<String, Object>();
                args.put("connstring", connstring);
                try {
-                       CyCommandManager.execute("idmapping", "deselect 
resource", args);
+                       // System.out.println("Deselecting resource 
"+connstring);
+                       CyCommandResult result = 
CyCommandManager.execute("idmapping", "deselect resource", args);
                } catch (Exception e) {}
        }
 
@@ -162,7 +174,8 @@
                Map<String,Object>args = new HashMap<String, Object>();
                args.put("connstring", connstring);
                try {
-                       CyCommandManager.execute("idmapping", "select 
resource", args);
+                       // System.out.println("Selecting resource "+connstring);
+                       CyCommandResult result = 
CyCommandManager.execute("idmapping", "select resource", args);
                } catch (Exception e) {}
 
                selectedResource = connstring;

Added: 
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/tasks/ExpandNodesTask.java
===================================================================
--- 
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/tasks/ExpandNodesTask.java
                           (rev 0)
+++ 
csplugins/trunk/ucsf/scooter/bindingDb/src/main/java/bindingDB/tasks/ExpandNodesTask.java
   2012-07-06 00:29:19 UTC (rev 29779)
@@ -0,0 +1,72 @@
+/* vim: set ts=2: */
+/**
+ * Copyright (c) 2012 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 bindingDB.tasks;
+
+import java.util.List;
+import java.util.Set;
+
+import cytoscape.Cytoscape;
+import cytoscape.CyNode;
+import cytoscape.command.CyCommandResult;
+import cytoscape.logger.CyLogger;
+import cytoscape.task.Task;
+import cytoscape.task.TaskMonitor;
+
+import bindingDB.commands.ExpandNodesCommand;
+
+public class ExpandNodesTask extends AbstractTask {
+
+       public ExpandNodesTask(CyLogger logger) {
+               this.logger = logger;
+       }
+
+       public String getTitle() {
+               return "Expanding Nodes";
+       }
+
+       public void run() {
+               setStatus("Expanding hits");
+               double complete = 0.0;
+               Set<CyNode> selectedNodes = 
(Set<CyNode>)Cytoscape.getCurrentNetwork().getSelectedNodes();
+               if (selectedNodes == null || selectedNodes.size() == 0)
+                       selectedNodes = 
(Set<CyNode>)Cytoscape.getCurrentNetwork().nodesList();
+               double stepSize = (100.0)/selectedNodes.size();
+               for (CyNode node: selectedNodes) {
+                       CyCommandResult result = 
ExpandNodesCommand.expandNodes(logger, node);
+                       // logger.debug("Annotating "+node.getIdentifier());
+                       complete += stepSize;
+                       setPercentCompleted((int)complete);
+                       if (canceled) break;
+               }
+       }
+}

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