/
protein_residues.jl
356 lines (282 loc) · 10.8 KB
/
protein_residues.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#
# Data for natural protein residues
#
@kwdef struct ProteinResidue
name::String
three_letter_code::String
one_letter_code::String
type::String
polar::Bool
hydrophobic::Bool
mono_isotopic_mass::Float64
mass::Float64
charge::Int
custom::Bool = true
end
#! format: off
protein_residues = Dict{String,ProteinResidue}(
"ALA" => ProteinResidue("Alanine", "ALA", "A", "Aliphatic", false, false, 71.037114, 71.0779, 0, false),
"ARG" => ProteinResidue("Arginine", "ARG", "R", "Basic", true, false, 156.101111, 156.1857, 1, false),
"ASN" => ProteinResidue("Asparagine", "ASN", "N", "Amide", true, false, 114.042927, 114.1026, 0, false),
"ASP" => ProteinResidue("Aspartic acid", "ASP", "D", "Acidic", true, false, 115.026943, 115.0874, -1, false),
"CYS" => ProteinResidue("Cysteine", "CYS", "C", "Sulfuric", false, false, 103.009185, 103.1429, 0, false),
"GLN" => ProteinResidue("Glutamine", "GLN", "Q", "Amide", true, false, 128.058578, 128.1292, 0, false),
"GLU" => ProteinResidue("Glutamic acid", "GLU", "E", "Acidic", true, false, 129.042593, 129.1140, -1, false),
"GLY" => ProteinResidue("Glycine", "GLY", "G", "Aliphatic", false, false, 57.021464, 57.0513, 0, false),
"HIS" => ProteinResidue("Histidine", "HIS", "H", "Aromatic", true, false, 137.058912, 137.1393, 0, false),
"ILE" => ProteinResidue("Isoleucine", "ILE", "I", "Aliphatic", false, true, 113.084064, 113.1576, 0, false),
"LEU" => ProteinResidue("Leucine", "LEU", "L", "Aliphatic", false, true, 113.084064, 113.1576, 0, false),
"LYS" => ProteinResidue("Lysine", "LYS", "K", "Basic", true, false, 128.094963, 128.1723, 1, false),
"MET" => ProteinResidue("Methionine", "MET", "M", "Sulfuric", false, false, 131.040485, 131.1961, 0, false),
"PHE" => ProteinResidue("Phenylalanine", "PHE", "F", "Aromatic", false, true, 147.068414, 147.1739, 0, false),
"PRO" => ProteinResidue("Proline", "PRO", "P", "Cyclic", false, false, 97.052764, 97.1152, 0, false),
"SER" => ProteinResidue("Serine", "SER", "S", "Hydroxylic", true, false, 87.032028, 87.07730, 0, false),
"THR" => ProteinResidue("Threonine", "THR", "T", "Hydroxylic", true, false, 101.047679, 101.1039, 0, false),
"TRP" => ProteinResidue("Tryptophan", "TRP", "W", "Aromatic", false, true, 186.079313, 186.2099, 0, false),
"TYR" => ProteinResidue("Tyrosine", "TYR", "Y", "Aromatic", true, false, 163.063320, 163.1733, 0, false),
"VAL" => ProteinResidue("Valine", "VAL", "V", "Aliphatic", false, true, 99.068414, 99.1311, 0, false),
# Alternate protonation states for CHARMM and AMBER
"ASPP" => ProteinResidue("Aspartic acid (protonated)", "ASP", "D", "Acidic", true, false, 115.026943, 115.0874, 0, false),
"GLUP" => ProteinResidue("Glutamic acid (protonated)", "GLU", "E", "Acidic", true, false, 129.042593, 129.1140, 0, false),
"HSD" => ProteinResidue("Histidine (D)", "HIS", "H", "Aromatic", true, false, 137.058912, 137.1393, 0, false),
"HSE" => ProteinResidue("Histidine (E)", "HIS", "H", "Aromatic", true, false, 137.058912, 137.1393, 0, false),
"HSP" => ProteinResidue("Histidine (doubly protonated)", "HIS", "H", "Aromatic", true, false, 137.058912, 137.1393, 1, false),
"HID" => ProteinResidue("Histidine (D)", "HIS", "H", "Aromatic", true, false, 137.058912, 137.1393, 0, false),
"HIE" => ProteinResidue("Histidine (E)", "HIS", "H", "Aromatic", true, false, 137.058912, 137.1393, 0, false),
"HIP" => ProteinResidue("Histidine (doubly protonated)", "HIS", "H", "Aromatic", true, false, 137.058912, 137.1393, 1, false),
)
#! format: on
"""
threeletter(residue::String)
Function to return the three-letter natural-amino acid residue code from the one-letter
code or residue name. The function is case-insensitive.
# Examples
```julia-repl
julia> threeletter("A")
"ALA"
julia> threeletter("Aspartic acid")
"ASP"
julia> threeletter("HSD")
"HIS"
```
"""
function threeletter(residue::Union{String,Char})
# Convert to String if Char
code = "$residue"
key = _case_insensitve_check(code, protein_residues)
if !isnothing(key)
return protein_residues[key].three_letter_code
elseif length(code) == 1
return findfirst(r -> r.one_letter_code == code, protein_residues)
else
return findfirst(r -> r.name == code, protein_residues)
end
end
function _case_insensitve_check(code, protein_residues)
isnothing(code) && return code
if code in keys(protein_residues)
return code
elseif uppercase(code) in keys(protein_residues)
return uppercase(code)
end
return nothing
end
@testitem "threeletter" begin
@test threeletter("ALA") == "ALA"
@test threeletter("A") == "ALA"
@test threeletter('A') == "ALA"
@test threeletter("Alanine") == "ALA"
@test threeletter("GLUP") == "GLU"
@test threeletter("HSP") == "HIS"
@test isnothing(threeletter("XXX"))
end
"""
oneletter(residue::Union{String,Char})
Function to return a one-letter residue code from the three letter code or residue name. The function is case-insensitive.
### Examples
```julia-repl
julia> oneletter("ALA")
"A"
julia> oneletter("Glutamic acid")
"E"
```
"""
function oneletter(residue::Union{String,Char})
code = resname("$residue")
key = _case_insensitve_check(code, protein_residues)
if !isnothing(key)
return protein_residues[key].one_letter_code
else
return "X"
end
end
@testitem "oneletter" begin
@test oneletter("ALA") == "A"
@test oneletter("A") == "A"
@test oneletter('A') == "A"
@test oneletter("Alanine") == "A"
@test oneletter("GLUP") == "E"
@test oneletter("HSP") == "H"
@test oneletter("XXX") == "X"
end
"""
resname(residue::Union{String,Char})
Returns the residue name, given the one-letter code or residue name. Differently from
`threeletter`, this function will return the force-field name if available in the list
of protein residues.
# Examples
```julia-repl
julia> resname("ALA")
"ALA"
julia> resname("GLUP")
"GLUP"
```
"""
function resname(residue::Union{String,Char})
code = "$residue"
key = _case_insensitve_check(code, protein_residues)
if !isnothing(key)
return key
else
return threeletter(code)
end
end
@testitem "resname" begin
@test resname("ALA") == "ALA"
@test resname("A") == "ALA"
@test resname('A') == "ALA"
@test resname("Alanine") == "ALA"
@test resname("GLUP") == "GLUP"
@test resname("HSP") == "HSP"
@test isnothing(resname("XXX"))
end
"""
residuename(residue::Union{String,Char})
Function to return the long residue name from other residue codes. The function is case-insensitive.
### Examples
```julia-repl
julia> residuename("A")
"Alanine"
julia> residuename("Glu")
"Glutamic Acid"
```
"""
function residuename(residue::Union{String,Char})
code = resname(residue)
key = _case_insensitve_check(code, protein_residues)
if !isnothing(key)
return protein_residues[key].name
else
return nothing
end
end
@testitem "residuename" begin
@test residuename("A") == "Alanine"
@test residuename("GLUP") == "Glutamic acid (protonated)"
@test residuename("Ala") == "Alanine"
end
"""
Sequence
Wrapper for strings, or vectors of chars, strings, or residue names, to dispatch on
functions that operate on amino acid sequences.
# Example
```julia-repl
julia> seq = ["Alanine", "Glutamic acid", "Glycine"];
julia> mass(Sequence(seq))
257.2432
julia> seq = "AEG";
julia> mass(Sequence(seq))
257.2432
```
"""
struct Sequence{T}
s::T
end
"""
mass(s::Sequence)
Returns the mass of a sequence of amino acids, given a `Sequence` struct type.
# Examples
```julia-repl
julia> seq = ["Alanine", "Glutamic acid", "Glycine"];
julia> mass(Sequence(seq))
257.2432
julia> seq = "AEG";
julia> mass(Sequence(seq))
257.2432
julia> seq = ["ALA", "GLU", "GLY"];
julia> mass(Sequence(seq))
257.2432
```
"""
function mass(s::Sequence)
m = 0.0
for aa in s.s
rname = resname(aa)
m += protein_residues[rname].mass
end
return m
end
@testitem "sequence mass" begin
@test mass(Sequence("AEG")) == 257.2432
@test mass(Sequence(["ALA", "GLU", "GLY"])) == 257.2432
@test mass(Sequence(["A", "E", "G"])) == 257.2432
@test mass(Sequence(['A', 'E', 'G'])) == 257.2432
@test mass(Sequence(["Alanine", "Glutamic acid", "Glycine"])) == 257.2432
end
"""
add_protein_residue!(resname::String, reference_residue::PDBTools.ProteinResidue)
Function to add a custom protein residue to the list of protein residues. The function will
return the `ProteinResidue` object that was added. To remove all custom protein residues
use `remove_custom_protein_residues!()`.
# Example
```jldoctest
julia> using PDBTools
julia> remove_custom_protein_residues!();
julia> add_protein_residue!("sA", PDBTools.protein_residues["ALA"])
PDBTools.ProteinResidue("sA", "ALA", "A", "Aliphatic", false, false, 71.037114, 71.0779, 0, true)
julia> isprotein(Atom(resname="sA"))
true
julia> remove_custom_protein_residues!(); # clean up
```
Here we repeteadly call `remove_custom_residues!()` to guarantee the proper execution of the
test codes, without any custom residues in the list of protein residues.
"""
function add_protein_residue!(resname::String, reference_residue::PDBTools.ProteinResidue)
if haskey(PDBTools.protein_residues, resname)
@warn """\n
Residue $resname already exists in the list of protein residues. Overwriting.
""" _file=nothing _line=nothing
end
PDBTools.protein_residues[resname] = PDBTools.ProteinResidue(
resname,
reference_residue.three_letter_code,
reference_residue.one_letter_code,
reference_residue.type,
reference_residue.polar,
reference_residue.hydrophobic,
reference_residue.mono_isotopic_mass,
reference_residue.mass,
reference_residue.charge,
true
)
return PDBTools.protein_residues[resname]
end
"""
remove_custom_protein_residues!()
Function to remove all custom protein residues from the list of protein residues.
# Example
```jldoctest
julia> using PDBTools
julia> remove_custom_protein_residues!(); # clean up
julia> add_protein_residue!("sA", PDBTools.protein_residues["ALA"])
PDBTools.ProteinResidue("sA", "ALA", "A", "Aliphatic", false, false, 71.037114, 71.0779, 0, true)
julia> isprotein(Atom(resname="sA"))
true
julia> remove_custom_protein_residues!();
julia> isprotein(Atom(resname="sA"))
false
```
Here we repeteadly call `remove_custom_residues!()` to guarantee the proper execution of the
test codes, without any custom residues in the list of protein residues.
"""
remove_custom_protein_residues!() = filter!(r -> !last(r).custom, protein_residues)