Author: ghuck
Date: 2011-06-15 13:10:19 -0700 (Wed, 15 Jun 2011)
New Revision: 25764

Modified:
   
csplugins/trunk/soc/ghuck/IgraphPlugin/src/cytoscape/plugins/igraph/IgraphPlugin.java
   
csplugins/trunk/soc/ghuck/IgraphPlugin/src/cytoscape/plugins/igraph/IsConnected.java
Log:
Added support for passing only selected nodes to Igraph

Modified: 
csplugins/trunk/soc/ghuck/IgraphPlugin/src/cytoscape/plugins/igraph/IgraphPlugin.java
===================================================================
--- 
csplugins/trunk/soc/ghuck/IgraphPlugin/src/cytoscape/plugins/igraph/IgraphPlugin.java
       2011-06-15 20:03:15 UTC (rev 25763)
+++ 
csplugins/trunk/soc/ghuck/IgraphPlugin/src/cytoscape/plugins/igraph/IgraphPlugin.java
       2011-06-15 20:10:19 UTC (rev 25764)
@@ -67,8 +67,11 @@
        NodeCount nodeCountAction = new NodeCount(this);
        
Cytoscape.getDesktop().getCyMenus().addCytoscapeAction((CytoscapeAction) 
nodeCountAction);
 
