CytoscapeRPCPerlExample

From BioAssist
Revision as of 20:14, 4 November 2010 by Rob Hooft (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

A more elaborate example for using CytoscapeRPC with Perl.

Code Result
  1. #!/usr/bin/perl 
  2.  
  3. # An example of how to call CytoscapeRPC from Perl.
  4. # Jan Bot | 27/04/2010
  5. # Modified & cleaned 18/08/2010
  6.  
  7. use strict;
  8. use warnings;
  9. use Frontier::RPC2;
  10. use Frontier::Client;
  11.  
  12. # Tell Frontier where the server lives
  13. my $url = "http://localhost:9000/Cytoscape";
  14. my $client = Frontier::Client->new( url     => $url,
  15.                                     debug   => 0,
  16.                                     );
  17.  
  18. # We need a coder because of the 'intelligent' var casting in perl
  19. my $coder = Frontier::RPC2->new;
  20.  
  21. # Create a network to work with
  22. my $networkID = $client->call(
  23.         'Cytoscape.createNetwork',      # function name
  24.         'Perl Test Network'             # name of the network to create
  25. );
  26.  
  27. # Lets see if the name we gave the network is still the same
  28. my $networkTitle = $client->call(
  29.         'Cytoscape.getNetworkTitle',    # function name
  30.         $coder->string("$networkID")    # identifier of the network
  31. );
  32. print $networkTitle, "\n";
  33.  
  34. # Create some nodes.
  35. my $done = $client->call(
  36.         'Cytoscape.createNodes',        # function name
  37.         ["a", "b", "c", "d", "e", "f", "g"] # list of node names
  38. );
  39. print "Done adding nodes.\n";
  40. my $nrNodes = $client->call('Cytoscape.countAllNodes');
  41. print "There are now ", $nrNodes, " nodes present in Cytoscape.\n";
  42.  
  43. # Add some attributes.
  44. $done = $client->call(
  45.         'Cytoscape.addNodeAttributes',  # function name
  46.         $coder->string("str"),          # name of the attribute to add
  47.         $coder->string("STRING"),       # type of the attribute
  48.         {                               # hash of key-value pairs for the
  49.             'a' => 'aa',                # attributes
  50.             'b' => 'aa',
  51.             'c' => 'bb',
  52.             'd' => 'bb',
  53.             'e' => 'cc',
  54.             'f' => 'cc',
  55.             'g' => 'cc'
  56.         },
  57.         $coder->boolean(1)              # forgive parameter
  58. );
  59.  
  60. # Add edges
  61. print "Adding edges.\n";
  62. my @from = ("b", "b", "c", "d", "d", "d", "d");
  63. my @to =   ("b", "c", "d", "a", "e", "f", "g");
  64. my @type = ("relation") x 7;
  65. my @directed = ($coder->boolean(1)) x 7;
  66. $done = $client->call(
  67.         'Cytoscape.createEdges',        # function name
  68.         $coder->string("$networkID"),   # identifier of the network
  69.         \@from,                         # list of node names (from)
  70.         \@to,                           # list of node names (to)
  71.         \@type,                         # list of types of the edges
  72.         \@directed,                     # list of booleans
  73.         $coder->boolean(1)              # forgive parameter
  74. );
  75. print "Done adding edges.\n";
  76. my $nrEdges = $client->call("Cytoscape.countAllEdges");
  77. print "There are now " . $nrEdges . " edges present in Cytoscape.\n";
  78.  
  79. # Set node selection
  80. $done = $client->call(
  81.         "Cytoscape.setSelectedNodes",   # function name
  82.         \@from                          # nodes to select
  83. );
  84.  
  85. # Get a list of the selected nodes
  86. print "calling getSelectedNodes...";
  87. my $out = $client->call("Cytoscape.getSelectedNodes");
  88. print @$out;
  89.  
  90. # Perform force-directed Layout
  91. # Take a look at the possible layouts with: getLayoutNames
  92. $done = $client->call(
  93.         'Cytoscape.performLayout',
  94.         $coder->string("$networkID"),
  95.         "force-directed"
  96. );
  97.  
  98. # Set another attribute as the node label
  99. $done = $client->call(
  100.         "Cytoscape.setNodeLabel",       # function name
  101.         $coder->string("$networkID"),   # identifier of the network
  102.         "str",                          # name of the attribute to use
  103.         "defaultvalue",                 # default value
  104.         "default"                       # name of the vizmapper to use
  105. );
  106.  
  107. # Force a redraw of the current network
  108. $done = $client->call("Cytoscape.redraw");
  109.  
  110. # Set the shapes of the nodes based on another attribute
  111. $done = $client->call(
  112.         "Cytoscape.discreteMapper",     # function name
  113.         $coder->string("$networkID"),   # identifier of the network
  114.         "default",                      # name of the vizmap to use
  115.         "str",                          # name of the attribute to use
  116.         "Node Shape",                   # name of the vis. param. to change
  117.         "triangle",                     # default value
  118.         {                               # hash of nodeid - shape
  119.             'aa' => 'triangle',
  120.             'bb' => 'ellipse',
  121.             'cc' => 'rect'
  122.         }
  123. );
  124.  
  125. print "\n";
CytoscapeRPC perl example.png

Get the code here