Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package plugin;
- import java.awt.event.ActionEvent;
- import java.io.File;
- import java.io.IOException;
- import java.text.DecimalFormat;
- import java.text.NumberFormat;
- import javax.swing.JOptionPane;
- import javax.swing.SwingUtilities;
- import cytoscape.CyNetwork;
- import cytoscape.Cytoscape;
- import cytoscape.CytoscapeInit;
- import cytoscape.data.readers.GMLReader;
- import cytoscape.data.readers.GraphReader;
- import cytoscape.ding.DingNetworkView;
- import cytoscape.plugin.CytoscapePlugin;
- import cytoscape.task.Task;
- import cytoscape.task.TaskMonitor;
- import cytoscape.task.ui.JTaskConfig;
- import cytoscape.task.util.TaskManager;
- import cytoscape.util.CyFileFilter;
- import cytoscape.util.CyNetworkNaming;
- import cytoscape.util.CytoscapeAction;
- import cytoscape.util.FileUtil;
- import cytoscape.view.CyNetworkView;
- import cytoscape.visual.ArrowShape;
- import cytoscape.visual.CalculatorCatalog;
- import cytoscape.visual.EdgeAppearanceCalculator;
- import cytoscape.visual.NodeAppearanceCalculator;
- import cytoscape.visual.NodeShape;
- import cytoscape.visual.VisualMappingManager;
- import cytoscape.visual.VisualPropertyType;
- import cytoscape.visual.VisualStyle;
- import cytoscape.visual.calculators.BasicCalculator;
- import cytoscape.visual.calculators.Calculator;
- import cytoscape.visual.mappings.DiscreteMapping;
- import cytoscape.visual.mappings.ObjectMapping;
- import ding.view.DGraphView;
- /**
- * A menu item "Hello World" will appear at Plugins menu. Click on the menu
- * item, a message dialog will show up.
- */
- public class FirstPlugin extends CytoscapePlugin {
- public FirstPlugin() {
- LoadNetworkAction menuAction = new LoadNetworkAction(this);
- Cytoscape.getDesktop().getCyMenus().addCytoscapeAction((CytoscapeAction) menuAction);
- ((LoadNetworkAction) menuAction).perform();
- }
- // @Override
- // public void activate() {
- // // TODO Auto-generated method stub
- // super.activate();
- // LoadNetworkAction menuAction = new LoadNetworkAction(this);
- // ((LoadNetworkAction) menuAction).perform();
- // }
- class LoadNetworkAction extends CytoscapeAction {
- public LoadNetworkAction(FirstPlugin myPlugin) {
- super("Configure Plugin");
- setPreferredMenu("Plugins");
- }
- @Override
- public void actionPerformed(ActionEvent arg0) {
- // CyFileFilter intFilter = new CyFileFilter();
- // CyFileFilter gmlFilter = new CyFileFilter();
- // CyFileFilter graphFilter = new CyFileFilter();
- // Add accepted File Extensions
- // gmlFilter.addExtension("gml");
- // gmlFilter.setDescription("GML files");
- // intFilter.addExtension("sif");
- // intFilter.setDescription("Interaction files");
- // graphFilter.addExtension("sif");
- // graphFilter.addExtension("gml");
- // graphFilter.setDescription("All network files");
- // Get the file name
- // File file = FileUtil.getFile("Load Network File",
- // FileUtil.LOAD, new CyFileFilter[]
- // {intFilter, gmlFilter, graphFilter});
- File file =new File("H:\\Projects\\product_supplier.gml");
- if (file != null) {
- int fileType = Cytoscape.FILE_SIF;
- // long enough to have a "gml" extension
- if (file.getName().length() > 4) {
- String name = file.getName();
- String extension = name.substring(name.length() - 3);
- if (extension.equalsIgnoreCase("gml"))
- fileType = Cytoscape.FILE_GML;
- }
- // Create LoadNetwork Task
- // LoadNetworkTask task = new LoadNetworkTask(file, fileType);
- LoadNetworkTask task = new LoadNetworkTask(file, Cytoscape.FILE_GML);
- // Configure JTask Dialog Pop-Up Box
- JTaskConfig jTaskConfig = new JTaskConfig();
- jTaskConfig.setOwner(Cytoscape.getDesktop());
- jTaskConfig.displayCloseButton(true);
- jTaskConfig.displayStatus(true);
- jTaskConfig.setAutoDispose(false);
- // Execute Task in New Thread; pops open JTask Dialog Box.
- TaskManager.executeTask(task, jTaskConfig);
- }
- }
- public void perform() {
- File file =new File("H:\\Projects\\product_supplier.gml");
- if (file != null) {
- int fileType = Cytoscape.FILE_SIF;
- // long enough to have a "gml" extension
- if (file.getName().length() > 4) {
- String name = file.getName();
- String extension = name.substring(name.length() - 3);
- if (extension.equalsIgnoreCase("gml"))
- fileType = Cytoscape.FILE_GML;
- }
- // Create LoadNetwork Task
- // LoadNetworkTask task = new LoadNetworkTask(file, fileType);
- LoadNetworkTask task = new LoadNetworkTask(file, Cytoscape.FILE_GML);
- // Configure JTask Dialog Pop-Up Box
- JTaskConfig jTaskConfig = new JTaskConfig();
- jTaskConfig.setOwner(Cytoscape.getDesktop());
- jTaskConfig.displayCloseButton(true);
- jTaskConfig.displayStatus(true);
- jTaskConfig.setAutoDispose(false);
- // Execute Task in New Thread; pops open JTask Dialog Box.
- TaskManager.executeTask(task, jTaskConfig);
- }
- }
- }
- }
- /**
- * Task to Load New Network Data.
- */
- class LoadNetworkTask implements Task {
- private File file;
- private int fileType;
- private CyNetwork cyNetwork;
- private TaskMonitor taskMonitor;
- /**
- * Constructor.
- *
- * @param file File.
- * @param fileType FileType, e.g. Cytoscape.FILE_SIF or
- * Cytoscape.FILE_GML.
- */
- public LoadNetworkTask(File file, int fileType) {
- this.file = file;
- this.fileType = fileType;
- }
- /**
- * Executes Task.
- */
- public void run() {
- taskMonitor.setStatus("Reading in Network Data...");
- try {
- cyNetwork = this.createNetwork(file.getAbsolutePath(), fileType);
- setVisualStyle();
- if (cyNetwork != null) {
- informUserOfGraphStats(cyNetwork);
- } else {
- StringBuffer sb = new StringBuffer();
- sb.append("Could not read network from file: " + file.getName());
- sb.append("\nThis file may not be a valid GML or SIF file.");
- taskMonitor.setException(new IOException(sb.toString()),
- sb.toString());
- }
- taskMonitor.setPercentCompleted(100);
- } catch (IOException e) {
- taskMonitor.setException(e, "Unable to load network file.");
- }
- }
- private void setVisualStyle() {
- // get the network and view
- CyNetwork network = Cytoscape.getCurrentNetwork();
- CyNetworkView networkView = Cytoscape.getCurrentNetworkView();
- // get the VisualMappingManager and CalculatorCatalog
- VisualMappingManager manager = Cytoscape.getVisualMappingManager();
- CalculatorCatalog catalog = manager.getCalculatorCatalog();
- // check to see if a visual style with this name already exists
- VisualStyle vs = catalog.getVisualStyle("Sample1");
- if (vs != null) {
- EdgeAppearanceCalculator edgeAppCalc = vs.getEdgeAppearanceCalculator();
- // Discrete Mapping - Set edge target arrow shape
- DiscreteMapping arrowMapping = new DiscreteMapping(ArrowShape.NONE,
- ObjectMapping.EDGE_MAPPING);
- arrowMapping.setControllingAttributeName("interaction", network, false);
- arrowMapping.putMapValue("SUPPLIES", ArrowShape.ARROW);
- // arrowMapping.putMapValue("pd", ArrowShape.CIRCLE);
- Calculator edgeArrowCalculator = new BasicCalculator("Example Edge Arrow Shape Calculator",
- arrowMapping, VisualPropertyType.EDGE_TGTARROW_SHAPE);
- edgeAppCalc.setCalculator(edgeArrowCalculator);
- vs.setEdgeAppearanceCalculator(edgeAppCalc);
- NodeAppearanceCalculator nodeAppCalc = vs.getNodeAppearanceCalculator();
- DiscreteMapping disMapping = new DiscreteMapping(NodeShape.RECT,
- ObjectMapping.NODE_MAPPING);
- disMapping.setControllingAttributeName("label", network, false);
- disMapping.putMapValue("product1", NodeShape.HEXAGON);
- disMapping.putMapValue("supplier2", NodeShape.TRAPEZOID);
- Calculator shapeCalculator = new BasicCalculator("Example Node Shape Calculator",
- disMapping,
- VisualPropertyType.NODE_SHAPE);
- nodeAppCalc.setCalculator(shapeCalculator);
- vs.setNodeAppearanceCalculator(nodeAppCalc);
- // if not, create it and add it to the catalog
- networkView.setVisualStyle(vs.getName());
- // actually apply the visual style
- manager.setVisualStyle(vs);
- networkView.redrawGraph(true,true);
- }
- }
- /**
- * Inform User of Network Stats.
- */
- private void informUserOfGraphStats(CyNetwork newNetwork) {
- NumberFormat formatter = new DecimalFormat("#,###,###");
- StringBuffer sb = new StringBuffer();
- // Give the user some confirmation
- sb.append("Succesfully loaded network from: " + file.getName());
- sb.append("\n\nNetwork contains " + formatter.format
- (newNetwork.getNodeCount()));
- sb.append(" nodes and " + formatter.format(newNetwork.getEdgeCount()));
- sb.append(" edges.\n\n");
- if (newNetwork.getNodeCount() < Integer.parseInt(CytoscapeInit.getProperties().getProperty("viewThreshold"))) {
- sb.append("Network is under "
- + Integer.parseInt(CytoscapeInit.getProperties().getProperty("viewThreshold"))
- + " nodes. A view will be automatically created.");
- } else {
- sb.append("Network is over "
- + Integer.parseInt(CytoscapeInit.getProperties().getProperty("viewThreshold"))
- + " nodes. A view will not been created."
- + " If you wish to view this network, use "
- + "\"Create View\" from the \"Edit\" menu.");
- }
- taskMonitor.setStatus(sb.toString());
- }
- /**
- * Halts the Task: Not Currently Implemented.
- */
- public void halt() {
- // Task can not currently be halted.
- }
- /**
- * Sets the Task Monitor.
- *
- * @param taskMonitor TaskMonitor Object.
- */
- public void setTaskMonitor(TaskMonitor taskMonitor)
- throws IllegalThreadStateException {
- this.taskMonitor = taskMonitor;
- }
- /**
- * Gets the Task Title.
- *
- * @return Task Title.
- */
- public String getTitle() {
- return new String("Loading Network");
- }
- /**
- * Creates a cytoscape.data.CyNetwork from a file.
- * The passed variable determines the
- * type of file, i.e. GML, SIF, etc.<p>
- * This operation may take a long time to complete.
- *
- * @param location the location of the file
- * @param file_type the type of file GML, SIF, SBML, etc.
- * @param biodataserver provides the name conversion service
- * @param species the species used by the BioDataServer
- */
- private CyNetwork createNetwork(String location, int file_type) throws IOException {
- GraphReader reader;
- taskMonitor.setPercentCompleted(5);
- // Set the reader according to what file type was passed.
- // if (file_type == Cytoscape.FILE_SIF) {
- // reader = new InteractionsReader(biodataserver, species, location, taskMonitor);
- // } else
- if (file_type == Cytoscape.FILE_GML) {
- reader = new GMLReader(location, taskMonitor);
- } else {
- throw new IOException("File Type not Supported.");
- }
- // Have the GraphReader read the given file
- reader.read();
- // Get the RootGraph indices of the nodes and
- // Edges that were just created
- final int[] nodes = reader.getNodeIndicesArray();
- final int[] edges = reader.getEdgeIndicesArray();
- File file = new File (location);
- final String title = file.getName();
- // Create a new cytoscape.data.CyNetwork from these nodes and edges
- taskMonitor.setStatus("Creating Cytoscape Network...");
- // Create the CyNetwork
- // First, set the view threshold to 0. By doing so, we can disable
- // the auto-creating of the CyNetworkView.
- int realThreshold = Integer.parseInt(CytoscapeInit.getProperties().getProperty("viewThreshold"));
- CytoscapeInit.getProperties().setProperty("viewThreshold", "0");
- CyNetwork network = Cytoscape.createNetwork(nodes, edges,
- CyNetworkNaming.getSuggestedNetworkTitle (title));
- // Reset back to the real View Threshold
- CytoscapeInit.getProperties().setProperty("viewThreshold", String.valueOf(realThreshold));
- // Store GML Data as a Network Attribute
- if (file_type == Cytoscape.FILE_GML) {
- network.putClientData("GML", reader);
- }
- // Conditionally, Create the CyNetworkView
- if (network.getNodeCount() < Integer.parseInt(CytoscapeInit.getProperties().getProperty("viewThreshold")) ) {
- createCyNetworkView(network);
- // Layout Network
- if (Cytoscape.getNetworkView(network.getIdentifier()) != null) {
- ((GMLReader) reader).layout(Cytoscape.getNetworkView(network.getIdentifier()));
- }
- // Lastly, make the GraphView Canvas Visible.
- SwingUtilities.invokeLater(new Runnable () {
- public void run() {
- // PGraphView view =(PGraphView)
- ((DGraphView) Cytoscape.getCurrentNetworkView()).getCanvas().setVisible(true);
- // PCanvas pCanvas = view.getCanvas();
- // pCanvas.setVisible(true);
- }
- });
- }
- return network;
- }
- /**
- * Creates the CyNetworkView.
- * Most of this code is copied directly from Cytoscape.createCyNetworkView.
- * However, it requires a bit of a hack to actually hide the network
- * view from the user, and I didn't want to use this hack in the core
- * Cytoscape.java class.
- */
- private void createCyNetworkView (CyNetwork cyNetwork) {
- final DingNetworkView view = new DingNetworkView (cyNetwork, cyNetwork.getTitle());
- // Start of Hack: Hide the View
- // PCanvas pCanvas =
- view.getCanvas().setVisible(false);
- // pCanvas.setVisible(false);
- // End of Hack
- view.setIdentifier(cyNetwork.getIdentifier());
- Cytoscape.getNetworkViewMap().put(cyNetwork.getIdentifier(), view);
- view.setTitle(cyNetwork.getTitle());
- // if Squiggle function enabled, enable squiggling on the created view
- // if (Cytoscape.isSquiggleEnabled()) {
- // view.getSquiggleHandler().beginSquiggling();
- // }
- // set the selection mode on the view
- Cytoscape.setSelectionMode(Cytoscape.getSelectionMode(), view);
- Cytoscape.firePropertyChange
- (cytoscape.view.CytoscapeDesktop.NETWORK_VIEW_CREATED,null, view);
- // Instead of calling fitContent(), access PGraphView directly.
- // view.getCanvas().getCamera().animateViewToCenterBounds
- // (view.getCanvas().getLayer().getFullBounds(), true, 0);
- }
- }
Add Comment
Please, Sign In to add comment