Author: pwang
Date: 2012-02-15 16:50:50 -0800 (Wed, 15 Feb 2012)
New Revision: 28295
Added:
csplugins/trunk/ucsd/pwang/subnetworkByCategory/src/main/java/org/cytoscape/subnetwork/DetailedNetworkCreator.java
csplugins/trunk/ucsd/pwang/subnetworkByCategory/src/main/java/org/cytoscape/subnetwork/DetailedViewLayout.java
csplugins/trunk/ucsd/pwang/subnetworkByCategory/src/main/java/org/cytoscape/subnetwork/NNTKNetworkListener.java
csplugins/trunk/ucsd/pwang/subnetworkByCategory/src/main/java/org/cytoscape/subnetwork/NNTKNodeContextMenuListener.java
Log:
Original creation
Added:
csplugins/trunk/ucsd/pwang/subnetworkByCategory/src/main/java/org/cytoscape/subnetwork/DetailedNetworkCreator.java
===================================================================
---
csplugins/trunk/ucsd/pwang/subnetworkByCategory/src/main/java/org/cytoscape/subnetwork/DetailedNetworkCreator.java
(rev 0)
+++
csplugins/trunk/ucsd/pwang/subnetworkByCategory/src/main/java/org/cytoscape/subnetwork/DetailedNetworkCreator.java
2012-02-16 00:50:50 UTC (rev 28295)
@@ -0,0 +1,203 @@
+package org.cytoscape.subnetwork;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+import giny.model.GraphPerspective;
+import giny.model.Node;
+import cytoscape.data.CyAttributes;
+import cytoscape.layout.CyLayoutAlgorithm;
+import cytoscape.view.*;
+import cytoscape.visual.VisualStyle;
+import cytoscape.*;
+import cytoscape.util.CyNetworkNaming;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.HashMap;
+import java.util.HashSet;
+
+public class DetailedNetworkCreator
+{
+// private static String edgeAttrASDF = "PanGIA.Interaction Type";
+// private static String edgeValuePHYSICAL = "Physical";
+// private static String edgeValueGENETIC_NEGATIVE = "Genetic(negative)";
+// private static String edgeValueGENETIC_POSITIVE = "Genetic(postive)";
+// private static String edgeValueGENETIC = "Genetic";
+// private static String edgeValuePHYS_GEN_POS =
"Physical&Genetic(positive)";
+// private static String edgeValuePHYS_GEN_NEG =
"Physical&Genetic(negative)";
+// private static String edgeValuePHYS_GEN = "Physical&Genetic";
+
+ @SuppressWarnings("unchecked")
+ public static void createDetailedView(CyNetworkView view)
+ {
+ if (view.getSelectedNodeIndices().length==0) return;
+
+ if (view.getSelectedNodeIndices().length==1)
+ {
+
goToNestedNetwork(Cytoscape.getRootGraph().getNode(view.getSelectedNodeIndices()[0]));
+ System.out.println("Going to nested network.");
+ return;
+ }
+
+ //VisualStyleObserver.setOverviewView(view);
+
+ String netID = view.getNetwork().getIdentifier();
+
+ //PanGIAOutput output = PanGIAPlugin.output.get(netID);
+
+// CyNetwork origPhysNetwork = null; //output.getOrigPhysNetwork();
+// CyNetwork origGenNetwork = null; //output.getOrigGenNetwork();
+// String physEdgeAttrName = ""; //output.getPhysEdgeAttrName();
+// String genEdgeAttrName = ""; //output.getGenEdgeAttrName();
+
+ String title = "Detailed View";
+
+ int[] selected = view.getSelectedNodeIndices();
+
+ if (selected.length<=3)
+ {
+ title =
Cytoscape.getRootGraph().getNode(selected[0]).getIdentifier();
+ for (int ni=1;ni<selected.length;ni++)
+ title+=" |
"+Cytoscape.getRootGraph().getNode(selected[ni]).getIdentifier();
+ }
+
+ String name = CyNetworkNaming.getSuggestedNetworkTitle(title);
+
+ CyNetwork detailedNetwork = Cytoscape.createNetwork(name,
/* create_view = */false);
+ CyAttributes networkAttr = Cytoscape.getNetworkAttributes();
+// networkAttr.setAttribute(detailedNetwork.getIdentifier(),
VisualStyleObserver.NETWORK_TYPE_ATTRIBUTE_NAME, NetworkType.DETAILED.name());
+//
networkAttr.setUserVisible(VisualStyleObserver.NETWORK_TYPE_ATTRIBUTE_NAME,
false);
+//
networkAttr.setUserEditable(VisualStyleObserver.NETWORK_TYPE_ATTRIBUTE_NAME,
false);
+
+ // Get parent network
+ String parentNetworkId = networkAttr.getAttribute(netID,
"parentNetworkId").toString();
+ CyNetwork parentNetwrok = Cytoscape.getNetwork(parentNetworkId);
+
+ //Populate network
+ //Nodes
+ for (int ni : selected)
+ {
+ GraphPerspective nn =
Cytoscape.getRootGraph().getNode(ni).getNestedNetwork();
+ if (nn!=null)
+ {
+ for (int ni2 : nn.getNodeIndicesArray())
+ detailedNetwork.addNode(ni2);
+ }
+ }
+
+ //Edges
+ final CyAttributes edgeAttributes =
Cytoscape.getEdgeAttributes();
+
+ List<CyNode> nodes = detailedNetwork.nodesList();
+
+ List<CyEdge> edges = (List<CyEdge>)
parentNetwrok.getConnectingEdges(getIntersectingNodes(parentNetwrok, nodes));
+
+ Iterator it = edges.iterator();
+ while (it.hasNext()){
+ detailedNetwork.addEdge((CyEdge)it.next());
+ }
+
+
+// // Add the edges induced by "origPhysNetwork" to our new nested
network.
+// List<CyEdge> edges = (List<CyEdge>)
origPhysNetwork.getConnectingEdges(getIntersectingNodes(origPhysNetwork,
nodes));
+// for (final CyEdge edge : edges)
+// {
+// final Double attrValue =
edgeAttributes.getDoubleAttribute(edge.getIdentifier(), physEdgeAttrName);
+// if (attrValue != null)
+// {
+// detailedNetwork.addEdge(edge);
+//
edgeAttributes.setAttribute(edge.getIdentifier(), edgeAttrASDF,
edgeValuePHYSICAL);
+// }
+// }
+//
+// // Add the edges induced by "origGenNetwork" to our new nested
network.
+// edges = (List<CyEdge>)
origGenNetwork.getConnectingEdges(getIntersectingNodes(origGenNetwork, nodes));
+// for (final CyEdge edge : edges)
+// {
+// final Double attrValue =
edgeAttributes.getDoubleAttribute(edge.getIdentifier(), genEdgeAttrName);
+// if (attrValue != null)
+// {
+// detailedNetwork.addEdge(edge);
+// Object existingAttribute =
edgeAttributes.getAttribute(edge.getIdentifier(), edgeAttrASDF);
+//
+// if (existingAttribute==null ||
!existingAttribute.equals(edgeValuePHYSICAL))
+// {
+// if (true /* FIXME! output.isSigned() */)
+// {
+// if (attrValue<0)
edgeAttributes.setAttribute(edge.getIdentifier(), edgeAttrASDF,
edgeValueGENETIC_NEGATIVE);
+// else
edgeAttributes.setAttribute(edge.getIdentifier(), edgeAttrASDF,
edgeValueGENETIC_POSITIVE);
+// }else
edgeAttributes.setAttribute(edge.getIdentifier(), edgeAttrASDF,
edgeValueGENETIC);
+// }
+// else
+// if (true /* FIXME! output.isSigned() */)
+// {
+// if (attrValue<0)
edgeAttributes.setAttribute(edge.getIdentifier(), edgeAttrASDF,
edgeValuePHYS_GEN_NEG);
+// else
edgeAttributes.setAttribute(edge.getIdentifier(), edgeAttrASDF,
edgeValuePHYS_GEN_POS);
+// }else
edgeAttributes.setAttribute(edge.getIdentifier(), edgeAttrASDF,
edgeValuePHYS_GEN);
+//
+//
+// //if (existingAttribute==null ||
!existingAttribute.equals(edgeValuePHYSICAL))
edgeAttributes.setAttribute(edge.getIdentifier(), edgeAttrASDF,
edgeValueGENETIC);
+// //else
edgeAttributes.setAttribute(edge.getIdentifier(), edgeAttrASDF,
edgeValuePHYS_GEN);
+// }
+// }
+
+
+ // Keep track of modules and their node sets
+ Map<String,Set<Node>> module_nodes = new
HashMap<String,Set<Node>>();
+ for (int i=0;i<selected.length; i++ )
+ {
+ CyNode node =
(CyNode)Cytoscape.getRootGraph().getNode(selected[i]);
+ String parent = node.getIdentifier();
+
+ Set<Node> sset = new HashSet<Node>();
+ Iterator nodeIt2 =
node.getNestedNetwork().nodesList().iterator();
+ while (nodeIt2.hasNext()){
+ sset.add((Node)nodeIt2.next());
+ }
+ module_nodes.put(parent, sset);
+ }
+
+
+ CyNetworkView theView =
Cytoscape.createNetworkView(detailedNetwork);
+
+ DetailedViewLayout.layout(theView, view, module_nodes);
+
+// theView.setVisualStyle(VisualStyleObserver.VS_MODULE_NAME);
+//
Cytoscape.getVisualMappingManager().setVisualStyle(Cytoscape.getVisualMappingManager().getCalculatorCatalog().getVisualStyle(VisualStyleObserver.VS_MODULE_NAME));
+ theView.redrawGraph(false, true);
+ }
+
+ public static void goToNestedNetwork(Node n)
+ {
+ if (n.getNestedNetwork() == null)
+ return;
+
+ CyNetwork nestedNetwork = (CyNetwork)n.getNestedNetwork();
+
+ CyNetworkView theView =
Cytoscape.getNetworkView(nestedNetwork.getIdentifier());
+ if (theView == null || theView.getIdentifier() == null)
+ {
+ theView = Cytoscape.createNetworkView(nestedNetwork);
+ CyLayoutAlgorithm alg =
cytoscape.layout.CyLayouts.getLayout("force-directed");
+ theView.applyLayout(alg);
+ theView.redrawGraph(false, false);
+ }
+
+ Cytoscape.getDesktop().setFocus(nestedNetwork.getIdentifier());
+ }
+
+ /**
+ * @returns the list of nodes that are both, in "network", and in
"nodes"
+ */
+ private static List<CyNode> getIntersectingNodes(final CyNetwork
network, final List<CyNode> nodes) {
+ final List<CyNode> commonNodes = new ArrayList<CyNode>();
+ for (final CyNode node : nodes) {
+ if (network.containsNode(node))
+ commonNodes.add(node);
+ }
+
+ return commonNodes;
+ }
+}
Added:
csplugins/trunk/ucsd/pwang/subnetworkByCategory/src/main/java/org/cytoscape/subnetwork/DetailedViewLayout.java
===================================================================
---
csplugins/trunk/ucsd/pwang/subnetworkByCategory/src/main/java/org/cytoscape/subnetwork/DetailedViewLayout.java
(rev 0)
+++
csplugins/trunk/ucsd/pwang/subnetworkByCategory/src/main/java/org/cytoscape/subnetwork/DetailedViewLayout.java
2012-02-16 00:50:50 UTC (rev 28295)
@@ -0,0 +1,226 @@
+package org.cytoscape.subnetwork;
+
+import giny.model.Node;
+import giny.view.NodeView;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import cytoscape.Cytoscape;
+import cytoscape.data.CyAttributes;
+import cytoscape.layout.CyLayoutAlgorithm;
+import cytoscape.layout.CyLayouts;
+import cytoscape.view.CyNetworkView;
+
+import org.cytoscape.subnetwork.data.DoubleVector;
+
+public class DetailedViewLayout
+{
+ private static double BUFFER_RATIO = 1.5;
+
+ public static void layout(CyNetworkView view, CyNetworkView overview,
Map<String,Set<Node>> module_nodes)
+ {
+ //Get values of Parent Module attribute
+// CyAttributes nodeAttr = Cytoscape.getNodeAttributes();
+// Map<String,Set<Node>> module_nodes = new
HashMap<String,Set<Node>>();
+// for (int ni : view.getNetwork().getNodeIndicesArray())
+// {
+// String nodeID =
view.getNetwork().getNode(ni).getIdentifier();
+// String parent = nodeAttr.getAttribute(nodeID,
"nestedNetworkId"
/*VisualStyleObserver.PARENT_MODULE_ATTRIBUTE_NAME*/).toString();
+//
+// Set<Node> sset = module_nodes.get(parent);
+// if (sset==null)
+// {
+// sset = new HashSet<Node>();
+// sset.add(view.getNetwork().getNode(ni));
+// module_nodes.put(parent, sset);
+// }else sset.add(view.getNetwork().getNode(ni));
+// }
+
+ //For each parent module
+ CyLayoutAlgorithm fd = CyLayouts.getLayout("force-directed");
+ fd.setSelectedOnly(true);
+ for (Entry<String,Set<Node>> e : module_nodes.entrySet())
+ {
+ //Select all nodes with this attribute value
+ view.getNetwork().unselectAllNodes();
+ view.getNetwork().setSelectedNodeState(e.getValue(),
true);
+
+ //Perform force-directed layout of just the selected
+
+ fd.getSettings().updateValues();
+ fd.updateSettings();
+ view.applyLayout(fd);
+
+ view.redrawGraph(true, true);
+ }
+ fd.setSelectedOnly(false);
+ view.getNetwork().unselectAllNodes();
+
+ //Get the meanPosition and radius of each group
+ List<String> moduleList = new
ArrayList<String>(module_nodes.keySet());
+ double[] centerX = new double[moduleList.size()];
+ double[] centerY = new double[moduleList.size()];
+ double[] radius = new double[moduleList.size()];
+
+ for (int i=0;i<moduleList.size();i++)
+ {
+ Set<Node> nodes = module_nodes.get(moduleList.get(i));
+
+ if (nodes.size()<=1)
+ {
+ radius[i] = 10;
+ continue;
+ }
+
+ double minX = Double.MAX_VALUE;
+ double maxX = Double.MIN_VALUE;
+ double minY = Double.MAX_VALUE;
+ double maxY = Double.MIN_VALUE;
+
+ for (Node n : nodes)
+ {
+ NodeView nv = view.getNodeView(n);
+ double x = nv.getXPosition();
+ double y = nv.getYPosition();
+
+ if (x<minX) minX = x;
+ else if (x>maxX) maxX = x;
+
+ if (y<minY) minY = y;
+ else if (y>maxY) maxY = y;
+ }
+
+ centerX[i] = (maxX+minX)/2.0;
+ centerY[i] = (maxY+minY)/2.0;
+
+ double diffX = maxX-centerX[i];
+ double diffY = maxY-centerY[i];
+ radius[i] = Math.sqrt(diffX*diffX+diffY*diffY);
+
+ if (Double.isNaN(radius[i])) radius[i] = 10;
+ }
+
+
+ //Get the map from module to overview node
+ Map<String,NodeView> module_overviewNode = new
HashMap<String,NodeView>(module_nodes.size(),1);
+
+ @SuppressWarnings("rawtypes")
+ List overviewNodes = overview.getSelectedNodes();
+ for (String mod : module_nodes.keySet())
+ for (Object n : overviewNodes)
+ if
(((NodeView)n).getNode().getIdentifier().equals(mod))
+ {
+ module_overviewNode.put(mod,
(NodeView)n);
+ break;
+ }
+
+
+ //Get the reference positions. Normalize to current scale
+ double minRadius = DoubleVector.min(radius);
+ double maxRadius = DoubleVector.max(radius);
+
+ double scale = BUFFER_RATIO*(minRadius+maxRadius);
+ minRadius = Math.max(minRadius, scale/100);
+ scale = BUFFER_RATIO*(minRadius+maxRadius);
+
+ for (int i=0;i<radius.length;i++)
+ if (radius[i]<minRadius) radius[i] = minRadius;
+
+ double[] newCenterX = new double[moduleList.size()];
+ double[] newCenterY = new double[moduleList.size()];
+
+ double minX = Double.MAX_VALUE;
+ double maxX = Double.MIN_VALUE;
+ double minY = Double.MAX_VALUE;
+ double maxY = Double.MIN_VALUE;
+
+ for (int i=0;i<radius.length;i++)
+ {
+ NodeView on =
module_overviewNode.get(moduleList.get(i));
+ double x = on.getXPosition();
+ double y = on.getYPosition();
+
+ newCenterX[i] = x;
+ newCenterY[i] = y;
+
+ if (x<minX) minX = x;
+ else if (x>maxX) maxX = x;
+
+ if (y<minY) minY = y;
+ else if (y>maxY) maxY = y;
+ }
+
+ if (Math.abs(maxX-minX) < 1e-3) DoubleVector.fill(newCenterX,0);
+ else newCenterX = DoubleVector.times(newCenterX, scale /
(maxX-minX));
+
+ if (Math.abs(maxY-minY) < 1e-3) DoubleVector.fill(newCenterY,0);
+ else newCenterY = DoubleVector.times(newCenterY, scale /
(maxY-minY));
+
+ newCenterX = DoubleVector.meanCenter(newCenterX);
+ newCenterY = DoubleVector.meanCenter(newCenterY);
+
+ double maxOverlapRatio = Double.MIN_VALUE;
+ OverlapRatio:
+ while (maxOverlapRatio==Double.MIN_VALUE)
+ for (int i=0;i<radius.length;i++)
+ for (int j=i+1;j<radius.length;j++)
+ {
+ double diffX =
Math.abs(newCenterX[i]-newCenterX[j]);
+ double diffY =
Math.abs(newCenterY[i]-newCenterY[j]);
+
+ double overlapRatio =
BUFFER_RATIO*(radius[i]+radius[j])/Math.sqrt(diffX*diffX+diffY*diffY);
+
+ if (diffX<1e-3 || diffY<1e-3)
+ {
+ //System.out.println(i+",
"+j+", "+diffX+", "+diffY);
+
+ if (diffX<1e-3)
+ {
+
newCenterX[i]-=(radius[i]/2+scale/100);
+
newCenterX[j]+=(radius[j]/2+scale/100);
+ }
+
+ if (diffY<1e-3)
+ {
+
newCenterY[i]-=(radius[i]/2+scale/100);
+
newCenterY[j]+=(radius[j]/2+scale/100);
+ }
+
+ maxOverlapRatio =
Double.MIN_VALUE;
+ continue OverlapRatio;
+ }
+
+ if (overlapRatio>maxOverlapRatio)
maxOverlapRatio = overlapRatio;
+ }
+
+ if (Double.isInfinite(maxOverlapRatio)) maxOverlapRatio =
Double.MAX_VALUE/1000;
+
+ //System.out.println("Max overlap ratio: "+maxOverlapRatio);
+
+ newCenterX = DoubleVector.times(newCenterX, maxOverlapRatio);
+ newCenterY = DoubleVector.times(newCenterY, maxOverlapRatio);
+
+ //Shift nodes to the new centers
+ double[] shiftX = DoubleVector.subtract(newCenterX,centerX);
+ double[] shiftY = DoubleVector.subtract(newCenterY,centerY);
+
+ for (int i=0;i<radius.length;i++)
+ {
+ for (Node n : module_nodes.get(moduleList.get(i)))
+ {
+ NodeView on = view.getNodeView(n);
+ on.setXPosition(on.getXPosition()+shiftX[i]);
+ on.setYPosition(on.getYPosition()+shiftY[i]);
+ }
+ }
+
+ view.fitContent();
+ view.updateView();
+ }
+}
Added:
csplugins/trunk/ucsd/pwang/subnetworkByCategory/src/main/java/org/cytoscape/subnetwork/NNTKNetworkListener.java
===================================================================
---
csplugins/trunk/ucsd/pwang/subnetworkByCategory/src/main/java/org/cytoscape/subnetwork/NNTKNetworkListener.java
(rev 0)
+++
csplugins/trunk/ucsd/pwang/subnetworkByCategory/src/main/java/org/cytoscape/subnetwork/NNTKNetworkListener.java
2012-02-16 00:50:50 UTC (rev 28295)
@@ -0,0 +1,24 @@
+package org.cytoscape.subnetwork;
+
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+
+import cytoscape.CyNetwork;
+import cytoscape.CyNetworkTitleChange;
+import cytoscape.Cytoscape;
+import cytoscape.view.CyNetworkView;
+import cytoscape.view.CytoscapeDesktop;
+
+public class NNTKNetworkListener implements PropertyChangeListener
+{
+ public void propertyChange(PropertyChangeEvent event) {
+
+ if
(CytoscapeDesktop.NETWORK_VIEW_CREATED.equals(event.getPropertyName()))
+ {
+ final CyNetworkView view = (CyNetworkView) event.getNewValue();
+
+ final NNTKNodeContextMenuListener nodeMenuListener = new
NNTKNodeContextMenuListener(view);
+ view.addNodeContextMenuListener(nodeMenuListener);
+ }
+ }
+}
Added:
csplugins/trunk/ucsd/pwang/subnetworkByCategory/src/main/java/org/cytoscape/subnetwork/NNTKNodeContextMenuListener.java
===================================================================
---
csplugins/trunk/ucsd/pwang/subnetworkByCategory/src/main/java/org/cytoscape/subnetwork/NNTKNodeContextMenuListener.java
(rev 0)
+++
csplugins/trunk/ucsd/pwang/subnetworkByCategory/src/main/java/org/cytoscape/subnetwork/NNTKNodeContextMenuListener.java
2012-02-16 00:50:50 UTC (rev 28295)
@@ -0,0 +1,80 @@
+
+package org.cytoscape.subnetwork;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.JFileChooser;
+import javax.swing.JMenu;
+import javax.swing.JMenuItem;
+import javax.swing.JOptionPane;
+import javax.swing.JPopupMenu;
+
+import giny.model.Edge;
+import giny.model.Node;
+import giny.view.EdgeView;
+import giny.view.GraphView;
+import giny.view.NodeView;
+import cytoscape.CyEdge;
+import cytoscape.CyNetwork;
+import cytoscape.Cytoscape;
+import cytoscape.CyNode;
+import cytoscape.data.CyAttributes;
+import cytoscape.view.CyNetworkView;
+import ding.view.NodeContextMenuListener;
+
+import java.util.*;
+
+import java.io.*;
+
+public class NNTKNodeContextMenuListener implements NodeContextMenuListener
+{
+ private final CyNetworkView view;
+
+ public NNTKNodeContextMenuListener(CyNetworkView view)
+ {
+ this.view = view;
+ }
+
+ public void addNodeContextMenuItems(NodeView nv, JPopupMenu menu)
+ {
+
+ GraphView gv = nv.getGraphView();
+
+ if (menu == null)
+ return;
+
+ final JMenu nntkMenu = new JMenu("Nested Network Toolkit");
+
+ boolean selectedHasNested = false;
+
+ for (Object n : gv.getSelectedNodes())
+ if
(((ding.view.DNodeView)n).getNode().getNestedNetwork()!=null)
+ {
+ selectedHasNested = true;
+ break;
+ }
+
+ boolean isOverviewNetwork =
view.getNetwork().getTitle().startsWith(SubnetworkByCategoryPlugin.overviewTitle);
+
+ //ITEM1
+ if (selectedHasNested && isOverviewNetwork)
+ {
+ JMenuItem item = new JMenuItem();
+ item.addActionListener(new ActionListener()
+ {
+ public void actionPerformed(ActionEvent e) {
+ DetailedNetworkCreator.createDetailedView(view);
+ }
+ });
+ item.setText("Create Detailed View");
+
+ nntkMenu.add(item);
+ }
+
+
+ //MENU
+ if (nntkMenu.getItemCount()>0)
+ menu.add(nntkMenu);
+ }
+}
--
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.