from kprot.uniprot import get_uniprot_seq, get_uniprot_features, get_uniprot_kd, get_uniprot_type, apply_mut_single, apply_mut_complex, compare_seqkprot
kprot is a Python package for protein related analysis.
The sections below introduce the package and show quick-start examples from each exported module.
Installation
pip install kprotQuick start
The examples below follow the notebooks under nbs/ in order. Each function example lives in its own cell and starts with a short comment derived from the function docstring.
01 utils
import re
from functools import lru_cache
import requests
from Bio.Align import PairwiseAligner# Queries the UniProt database to retrieve the protein sequence for a given UniProt ID.
get_uniprot_seq("P04626")[:60]'MELAALCRWGLLLALLPPGAASTQVCTGTDMKLRLPASPETHLDMLRHLYQGCQVVQGNL'
# Given a UniProt ID, fetch the protein metadata and annotated features.
get_uniprot_features("P04626").keys()dict_keys(['uniprot_id', 'protein_name', 'gene_name', 'features'])
# Query Domain: Protein kinase entries based on a UniProt ID.
get_uniprot_kd("P04626")[{'uniprot_id': 'P04626',
'protein_name': 'Receptor tyrosine-protein kinase erbB-2',
'gene_name': 'ERBB2',
'start': 720,
'end': 987,
'description': 'Protein kinase',
'sequence': 'LRKVKVLGSGAFGTVYKGIWIPDGENVKIPVAIKVLRENTSPKANKEILDEAYVMAGVGSPYVSRLLGICLTSTVQLVTQLMPYGCLLDHVRENRGRLGSQDLLNWCMQIAKGMSYLEDVRLVHRDLAARNVLVKSPNHVKITDFGLARLLDIDETEYHADGGKVPIKWMALESILRRRFTHQSDVWSYGVTVWELMTFGAKPYDGIPAREIPDLLEKGERLPQPPICTIDVYMIMVKCWMIDSECRPRFRELVSEFSRMARDPQRFV'}]
# Get region sequences for a UniProt feature type.
get_uniprot_type("P04626", "Signal")[:1][{'uniprot_id': 'P04626',
'type': 'Signal',
'protein_name': 'Receptor tyrosine-protein kinase erbB-2',
'gene_name': 'ERBB2',
'start': 1,
'end': 22,
'description': '',
'sequence': 'MELAALCRWGLLLALLPPGAAS'}]
# Apply point mutations to a protein sequence.
apply_mut_single("MEEPQ", "M1A", "E2S")Converted: M1A
Converted: E2S
'ASEPQ'
# Apply a composite mutation string to a protein sequence.
her2_seq = "LRKVKVLGSGAFGTVYKGIWIPDGENVKIPVAIKVLRENTSPKANKEILDEAYVMAGVGSPYVSRLLGICLTSTVQLVTQLMPYGCLLDHVRENRGRLGSQDLLNWCMQIAKGMSYLEDVRLVHRDLAARNVLVKSPNHVKITDFGLARLLDIDETEYHADGGKVPIKWMALESILRRRFTHQSDVWSYGVTVWELMTFGAKPYDGIPAREIPDLLEKGERLPQPPICTIDVYMIMVKCWMIDSECRPRFRELVSEFSRMARDPQRFV"
apply_mut_complex(her2_seq, "G776delinsVC/S783C", start_pos=720)[:25]'LRKVKVLGSGAFGTVYKGIWIPDGE'
# Align two protein sequences and summarize the differences.
print(compare_seq("MEEPQ", "MAEPQ", return_text=True))Original 1-5 : MEEPQ
Mutant : MAEPQ
^
Differences:
substitution at 2: E → A
02 onehot kmeans
from kprot.onehot import onehot_encode, onehot_encode_df, run_kmeans, filter_range_columns, get_clusters_elbowimport pandas as pd
# Set up the objects used by the examples below.
df = pd.DataFrame(
{
"site_seq": [
"AASTYGVAKR",
"GGSTYGVAKR",
"AASTFGVAKR",
"PPSTYGVAKK",
"VVSTYGIARR",
]
}
)
df.shape
df.head()| site_seq | |
|---|---|
| 0 | AASTYGVAKR |
| 1 | GGSTYGVAKR |
| 2 | AASTFGVAKR |
| 3 | PPSTYGVAKK |
| 4 | VVSTYGIARR |
# One-hot encode aligned protein sequence windows.
onehot_encode(df["site_seq"]).head()| -20A | -20G | -20P | -20V | -19A | -19G | -19P | -19V | -18S | -17T | -16F | -16Y | -15G | -14I | -14V | -13A | -12K | -12R | -11K | -11R | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 1.0 | 0.0 | 1.0 | 1.0 | 0.0 | 1.0 | 1.0 | 1.0 | 0.0 | 0.0 | 1.0 |
| 1 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 1.0 | 1.0 | 0.0 | 1.0 | 1.0 | 0.0 | 1.0 | 1.0 | 1.0 | 0.0 | 0.0 | 1.0 |
| 2 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 1.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 1.0 | 1.0 | 0.0 | 0.0 | 1.0 |
| 3 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 1.0 | 1.0 | 0.0 | 1.0 | 1.0 | 0.0 | 1.0 | 1.0 | 1.0 | 0.0 | 1.0 | 0.0 |
| 4 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 1.0 | 1.0 | 0.0 | 1.0 | 1.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 |
# One-hot encode a sequence column from a dataframe.
onehot = onehot_encode_df(df, seq_col="site_seq")
onehot.head()| -20A | -20G | -20P | -20V | -19A | -19G | -19P | -19V | -18S | -17T | -16F | -16Y | -15G | -14I | -14V | -13A | -12K | -12R | -11K | -11R | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 1.0 | 0.0 | 1.0 | 1.0 | 0.0 | 1.0 | 1.0 | 1.0 | 0.0 | 0.0 | 1.0 |
| 1 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 1.0 | 1.0 | 0.0 | 1.0 | 1.0 | 0.0 | 1.0 | 1.0 | 1.0 | 0.0 | 0.0 | 1.0 |
| 2 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 1.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 1.0 | 1.0 | 0.0 | 0.0 | 1.0 |
| 3 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 1.0 | 1.0 | 0.0 | 1.0 | 1.0 | 0.0 | 1.0 | 1.0 | 1.0 | 0.0 | 1.0 | 0.0 |
| 4 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 | 0.0 | 0.0 | 1.0 | 1.0 | 1.0 | 0.0 | 1.0 | 1.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 | 0.0 | 1.0 |
# Fit KMeans to one-hot encoded features and return the assigned labels.
run_kmeans(onehot, n=2)array([0, 0, 0, 0, 1], dtype=int32)
# Filter one-hot columns to a position window around the center residue.
onehot_10 = filter_range_columns(onehot, low=-10, high=10)
onehot_10.head()| 0 |
|---|
| 1 |
| 2 |
| 3 |
| 4 |
# Plot the elbow curve for a range of KMeans cluster counts.
get_clusters_elbow(onehot, max_cluster=5, interval=2)
03 embeddings
from kprot.embeddings import get_esm, get_t5, get_t5_bfdimport pandas as pd
# Set up the objects used by the examples below.
df = pd.DataFrame(
{
"sequence": [
"MKWVTFISLLLLFSSAYSRGVFRRDAHKSEVAHRFKDLGE",
"GSSHHHHHHSSGLVPRGSHMNNIRRVAILAALVAGQALA",
]
}
)
df.shape
df.head()
run_model_examples = False# Run the example.
df = pd.DataFrame(
{
"sequence": [
"MKWVTFISLLLLFSSAYSRGVFRRDAHKSEVAHRFKDLGE",
"GSSHHHHHHSSGLVPRGSHMNNIRRVAILAALVAGQALA",
]
}
)
df.shape(2, 1)
# Extract ESM2 embeddings (mean pooled per sequence).
if run_model_examples:
get_esm(df, "sequence").head()
else:
print("Skipped get_esm example: set run_model_examples=True to allow model downloads.")Skipped get_esm example: set run_model_examples=True to allow model downloads.
# Extract ProtT5-XL-uniref50 embeddings from protein sequences in a dataframe.
if run_model_examples:
get_t5(df, "sequence").head()
else:
print("Skipped get_t5 example: set run_model_examples=True to allow model downloads.")Skipped get_t5 example: set run_model_examples=True to allow model downloads.
# Extract ProtT5-XL-BFD embeddings from protein sequences in a dataframe.
if run_model_examples:
get_t5_bfd(df, "sequence").head()
else:
print("Skipped get_t5_bfd example: set run_model_examples=True to allow model downloads.")Skipped get_t5_bfd example: set run_model_examples=True to allow model downloads.