示例#1
0
def remove_filtered_nodes(graph, node_filters):
    """Removes nodes passing the given node filters

    :param pybel.BELGraph graph: A BEL graph
    :param node_filters: A node filter list of node filters (graph, node) -> bool
    :type node_filters: types.FunctionType or iter[types.FunctionType]
    """
    nodes = list(filter_nodes(graph, node_filters))
    graph.remove_nodes_from(nodes)
示例#2
0
def add_canonical_names(graph, replace=False):
    """Adds a canonical name to each node's data dictionary if they are missing, in place. 

    :param pybel.BELGraph graph: A BEL graph
    :param bool replace: Should the canonical names be recalculated?
    """
    nodes = graph if replace else filter_nodes(graph, node_missing_cname)

    for node in nodes:
        graph.node[node][CNAME] = calculate_canonical_name(graph, node)
示例#3
0
def get_upstream_leaves(graph):
    """Gets all leaves of the graph (with no incoming edges and only one outgoing edge)

    .. seealso:: :func:`upstream_leaf_predicate`

    :param pybel.BELGraph graph: A BEL graph
    :return: An iterator over nodes that are upstream leaves
    :rtype: iter[tuple]
    """
    return filter_nodes(graph, node_is_upstream_leaf)
示例#4
0
def get_unweighted_upstream_leaves(graph: BELGraph, key: Optional[str] = None) -> Iterable[BaseEntity]:
    """Get nodes with no incoming edges, one outgoing edge, and without the given key in its data dictionary.

    .. seealso :: :func:`data_does_not_contain_key_builder`

    :param graph: A BEL graph
    :param key: The key in the node data dictionary representing the experimental data. Defaults to
     :data:`pybel_tools.constants.WEIGHT`.
    :return: An iterable over leaves (nodes with an in-degree of 0) that don't have the given annotation
    """
    if key is None:
        key = 'weight'

    return filter_nodes(graph, [node_is_upstream_leaf, data_missing_key_builder(key)])
示例#5
0
def get_unweighted_upstream_leaves(graph, key):
    """Gets all leaves of the graph with no incoming edges, one outgoing edge, and without the given key in
    its data dictionary

    .. seealso :: :func:`data_does_not_contain_key_builder`

    :param pybel.BELGraph graph: A BEL graph
    :param str key: The key in the node data dictionary representing the experimental data
    :return: An iterable over leaves (nodes with an in-degree of 0) that don't have the given annotation
    :rtype: iter[tuple]
    """
    return filter_nodes(graph,
                        [node_is_upstream_leaf,
                         data_missing_key_builder(key)])
示例#6
0
def search_node_names(graph: BELGraph, query: Strings) -> Iterable[BaseEntity]:
    """Search for nodes containing a given string(s).

    :param graph: A BEL graph
    :param query: The search query
    :return: An iterator over nodes whose names match the search query

    Example:
    .. code-block:: python

        >>> from pybel.examples import sialic_acid_graph
        >>> from pybel_tools.selection import search_node_names
        >>> list(search_node_names(sialic_acid_graph, 'CD33'))
        [('Protein', 'HGNC', 'CD33'), ('Protein', 'HGNC', 'CD33', ('pmod', ('bel', 'Ph')))]

    """
    return filter_nodes(graph, build_node_name_search(query))
示例#7
0
def search_node_namespace_names(
    graph: BELGraph,
    query: Strings,
    namespace: Strings,
) -> Iterable[BaseEntity]:
    """Search for nodes with the given namespace(s) and whose names containing a given string(s).

    :param graph: A BEL graph
    :param query: The search query
    :param namespace: The namespace(s) to filter
    :return: An iterator over nodes whose names match the search query
    """
    node_predicates = [
        namespace_inclusion_builder(namespace),
        build_node_name_search(query),
    ]

    return filter_nodes(graph, node_predicates)
示例#8
0
def overlay_type_data(graph, data, label, func, namespace, overwrite=False, impute=None):
    """Overlays tabular data on the network for data that comes from an data set with identifiers that lack
    namespaces.

    For example, if you want to overlay differential gene expression data from a table, that table
    probably has HGNC identifiers, but no specific annotations that they are in the HGNC namespace or
    that the entities to which they refer are RNA.

    :param pybel.BELGraph graph: A BEL Graph
    :param dict[str,float] dict data: A dictionary of {name: data}
    :param str label: The annotation label to put in the node dictionary
    :param str func: The function of the keys in the data dictionary
    :param str namespace: The namespace of the keys in the data dictionary
    :param bool overwrite: Should old annotations be overwritten?
    :param Optional[float] impute: The value to use for missing data
    """
    new_data = {
        node: data.get(graph.node[node][NAME], impute)
        for node in filter_nodes(graph, function_namespace_inclusion_builder(func, namespace))
    }

    overlay_data(graph, new_data, label, overwrite=overwrite)
示例#9
0
def get_upstream_leaves(graph: BELGraph) -> Iterable[BaseEntity]:
    """Iterate over all of the leaves of the graph (with no incoming edges and only one outgoing edge).

    .. seealso:: :func:`upstream_leaf_predicate`
    """
    return filter_nodes(graph, node_is_upstream_leaf)