<jupyter_start><jupyter_text>Cleavage Site and TAP Prediction This tutorial illustrates the use of epytope to predict the steps of the HLA-I antigen processing pathway including proteasomal cleavage and TAP transport. epytope offers a long list of prediction methods and was  designed in such a way that extending epytope with your favorite method is easy.This tutorial will entail:- Simple cleavage site/fragment prediction from a list of peptide sequences and protein sequences- Simple TAP prediction methods- Consensus prediction of proteasomal cleavage, TAP, and HLA binding to model the complete antigen processing pathway<jupyter_code>%matplotlib inline
%reload_ext autoreload
%autoreload 2

from epytope.Core import Protein, Peptide, Allele
from epytope.Core import generate_peptides_from_proteins
from epytope.IO import read_fasta
from epytope.CleavagePrediction import CleavageSitePredictorFactory, CleavageFragmentPredictorFactory
from epytope.TAPPrediction import TAPPredictorFactory<jupyter_output><empty_output><jupyter_text>Chapter 1: Cleavage Prediction epytope offers a comprehensive list of state-of-the art proteasomal cleavage prediction methods. Usually one distinguishes cleavage methods into cleavage site and cleavage fragment prediction. Cleavage site prediction methods predict each possible cleavage site within a given amino acid sequence, whereas cleavage fragment prediction methods predict the likelihood of a peptide fragment of being a product of proteasomal cleavage. Additionally, the source of the training data are often also quite different. The majority of prediction tools was train on in vitro data of up to three fully analyzed proteins and distinguish between constitutive and immunoproteasomal cleavage. Some prediction methods use natural HLA ligands as training data as they are products of antigen processing pathway and therefore also of cleavage events.But all methods start with reading in protein sequences. epytope offers several ways of defining Proteins. We can either directly initialize a `epytope.Core.Protein` object by specifying a amino acid sequence and optionally a progenitor gene and transcript id, as well as the progenitor `epytope.Core.Transcript` object, or we can directly read in proteins from FASTA files by using `epytope.IO.read_fasta`.<jupyter_code>protein = Protein("AAAAAAAAAAA", gene_id="Dummy", transcript_id="Dummy")
proteins = read_fasta("./data/proteins.fasta", id_position=3, in_type=Protein)
proteins<jupyter_output><empty_output><jupyter_text>Once we have a protein sequence to work with, we can specify the cleavage site prediction method of our choice. epytope offers one entry point for each type of prediction methods via so called factories. For cleavage site prediction it is `CleavageSitePredictorFactory`. To get an overview which prediction methods are currently implemented, we can use `CleavageSitePredictorFactory` as follows:<jupyter_code>for name,version in CleavageSitePredictorFactory.available_methods().items():
    print(name, ",".join(version))<jupyter_output>pcm 1.0
proteasmm_c 1.0
proteasmm_i 1.0
netchop 3.1<jupyter_text>Lets select `PCM` for example and make predictions:<jupyter_code>pcm = CleavageSitePredictorFactory("PCM")
site_result = pcm.predict(proteins)
site_result<jupyter_output><empty_output><jupyter_text>To specify a particular version of a prediction method, we can use the flag `version=""` when calling the PredictorFactories. If we do not specify any version, epytope will initialize the most recent version that is supported.<jupyter_code>pcm = CleavageSitePredictorFactory("PCM", version="1.0")
site_result = pcm.predict(proteins)
site_result.head()<jupyter_output><empty_output><jupyter_text>External tools like `NetChop` offer two additional flags when calling `.predict()`, `command="/path/to/binary"` and `options="command options"`. `command=""` specifies the path to an alternative binary that should be used instead of the one which is globally registered. With options you can specify additional commands that will directly be passed to the command line call without any sanity checks. For CleavageFragment prediction we first have to generate peptides from protein sequences with `epytope.Core.generate_peptides_from_proteins`. epytope currently supports only one CleavageFragment prediction methods proposed by Ginodi et al. (Ginodi, et al.(2008) Bioinformatics 24(4)), which supports only 11mers (9mer epitopes and two flaking amino acids).<jupyter_code>pep = generate_peptides_from_proteins(proteins, 11)
CleavageFragmentPredictorFactory("Ginodi").predict(pep).head()<jupyter_output><empty_output><jupyter_text>The result object is based on pandas' `DataFrame`, thus all possibilities of manipulating the results pandas offers are possible, including rudimentary plotting capabilities.<jupyter_code>import matplotlib.pyplot as plt

f, a = plt.subplots(len(site_result.index.levels[0]),1)

for i,r in enumerate(site_result.index.levels[0]):
    site_result.xs(r).plot(kind='bar', ax=a[i]).set_xticklabels(site_result.loc[(r,slice(None)),"Seq"], rotation=0)<jupyter_output><empty_output><jupyter_text>We also can combine several prediction results of the same type via `CleavageSitePredictionResults.merge_results` (Note this function returns a merge Result DataFrame):<jupyter_code>import pandas as pd
import numpy

site_result2 = CleavageSitePredictorFactory("proteasmm_c").predict(proteins)
merged_result = site_result.merge_results([site_result2])
merged_result.head(7)<jupyter_output><empty_output><jupyter_text>We can also filter the results based on multiple expressions with `CleavageSitePredictionResults.filter_result`.<jupyter_code>comp = lambda x,y: x > y
expressions=[("pcm",comp,0)]

