<commit_before>
import gzip
import io
import logging
import requests

import pandas as pd

import pyproteome as pyp

LOGGER = logging.getLogger("pyproteome.phosphosite")

PSP_REGULATORY_URL = (
    "https://www.phosphosite.org/downloads/Regulatory_sites.gz"
)
PSP_SITE_MAPPING_URL = (
    "https://www.phosphosite.org/downloads/Phosphorylation_site_dataset.gz"
)


@pyp.utils.memoize
def get_phosphomap_data():
    """
    Fetch mapping between phosphorylation sites of different species.

    Returns
    -------
    df : :class:`pandas.DataFrame`
    """
    LOGGER.info("Fetching Phosphosite Plus mapping data")

    url = PSP_SITE_MAPPING_URL

    r = requests.get(url, stream=True)
    r.raise_for_status()

    gz = gzip.GzipFile(fileobj=io.BytesIO(r.content))

    return pd.read_csv(gz, skiprows=[0, 1, 2], sep="\t")


@pyp.utils.memoize
def get_phosphoreg_data():
    """
    Fetch Phosphosite Plus regulation data.

    Returns
    -------
    df : :class:`pandas.DataFrame`
    """
    LOGGER.info("Fetching Phosphosite Plus regulation data")

    url = PSP_REGULATORY_URL

    r = requests.get(url, stream=True)
    r.raise_for_status()

    gz = gzip.GzipFile(fileobj=io.BytesIO(r.content))

    return pd.read_table(gz, skiprows=[0, 1, 2], sep="\t", usecols=range(21))


@pyp.utils.memoize
def get_phosphosite(species, remap=False):
    """
    Download phospho sets from PhophoSite Plus.

    Parameters
    ----------
    species : str
    remap : bool, optional

    Returns
    -------
    df : :class:`pandas.DataFrame`, optional
    """
    LOGGER.info("Getting phosphosite data for {}".format(species))

    species = pyp.species.ORGANISM_MAPPING.get(species, species)

    psp_data = pyp.motifs.phosphosite.get_data()

    if remap:
        psp_data = _remap_psp(
            psp_data, species,
            acc_col="SUB_ACC_ID",
            mod_col="SUB_MOD_RSD",
            org_col="SUB_ORGANISM",
        )

    psp_data = psp_data[psp_data["SUB_ORGANISM"] == species]

    return pd.DataFrame(
        [
            (
                kinase,
                set(
                    psp_data[
                        psp_data["KINASE"] == kinase
                    ].apply(
                        lambda x:
                        ",".join([
                            x["SUB_ACC_ID"].split("-")[0],
                            x["SUB_MOD_RSD"] + ("" if remap else "-p"),
                        ]),
                        axis=1,
                    )
                ),
                set(),
            )
            for kinase in set(psp_data["KINASE"])
        ],
        columns=["name", "up_set", "down_set"]
    )


@pyp.utils.memoize
def get_phosphosite_regulation(species, remap=False):
    """
    Download phospho sets from PhophoSite Plus.

    Parameters
    ----------
    species : str
    remap : bool, optional

    Returns
    -------
    df : :class:`pandas.DataFrame`, optional<commit_msg>Don't error on missing phosphosites in PSP's tables<commit_after>
import gzip
import io
import logging
import requests

import pandas as pd

import pyproteome as pyp

LOGGER = logging.getLogger("pyproteome.phosphosite")

PSP_REGULATORY_URL = (
    "https://www.phosphosite.org/downloads/Regulatory_sites.gz"
)
PSP_SITE_MAPPING_URL = (
    "https://www.phosphosite.org/downloads/Phosphorylation_site_dataset.gz"
)


@pyp.utils.memoize
def get_phosphomap_data():
    """
    Fetch mapping between phosphorylation sites of different species.

    Returns
    -------
    df : :class:`pandas.DataFrame`
    """
    LOGGER.info("Fetching Phosphosite Plus mapping data")

    url = PSP_SITE_MAPPING_URL

    r = requests.get(url, stream=True)
    r.raise_for_status()

    gz = gzip.GzipFile(fileobj=io.BytesIO(r.content))

    return pd.read_csv(gz, skiprows=[0, 1, 2], sep="\t")


@pyp.utils.memoize
def get_phosphoreg_data():
    """
    Fetch Phosphosite Plus regulation data.

    Returns
    -------
    df : :class:`pandas.DataFrame`
    """
    LOGGER.info("Fetching Phosphosite Plus regulation data")

    url = PSP_REGULATORY_URL

    r = requests.get(url, stream=True)
    r.raise_for_status()

    gz = gzip.GzipFile(fileobj=io.BytesIO(r.content))

    return pd.read_table(gz, skiprows=[0, 1, 2], sep="\t", usecols=range(21))


@pyp.utils.memoize
def get_phosphosite(species, remap=False):
    """
    Download phospho sets from PhophoSite Plus.

    Parameters
    ----------
    species : str
    remap : bool, optional

    Returns
    -------
    df : :class:`pandas.DataFrame`, optional
    """
    LOGGER.info("Getting phosphosite data for {}".format(species))

    species = pyp.species.ORGANISM_MAPPING.get(species, species)

    psp_data = pyp.motifs.phosphosite.get_data()

    if remap:
        psp_data = _remap_psp(
            psp_data, species,
            acc_col="SUB_ACC_ID",
            mod_col="SUB_MOD_RSD",
            org_col="SUB_ORGANISM",
        )

    psp_data = psp_data[psp_data["SUB_ORGANISM"] == species]

    return pd.DataFrame(
        [
            (
                kinase,
                set(
                    psp_data[
                        psp_data["KINASE"] == kinase
                    ].apply(
                        lambda x:
                        ",".join([
                            x["SUB_ACC_ID"].split("-")[0]
                            if isinstance(x['SUB_ACC_ID'], str) else
                            '',
                            x["SUB_MOD_RSD"] + ("" if remap else "-p"),
                        ]),
                        axis=1,
                    )
                ),
                set(),
            )
            for kinase in set(psp_data["KINASE"])
        ],
        columns=["name", "up_set", "down_set"]
    )


@pyp.utils.memoize
def get_phosphosite_regulation(species, remap=False):
    """
    Download phospho sets from PhophoSite Plus.

    Parameters
    ----------
    species : str
    remap : bool, optional

    Returns
    -------
    df : :class:`pandas.DataFrame`, optional
{'fraction_non_alphanumeric': 0.12807308970099668, 'fraction_numerical': 0.0033222591362126247, 'mean_word_length': 1.9157384987893462, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3681443', 'n_tokens_mistral': 2091, 'n_tokens_neox': 1908, 'n_words': 360}