Author: ghuck
Date: 2011-07-31 13:46:09 -0700 (Sun, 31 Jul 2011)
New Revision: 26344

Modified:
   
csplugins/trunk/soc/ghuck/IgraphPlugin/src/cytoscape/plugins/igraph/IgraphAPI.java
   
csplugins/trunk/soc/ghuck/IgraphPlugin/src/cytoscape/plugins/igraph/MinimumSpanningTree.java
Log:


Modified: 
csplugins/trunk/soc/ghuck/IgraphPlugin/src/cytoscape/plugins/igraph/IgraphAPI.java
===================================================================
--- 
csplugins/trunk/soc/ghuck/IgraphPlugin/src/cytoscape/plugins/igraph/IgraphAPI.java
  2011-07-30 14:49:43 UTC (rev 26343)
+++ 
csplugins/trunk/soc/ghuck/IgraphPlugin/src/cytoscape/plugins/igraph/IgraphAPI.java
  2011-07-31 20:46:09 UTC (rev 26344)
@@ -98,6 +98,108 @@
        return nodeIdMapping;
     } // loadGraph()
        
+    public static double[] computeWeights(String attrName, 
+                                         HashMap<Integer,Integer> mapping, 
+                                         boolean selectedOnly) {
+       
+       CyNetwork network = Cytoscape.getCurrentNetwork();
+       CyAttributes edgeAttributes = Cytoscape.getEdgeAttributes();
+       double[] weights = new double[network.getEdgeCount() * 2];              
 
+       boolean defaultSet = false;
+       double defaultValue = 0.0;
 
+       int i = 0;      
+       byte type = edgeAttributes.getType(attrName);
+       Iterator<Edge> it = network.edgesIterator();
+
+       if (type == CyAttributes.TYPE_INTEGER) { 
+
+           while (it.hasNext()) {
+               Edge e = (CyEdge) it.next();
+
+               Node source = e.getSource();
+               Node target = e.getTarget();
+
+
+               if (!selectedOnly || 
+                   (network.isSelected(source) && network.isSelected(target)) 
){
+                   //          int s = 
nodeIdMapping.get(source.getRootGraphIndex());
+                   //          int t = 
nodeIdMapping.get(target.getRootGraphIndex());
+
+                   Integer a = 
edgeAttributes.getIntegerAttribute(e.getIdentifier(), attrName);
+                   if (null == a) {
+                       if (defaultSet) {
+                           weights[i] = defaultValue;
+                       } else {
+                           while (true) {
+                               String inputValue = 
JOptionPane.showInputDialog("Found an edge without attribute. Please enter a 
default value to be used for all edges without attribute " + attrName);
+                               try {
+                                   Double d = Double.parseDouble(inputValue);
+                                   defaultValue = d.doubleValue();
+                                   defaultSet = true;
+                                   break;
+                               } catch (Exception ex) {
+                                   
JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "Please enter a valid 
number");           
+                               }
+                           }
+                           
JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "default: " + 
defaultValue);
+                       }
+
+                   } else {
+                       weights[i] = a.doubleValue();
+                   }
+                   i++;
+
+               }
+           } // while
+           
+       } else { // (type == CyAttributes.TYPE_FLOATING) 
+           while (it.hasNext()) {
+               Edge e = (CyEdge) it.next();
+
+               Node source = e.getSource();
+               Node target = e.getTarget();
+
+               if (!selectedOnly || 
+                   (network.isSelected(source) && network.isSelected(target)) 
){
+
+                   Double a = 
edgeAttributes.getDoubleAttribute(e.getIdentifier(), attrName);
+                   if (null == a) {
+                       if (defaultSet) {
+                           weights[i] = defaultValue;
+                       } else {
+                           while (true) {
+                               String inputValue = 
JOptionPane.showInputDialog("Found an edge without attribute. Please enter a 
default value to be used for all edges without attribute " + attrName);
+                               try {
+                                   Double d = Double.parseDouble(inputValue);
+                                   defaultValue = d.doubleValue();
+                                   defaultSet = true;
+                                   break;
+                               
+                               } catch (Exception ex) {
+                                       
JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "Please enter a valid 
number");       
+                               }
+                           }
+
+                           
JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "default: " + 
defaultValue);
+                       }
+                   } else {
+                       weights[i] = a.doubleValue();
+                   }
+                   i++;
+               }
+           } // while
+                   
+       } 
+       
+    JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "i = " + i);     
+       // if (i != mapping.size())
+//         JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "ERROR! 
\nNumber of edge attributes do not match number of edges!!");     
+//     else
+//         JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "Weights 
OK!");           
+
+       return weights;
+    }
+
 }