merged_result.filter_result(expressions)<jupyter_output><empty_output><jupyter_text>Chapter 2: TAP prediction  epytope offers only limited prediction methods for TAP prediction, due to lack of publicly available methods.<jupyter_code>for name,version in TAPPredictorFactory.available_methods().items():
    print(name, ",".join(version))<jupyter_output>doytchinova 1.0
smmtap 1.0<jupyter_text>For TAP prediction, we first have to generate peptides. Lets take the proteins we already imported and generate 9mers, as these two methods only support 9mer peptides.<jupyter_code>pep = list(generate_peptides_from_proteins(proteins,9))

tap_result = TAPPredictorFactory("smmtap").predict(pep[:15])
tap_result.head()<jupyter_output><empty_output><jupyter_text>Again we can do all rudimentary operations on the result object as with the cleavage result objects, including merging and filtering.<jupyter_code>tap_result2 = TAPPredictorFactory("Doytchinova").predict(pep[:15])
tap_result.merge_results(tap_result2)
from operator import ge

tap_result.filter_result([("smmtap",ge, -1)])<jupyter_output><empty_output><jupyter_text>Chapter 3: Consensus prediction for natural ligand prediction Proteasomal cleavage, TAP prediction, as well as HLA binding prediction have been combined to increase the specificity of predicting natural processed HLA ligands. One example is `WAPP` (Dönnes, et al.(2005). Protein Science 14(8)), which uses proteasomal cleavage and TAP prediction methods to filter for possibly processed peptides.The same approach can be implemented with epytope. We start again with the two protein sequences and exemplify the workflows for CleavageFragmentPrediction and CleavageSitePrediction methods.  Antigen processing prediction with CleavageFragment prediction We will use `PSSMGinodi`, `SVMTAP`, and `UniTope` for prediction for HLA-A*02:01. For `PSSMGionid` and `SVMTAP` we use a threshold of -15 and -30 respectively.<jupyter_code>from operator import ge
from epytope.Core import Allele
from epytope.EpitopePrediction import EpitopePredictorFactory

allele = Allele("HLA-A*02:01")

pep = list(generate_peptides_from_proteins(proteins,11))
print("Number of peptides: ", len(pep))

#cleavage prediction and filtering
df_cl = CleavageFragmentPredictorFactory("Ginodi").predict(pep).filter_result(("ginodi",ge,-15))
print("Number of peptides after proteasomal cleavage: ", len(df_cl))

#tap prediction and filtering
df_tap = TAPPredictorFactory("smmtap").predict(df_cl.index).filter_result(("smmtap",ge,1))
print("Number of peptides after TAP transport: ", len(df_tap))

#epitope prediction and filtering
df_epi = EpitopePredictorFactory("smm").predict(df_tap.index,alleles=allele)
df_epi<jupyter_output>Number of peptides:  50
Number of peptides after proteasomal cleavage:  31
Number of peptides after TAP transport:  13<jupyter_text>Based on this analysis, there are no natural ligands predicted for the two test proteins.  Antigen processing prediction with CleavageSite prediction We will use `PCM`, `SVMTAP`, and `SVMHC` for prediction for HLA-A*02:01 like in the original work of WAPP. For `PCM` and `SVMTAP` we use a threshold of −4.8 and −27 respectively.<jupyter_code>from operator import ge
from epytope.Core import Allele,Protein
from epytope.EpitopePrediction import EpitopePredictorFactory

allele = Allele("HLA-A*02:01")



#cleavage prediction and filtering
df_cl = CleavageSitePredictorFactory("PCM").predict(proteins).filter_result(("pcm",ge,-4.8))

print("Number of peptides after proteasomal cleavage: ", len(df_cl))

#since we only predicted possible cleavage site, we now have to generate all possible peptides
#peptides
pep_dic = {}
for p in proteins:
    for i in df_cl.loc[(p.transcript_id,slice(None)),:].index.codes[1]:
        if i-8>=0:
            seq = str(p[i-8:i+1])
            pep_dic.setdefault(seq, []).append(p)
peps = [Peptide(seq, protein_pos={pp:[0] for pp in p}) for seq, p in pep_dic.items()]


#tap prediction and filtering
df_tap = TAPPredictorFactory("smmtap").predict(peps).filter_result(("smmtap",ge,1))
print("Number of peptides after TAP transport: ", len(df_tap))


#epitope prediction and filtering
df_epi = EpitopePredictorFactory("smm").predict(df_tap.index,alleles=allele).filter_result(("smm",ge,100000.0))
df_epi<jupyter_output>Number of peptides after proteasomal cleavage:  62
Number of peptides after TAP transport:  17
{'fraction_non_alphanumeric': 0.07763729751086527, 'fraction_numerical': 0.01096404583168708, 'mean_word_length': 6.916340891321345, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '419635', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3167, 'n_tokens_neox': 2846, 'n_words': 1021, 'path': 'epytope/tutorials/CleavageAndTAPPrediction.ipynb', 'repo_name': 'christopher-mohr/epytope', 'size': '10124'}