-
Notifications
You must be signed in to change notification settings - Fork 0
added protein n-gram #6
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
Draft
jspaezp
wants to merge
5
commits into
main
Choose a base branch
from
feature/annotate_proteins
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
| 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 | ||
|
|
||
| if candidates is None: | ||
| 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 | ||
| else: | ||
| kept += 1 | ||
| sequence = entry.sequence | ||
| if len(sequence) < ngram_size: | ||
| logger.warning( | ||
| f"Skipping {entry_name} because it is shorter than the n-gram size" | ||
| ) | ||
| continue | ||
|
|
||
| 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( | ||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
|
|
||
| def _add_peptidetoprotein( | ||
| 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[ | ||
| mokapot_proteins["mokapot q-value"] < q_threshold | ||
| ] | ||
|
|
||
| accessions_keep = get_protein_accessions( | ||
| mokapot_proteins["mokapot protein group"].tolist() | ||
| ) | ||
|
|
||
| ngram = ProteinNGram.from_fasta( | ||
| 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 | ||
|
|
||
| matches = [ngram.search_ngram(p) for p in peps] | ||
| num_marches = sum([len(m) for m in matches]) | ||
|
|
||
| peptide_col = [None] * num_marches | ||
| protein_col = [None] * num_marches | ||
|
|
||
| i = 0 | ||
| for p, m in zip(peps, matches): | ||
| for n in m: | ||
| peptide_col[i] = p | ||
| protein_col[i] = n | ||
| i += 1 | ||
|
|
||
| df = pd.DataFrame( | ||
| {"PeptideSeq": peptide_col, "ProteinAccession": protein_col} | ||
| ) | ||
| df["isDecoy"] = False | ||
| conn = sqlite3.connect(speclib) | ||
| conn.execute("DROP TABLE IF EXISTS peptidetoprotein") | ||
| conn.execute( | ||
| ( | ||
| "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() | ||
|
|
||
|
|
||
| @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( | ||
| "--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( | ||
| "--q_threshold", | ||
| type=float, | ||
| default=0.1, | ||
| help="Threshold for mokapot q-value.", | ||
| ) | ||
| def add_peptidetoprotein( | ||
| mokapot_proteins, | ||
| fasta_file, | ||
| speclib, | ||
| ngram_size=4, | ||
| q_threshold=0.1, | ||
| ): | ||
| """Add peptidetoprotein table to dlib/elib file.""" | ||
| _add_peptidetoprotein( | ||
| mokapot_proteins=mokapot_proteins, | ||
| fasta_file=fasta_file, | ||
| speclib=speclib, | ||
| ngram_size=ngram_size, | ||
| q_threshold=q_threshold, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| add_peptidetoprotein() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.