Skip to content

Commit

Permalink
uniform import-export structure; sorted import groups; completed ever…
Browse files Browse the repository at this point in the history
…y `__all__`; updated every `__init__`; added missing module docstrings
  • Loading branch information
deeenes committed Jul 3, 2024
1 parent 10a787d commit f585b2f
Show file tree
Hide file tree
Showing 31 changed files with 470 additions and 181 deletions.
4 changes: 2 additions & 2 deletions networkcommons/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
#

from ._builtin import _module_data, urls
from .omics import *
from .network import *
from . import omics
from . import network
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
# https://www.gnu.org/licenses/gpl-3.0.txt
#

"""
Prior knowledge network used by LIANA+.
"""

__all__ = ['get_lianaplus']

import lazy_import

liana = lazy_import.lazy_module('liana')


def get_lianaplus(resource='Consensus'):
"""
Expand All @@ -27,8 +37,6 @@ def get_lianaplus(resource='Consensus'):
pandas.DataFrame: Liana+ network with source, target, and sign columns.
"""

import liana

network = liana.resource.select_resource(resource).drop_duplicates()
network.columns = ['source', 'target']
network['sign'] = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,34 @@
# https://www.gnu.org/licenses/gpl-3.0.txt
#

"""
Prior knowledge network used by MOON.
"""

__all__ = ['build_moon_regulons']

import lazy_import
import numpy as np
import pandas as pd

dc = lazy_import.lazy_module('decoupler')

from networkcommons import _utils
from . import _omnipath
from . import _liana


def build_moon_regulons(include_liana=False):

import decoupler as dc
from . import omnipath

dorothea_df = dc.get_collectri()

TFs = np.unique(dorothea_df['source'])

full_pkn = omnipath.get_omnipath(genesymbols=True, directed_signed=True)
full_pkn = _omnipath.get_omnipath(genesymbols=True, directed_signed=True)

if include_liana:
from . import lianaplus
ligrec_resource = lianaplus.get_lianaplus()

ligrec_resource = _liana.get_lianaplus()

full_pkn = pd.concat([full_pkn, ligrec_resource])
full_pkn['edgeID'] = full_pkn['source'] + '_' + full_pkn['target']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@
# https://www.gnu.org/licenses/gpl-3.0.txt
#

import omnipath as op
"""
Access to the OmniPath database.
"""

__all__ = ['get_omnipath']

import lazy_import
import numpy as np

op = lazy_import.lazy_module('omnipath')


def get_omnipath(genesymbols=True,
directed_signed=True):
def get_omnipath(genesymbols: bool = True, directed_signed: bool = True):
"""
Retrieves the Omnipath network with directed interactions
and specific criteria.
Expand Down
14 changes: 9 additions & 5 deletions networkcommons/data/omics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
# https://www.gnu.org/licenses/gpl-3.0.txt
#

from .decryptm import *
from .decryptm_ebi import get_decryptm as _get_decryptm
from .deseq2 import *
from .panacea import *
from .scperturb import *
"""
Access to public omics datasets.
"""

from ._common import *
from ._decryptm import *
from ._deseq2 import *
from ._panacea import *
from ._scperturb import *
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,28 @@
# https://www.gnu.org/licenses/gpl-3.0.txt
#

"""
General procedures for downloading omics datasets.
"""

from __future__ import annotations

from typing import Any, IO
__all__ = ['datasets']

from typing import IO
import zipfile
import os
import re
import glob
import hashlib
import contextlib
import urllib.parse

import shutil
import requests
import bs4
import pandas as pd

from .._builtin import _module_data
from networkcommons import _conf
from networkcommons._session import _log

__all__ = ['datasets']
from .._builtin import _module_data


def _datasets() -> dict[str, dict]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@
# https://www.gnu.org/licenses/gpl-3.0.txt
#

"""
DecryptM proteomics and phosphoproteomics data.
"""

from __future__ import annotations

__all__ = ['decryptm_datasets', 'decryptm_table', 'decryptm_experiment']

import os
import urllib.parse

import pandas as pd

from . import common
from . import _common
from networkcommons import _conf

__all__ = ['decryptm_datasets', 'decryptm_table', 'decryptm_experiment']


def decryptm_datasets(update: bool = False) -> pd.DataFrame:
"""
Expand All @@ -43,7 +47,7 @@ def decryptm_datasets(update: bool = False) -> pd.DataFrame:

if update or not os.path.exists(path):

baseurl = urllib.parse.urljoin(common._baseurl(), 'decryptm')
baseurl = urllib.parse.urljoin(_common._baseurl(), 'decryptm')