\ No newline at end of file

Modified: 
csplugins/trunk/soc/ghuck/IgraphPlugin/src/cytoscape/plugins/igraph/MinimumSpanningTree.java
===================================================================
--- 
csplugins/trunk/soc/ghuck/IgraphPlugin/src/cytoscape/plugins/igraph/MinimumSpanningTree.java
        2011-07-30 14:49:43 UTC (rev 26343)
+++ 
csplugins/trunk/soc/ghuck/IgraphPlugin/src/cytoscape/plugins/igraph/MinimumSpanningTree.java
        2011-07-31 20:46:09 UTC (rev 26344)
@@ -79,25 +79,18 @@
            int numEdges;          
 
            /*          Compute MST          */
-           if (isWeighted) {
-               // Compute weights
-               double[] weights;
-               weights = new double[mapping.size()];           
+           if (isWeighted) {                                   
+               String attribute = chooseEdgeAttribute();
+               double[] weights = IgraphAPI.computeWeights(attribute, 
+                                                           mapping, 
+                                                           selectedOnly);
 
-               // TODO: Compute weights!!!
-
                numEdges = IgraphInterface.minimum_spanning_tree_weighted(mst, 
weights);
 
            } else {
                numEdges = 
IgraphInterface.minimum_spanning_tree_unweighted(mst);
            }
 
-//         logger.info("Nodes in MST: " + numNodes);
-//         logger.info("Edges in MST: " + numEdges);
-//         String nodesString = new String();
-//         for (int i = 0; i < 2 * numEdges; i++)
-//             nodesString = nodesString + mst[i] + ", ";
-//         logger.info("Nodes: "+ nodesString);
 
            /*          Create new network & networkView          */
            CyNetworkView oldView = Cytoscape.getCurrentNetworkView();
@@ -208,4 +201,37 @@
 
     }
 
+
+    protected static String chooseEdgeAttribute() {
+
+       String attribute;
+
+       // Create a list with all Integer or Double edge attributes
+       CyAttributes edgeAttributes = Cytoscape.getEdgeAttributes();
+       String[] attrArr = edgeAttributes.getAttributeNames();
+       Vector attrVector = new Vector();
+       
+       for (int i = 0; i < attrArr.length; i++) {
+           byte type = edgeAttributes.getType(attrArr[i]);
+           if (type == CyAttributes.TYPE_INTEGER || type == 
CyAttributes.TYPE_FLOATING)
+               attrVector.add(attrArr[i]);
+       }
+
+       if (attrVector.size() == 0) {
+           JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "No suitable 
(numeric) edge attribute found.\nWill perform unweighted Minimum Spanning Tree 
computation instead.");
+           attribute = "";
+       } else {
+           Object[] possibleValues = attrVector.toArray();
+           attribute = (String) 
JOptionPane.showInputDialog(Cytoscape.getDesktop(),
+                                                            "Choose which edge 
attribute will be used as weight", 
+                                                            "Minimum Spanning 
Tree (Weighted)",
+                                                            
JOptionPane.INFORMATION_MESSAGE, null,
+                                                            possibleValues, 
possibleValues[0]);            
+       }
+       
+       // JOptionPane.showMessageDialog(Cytoscape.getDesktop(), "Edge 
attributes:\n" +  attribute);
+
+       return attribute;
+    }
+
 }
\ 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