Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging #221

Merged
merged 44 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
218d80f
add logging control function
a-r-j Oct 23, 2022
cb10aa6
change logging to loguru
a-r-j Oct 23, 2022
95a887c
docstring and loguru logging
a-r-j Oct 23, 2022
741cf2e
docstring and loguru
a-r-j Oct 23, 2022
a7a0d95
loguru
a-r-j Oct 23, 2022
5cffa04
loguru
a-r-j Oct 23, 2022
31fabf2
loguru
a-r-j Oct 23, 2022
8b5b9d3
loguru
a-r-j Oct 23, 2022
ec40954
loguru; docstrings
a-r-j Oct 23, 2022
59f8ae2
loguru
a-r-j Oct 23, 2022
f108ee0
loguru
a-r-j Oct 23, 2022
f97977b
docstrings
a-r-j Oct 23, 2022
c49f829
loguru docstrings
a-r-j Oct 23, 2022
2be414a
loguru docstrings
a-r-j Oct 23, 2022
beab76d
loguru docstrings
a-r-j Oct 23, 2022
b035222
loguru docstrings
a-r-j Oct 23, 2022
e87c263
loguru docstrings
a-r-j Oct 23, 2022
b825dda
loguru docstrings
a-r-j Oct 23, 2022
61fc497
logging and docstrings
a-r-j Oct 23, 2022
02ecacf
loguru, docstrings
a-r-j Oct 23, 2022
5603129
loguru, docstrings
a-r-j Oct 23, 2022
f69ae4d
loguru, docstrings
a-r-j Oct 23, 2022
56279ed
loguru, docstrings
a-r-j Oct 23, 2022
cdd2d44
docstrings
a-r-j Oct 23, 2022
cfe7843
docstrings
a-r-j Oct 23, 2022
10c87d4
docstrings, loguru
a-r-j Oct 23, 2022
8dc1cdd
docstrings, loguru
a-r-j Oct 23, 2022
329eb6a
docstrings, loguru
a-r-j Oct 23, 2022
6d7a244
docstrings, loguru
a-r-j Oct 23, 2022
99d2f27
docstrings, loguru
a-r-j Oct 23, 2022
d99a620
docstrings, loguru
a-r-j Oct 23, 2022
cc9a530
docstrings, loguru
a-r-j Oct 23, 2022
82ef776
docstrings, loguru
a-r-j Oct 23, 2022
d985cd6
docstrings, loguru
a-r-j Oct 23, 2022
f23c532
docstrings, loguru
a-r-j Oct 23, 2022
e17314d
docstrings, loguru
a-r-j Oct 23, 2022
8ae87f1
docstrings, loguru
a-r-j Oct 23, 2022
808a86d
docstrings, loguru
a-r-j Oct 23, 2022
359c495
docstrings
a-r-j Oct 24, 2022
d3ea986
add missing import
a-r-j Oct 24, 2022
2877486
Merge branch 'master' into logging
a-r-j Oct 24, 2022
3bac8a1
black
a-r-j Oct 24, 2022
7e583c4
add docstring
a-r-j Oct 24, 2022
bfbe843
update changelog
a-r-j Oct 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
### 1.5.3 - UNRELEASED

#### Improvements
* [Logging] - [#221](https://github.com/a-r-j/graphein/pull/221) Adds global control of logging with `graphein.verbose(enabled=False)`.

#### Protein

* [Bugfix] - [#220](https://github.com/a-r-j/graphein/pull/220) Fixes edge metadata conversion to PyG. Contribution by @manonreau.
Expand Down
12 changes: 12 additions & 0 deletions graphein/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@
{"sink": RichHandler(rich_tracebacks=True), "format": "{message}"}
]
)


