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

added protein n-gram #6

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
9 changes: 1 addition & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest pytest-cov wheel
pip install -e .
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
pip install -e ".[test,build]"
- name: Run unit and system tests
run: |
pytest -v --cov=petasus tests/
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-toml
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: detect-private-key
- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.12.0
hooks:
- id: black
language_version: python3.10
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.264
rev: v0.1.7
hooks:
- id: ruff
args: ['--fix']
181 changes: 181 additions & 0 deletions petasus/protein_ngram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
from __future__ import annotations

import re
from collections import defaultdict
from os import PathLike

from loguru import logger
from pyteomics.fasta import FASTA
from tqdm.auto import tqdm

FASTA_NAME_REGEX = re.compile(r"^.*\|(.*)\|.*$")
UNIPROT_ACC_REGEX = re.compile(r"^[A-Z0-9]{6}(-\d+)?$")


class ProteinNGram:
"""Implements an n-gram to fast lookup of proteins that match a peptide.

Usage
-----
```python
ngram = ProteinNGram.from_fasta("path/to/fasta")
ngram.search_ngram("AAC")
['Prot1']
```

Examples
--------
>>> base_ngram = {'AA': {1,3}, 'AB': {2,3}, 'AC': {1, 4}, 'CA': {4}}
>>> inv_index = {1: "Prot1", 2: "Prot2", 3: "Prot3", 4: "Prot4"}
>>> inv_seq = {1: "AACAA", 2: "AABAA", 3: "ABDAA", 4: "CACAA"}
>>> ngram = ProteinNGram(
... ngram = base_ngram,
... inv_alias = inv_index,
... inv_seq = inv_seq)
>>> ngram.search_ngram("AAC")
['Prot1']
>>> ngram.search_ngram("CAC")
['Prot4']
"""

__slots__ = ("ngram_size", "ngram", "inv_alias", "inv_seq")

def __init__(
self,
ngram: dict[str, set[int]],
inv_alias: dict[int, str],
inv_seq: dict[int, str],
) -> None:
"""Initialized an ngram for fast lookup.

For details check the main class docstring.
"""
keys = list(ngram)
if not all(len(keys[0]) == len(k) for k in ngram):
raise ValueError("All ngram keys need to be the same length")

Check warning on line 55 in petasus/protein_ngram.py

View check run for this annotation

Codecov / codecov/patch

petasus/protein_ngram.py#L55

Added line #L55 was not covered by tests
self.ngram_size: int = len(keys[0])
self.ngram = ngram
self.inv_alias = inv_alias
self.inv_seq = inv_seq

def search_ngram(self, entry: str) -> list[str]:
"""Searches a sequence using the n-gram and returns the matches."""

if len(entry) < self.ngram_size:
raise ValueError(
f"Entry {entry} is shorter than the n-gram size ({self.ngram_size})"
)

candidates = None
for x in [
entry[x : x + self.ngram_size]
for x in range(1 + len(entry) - self.ngram_size)
]:
if len(x) < self.ngram_size:
raise

Check warning on line 75 in petasus/protein_ngram.py

View check run for this annotation

Codecov / codecov/patch

petasus/protein_ngram.py#L75

Added line #L75 was not covered by tests

if candidates is None:
jspaezp marked this conversation as resolved.
Show resolved Hide resolved
candidates = self.ngram.get(x, set())
else:
candidates = candidates.intersection(self.ngram[x])
if len(candidates) <= 1:
break

# This makes sure the whole sequence is matched.
# For instance ... "BAAAAB" and "BAAAAAAAAAB" share all the same length 2
# ngrams, but do not match the same sequence (if the protein is "PEPTIDEBAAAB",
# only the first would be kept).
out = [
self.inv_alias[x] for x in candidates if entry in self.inv_seq[x]
]
return out

@staticmethod
def from_fasta(
fasta_file: PathLike | str,
ngram_size: int = 4,
progress: bool = True,
proteins_keep: None | set[str] = None,
) -> ProteinNGram:
"""Builds a protein n-gram from a fasta file.

Parameters
----------
fasta_file:
Path-like or string representing the fasta file to read in order
to build the index.
ngram_size:
Size of the chunks that will be used to build the n-gram, should
be smaller than the smallest peptide to be searched. Longer sequences
should give a more unique aspect to it but a larger index is built.
progress:
Whether to show a progress bar while building the index.
proteins_keep: set[str]:
If not None, only keep the proteins in the set, by matching the
uniprot ID. Example: {'Q92804', 'Q92804-2', 'P13639'}

"""
ngram = defaultdict(set)
inv_alias = {}
inv_seq = {}
skipped = 0
kept = 0

