Guest User

Untitled

a guest
Jan 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.16 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 ding.view.DGraphView;
  28.  
  29. /**
  30. * A menu item "Hello World" will appear at Plugins menu. Click on the menu
  31. * item, a message dialog will show up.
  32. */
  33. public class FirstPlugin extends CytoscapePlugin {
  34.  
  35. public FirstPlugin() {
  36. LoadNetworkAction menuAction = new LoadNetworkAction(this);
  37. Cytoscape.getDesktop().getCyMenus().addCytoscapeAction((CytoscapeAction) menuAction);
  38. ((LoadNetworkAction) menuAction).perform();
  39. }
  40.  
  41. class LoadNetworkAction extends CytoscapeAction {
  42.  
  43. public LoadNetworkAction(FirstPlugin myPlugin) {
  44. super("Configure Plugin");
  45. setPreferredMenu("Plugins");
  46. }
  47.  
  48. @Override
  49. public void actionPerformed(ActionEvent arg0) {
  50. // CyFileFilter intFilter = new CyFileFilter();
  51. // CyFileFilter gmlFilter = new CyFileFilter();
  52. // CyFileFilter graphFilter = new CyFileFilter();
  53.  
  54. // Add accepted File Extensions
  55. // gmlFilter.addExtension("gml");
  56. // gmlFilter.setDescription("GML files");
  57. // intFilter.addExtension("sif");
  58. // intFilter.setDescription("Interaction files");
  59. // graphFilter.addExtension("sif");
  60. // graphFilter.addExtension("gml");
  61. // graphFilter.setDescription("All network files");
  62.  
  63. // Get the file name
  64. // File file = FileUtil.getFile("Load Network File",
  65. // FileUtil.LOAD, new CyFileFilter[]
  66. // {intFilter, gmlFilter, graphFilter});
  67. File file =new File("H:\\Projects\\product_supplier.gml");
  68. if (file != null) {
  69. int fileType = Cytoscape.FILE_SIF;
  70.  
  71. // long enough to have a "gml" extension
  72. if (file.getName().length() > 4) {
  73. String name = file.getName();
  74. String extension = name.substring(name.length() - 3);
  75. if (extension.equalsIgnoreCase("gml"))
  76. fileType = Cytoscape.FILE_GML;
  77. }
  78.  
  79. // Create LoadNetwork Task
  80. // LoadNetworkTask task = new LoadNetworkTask(file, fileType);
  81. LoadNetworkTask task = new LoadNetworkTask(file, Cytoscape.FILE_GML);
  82.  
  83. // Configure JTask Dialog Pop-Up Box
  84. JTaskConfig jTaskConfig = new JTaskConfig();
  85. jTaskConfig.setOwner(Cytoscape.getDesktop());
  86. jTaskConfig.displayCloseButton(true);
  87. jTaskConfig.displayStatus(true);
  88. jTaskConfig.setAutoDispose(false);
  89.  
  90. // Execute Task in New Thread; pops open JTask Dialog Box.
  91. TaskManager.executeTask(task, jTaskConfig);
  92. }
  93. }
  94. public void perform() {
  95. File file =new File("H:\\Projects\\product_supplier.gml");
  96. if (file != null) {
  97. int fileType = Cytoscape.FILE_SIF;
  98.  
  99. // long enough to have a "gml" extension
  100. if (file.getName().length() > 4) {
  101. String name = file.getName();
  102. String extension = name.substring(name.length() - 3);
  103. if (extension.equalsIgnoreCase("gml"))
  104. fileType = Cytoscape.FILE_GML;
  105. }
  106.  
  107. // Create LoadNetwork Task
  108. // LoadNetworkTask task = new LoadNetworkTask(file, fileType);
  109. LoadNetworkTask task = new LoadNetworkTask(file, Cytoscape.FILE_GML);
  110.  
  111. // Configure JTask Dialog Pop-Up Box
  112. JTaskConfig jTaskConfig = new JTaskConfig();
  113. jTaskConfig.setOwner(Cytoscape.getDesktop());
  114. jTaskConfig.displayCloseButton(true);
  115. jTaskConfig.displayStatus(true);
  116. jTaskConfig.setAutoDispose(false);
  117.  
  118. // Execute Task in New Thread; pops open JTask Dialog Box.
  119. TaskManager.executeTask(task, jTaskConfig);
  120. }
  121. }
  122.  
  123. }
  124. }
  125. /**
  126. * Task to Load New Network Data.
  127. */
  128. class LoadNetworkTask implements Task {
  129. private File file;
  130. private int fileType;
  131. private CyNetwork cyNetwork;
  132. private TaskMonitor taskMonitor;
  133.  
  134. /**
  135. * Constructor.
  136. *
  137. * @param file File.
  138. * @param fileType FileType, e.g. Cytoscape.FILE_SIF or
  139. * Cytoscape.FILE_GML.
  140. */
  141. public LoadNetworkTask(File file, int fileType) {
  142. this.file = file;
  143. this.fileType = fileType;
  144. }
  145.  
  146. /**
  147. * Executes Task.
  148. */
  149. public void run() {
  150. taskMonitor.setStatus("Reading in Network Data...");
  151.  
  152. try {
  153. cyNetwork = this.createNetwork(file.getAbsolutePath(), fileType);
  154.  
  155. if (cyNetwork != null) {
  156. informUserOfGraphStats(cyNetwork);
  157. } else {
  158. StringBuffer sb = new StringBuffer();
  159. sb.append("Could not read network from file: " + file.getName());
  160. sb.append("\nThis file may not be a valid GML or SIF file.");
  161. taskMonitor.setException(new IOException(sb.toString()),
  162. sb.toString());
  163. }
  164. taskMonitor.setPercentCompleted(100);
  165. } catch (IOException e) {
  166. taskMonitor.setException(e, "Unable to load network file.");
  167. }
  168. }
  169.  
  170. /**
  171. * Inform User of Network Stats.
  172. */
  173. private void informUserOfGraphStats(CyNetwork newNetwork) {
  174. NumberFormat formatter = new DecimalFormat("#,###,###");
  175. StringBuffer sb = new StringBuffer();
  176.  
  177. // Give the user some confirmation
  178. sb.append("Succesfully loaded network from: " + file.getName());
  179. sb.append("\n\nNetwork contains " + formatter.format
  180. (newNetwork.getNodeCount()));
  181. sb.append(" nodes and " + formatter.format(newNetwork.getEdgeCount()));
  182. sb.append(" edges.\n\n");
  183.  
  184. if (newNetwork.getNodeCount() < Integer.parseInt(CytoscapeInit.getProperties().getProperty("viewThreshold"))) {
  185. sb.append("Network is under "
  186. + Integer.parseInt(CytoscapeInit.getProperties().getProperty("viewThreshold"))
  187. + " nodes. A view will be automatically created.");
  188. } else {
  189. sb.append("Network is over "
  190. + Integer.parseInt(CytoscapeInit.getProperties().getProperty("viewThreshold"))
  191. + " nodes. A view will not been created."
  192. + " If you wish to view this network, use "
  193. + "\"Create View\" from the \"Edit\" menu.");
  194. }
  195. taskMonitor.setStatus(sb.toString());
  196. }
  197.  
  198. /**
  199. * Halts the Task: Not Currently Implemented.
  200. */
  201. public void halt() {
  202. // Task can not currently be halted.
  203. }
  204.  
  205. /**
  206. * Sets the Task Monitor.
  207. *
  208. * @param taskMonitor TaskMonitor Object.
  209. */
  210. public void setTaskMonitor(TaskMonitor taskMonitor)
  211. throws IllegalThreadStateException {
  212. this.taskMonitor = taskMonitor;
  213. }
  214.  
  215. /**
  216. * Gets the Task Title.
  217. *
  218. * @return Task Title.
  219. */
  220. public String getTitle() {
  221. return new String("Loading Network");
  222. }
  223.  
  224. /**
  225. * Creates a cytoscape.data.CyNetwork from a file.
  226. * The passed variable determines the
  227. * type of file, i.e. GML, SIF, etc.<p>
  228. * This operation may take a long time to complete.
  229. *
  230. * @param location the location of the file
  231. * @param file_type the type of file GML, SIF, SBML, etc.
  232. * @param biodataserver provides the name conversion service
  233. * @param species the species used by the BioDataServer
  234. */
  235. private CyNetwork createNetwork(String location, int file_type) throws IOException {
  236.  
  237. GraphReader reader;
  238. taskMonitor.setPercentCompleted(5);
  239.  
  240. // Set the reader according to what file type was passed.
  241. // if (file_type == Cytoscape.FILE_SIF) {
  242. // reader = new InteractionsReader(biodataserver, species, location, taskMonitor);
  243. // } else
  244. if (file_type == Cytoscape.FILE_GML) {
  245. reader = new GMLReader(location, taskMonitor);
  246. } else {
  247. throw new IOException("File Type not Supported.");
  248. }
  249.  
  250. // Have the GraphReader read the given file
  251. reader.read();
  252.  
  253. // Get the RootGraph indices of the nodes and
  254. // Edges that were just created
  255. final int[] nodes = reader.getNodeIndicesArray();
  256. final int[] edges = reader.getEdgeIndicesArray();
  257.  
  258. File file = new File (location);
  259. final String title = file.getName();
  260.  
  261. // Create a new cytoscape.data.CyNetwork from these nodes and edges
  262. taskMonitor.setStatus("Creating Cytoscape Network...");
  263.  
  264. // Create the CyNetwork
  265. // First, set the view threshold to 0. By doing so, we can disable
  266. // the auto-creating of the CyNetworkView.
  267. int realThreshold = Integer.parseInt(CytoscapeInit.getProperties().getProperty("viewThreshold"));
  268. CytoscapeInit.getProperties().setProperty("viewThreshold", "0");
  269. CyNetwork network = Cytoscape.createNetwork(nodes, edges,
  270. CyNetworkNaming.getSuggestedNetworkTitle (title));
  271.  
  272. // Reset back to the real View Threshold
  273. CytoscapeInit.getProperties().setProperty("viewThreshold", String.valueOf(realThreshold));
  274.  
  275. // Store GML Data as a Network Attribute
  276. if (file_type == Cytoscape.FILE_GML) {
  277. network.putClientData("GML", reader);
  278. }
  279.  
  280. // Conditionally, Create the CyNetworkView
  281. if (network.getNodeCount() < Integer.parseInt(CytoscapeInit.getProperties().getProperty("viewThreshold")) ) {
  282. createCyNetworkView(network);
  283.  
  284. // Layout Network
  285. if (Cytoscape.getNetworkView(network.getIdentifier()) != null) {
  286. ((GMLReader) reader).layout(Cytoscape.getNetworkView(network.getIdentifier()));
  287. }
  288.  
  289. // Lastly, make the GraphView Canvas Visible.
  290. SwingUtilities.invokeLater(new Runnable () {
  291. public void run() {
  292. // PGraphView view =(PGraphView)
  293. ((DGraphView) Cytoscape.getCurrentNetworkView()).getCanvas().setVisible(true);
  294. // PCanvas pCanvas = view.getCanvas();
  295. // pCanvas.setVisible(true);
  296. }
  297. });
  298. }
  299. return network;
  300. }
  301.  
  302. /**
  303. * Creates the CyNetworkView.
  304. * Most of this code is copied directly from Cytoscape.createCyNetworkView.
  305. * However, it requires a bit of a hack to actually hide the network
  306. * view from the user, and I didn't want to use this hack in the core
  307. * Cytoscape.java class.
  308. */
  309. private void createCyNetworkView (CyNetwork cyNetwork) {
  310. final DingNetworkView view = new DingNetworkView (cyNetwork, cyNetwork.getTitle());
  311.  
  312. // Start of Hack: Hide the View
  313. // PCanvas pCanvas =
  314. view.getCanvas().setVisible(false);
  315. // pCanvas.setVisible(false);
  316. // End of Hack
  317.  
  318. view.setIdentifier(cyNetwork.getIdentifier());
  319. Cytoscape.getNetworkViewMap().put(cyNetwork.getIdentifier(), view);
  320. view.setTitle(cyNetwork.getTitle());
  321.  
  322. // if Squiggle function enabled, enable squiggling on the created view
  323. // if (Cytoscape.isSquiggleEnabled()) {
  324. // view.getSquiggleHandler().beginSquiggling();
  325. // }
  326.  
  327. // set the selection mode on the view
  328. Cytoscape.setSelectionMode(Cytoscape.getSelectionMode(), view);
  329.  
  330. Cytoscape.firePropertyChange
  331. (cytoscape.view.CytoscapeDesktop.NETWORK_VIEW_CREATED,null, view);
  332.  
  333. // Instead of calling fitContent(), access PGraphView directly.
  334. // view.getCanvas().getCamera().animateViewToCenterBounds
  335. // (view.getCanvas().getLayer().getFullBounds(), true, 0);
  336. }
  337. }
Add Comment
Please, Sign In to add comment