Skip to content

Commit

Permalink
Refs Murali-group#33: Creating sub-graph instead of new graph to pass…
Browse files Browse the repository at this point in the history
… down the bypass layouts
  • Loading branch information
LHuang2019 committed Aug 18, 2017
1 parent da7afa0 commit 296f97f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import javax.swing.*;
import javax.swing.border.TitledBorder;
Expand Down Expand Up @@ -671,15 +673,42 @@ private void writeResult(ArrayList<Path> paths) {

/**
* Creates a new network and view for the subgraph
* If k <= 200, then apply hierarchical layout by calling applyHierarchicalLayout method
*/
private void createKSPSubgraphView() {

CyNetwork kspSubgraph = _model.getKspSubgraph();
TaskIterator subNetworkTask = _adapter.get_NewNetworkSelectedNodesAndEdgesTaskFactory()
.createTaskIterator(_model.getOriginalNetwork());
_adapter.getTaskManager().execute(subNetworkTask);

// creates the new network and its view
CyNetworkView kspSubgraphView = _networkViewFactory.createNetworkView(kspSubgraph);
_networkManager.addNetwork(kspSubgraph);
_networkViewManager.addNetworkView(kspSubgraphView);
//int currentSize = _networkManager.getNetworkSet().size();
//while (currentSize == _networkManager.getNetworkSet().size()) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//}

Set<CyNetwork> allnetworks = _networkManager.getNetworkSet();
long maxSUID = Integer.MIN_VALUE;
for(CyNetwork net : allnetworks) {
if(net.getSUID() > maxSUID) maxSUID = net.getSUID();
}

CyNetwork kspSubgraph = _networkManager.getNetwork(maxSUID);
String subgraphName = "PathLinker-subnetwork-" + _model.getK() + "-paths";
kspSubgraph.getRow(kspSubgraph).set(CyNetwork.NAME, subgraphName);

_applicationManager.setCurrentNetwork(kspSubgraph);
_applicationManager.setSelectedNetworks(Arrays.<CyNetwork>asList(_applicationManager.getCurrentNetwork()));
CyNetworkView kspSubgraphView = _applicationManager.getCurrentNetworkView();
_applicationManager.setSelectedNetworkViews(Arrays.<CyNetworkView>asList(kspSubgraphView));

/* CyNetworkView kspSubgraphView = _networkViewFactory.createNetworkView(kspSubgraph);
_applicationManager.setCurrentNetworkView(kspSubgraphView);
_applicationManager.setSelectedNetworkViews(Arrays.<CyNetworkView>asList(kspSubgraphView));*/

Color targetColor = new Color(255, 223, 0);

Expand All @@ -693,76 +722,68 @@ private void createKSPSubgraphView() {
View<CyNode> currView = kspSubgraphView.getNodeView(target);
currView.setLockedValue(BasicVisualLexicon.NODE_SHAPE, NodeShapeVisualProperty.RECTANGLE);
currView.setLockedValue(BasicVisualLexicon.NODE_FILL_COLOR, targetColor);

}

applyLayout(kspSubgraph, kspSubgraphView);
if (_model.getK() <= 200)
applyHierarchicalLayout(kspSubgraph, kspSubgraphView);
}

/**
* Applies a layout algorithm to the nodes If k <= 200, we apply a
* hierarchical layout Otherwise, we apply the default layout of the existing network view
* @param kspSubgraph
* @param kspSubgraphView
* Applies hierarchical layout algorithm to the nodes If k <= 200
* @param kspSubgraph the network model for the network view
* @param kspSubgraphView network view
*/
private void applyLayout(CyNetwork kspSubgraph, CyNetworkView kspSubgraphView) {
boolean hierarchical = _model.getK() <= 200;
private void applyHierarchicalLayout(CyNetwork kspSubgraph, CyNetworkView kspSubgraphView) {

// set node layout by applying the default layout algorithm
CyLayoutAlgorithm algo = hierarchical ? _adapter.getCyLayoutAlgorithmManager().getLayout("hierarchical")
: _adapter.getCyLayoutAlgorithmManager().getDefaultLayout();
// set node layout by applying the hierarchical layout algorithm
CyLayoutAlgorithm algo = _adapter.getCyLayoutAlgorithmManager().getLayout("hierarchical");
TaskIterator iter = algo.createTaskIterator(kspSubgraphView, algo.createLayoutContext(),
CyLayoutAlgorithm.ALL_NODE_VIEWS, null);
_adapter.getTaskManager().execute(iter);
SynchronousTaskManager<?> synTaskMan = _adapter.getCyServiceRegistrar()
.getService(SynchronousTaskManager.class);
synTaskMan.execute(iter);
_adapter.getVisualMappingManager().getVisualStyle(_applicationManager.getCurrentNetworkView()).apply(kspSubgraphView);
kspSubgraphView.updateView();

// if we applied the hierarchical layout, by default it is rendered
// upside down
// if we applied the hierarchical layout, by default it is rendered upside down
// so we reflect all the nodes about the x axis
if (hierarchical) {
// sleep so the hierarchical layout can get applied
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// reflect nodes about the x-axis because the default hierarchical
// layout renders the nodes upside down
// reflect nodes
double maxY = Integer.MIN_VALUE;
double minY = Integer.MAX_VALUE;
// sleep so the hierarchical layout can get applied
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// finds the midpoint x coordinate
for (CyNode node : kspSubgraph.getNodeList()) {
View<CyNode> nodeView = kspSubgraphView.getNodeView(node);
double yCoord = nodeView.getVisualProperty(BasicVisualLexicon.NODE_Y_LOCATION);
// reflect nodes about the x-axis because the default hierarchical
// layout renders the nodes upside down
// reflect nodes
double maxY = Integer.MIN_VALUE;
double minY = Integer.MAX_VALUE;

if (yCoord > maxY)
maxY = yCoord;
// finds the midpoint x coordinate
for (CyNode node : kspSubgraph.getNodeList()) {
View<CyNode> nodeView = kspSubgraphView.getNodeView(node);
double yCoord = nodeView.getVisualProperty(BasicVisualLexicon.NODE_Y_LOCATION);

if (yCoord < minY)
minY = yCoord;
}
if (yCoord > maxY)
maxY = yCoord;

double midY = (maxY + minY) / 2;
if (yCoord < minY)
minY = yCoord;
}

// reflects each node about the midpoint x axis
for (CyNode node : kspSubgraph.getNodeList()) {
View<CyNode> nodeView = kspSubgraphView.getNodeView(node);
double yCoord = nodeView.getVisualProperty(BasicVisualLexicon.NODE_Y_LOCATION);
double midY = (maxY + minY) / 2;

double newY = -1 * yCoord + 2 * midY;
nodeView.setVisualProperty(BasicVisualLexicon.NODE_Y_LOCATION, newY);
}
// reflects each node about the midpoint x axis
for (CyNode node : kspSubgraph.getNodeList()) {
View<CyNode> nodeView = kspSubgraphView.getNodeView(node);
double yCoord = nodeView.getVisualProperty(BasicVisualLexicon.NODE_Y_LOCATION);

kspSubgraphView.updateView();
double newY = -1 * yCoord + 2 * midY;
nodeView.setVisualProperty(BasicVisualLexicon.NODE_Y_LOCATION, newY);
}

kspSubgraphView.updateView();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,14 +626,7 @@ private void checkAddEdge(HashMap<String, CyEdge> sourcetargetToEdge,
* @param paths the list of paths generated by ksp algorithm
*/
private void createKSPSubgraph(ArrayList<Path> paths) {

// creates a new network in the same network collection
// as the original network
CyRootNetwork root = ((CySubNetwork) originalNetwork).getRootNetwork();

HashSet<CyNode> nodesToAdd = new HashSet<CyNode>();
HashSet<CyEdge> edgesToAdd = new HashSet<CyEdge>();


// keeps track of sources/targets in the ksp subgraph
// to change their visual properties later
subgraphSources = new HashSet<CyNode>();
Expand All @@ -644,8 +637,8 @@ private void createKSPSubgraph(ArrayList<Path> paths) {
for (int i = 1; i < currPath.size() - 2; i++) {
CyNode node1 = currPath.get(i);
CyNode node2 = currPath.get(i + 1);
nodesToAdd.add(node1);
nodesToAdd.add(node2);
originalNetwork.getRow(node1).set(CyNetwork.SELECTED, true);
originalNetwork.getRow(node2).set(CyNetwork.SELECTED, true);

// check if the nodes are part of the sources or targets specified
String node1name = originalNetwork.getRow(node1).get(CyNetwork.NAME, String.class);
Expand All @@ -660,18 +653,17 @@ private void createKSPSubgraph(ArrayList<Path> paths) {
for (CyEdge edge : edges){
// verifies the edges direction
if (edge.getSource().equals(node1) && edge.getTarget().equals(node2))
edgesToAdd.add(edge);
originalNetwork.getRow(edge).set(CyNetwork.SELECTED, true);
}
// also add all of the undirected edges from node1 to node2
edgesToAdd.addAll(originalNetwork.getConnectingEdgeList(node1, node2, CyEdge.Type.UNDIRECTED));
edges = originalNetwork.getConnectingEdgeList(node1, node2, CyEdge.Type.UNDIRECTED);
for (CyEdge edge : edges) originalNetwork.getRow(edge).set(CyNetwork.SELECTED, true);
}
}

kspSubgraph = root.addSubNetwork(nodesToAdd, edgesToAdd);

// sets the network name
String subgraphName = "PathLinker-subnetwork-" + k + "-paths";
kspSubgraph.getRow(kspSubgraph).set(CyNetwork.NAME, subgraphName);
//String subgraphName = "PathLinker-subnetwork-" + k + "-paths";
//kspSubgraph.getRow(kspSubgraph).set(CyNetwork.NAME, subgraphName);
}

/**
Expand Down

0 comments on commit 296f97f

Please sign in to comment.