for i, entry in tqdm(
enumerate(FASTA(str(fasta_file))),
disable=not progress,
desc="Building peptide n-gram index",
):
entry_name = FASTA_NAME_REGEX.search(entry.description).group(1)
if proteins_keep is not None and entry_name not in proteins_keep:
skipped += 1
continue

Check warning on line 132 in petasus/protein_ngram.py

View check run for this annotation

Codecov / codecov/patch

petasus/protein_ngram.py#L131-L132

Added lines #L131 - L132 were not covered by tests
else:
kept += 1
sequence = entry.sequence
if len(sequence) < ngram_size:
logger.warning(

Check warning on line 137 in petasus/protein_ngram.py

View check run for this annotation

Codecov / codecov/patch

petasus/protein_ngram.py#L137

Added line #L137 was not covered by tests
f"Skipping {entry_name} because it is shorter than the n-gram size"
)
continue

Check warning on line 140 in petasus/protein_ngram.py

View check run for this annotation

Codecov / codecov/patch

petasus/protein_ngram.py#L140

Added line #L140 was not covered by tests

inv_alias[i] = entry_name
inv_seq[i] = sequence
jspaezp marked this conversation as resolved.
Show resolved Hide resolved
for x in [
sequence[x : x + ngram_size]
for x in range(1 + len(sequence) - ngram_size)
]:
ngram[x].add(i)

if proteins_keep is not None:
logger.info(

Check warning on line 151 in petasus/protein_ngram.py

View check run for this annotation

Codecov / codecov/patch

petasus/protein_ngram.py#L151

Added line #L151 was not covered by tests
f"Kept {kept} proteins and skipped {skipped} "
"proteins when importing the fasta file",
)

return ProteinNGram(ngram=ngram, inv_alias=inv_alias, inv_seq=inv_seq)


def get_protein_accessions(protein_list: list[str]) -> set[str]:
jspaezp marked this conversation as resolved.
Show resolved Hide resolved
"""Extracts all protein accessions from a list of protein groups.

This is meant to be used on the protein groups column in a mokapot
results file.

Examples
--------
>>> tmp = [
... "sp|Q92804|RBP56_HUMAN",
... "sp|Q92804-2|RBP56_HUMAN",
... "sp|P13639|EF2_HUMAN"]
>>> got = get_protein_accessions(tmp)
>>> exp = {'Q92804', 'Q92804-2', 'P13639'}
>>> exp == got
True
"""
out = set()
for protein in protein_list:
for accession in protein.split(","):
acc = accession.split("|")[1]
out.add(acc)
return out
107 changes: 107 additions & 0 deletions petasus/scripts/add_peptidetoprotein.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import click
import sqlite3
import os
import pandas as pd
from petasus.protein_ngram import ProteinNGram, get_protein_accessions

Check warning on line 5 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L1-L5

Added lines #L1 - L5 were not covered by tests


def _add_peptidetoprotein(

Check warning on line 8 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L8

Added line #L8 was not covered by tests
mokapot_proteins: os.PathLike,
fasta_file: os.PathLike,
speclib: os.PathLike,
ngram_size=4,
q_threshold=0.1,
):
"""Add peptidetoprotein table to dlib/elib file."""
mokapot_proteins = pd.read_csv(mokapot_proteins, sep="\t")
mokapot_proteins = mokapot_proteins[

Check warning on line 17 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L16-L17

Added lines #L16 - L17 were not covered by tests
mokapot_proteins["mokapot q-value"] < q_threshold
]

accessions_keep = get_protein_accessions(

Check warning on line 21 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L21

Added line #L21 was not covered by tests
mokapot_proteins["mokapot protein group"].tolist()
)

ngram = ProteinNGram.from_fasta(

Check warning on line 25 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L25

Added line #L25 was not covered by tests
fasta_file,
proteins_keep=accessions_keep,
ngram_size=ngram_size,
)

conn = sqlite3.connect(speclib)
df = pd.read_sql("SELECT PeptideSeq FROM entries", conn)
conn.close()
peps = df["PeptideSeq"].unique().tolist()
del df

Check warning on line 35 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L31-L35

Added lines #L31 - L35 were not covered by tests

