CytoscapeをPHPで動かす

Pocket

perl から Cytoscape を扱えることはperlからCytoscapeを利用し、jpegに保存の通りできることがわかった
しかし、使う言語ってphpかCのどちらかなんですよね
皆さんはどうでしょう?
今回は perl で書かれていたサンプルプログラムをPHPにしてみました

ソースはこちら

<?php

        require_once("XML/RPC2/Client.php");

        $options = array(
                "prefix"        => "Cytoscape."
                );

        $client = XML_RPC2_Client::create("http://localhost:9000/Cytoscape",
                                        $options
                );

        $networkID = $client->createNetwork("PHP Test Network");

        $networkTitle = $client->getNetworkTitle((string)$networkID);

        print $networkTitle . "\n";

        $done = $client->createNodes(array("a","b","c","d","e","f","g"));

        print "Done adding nodes."."\n";
        $nrNodes = $client->countAllNodes();
        print "There are now ". $nrNodes. " nodes present in Cytoscape"."\n";

        $done = $client->addNodeAttributes(
                        "str",
                        "STRING",
                        array(
                                'a' => 'aa',
                                'b' => 'aa',
                                'c' => 'bb',
                                'd' => 'ee',
                                'e' => 'cc',
                                'f' => 'cc',
                                'g' => 'cc'
                        ),
                        true
                );

        $done = $client->addNodeAttributes(
                        "nr",
                        "FLOATING",
                        array(
                                "a"     =>      1.10,
                                "b"     =>      2.10,
                                "c"     =>      3.10,
                                "d"     =>      4.10,
                                "e"     =>      5.10,
                                "f"     =>      6.10,
                                "g"     =>      7.10
                        ),
                        true
                );

        print "Adding edges."."\n";

        $from = array("b", "b", "c", "d", "d", "d", "d");
        $to =   array("b", "c", "d", "a", "e", "f", "g");
        $type = array("relation","relation","relation","relation","relation","relation","relation");

        $directed = array(true,true,true,true,true,true,true);

        $edge_ids = $client->createEdges(
                        (string)$networkID,
                        $from,
                        $to,
                        $type,
                        $directed,
                        true
                );

        print "Done adding edges." . "\n";

        $nrEdges = $client->countAllEdges();

        print "There are now " . $nrEdges . " edges present in Cytoscape." . "\n";

        $edge_attrs = array();
        foreach( $edge_ids as $edge) {
                $edge_attrs[$edge] = (int)(rand(0,99));
        }

        /*
        $done = $client->addIntegerEdgeAttributes(
                        "integer",
                        $edge_ids,
                        array_values($edge_attrs)
                );
        */

        $done = $client->addEdgeAttributes(
                        "integer_attribute",
                        "INTEGER",
                        $edge_attrs
        );



        // Set node selection
        $done = $client->setSelectedNodes(
                        $from
                );

        // Get a list of the selected nodes
        print "calling getSelectedNodes...";
        $out = $client->getSelectedNodes();
        print_r( $out);

        // Perform force-directed Layout
        // Take a look at the possible layouts with: getLayoutNames
        $done = $client->performLayout(
                        (string)$networkID,
                        "force-directed"
                );

        // Set another attribute as the node label
        /*
        $done = $client->setNodeLabel(
                        (string)$netowrkID,
                        "str",
                        "defaultvalue",
                        "default"
                );
        */

        //Force a redraw of the current network
        $done = $client->redraw();

        //Set the shapes of the nodes based on another attribute
        $done = $client->discreteMapper(
                        (string)$networkID,
                        "default",
                        "str",
                        "Node Shape",
                        "triangle",
                        array(
                                'aa' => 'hexagon',
                                'bb' => 'ellipse',
                                'cc' => 'rect'
                        )
                );

        $done = $client->createContinuousNodeVisualStyle(
                        "nr",
                        "Node Color",
                        array(2.1, 2.51, 7.3, 8.2),
                        array('0F0FFF', '#0FFF0A', '#FF000A', '#0F000A', '#A000F0', '#AAFA0A')
                );


        $done = $client->exportView(
                        (string)$networkID,
                        "/tmp/hoge.jpg",
                        "jpg",
                        2.0
        );

        print "\n";
?>

当初、実行したら下のようなエラーが出てexportView() の所が動かなかった

PHP Fatal error:  Uncaught exception 'XML_RPC2_FaultException' with message 'No method matching arguments: java.lang.String, java.lang.String, java.lang.String, java.lang.String' in /usr/share/pear/XML/RPC2/Backend/Xmlrpcext/Client.php:126
Stack trace:
#0 [internal function]: XML_RPC2_Backend_Xmlrpcext_Client->remoteCall___('exportView', Array)
#1 /usr/share/pear/XML/RPC2/Client.php(256): call_user_func_array(Array, Array)
#2 /home/hoge/cytoscape.php(156): XML_RPC2_Client->__call('exportView', Array)
#3 /home/hoge/cytoscape.php(156): XML_RPC2_Backend_Xmlrpcext_Client->exportView('15', '/tmp/hoge.jpg', 'jpg', '2.0')
#4 {main}
  thrown in /usr/share/pear/XML/RPC2/Backend/Xmlrpcext/Client.php on line 126

exportView() の 2.0 の所を下のようにダブルクォートで囲っていた

        $done = $client->exportView(
                        (string)$networkID,
                        "/tmp/hoge.jpg",
                        "jpg",
                        "2.0"
        );

perl の方は動いてたので、perl.pl を実行してwireshark でパケット見てみた
exportView() のリクエストで 2.0 がdoubleタグで囲まれてた
phpのソースから exportView() の 2.0 についてたダブルクォート抜いたら動いたよ

コメントを残す