简体   繁体   English

如何从 cytoscape.js 中的 mysql 数据库中检索节点和元素?

[英]how to retrieve nodes and elements from mysql database in cytoscape.js?

I have a table 'entrezgene' containing geneID, name which will used to create the nodes and another table 'interaction' containing geneID, geneID2 which will be used as the source and target respectively in cytoscape.js.我有一个包含geneID的表'entrezgene',用于创建节点的名称和另一个包含geneID,geneID2的表'interaction',它们将分别用作cytoscape.js中的源和目标。

I have written a php file below (only for the entrezgene table):我在下面写了一个 php 文件(仅适用于 entrezgene 表):

<?php
include 'dbconnection.php';

$sqlentrezgene = "select geneID, name from entrezgene";
$resultentrezgene = mysqli_query($connection, $sqlentrezgene) or die("Error in Selecting " . mysqli_error($connection));

$json = array();

while($row = mysqli_fetch_array ($resultentrezgene))     
{
$entrezgene = array(
    'id' => $row['geneID'],
    'name' => $row['name']
);
array_push($json, $entrezgene);
}

$jsonstring = json_encode($json);
echo $jsonstring;
?>

Cytoscape.js File: Cytoscape.js 文件:

        $('#cy').cytoscape({
  style: cytoscape.stylesheet()
    .selector('node')
      .css({
        'content': 'data(name)',
        'text-valign': 'top',
        'color': 'white',
        'text-outline-width': 1,
        'text-outline-color': '#888'
      })
    .selector('edge')
      .css({
        'target-arrow-shape': 'triangle'
      })
    .selector(':selected')
      .css({
        'background-color': 'black',
        'line-color': 'black',
        'target-arrow-color': 'black',
        'source-arrow-color': 'black'
      })
    .selector('.faded')
      .css({
        'opacity': 0,
        'text-opacity': 0.25
      }),



  elements: {
    nodes: [

    //Manually entered
      { data: {id: '1', name: 'A1BG_HUMAN'} },
      { data: {id: '10549', name: 'PRDX4_HUMAN'} },
      { data: {id: '10935', name: 'PRDX3_HUMAN'} },
      { data: {id: '1192', name: 'CLIC1_HUMAN'} },
      { data: {id: '2923', name: 'PDIA3_HUMAN'} }

    ],
    edges: [
    //Manually entered
      { data: { source: '1', target: '10549' } },
      { data: { source: '10549', target: '10935' } },
      { data: { source: '10549', target: '1192' } },
      { data: { source: '10549', target: '2923' } }
    ]
  },

  ready: function(){
    window.cy = this;

    // giddy up...

    cy.elements().unselectify();

    cy.on('tap', 'node', function(e){
      var node = e.cyTarget; 
      var neighborhood = node.neighborhood().add(node);

      cy.elements().addClass('faded');
      neighborhood.removeClass('faded');
    });

    cy.on('tap', function(e){
      if( e.cyTarget === cy ){
        cy.elements().removeClass('faded');
      }
    });
  }
});

But the problem is that the table entrezgene has 16,388 rows and the table interaction has 225,287 rows.但问题是表 entrezgene 有 16,388 行,表交互有 225,287 行。 So entering them manually will be time consuming.因此,手动输入它们将非常耗时。 Is there a solution to that?有解决办法吗?

I don't know your overall project structure, so I may not be able to give detailed instructions, but I can show you how I use cytoscape.js in one of my projects:我不知道你的整体项目结构,所以我可能无法给出详细说明,但我可以向你展示我如何在我的一个项目中使用 cytoscape.js:

This is an except of my graphview.js这是我的 graphview.js 的一个例外

Main entry point is the init function (which you may call on document ready or a click on an element, ...).主要入口点是 init 函数(您可以在文档准备好或单击元素时调用它,...)。 This performs an ajax call to your server, and in case the call is successful, it calls the function displaygraph with the received data.这将对您的服务器执行 ajax 调用,如果调用成功,它将使用接收到的数据调用函数displaygraph

var init = function() {
    //.. do some overall initialization
    $.ajax({
        url: '/url/to/getgraph.php',
        method: 'GET', 
        dataType: 'json',
        data: {},  //probably you have some parameters
        success: displaygraph, 
        error: function() {alert('error getting data');}
    });
}

The function displaygraph is then responsible for your graph initialization and display.然后函数 displaygraph 负责您的图形初始化和显示。 It receives the data from the ajax call.它从 ajax 调用接收数据。 This could be more or less your code above, just the nodes and edges are not preentered, but taken from the data received from the server.这可能或多或少是你上面的代码,只是节点和边缘不是预先输入的,而是从从服务器接收到的数据中获取的。

var displaygraph = function(data) {
    $('#cy').cytoscape({
        style: // ... 

        elements: {
            nodes: data.nodes,
            edges: data.edges
        },

        ready: function(){
            //...
        }
    });

}

And on the serverside, you have to make sure, the data which is sent back to the client contains all necessary information, ie, the nodes and edges structured as cytoscape expects it.在服务器端,您必须确保发送回客户端的数据包含所有必要的信息,即按照 cytoscape 所期望的结构节点和边。 I assume your inital code for reading the database works (I'm not a PHP guy, so can't check it), so I just added reading the interactions from the other table and created on object which contains nodes and edges.我假设您用于读取数据库的初始代码有效(我不是 PHP 专家,因此无法检查它),因此我只是添加了从另一个表读取交互并在包含节点和边的对象上创建的代码。

<?php
include 'dbconnection.php';

$sqlentrezgene = "select geneID, name from entrezgene";
$resultentrezgene = mysqli_query($connection, $sqlentrezgene) or die("Error in Selecting " . mysqli_error($connection));

$nodes= array();
while($row = mysqli_fetch_array ($resultentrezgene))     
{
    //create each entry in the array exactly as cytoscape expects it, ie with the data property
    $entrezgene = array('data' => array(
        'id' => $row['geneID'],
        'name' => $row['name']
    ));
    array_push($nodes, $entrezgene);
}

$sqlentrezinter = "select geneID1, geneID2 from interaction";
$resultentrezinter = mysqli_query($connection, $sqlentrezinter) or die("Error in Selecting " . mysqli_error($connection));

$edges= array();
while($row = mysqli_fetch_array ($resultentrezinter))     
{
    $entrezinter = array('data' => array(
        'source' => $row['geneID1'],
        'target' => $row['geneID2']
    ));
    array_push($edges, $entrezinter);
}

$json = array(
   'nodes' => $nodes,
   'edges' => $edges
);

$jsonstring = json_encode($json);
echo $jsonstring;
?>

I'm not a PHP guy, so there may be some glitches in the server part, but you should get the overall idea.我不是 PHP 专家,因此服务器部分可能存在一些小故障,但您应该了解总体思路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM