Filters

This module contains functions for filtering node and edge iterables. It relies heavily on the concepts of functional programming and the concept of predicates.

Node Filters

A node filter is a function that takes two arguments: a pybel.BELGraph and a node tuple. It returns a boolean representing whether the node passed the given test.

This module contains a set of default functions for filtering lists of nodes and building node filtering functions.

A general use for a node filter function is to use the built-in filter() in code like filter(your_node_filter, graph.nodes_iter())

pybel_tools.filters.node_filters.summarize_node_filter(graph, node_filters)[source]

Prints a summary of the number of nodes passing a given set of filters

Parameters:
pybel_tools.filters.node_filters.node_inclusion_filter_builder(nbunch)[source]

Builds a filter that only passes on nodes in the given list

Parameters:nbunch (iter[tuple]) – An iterable of BEL nodes
Returns:A node filter (graph, node) -> bool
Return type:types.FunctionType
pybel_tools.filters.node_filters.node_exclusion_filter_builder(nodes)[source]

Builds a filter that fails on nodes in the given list

Parameters:nodes (list) – A list of nodes
Returns:A node filter (graph, node) -> bool
Return type:types.FunctionType
pybel_tools.filters.node_filters.function_inclusion_filter_builder(function)[source]

Builds a filter that only passes on nodes of the given function(s)

Parameters:function (str or iter[str]) – A BEL Function or list/set/tuple of BEL functions
Returns:A node filter (graph, node) -> bool
Return type:types.FunctionType
pybel_tools.filters.node_filters.function_exclusion_filter_builder(function)[source]

Builds a filter that fails on nodes of the given function(s)

Parameters:function (str or list[str] or tuple[str] or set[str]) – A BEL Function or list/set/tuple of BEL functions
Returns:A node filter (graph, node) -> bool
Return type:types.FunctionType
pybel_tools.filters.node_filters.function_namespace_inclusion_builder(function, namespace)[source]

Builds a filter function for matching the given BEL function with the given namespace or namespaces

Parameters:
  • function (str) – A BEL function
  • or list[str] or tuple[str] or set[str] namespace (str) – The namespace to serach by
Returns:

A node filter (graph, node) -> bool

Return type:

types.FunctionType

pybel_tools.filters.node_filters.namespace_inclusion_builder(namespace)[source]

Builds a predicate for namespace inclusion

Parameters:or list[str] or tuple[str] or set[str] namespace (str) – A namespace or iter of namespaces
Returns:A node filter (graph, node) -> bool
Return type:types.FunctionType
pybel_tools.filters.node_filters.include_pathology_filter(graph, node)

A filter that passes for nodes that are pybel.constants.PATHOLOGY

pybel_tools.filters.node_filters.exclude_pathology_filter(graph, node)

A filter that fails for nodes that are pybel.constants.PATHOLOGY

pybel_tools.filters.node_filters.node_has_molecular_activity(graph, node)[source]

Returns true if over any of the node’s edges it has a molecular activity

Parameters:
Returns:

If the node has a known molecular activity

Return type:

bool

pybel_tools.filters.node_filters.iter_undefined_families(graph, namespace)[source]

Finds protein families from a given namespace (Such as SFAM) that aren’t qualified by members

Parameters:
  • graph (pybel.BELGraph) – A BEL graph
  • or list[str] namespace (str) – The namespace to filter by
Returns:

An iterator over nodes that don’t

Return type:

iter[tuple]

Edge Filters

A edge filter is a function that takes five arguments: a pybel.BELGraph, a source node tuple, a target node tuple, a key, and a data dictionary. It returns a boolean representing whether the edge passed the given test.

This module contains a set of default functions for filtering lists of edges and building edge filtering functions.

A general use for an edge filter function is to use the built-in filter() in code like filter(your_edge_filter, graph.edges_iter(keys=True, data=True))

pybel_tools.filters.edge_filters.summarize_edge_filter(graph, edge_filters)[source]

Prints a summary of the number of edges passing a given set of filters

Parameters:
pybel_tools.filters.edge_filters.edge_is_causal(graph, u, v, k, d)[source]

Only passes on causal edges, belonging to the set pybel.constants.CAUSAL_RELATIONS

Parameters:
  • graph (pybel.BELGraph) – A BEL Graph
  • u (tuple) – A BEL node
  • v (tuple) – A BEL node
  • k (int) – The edge key between the given nodes
  • d (dict) – The edge data dictionary
Returns:

If the edge is a causal edge

Return type:

bool

pybel_tools.filters.edge_filters.edge_has_author_annotation(graph, u, v, k, d)[source]

Passes for edges that have citations with authors

Parameters:
  • graph (pybel.BELGraph) – A BEL Graph
  • u (tuple) – A BEL node
  • v (tuple) – A BEL node
  • k (int) – The edge key between the given nodes
  • d (dict) – The edge data dictionary
Returns:

Does the edge’s citation data dictionary have authors included?

Return type:

bool

pybel_tools.filters.edge_filters.build_inverse_filter(edge_filter)[source]

Builds a filter that is the inverse of the given filter

Parameters:edge_filter (types.FunctionType) – An edge filter function (graph, node, node, key, data) -> bool
Returns:An edge filter function (graph, node, node, key, data) -> bool
Return type:types.FunctionType
pybel_tools.filters.edge_filters.build_annotation_value_filter(annotation, value)[source]

Builds a filter that only passes for edges that contain the given annotation and have the given value(s)

Parameters:
  • annotation (str) – The annotation to filter on
  • or iter[str] value (str) – The value or values for the annotation to filter on
Returns:

An edge filter function (graph, node, node, key, data) -> bool

Return type:

types.FunctionType

pybel_tools.filters.edge_filters.build_edge_data_filter(annotations, partial_match=True)[source]

Builds a filter that keeps edges whose data dictionaries are superdictionaries to the given dictionary

Parameters:
  • annotations (dict) – The annotation query dict to match
  • partial_match (bool) – Should the query values be used as partial or exact matches? Defaults to True.
pybel_tools.filters.edge_filters.build_relation_filter(relations)[source]

Builds a filter that passes only for edges with the given relation

Parameters:relations (str or iter[str]) – A relation or iterable of relations
Returns:An edge filter function (graph, node, node, key, data) -> bool
Return type:types.FunctionType
pybel_tools.filters.edge_filters.build_pmid_inclusion_filter(pmids)[source]

Only passes for edges with citations whose references are one of the given PubMed identifiers

Parameters:pmids (str or iter[str]) – A PubMed identifier or list of PubMed identifiers to filter for
Returns:An edge filter function (graph, node, node, key, data) -> bool
Return type:types.FunctionType
pybel_tools.filters.edge_filters.build_pmid_exclusion_filter(pmids)[source]

Fails for edges with citations whose references are one of the given PubMed identifiers

Parameters:pmids (str or iter[str]) – A PubMed identifier or list of PubMed identifiers to filter against
Returns:An edge filter function (graph, node, node, key, data) -> bool
Return type:types.FunctionType
pybel_tools.filters.edge_filters.build_author_inclusion_filter(authors)[source]

Only passes for edges with author information that matches one of the given authors

Parameters:authors (str or iter[str]) – The author or list of authors to filter by
Returns:An edge filter
Return type:types.FunctionType
pybel_tools.filters.edge_filters.edge_has_pathology_causal(graph, u, v, k, d)[source]

Returns if the subject of this edge is a pathology and participates in a causal relation where the object is not a pathology. These relations are generally nonsense.

Parameters:
  • graph (pybel.BELGraph) – A BEL Graph
  • u (tuple) – A BEL node
  • v (tuple) – A BEL node
  • k (int) – The edge key between the given nodes
  • d (dict) – The edge data dictionary
Returns:

If the subject of this edge is a pathology and it participates in a causal reaction.

Return type:

bool