annotate digest.py @ 9:4d3ac66875d2 draft default tip

Uploaded
author jjohnson
date Thu, 14 Dec 2017 13:35:00 -0500
parents a8218b11216f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
1 # Copyright 2012 Anton Goloborodko, Lev Levitsky
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
2 #
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
3 # Licensed under the Apache License, Version 2.0 (the "License");
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
4 # you may not use this file except in compliance with the License.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
5 # You may obtain a copy of the License at
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
6 #
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
7 # http://www.apache.org/licenses/LICENSE-2.0
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
8 #
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
9 # Unless required by applicable law or agreed to in writing, software
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
10 # distributed under the License is distributed on an "AS IS" BASIS,
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
12 # See the License for the specific language governing permissions and
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
13 # limitations under the License.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
14
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
15 import re
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
16 from collections import deque
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
17 import itertools as it
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
18
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
19 def cleave(sequence, rule, missed_cleavages=0, min_length=None):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
20 """Cleaves a polypeptide sequence using a given rule.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
21
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
22 Parameters
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
23 ----------
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
24 sequence : str
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
25 The sequence of a polypeptide.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
26
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
27 .. note::
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
28 The sequence is expected to be in one-letter uppercase notation.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
29 Otherwise, some of the cleavage rules in :py:data:`expasy_rules`
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
30 will not work as expected.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
31
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
32 rule : str or compiled regex
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
33 A regular expression describing the site of cleavage. It is recommended
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
34 to design the regex so that it matches only the residue whose C-terminal
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
35 bond is to be cleaved. All additional requirements should be specified
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
36 using `lookaround assertions
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
37 <http://www.regular-expressions.info/lookaround.html>`_.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
38 :py:data:`expasy_rules` contains cleavage rules for popular cleavage agents.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
39 missed_cleavages : int, optional
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
40 Maximum number of allowed missed cleavages. Defaults to 0.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
41 min_length : int or None, optional
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
42 Minimum peptide length. Defaults to :py:const:`None`.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
43
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
44 ..note ::
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
45 This checks for string length, which is only correct for one-letter
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
46 notation and not for full *modX*. Use :py:func:`length` manually if
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
47 you know what you are doing and apply :py:func:`cleave` to *modX*
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
48 sequences.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
49
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
50 Returns
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
51 -------
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
52 out : set
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
53 A set of unique (!) peptides.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
54
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
55 Examples
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
56 --------
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
57 >>> cleave('AKAKBK', expasy_rules['trypsin'], 0) == {'AK', 'BK'}
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
58 True
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
59 >>> cleave('GKGKYKCK', expasy_rules['trypsin'], 2) == \
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
60 {'CK', 'GKYK', 'YKCK', 'GKGK', 'GKYKCK', 'GK', 'GKGKYK', 'YK'}
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
61 True
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
62
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
63 """
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
64 return set(_cleave(sequence, rule, missed_cleavages, min_length))
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
65
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
66 def _cleave(sequence, rule, missed_cleavages=0, min_length=None):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
67 """Like :py:func:`cleave`, but the result is a list. Refer to
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
68 :py:func:`cleave` for explanation of parameters.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
69 """
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
70 peptides = []
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
71 ml = missed_cleavages+2
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
72 trange = range(ml)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
73 cleavage_sites = deque([0], maxlen=ml)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
74 cl = 1
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
75 for i in it.chain([x.end() for x in re.finditer(rule, sequence)],
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
76 [None]):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
77 cleavage_sites.append(i)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
78 if cl < ml:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
79 cl += 1
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
80 for j in trange[:cl-1]:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
81 seq = sequence[cleavage_sites[j]:cleavage_sites[-1]]
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
82 if seq:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
83 if min_length is None or len(seq) >= min_length:
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
84 peptides.append(seq)
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
85 return peptides
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
86
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
87 def num_sites(sequence, rule, **kwargs):
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
88 """Count the number of sites where `sequence` can be cleaved using
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
89 the given `rule` (e.g. number of miscleavages for a peptide).
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
90
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
91 Parameters
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
92 ----------
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
93 sequence : str
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
94 The sequence of a polypeptide.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
95 rule : str or compiled regex
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
96 A regular expression describing the site of cleavage. It is recommended
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
97 to design the regex so that it matches only the residue whose C-terminal
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
98 bond is to be cleaved. All additional requirements should be specified
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
99 using `lookaround assertions
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
100 <http://www.regular-expressions.info/lookaround.html>`_.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
101 labels : list, optional
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
102 A list of allowed labels for amino acids and terminal modifications.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
103
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
104 Returns
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
105 -------
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
106 out : int
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
107 Number of cleavage sites.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
108 """
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
109 return len(_cleave(sequence, rule, **kwargs)) - 1
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
110
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
111 expasy_rules = {
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
112 'arg-c': r'R',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
113 'asp-n': r'\w(?=D)',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
114 'bnps-skatole' : r'W',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
115 'caspase 1': r'(?<=[FWYL]\w[HAT])D(?=[^PEDQKR])',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
116 'caspase 2': r'(?<=DVA)D(?=[^PEDQKR])',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
117 'caspase 3': r'(?<=DMQ)D(?=[^PEDQKR])',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
118 'caspase 4': r'(?<=LEV)D(?=[^PEDQKR])',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
119 'caspase 5': r'(?<=[LW]EH)D',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
120 'caspase 6': r'(?<=VE[HI])D(?=[^PEDQKR])',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
121 'caspase 7': r'(?<=DEV)D(?=[^PEDQKR])',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
122 'caspase 8': r'(?<=[IL]ET)D(?=[^PEDQKR])',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
123 'caspase 9': r'(?<=LEH)D',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
124 'caspase 10': r'(?<=IEA)D',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
125 'chymotrypsin high specificity' : r'([FY](?=[^P]))|(W(?=[^MP]))',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
126 'chymotrypsin low specificity':
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
127 r'([FLY](?=[^P]))|(W(?=[^MP]))|(M(?=[^PY]))|(H(?=[^DMPW]))',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
128 'clostripain': r'R',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
129 'cnbr': r'M',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
130 'enterokinase': r'(?<=[DE]{3})K',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
131 'factor xa': r'(?<=[AFGILTVM][DE]G)R',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
132 'formic acid': r'D',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
133 'glutamyl endopeptidase': r'E',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
134 'granzyme b': r'(?<=IEP)D',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
135 'hydroxylamine': r'N(?=G)',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
136 'iodosobenzoic acid': r'W',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
137 'lysc': r'K',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
138 'ntcb': r'\w(?=C)',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
139 'pepsin ph1.3': r'((?<=[^HKR][^P])[^R](?=[FLWY][^P]))|'
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
140 r'((?<=[^HKR][^P])[FLWY](?=\w[^P]))',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
141 'pepsin ph2.0': r'((?<=[^HKR][^P])[^R](?=[FL][^P]))|'
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
142 r'((?<=[^HKR][^P])[FL](?=\w[^P]))',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
143 'proline endopeptidase': r'(?<=[HKR])P(?=[^P])',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
144 'proteinase k': r'[AEFILTVWY]',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
145 'staphylococcal peptidase i': r'(?<=[^E])E',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
146 'thermolysin': r'[^DE](?=[AFILMV])',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
147 'thrombin': r'((?<=G)R(?=G))|'
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
148 r'((?<=[AFGILTVM][AFGILTVWA]P)R(?=[^DE][^DE]))',
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
149 'trypsin': r'([KR](?=[^P]))|((?<=W)K(?=P))|((?<=M)R(?=P))'
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
150 }
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
151 """
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
152 This dict contains regular expressions for cleavage rules of the most
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
153 popular proteolytic enzymes. The rules were taken from the
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
154 `PeptideCutter tool
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
155 <http://ca.expasy.org/tools/peptidecutter/peptidecutter_enzymes.html>`_
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
156 at Expasy.
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
157 """
a8218b11216f Uploaded
jjohnson
parents:
diff changeset
158