def verbose(enabled: bool = False):
"""Enable/Disable logging.

:param enabled: Whether or not to enable logging, defaults to ``False``.
:type enabled: bool, optional
"""
if not enabled:
logger.disable("graphein")
else:
logger.enable("graphein")
30 changes: 17 additions & 13 deletions graphein/grn/edges.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
"""Functions for adding edges to Gene Regulatory Networks from various sources."""
# %%
# Graphein
# Author: Arian Jamasb <arian@jamasb.io>, Ramon Vinas
# License: MIT
# Project Website: https://github.com/a-r-j/graphein
# Code Repository: https://github.com/a-r-j/graphein
import logging
from typing import Callable, List, Optional

import networkx as nx
import pandas as pd
from loguru import logger as log

from graphein.grn.parse_regnetwork import RegNetwork_df
from graphein.grn.parse_trrust import TRRUST_df

log = logging.getLogger(__name__)


def add_trrust_edges(
G: nx.Graph, trrust_filtering_funcs: Optional[List[Callable]] = None
Expand All @@ -24,9 +23,12 @@ def add_trrust_edges(

:param G: Graph to edges to (populated with gene_id nodes)
:type G: nx.Graph
:param trrust_filtering_funcs: List of functions to apply to TRRUST dataframe as pre-processing prior to graph constructions. Defaults to None.
:param trrust_filtering_funcs: List of functions to apply to TRRUST
dataframe as pre-processing prior to graph constructions. Defaults to
``None``.
:type trrust_filtering_funcs: List[Callable], optional
:return: nx.Graph GRNGraph with TRRUST regulatory interactions added as edges
:return: nx.Graph GRNGraph with TRRUST regulatory interactions added
as edges
:rtype: nx.Graph
"""
G.graph["sources"].append("trrust")
Expand All @@ -45,9 +47,11 @@ def add_regnetwork_edges(
"""
Adds edges from RegNetwork to GRNGraph

:param G: Graph to edges to (populated with gene_id nodes)
:param kwargs: Additional parameters to pass to RegNetwork
:return: nx.Graph GRNGraph with RegNetwork regulatory interactions added as edges
:param G: Graph to edges to (populated with gene_id nodes).
:type G: nx.Graph
:param kwargs: Additional parameters to pass to RegNetwork.
:return: nx.Graph GRNGraph with RegNetwork regulatory interactions added
as edges.
"""
G.graph["sources"].append("regnetwork")
G.graph["regnetwork_df"] = RegNetwork_df(
Expand All @@ -63,15 +67,15 @@ def add_interacting_genes(
G: nx.Graph, df: pd.DataFrame, kind: str
) -> nx.Graph:
"""
Generic function for adding interaction edges to GRNGraph
Generic function for adding interaction edges to GRNGraph.

:param G: GRNGraph to populate with edges
:param G: GRNGraph to populate with edges.
:type G: nx.Graph
:param df: DataFrame containing edgelist
:param df: DataFrame containing edgelist.
:type df: pd.DataFrame
:param kind: name of interaction type
:param kind: name of interaction type.
:type kind: str
:returns: Graph with edges added
:returns: Graph with edges added.
:rtype: nx.Graph
"""

Expand Down
6 changes: 3 additions & 3 deletions graphein/grn/features/node_features.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import logging
"""Node featurisation utilities for Gene Regulatory Networks."""
from typing import Any, Dict

from graphein.utils.utils import import_message
from loguru import logger as log

log = logging.getLogger(__name__)
from graphein.utils.utils import import_message

try:
from bioservices import HGNC, UniProt
Expand Down
6 changes: 2 additions & 4 deletions graphein/grn/graphs.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Graph construction utilities for Gene Regulatory Networks."""
# %%
# Graphein
# Author: Arian Jamasb <arian@jamasb.io>, Ramon Vinas
# License: MIT
# Project Website: https://github.com/a-r-j/graphein
# Code Repository: https://github.com/a-r-j/graphein
import logging
from typing import Callable, List, Optional

import networkx as nx
from loguru import logger as log

from graphein.grn.config import GRNGraphConfig
from graphein.utils.utils import (
Expand All @@ -17,9 +18,6 @@
compute_edges,
)

log = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

EDGE_COLOR_MAPPING = {"trrust": "r", "regnetwork": "b", "abasy": "g"}


Expand Down
97 changes: 68 additions & 29 deletions graphein/grn/parse_regnetwork.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import functools

"""Functions for parsing GRNs from RegNetwork."""
# %%
# Graphein
# Author: Ramon Vinas, Arian Jamasb <arian@jamasb.io>
# License: MIT
# Project Website: https://github.com/a-r-j/graphein
# Code Repository: https://github.com/a-r-j/graphein
import logging

import functools
import os
import ssl
import zipfile
Expand All @@ -15,21 +15,25 @@

import pandas as pd
import wget
from loguru import logger as log

from graphein.utils.utils import filter_dataframe, ping

log = logging.getLogger(__name__)


def _download_RegNetwork(
root_dir: Optional[Path] = None, network_type: str = "human"
) -> str:
"""
Downloads RegNetwork regulatory interactions to the root directory. Returns the filepath.

:param root_dir: Path object specifying the location to download RegNetwork to. Default is None which downloads to the dataset/ directory inside graphein.
:type root_dir: patlib.Path, optional
:param network_type: Specifies whether to download human or mouse regulatory network. Supported values: "human" (default), "mouse".
Downloads RegNetwork regulatory interactions to the root directory.
Returns the filepath.

:param root_dir: ``Path`` object specifying the location to download
RegNetwork to. Default is ``None`` which downloads to the ``dataset/``
directory inside graphein.
:type root_dir: pathlib.Path, optional
:param network_type: Specifies whether to download human or mouse
regulatory network. Supported values: ``"human"`` (default),
``"mouse"``.
:type network_type: str
:returns: path to downloaded RegNetwork
:rtype: str
Expand All @@ -39,7 +43,8 @@ def _download_RegNetwork(
ping_result = ping("regnetworkweb.org")
if not ping_result:
log.warning(
"RegNetwork is not available. Please check your internet connection or verify at: http://www.regnetworkweb.org"
"RegNetwork is not available. Please check your internet \
connection or verify at: http://www.regnetworkweb.org"
)

mouse_url = "https://regnetworkweb.org/download/mouse.zip"
Expand All @@ -51,7 +56,8 @@ def _download_RegNetwork(
url = mouse_url
else:
raise ValueError(
f"network_type: {network_type} is unsupported. Please use 'human' or 'mouse'"
f"network_type: {network_type} is unsupported. Please use 'human' \
or 'mouse'"
)

# If no root dir is provided, use the dataset directory inside graphein.
Expand Down Expand Up @@ -81,9 +87,14 @@ def _download_RegNetwork(

def _download_RegNetwork_regtypes(root_dir: Optional[Path] = None) -> str:
"""
Downloads RegNetwork regulatory interactions types to the root directory
Downloads RegNetwork regulatory interactions types to the root directory.

:param root_dir: Path object specifying the location to download RegNetwork to
:param root_dir: ``Path`` object specifying the location to download
RegNetwork to
:type root_dir: pathlib.Path, optional. Default is ``None`` which downloads
to the ``dataset/`` directory inside graphein.
:returns: Path to downloaded RegNetwork
:rtype: str
"""
url = "https://regnetworkweb.org/download/RegulatoryDirections.zip"

Expand Down Expand Up @@ -117,7 +128,14 @@ def load_RegNetwork_interactions(
root_dir: Optional[Path] = None,
) -> pd.DataFrame:
"""
Loads RegNetwork interaction datafile. Downloads the file first if not already present.
Loads RegNetwork interaction datafile. Downloads the file first if not
already present.

:param root_dir: ``Path`` object specifying the location of the
RegNetwork download.
:type root_dir: pathlib.Path, optional. Default is ``None``.
:returns: RegNetwork interaction data as a pd.DataFrame.
:rtype: pd.DataFrame
"""
file = _download_RegNetwork(root_dir)
return pd.read_csv(
Expand All @@ -130,7 +148,14 @@ def load_RegNetwork_regulation_types(
root_dir: Optional[Path] = None,
) -> pd.DataFrame:
"""
Loads RegNetwork regulation types. Downloads the file first if not already present.
Loads RegNetwork regulation types. Downloads the file first if not already
present.

:param root_dir: ``Path`` object specifying the location of the RegNetwork
download.
:type root_dir: pathlib.Path, optional. Default is ``None``.
:returns: RegNetwork regulation types as a pd.DataFrame.
:rtype: pd.DataFrame
"""
file = _download_RegNetwork_regtypes(root_dir)
return pd.read_csv(
Expand All @@ -149,7 +174,12 @@ def parse_RegNetwork(
Parser for RegNetwork interactions

:param gene_list: List of gene identifiers
:return Pandas dataframe with the regulatory interactions between genes in the gene list
:type gene_list: List[str]
:param root_dir: ``Path`` object specifying the location of the RegNetwork
download.
:return: Pandas DataFrame with the regulatory interactions between genes
in the gene list.
:rtype: pd.DataFrame
"""
# Load dataframes
df = load_RegNetwork_interactions(root_dir)
Expand All @@ -175,11 +205,13 @@ def filter_RegNetwork(
df: pd.DataFrame, funcs: Optional[List[Callable]] = None
) -> pd.DataFrame:
"""
Filters results of RegNetwork call by providing a list of user-defined functions that accept a dataframe and return a dataframe
Filters results of RegNetwork call by providing a list of user-defined
functions that accept a DataFrame and return a DataFrame

:param df: pd.Dataframe to filter
:param funcs: list of functions that carry out dataframe processing
:return: processed dataframe
:param df: pd.DataFrame to filter
:param funcs: List of functions that carry out DataFrame processing.
:return: Processed DataFrame.
:rtype: pd.DataFrame
"""
if funcs is not None:
df = filter_dataframe(df, funcs)
Expand All @@ -189,7 +221,7 @@ def filter_RegNetwork(

def standardise_RegNetwork(df: pd.DataFrame) -> pd.DataFrame:
"""
Standardises STRING dataframe, e.g. puts everything into a common format
Standardises RegNetwork dataframe, e.g. puts everything into a common format

:param df: Source specific Pandas dataframe
:type df: pd.DataFrame
Expand Down Expand Up @@ -217,16 +249,23 @@ def RegNetwork_df(
filtering_funcs: Optional[List[Callable]] = None,
) -> pd.DataFrame:
"""
Generates standardised dataframe with RegNetwork protein-protein interactions, filtered according to user's input
:return: Standardised dataframe with RegNetwork interactions
Generates standardised dataframe with RegNetwork protein-protein
interactions, filtered according to user's input.

:param gene_list: List of gene identifiers.
:type gene_list: List[str]
:param root_dir: ``Path`` object specifying the location of the RegNetwork
dataset.
:type root_dir: pathlib.Path, optional. Default is ``None``.
:param filtering_funcs: List of (optional) functions that accept a dataframe
and return a dataframe. These functions are applied to the dataframe to
filter entries according to the user's input.
:type filtering_funcs: List[Callable], optional. Default is ``None``.
:return: Standardised dataframe with RegNetwork interactions.
:rtype: pd.DataFrame
"""
df = parse_RegNetwork(gene_list=gene_list, root_dir=root_dir)
df = filter_RegNetwork(df, filtering_funcs)
df = standardise_RegNetwork(df)

return df


if __name__ == "__main__":
df = RegNetwork_df(["AATF", "MYC", "USF1", "SP1", "TP53", "DUSP1"])
print(df)