-       IsConnected isConnectedAction = new IsConnected(this);
-       
Cytoscape.getDesktop().getCyMenus().addCytoscapeAction((CytoscapeAction) 
isConnectedAction);
+       IsConnected isConnectedAction1 = new IsConnected(this, "All 
nodes",false);
+       
Cytoscape.getDesktop().getCyMenus().addCytoscapeAction((CytoscapeAction) 
isConnectedAction1);
+
+       IsConnected isConnectedAction2 = new IsConnected(this, "Selected 
Nodes", true);
+       
Cytoscape.getDesktop().getCyMenus().addCytoscapeAction((CytoscapeAction) 
isConnectedAction2);
     }
 
     private boolean isOldVersion(){

Modified: 
csplugins/trunk/soc/ghuck/IgraphPlugin/src/cytoscape/plugins/igraph/IsConnected.java
===================================================================
--- 
csplugins/trunk/soc/ghuck/IgraphPlugin/src/cytoscape/plugins/igraph/IsConnected.java
        2011-06-15 20:03:15 UTC (rev 25763)
+++ 
csplugins/trunk/soc/ghuck/IgraphPlugin/src/cytoscape/plugins/igraph/IsConnected.java
        2011-06-15 20:10:19 UTC (rev 25764)
@@ -1,3 +1,22 @@
+/**************************************************************************************
+Copyright (C) Gerardo Huck, 2011
+
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+**************************************************************************************/
+
 package cytoscape.plugins.igraph;
 
 import java.awt.event.ActionEvent;
@@ -9,10 +28,7 @@
 import cytoscape.data.*;
 import cytoscape.plugin.CytoscapePlugin;
 import cytoscape.util.CytoscapeAction;
-import cytoscape.task.Task;
-import cytoscape.task.TaskMonitor;
 
-
 import giny.model.*;
 
 import com.sun.jna.Library;
@@ -21,103 +37,107 @@
 
 public class IsConnected extends CytoscapeAction {
 
+    Boolean selectedOnly;
 
-    private IgraphTaskThread t; //This thread is actually where the action is
-    private Thread currentThread;
-
-
-    public IsConnected(IgraphPlugin myPlugin) {
-       super("IsConnected");
-       setPreferredMenu("Plugins.Igraph");
+    public IsConnected(IgraphPlugin myPlugin, String name, boolean 
selectedOnly) {
+       super(name);
+       setPreferredMenu("Plugins.Igraph.IsConnected");
+       this.selectedOnly = new Boolean(selectedOnly);
     }
 
     public void actionPerformed(ActionEvent e) {
-       JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "Is Connected?: " 
+ isConnected());
+       JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "Is Connected?: " 
+ isConnected(this.selectedOnly));
     }
 
-    public boolean isConnected() {
-
-       loadGraph_optimized();
-        return IgraphInterface.isConnected(); //Only call this function if the 
graph has been loaded
-
-
-//     boolean res = false;
-//     try {
-//         ArrayList<Integer> nodes = loadGraph_optimized();
-//         JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "Graph 
loaded...Number of nodes: " + nodes.size());      
-//         res = IgraphInterface.isConnected();
-//     }
-//     catch (Exception e) {
-//         e.printStackTrace();
-//         JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "Igraph 
Plugin error:\n" + e.getMessage());          
-//         res =  false;
-//     }
-//     finally{
-//         return res;
-//     }
-
-
-//         t = new IgraphTaskThread();
-//         t.setAlgorithm(1);
-
-//         //This marks the current thread
-//         currentThread = Thread.currentThread();
-
-//         t.start();
-//         try{
-//             t.join(); //Join the background thread running the algorithm 
with the current thread showing the progressbar
-//         }
-//         catch(InterruptedException e){
-//             //System.out.println("Algorithm terminated");
-//             //The failed C algorithm may cause issues here
-//         e.printStackTrace();
-//         JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "Igraph 
Plugin error:\n" + e.getMessage()); 
-//         }
-
-//     return t.getResult();
+    public boolean isConnected(boolean selectedOnly) {
+       loadGraph_optimized(selectedOnly);
+        return IgraphInterface.isConnected(); 
     }
 
     //This will load the graph into c
-    public static ArrayList<Integer> loadGraph_optimized(){
+    public static HashMap<Integer,Integer> loadGraph_optimized(boolean 
selectedOnly){
 
         // Nodes are ordered according to node array.
         CyNetwork network = Cytoscape.getCurrentNetwork();
        
-        ArrayList<Integer> selectedNodeIndices = new ArrayList<Integer>();
+       //        ArrayList<Integer> selectedNodeIndices = new 
ArrayList<Integer>();
 
-        int[] edgeIndicesArray = network.getEdgeIndicesArray();
-        int[] nodeArray = network.getNodeIndicesArray();
+       //        int[] edgeIndicesArray = network.getEdgeIndicesArray();
+        // int[] nodeArray = network.getNodeIndicesArray();
+       //      Set<Node> selectedNodes = network.geSelectedNodes();
 
         // Create a reverse mapping
-        HashMap<Integer, Integer> nodeIdMapping = new HashMap<Integer, 
Integer>(nodeArray.length);
-        for(int i=0; i<nodeArray.length; i++){
-            nodeIdMapping.put(nodeArray[i], i);
+       int nodeCount;
+       if(selectedOnly) {
+           nodeCount = network.getSelectedNodes().size();
+       } else {
+           nodeCount = network.getNodeCount();
+       }
+        HashMap<Integer, Integer> nodeIdMapping = new HashMap<Integer, 
Integer>(nodeCount);
+       int j = 0;
+       Iterator<Node> nodeIt;
+       if(selectedOnly) {
+          nodeIt = network.getSelectedNodes().iterator();
+       } else {
+           nodeIt = network.nodesIterator();
+       }
+       
+        while(nodeIt.hasNext()){            
+            Node node = nodeIt.next();
+            nodeIdMapping.put(node.getRootGraphIndex(), j);
+           j++;
         }
+       
 
+//         for(int i=0; i<nodeArray.length; i++){
+//             nodeIdMapping.put(nodeArray[i],i);
+//         }
+
         // Get the selectedNodes
         // Return the selected node array list
-        Iterator<Node> nodeIt = network.getSelectedNodes().iterator();
-        while(nodeIt.hasNext()){
+//         Iterator<Node> nodeIt = network.getSelectedNodes().iterator();
+//         while(nodeIt.hasNext()){
             
-            Node node = nodeIt.next();
-            ////System.out.println("This is gd:" + 
nodeIdMapping.get(node.getRootGraphIndex()));
-            
selectedNodeIndices.add(nodeIdMapping.get(node.getRootGraphIndex()));
-        }
+//             Node node = nodeIt.next();
+//             ////System.out.println("This is gd:" + 
nodeIdMapping.get(node.getRootGraphIndex()));
+//             
selectedNodeIndices.add(nodeIdMapping.get(node.getRootGraphIndex()));
+//         }
 
         //If this works then it will be a lot faster
-        int[] edgeArray = new int[edgeIndicesArray.length * 2];
 
-        //Get all edges, then simplify in igraph
-        for(int i = 0; i < edgeIndicesArray.length; i++){
 
-            edgeArray[i*2] = 
nodeIdMapping.get(network.getEdgeSourceIndex(edgeIndicesArray[i]));
-            edgeArray[i*2 + 1] = 
nodeIdMapping.get(network.getEdgeTargetIndex(edgeIndicesArray[i]));
-        }
+        int[] edgeArray = new int[network.getEdgeCount() * 2];
+       int i = 0;
 
-        IgraphInterface.createGraph(edgeArray, edgeArray.length);
-       IgraphInterface.simplify();
+       Iterator<Edge> it = network.edgesIterator();
 
-        return selectedNodeIndices;
+       while (it.hasNext()) {
+           Edge e = (CyEdge) it.next();
+
+           Node source = e.getSource();
+           Node target = e.getTarget();
+
+           if(!selectedOnly || (network.isSelected(source) && 
network.isSelected(target)) ){
+               edgeArray[i]     = 
nodeIdMapping.get(source.getRootGraphIndex());
+               edgeArray[i + 1] = 
nodeIdMapping.get(target.getRootGraphIndex());
+               i += 2;
+           }
+       }
+
+
+//         for(int i = 0; i < edgeIndicesArray.length; i++){
+
+//         if(!selectedOnly || 
+//            (network.isSelected() && network.isSelected()) ){
+//             edgeArray[i * 2]     = 
nodeIdMapping.get(network.getEdgeTargetIndex(edgeIndicesArray[i]));
+//             edgeArray[i * 2 + 1] = 
nodeIdMapping.get(network.getEdgeSourceIndex(edgeIndicesArray[i]));
+//         }
+//     }
+
+        IgraphInterface.createGraph(edgeArray, i);
+       //      IgraphInterface.simplify();
+
+        return nodeIdMapping;
     }
     
 }
\ No newline at end of file

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