matches = [ngram.search_ngram(p) for p in peps]
num_marches = sum([len(m) for m in matches])

Check warning on line 38 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L37-L38

Added lines #L37 - L38 were not covered by tests

peptide_col = [None] * num_marches
protein_col = [None] * num_marches

Check warning on line 41 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L40-L41

Added lines #L40 - L41 were not covered by tests

i = 0
for p, m in zip(peps, matches):
for n in m:
peptide_col[i] = p
protein_col[i] = n
i += 1

Check warning on line 48 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L43-L48

Added lines #L43 - L48 were not covered by tests

df = pd.DataFrame(

Check warning on line 50 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L50

Added line #L50 was not covered by tests
{"PeptideSeq": peptide_col, "ProteinAccession": protein_col}
)
df["isDecoy"] = False
conn = sqlite3.connect(speclib)
conn.execute("DROP TABLE IF EXISTS peptidetoprotein")
conn.execute(

Check warning on line 56 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L53-L56

Added lines #L53 - L56 were not covered by tests
(
"CREATE TABLE peptidetoprotein "
"(PeptideSeq string not null, "
"isDecoy boolean, "
"ProteinAccession string not null)"
)
)
df.to_sql("peptidetoprotein", conn, if_exists="append", index=False)
conn.close()

Check warning on line 65 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L64-L65

Added lines #L64 - L65 were not covered by tests


@click.command()
@click.argument("mokapot_proteins", type=click.Path(exists=True))
@click.argument("fasta_file", type=click.Path(exists=True))
@click.argument("speclib", type=click.Path(exists=True))
@click.option(

Check warning on line 72 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L68-L72

Added lines #L68 - L72 were not covered by tests
"--ngram_size",
type=int,
default=4,
help=(
"Size of the chunks that will be used to build the n-gram,"
" should be smaller than the smallest peptide to be searched."
" Longer sequences should give a more unique aspect to it"
" but a larger index is built.",
),
)
@click.option(

Check warning on line 83 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L83

Added line #L83 was not covered by tests
"--q_threshold",
type=float,
default=0.1,
help="Threshold for mokapot q-value.",
)
def add_peptidetoprotein(

Check warning on line 89 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L89

Added line #L89 was not covered by tests
mokapot_proteins,
fasta_file,
speclib,
ngram_size=4,
q_threshold=0.1,
):
"""Add peptidetoprotein table to dlib/elib file."""
_add_peptidetoprotein(

Check warning on line 97 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L97

Added line #L97 was not covered by tests
mokapot_proteins=mokapot_proteins,
fasta_file=fasta_file,
speclib=speclib,
ngram_size=ngram_size,
q_threshold=q_threshold,
)


if __name__ == "__main__":
add_peptidetoprotein()

Check warning on line 107 in petasus/scripts/add_peptidetoprotein.py

View check run for this annotation

Codecov / codecov/patch

petasus/scripts/add_peptidetoprotein.py#L106-L107

Added lines #L106 - L107 were not covered by tests
48 changes: 23 additions & 25 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ classifiers = [
]
requires-python = ">=3.10"
dependencies = [
"click",
"click", # General
"loguru",
"polars",
"pyteomics",
"lxml",
"appdirs",
"polars", # Data science
"pyarrow",
"pandas",
"numpy",
"numba",
"pyarrow",
"hdf5plugin>=3.10.0",
"ms2ml @ git+https://github.com/wfondrie/ms2ml.git@encyclopedia-update"
"lark",
"hdf5plugin>=3.10.0", # Proteomics
"ms2ml>=0.0.45",
"pyteomics",
"psims",
"mokapot",
"spectrum_utils>=0.4.2",
]
dynamic = ["version"]

Expand All @@ -42,6 +48,16 @@ Homepage = "https://github.com/TalusBio/search2dlib"
[project.optional-dependencies]
dev = [
"pre-commit>=2.7.1",
"black",
"ruff",
]
test = [
"pytest",
"pytest-cov",
"pytest-datadir",
]
build = [
"wheel",
]

[project.scripts]
Expand All @@ -65,22 +81,4 @@ select = ["E", "F", "T20"] # T20 is for print() statements.
line-length = 79
target-version = ['py310']
include = '\.pyi?$'
exclude = '''

(
/(
\.eggs # exclude a few common directories in the
| \.git # root of the project
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
)/
| foo.py # also separately exclude a file named foo.py in
# the root of the project
)
'''
# Black excludes files in the .gitignore by default
Loading