Query

Query builder for PyBEL.

exception pybel.struct.query.QueryMissingNetworksError[source]

Raised if a query is created from json but doesn’t have a listing of network identifiers.

exception pybel.struct.query.NodeDegreeIterError[source]

Raised when failing to iterate over node degrees.

class pybel.struct.query.Query(network_ids=None, seeding=None, pipeline=None)[source]

Represents a query over a network store.

Build a query.

Parameters

network_ids (Union[None, int, Iterable[int]]) – Database network identifiers identifiers

append_network(network_id)[source]

Add a network to this query.

Parameters

network_id (int) – The database identifier of the network

Return type

Query

Returns

self for fluid API

append_seeding_induction(nodes)[source]

Add a seed induction method.

Return type

Seeding

Returns

seeding container for fluid API

append_seeding_neighbors(nodes)[source]

Add a seed by neighbors.

Return type

Seeding

Returns

seeding container for fluid API

append_seeding_annotation(annotation, values)[source]

Add a seed induction method for single annotation’s values.

Parameters
  • annotation (str) – The annotation to filter by

  • values (Set[str]) – The values of the annotation to keep

Return type

Seeding

append_seeding_sample(**kwargs)[source]

Add seed induction methods.

Kwargs can have number_edges or number_seed_nodes.

Return type

Seeding

append_pipeline(name, *args, **kwargs)[source]

Add an entry to the pipeline. Defers to pybel_tools.pipeline.Pipeline.append().

Parameters

name (str or types.FunctionType) – The name of the function

Return type

Pipeline

Returns

This pipeline for fluid query building

run(manager)[source]

Run this query and returns the resulting BEL graph.

Parameters

manager – A cache manager

Return type

Optional[pybel.BELGraph]

to_json()[source]

Return this query as a JSON object.

Return type

Dict

dump(file, **kwargs)[source]

Dump this query to a file as JSON.

Return type

None

dumps(**kwargs)[source]

Dump this query to a string as JSON.

Return type

str

static from_json(data)[source]

Load a query from a JSON dictionary.

Parameters

data (Mapping) – A JSON dictionary

Raises

QueryMissingNetworksError

Return type

Query

static load(file)[source]

Load a query from a JSON file.

Raises

QueryMissingNetworksError

Return type

Query

static loads(s)[source]

Load a query from a JSON string.

Parameters

s (str) – A stringified JSON query

Raises

QueryMissingNetworksError

Return type

Query

class pybel.struct.query.Seeding(initlist=None)[source]

Represents a container of seeding methods to apply to a network.

append_induction(nodes)[source]

Add a seed induction method.

Parameters

nodes (Union[BaseEntity, List[BaseEntity], List[Dict]]) – A node or list of nodes

Return type

Seeding

Returns

self for fluid API

append_neighbors(nodes)[source]

Add a seed by neighbors.

Parameters

nodes (Union[BaseEntity, List[BaseEntity], List[Dict]]) – A node or list of nodes

Return type

Seeding

Returns

self for fluid API

append_annotation(annotation, values)[source]

Add a seed induction method for single annotation’s values.

Parameters
  • annotation (str) – The annotation to filter by

  • values (Set[str]) – The values of the annotation to keep

Return type

Seeding

Returns

self for fluid API

append_sample(**kwargs)[source]

Add seed induction methods.

Kwargs can have number_edges or number_seed_nodes.

Return type

Seeding

Returns

self for fluid API

run(graph)[source]

Seed the graph or return none if not possible.

Return type

Optional[pybel.BELGraph]

to_json()[source]

Serialize this seeding container to a JSON object.

Return type

List[Dict]

dump(file, sort_keys=True, **kwargs)[source]

Dump this seeding container to a file as JSON.

Return type

None

dumps(sort_keys=True, **kwargs)[source]

Dump this query to a string as JSON.

Return type

str

static from_json(data)[source]

Build a seeding container from a JSON list.

Return type

Seeding

static load(file)[source]

Load a seeding container from a JSON file.

Return type

Seeding

static loads(s)[source]

Load a seeding container from a JSON string.

Return type

Seeding

pybel.struct.query.get_subgraph(graph, seed_method=None, seed_data=None, expand_nodes=None, remove_nodes=None)[source]

Run a pipeline query on graph with multiple sub-graph filters and expanders.

Order of Operations:

  1. Seeding by given function name and data

  2. Add nodes

  3. Remove nodes

Parameters
  • graph (pybel.BELGraph) – A BEL graph

  • seed_method (Optional[str]) – The name of the get_subgraph_by_* function to use

  • seed_data (Optional[Any]) – The argument to pass to the get_subgraph function

  • expand_nodes (Optional[List[BaseEntity]]) – Add the neighborhoods around all of these nodes

  • remove_nodes (Optional[List[BaseEntity]]) – Remove these nodes and all of their in/out edges

Return type

Optional[pybel.BELGraph]