Tim B.
Tim B.

Reputation: 466

How can I select edges based on data inside array object of an edge?

I am trying to select edges based on a passed in string. Here is a sample json:

{
"group": "edges",
"data": {
  "id": "8",
  "source": "Q14814",
  "target": "P20393",
  "direction": "|->",
  "Sources": {
    "dataSource": "database",
    "dbId": "0",
    "sourceId": "1368140",
    "sourceType": "REACTION"
  }
}

}

Each edge may have a single source, or an array of sources. I want to select all edges that have a sourceId of the string I pass in. So, if I pass in a string of "1368140" I want to get all edges that contain a source with a sourceId of "1368140".

I have tried a few different ways to select this including:

cy.filter('edge[reactomeId = "' + string + '"]'));

or

cy.elements('edge[reactomeId = "' + string + '"]'));

or

cy.filter('reactomeId = ' + string + ''));

and seemingly all other permutations of this. Does anyone have any idea how I can select these edges correctly?

Upvotes: 1

Views: 369

Answers (1)

Stephan T.
Stephan T.

Reputation: 6074

This is my way of doing this:

// The syntax is important here!
cy.edges('[source = "' + string + '"]');

The source of this particular syntax can be found here.

Live example (output in console):

var cy = (window.cy = cytoscape({
  container: document.getElementById("cy"),

  boxSelectionEnabled: false,
  autounselectify: true,

  style: [{
      selector: "node",
      css: {
        content: "data(id)",
        "text-valign": "center",
        "text-halign": "center",
        height: "60px",
        width: "100px",
        shape: "rectangle",
        "background-color": "data(faveColor)"
      }
    },
    {
      selector: "edge",
      css: {
        "curve-style": "bezier",
        "control-point-step-size": 40,
        "target-arrow-shape": "triangle"
      }
    }
  ],

  elements: {
    nodes: [{
        data: {
          id: "Top",
          faveColor: "#2763c4"
        }
      },
      {
        data: {
          id: "yes",
          faveColor: "#37a32d"
        }
      },
      {
        data: {
          id: "no",
          faveColor: "#2763c4"
        }
      },
      {
        data: {
          id: "Third",
          faveColor: "#2763c4"
        }
      },
      {
        data: {
          id: "Fourth",
          faveColor: "#56a9f7"
        }
      }
    ],
    edges: [{
        data: {
          source: "Top",
          target: "yes"
        }
      },
      {
        data: {
          source: "Top",
          target: "no"
        }
      },
      {
        data: {
          source: "no",
          target: "Third"
        }
      },
      {
        data: {
          source: "Third",
          target: "Fourth"
        }
      },
      {
        data: {
          source: "Fourth",
          target: "Third"
        }
      }
    ]
  },
  layout: {
    name: "dagre"
  }
}));

cy.bind('click', 'node', function(event) {
  console.log(cy.edges('[source = "' + event.target.id() + '"]'));
});
body {
  font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#cy {
  height: 85%;
  width: 100%;
  float: right;
  position: absolute;
}
<html>

<head>
  <meta charset=utf-8 />
  <meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
  <script src="https://unpkg.com/[email protected]/dist/cytoscape.min.js">
  </script>
  <!-- cyposcape dagre -->
  <script src="https://unpkg.com/[email protected]/dist/dagre.js"></script>
  <script src="https://cdn.rawgit.com/cytoscape/cytoscape.js-dagre/1.5.0/cytoscape-dagre.js"></script>
</head>

<body>
  <div id="cy"></div>
</body>

</html>

Upvotes: 3

Related Questions