Guest User

Untitled

a guest
Jan 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.02 KB | None | 0 0
  1. package plugin;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.text.DecimalFormat;
  7. import java.text.NumberFormat;
  8.  
  9. import javax.swing.JOptionPane;
  10. import javax.swing.SwingUtilities;
  11.  
  12. import cytoscape.CyNetwork;
  13. import cytoscape.Cytoscape;
  14. import cytoscape.CytoscapeInit;
  15. import cytoscape.data.readers.GMLReader;
  16. import cytoscape.data.readers.GraphReader;
  17. import cytoscape.ding.DingNetworkView;
  18. import cytoscape.plugin.CytoscapePlugin;
  19. import cytoscape.task.Task;
  20. import cytoscape.task.TaskMonitor;
  21. import cytoscape.task.ui.JTaskConfig;
  22. import cytoscape.task.util.TaskManager;
  23. import cytoscape.util.CyFileFilter;
  24. import cytoscape.util.CyNetworkNaming;
  25. import cytoscape.util.CytoscapeAction;
  26. import cytoscape.util.FileUtil;
  27. import cytoscape.view.CyNetworkView;
  28. import cytoscape.visual.ArrowShape;
  29. import cytoscape.visual.CalculatorCatalog;
  30. import cytoscape.visual.EdgeAppearanceCalculator;
  31. import cytoscape.visual.NodeAppearanceCalculator;
  32. import cytoscape.visual.NodeShape;
  33. import cytoscape.visual.VisualMappingManager;
  34. import cytoscape.visual.VisualPropertyType;
  35. import cytoscape.visual.VisualStyle;
  36. import cytoscape.visual.calculators.BasicCalculator;
  37. import cytoscape.visual.calculators.Calculator;
  38. import cytoscape.visual.mappings.DiscreteMapping;
  39. import cytoscape.visual.mappings.ObjectMapping;
  40. import ding.view.DGraphView;
  41.  
  42. /**
  43. * A menu item "Hello World" will appear at Plugins menu. Click on the menu
  44. * item, a message dialog will show up.
  45. */
  46. public class FirstPlugin extends CytoscapePlugin {
  47.  
  48. public FirstPlugin() {
  49. LoadNetworkAction menuAction = new LoadNetworkAction(this);
  50. Cytoscape.getDesktop().getCyMenus().addCytoscapeAction((CytoscapeAction) menuAction);
  51. ((LoadNetworkAction) menuAction).perform();
  52. }
  53.  
  54. // @Override
  55. // public void activate() {
  56. // // TODO Auto-generated method stub
  57. // super.activate();
  58. // LoadNetworkAction menuAction = new LoadNetworkAction(this);
  59. // ((LoadNetworkAction) menuAction).perform();
  60. // }
  61.  
  62. class LoadNetworkAction extends CytoscapeAction {
  63.  
  64. public LoadNetworkAction(FirstPlugin myPlugin) {
  65. super("Configure Plugin");
  66. setPreferredMenu("Plugins");
  67. }
  68.  
  69. @Override
  70. public void actionPerformed(ActionEvent arg0) {
  71. // CyFileFilter intFilter = new CyFileFilter();
  72. // CyFileFilter gmlFilter = new CyFileFilter();
  73. // CyFileFilter graphFilter = new CyFileFilter();
  74.  
  75. // Add accepted File Extensions
  76. // gmlFilter.addExtension("gml");
  77. // gmlFilter.setDescription("GML files");
  78. // intFilter.addExtension("sif");
  79. // intFilter.setDescription("Interaction files");
  80. // graphFilter.addExtension("sif");
  81. // graphFilter.addExtension("gml");
  82. // graphFilter.setDescription("All network files");
  83.  
  84. // Get the file name
  85. // File file = FileUtil.getFile("Load Network File",
  86. // FileUtil.LOAD, new CyFileFilter[]
  87. // {intFilter, gmlFilter, graphFilter});
  88. File file =new File("H:\\Projects\\product_supplier.gml");
  89. if (file != null) {
  90. int fileType = Cytoscape.FILE_SIF;
  91.  
  92. // long enough to have a "gml" extension
  93. if (file.getName().length() > 4) {
  94. String name = file.getName();
  95. String extension = name.substring(name.length() - 3);
  96. if (extension.equalsIgnoreCase("gml"))
  97. fileType = Cytoscape.FILE_GML;
  98. }
  99.  
  100. // Create LoadNetwork Task
  101. // LoadNetworkTask task = new LoadNetworkTask(file, fileType);
  102. LoadNetworkTask task = new LoadNetworkTask(file, Cytoscape.FILE_GML);
  103.  
  104. // Configure JTask Dialog Pop-Up Box
  105. JTaskConfig jTaskConfig = new JTaskConfig();
  106. jTaskConfig.setOwner(Cytoscape.getDesktop());
  107. jTaskConfig.displayCloseButton(true);
  108. jTaskConfig.displayStatus(true);
  109. jTaskConfig.setAutoDispose(false);
  110.  
  111. // Execute Task in New Thread; pops open JTask Dialog Box.
  112. TaskManager.executeTask(task, jTaskConfig);
  113. }
  114. }
  115. public void perform() {
  116. File file =new File("H:\\Projects\\product_supplier.gml");
  117. if (file != null) {
  118. int fileType = Cytoscape.FILE_SIF;
  119.  
  120. // long enough to have a "gml" extension
  121. if (file.getName().length() > 4) {
  122. String name = file.getName();
  123. String extension = name.substring(name.length() - 3);
  124. if (extension.equalsIgnoreCase("gml"))
  125. fileType = Cytoscape.FILE_GML;
  126. }
  127.  
  128. // Create LoadNetwork Task
  129. // LoadNetworkTask task = new LoadNetworkTask(file, fileType);
  130. LoadNetworkTask task = new LoadNetworkTask(file, Cytoscape.FILE_GML);
  131.  
  132. // Configure JTask Dialog Pop-Up Box
  133. JTaskConfig jTaskConfig = new JTaskConfig();
  134. jTaskConfig.setOwner(Cytoscape.getDesktop());
  135. jTaskConfig.displayCloseButton(true);
  136. jTaskConfig.displayStatus(true);
  137. jTaskConfig.setAutoDispose(false);
  138.  
  139. // Execute Task in New Thread; pops open JTask Dialog Box.
  140. TaskManager.executeTask(task, jTaskConfig);
  141. }
  142. }
  143.  
  144. }
  145. }
  146. /**
  147. * Task to Load New Network Data.
  148. */
  149. class LoadNetworkTask implements Task {
  150. private File file;
  151. private int fileType;
  152. private CyNetwork cyNetwork;
  153. private TaskMonitor taskMonitor;
  154.  
  155. /**
  156. * Constructor.
  157. *
  158. * @param file File.
  159. * @param fileType FileType, e.g. Cytoscape.FILE_SIF or
  160. * Cytoscape.FILE_GML.
  161. */
  162. public LoadNetworkTask(File file, int fileType) {
  163. this.file = file;
  164. this.fileType = fileType;
  165. }
  166.  
  167. /**
  168. * Executes Task.
  169. */
  170. public void run() {
  171. taskMonitor.setStatus("Reading in Network Data...");
  172.  
  173. try {
  174. cyNetwork = this.createNetwork(file.getAbsolutePath(), fileType);
  175. setVisualStyle();
  176.  
  177. if (cyNetwork != null) {
  178. informUserOfGraphStats(cyNetwork);
  179. } else {
  180. StringBuffer sb = new StringBuffer();
  181. sb.append("Could not read network from file: " + file.getName());
  182. sb.append("\nThis file may not be a valid GML or SIF file.");
  183. taskMonitor.setException(new IOException(sb.toString()),
  184. sb.toString());
  185. }
  186. taskMonitor.setPercentCompleted(100);
  187. } catch (IOException e) {
  188. taskMonitor.setException(e, "Unable to load network file.");
  189. }
  190. }
  191.  
  192. private void setVisualStyle() {
  193. // get the network and view
  194. CyNetwork network = Cytoscape.getCurrentNetwork();
  195. CyNetworkView networkView = Cytoscape.getCurrentNetworkView();
  196.  
  197. // get the VisualMappingManager and CalculatorCatalog
  198. VisualMappingManager manager = Cytoscape.getVisualMappingManager();
  199. CalculatorCatalog catalog = manager.getCalculatorCatalog();
  200.  
  201. // check to see if a visual style with this name already exists
  202. VisualStyle vs = catalog.getVisualStyle("Sample1");
  203.  
  204. if (vs != null) {
  205. EdgeAppearanceCalculator edgeAppCalc = vs.getEdgeAppearanceCalculator();
  206. // Discrete Mapping - Set edge target arrow shape
  207. DiscreteMapping arrowMapping = new DiscreteMapping(ArrowShape.NONE,
  208. ObjectMapping.EDGE_MAPPING);
  209. arrowMapping.setControllingAttributeName("interaction", network, false);
  210. arrowMapping.putMapValue("SUPPLIES", ArrowShape.ARROW);
  211. // arrowMapping.putMapValue("pd", ArrowShape.CIRCLE);
  212. Calculator edgeArrowCalculator = new BasicCalculator("Example Edge Arrow Shape Calculator",
  213. arrowMapping, VisualPropertyType.EDGE_TGTARROW_SHAPE);
  214. edgeAppCalc.setCalculator(edgeArrowCalculator);
  215. vs.setEdgeAppearanceCalculator(edgeAppCalc);
  216. NodeAppearanceCalculator nodeAppCalc = vs.getNodeAppearanceCalculator();
  217.  
  218. DiscreteMapping disMapping = new DiscreteMapping(NodeShape.RECT,
  219. ObjectMapping.NODE_MAPPING);
  220. disMapping.setControllingAttributeName("label", network, false);
  221. disMapping.putMapValue("product1", NodeShape.HEXAGON);
  222. disMapping.putMapValue("supplier2", NodeShape.TRAPEZOID);
  223.  
  224. Calculator shapeCalculator = new BasicCalculator("Example Node Shape Calculator",
  225. disMapping,
  226. VisualPropertyType.NODE_SHAPE);
  227. nodeAppCalc.setCalculator(shapeCalculator);
  228. vs.setNodeAppearanceCalculator(nodeAppCalc);
  229. // if not, create it and add it to the catalog
  230. networkView.setVisualStyle(vs.getName());
  231.  
  232. // actually apply the visual style
  233. manager.setVisualStyle(vs);
  234. networkView.redrawGraph(true,true);
  235. }
  236.  
  237. }
  238.  
  239. /**
  240. * Inform User of Network Stats.
  241. */
  242. private void informUserOfGraphStats(CyNetwork newNetwork) {
  243. NumberFormat formatter = new DecimalFormat("#,###,###");
  244. StringBuffer sb = new StringBuffer();
  245.  
  246. // Give the user some confirmation
  247. sb.append("Succesfully loaded network from: " + file.getName());
  248. sb.append("\n\nNetwork contains " + formatter.format
  249. (newNetwork.getNodeCount()));
  250. sb.append(" nodes and " + formatter.format(newNetwork.getEdgeCount()));
  251. sb.append(" edges.\n\n");
  252.  
  253. if (newNetwork.getNodeCount() < Integer.parseInt(CytoscapeInit.getProperties().getProperty("viewThreshold"))) {
  254. sb.append("Network is under "
  255. + Integer.parseInt(CytoscapeInit.getProperties().getProperty("viewThreshold"))
  256. + " nodes. A view will be automatically created.");
  257. } else {
  258. sb.append("Network is over "
  259. + Integer.parseInt(CytoscapeInit.getProperties().getProperty("viewThreshold"))
  260. + " nodes. A view will not been created."
  261. + " If you wish to view this network, use "
  262. + "\"Create View\" from the \"Edit\" menu.");
  263. }
  264. taskMonitor.setStatus(sb.toString());
  265. }
  266.  
  267. /**
  268. * Halts the Task: Not Currently Implemented.
  269. */
  270. public void halt() {
  271. // Task can not currently be halted.
  272. }
  273.  
  274. /**
  275. * Sets the Task Monitor.
  276. *
  277. * @param taskMonitor TaskMonitor Object.
  278. */
  279. public void setTaskMonitor(TaskMonitor taskMonitor)
  280. throws IllegalThreadStateException {
  281. this.taskMonitor = taskMonitor;
  282. }
  283.  
  284. /**
  285. * Gets the Task Title.
  286. *
  287. * @return Task Title.
  288. */
  289. public String getTitle() {
  290. return new String("Loading Network");
  291. }
  292.  
  293. /**
  294. * Creates a cytoscape.data.CyNetwork from a file.
  295. * The passed variable determines the
  296. * type of file, i.e. GML, SIF, etc.<p>
  297. * This operation may take a long time to complete.
  298. *
  299. * @param location the location of the file
  300. * @param file_type the type of file GML, SIF, SBML, etc.
  301. * @param biodataserver provides the name conversion service
  302. * @param species the species used by the BioDataServer
  303. */
  304. private CyNetwork createNetwork(String location, int file_type) throws IOException {
  305.  
  306. GraphReader reader;
  307. taskMonitor.setPercentCompleted(5);
  308.  
  309. // Set the reader according to what file type was passed.
  310. // if (file_type == Cytoscape.FILE_SIF) {
  311. // reader = new InteractionsReader(biodataserver, species, location, taskMonitor);
  312. // } else
  313. if (file_type == Cytoscape.FILE_GML) {
  314. reader = new GMLReader(location, taskMonitor);
  315. } else {
  316. throw new IOException("File Type not Supported.");
  317. }
  318.  
  319. // Have the GraphReader read the given file
  320. reader.read();
  321.  
  322. // Get the RootGraph indices of the nodes and
  323. // Edges that were just created
  324. final int[] nodes = reader.getNodeIndicesArray();
  325. final int[] edges = reader.getEdgeIndicesArray();
  326.  
  327. File file = new File (location);
  328. final String title = file.getName();
  329.  
  330. // Create a new cytoscape.data.CyNetwork from these nodes and edges
  331. taskMonitor.setStatus("Creating Cytoscape Network...");
  332.  
  333. // Create the CyNetwork
  334. // First, set the view threshold to 0. By doing so, we can disable
  335. // the auto-creating of the CyNetworkView.
  336. int realThreshold = Integer.parseInt(CytoscapeInit.getProperties().getProperty("viewThreshold"));
  337. CytoscapeInit.getProperties().setProperty("viewThreshold", "0");
  338. CyNetwork network = Cytoscape.createNetwork(nodes, edges,
  339. CyNetworkNaming.getSuggestedNetworkTitle (title));
  340.  
  341. // Reset back to the real View Threshold
  342. CytoscapeInit.getProperties().setProperty("viewThreshold", String.valueOf(realThreshold));
  343.  
  344. // Store GML Data as a Network Attribute
  345. if (file_type == Cytoscape.FILE_GML) {
  346. network.putClientData("GML", reader);
  347. }
  348.  
  349. // Conditionally, Create the CyNetworkView
  350. if (network.getNodeCount() < Integer.parseInt(CytoscapeInit.getProperties().getProperty("viewThreshold")) ) {
  351. createCyNetworkView(network);
  352.  
  353. // Layout Network
  354. if (Cytoscape.getNetworkView(network.getIdentifier()) != null) {
  355. ((GMLReader) reader).layout(Cytoscape.getNetworkView(network.getIdentifier()));
  356. }
  357.  
  358. // Lastly, make the GraphView Canvas Visible.
  359. SwingUtilities.invokeLater(new Runnable () {
  360. public void run() {
  361. // PGraphView view =(PGraphView)
  362. ((DGraphView) Cytoscape.getCurrentNetworkView()).getCanvas().setVisible(true);
  363. // PCanvas pCanvas = view.getCanvas();
  364. // pCanvas.setVisible(true);
  365. }
  366. });
  367. }
  368. return network;
  369. }
  370.  
  371. /**
  372. * Creates the CyNetworkView.
  373. * Most of this code is copied directly from Cytoscape.createCyNetworkView.
  374. * However, it requires a bit of a hack to actually hide the network
  375. * view from the user, and I didn't want to use this hack in the core
  376. * Cytoscape.java class.
  377. */
  378. private void createCyNetworkView (CyNetwork cyNetwork) {
  379. final DingNetworkView view = new DingNetworkView (cyNetwork, cyNetwork.getTitle());
  380.  
  381. // Start of Hack: Hide the View
  382. // PCanvas pCanvas =
  383. view.getCanvas().setVisible(false);
  384. // pCanvas.setVisible(false);
  385. // End of Hack
  386.  
  387. view.setIdentifier(cyNetwork.getIdentifier());
  388. Cytoscape.getNetworkViewMap().put(cyNetwork.getIdentifier(), view);
  389. view.setTitle(cyNetwork.getTitle());
  390.  
  391. // if Squiggle function enabled, enable squiggling on the created view
  392. // if (Cytoscape.isSquiggleEnabled()) {
  393. // view.getSquiggleHandler().beginSquiggling();
  394. // }
  395.  
  396. // set the selection mode on the view
  397. Cytoscape.setSelectionMode(Cytoscape.getSelectionMode(), view);
  398.  
  399. Cytoscape.firePropertyChange
  400. (cytoscape.view.CytoscapeDesktop.NETWORK_VIEW_CREATED,null, view);
  401.  
  402. // Instead of calling fitContent(), access PGraphView directly.
  403. // view.getCanvas().getCamera().animateViewToCenterBounds
  404. // (view.getCanvas().getLayer().getFullBounds(), true, 0);
  405. }
  406. }
Add Comment
Please, Sign In to add comment