Skip to content

Instantly share code, notes, and snippets.

@keiono
Created November 10, 2014 00:15
Show Gist options
  • Select an option

  • Save keiono/c8b3338776e34bfd7d34 to your computer and use it in GitHub Desktop.

Select an option

Save keiono/c8b3338776e34bfd7d34 to your computer and use it in GitHub Desktop.
package edu.ucsf.rbvi.internal.myapp.tasks;
import java.awt.Color;
import java.awt.Paint;
import java.util.Arrays;
import java.util.List;
import org.cytoscape.model.CyEdge;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNetworkFactory;
import org.cytoscape.model.CyNetworkManager;
import org.cytoscape.model.CyNode;
import org.cytoscape.model.CyTable;
import org.cytoscape.view.model.CyNetworkView;
import org.cytoscape.view.model.CyNetworkViewFactory;
import org.cytoscape.view.model.CyNetworkViewManager;
import org.cytoscape.view.model.View;
import org.cytoscape.view.presentation.property.BasicVisualLexicon;
import org.cytoscape.view.presentation.property.NodeShapeVisualProperty;
import org.cytoscape.view.presentation.property.values.NodeShape;
import org.cytoscape.view.vizmap.VisualMappingFunctionFactory;
import org.cytoscape.view.vizmap.VisualMappingManager;
import org.cytoscape.view.vizmap.VisualStyle;
import org.cytoscape.view.vizmap.VisualStyleFactory;
import org.cytoscape.view.vizmap.mappings.BoundaryRangeValues;
import org.cytoscape.view.vizmap.mappings.ContinuousMapping;
import org.cytoscape.work.AbstractTask;
import org.cytoscape.work.ProvidesTitle;
import org.cytoscape.work.TaskMonitor;
import org.cytoscape.work.Tunable;
import org.cytoscape.work.util.ListSingleSelection;
public class MyAppTask extends AbstractTask {
final CyNetworkManager netManager;
final CyNetworkFactory netFactory;
final CyNetworkViewManager viewManager;
final CyNetworkViewFactory viewFactory;
final VisualMappingManager vmm;
final VisualMappingFunctionFactory cFactory;
final VisualStyleFactory vFactory;
enum ColorMapper {
BLACK(Color.BLACK, "black"), BLUE(Color.BLUE, "blue"), CYAN(Color.CYAN, "cyan"), DARK_GRAY(Color.DARK_GRAY,
"dark gray"), GRAY(Color.GRAY, "gray"), GREEN(Color.GREEN, "green"), LIGHT_GRAY(Color.LIGHT_GRAY,
"light gray"), MAGENTA(Color.MAGENTA, "magenta"), ORANGE(Color.ORANGE, "orange"), PINK(Color.PINK,
"pink"), RED(Color.RED, "red"), WHITE(Color.WHITE, "white"), YELLOW(Color.YELLOW, "yellow");
Color color;
String colorString;
ColorMapper(Color color, String colorString) {
this.color = color;
this.colorString = colorString;
}
public String toString() {
return colorString;
}
public Color getColor() {
return color;
}
}
ColorMapper colorArray[] = { ColorMapper.BLACK, ColorMapper.BLUE, ColorMapper.CYAN, ColorMapper.DARK_GRAY,
ColorMapper.GRAY, ColorMapper.GREEN, ColorMapper.LIGHT_GRAY, ColorMapper.MAGENTA, ColorMapper.ORANGE,
ColorMapper.PINK, ColorMapper.RED, ColorMapper.WHITE, ColorMapper.YELLOW };
// Ideally, this would be initiallized in our constructor
@Tunable(description = "Select the node shape", groups = { "Outer Values" })
public ListSingleSelection<NodeShape> nodeShape = new ListSingleSelection<NodeShape>(
NodeShapeVisualProperty.DIAMOND, NodeShapeVisualProperty.ELLIPSE, NodeShapeVisualProperty.HEXAGON,
NodeShapeVisualProperty.OCTAGON, NodeShapeVisualProperty.PARALLELOGRAM, NodeShapeVisualProperty.RECTANGLE,
NodeShapeVisualProperty.ROUND_RECTANGLE, NodeShapeVisualProperty.TRIANGLE);
@Tunable(description = "Select the lower color", groups = { "Outer Values", "Colors" })
public ListSingleSelection<ColorMapper> lowerColor = new ListSingleSelection<ColorMapper>(Arrays.asList(colorArray));
@Tunable(description = "Select the upper color", groups = { "Outer Values", "Colors" })
public ListSingleSelection<ColorMapper> upperColor = new ListSingleSelection<ColorMapper>(Arrays.asList(colorArray));
public MyAppTask(CyNetworkManager networkManager, CyNetworkFactory networkFactory,
CyNetworkViewManager viewManager, CyNetworkViewFactory viewFactory, VisualMappingManager vmm,
VisualStyleFactory vFactory, VisualMappingFunctionFactory cFactory) {
super();
netManager = networkManager;
netFactory = networkFactory;
this.viewManager = viewManager;
this.viewFactory = viewFactory;
this.vmm = vmm;
this.cFactory = cFactory;
this.vFactory = vFactory;
}
public void run(TaskMonitor monitor) {
CyNetwork myNetwork = netFactory.createNetwork();
CyNode node1 = myNetwork.addNode();
CyNode node2 = myNetwork.addNode();
CyEdge edge1 = myNetwork.addEdge(node1, node2, false);
myNetwork.getRow(node1).set(CyNetwork.NAME, "Node1");
myNetwork.getRow(node2).set(CyNetwork.NAME, "Node2");
myNetwork.getRow(edge1).set(CyNetwork.NAME, "Edge2");
myNetwork.getRow(myNetwork).set(CyNetwork.NAME, "NetworkName");
// Create our new columns
CyTable nodeTable = myNetwork.getDefaultNodeTable();
// CyTable nodeTable = myNetwork.getTable(CyNode.class,
// CyNetwork.DEFAULT_ATTRS);
if (nodeTable.getColumn("Hello") == null) {
nodeTable.createListColumn("Hello", String.class, false);
}
if (nodeTable.getColumn("World") == null) {
nodeTable.createColumn("World", Double.class, false);
}
// Now, add the data
myNetwork.getRow(node1).set("Hello", Arrays.asList("One", "Two", "Three"));
// nodeTable.getRow(node1.getSUID()).set("Hello",Arrays.asList("One",
// "Two", "Three"));
myNetwork.getRow(node2).set("Hello", Arrays.asList("Four", "Five", "Six"));
// nodeTable.getRow(node2.getSUID()).set("Hello",Arrays.asList("One",
// "Two", "Three"));
myNetwork.getRow(node1).set("World", 1.0);
myNetwork.getRow(node2).set("World", 2.0);
netManager.addNetwork(myNetwork);
monitor.setStatusMessage("Added network");
// Create our view
CyNetworkView view = viewFactory.createNetworkView(myNetwork);
viewManager.addNetworkView(view);
// First, just set the appropriate locked values
View<CyNode> nodeView1 = view.getNodeView(node1);
View<CyNode> nodeView2 = view.getNodeView(node2);
nodeView1.setLockedValue(BasicVisualLexicon.NODE_SHAPE, nodeShape.getSelectedValue());
nodeView2.setLockedValue(BasicVisualLexicon.NODE_SHAPE, nodeShape.getSelectedValue());
List<String> node1Hello = myNetwork.getRow(node1).getList("Hello", String.class);
List<String> node2Hello = myNetwork.getRow(node2).getList("Hello", String.class);
nodeView1.setLockedValue(BasicVisualLexicon.NODE_LABEL, node1Hello.get(0));
nodeView2.setLockedValue(BasicVisualLexicon.NODE_LABEL, node2Hello.get(0));
// Now create the visual style we're going to use
VisualStyle style = vmm.getVisualStyle(view);
VisualStyle myStyle = vFactory.createVisualStyle(style); // Make a copy
// of it
// And set up our ContinuousMapping
ContinuousMapping mapping = (ContinuousMapping) cFactory.createVisualMappingFunction("World", Double.class,
BasicVisualLexicon.NODE_FILL_COLOR);
// Define the points
Double val1 = 0d;
BoundaryRangeValues<Paint> brv1 = new BoundaryRangeValues<Paint>(Color.BLACK, lowerColor.getSelectedValue()
.getColor(), lowerColor.getSelectedValue().getColor());
Double val2 = 5d;
BoundaryRangeValues<Paint> brv2 = new BoundaryRangeValues<Paint>(upperColor.getSelectedValue().getColor(),
upperColor.getSelectedValue().getColor(), Color.BLACK);
// Set the points
mapping.addPoint(val1, brv1);
mapping.addPoint(val2, brv2);
// add the mapping to visual style
myStyle.addVisualMappingFunction(mapping);
vmm.addVisualStyle(myStyle);
vmm.setVisualStyle(myStyle, view);
}
@ProvidesTitle
public String getTitle() {
return "MyApp Settings";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment