prodigy-prot 2.1.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env python
2
+ #
3
+ # This code is part of the binding affinity prediction tools distribution
4
+ # and governed by its license. Please see the LICENSE file that should
5
+ # have been included as part of this package.
6
+ #
7
+
8
+ """
9
+ Binding affinity prediction methods developed by the Bonvin Lab.
10
+ """
@@ -0,0 +1,131 @@
1
+ #!/usr/bin/env python
2
+ #
3
+ # This code is part of the binding affinity prediction tools distribution
4
+ # and governed by its license. Please see the LICENSE file that should
5
+ # have been included as part of this package.
6
+ #
7
+
8
+ """
9
+ Generic properties of amino acids required for the binding affinity
10
+ prediction methods.
11
+ """
12
+
13
+ __author__ = ["Anna Vangone", "Joao Rodrigues"]
14
+
15
+ aa_character_ic = {
16
+ "ALA": "A",
17
+ "CYS": "A", # ?
18
+ "GLU": "C",
19
+ "ASP": "C",
20
+ "GLY": "A",
21
+ "PHE": "A",
22
+ "ILE": "A",
23
+ "HIS": "C",
24
+ "LYS": "C",
25
+ "MET": "A",
26
+ "LEU": "A",
27
+ "ASN": "P",
28
+ "GLN": "P",
29
+ "PRO": "A",
30
+ "SER": "P",
31
+ "ARG": "C",
32
+ "THR": "P",
33
+ "TRP": "A",
34
+ "VAL": "A",
35
+ "TYR": "A",
36
+ }
37
+
38
+ aa_character_protorp = {
39
+ "ALA": "A",
40
+ "CYS": "P",
41
+ "GLU": "C",
42
+ "ASP": "C",
43
+ "GLY": "A",
44
+ "PHE": "A",
45
+ "ILE": "A",
46
+ "HIS": "P",
47
+ "LYS": "C",
48
+ "MET": "A",
49
+ "LEU": "A",
50
+ "ASN": "P",
51
+ "GLN": "P",
52
+ "PRO": "A",
53
+ "SER": "P",
54
+ "ARG": "C",
55
+ "THR": "P",
56
+ "TRP": "P",
57
+ "VAL": "A",
58
+ "TYR": "P",
59
+ }
60
+
61
+ # Scaling factors for relative ASA
62
+ # Calculated using extended ALA-X-ALA peptides
63
+ # Taken from NACCESS
64
+ rel_asa = {
65
+ "total": {
66
+ "ALA": 107.95,
67
+ "CYS": 134.28,
68
+ "ASP": 140.39,
69
+ "GLU": 172.25,
70
+ "PHE": 199.48,
71
+ "GLY": 80.10,
72
+ "HIS": 182.88,
73
+ "ILE": 175.12,
74
+ "LYS": 200.81,
75
+ "LEU": 178.63,
76
+ "MET": 194.15,
77
+ "ASN": 143.94,
78
+ "PRO": 136.13,
79
+ "GLN": 178.50,
80
+ "ARG": 238.76,
81
+ "SER": 116.50,
82
+ "THR": 139.27,
83
+ "VAL": 151.44,
84
+ "TRP": 249.36,
85
+ "TYR": 212.76,
86
+ },
87
+ "bb": {
88
+ "ALA": 38.54,
89
+ "CYS": 37.53,
90
+ "ASP": 37.70,
91
+ "GLU": 37.51,
92
+ "PHE": 35.37,
93
+ "GLY": 47.77,
94
+ "HIS": 35.80,
95
+ "ILE": 37.16,
96
+ "LYS": 37.51,
97
+ "LEU": 37.51,
98
+ "MET": 37.51,
99
+ "ASN": 37.70,
100
+ "PRO": 16.23,
101
+ "GLN": 37.51,
102
+ "ARG": 37.51,
103
+ "SER": 38.40,
104
+ "THR": 37.57,
105
+ "VAL": 37.16,
106
+ "TRP": 38.10,
107
+ "TYR": 35.38,
108
+ },
109
+ "sc": {
110
+ "ALA": 69.41,
111
+ "CYS": 96.75,
112
+ "ASP": 102.69,
113
+ "GLU": 134.74,
114
+ "PHE": 164.11,
115
+ "GLY": 32.33,
116
+ "HIS": 147.08,
117
+ "ILE": 137.96,
118
+ "LYS": 163.30,
119
+ "LEU": 141.12,
120
+ "MET": 156.64,
121
+ "ASN": 106.24,
122
+ "PRO": 119.90,
123
+ "GLN": 140.99,
124
+ "ARG": 201.25,
125
+ "SER": 78.11,
126
+ "THR": 101.70,
127
+ "VAL": 114.28,
128
+ "TRP": 211.26,
129
+ "TYR": 177.38,
130
+ },
131
+ }
@@ -0,0 +1,242 @@
1
+ #!/usr/bin/env python
2
+ #
3
+ # This code is part of the binding affinity prediction tools distribution
4
+ # and governed by its license. Please see the LICENSE file that should
5
+ # have been included as part of this package.
6
+ #
7
+
8
+ """
9
+ Functions to execute freesasa and parse its output.
10
+ """
11
+
12
+ from __future__ import division, print_function
13
+
14
+ import os
15
+ import subprocess # nosec
16
+ import sys
17
+ import tempfile
18
+
19
+ import pkg_resources
20
+
21
+ try:
22
+ from Bio.PDB import PDBIO, PDBParser, Select
23
+ except ImportError as e:
24
+ print(
25
+ "[!] The binding affinity prediction tools require Biopython",
26
+ file=sys.stderr,
27
+ )
28
+ raise ImportError(e)
29
+
30
+
31
+ import contextlib
32
+
33
+ from .aa_properties import rel_asa
34
+
35
+
36
+ @contextlib.contextmanager
37
+ def stdchannel_redirected(stdchannel, dest_filename):
38
+ """
39
+ A context manager to temporarily redirect stdout or stderr
40
+ https://stackoverflow.com/questions/977840/redirecting-fortran-called-via-f2py-output-in-python/978264#978264
41
+ e.g.:
42
+
43
+
44
+ with stdchannel_redirected(sys.stderr, os.devnull):
45
+ if compiler.has_function('clock_gettime', libraries=['rt']):
46
+ libraries.append('rt')
47
+ """
48
+ oldstdchannel, dest_file = None, None
49
+ try:
50
+ oldstdchannel = os.dup(stdchannel.fileno())
51
+ dest_file = open(dest_filename, "w")
52
+ os.dup2(dest_file.fileno(), stdchannel.fileno())
53
+
54
+ yield
55
+ finally:
56
+ if oldstdchannel is not None:
57
+ os.dup2(oldstdchannel, stdchannel.fileno())
58
+ if dest_file is not None:
59
+ dest_file.close()
60
+
61
+
62
+ def execute_freesasa(structure, selection=None):
63
+ """
64
+ Runs the freesasa executable on a PDB file.
65
+
66
+ You can get the executable from:
67
+ https://github.com/mittinatten/freesasa
68
+
69
+ The binding affinity models are calibrated with the parameter
70
+ set for vdW radii used in NACCESS:
71
+ http://www.ncbi.nlm.nih.gov/pubmed/994183
72
+ """
73
+ io = PDBIO()
74
+
75
+ # try to get freesasa paths from environment if not use the
76
+ # ones defined in config file
77
+ try:
78
+ freesasa, param_f = [
79
+ os.environ[key] for key in ["FREESASA_BIN", "FREESASA_PAR"]
80
+ ]
81
+ except KeyError as err:
82
+ message = (
83
+ "{} not found. In order to use PRODIGY, set the FREESASA_BIN and "
84
+ "FREESASA_PAR environment variables to point to the freesasa "
85
+ "executable and the naccess.config file respectively."
86
+ )
87
+ raise KeyError(message.format(err))
88
+
89
+ if not os.path.isfile(freesasa):
90
+ raise IOError(
91
+ "[!] freesasa binary not found at `{0}`".format(freesasa)
92
+ )
93
+ if not os.path.isfile(param_f):
94
+ raise IOError(
95
+ "[!] Atomic radii file not found at `{0}`".format(param_f)
96
+ )
97
+
98
+ # Rewrite PDB using Biopython to have a proper format
99
+ # freesasa is very picky with line width (80 characters or fails!)
100
+ # Select chains if necessary
101
+ class ChainSelector(Select):
102
+ def accept_chain(self, chain):
103
+ if selection and chain.id in selection:
104
+ return 1
105
+ elif not selection:
106
+ return 1
107
+ else:
108
+ return 0
109
+
110
+ _pdbf = tempfile.NamedTemporaryFile()
111
+ io.set_structure(structure)
112
+ io.save(_pdbf.name, ChainSelector())
113
+
114
+ # Run freesasa
115
+ # Save atomic asa output to another temp file
116
+ _outf = tempfile.NamedTemporaryFile()
117
+ cmd = "{0} -o {1} --format=pdb -c {2} {3}".format(
118
+ freesasa, _outf.name, param_f, _pdbf.name
119
+ )
120
+ p = subprocess.Popen( # nosec
121
+ cmd,
122
+ shell=False,
123
+ stdout=subprocess.PIPE,
124
+ stderr=subprocess.PIPE,
125
+ )
126
+ stdout, stderr = p.communicate()
127
+
128
+ if p.returncode:
129
+ print("[!] freesasa did not run successfully", file=sys.stderr)
130
+ print(cmd, file=sys.stderr)
131
+ raise Exception(stderr)
132
+
133
+ # Rewind & Parse results file
134
+ # Save
135
+ _outf.seek(0)
136
+ asa, rsa = parse_freesasa_output(_outf)
137
+
138
+ _pdbf.close()
139
+ _outf.close()
140
+
141
+ return asa, rsa
142
+
143
+
144
+ def parse_freesasa_output(fpath):
145
+ """
146
+ Returns per-residue relative accessibility of side-chain and main-chain
147
+ atoms as calculated by freesasa.
148
+ """
149
+
150
+ asa_data, rsa_data = {}, {}
151
+
152
+ _rsa = rel_asa
153
+ # _bb = {'CA', 'C', 'N', 'O'}
154
+
155
+ p = PDBParser(QUIET=1)
156
+ s = p.get_structure("bogus", fpath.name)
157
+ for res in s.get_residues():
158
+ res_id = (res.parent.id, res.resname, res.id[1])
159
+ _, _, total_asa = 0, 0, 0
160
+ for atom in res:
161
+ aname = atom.name
162
+ at_id = (res.parent.id, res.resname, res.id[1], aname)
163
+ asa = atom.bfactor
164
+ # if atom.name in _bb:
165
+ # asa_mc += asa
166
+ # else:
167
+ # asa_sc += asa
168
+ total_asa += asa
169
+ asa_data[at_id] = asa
170
+
171
+ rsa_data[res_id] = total_asa / _rsa["total"][res.resname]
172
+
173
+ return asa_data, rsa_data
174
+
175
+
176
+ def execute_freesasa_api(structure):
177
+ """
178
+ Calls freesasa using its Python API and returns
179
+ per-residue accessibilities.
180
+ """
181
+ try:
182
+ from freesasa import Classifier, calc, structureFromBioPDB
183
+ except ImportError as err:
184
+ print(
185
+ (
186
+ "[!] The binding affinity prediction tools require the "
187
+ "'freesasa' Python API"
188
+ ),
189
+ file=sys.stderr,
190
+ )
191
+ raise ImportError(err)
192
+
193
+ asa_data, rsa_data = {}, {}
194
+ _rsa = rel_asa["total"]
195
+
196
+ config_path = os.environ.get(
197
+ "FREESASA_PAR",
198
+ pkg_resources.resource_filename("prodigy_prot", "naccess.config"),
199
+ )
200
+ classifier = Classifier(config_path)
201
+ pkg_resources.cleanup_resources()
202
+
203
+ # classifier = freesasa.Classifier( os.environ["FREESASA_PAR"])
204
+ # Disable
205
+ with stdchannel_redirected(sys.stderr, os.devnull):
206
+ try:
207
+ struct = structureFromBioPDB(
208
+ structure,
209
+ classifier,
210
+ )
211
+ result = calc(struct)
212
+ except AssertionError as e:
213
+ error_message = "" + os.linesep()
214
+ error_message += "[!] Error when running freesasa:" + os.linesep()
215
+ error_message += f"[!] {e}" + os.linesep()
216
+ error_message += (
217
+ "[!] Make sure the atom names in your PDB file match"
218
+ " the canonical naming and belong "
219
+ "to default residues" + os.linesep()
220
+ )
221
+ print(error_message)
222
+ raise Exception(error_message)
223
+
224
+ # iterate over all atoms to get SASA and residue name
225
+ for idx in range(struct.nAtoms()):
226
+ atname = struct.atomName(idx)
227
+ resname = struct.residueName(idx)
228
+ resid = struct.residueNumber(idx)
229
+ chain = struct.chainLabel(idx)
230
+ at_uid = (chain, resname, resid, atname)
231
+ res_uid = (chain, resname, resid)
232
+
233
+ asa = result.atomArea(idx)
234
+ asa_data[at_uid] = asa
235
+ # add asa to residue
236
+ rsa_data[res_uid] = rsa_data.get(res_uid, 0) + asa
237
+
238
+ # convert total asa ro relative asa
239
+ rsa_data.update(
240
+ (res_uid, asa / _rsa[res_uid[1]]) for res_uid, asa in rsa_data.items()
241
+ )
242
+ return asa_data, rsa_data
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env python
2
+ #
3
+ # This code is part of the binding affinity prediction tools distribution
4
+ # and governed by its license. Please see the LICENSE file that should
5
+ # have been included as part of this package.
6
+ #
7
+
8
+ """
9
+ Models to predict binding affinity based on molecular properties.
10
+ """
11
+
12
+
13
+ def IC_NIS(ic_cc, ic_ca, ic_pp, ic_pa, p_nis_a, p_nis_c):
14
+ """
15
+ Calculates the predicted binding affinity value
16
+ based on the IC-NIS model.
17
+ """
18
+
19
+ return (
20
+ -0.09459 * ic_cc
21
+ + -0.10007 * ic_ca
22
+ + 0.19577 * ic_pp
23
+ + -0.22671 * ic_pa
24
+ + 0.18681 * p_nis_a
25
+ + 0.13810 * p_nis_c
26
+ + -15.9433
27
+ )
28
+
29
+
30
+ def NIS(p_nis_c, p_nis_p, n_int_atoms):
31
+ """
32
+ Calculates the predicted binding affinity value
33
+ based on the NIS model.
34
+ """
35
+
36
+ return (
37
+ 0.0856851248873 * p_nis_p
38
+ + -0.0685254498746 * p_nis_c
39
+ + 0.0261591389985 * n_int_atoms
40
+ + 3.0124939659498
41
+ )
File without changes
@@ -0,0 +1,167 @@
1
+ #!/usr/bin/env python
2
+ #
3
+ # This code is part of the binding affinity prediction tools distribution
4
+ # and governed by its license. Please see the LICENSE file that should
5
+ # have been included as part of this package.
6
+ #
7
+
8
+ """
9
+ Functions to read PDB/mmCIF files
10
+ """
11
+
12
+ from __future__ import division, print_function
13
+
14
+ import logging
15
+ import os
16
+ import sys
17
+
18
+ try:
19
+ from Bio.PDB import MMCIFParser, PDBParser
20
+ from Bio.PDB.Polypeptide import PPBuilder, is_aa
21
+ except ImportError as e:
22
+ print(
23
+ "[!] The binding affinity prediction tools require Biopython",
24
+ file=sys.stderr,
25
+ )
26
+ raise ImportError(e)
27
+
28
+
29
+ def validate_structure(s, selection=None, clean=True):
30
+ # setup logging
31
+ logger = logging.getLogger("Prodigy")
32
+
33
+ # Keep first model only
34
+ if len(s) > 1:
35
+ logger.warning(
36
+ (
37
+ "[!] Structure contains more than one model."
38
+ " Only the first one will be kept"
39
+ )
40
+ )
41
+ model_one = s[0].id
42
+ for m in s.child_list[:]:
43
+ if m.id != model_one:
44
+ s.detach_child(m.id)
45
+
46
+ # process selected chains
47
+ chains = list(s.get_chains())
48
+ chain_ids = set([c.id for c in chains])
49
+
50
+ if selection:
51
+ sel_chains = []
52
+ # Match selected chain with structure
53
+ for sel in selection:
54
+ for c in sel.split(","):
55
+ sel_chains.append(c)
56
+ if c not in chain_ids:
57
+ raise ValueError(
58
+ "Selected chain not present"
59
+ f" in provided structure: {c}"
60
+ )
61
+
62
+ # Remove unselected chains
63
+ def _ignore(x):
64
+ return x.id not in sel_chains
65
+
66
+ for c in chains:
67
+ if _ignore(c):
68
+ c.parent.detach_child(c.id)
69
+
70
+ # Double occupancy check
71
+ for atom in list(s.get_atoms()):
72
+ if atom.is_disordered():
73
+ residue = atom.parent
74
+ sel_at = atom.selected_child
75
+ sel_at.altloc = " "
76
+ sel_at.disordered_flag = 0
77
+ residue.detach_child(atom.id)
78
+ residue.add(sel_at)
79
+
80
+ # Insertion code check
81
+ for c in chains:
82
+ for residue in c.get_residues():
83
+ if residue.get_id()[2] != " ":
84
+ c.detach_child(residue.id)
85
+
86
+ if clean:
87
+ # Remove HETATMs and solvent
88
+ res_list = list(s.get_residues())
89
+
90
+ def _ignore(r):
91
+ return r.id[0][0] == "W" or r.id[0][0] == "H"
92
+
93
+ for res in res_list:
94
+ if _ignore(res):
95
+ chain = res.parent
96
+ chain.detach_child(res.id)
97
+ elif not is_aa(res, standard=True):
98
+ raise ValueError(
99
+ "Unsupported non-standard amino acid found: {0}".format(
100
+ res.resname
101
+ )
102
+ )
103
+
104
+ # Remove Hydrogens
105
+ atom_list = list(s.get_atoms())
106
+
107
+ def _ignore(x):
108
+ return x.element == "H"
109
+
110
+ for atom in atom_list:
111
+ if _ignore(atom):
112
+ residue = atom.parent
113
+ residue.detach_child(atom.name)
114
+
115
+ # Detect gaps and compare with no. of chains
116
+ pep_builder = PPBuilder()
117
+ peptides = pep_builder.build_peptides(s)
118
+ n_peptides = len(peptides)
119
+
120
+ if n_peptides != len(chain_ids):
121
+ message = "[!] Structure contains gaps:\n"
122
+ for i_pp, pp in enumerate(peptides):
123
+ message += (
124
+ "\t{1.parent.id} {1.resname}{1.id[1]} < Fragment {0} > "
125
+ "{2.parent.id} {2.resname}{2.id[1]}\n".format(
126
+ i_pp, pp[0], pp[-1]
127
+ )
128
+ )
129
+ logger.warning(message)
130
+ # raise Exception(message)
131
+
132
+ return s
133
+
134
+
135
+ def parse_structure(path):
136
+ """
137
+ Parses a structure using Biopython's PDB/mmCIF Parser
138
+ Verifies the integrity of the structure (gaps) and its
139
+ suitability for the calculation (is it a complex?).
140
+ """
141
+ # setup logging
142
+ logger = logging.getLogger("Prodigy")
143
+ logger.info("[+] Reading structure file: {0}".format(path))
144
+ fname = os.path.basename(path)
145
+ sname = ".".join(fname.split(".")[:-1])
146
+ s_ext = fname.split(".")[-1]
147
+
148
+ _ext = {"pdb", "ent", "cif"}
149
+ if s_ext not in _ext:
150
+ raise IOError(
151
+ f"[!] Structure format '{s_ext}' is "
152
+ "not supported. Use '.pdb' or '.cif'."
153
+ )
154
+
155
+ sparser = PDBParser(QUIET=1) if s_ext in {"pdb", "ent"} else MMCIFParser()
156
+
157
+ try:
158
+ s = sparser.get_structure(sname, path)
159
+ except Exception as exeption:
160
+ logger.error("[!] Structure '{0}' could not be parsed".format(sname))
161
+ raise Exception(exeption)
162
+
163
+ return (
164
+ validate_structure(s),
165
+ len(set([c.id for c in s.get_chains()])),
166
+ len(list(s.get_residues())),
167
+ )
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env python
2
+ #
3
+ # This code is part of the binding affinity prediction tools distribution
4
+ # and governed by its license. Please see the LICENSE file that should
5
+ # have been included as part of this package.
6
+ #
7
+
8
+ """
9
+ Assorted utility functions.
10
+ """
11
+
12
+ import math
13
+ import os
14
+
15
+
16
+ def check_path(path):
17
+ """
18
+ Checks if a file is readable.
19
+ """
20
+
21
+ full_path = os.path.abspath(path)
22
+ if not os.path.isfile(full_path):
23
+ raise IOError("Could not read file: {0}".format(path))
24
+ return full_path
25
+
26
+
27
+ def dg_to_kd(dg, temperature=25.0):
28
+ """Coversion of DG into the dissociation constant kd"""
29
+
30
+ temp_in_k = temperature + 273.15
31
+ rt = 0.0019858775 * temp_in_k
32
+ return math.exp(dg / rt)