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

test #186

Closed
wants to merge 10 commits into from
Closed

test #186

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/_run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ on:
required: true
type: string
jobs:
pre-commit:
run-unit-tests:
runs-on: ${{ inputs.os }}
steps:
- uses: actions/checkout@v3
Expand All @@ -26,7 +26,7 @@ jobs:
- name: Conda info
shell: bash -l {0}
run: conda info
- name: Perform pip installation with all stable dependencies
- name: Perform pip installation
shell: bash -l {0}
run: |
cd misc
Expand All @@ -37,6 +37,12 @@ jobs:
conda activate alphabase
pip install pytest nbmake==1.5.3
conda deactivate
- name: Print environment
shell: bash -l {0}
run: |
conda activate alphabase
pip freeze
conda deactivate
- name: Run tests
shell: bash -l {0}
run: |
Expand Down
12 changes: 12 additions & 0 deletions alphabase/constants/modification.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import numba
import numpy as np
Expand Down Expand Up @@ -83,16 +84,26 @@ def keep_modloss_by_importance(modloss_importance_level: float = 1.0):
MOD_LOSS_MASS.update(MOD_DF["modloss"].to_dict())


def _print_df(df, s):
df = df[df["mod_name"].str.startswith("Gln->pyro-Glu@Q^Any")]
logging.error(f"YYY {s} {df}")


def load_mod_df(
tsv: str = os.path.join(CONST_FILE_FOLDER, "modification.tsv"),
*,
modloss_importance_level=1,
):
global MOD_DF
MOD_DF = pd.read_table(tsv, keep_default_na=False)
_print_df(MOD_DF, 1)
_df = MOD_DF[MOD_DF.mod_name.str.contains(" ", regex=False)].copy()
_print_df(_df, 2)
_df["mod_name"] = MOD_DF.mod_name.str.replace(" ", "_", regex=False)
_print_df(_df, 3)
MOD_DF = pd.concat([MOD_DF, _df], ignore_index=True).drop_duplicates("mod_name")

_print_df(MOD_DF, 4)
MOD_DF.fillna("", inplace=True)
MOD_DF["unimod_id"] = MOD_DF.unimod_id.astype(np.int32)
MOD_DF.set_index("mod_name", drop=False, inplace=True)
Expand All @@ -103,6 +114,7 @@ def load_mod_df(
MOD_DF["modloss"] = MOD_DF["modloss_original"]
keep_modloss_by_importance(modloss_importance_level)
update_all_by_MOD_DF()
_print_df(MOD_DF, 5)


load_mod_df()
Expand Down
20 changes: 19 additions & 1 deletion alphabase/psm_reader/sage_reader.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import numpy as np
import pandas as pd
import typing
Expand Down Expand Up @@ -73,7 +75,14 @@ def lookup_modification(
print(np.min(ppm_distance))
return None

matched_mod = filtered_mod_df.sort_values(by="unimod_id").iloc[0]
str1 = f'XXX {filtered_mod_df[["mod_name", "unimod_id"]]}'
logging.error(str1)
matched_mods = filtered_mod_df.sort_values(by="unimod_id")

str2 = f'XXX {matched_mods[["mod_name", "unimod_id"]]}'
logging.error(str2)

matched_mod = matched_mods.iloc[0]

return matched_mod["mod_name"]

Expand Down Expand Up @@ -139,13 +148,22 @@ def capture_modifications(
return ";".join(site_list), ";".join(mod_list)


def _print_df(df, s):
df = df[df["mod_name"].str.startswith("Gln->pyro-Glu@Q^Any")]
df = df[["mod_name", "mass"]]
logging.error(f"ZZZ {s} {df}")


def get_annotated_mod_df():
"""Annotates the modification dataframe with the location of the modification."""
mod_annotated_df = MOD_DF.copy()
mod_annotated_df["location"] = (
mod_annotated_df["mod_name"].str.split("@").str[1].str.split("^").str[0]
)
_print_df(mod_annotated_df, "1")
mod_annotated_df = mod_annotated_df.sort_values(by="mass").reset_index(drop=True)

_print_df(mod_annotated_df, "2")
mod_annotated_df["mod_name_stripped"] = mod_annotated_df["mod_name"].str.replace(
" ", "_"
)
Expand Down
8 changes: 5 additions & 3 deletions nbs_tests/psm_reader/sage_reader.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"source": [
"#| hide\n",
"annotated_mod_df = get_annotated_mod_df()\n",
"assert lookup_modification(57.021465, 'C', annotated_mod_df) == 'Carbamidomethyl@C'"
"# assert lookup_modification(57.021465, 'C', annotated_mod_df) == 'Carbamidomethyl@C'"
]
},
{
Expand All @@ -57,8 +57,10 @@
"metadata": {},
"outputs": [],
"source": [
"#| hide\n",
"assert capture_modifications('Q[-17.026548]DQSANEKNK[+42.010567]LEM[+15.9949]NK[+42.010567]', annotated_mod_df) == ('0;9;12;14', 'Gln->pyro-Glu@Q^Any N-term;Acetyl@K;Oxidation@M;Acetyl@K')"
"\n",
"result = capture_modifications('Q[-17.026548]DQSANEKNK[+42.010567]LEM[+15.9949]NK[+42.010567]', annotated_mod_df) \n",
"expected = ('0;9;12;14', 'Gln->pyro-Glu@Q^Any N-term;Acetyl@K;Oxidation@M;Acetyl@K') \n",
"assert result ==expected, f\"Expected: {expected}, got: {result}\""
jalew188 marked this conversation as resolved.
Show resolved Hide resolved
]
},
{
Expand Down
13 changes: 13 additions & 0 deletions test_sage_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# ruff: noqa
from alphabase.psm_reader.sage_reader import *


def test_reader():
annotated_mod_df = get_annotated_mod_df()
register_readers()
result = capture_modifications(
"Q[-17.026548]DQSANEKNK[+42.010567]LEM[+15.9949]NK[+42.010567]",
annotated_mod_df,
)
expected = ("0;9;12;14", "Gln->pyro-Glu@Q^Any N-term;Acetyl@K;Oxidation@M;Acetyl@K")
assert result == expected, f"Expected: {expected}, got: {result}"
9 changes: 5 additions & 4 deletions tests/run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# TODO make tutorial_dev_spectral_libraries.ipynb work
INCLUDED_NBS=$(find ../docs/nbs -name "*.ipynb" | grep -v tutorial_dev_spectral_libraries.ipynb)
python -m pytest --nbmake $(echo $INCLUDED_NBS)
#INCLUDED_NBS=$(find ../docs/nbs -name "*.ipynb" | grep -v tutorial_dev_spectral_libraries.ipynb)
#python -m pytest --nbmake $(echo $INCLUDED_NBS)

# TODO make test_isotope_mp.ipynb work
# Note: multiprocessing in ipynb sometimes suspended on some versions of Windows, ignore the
# corresponding notebook(s) if this occurs again
# INCLUDED_NBS=$(find ../nbs_tests -name "*.ipynb" | grep -v test_isotope_mp.ipynb)

INCLUDED_NBS=$(find ../nbs_tests -name "*.ipynb")
python -m pytest --nbmake $(echo $INCLUDED_NBS)
#INCLUDED_NBS=$(find ../nbs_tests -name "*.ipynb")
#python -m pytest --nbmake ../nbs_tests/psm_reader/sage_reader.ipynb
pytest --log-cli-level=INFO ../test_sage_reader.py
Loading