datasets = pd.DataFrame(
[
Expand All @@ -52,9 +56,9 @@ def decryptm_datasets(update: bool = False) -> pd.DataFrame:
data_type,
fname,
)
for experiment in common._ls(baseurl)
for data_type in common._ls(f'{baseurl}/{experiment}')
for fname in common._ls(f'{baseurl}/{experiment}/{data_type}')
for experiment in _common._ls(baseurl)
for data_type in _common._ls(f'{baseurl}/{experiment}')
for fname in _common._ls(f'{baseurl}/{experiment}/{data_type}')
if fname.startswith('curve')
],
columns = ['experiment', 'data_type', 'fname']
Expand Down Expand Up @@ -86,8 +90,8 @@ def decryptm_table(experiment: str, data_type: str, fname: str) -> pd.DataFrame:
The table as a pandas data frame.
"""

return common._open(
common._commons_url('decryptm', **locals()),
return _common._open(
_common._commons_url('decryptm', **locals()),
df = {'sep': '\t'},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@

from __future__ import annotations

__all__ = []

import os
import shutil
import glob
import urllib.parse

from . import common
from . import _common
from networkcommons._session import _log


def get_decryptm(path: str):
def _get_decryptm(path: str):
"""
Download DecryptM from EBI.
Expand All @@ -35,13 +37,13 @@ def get_decryptm(path: str):

os.makedirs(path, exist_ok = True)
url = 'https://ftp.pride.ebi.ac.uk/pride/data/archive/2023/03/PXD037285/'
files = [f for f in common._ls(url) if f.endswith('Curves.zip')]
files = [f for f in _common._ls(url) if f.endswith('Curves.zip')]

for fname in files:

zip_url = urllib.parse.urljoin(url, fname)

with common._open(zip_url) as zip_file:
with _common._open(zip_url) as zip_file:

_log(f'Extracting zip `{zip_file.filename}` to `{path}`.')
zip_file.extractall(path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
# https://www.gnu.org/licenses/gpl-3.0.txt
#

"""
Differential expression analysis with DESeq2.
"""

from __future__ import annotations

__all__ = ['deseq2']

from typing import TYPE_CHECKING
import multiprocessing
import importlib
Expand All @@ -23,17 +29,16 @@

import pandas as pd

import lazy_import
from pypath_common import _misc as _ppcommon
import pydeseq2 as _deseq2

for _mod in ('default_inference', 'dds', 'ds'):
importlib.import_module(f'pydeseq2.{_mod}')

globals()[f'_deseq2_{_mod}'] = lazy_import.lazy_module(f'pydeseq2.{_mod}')

from networkcommons import _conf
from networkcommons._session import _log

__all__ = ['deseq2']


def deseq2(
counts: pd.DataFrame,
Expand Down Expand Up @@ -70,9 +75,9 @@ def deseq2(
ref_group = ref_group.replace('_', '-')

n_cpus = _conf.get('cpu_count', multiprocessing.cpu_count())
inference = _deseq2.default_inference.DefaultInference(n_cpus = n_cpus)
inference = _deseq2_default_inference.DefaultInference(n_cpus = n_cpus)

dds = _deseq2.dds.DeseqDataSet(
dds = _deseq2_dds.DeseqDataSet(
counts=counts.T,
metadata=metadata,
design_factors=design_factors,
Expand All @@ -82,7 +87,7 @@ def deseq2(

dds.deseq2()

results = _deseq2.ds.DeseqStats(
results = _deseq2_ds.DeseqStats(
dds,
contrast=['group', test_group, ref_group],
inference=inference
Expand Down
6 changes: 3 additions & 3 deletions networkcommons/data/omics/_moon.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

from __future__ import annotations

import pandas as pd
__all__ = ['moon']

from . import common
import pandas as pd

__all__ = ['moon']
from . import _common


def moon() -> dict[str, pd.DataFrame]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
# https://www.gnu.org/licenses/gpl-3.0.txt
#

"""
RNA-Seq data from the 'Pancancer Analysis of Chemical Entity Activity'
resource.
"""

from __future__ import annotations

import pandas as pd
__all__ = ['panacea']

from . import common
import pandas as pd

__all__ = ['panacea']
from . import _common


def panacea() -> tuple[pd.DataFrame]:
Expand All @@ -31,8 +36,8 @@ def panacea() -> tuple[pd.DataFrame]:
"""

return tuple(
common._open(
common._commons_url('panacea', table = table),
_common._open(
_common._commons_url('panacea', table = table),
df = {'sep': '\t'},
)
for table in ('count', 'meta')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@
# https://www.gnu.org/licenses/gpl-3.0.txt
#

"""
Single-cell RNA-Seq data from the 'scPerturb' resource.
"""

from __future__ import annotations

__all__ = ['scperturb', 'scperturb_metadata', 'scperturb_datasets']

from typing import Any
import json

import bs4
import anndata as ad

from . import common
from . import _common

_URL = 'https://zenodo.org/record/10044268'

Expand Down Expand Up @@ -50,7 +56,7 @@ def scperturb_metadata() -> dict[str, Any]:
record is https://zenodo.org/records/10044268.
"""

soup = common._open(_URL, ftype = 'html')
soup = _common._open(_URL, ftype = 'html')
data = soup.find(id = 'recordCitation').attrs['data-record']

return json.loads(data)
Expand All @@ -72,6 +78,6 @@ def scperturb(dataset: str) -> ad.AnnData:
"""

urls = scperturb_datasets()
path = common._maybe_download(urls[dataset])
path = _common._maybe_download(urls[dataset])

return ad.read_h5ad(path)
2 changes: 1 addition & 1 deletion networkcommons/eval/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
Network metrics and analysis methods.
"""

from . import metrics as metrics
from .metrics import *
Loading

0 comments on commit f585b2f

Please sign in to comment.