QPromaQ commited on
Commit
e5f7066
·
verified ·
1 Parent(s): 7d2290f

Reset repository and upload final project (part 2)

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +1 -0
  2. .venv_haddock/include/site/python3.12/freesasa/parser.h +97 -0
  3. .venv_haddock/include/site/python3.12/freesasa/pdb.c +432 -0
  4. .venv_haddock/include/site/python3.12/freesasa/pdb.h +187 -0
  5. .venv_haddock/include/site/python3.12/freesasa/rsa.c +174 -0
  6. .venv_haddock/include/site/python3.12/freesasa/sasa_lr.c +487 -0
  7. .venv_haddock/include/site/python3.12/freesasa/sasa_sr.c +338 -0
  8. .venv_haddock/include/site/python3.12/freesasa/selection.c +1003 -0
  9. .venv_haddock/include/site/python3.12/freesasa/selection.h +49 -0
  10. .venv_haddock/include/site/python3.12/freesasa/structure.c +1243 -0
  11. .venv_haddock/include/site/python3.12/freesasa/util.c +141 -0
  12. .venv_haddock/lib/._.DS_Store +0 -0
  13. .venv_haddock/lib/._python3.12 +0 -0
  14. .venv_haddock/lib/python3.12/._.DS_Store +0 -0
  15. .venv_haddock/lib/python3.12/._site-packages +0 -0
  16. .venv_haddock/lib/python3.12/site-packages/._.DS_Store +0 -0
  17. .venv_haddock/lib/python3.12/site-packages/._Bio +0 -0
  18. .venv_haddock/lib/python3.12/site-packages/._BioSQL +0 -0
  19. .venv_haddock/lib/python3.12/site-packages/.___pycache__ +0 -0
  20. .venv_haddock/lib/python3.12/site-packages/.__distutils_hack +0 -0
  21. .venv_haddock/lib/python3.12/site-packages/.__plotly_utils +0 -0
  22. .venv_haddock/lib/python3.12/site-packages/.__yaml +0 -0
  23. .venv_haddock/lib/python3.12/site-packages/._biopython-1.87.dist-info +0 -0
  24. .venv_haddock/lib/python3.12/site-packages/._dateutil +0 -0
  25. .venv_haddock/lib/python3.12/site-packages/._distutils-precedence.pth +3 -0
  26. .venv_haddock/lib/python3.12/site-packages/._freesasa-2.2.1.dist-info +0 -0
  27. .venv_haddock/lib/python3.12/site-packages/._freesasa.cpython-312-darwin.so +0 -0
  28. .venv_haddock/lib/python3.12/site-packages/._haddock +0 -0
  29. .venv_haddock/lib/python3.12/site-packages/._haddock3-2026.3.0.dist-info +0 -0
  30. .venv_haddock/lib/python3.12/site-packages/._jsonpickle +0 -0
  31. .venv_haddock/lib/python3.12/site-packages/._jsonpickle-4.1.1.dist-info +0 -0
  32. .venv_haddock/lib/python3.12/site-packages/._narwhals +0 -0
  33. .venv_haddock/lib/python3.12/site-packages/._narwhals-2.19.0.dist-info +0 -0
  34. .venv_haddock/lib/python3.12/site-packages/._numpy +0 -0
  35. .venv_haddock/lib/python3.12/site-packages/._numpy-2.4.4.dist-info +0 -0
  36. .venv_haddock/lib/python3.12/site-packages/._packaging +0 -0
  37. .venv_haddock/lib/python3.12/site-packages/._packaging-26.1.dist-info +0 -0
  38. .venv_haddock/lib/python3.12/site-packages/._pandas +0 -0
  39. .venv_haddock/lib/python3.12/site-packages/._pandas-2.3.3.dist-info +0 -0
  40. .venv_haddock/lib/python3.12/site-packages/._pdb_tools-2.6.1.dist-info +0 -0
  41. .venv_haddock/lib/python3.12/site-packages/._pdbtools +0 -0
  42. .venv_haddock/lib/python3.12/site-packages/._pip +0 -0
  43. .venv_haddock/lib/python3.12/site-packages/._pip-26.0.1.dist-info +0 -0
  44. .venv_haddock/lib/python3.12/site-packages/._plotly +0 -0
  45. .venv_haddock/lib/python3.12/site-packages/._plotly-6.5.2.dist-info +0 -0
  46. .venv_haddock/lib/python3.12/site-packages/._prodigy_lig +0 -0
  47. .venv_haddock/lib/python3.12/site-packages/._prodigy_lig-1.1.4.dist-info +0 -0
  48. .venv_haddock/lib/python3.12/site-packages/._prodigy_prot +0 -0
  49. .venv_haddock/lib/python3.12/site-packages/._prodigy_prot-2.3.0.dist-info +0 -0
  50. .venv_haddock/lib/python3.12/site-packages/._python_dateutil-2.9.0.post0.dist-info +0 -0
.gitattributes CHANGED
@@ -61,3 +61,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
61
  .venv_haddock/bin/python filter=lfs diff=lfs merge=lfs -text
62
  .venv_haddock/bin/python3 filter=lfs diff=lfs merge=lfs -text
63
  .venv_haddock/bin/python3.12 filter=lfs diff=lfs merge=lfs -text
 
 
61
  .venv_haddock/bin/python filter=lfs diff=lfs merge=lfs -text
62
  .venv_haddock/bin/python3 filter=lfs diff=lfs merge=lfs -text
63
  .venv_haddock/bin/python3.12 filter=lfs diff=lfs merge=lfs -text
64
+ .venv_haddock/lib/python3.12/site-packages/Bio/Align/_pairwisealigner.cpython-312-darwin.so filter=lfs diff=lfs merge=lfs -text
.venv_haddock/include/site/python3.12/freesasa/parser.h ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* A Bison parser, made by GNU Bison 3.0.4. */
2
+
3
+ /* Bison interface for Yacc-like parsers in C
4
+
5
+ Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc.
6
+
7
+ This program is free software: you can redistribute it and/or modify
8
+ it under the terms of the GNU General Public License as published by
9
+ the Free Software Foundation, either version 3 of the License, or
10
+ (at your option) any later version.
11
+
12
+ This program is distributed in the hope that it will be useful,
13
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ GNU General Public License for more details.
16
+
17
+ You should have received a copy of the GNU General Public License
18
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
+
20
+ /* As a special exception, you may create a larger work that contains
21
+ part or all of the Bison parser skeleton and distribute that work
22
+ under terms of your choice, so long as that work isn't itself a
23
+ parser generator using the skeleton or a modified version thereof
24
+ as a parser skeleton. Alternatively, if you modify or redistribute
25
+ the parser skeleton itself, you may (at your option) remove this
26
+ special exception, which will cause the skeleton and the resulting
27
+ Bison output files to be licensed under the GNU General Public
28
+ License without this special exception.
29
+
30
+ This special exception was added by the Free Software Foundation in
31
+ version 2.2 of Bison. */
32
+
33
+ #ifndef YY_FREESASA_YY_PARSER_H_INCLUDED
34
+ # define YY_FREESASA_YY_PARSER_H_INCLUDED
35
+ /* Debug traces. */
36
+ #ifndef YYDEBUG
37
+ # define YYDEBUG 0
38
+ #endif
39
+ #if YYDEBUG
40
+ extern int freesasa_yydebug;
41
+ #endif
42
+ /* "%code requires" blocks. */
43
+ #line 13 "parser.y" /* yacc.c:1909 */
44
+
45
+
46
+ #ifndef FREESASA_TYPEDEF_YY_SCANNER_T
47
+ #define FREESASA_TYPEDEF_YY_SCANNER_T
48
+ typedef void* freesasa_yyscan_t;
49
+ #endif
50
+
51
+
52
+ #line 53 "parser.h" /* yacc.c:1909 */
53
+
54
+ /* Token type. */
55
+ #ifndef YYTOKENTYPE
56
+ # define YYTOKENTYPE
57
+ enum yytokentype
58
+ {
59
+ T_NUMBER = 258,
60
+ T_ID = 259,
61
+ T_SELID = 260,
62
+ T_AND = 261,
63
+ T_OR = 262,
64
+ T_NOT = 263,
65
+ T_RESN = 264,
66
+ T_RESI = 265,
67
+ T_SYMBOL = 266,
68
+ T_NAME = 267,
69
+ T_CHAIN = 268,
70
+ T_MINUS = 269,
71
+ ATOM = 270
72
+ };
73
+ #endif
74
+
75
+ /* Value type. */
76
+ #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
77
+
78
+ union YYSTYPE
79
+ {
80
+ #line 30 "parser.y" /* yacc.c:1909 */
81
+
82
+ const char *value;
83
+ expression *expression;
84
+
85
+ #line 86 "parser.h" /* yacc.c:1909 */
86
+ };
87
+
88
+ typedef union YYSTYPE YYSTYPE;
89
+ # define YYSTYPE_IS_TRIVIAL 1
90
+ # define YYSTYPE_IS_DECLARED 1
91
+ #endif
92
+
93
+
94
+
95
+ int freesasa_yyparse (expression **expression, freesasa_yyscan_t scanner);
96
+
97
+ #endif /* !YY_FREESASA_YY_PARSER_H_INCLUDED */
.venv_haddock/include/site/python3.12/freesasa/pdb.c ADDED
@@ -0,0 +1,432 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if HAVE_CONFIG_H
2
+ #include <config.h>
3
+ #endif
4
+
5
+ #include <assert.h>
6
+ #include <errno.h>
7
+ #include <stdlib.h>
8
+
9
+ #include "freesasa_internal.h"
10
+ #include "pdb.h"
11
+
12
+ /* len >= 6 */
13
+ static inline int
14
+ pdb_line_check(const char *line, size_t len)
15
+ {
16
+ assert(line);
17
+ if (len < 6) return FREESASA_FAIL;
18
+ if (strlen(line) < len) return FREESASA_FAIL;
19
+ if (strncmp("ATOM", line, 4) != 0 &&
20
+ strncmp("HETATM", line, 6) != 0) {
21
+ return FREESASA_FAIL;
22
+ }
23
+ return FREESASA_SUCCESS;
24
+ }
25
+
26
+ /**
27
+ Extracts a double from the line of maximum width characters, to
28
+ allow checking for empty fields (instead of just reading the first
29
+ float that comes along.
30
+ */
31
+ static inline int
32
+ pdb_get_double(const char *line, size_t width, double *val)
33
+ {
34
+ /* allow truncated lines */
35
+ char buf[PDB_LINE_STRL];
36
+ float tmp;
37
+
38
+ if (strlen(line) < width) width = strlen(line);
39
+
40
+ memcpy(buf, line, width);
41
+ buf[width] = '\0';
42
+
43
+ if (sscanf(buf, "%f", &tmp) == 1) {
44
+ *val = tmp;
45
+ return FREESASA_SUCCESS;
46
+ }
47
+
48
+ return FREESASA_FAIL;
49
+ }
50
+
51
+ int freesasa_pdb_get_models(FILE *pdb,
52
+ struct file_range **ranges)
53
+ {
54
+ char line[PDB_MAX_LINE_STRL];
55
+ int n = 0, n_end = 0, error = 0;
56
+ long last_pos = ftell(pdb);
57
+ struct file_range *it = NULL, *itb;
58
+
59
+ assert(pdb != NULL);
60
+
61
+ while (fgets(line, PDB_MAX_LINE_STRL, pdb) != NULL) {
62
+ if (strncmp("MODEL", line, 5) == 0) {
63
+ ++n;
64
+ itb = it;
65
+ it = realloc(it, sizeof(struct file_range) * n);
66
+ if (!it) {
67
+ free(itb);
68
+ error = mem_fail();
69
+ break;
70
+ }
71
+ it[n - 1].begin = last_pos;
72
+ }
73
+ if (strncmp("ENDMDL", line, 6) == 0) {
74
+ ++n_end;
75
+ if (n != n_end) {
76
+ error = fail_msg("mismatch between MODEL and ENDMDL in input");
77
+ break;
78
+ }
79
+ it[n - 1].end = ftell(pdb);
80
+ }
81
+ last_pos = ftell(pdb);
82
+ }
83
+ if (n == 0) { /* when there are no models, the whole file is the model */
84
+ free(it);
85
+ it = NULL;
86
+ }
87
+ if (error == FREESASA_FAIL) {
88
+ free(it);
89
+ *ranges = NULL;
90
+ return FREESASA_FAIL;
91
+ }
92
+ *ranges = it;
93
+ return n;
94
+ }
95
+
96
+ int freesasa_pdb_get_chains(FILE *pdb,
97
+ struct file_range model,
98
+ struct file_range **ranges,
99
+ int options)
100
+ {
101
+ /* it is assumed that 'model' is valid for 'pdb' */
102
+
103
+ int n_chains = 0;
104
+ char line[PDB_MAX_LINE_STRL];
105
+ struct file_range *chains = NULL, *chb;
106
+ char last_chain = '\0';
107
+ long last_pos = model.begin;
108
+
109
+ assert(pdb);
110
+ assert(ranges);
111
+
112
+ *ranges = NULL;
113
+
114
+ /* for each model, find file ranges for each chain, store them
115
+ in the dynamically growing array chains */
116
+ fseek(pdb, model.begin, SEEK_SET);
117
+ while (fgets(line, PDB_MAX_LINE_STRL, pdb) != NULL &&
118
+ ftell(pdb) < model.end) {
119
+ if (strncmp("ATOM", line, 4) == 0 || ((options & FREESASA_INCLUDE_HETATM) &&
120
+ (strncmp("HETATM", line, 6) == 0))) {
121
+ char chain = freesasa_pdb_get_chain_label(line);
122
+ if (chain != last_chain) {
123
+ if (n_chains > 0) chains[n_chains - 1].end = last_pos;
124
+ ++n_chains;
125
+ chb = chains;
126
+ chains = realloc(chains, sizeof(struct file_range) * n_chains);
127
+ if (!chains) {
128
+ free(chb);
129
+ return mem_fail();
130
+ }
131
+ chains[n_chains - 1].begin = last_pos;
132
+ last_chain = chain;
133
+ }
134
+ }
135
+ last_pos = ftell(pdb);
136
+ }
137
+
138
+ if (n_chains > 0) {
139
+ chains[n_chains - 1].end = last_pos;
140
+ chains[0].begin = model.begin; /* preserve model info */
141
+ *ranges = chains;
142
+ } else {
143
+ *ranges = NULL;
144
+ }
145
+ return n_chains;
146
+ }
147
+
148
+ int freesasa_pdb_get_atom_name(char *name,
149
+ const char *line)
150
+ {
151
+ assert(name);
152
+ assert(line);
153
+ if (pdb_line_check(line, PDB_ATOM_NAME_STRL + 12) == FREESASA_FAIL) {
154
+ name[0] = '\0';
155
+ return FREESASA_FAIL;
156
+ }
157
+ strncpy(name, line + 12, PDB_ATOM_NAME_STRL);
158
+ name[PDB_ATOM_NAME_STRL] = '\0';
159
+ return FREESASA_SUCCESS;
160
+ }
161
+
162
+ int freesasa_pdb_get_res_name(char *name,
163
+ const char *line)
164
+ {
165
+ assert(name);
166
+ assert(line);
167
+ if (pdb_line_check(line, PDB_ATOM_RES_NAME_STRL + 17) == FREESASA_FAIL) {
168
+ name[0] = '\0';
169
+ return FREESASA_FAIL;
170
+ }
171
+ strncpy(name, line + 17, PDB_ATOM_RES_NAME_STRL);
172
+ name[PDB_ATOM_RES_NAME_STRL] = '\0';
173
+ return FREESASA_SUCCESS;
174
+ }
175
+
176
+ int freesasa_pdb_get_coord(double *xyz,
177
+ const char *line)
178
+ {
179
+ int n_coord = 24; /* 54-30+1 */
180
+ char coord_section[25];
181
+
182
+ assert(xyz);
183
+ assert(line);
184
+
185
+ if (pdb_line_check(line, 54) == FREESASA_FAIL) {
186
+ return FREESASA_FAIL;
187
+ }
188
+
189
+ strncpy(coord_section, line + 30, n_coord);
190
+ coord_section[n_coord] = '\0';
191
+
192
+ if (sscanf(coord_section, "%lf%lf%lf", &xyz[0], &xyz[1], &xyz[2]) != 3) {
193
+ return fail_msg("could not read coordinates from line '%s'", line);
194
+ }
195
+
196
+ return FREESASA_SUCCESS;
197
+ }
198
+
199
+ int freesasa_pdb_get_res_number(char *number,
200
+ const char *line)
201
+ {
202
+ assert(number);
203
+ assert(line);
204
+ if (pdb_line_check(line, PDB_ATOM_RES_NUMBER_STRL + 22) == FREESASA_FAIL) {
205
+ number[0] = '\0';
206
+ return FREESASA_FAIL;
207
+ }
208
+ strncpy(number, line + 22, PDB_ATOM_RES_NUMBER_STRL);
209
+ number[PDB_ATOM_RES_NUMBER_STRL] = '\0';
210
+ return FREESASA_SUCCESS;
211
+ }
212
+ char freesasa_pdb_get_chain_label(const char *line)
213
+ {
214
+ assert(line);
215
+ if (pdb_line_check(line, 21) == FREESASA_FAIL) return '\0';
216
+ return line[21];
217
+ }
218
+
219
+ char freesasa_pdb_get_alt_coord_label(const char *line)
220
+ {
221
+ assert(line);
222
+ if (pdb_line_check(line, 16) == FREESASA_FAIL) return '\0';
223
+ return line[16];
224
+ }
225
+
226
+ int freesasa_pdb_get_symbol(char *symbol,
227
+ const char *line)
228
+ {
229
+ assert(line);
230
+ if (pdb_line_check(line, 76 + PDB_ATOM_SYMBOL_STRL) == FREESASA_FAIL) {
231
+ symbol[0] = '\0';
232
+ return FREESASA_FAIL;
233
+ }
234
+ strncpy(symbol, line + 76, 2);
235
+ symbol[2] = '\0';
236
+ return FREESASA_SUCCESS;
237
+ }
238
+
239
+ int freesasa_pdb_get_occupancy(double *occ,
240
+ const char *line)
241
+ {
242
+ assert(line);
243
+ /* allow truncated lines */
244
+ if (pdb_line_check(line, 55) == FREESASA_SUCCESS)
245
+ return pdb_get_double(line + 54, 6, occ);
246
+ return FREESASA_FAIL;
247
+ }
248
+
249
+ int freesasa_pdb_get_bfactor(double *bfac,
250
+ const char *line)
251
+ {
252
+ assert(line);
253
+ /* allow truncated lines */
254
+ if (pdb_line_check(line, 61) == FREESASA_SUCCESS)
255
+ return pdb_get_double(line + 60, 6, bfac);
256
+ return FREESASA_FAIL;
257
+ }
258
+
259
+ int freesasa_pdb_ishydrogen(const char *line)
260
+ {
261
+ assert(line);
262
+ char symbol[PDB_ATOM_SYMBOL_STRL + 1];
263
+ freesasa_pdb_get_symbol(symbol, line);
264
+
265
+ if (pdb_line_check(line, 13) == FREESASA_FAIL) return FREESASA_FAIL;
266
+
267
+ /* Check symbol first */
268
+ if (strncmp(symbol, " H", 2) == 0) return 1;
269
+ if (strncmp(symbol, " D", 2) == 0) return 1;
270
+ /* If the symbol is not blank and not H or D */
271
+ if (!(strncmp(symbol, " ", 2) == 0)) return 0;
272
+
273
+ /* When symbol is missing */
274
+ /* Cover elements such as Cd, Nd, Th, etc. ("CD ", "ND ") */
275
+ if (!(line[12] == ' ' || (line[12] >= '1' && line[12] <= '9'))) return 0;
276
+ /* Hydrogen, atom name "H**" or " H**" */
277
+ if (line[12] == 'H' || line[13] == 'H') return 1;
278
+ /* Deuterium */
279
+ if (line[12] == 'D' || line[13] == 'D') return 1;
280
+ return 0;
281
+ }
282
+
283
+ static int
284
+ write_pdb_impl(FILE *output,
285
+ freesasa_node *structure)
286
+ {
287
+ char buf[PDB_LINE_STRL + 1], buf2[6];
288
+ int model;
289
+ double radius;
290
+ const char *line = NULL;
291
+ freesasa_node *chain = NULL, *residue = NULL, *atom = NULL;
292
+ const freesasa_nodearea *area = NULL;
293
+ const char *last_res_name = NULL, *last_res_number = NULL, *last_chain = NULL;
294
+
295
+ assert(freesasa_node_type(structure) == FREESASA_NODE_STRUCTURE);
296
+
297
+ model = freesasa_node_structure_model(structure);
298
+ if (model > 0)
299
+ fprintf(output, "MODEL %4d\n", model);
300
+ else
301
+ fprintf(output, "MODEL 1\n");
302
+
303
+ chain = freesasa_node_children(structure);
304
+
305
+ /* Write ATOM entries */
306
+ while (chain) {
307
+ residue = freesasa_node_children(chain);
308
+ while (residue) {
309
+ atom = freesasa_node_children(residue);
310
+ while (atom) {
311
+ line = freesasa_node_atom_pdb_line(atom);
312
+ area = freesasa_node_area(atom);
313
+ radius = freesasa_node_atom_radius(atom);
314
+
315
+ if (line == NULL) {
316
+ return fail_msg("PDB input not valid or not present");
317
+ }
318
+
319
+ strncpy(buf, line, PDB_LINE_STRL);
320
+ sprintf(&buf[54], "%6.2f%6.2f", radius, area->total);
321
+ fprintf(output, "%s\n", buf);
322
+
323
+ atom = freesasa_node_next(atom);
324
+ }
325
+ last_res_name = freesasa_node_name(residue);
326
+ last_res_number = freesasa_node_residue_number(residue);
327
+ residue = freesasa_node_next(residue);
328
+ }
329
+ last_chain = freesasa_node_name(chain);
330
+ chain = freesasa_node_next(chain);
331
+ }
332
+
333
+ /* Write TER and ENDMDL lines */
334
+ strncpy(buf2, &buf[6], 5);
335
+ buf2[5] = '\0';
336
+ fprintf(output, "TER %5d %4s %c%5s\nENDMDL\n",
337
+ atoi(buf2) + 1, last_res_name, last_chain[0], last_res_number);
338
+
339
+ fflush(output);
340
+ if (ferror(output)) {
341
+ return fail_msg(strerror(errno));
342
+ }
343
+
344
+ return FREESASA_SUCCESS;
345
+ }
346
+
347
+ int freesasa_write_pdb(FILE *output,
348
+ freesasa_node *root)
349
+ {
350
+ freesasa_node *result = freesasa_node_children(root), *structure;
351
+
352
+ assert(output);
353
+ assert(root);
354
+ assert(freesasa_node_type(root) == FREESASA_NODE_ROOT);
355
+
356
+ fprintf(output, "REMARK 999 This PDB file was generated by %s.\n", freesasa_string);
357
+ fprintf(output, "REMARK 999 In the ATOM records temperature factors have been\n"
358
+ "REMARK 999 replaced by the SASA of the atom, and the occupancy\n"
359
+ "REMARK 999 by the radius used in the calculation.\n");
360
+
361
+ while (result) {
362
+ structure = freesasa_node_children(result);
363
+ while (structure) {
364
+ if (write_pdb_impl(output, structure) == FREESASA_FAIL) {
365
+ return fail_msg("");
366
+ }
367
+ structure = freesasa_node_next(structure);
368
+ }
369
+ result = freesasa_node_next(result);
370
+ }
371
+
372
+ return FREESASA_SUCCESS;
373
+ }
374
+
375
+ #if USE_CHECK
376
+ #include <check.h>
377
+ #include <math.h>
378
+
379
+ START_TEST(test_pdb)
380
+ {
381
+ double v;
382
+ ck_assert(pdb_line_check("", 6) == FREESASA_FAIL);
383
+ ck_assert(pdb_line_check("ATOM", 4) == FREESASA_FAIL);
384
+ ck_assert(pdb_line_check("ATOM", 6) == FREESASA_FAIL);
385
+ ck_assert(pdb_line_check("HETAT", 6) == FREESASA_FAIL);
386
+ ck_assert(pdb_line_check("HETAT ", 6) == FREESASA_FAIL);
387
+ ck_assert(pdb_line_check("BLA BLA BLA", 10) == FREESASA_FAIL);
388
+ ck_assert(pdb_line_check("BLA BLA BLA", 11) == FREESASA_FAIL);
389
+ ck_assert(pdb_line_check("BLA BLA BLA", 12) == FREESASA_FAIL);
390
+
391
+ /* these will pass, although they would be useless */
392
+ ck_assert(pdb_line_check("ATOM ", 6) == FREESASA_SUCCESS);
393
+ ck_assert(pdb_line_check("HETATM", 6) == FREESASA_SUCCESS);
394
+
395
+ /* a more likely type of error */
396
+ ck_assert(pdb_line_check("HETATM", 7) == FREESASA_FAIL);
397
+
398
+ /* The normal case */
399
+ ck_assert(pdb_line_check("ATOM 1 N MET A 1 27.340 "
400
+ "24.430 2.614 1.00 9.67 N ",
401
+ 80) == FREESASA_SUCCESS);
402
+
403
+ v = 0;
404
+ ck_assert(pdb_get_double("1.23", 4, &v) == FREESASA_SUCCESS);
405
+ ck_assert(fabs(1.23 - v) < 1e-5);
406
+ v = 0;
407
+ ck_assert(pdb_get_double(" 1.23", 5, &v) == FREESASA_SUCCESS);
408
+ ck_assert(fabs(1.23 - v) < 1e-5);
409
+ v = 0;
410
+ ck_assert(pdb_get_double("1.23", 10, &v) == FREESASA_SUCCESS);
411
+ ck_assert(fabs(1.23 - v) < 1e-5);
412
+ v = 0;
413
+ ck_assert(pdb_get_double("1.23 4.56", 10, &v) == FREESASA_SUCCESS);
414
+ ck_assert(fabs(1.23 - v) < 1e-5);
415
+
416
+ ck_assert(pdb_get_double(" ", 10, &v) == FREESASA_FAIL);
417
+ ck_assert(pdb_get_double(" 1.23", 4, &v) == FREESASA_FAIL);
418
+ ck_assert(pdb_get_double("abc", 10, &v) == FREESASA_FAIL);
419
+ ck_assert(pdb_get_double("a 1.23", 6, &v) == FREESASA_FAIL);
420
+ }
421
+ END_TEST
422
+
423
+ TCase *
424
+ test_pdb_static()
425
+ {
426
+ TCase *tc = tcase_create("pdb.c static");
427
+ tcase_add_test(tc, test_pdb);
428
+
429
+ return tc;
430
+ }
431
+
432
+ #endif /* USE_CHECK */
.venv_haddock/include/site/python3.12/freesasa/pdb.h ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef FREESASA_PDB_H
2
+ #define FREESASA_PDB_H
3
+
4
+ #include "freesasa.h"
5
+ #include "freesasa_internal.h"
6
+
7
+ /**
8
+ @file
9
+ @author Simon Mitternacht
10
+
11
+ The following functions all extract info from the PDB lines `ATOM`
12
+ and `HETATM`. Valid lines have to begin with either `ATOM` or
13
+ `HETATM` and be sufficiently long to contain the value in
14
+ question.
15
+ */
16
+
17
+ #define PDB_ATOM_NAME_STRL 4 /**< Length of strings with atom names, such as `" CA "`. */
18
+ #define PDB_ATOM_RES_NAME_STRL 3 /**< Length of string with residue names, such as `"ALA"`. */
19
+ #define PDB_ATOM_RES_NUMBER_STRL 5 /**< Length of string with residue number, such as `" 123"`. */
20
+ #define PDB_ATOM_SYMBOL_STRL 2 /**< Length for string with element symbol, such "FE". */
21
+ #define PDB_LINE_STRL 80 /**< Length of a line in PDB file. */
22
+ #define PDB_MAX_LINE_STRL 120 /**< for reading, allows nonstandard input with extra fields. */
23
+
24
+ /**
25
+ Finds the location of all MODEL entries in the file pdb, returns
26
+ the number of models found.
27
+
28
+ The array *ranges will be dynamically allocated to contain a
29
+ file ranges for each model.
30
+
31
+ @return Returns 0 if no MODEL lines were found (for example an
32
+ X-ray structure with only one model) and sets *ranges to
33
+ NULL. A return value of 0 doesn't have to mean the file is
34
+ empty. ::FREESASA_FAIL if malloc-failure.
35
+ */
36
+ int freesasa_pdb_get_models(FILE *pdb,
37
+ struct file_range **ranges);
38
+
39
+ /**
40
+ Finds the location of all chains within the file range 'model'.
41
+
42
+ @param pdb The pdb-file
43
+ @param model The ::file_range to search for chains within.
44
+ @param ranges The address to a dynamically allocated array
45
+ containing the ::file_range of each chain will be stored here.
46
+ @param options Bitfield, can be used to set
47
+ ::FREESASA_INCLUDE_HETATM.
48
+ @return Number of chains found. Returns ::FREESASA_FAIL if memory
49
+ allocation fails.
50
+ */
51
+ int freesasa_pdb_get_chains(FILE *pdb,
52
+ struct file_range model,
53
+ struct file_range **ranges,
54
+ int options);
55
+ /**
56
+ Get atom name from a PDB line.
57
+
58
+ Extracts the whole atom name field from an `ATOM` or `HETATM` PDB
59
+ line, including padding, i.e. a string of ::PDB_ATOM_NAME_STRL
60
+ characters. For example `" CA "` for a regular C-alpha. If the
61
+ line is invalid, the name will be an empty string and the function
62
+ returns ::FREESASA_FAIL.
63
+
64
+ @param name The name is written to this string.
65
+ @param line Line from a PDB file.
66
+ @return ::FREESASA_SUCCESS if input is readable, else ::FREESASA_FAIL.
67
+ */
68
+ int freesasa_pdb_get_atom_name(char *name,
69
+ const char *line);
70
+
71
+ /**
72
+ Get residue name from a PDB line.
73
+
74
+ Extracts the whole residue name from an `ATOM` or `HETATM` PDB
75
+ line, i.e. a string of ::PDB_ATOM_RES_NAME_STRL characters. For
76
+ example `"ALA"` for an Alanine. If theline is invalid, the name
77
+ will be an empty string and the function returns ::FREESASA_FAIL.
78
+
79
+ @param name The name is written to this string.
80
+ @param line Line from a PDB file.
81
+ @return ::FREESASA_SUCCESS if input is readable, else ::FREESASA_FAIL.
82
+ */
83
+ int freesasa_pdb_get_res_name(char *name,
84
+ const char *line);
85
+
86
+ /**
87
+ Get atom coordinates from a PDB line.
88
+
89
+ Extracts x-, y- and z-coordinates from an `ATOM` or `HETATM` PDB
90
+ line. If the line is invalid, the function returns ::FREESASA_FAIL
91
+ and coord will remain unchanged.
92
+
93
+ @param coord The coordiantes are written to this array as x,y,z.
94
+ @param line Line from a PDB file.
95
+ @return ::FREESASA_SUCCESS if input is readable, else ::FREESASA_FAIL.
96
+ */
97
+ int freesasa_pdb_get_coord(double *coord,
98
+ const char *line);
99
+
100
+ /**
101
+ Get residue number from a PDB line.
102
+
103
+ Extracts residue number (ResSeq) as a string from an `ATOM` or
104
+ `HETATM` PDB line as a string. String format is used because not
105
+ all residue-numbers are numbers. The string should have length
106
+ ::PDB_ATOM_RES_NUMBER_STRL. If the line is invalid, the number will be an
107
+ empty string and the function returns ::FREESASA_FAIL.
108
+
109
+ @param number The residue number will be saved to this string.
110
+ @param line Line from a PDB file.
111
+ @return ::FREESASA_SUCCESS if input is readable, else ::FREESASA_FAIL.
112
+ */
113
+ int freesasa_pdb_get_res_number(char *number,
114
+ const char *line);
115
+
116
+ /**
117
+ Get chain label from PDB line.
118
+
119
+ Extracts the one character chain label (Chain identifier) from an
120
+ `ATOM` or `HETATM` PDB line (i.e. `'A'`, `'B'`, `'C'`, ...)
121
+
122
+ @param line Line from a PDB file.
123
+ @return The chain label. If the line is invalid, the function
124
+ returns '\0'.
125
+ */
126
+ char freesasa_pdb_get_chain_label(const char *line);
127
+
128
+ /**
129
+ Get alternate coordinate label from PDB line.
130
+
131
+ If there is more than one set of coordinates for an atom there
132
+ will be a label 'A', 'B', etc (Alternate location indicator). Else
133
+ the label is ' '.
134
+
135
+ @param line Line from a PDB file.
136
+ @return The label. If line is invalid, the function returns
137
+ '\0'.
138
+ */
139
+ char freesasa_pdb_get_alt_coord_label(const char *line);
140
+
141
+ /**
142
+ Get element symbol from PDB line.
143
+
144
+ Writes padded string to argument symbol, i.e. " C", "SE", etc.
145
+
146
+ @param symbol The symbol will be written to this string.
147
+ @param line Line from a PDB file.
148
+ @return ::FREESASA_SUCCESS line has 78 or more characters. ::FREESASA_FAIL if
149
+ line too short. Does not check if symbol is valid.
150
+ */
151
+ int freesasa_pdb_get_symbol(char *symbol,
152
+ const char *line);
153
+
154
+ /**
155
+ Get occupancy from PDB line
156
+
157
+ @param occ The occupancy will be written to this location.
158
+ @param line Line from a PDB file
159
+
160
+ @return ::FREESASA_SUCCESS if line is long enough, starts with
161
+ ATOM or HETATM, and characters 55-60 contain a number.
162
+ */
163
+ int freesasa_pdb_get_occupancy(double *occ,
164
+ const char *line);
165
+ /**
166
+ Get temperature factor (B-factor) from PDB line
167
+
168
+ @param bfac The B-factor will be written to this location.
169
+ @param line Line from a PDB file
170
+ @return ::FREESASA_SUCCESS if line is long enough, starts with
171
+ ATOM or HETATM, and characters 61-66 contain a number.
172
+ */
173
+ int freesasa_pdb_get_bfactor(double *bfac,
174
+ const char *line);
175
+
176
+ /**
177
+ Is atom Hydrogen?
178
+
179
+ Checks if an atom from an `ATOM` or `HETATM` PDB line is Hydrogen.
180
+
181
+ @param line Line from a PDB file.
182
+ @return 1 if Hydrogen (or deuterium). 0 otherwise. If line is
183
+ invalid, the function returns ::FREESASA_FAIL.
184
+ */
185
+ int freesasa_pdb_ishydrogen(const char *line);
186
+
187
+ #endif /* FREESASA_PDB_H */
.venv_haddock/include/site/python3.12/freesasa/rsa.c ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if HAVE_CONFIG_H
2
+ #include <config.h>
3
+ #endif
4
+
5
+ #include <assert.h>
6
+ #include <errno.h>
7
+ #include <math.h>
8
+ #include <stdlib.h>
9
+
10
+ #include "classifier.h"
11
+ #include "freesasa_internal.h"
12
+ #include "pdb.h"
13
+
14
+ void freesasa_residue_rel_nodearea(freesasa_nodearea *rel,
15
+ const freesasa_nodearea *abs,
16
+ const freesasa_nodearea *ref)
17
+ {
18
+ rel->total = 100. * abs->total / ref->total;
19
+ rel->side_chain = 100. * abs->side_chain / ref->side_chain;
20
+ rel->main_chain = 100. * abs->main_chain / ref->main_chain;
21
+ rel->polar = 100. * abs->polar / ref->polar;
22
+ rel->apolar = 100. * abs->apolar / ref->apolar;
23
+ rel->name = abs->name;
24
+ }
25
+
26
+ static void
27
+ rsa_print_header(FILE *output,
28
+ const char *config_name,
29
+ const char *protein_name,
30
+ const char *chains,
31
+ const freesasa_parameters *parameters,
32
+ int options)
33
+ {
34
+ freesasa_algorithm alg = parameters->alg;
35
+ #ifdef PACKAGE_VERSION
36
+ fprintf(output, "REM FreeSASA " PACKAGE_VERSION "\n");
37
+ #else
38
+ fprintf(output, "REM FreeSASA\n");
39
+ #endif
40
+ fprintf(output, "REM Absolute and relative SASAs for %s\n", protein_name);
41
+ if (!(options & FREESASA_OUTPUT_SKIP_REL))
42
+ fprintf(output, "REM Atomic radii and reference values for relative SASA: %s\n", config_name);
43
+ else
44
+ fprintf(output, "REM No reference values available to calculate relative SASA\n");
45
+ fprintf(output, "REM Chains: %s\n", chains);
46
+ fprintf(output, "REM Algorithm: %s\n", freesasa_alg_name(alg));
47
+ fprintf(output, "REM Probe-radius: %.2f\n", parameters->probe_radius);
48
+ if (alg == FREESASA_LEE_RICHARDS) {
49
+ fprintf(output, "REM Slices: %d\n", parameters->lee_richards_n_slices);
50
+ } else if (alg == FREESASA_SHRAKE_RUPLEY) {
51
+ fprintf(output, "REM Test-points: %d\n", parameters->shrake_rupley_n_points);
52
+ }
53
+ fprintf(output, "REM RES _ NUM All-atoms Total-Side Main-Chain Non-polar All polar\n");
54
+ fprintf(output, "REM ABS REL ABS REL ABS REL ABS REL ABS REL\n");
55
+ }
56
+
57
+ static inline void
58
+ rsa_print_abs_rel(FILE *output,
59
+ double abs,
60
+ double rel)
61
+ {
62
+ fprintf(output, "%7.2f", abs);
63
+ if (isfinite(rel))
64
+ fprintf(output, "%6.1f", rel);
65
+ else
66
+ fprintf(output, " N/A");
67
+ }
68
+
69
+ static inline void
70
+ rsa_print_abs_only(FILE *output,
71
+ double abs)
72
+ {
73
+ fprintf(output, "%7.2f", abs);
74
+ fprintf(output, " N/A");
75
+ }
76
+
77
+ static int
78
+ rsa_print_residue(FILE *output,
79
+ int iaa,
80
+ const freesasa_nodearea *abs,
81
+ const freesasa_nodearea *rel,
82
+ freesasa_node *residue)
83
+ {
84
+ const char *resi_str;
85
+ char chain;
86
+
87
+ resi_str = freesasa_node_residue_number(residue);
88
+ chain = freesasa_node_name(freesasa_node_parent(residue))[0];
89
+
90
+ fprintf(output, "RES %s %c%s ", abs->name, chain, resi_str);
91
+ if (rel->name != NULL) {
92
+ rsa_print_abs_rel(output, abs->total, rel->total);
93
+ rsa_print_abs_rel(output, abs->side_chain, rel->side_chain);
94
+ rsa_print_abs_rel(output, abs->main_chain, rel->main_chain);
95
+ rsa_print_abs_rel(output, abs->apolar, rel->apolar);
96
+ rsa_print_abs_rel(output, abs->polar, rel->polar);
97
+ } else {
98
+ rsa_print_abs_only(output, abs->total);
99
+ rsa_print_abs_only(output, abs->side_chain);
100
+ rsa_print_abs_only(output, abs->main_chain);
101
+ rsa_print_abs_only(output, abs->apolar);
102
+ rsa_print_abs_only(output, abs->polar);
103
+ }
104
+ fprintf(output, "\n");
105
+ return FREESASA_SUCCESS;
106
+ }
107
+
108
+ int freesasa_write_rsa(FILE *output,
109
+ freesasa_node *tree,
110
+ int options)
111
+ {
112
+ freesasa_node *residue, *chain, *structure_node, *result_node;
113
+ const freesasa_nodearea *abs, *reference;
114
+ freesasa_nodearea rel;
115
+ int res_index, chain_index;
116
+ const freesasa_parameters *parameters;
117
+
118
+ assert(output);
119
+ assert(tree);
120
+
121
+ result_node = freesasa_node_children(tree);
122
+ parameters = freesasa_node_result_parameters(result_node);
123
+ structure_node = freesasa_node_children(result_node);
124
+ chain = freesasa_node_children(structure_node);
125
+
126
+ rsa_print_header(output, freesasa_node_classified_by(result_node),
127
+ freesasa_node_name(result_node), freesasa_node_name(structure_node), parameters, options);
128
+
129
+ res_index = chain_index = 0;
130
+ while (chain) {
131
+ residue = freesasa_node_children(chain);
132
+ while (residue) {
133
+ abs = freesasa_node_area(residue);
134
+ reference = freesasa_node_residue_reference(residue);
135
+ if (reference && !(options & FREESASA_OUTPUT_SKIP_REL)) {
136
+ freesasa_residue_rel_nodearea(&rel, abs, reference);
137
+ } else {
138
+ rel = freesasa_nodearea_null;
139
+ }
140
+ rsa_print_residue(output, res_index, abs, &rel, residue);
141
+ ++res_index;
142
+ residue = freesasa_node_next(residue);
143
+ }
144
+ chain = freesasa_node_next(chain);
145
+ }
146
+
147
+ fprintf(output, "END Absolute sums over single chains surface\n");
148
+
149
+ chain = freesasa_node_children(structure_node);
150
+ chain_index = 0;
151
+ while (chain) {
152
+ const char *name = freesasa_node_name(chain);
153
+ abs = freesasa_node_area(chain);
154
+
155
+ fprintf(output, "CHAIN%3d %c %10.1f %10.1f %10.1f %10.1f %10.1f\n",
156
+ chain_index + 1, name[0], abs->total, abs->side_chain,
157
+ abs->main_chain, abs->apolar, abs->polar);
158
+
159
+ ++chain_index;
160
+ chain = freesasa_node_next(chain);
161
+ }
162
+
163
+ abs = freesasa_node_area(structure_node);
164
+ fprintf(output, "END Absolute sums over all chains\n");
165
+ fprintf(output, "TOTAL %10.1f %10.1f %10.1f %10.1f %10.1f\n",
166
+ abs->total, abs->side_chain, abs->main_chain, abs->apolar, abs->polar);
167
+
168
+ fflush(output);
169
+ if (ferror(output)) {
170
+ return fail_msg(strerror(errno));
171
+ }
172
+
173
+ return FREESASA_SUCCESS;
174
+ }
.venv_haddock/include/site/python3.12/freesasa/sasa_lr.c ADDED
@@ -0,0 +1,487 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if HAVE_CONFIG_H
2
+ #include <config.h>
3
+ #endif
4
+ #include <assert.h>
5
+ #include <errno.h>
6
+ #include <stdio.h>
7
+ #include <stdlib.h>
8
+ #include <string.h>
9
+
10
+ #ifdef _MSC_VER
11
+ #define _USE_MATH_DEFINES
12
+ #endif
13
+ #include <math.h>
14
+
15
+ #if USE_THREADS
16
+ #include <pthread.h>
17
+ #define MAX_LR_THREADS 16
18
+ #else
19
+ #define MAX_LR_THREADS 1
20
+ #endif
21
+
22
+ #include "freesasa_internal.h"
23
+ #include "nb.h"
24
+
25
+ const double TWOPI = 2 * M_PI;
26
+
27
+ /* calculation parameters and data (results stored in *sasa) */
28
+ typedef struct {
29
+ int n_atoms;
30
+ double *radii; /* including probe */
31
+ const coord_t *xyz;
32
+ nb_list *adj;
33
+ int n_slices_per_atom;
34
+ double *sasa; /* results */
35
+ double *arc[MAX_LR_THREADS], *z_nb[MAX_LR_THREADS], *R_nb[MAX_LR_THREADS];
36
+ int n_threads;
37
+ } lr_data;
38
+
39
+ typedef struct {
40
+ int first_atom;
41
+ int last_atom;
42
+ int thread_id;
43
+ lr_data *lr;
44
+ } lr_thread_interval;
45
+
46
+ #if USE_THREADS
47
+ static int lr_do_threads(int n_threads, lr_data *);
48
+ static void *lr_thread(void *arg);
49
+ #endif
50
+
51
+ /** Returns the are of atom i */
52
+ static double
53
+ atom_area(lr_data *lr, int i, int thread_id);
54
+
55
+ /** Sum of exposed arcs based on buried arc intervals arc, assumes no
56
+ intervals cross zero */
57
+ static double
58
+ exposed_arc_length(double *restrict arc, int n);
59
+
60
+ /** Release contenst of lr_data pointer*/
61
+ static void
62
+ release_lr(lr_data *lr)
63
+ {
64
+ int i;
65
+
66
+ free(lr->radii);
67
+ freesasa_nb_free(lr->adj);
68
+ lr->radii = NULL;
69
+ lr->adj = NULL;
70
+
71
+ for (i = 0; i < lr->n_threads; ++i) {
72
+ free(lr->arc[i]);
73
+ free(lr->z_nb[i]);
74
+ free(lr->R_nb[i]);
75
+ }
76
+ }
77
+
78
+ /* Allocate some helper arrays in area calculation that need to be pre-allocated */
79
+ static int
80
+ alloc_lr_calc_arrays(lr_data *lr, int n_threads)
81
+ {
82
+ int max_nni = 0, i, nni;
83
+ const int n_atoms = lr->n_atoms;
84
+
85
+ for (i = 0; i < n_atoms; ++i) {
86
+ nni = lr->adj->nn[i];
87
+ max_nni = max_nni < nni ? nni : max_nni;
88
+ }
89
+
90
+ for (i = 0; i < n_threads; ++i) {
91
+ lr->arc[i] = malloc(sizeof(double) * 4 * max_nni);
92
+ lr->z_nb[i] = malloc(sizeof(double) * max_nni);
93
+ lr->R_nb[i] = malloc(sizeof(double) * max_nni);
94
+
95
+ if (!lr->arc[i] || !lr->z_nb[i] || !lr->R_nb[i]) {
96
+ return mem_fail();
97
+ }
98
+ }
99
+
100
+ return FREESASA_SUCCESS;
101
+ }
102
+
103
+ /** Initialize object to be used for L&R calculation */
104
+ static int
105
+ init_lr(lr_data *lr,
106
+ double *sasa,
107
+ const coord_t *xyz,
108
+ const double *atom_radii,
109
+ double probe_radius,
110
+ int n_slices_per_atom,
111
+ int n_threads)
112
+ {
113
+ const int n_atoms = freesasa_coord_n(xyz);
114
+ int i;
115
+
116
+ lr->n_atoms = n_atoms;
117
+ lr->xyz = xyz;
118
+ lr->adj = NULL;
119
+ lr->n_slices_per_atom = n_slices_per_atom;
120
+ lr->sasa = sasa;
121
+ lr->n_threads = n_threads;
122
+
123
+ for (i = 0; i < n_threads; ++i) {
124
+ lr->arc[i] = NULL;
125
+ lr->z_nb[i] = NULL;
126
+ lr->R_nb[i] = NULL;
127
+ }
128
+
129
+ lr->radii = malloc(sizeof(double) * n_atoms);
130
+ if (lr->radii == NULL) {
131
+ return mem_fail();
132
+ }
133
+
134
+ /* init some arrays */
135
+ for (i = 0; i < n_atoms; ++i) {
136
+ lr->radii[i] = atom_radii[i] + probe_radius;
137
+ sasa[i] = 0.;
138
+ }
139
+
140
+ /* determine which atoms are neighbours */
141
+ lr->adj = freesasa_nb_new(xyz, lr->radii);
142
+
143
+ if (lr->adj == NULL) {
144
+ release_lr(lr);
145
+ return fail_msg("");
146
+ }
147
+
148
+ if (alloc_lr_calc_arrays(lr, n_threads)) {
149
+ release_lr(lr);
150
+ return fail_msg("");
151
+ }
152
+
153
+ return FREESASA_SUCCESS;
154
+ }
155
+
156
+ int freesasa_lee_richards(double *sasa,
157
+ const coord_t *xyz,
158
+ const double *atom_radii,
159
+ const freesasa_parameters *param)
160
+ {
161
+ int return_value, n_atoms, n_threads, resolution, i;
162
+ double probe_radius;
163
+ lr_data lr;
164
+
165
+ assert(sasa);
166
+ assert(xyz);
167
+ assert(atom_radii);
168
+
169
+ if (param == NULL) param = &freesasa_default_parameters;
170
+
171
+ return_value = FREESASA_SUCCESS;
172
+ n_atoms = freesasa_coord_n(xyz);
173
+ n_threads = param->n_threads;
174
+ resolution = param->lee_richards_n_slices;
175
+ probe_radius = param->probe_radius;
176
+
177
+ if (n_threads > MAX_LR_THREADS) {
178
+ return fail_msg("L&R does not support more than %d threads", MAX_LR_THREADS);
179
+ }
180
+
181
+ if (resolution <= 0) {
182
+ return fail_msg("%f slices per atom invalid resolution in L&R, must be > 0\n", resolution);
183
+ }
184
+
185
+ if (n_atoms == 0) {
186
+ return freesasa_warn("in %s(): empty coordinates", __func__);
187
+ }
188
+
189
+ if (n_threads > n_atoms) {
190
+ n_threads = n_atoms;
191
+ freesasa_warn("no sense in having more threads than atoms, only using %d threads",
192
+ n_threads);
193
+ }
194
+
195
+ if (init_lr(&lr, sasa, xyz, atom_radii, probe_radius, resolution, n_threads))
196
+ return FREESASA_FAIL;
197
+
198
+ if (n_threads > 1) {
199
+ #if USE_THREADS
200
+ return_value = lr_do_threads(n_threads, &lr);
201
+ #else
202
+ return_value = freesasa_warn("in %s(): program compiled for single-threaded use, "
203
+ "but multiple threads were requested, will "
204
+ "proceed in single-threaded mode\n",
205
+ __func__);
206
+ n_threads = 1;
207
+ #endif /* pthread */
208
+ }
209
+ if (n_threads == 1) {
210
+ for (i = 0; i < lr.n_atoms; ++i) {
211
+ lr.sasa[i] = atom_area(&lr, i, 0);
212
+ }
213
+ }
214
+ release_lr(&lr);
215
+ return return_value;
216
+ }
217
+
218
+ #if USE_THREADS
219
+ static int
220
+ lr_do_threads(int n_threads,
221
+ lr_data *lr)
222
+ {
223
+ pthread_t thread[MAX_LR_THREADS];
224
+ lr_thread_interval t_data[MAX_LR_THREADS];
225
+ int n_perthread = lr->n_atoms / n_threads, res;
226
+ int threads_created = 0, return_value = FREESASA_SUCCESS;
227
+ int t;
228
+
229
+ for (t = 0; t < n_threads; ++t) {
230
+ t_data[t].first_atom = t * n_perthread;
231
+ if (t == n_threads - 1) {
232
+ t_data[t].last_atom = lr->n_atoms - 1;
233
+ } else {
234
+ t_data[t].last_atom = (t + 1) * n_perthread - 1;
235
+ }
236
+ t_data[t].lr = lr;
237
+ t_data[t].thread_id = t;
238
+ res = pthread_create(&thread[t], NULL, lr_thread,
239
+ (void *)&t_data[t]);
240
+ if (res) {
241
+ return_value = fail_msg(freesasa_thread_error(res));
242
+ break;
243
+ }
244
+ ++threads_created;
245
+ }
246
+ for (int t = 0; t < threads_created; ++t) {
247
+ res = pthread_join(thread[t], NULL);
248
+ if (res) {
249
+ return_value = fail_msg(freesasa_thread_error(res));
250
+ }
251
+ }
252
+ return return_value;
253
+ }
254
+
255
+ static void *
256
+ lr_thread(void *arg)
257
+ {
258
+ int i;
259
+ lr_thread_interval *ti = ((lr_thread_interval *)arg);
260
+
261
+ for (i = ti->first_atom; i <= ti->last_atom; ++i) {
262
+ /* the different threads write to different parts of the
263
+ array, so locking shouldn't be necessary */
264
+ ti->lr->sasa[i] = atom_area(ti->lr, i, ti->thread_id);
265
+ }
266
+ pthread_exit(NULL);
267
+ }
268
+ #endif /* USE_THREADS */
269
+
270
+ static double
271
+ atom_area(lr_data *lr,
272
+ int i,
273
+ int thread_id)
274
+ {
275
+ /* This function is large because a large number of pre-calculated
276
+ arrays need to be accessed efficiently. Partially dereferenced
277
+ here to make access more efficient.
278
+
279
+ Variables are named according to the documentation (see page
280
+ "Geometry of Lee & Richards' algorithm") */
281
+
282
+ const int nni = lr->adj->nn[i];
283
+ const double *restrict const v = freesasa_coord_all(lr->xyz);
284
+ const double *restrict const R = lr->radii;
285
+ const int *restrict const nbi = lr->adj->nb[i];
286
+ const double *restrict const xydi = lr->adj->xyd[i];
287
+ const double *restrict const xdi = lr->adj->xd[i];
288
+ const double *restrict const ydi = lr->adj->yd[i];
289
+ const double zi = v[3 * i + 2], Ri = R[i];
290
+ const int ns = lr->n_slices_per_atom;
291
+
292
+ int j, islice, n_arcs, is_buried, narc2;
293
+ double *arc = lr->arc[thread_id],
294
+ *z_nb = lr->z_nb[thread_id],
295
+ *R_nb = lr->R_nb[thread_id];
296
+ double z, delta, sasa = 0, alpha, beta, inf, sup;
297
+ double zj, di, dj, dij, Rj, Ri_prime2, Ri_prime, Rj_prime2, Rj_prime;
298
+
299
+ for (j = 0; j < nni; ++j) {
300
+ z_nb[j] = v[3 * nbi[j] + 2];
301
+ R_nb[j] = R[nbi[j]];
302
+ }
303
+
304
+ delta = 2 * Ri / ns;
305
+ z = zi - Ri - 0.5 * delta;
306
+ for (islice = 0; islice < ns; ++islice) {
307
+ z += delta;
308
+ di = fabs(zi - z);
309
+ Ri_prime2 = Ri * Ri - di * di;
310
+ if (Ri_prime2 < 0) continue; /* handle round-off errors */
311
+ Ri_prime = sqrt(Ri_prime2);
312
+ if (Ri_prime <= 0) continue; /* more round-off errors */
313
+ n_arcs = 0;
314
+ is_buried = 0;
315
+ for (j = 0; j < nni; ++j) {
316
+ zj = z_nb[j];
317
+ dj = fabs(zj - z);
318
+ Rj = R_nb[j];
319
+
320
+ if (dj < Rj) {
321
+ Rj_prime2 = Rj * Rj - dj * dj;
322
+ Rj_prime = sqrt(Rj_prime2);
323
+ dij = xydi[j];
324
+ if (dij >= Ri_prime + Rj_prime) { /* atoms aren't in contact */
325
+ continue;
326
+ }
327
+ if (dij + Ri_prime < Rj_prime) { /* circle i is completely inside j */
328
+ is_buried = 1;
329
+ break;
330
+ }
331
+ if (dij + Rj_prime < Ri_prime) { /* circle j is completely inside i */
332
+ continue;
333
+ }
334
+ /* arc of circle i intersected by circle j */
335
+ alpha = acos((Ri_prime2 + dij * dij - Rj_prime2) / (2.0 * Ri_prime * dij));
336
+ /* position of mid-point of intersection along circle i */
337
+ beta = atan2(ydi[j], xdi[j]) + M_PI;
338
+ inf = beta - alpha;
339
+ sup = beta + alpha;
340
+ if (inf < 0) inf += TWOPI;
341
+ if (sup > 2 * M_PI) sup -= TWOPI;
342
+ narc2 = 2 * n_arcs;
343
+ /* store the arc, if arc passes 2*PI split into two */
344
+ if (sup < inf) {
345
+ /* store arcs as contiguous pairs of angles */
346
+ arc[narc2] = 0;
347
+ arc[narc2 + 1] = sup;
348
+ /* second arc */
349
+ arc[narc2 + 2] = inf;
350
+ arc[narc2 + 3] = TWOPI;
351
+ n_arcs += 2;
352
+ } else {
353
+ arc[narc2] = inf;
354
+ arc[narc2 + 1] = sup;
355
+ ++n_arcs;
356
+ }
357
+ }
358
+ }
359
+ if (is_buried == 0) {
360
+ sasa += delta * Ri * exposed_arc_length(arc, n_arcs);
361
+ }
362
+ }
363
+ return sasa;
364
+ }
365
+
366
+ /* insertion sort (faster than qsort for these short lists) */
367
+ inline static void
368
+ sort_arcs(double *restrict arc,
369
+ int n)
370
+ {
371
+ double tmp[2];
372
+ double *end = arc + 2 * n, *arcj, *arci;
373
+ for (arci = arc + 2; arci < end; arci += 2) {
374
+ *tmp = *arci;
375
+ *(tmp + 1) = *(arci + 1);
376
+ arcj = arci;
377
+ while (arcj > arc && *(arcj - 2) > tmp[0]) {
378
+ *arcj = *(arcj - 2);
379
+ *(arcj + 1) = *(arcj - 1);
380
+ arcj -= 2;
381
+ }
382
+ *arcj = *tmp;
383
+ *(arcj + 1) = *(tmp + 1);
384
+ }
385
+ }
386
+
387
+ /* sort arcs by start-point, loop through them to sum parts of circle
388
+ not covered by any of the arcs */
389
+ inline static double
390
+ exposed_arc_length(double *restrict arc,
391
+ int n)
392
+ {
393
+ int i2;
394
+ double sum, sup, tmp;
395
+
396
+ if (n == 0) return TWOPI;
397
+
398
+ sort_arcs(arc, n);
399
+ sum = arc[0];
400
+ sup = arc[1];
401
+ /* in the following it is assumed that the arc[i2] <= arc[i2+1] */
402
+ for (i2 = 2; i2 < 2 * n; i2 += 2) {
403
+ if (sup < arc[i2]) sum += arc[i2] - sup;
404
+ tmp = arc[i2 + 1];
405
+ if (tmp > sup) sup = tmp;
406
+ }
407
+ return sum + TWOPI - sup;
408
+ }
409
+
410
+ #if USE_CHECK
411
+ #include <check.h>
412
+
413
+ static int
414
+ is_identical(const double *l1, const double *l2, int n)
415
+ {
416
+ int i;
417
+
418
+ for (i = 0; i < n; ++i) {
419
+ if (l1[i] != l2[i]) return 0;
420
+ }
421
+
422
+ return 1;
423
+ }
424
+
425
+ static int
426
+ is_sorted(const double *list, int n)
427
+ {
428
+ int i;
429
+
430
+ for (i = 0; i < n - 1; ++i)
431
+ if (list[2 * i] > list[2 * i + 1]) return 0;
432
+
433
+ return 1;
434
+ }
435
+
436
+ START_TEST(test_sort_arcs)
437
+ {
438
+ double a_ref[] = {0, 1, 2, 3}, b_ref[] = {-2, 0, -1, 0, -1, 1};
439
+ double a1[4] = {0, 1, 2, 3}, a2[4] = {2, 3, 0, 1};
440
+ double b1[6] = {-2, 0, -1, 0, -1, 1}, b2[6] = {-1, 1, -2, 0, -1, 1};
441
+ sort_arcs(a1, 2);
442
+ sort_arcs(a2, 2);
443
+ sort_arcs(b1, 3);
444
+ sort_arcs(b2, 3);
445
+ ck_assert(is_sorted(a1, 2));
446
+ ck_assert(is_sorted(a2, 2));
447
+ ck_assert(is_sorted(b1, 3));
448
+ ck_assert(is_sorted(b2, 3));
449
+ ck_assert(is_identical(a_ref, a1, 4));
450
+ ck_assert(is_identical(a_ref, a2, 4));
451
+ ck_assert(is_identical(b_ref, b1, 6));
452
+ }
453
+ END_TEST
454
+
455
+ START_TEST(test_exposed_arc_length)
456
+ {
457
+ double a1[4] = {0, 0.1 * TWOPI, 0.9 * TWOPI, TWOPI}, a2[4] = {0.9 * TWOPI, TWOPI, 0, 0.1 * TWOPI};
458
+ double a3[4] = {0, TWOPI, 1, 2}, a4[4] = {1, 2, 0, TWOPI};
459
+ double a5[4] = {0.1 * TWOPI, 0.2 * TWOPI, 0.5 * TWOPI, 0.6 * TWOPI};
460
+ double a6[4] = {0.1 * TWOPI, 0.2 * TWOPI, 0.5 * TWOPI, 0.6 * TWOPI};
461
+ double a7[4] = {0.1 * TWOPI, 0.3 * TWOPI, 0.15 * TWOPI, 0.2 * TWOPI};
462
+ double a8[4] = {0.15 * TWOPI, 0.2 * TWOPI, 0.1 * TWOPI, 0.3 * TWOPI};
463
+ double a9[10] = {0.05, 0.1, 0.5, 0.6, 0, 0.15, 0.7, 0.8, 0.75, TWOPI};
464
+ ck_assert(fabs(exposed_arc_length(a1, 2) - 0.8 * TWOPI) < 1e-10);
465
+ ck_assert(fabs(exposed_arc_length(a2, 2) - 0.8 * TWOPI) < 1e-10);
466
+ ck_assert(fabs(exposed_arc_length(a3, 2)) < 1e-10);
467
+ ck_assert(fabs(exposed_arc_length(a4, 2)) < 1e-10);
468
+ ck_assert(fabs(exposed_arc_length(a5, 2) - 0.8 * TWOPI) < 1e-10);
469
+ ck_assert(fabs(exposed_arc_length(a6, 2) - 0.8 * TWOPI) < 1e-10);
470
+ ck_assert(fabs(exposed_arc_length(a7, 2) - 0.8 * TWOPI) < 1e-10);
471
+ ck_assert(fabs(exposed_arc_length(a8, 2) - 0.8 * TWOPI) < 1e-10);
472
+ ck_assert(fabs(exposed_arc_length(a9, 5) - 0.45) < 1e-10);
473
+ /* can't think of anything more qualitatively different here */
474
+ }
475
+ END_TEST
476
+
477
+ TCase *
478
+ test_LR_static()
479
+ {
480
+ TCase *tc = tcase_create("sasa_lr.c static");
481
+ tcase_add_test(tc, test_sort_arcs);
482
+ tcase_add_test(tc, test_exposed_arc_length);
483
+
484
+ return tc;
485
+ }
486
+
487
+ #endif /* USE_CHECK */
.venv_haddock/include/site/python3.12/freesasa/sasa_sr.c ADDED
@@ -0,0 +1,338 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if HAVE_CONFIG_H
2
+ #include <config.h>
3
+ #endif
4
+ #include <assert.h>
5
+ #include <stdio.h>
6
+ #include <stdlib.h>
7
+ #include <string.h>
8
+
9
+ #ifdef _MSC_VER
10
+ #define _USE_MATH_DEFINES
11
+ #endif
12
+ #include <math.h>
13
+
14
+ #if USE_THREADS
15
+ #include <pthread.h>
16
+ #define MAX_SR_THREADS 16
17
+ #else
18
+ #define MAX_SR_THREADS 1
19
+ #endif
20
+
21
+ #include "freesasa_internal.h"
22
+ #include "nb.h"
23
+
24
+ #ifdef __GNUC__
25
+ #define __attrib_pure__ __attribute__((pure))
26
+ #else
27
+ #define __attrib_pure__
28
+ #endif
29
+
30
+ /* calculation parameters (results stored in *sasa) */
31
+ typedef struct {
32
+ int i1, i2; /* for multithreading, range of atoms */
33
+ int thread_index;
34
+ int n_atoms;
35
+ int n_points;
36
+ int n_threads;
37
+ double probe_radius;
38
+ const coord_t *xyz;
39
+ coord_t *srp; /* test-points */
40
+ coord_t *tp_local[MAX_SR_THREADS]; /* coord object for storing intermediates */
41
+ int *spcount[MAX_SR_THREADS];
42
+ double *r;
43
+ double *r2;
44
+ nb_list *nb;
45
+ double *sasa;
46
+ } sr_data;
47
+
48
+ #if USE_THREADS
49
+ static int sr_do_threads(int n_threads, sr_data *sr);
50
+ static void *sr_thread(void *arg);
51
+ #endif
52
+
53
+ static double
54
+ sr_atom_area(int i, const sr_data *sr, int thread_index) __attrib_pure__;
55
+
56
+ static coord_t *
57
+ test_points(int N)
58
+ {
59
+ /* Golden section spiral on a sphere
60
+ from http://web.archive.org/web/20120421191837/http://www.cgafaq.info/wiki/Evenly_distributed_points_on_sphere */
61
+ double dlong = M_PI * (3 - sqrt(5)), dz = 2.0 / N, longitude = 0, z = 1 - dz / 2, r;
62
+ coord_t *coord = freesasa_coord_new();
63
+ double *tp = malloc(3 * N * sizeof(double)), *p;
64
+ if (tp == NULL || coord == NULL) {
65
+ mem_fail();
66
+ goto cleanup;
67
+ }
68
+
69
+ for (p = tp; p - tp < 3 * N; p += 3) {
70
+ r = sqrt(1 - z * z);
71
+ p[0] = cos(longitude) * r;
72
+ p[1] = sin(longitude) * r;
73
+ p[2] = z;
74
+ z -= dz;
75
+ longitude += dlong;
76
+ }
77
+
78
+ if (freesasa_coord_append(coord, tp, N) == FREESASA_FAIL) {
79
+ fail_msg("");
80
+ goto cleanup;
81
+ }
82
+ free(tp);
83
+
84
+ return coord;
85
+
86
+ cleanup:
87
+ free(tp);
88
+ freesasa_coord_free(coord);
89
+ return NULL;
90
+ }
91
+
92
+ /* free contents */
93
+ void release_sr(sr_data *sr)
94
+ {
95
+ int i;
96
+
97
+ freesasa_coord_free(sr->srp);
98
+ freesasa_nb_free(sr->nb);
99
+ free(sr->r);
100
+ free(sr->r2);
101
+
102
+ for (i = 0; i < sr->n_threads; ++i) {
103
+ freesasa_coord_free(sr->tp_local[i]);
104
+ free(sr->spcount[i]);
105
+ }
106
+ }
107
+
108
+ int init_sr(sr_data *sr,
109
+ double *sasa,
110
+ const coord_t *xyz,
111
+ const double *r,
112
+ double probe_radius,
113
+ int n_points,
114
+ int n_threads)
115
+ {
116
+ int n_atoms = freesasa_coord_n(xyz), i;
117
+ coord_t *srp = test_points(n_points);
118
+ double ri;
119
+
120
+ if (srp == NULL) return fail_msg("failed to initialize test points");
121
+
122
+ /* store parameters and reference arrays */
123
+ sr->n_atoms = n_atoms;
124
+ sr->n_points = n_points;
125
+ sr->n_threads = n_threads;
126
+ sr->probe_radius = probe_radius;
127
+ sr->xyz = xyz;
128
+ sr->srp = srp;
129
+ sr->sasa = sasa;
130
+ sr->nb = NULL;
131
+
132
+ /* should be done before any mallocs (to avoid problems in potential cleanup) */
133
+ for (i = 0; i < n_threads; ++i) {
134
+ sr->tp_local[i] = NULL;
135
+ sr->spcount[i] = NULL;
136
+ }
137
+
138
+ sr->r = malloc(sizeof(double) * n_atoms);
139
+ sr->r2 = malloc(sizeof(double) * n_atoms);
140
+
141
+ if (sr->r == NULL || sr->r2 == NULL) goto cleanup;
142
+
143
+ for (i = 0; i < n_atoms; ++i) {
144
+ ri = r[i] + probe_radius;
145
+ sr->r[i] = ri;
146
+ sr->r2[i] = ri * ri;
147
+ }
148
+
149
+ for (i = 0; i < n_threads; ++i) {
150
+ sr->tp_local[i] = freesasa_coord_clone(sr->srp);
151
+ sr->spcount[i] = malloc(sizeof(int) * n_points);
152
+ if (sr->tp_local[i] == NULL || sr->spcount[i] == NULL) {
153
+ goto cleanup;
154
+ }
155
+ }
156
+
157
+ /* calculate distances */
158
+ sr->nb = freesasa_nb_new(xyz, sr->r);
159
+ if (sr->nb == NULL) goto cleanup;
160
+
161
+ return FREESASA_SUCCESS;
162
+
163
+ cleanup:
164
+ release_sr(sr);
165
+ return mem_fail();
166
+ }
167
+
168
+ int freesasa_shrake_rupley(double *sasa,
169
+ const coord_t *xyz,
170
+ const double *r,
171
+ const freesasa_parameters *param)
172
+ {
173
+ int i, n_atoms, n_threads = param->n_threads, resolution, return_value;
174
+ double probe_radius = param->probe_radius;
175
+ sr_data sr;
176
+
177
+ assert(sasa);
178
+ assert(xyz);
179
+ assert(r);
180
+
181
+ if (param == NULL) param = &freesasa_default_parameters;
182
+
183
+ n_atoms = freesasa_coord_n(xyz);
184
+ n_threads = param->n_threads;
185
+ resolution = param->shrake_rupley_n_points;
186
+ return_value = FREESASA_SUCCESS;
187
+
188
+ if (n_threads > MAX_SR_THREADS) {
189
+ return fail_msg("S&R does not support more than %d threads", MAX_SR_THREADS);
190
+ }
191
+ if (resolution <= 0) {
192
+ return fail_msg("%f test points invalid resolution in S&R, must be > 0\n", resolution);
193
+ }
194
+ if (n_atoms == 0) return freesasa_warn("in %s(): empty coordinates", __func__);
195
+ if (n_threads > n_atoms) {
196
+ n_threads = n_atoms;
197
+ freesasa_warn("no sense in having more threads than atoms, only using %d threads",
198
+ n_threads);
199
+ }
200
+
201
+ if (init_sr(&sr, sasa, xyz, r, probe_radius, resolution, n_threads))
202
+ return FREESASA_FAIL;
203
+
204
+ /* calculate SASA */
205
+ if (n_threads > 1) {
206
+ #if USE_THREADS
207
+ return_value = sr_do_threads(n_threads, &sr);
208
+ #else
209
+ return_value = freesasa_warn("in %s(): program compiled for single-threaded use, "
210
+ "but multiple threads were requested, will "
211
+ "proceed in single-threaded mode\n",
212
+ __func__);
213
+ n_threads = 1;
214
+ #endif
215
+ }
216
+ if (n_threads == 1) {
217
+ /* don't want the overhead of generating threads if only one is used */
218
+ for (i = 0; i < n_atoms; ++i) {
219
+ sasa[i] = sr_atom_area(i, &sr, 0);
220
+ }
221
+ }
222
+ release_sr(&sr);
223
+ return return_value;
224
+ }
225
+
226
+ #if USE_THREADS
227
+ static int
228
+ sr_do_threads(int n_threads,
229
+ sr_data *sr)
230
+ {
231
+ pthread_t thread[MAX_SR_THREADS];
232
+ sr_data srt[MAX_SR_THREADS];
233
+ int thread_block_size = sr->n_atoms / n_threads;
234
+ int res, return_value = FREESASA_SUCCESS;
235
+ int threads_created = 0, t;
236
+
237
+ /* divide atoms evenly over threads */
238
+ for (t = 0; t < n_threads; ++t) {
239
+ srt[t] = *sr;
240
+ srt[t].i1 = t * thread_block_size;
241
+ if (t == n_threads - 1)
242
+ srt[t].i2 = sr->n_atoms;
243
+ else
244
+ srt[t].i2 = (t + 1) * thread_block_size;
245
+ srt[t].thread_index = t;
246
+ res = pthread_create(&thread[t], NULL, sr_thread, (void *)&srt[t]);
247
+ if (res) {
248
+ return_value = fail_msg(freesasa_thread_error(res));
249
+ break;
250
+ }
251
+ ++threads_created;
252
+ }
253
+ for (t = 0; t < threads_created; ++t) {
254
+ res = pthread_join(thread[t], NULL);
255
+ if (res) {
256
+ return_value = fail_msg(freesasa_thread_error(res));
257
+ }
258
+ }
259
+ return return_value;
260
+ }
261
+
262
+ static void *
263
+ sr_thread(void *arg)
264
+ {
265
+ int i;
266
+ sr_data *sr = ((sr_data *)arg);
267
+
268
+ for (i = sr->i1; i < sr->i2; ++i) {
269
+ /* mutex should not be necessary, writes to non-overlapping regions */
270
+ sr->sasa[i] = sr_atom_area(i, sr, sr->thread_index);
271
+ }
272
+ pthread_exit(NULL);
273
+ }
274
+ #endif
275
+
276
+ static double
277
+ sr_atom_area(int i,
278
+ const sr_data *sr,
279
+ int thread_index)
280
+ {
281
+ const int n_points = sr->n_points;
282
+ /* this array keeps track of which testpoints belonging to
283
+ a certain atom do not overlap with any other atoms */
284
+ int *spcount = sr->spcount[thread_index];
285
+ const int nni = sr->nb->nn[i];
286
+ const int *restrict nbi = sr->nb->nb[i];
287
+ const double ri = sr->r[i];
288
+ const double *restrict r2 = sr->r2;
289
+ const double *restrict v = freesasa_coord_all(sr->xyz);
290
+ const double *restrict vi = v + 3 * i;
291
+ const double *restrict tp;
292
+ int n_surface = 0, current_nb, a, j, k;
293
+ double dx, dy, dz;
294
+ /* testpoints for this atom */
295
+ coord_t *restrict tp_coord_ri = sr->tp_local[thread_index];
296
+
297
+ freesasa_coord_copy(tp_coord_ri, sr->srp);
298
+ freesasa_coord_scale(tp_coord_ri, ri);
299
+ freesasa_coord_translate(tp_coord_ri, vi);
300
+ tp = freesasa_coord_all(tp_coord_ri);
301
+
302
+ /* initialize with all surface points hidden */
303
+ memset(spcount, 0, n_points * sizeof(int));
304
+
305
+ /* Using the trick from NSOL to check points one by one for all
306
+ atoms, start comparing with the first neighbor. If there is no
307
+ overlap for a given test-point, try with other neighbors
308
+ instead. Would probably work even better if test points were
309
+ organized in patches and not spirals. */
310
+ current_nb = 0;
311
+ for (j = 0; j < n_points; ++j) {
312
+ /* a is the index of the atom under consideration */
313
+ a = nbi[current_nb];
314
+ dx = tp[j * 3] - v[a * 3];
315
+ dy = tp[j * 3 + 1] - v[a * 3 + 1];
316
+ dz = tp[j * 3 + 2] - v[a * 3 + 2];
317
+ if (dx * dx + dy * dy + dz * dz > r2[a]) {
318
+ k = 0;
319
+ for (; k < nni; ++k) {
320
+ a = nbi[k];
321
+ dx = tp[j * 3] - v[a * 3];
322
+ dy = tp[j * 3 + 1] - v[a * 3 + 1];
323
+ dz = tp[j * 3 + 2] - v[a * 3 + 2];
324
+ if (dx * dx + dy * dy + dz * dz <= r2[a]) {
325
+ current_nb = k;
326
+ break;
327
+ }
328
+ }
329
+ /* we have gone through the whole list without overlap */
330
+ if (k == nni) spcount[j] = 1;
331
+ }
332
+ }
333
+ for (k = 0; k < n_points; ++k) {
334
+ if (spcount[k]) ++n_surface;
335
+ }
336
+
337
+ return (4.0 * M_PI * ri * ri * n_surface) / n_points;
338
+ }
.venv_haddock/include/site/python3.12/freesasa/selection.c ADDED
@@ -0,0 +1,1003 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if HAVE_CONFIG_H
2
+ #include <config.h>
3
+ #endif
4
+
5
+ #include <assert.h>
6
+ #include <ctype.h>
7
+ #include <stdlib.h>
8
+
9
+ #include "freesasa_internal.h"
10
+ #include "pdb.h"
11
+ #include "selection.h"
12
+
13
+ #include "parser.h"
14
+
15
+ #include "lexer.h"
16
+
17
+ struct freesasa_selection {
18
+ char *name;
19
+ char *command;
20
+ double area;
21
+ int n_atoms;
22
+ };
23
+
24
+ struct selection {
25
+ const char *name;
26
+ int *atom;
27
+ int size;
28
+ };
29
+
30
+ static const char *
31
+ e_str(expression_type e)
32
+ {
33
+ switch (e) {
34
+ case E_SELECTION:
35
+ return "<selection>";
36
+ case E_SYMBOL:
37
+ return "symbol";
38
+ case E_NAME:
39
+ return "name";
40
+ case E_RESN:
41
+ return "resn";
42
+ case E_RESI:
43
+ return "resi";
44
+ case E_CHAIN:
45
+ return "chain";
46
+ case E_ID:
47
+ return "<id>";
48
+ case E_NUMBER:
49
+ return "<number>";
50
+ case E_NEGNUM:
51
+ return "<neg_number>";
52
+ case E_AND:
53
+ return "and";
54
+ case E_OR:
55
+ return "or";
56
+ case E_NOT:
57
+ return "not";
58
+ case E_PLUS:
59
+ return "< + >";
60
+ case E_RANGE:
61
+ return "< - >";
62
+ case E_RANGE_OPEN_R:
63
+ return "< - R >";
64
+ case E_RANGE_OPEN_L:
65
+ return "< - L >";
66
+ }
67
+ return NULL;
68
+ }
69
+
70
+ extern int freesasa_yyparse(expression **expression, yyscan_t scanner);
71
+
72
+ static expression *
73
+ expression_new()
74
+ {
75
+ struct expression *e = malloc(sizeof(expression));
76
+
77
+ if (e == NULL)
78
+ mem_fail();
79
+ else {
80
+ e->type = E_SELECTION;
81
+ e->left = NULL;
82
+ e->right = NULL;
83
+ e->value = NULL;
84
+ }
85
+ return e;
86
+ }
87
+
88
+ static void
89
+ expression_free(expression *expression)
90
+ {
91
+ if (expression) {
92
+ expression_free(expression->left);
93
+ expression_free(expression->right);
94
+ free(expression->value);
95
+ free(expression);
96
+ }
97
+ }
98
+
99
+ expression *
100
+ freesasa_selection_atom(expression_type type,
101
+ const char *val)
102
+ {
103
+ expression *e = expression_new();
104
+ int i, n;
105
+ char *buf;
106
+
107
+ assert(val);
108
+
109
+ if (e != NULL) {
110
+ if (type == E_NEGNUM) {
111
+ n = strlen(val) + 2;
112
+ buf = malloc(n);
113
+ if (buf == NULL) {
114
+ mem_fail();
115
+ expression_free(e);
116
+ return NULL;
117
+ }
118
+ sprintf(buf, "-%s", val);
119
+ e->type = E_NUMBER;
120
+ e->value = strdup(buf);
121
+ free(buf);
122
+ } else {
123
+ e->type = type;
124
+ e->value = strdup(val);
125
+ }
126
+
127
+ if (e->value == NULL) {
128
+ mem_fail();
129
+ expression_free(e);
130
+ return NULL;
131
+ }
132
+
133
+ for (i = 0; i < strlen(e->value); ++i)
134
+ e->value[i] = toupper(e->value[i]);
135
+ }
136
+ return e;
137
+ }
138
+
139
+ expression *
140
+ freesasa_selection_create(expression *selection,
141
+ const char *id)
142
+ {
143
+ expression *e = expression_new();
144
+
145
+ assert(id);
146
+
147
+ if (e == NULL)
148
+ expression_free(selection);
149
+ else {
150
+ e->type = E_SELECTION;
151
+ e->left = selection;
152
+ e->value = strdup(id);
153
+
154
+ if (e->value == NULL) {
155
+ mem_fail();
156
+ expression_free(e);
157
+ e = NULL;
158
+ }
159
+ }
160
+
161
+ return e;
162
+ }
163
+
164
+ expression *
165
+ freesasa_selection_selector(expression_type type,
166
+ expression *list)
167
+ {
168
+ expression *e = expression_new();
169
+
170
+ if (e == NULL) {
171
+ expression_free(list);
172
+ } else {
173
+ e->type = type;
174
+ e->left = list;
175
+ }
176
+
177
+ return e;
178
+ }
179
+
180
+ expression *
181
+ freesasa_selection_operation(expression_type type,
182
+ expression *left,
183
+ expression *right)
184
+ {
185
+ expression *e = expression_new();
186
+
187
+ if (e == NULL) {
188
+ expression_free(left);
189
+ expression_free(right);
190
+ } else {
191
+ e->type = type;
192
+ e->left = left;
193
+ e->right = right;
194
+ }
195
+
196
+ return e;
197
+ }
198
+
199
+ /* for debugging */
200
+ static void
201
+ print_expr(FILE *output, const expression *e, int level)
202
+ {
203
+ int i;
204
+ fprintf(output, "\n");
205
+ for (i = 0; i < level; ++i)
206
+ fprintf(output, " ");
207
+
208
+ if (e == NULL) {
209
+ fprintf(output, "()");
210
+ } else {
211
+ fprintf(output, "(%s ", e_str(e->type));
212
+ if (e->value) fprintf(output, ": %s ", e->value);
213
+ print_expr(output, e->left, level + 1);
214
+ print_expr(output, e->right, level + 1);
215
+ fprintf(output, ")");
216
+ }
217
+ fflush(output);
218
+ }
219
+
220
+ static expression *
221
+ get_expression(const char *selector)
222
+ {
223
+ freesasa_yyscan_t scanner;
224
+ YY_BUFFER_STATE state;
225
+ int err;
226
+ expression *expression = NULL;
227
+
228
+ if (freesasa_yylex_init(&scanner)) {
229
+ fail_msg("lexer failed");
230
+ } else {
231
+ state = freesasa_yy_scan_string(selector, scanner);
232
+ err = freesasa_yyparse(&expression, scanner);
233
+ if (err) {
234
+ if (err == 1) fail_msg("parser failed");
235
+ if (err == 2) mem_fail();
236
+ expression_free(expression);
237
+ expression = NULL;
238
+ }
239
+ freesasa_yy_delete_buffer(state, scanner);
240
+ freesasa_yylex_destroy(scanner);
241
+ }
242
+
243
+ return expression;
244
+ }
245
+
246
+ static struct selection *
247
+ selection_new(int n)
248
+ {
249
+ struct selection *selection = malloc(sizeof(struct selection));
250
+ int i;
251
+
252
+ if (selection == NULL) {
253
+ mem_fail();
254
+ } else {
255
+ selection->size = n;
256
+ selection->atom = malloc(sizeof(int) * n);
257
+
258
+ if (selection->atom == NULL) {
259
+ free(selection);
260
+ mem_fail();
261
+ selection = NULL;
262
+ } else {
263
+ for (i = 0; i < n; ++i)
264
+ selection->atom[i] = 0;
265
+ }
266
+ }
267
+
268
+ return selection;
269
+ }
270
+
271
+ static void
272
+ selection_free(struct selection *selection)
273
+ {
274
+ if (selection) {
275
+ free(selection->atom);
276
+ free(selection);
277
+ }
278
+ }
279
+
280
+ /* Looks for exact match between the atom-name and expr->value */
281
+ static int
282
+ match_name(const freesasa_structure *structure,
283
+ const char *id,
284
+ int i)
285
+ {
286
+ char atom[PDB_ATOM_NAME_STRL + 1];
287
+ sscanf(freesasa_structure_atom_name(structure, i), "%s", atom);
288
+ if (strcmp(atom, id) == 0)
289
+ return 1;
290
+ return 0;
291
+ }
292
+
293
+ /** Looks for match of strlen(expr->value) first characters of atom-name and expr->value */
294
+ static int
295
+ match_symbol(const freesasa_structure *structure,
296
+ const char *id,
297
+ int i)
298
+ {
299
+ char symbol[PDB_ATOM_SYMBOL_STRL + 1];
300
+ sscanf(freesasa_structure_atom_symbol(structure, i), "%s", symbol);
301
+ if (strcmp(id, symbol) == 0) {
302
+ return 1;
303
+ }
304
+ return 0;
305
+ }
306
+
307
+ static int
308
+ match_resn(const freesasa_structure *structure,
309
+ const char *id,
310
+ int i)
311
+ {
312
+ char resn[PDB_ATOM_RES_NAME_STRL + 1];
313
+ sscanf(freesasa_structure_atom_res_name(structure, i), "%s", resn);
314
+ if (strcmp(resn, id) == 0)
315
+ return 1;
316
+ return 0;
317
+ }
318
+
319
+ static int
320
+ match_resi(const freesasa_structure *structure,
321
+ const char *id,
322
+ int i)
323
+ {
324
+ char resi[PDB_ATOM_RES_NUMBER_STRL + 1];
325
+ sscanf(freesasa_structure_atom_res_number(structure, i), "%s", resi);
326
+ if (strcmp(resi, id) == 0)
327
+ return 1;
328
+ return 0;
329
+ }
330
+
331
+ static int
332
+ match_chain(const freesasa_structure *structure,
333
+ const char *id,
334
+ int i)
335
+ {
336
+ return id[0] == freesasa_structure_atom_chain(structure, i);
337
+ }
338
+
339
+ static void
340
+ select_id(expression_type parent_type,
341
+ struct selection *selection,
342
+ const freesasa_structure *structure,
343
+ const char *id)
344
+ {
345
+ int count = 0, match, i;
346
+
347
+ assert(id);
348
+
349
+ for (i = 0; i < selection->size; ++i) {
350
+ match = 0;
351
+ switch (parent_type) {
352
+ case E_NAME:
353
+ match = match_name(structure, id, i);
354
+ break;
355
+ case E_SYMBOL:
356
+ match = match_symbol(structure, id, i);
357
+ break;
358
+ case E_RESN:
359
+ match = match_resn(structure, id, i);
360
+ break;
361
+ case E_RESI:
362
+ match = match_resi(structure, id, i);
363
+ break;
364
+ case E_CHAIN:
365
+ match = match_chain(structure, id, i);
366
+ break;
367
+ default:
368
+ assert(0);
369
+ break;
370
+ }
371
+ if (match) selection->atom[i] = 1;
372
+ count += match;
373
+ }
374
+ if (count == 0) freesasa_warn("Found no matches to %s '%s', typo?",
375
+ e_str(parent_type), id);
376
+ }
377
+
378
+ static int
379
+ is_valid_id(int parent_type,
380
+ const expression *expr)
381
+ {
382
+ int type, n, warn, i;
383
+ const char *val;
384
+
385
+ assert(expr->type == E_NUMBER || expr->type == E_ID);
386
+
387
+ type = expr->type;
388
+ val = expr->value;
389
+
390
+ switch (parent_type) {
391
+ case E_NAME:
392
+ if (strlen(val) > PDB_ATOM_NAME_STRL)
393
+ return freesasa_warn("select: %s: atom name '%s' invalid (string too long), "
394
+ "will be ignored",
395
+ e_str(parent_type), val);
396
+ break;
397
+ case E_SYMBOL:
398
+ if (type != E_ID)
399
+ return freesasa_warn("select: %s: '%s' invalid (should be 1 or 2 letters, 'C', 'N', 'SE', etc), "
400
+ "will be ignored",
401
+ e_str(parent_type), val);
402
+ if (strlen(val) > 2)
403
+ return freesasa_warn("select: %s: '%s' invalid (element names have 1 or 2 characters), "
404
+ "will be ignored",
405
+ e_str(parent_type), val);
406
+ break;
407
+ case E_RESN:
408
+ if (strlen(val) > PDB_ATOM_RES_NAME_STRL)
409
+ return freesasa_warn("select: %s: '%s' invalid (string too long), "
410
+ "will be ignored",
411
+ e_str(parent_type), val);
412
+ break;
413
+ case E_RESI:
414
+ if (type == E_ID) {
415
+ /* these should have format 1, 2, 345, etc or 12A, 12B, etc. */
416
+ n = strlen(val);
417
+ if (n > PDB_ATOM_RES_NUMBER_STRL) {
418
+ return freesasa_warn("select: %s: '%s' invalid (string too long), "
419
+ "will be ignored",
420
+ e_str(parent_type), val);
421
+ } else {
422
+ warn = 0;
423
+ if (n == 1) ++warn;
424
+ if (!warn && (toupper(val[n - 1]) < 'A' || toupper(val[n - 1]) > 'Z')) {
425
+ ++warn;
426
+ }
427
+ for (i = 0; !warn && i < n - 1; ++i) {
428
+ if (val[i] < '0' || val[i] > '9') {
429
+ ++warn;
430
+ }
431
+ }
432
+ if (warn) {
433
+ return freesasa_warn("select: %s: '%s' invalid, should either be "
434
+ "number (1, 2, 3) or number with insertion code (1A, 1B, ...), "
435
+ "will be ignored",
436
+ e_str(parent_type), val);
437
+ }
438
+ }
439
+ } else if (type != E_NUMBER) {
440
+ return freesasa_warn("select: %s: '%s' invalid, will be ignored",
441
+ e_str(parent_type), val);
442
+ }
443
+ break;
444
+ case E_CHAIN:
445
+ if (strlen(val) > 1)
446
+ return freesasa_warn("select: %s: '%s' invalid (string too long), "
447
+ "will be ignored",
448
+ e_str(parent_type), val);
449
+ break;
450
+ default:
451
+ assert(0);
452
+ break;
453
+ }
454
+ return FREESASA_SUCCESS;
455
+ }
456
+
457
+ static int
458
+ select_range(expression_type range_type,
459
+ expression_type parent_type,
460
+ struct selection *selection,
461
+ const freesasa_structure *structure,
462
+ const expression *left,
463
+ const expression *right)
464
+ {
465
+ int lower, upper, i, j;
466
+
467
+ assert(range_type == E_RANGE || range_type == E_RANGE_OPEN_L || range_type == E_RANGE_OPEN_R);
468
+ assert(parent_type == E_RESI || parent_type == E_CHAIN);
469
+
470
+ if (parent_type == E_RESI) { /* residues have integer numbering */
471
+ if ((left && left->type != E_NUMBER) ||
472
+ (right && right->type != E_NUMBER)) {
473
+ return freesasa_warn("select: %s: range '%s-%s' invalid, needs to be two numbers, "
474
+ "will be ignored",
475
+ e_str(parent_type), left->value, right->value);
476
+ }
477
+ } else { /* chains can be numbered by both letters (common) and numbers (uncommon) */
478
+ if (left->type != right->type ||
479
+ (left->type == E_ID && (strlen(left->value) > 1 || strlen(right->value) > 1)))
480
+ return freesasa_warn("select: %s: range '%s-%s' invalid, should be two letters (A-C) or numbers (1-5), "
481
+ "will be ignored",
482
+ e_str(parent_type), left->value, right->value);
483
+ }
484
+ if (range_type == E_RANGE_OPEN_L) {
485
+ lower = atoi(freesasa_structure_atom_res_number(structure, 0));
486
+ upper = atoi(right->value);
487
+ } else if (range_type == E_RANGE_OPEN_R) {
488
+ lower = atoi(left->value);
489
+ upper = atoi(freesasa_structure_atom_res_number(structure, freesasa_structure_n(structure) - 1));
490
+ } else if (left->type == E_NUMBER) {
491
+ lower = atoi(left->value);
492
+ upper = atoi(right->value);
493
+ } else {
494
+ lower = (int)left->value[0];
495
+ upper = (int)right->value[0];
496
+ }
497
+ for (i = 0; i < selection->size; ++i) {
498
+ if (parent_type == E_RESI)
499
+ j = atoi(freesasa_structure_atom_res_number(structure, i));
500
+ else
501
+ j = (int)freesasa_structure_atom_chain(structure, i);
502
+ if (j >= lower && j <= upper)
503
+ selection->atom[i] = 1;
504
+ }
505
+ return FREESASA_SUCCESS;
506
+ }
507
+
508
+ static int
509
+ select_list(expression_type parent_type,
510
+ struct selection *selection,
511
+ const freesasa_structure *structure,
512
+ const expression *expr)
513
+ {
514
+ int resr, resl;
515
+ expression *left, *right;
516
+
517
+ if (expr == NULL)
518
+ return fail_msg("NULL expression");
519
+
520
+ left = expr->left;
521
+ right = expr->right;
522
+
523
+ switch (expr->type) {
524
+ case E_PLUS:
525
+ if (left == NULL || right == NULL)
526
+ return fail_msg("NULL expression");
527
+ resl = select_list(parent_type, selection, structure, left);
528
+ resr = select_list(parent_type, selection, structure, right);
529
+ if (resl == FREESASA_WARN || resr == FREESASA_WARN)
530
+ return FREESASA_WARN;
531
+ break;
532
+ case E_RANGE:
533
+ if (left == NULL || right == NULL)
534
+ return fail_msg("NULL expression");
535
+ return select_range(E_RANGE, parent_type, selection, structure, left, right);
536
+ case E_RANGE_OPEN_L:
537
+ if (left != NULL || right == NULL)
538
+ return fail_msg("NULL expression");
539
+ return select_range(E_RANGE_OPEN_L, parent_type, selection, structure, left, right);
540
+ case E_RANGE_OPEN_R:
541
+ if (left == NULL || right != NULL)
542
+ return fail_msg("NULL expression");
543
+ return select_range(E_RANGE_OPEN_R, parent_type, selection, structure, left, right);
544
+ case E_ID:
545
+ case E_NUMBER:
546
+ if (is_valid_id(parent_type, expr) == FREESASA_SUCCESS)
547
+ select_id(parent_type, selection, structure, expr->value);
548
+ else
549
+ return freesasa_warn("select: %s: '%s' invalid %s",
550
+ e_str(parent_type), expr->value, e_str(expr->type));
551
+ break;
552
+ default:
553
+ return freesasa_fail("select: parse error (expression: '%s %s')",
554
+ e_str(parent_type), e_str(expr->type));
555
+ }
556
+ return FREESASA_SUCCESS;
557
+ }
558
+
559
+ static int
560
+ selection_join(struct selection *target,
561
+ const struct selection *s1,
562
+ const struct selection *s2,
563
+ int type)
564
+ {
565
+ int n, i;
566
+
567
+ if (s1 == NULL || s2 == NULL || target == NULL)
568
+ return fail_msg("trying to join NULL selections");
569
+
570
+ assert(s1->size == s2->size);
571
+ assert(s1->size == target->size);
572
+
573
+ n = target->size;
574
+
575
+ switch (type) {
576
+ case E_AND:
577
+ for (i = 0; i < n; ++i)
578
+ target->atom[i] = s1->atom[i] && s2->atom[i];
579
+ break;
580
+ case E_OR:
581
+ for (i = 0; i < n; ++i)
582
+ target->atom[i] = s1->atom[i] || s2->atom[i];
583
+ break;
584
+ default:
585
+ assert(0);
586
+ }
587
+
588
+ return FREESASA_SUCCESS;
589
+ }
590
+
591
+ static int
592
+ selection_not(struct selection *s)
593
+ {
594
+ int i;
595
+
596
+ if (s == NULL) return fail_msg("NULL selection");
597
+
598
+ for (i = 0; i < s->size; ++i) {
599
+ s->atom[i] = !s->atom[i];
600
+ }
601
+
602
+ return FREESASA_SUCCESS;
603
+ }
604
+
605
+ /* Called recursively, the selection is built as we cover the expression tree */
606
+ static int
607
+ select_atoms(struct selection *selection,
608
+ const expression *expr,
609
+ const freesasa_structure *structure)
610
+ {
611
+ int warn = 0, err = 0, n, ret;
612
+ struct selection *sl, *sr;
613
+
614
+ assert(selection);
615
+ assert(structure);
616
+
617
+ n = selection->size;
618
+
619
+ /* this should only happen if memory allocation failed during parsing */
620
+ if (expr == NULL) return fail_msg("NULL expression");
621
+
622
+ switch (expr->type) {
623
+ case E_SELECTION:
624
+ assert(expr->value != NULL);
625
+ selection->name = expr->value;
626
+ return select_atoms(selection, expr->left, structure);
627
+ break;
628
+ case E_SYMBOL:
629
+ case E_NAME:
630
+ case E_RESN:
631
+ case E_RESI:
632
+ case E_CHAIN:
633
+ return select_list(expr->type, selection, structure, expr->left);
634
+ break;
635
+ case E_AND:
636
+ case E_OR: {
637
+ sl = selection_new(n);
638
+ sr = selection_new(n);
639
+
640
+ if (sl != NULL && sr != NULL) {
641
+ if ((ret = select_atoms(sl, expr->left, structure))) {
642
+ if (ret == FREESASA_WARN) ++warn;
643
+ if (ret == FREESASA_FAIL) ++err;
644
+ }
645
+ if ((ret = select_atoms(sr, expr->right, structure))) {
646
+ if (ret == FREESASA_WARN) ++warn;
647
+ if (ret == FREESASA_FAIL) ++err;
648
+ }
649
+ selection_join(selection, sl, sr, expr->type);
650
+ } else {
651
+ ++err;
652
+ }
653
+
654
+ selection_free(sl);
655
+ selection_free(sr);
656
+
657
+ if (err) return fail_msg("error joining selections");
658
+ break;
659
+ }
660
+ case E_NOT: {
661
+ ret = select_atoms(selection, expr->right, structure);
662
+ if (ret == FREESASA_WARN) ++warn;
663
+ if (ret == FREESASA_FAIL) return FREESASA_FAIL;
664
+ if (selection_not(selection)) return FREESASA_FAIL;
665
+ break;
666
+ }
667
+ case E_ID:
668
+ case E_NUMBER:
669
+ case E_PLUS:
670
+ case E_RANGE:
671
+ /* these four are handled by the RESN,SYMBOL,ETC */
672
+ default:
673
+ return fail_msg("parser error");
674
+ }
675
+ if (warn) return FREESASA_WARN;
676
+ return FREESASA_SUCCESS;
677
+ }
678
+
679
+ static int
680
+ select_area_impl(const char *command,
681
+ char *name,
682
+ double *area,
683
+ const freesasa_structure *structure,
684
+ const freesasa_result *result)
685
+ {
686
+ struct selection *selection = NULL;
687
+ struct expression *expression = NULL;
688
+ const int maxlen = FREESASA_MAX_SELECTION_NAME;
689
+ double sasa = 0;
690
+ int err = 0, warn = 0, n_atoms = 0, j, len;
691
+
692
+ assert(name);
693
+ assert(area);
694
+ assert(command);
695
+ assert(structure);
696
+ assert(result);
697
+ assert(freesasa_structure_n(structure) == result->n_atoms);
698
+
699
+ *area = 0;
700
+ name[0] = '\0';
701
+
702
+ expression = get_expression(command);
703
+ selection = selection_new(result->n_atoms);
704
+
705
+ if (selection == NULL) {
706
+ return fail_msg("");
707
+ }
708
+
709
+ if (expression != NULL && selection != NULL) {
710
+ switch (select_atoms(selection, expression, structure)) {
711
+ case FREESASA_FAIL:
712
+ err = 1;
713
+ break;
714
+ case FREESASA_WARN:
715
+ warn = 1; /* proceed with calculation, print warning later */
716
+ case FREESASA_SUCCESS: {
717
+ for (j = 0; j < selection->size; ++j) {
718
+ ++n_atoms;
719
+ sasa += selection->atom[j] * result->sasa[j];
720
+ }
721
+
722
+ *area = sasa;
723
+ len = strlen(selection->name);
724
+ strncpy(name, selection->name, maxlen);
725
+
726
+ if (len > maxlen) {
727
+ name[maxlen] = '\0';
728
+ } else {
729
+ name[len] = '\0';
730
+ }
731
+
732
+ break;
733
+ }
734
+ default:
735
+ assert(0);
736
+ }
737
+ } else {
738
+ err = 1;
739
+ }
740
+ selection_free(selection);
741
+ expression_free(expression);
742
+
743
+ if (err)
744
+ return fail_msg("problems parsing expression '%s'", command);
745
+ if (warn)
746
+ return freesasa_warn("in %s(): There were warnings", __func__);
747
+ return n_atoms;
748
+ }
749
+
750
+ freesasa_selection *
751
+ freesasa_selection_alloc(const char *name, const char *command)
752
+ {
753
+ freesasa_selection *selection = malloc(sizeof(freesasa_selection));
754
+
755
+ if (selection == NULL) {
756
+ mem_fail();
757
+ return NULL;
758
+ }
759
+
760
+ selection->name = NULL;
761
+ selection->command = NULL;
762
+ selection->area = 0;
763
+ selection->n_atoms = 0;
764
+
765
+ selection->name = strdup(name);
766
+ if (selection->name == NULL) {
767
+ mem_fail();
768
+ goto cleanup;
769
+ }
770
+
771
+ selection->command = strdup(command);
772
+ if (selection->command == NULL) {
773
+ mem_fail();
774
+ goto cleanup;
775
+ }
776
+
777
+ return selection;
778
+
779
+ cleanup:
780
+ freesasa_selection_free(selection);
781
+ return NULL;
782
+ }
783
+
784
+ void freesasa_selection_free(freesasa_selection *selection)
785
+ {
786
+ if (selection != NULL) {
787
+ free(selection->name);
788
+ free(selection->command);
789
+ free(selection);
790
+ }
791
+ }
792
+
793
+ freesasa_selection *
794
+ freesasa_selection_clone(const freesasa_selection *src)
795
+ {
796
+ freesasa_selection *cpy = freesasa_selection_alloc(src->name, src->command);
797
+
798
+ if (cpy == NULL) {
799
+ fail_msg("");
800
+ goto cleanup;
801
+ }
802
+
803
+ cpy->area = src->area;
804
+ cpy->n_atoms = src->n_atoms;
805
+
806
+ return cpy;
807
+
808
+ cleanup:
809
+ freesasa_selection_free(cpy);
810
+ return NULL;
811
+ }
812
+
813
+ const char *
814
+ freesasa_selection_name(const freesasa_selection *selection)
815
+ {
816
+ assert(selection);
817
+ return selection->name;
818
+ }
819
+
820
+ const char *
821
+ freesasa_selection_command(const freesasa_selection *selection)
822
+ {
823
+ assert(selection);
824
+ return selection->command;
825
+ }
826
+
827
+ double
828
+ freesasa_selection_area(const freesasa_selection *selection)
829
+ {
830
+ assert(selection);
831
+ return selection->area;
832
+ }
833
+
834
+ freesasa_selection *
835
+ freesasa_selection_new(const char *command,
836
+ const freesasa_structure *structure,
837
+ const freesasa_result *result)
838
+ {
839
+ char name[FREESASA_MAX_SELECTION_NAME];
840
+ double area;
841
+ freesasa_selection *selection;
842
+ int n_atoms;
843
+
844
+ n_atoms = select_area_impl(command, name, &area, structure, result);
845
+
846
+ if (n_atoms == FREESASA_FAIL) {
847
+ fail_msg("");
848
+ return NULL;
849
+ }
850
+
851
+ selection = freesasa_selection_alloc(name, command);
852
+ if (selection == NULL) {
853
+ mem_fail();
854
+ return NULL;
855
+ }
856
+
857
+ selection->area = area;
858
+ selection->n_atoms = n_atoms;
859
+
860
+ return selection;
861
+ }
862
+
863
+ int freesasa_select_area(const char *command,
864
+ char *name,
865
+ double *area,
866
+ const freesasa_structure *structure,
867
+ const freesasa_result *result)
868
+ {
869
+ int ret = select_area_impl(command, name, area, structure, result);
870
+ if (ret >= 0) return FREESASA_SUCCESS;
871
+ return ret;
872
+ }
873
+
874
+ int freesasa_selection_parse_error(expression *e,
875
+ yyscan_t scanner,
876
+ const char *msg)
877
+ {
878
+ if (freesasa_get_verbosity() == FREESASA_V_DEBUG) print_expr(stderr, e, 0);
879
+ if (freesasa_get_verbosity() == FREESASA_V_NORMAL) fprintf(stderr, "\n");
880
+ return freesasa_fail(msg);
881
+ }
882
+
883
+ #if USE_CHECK
884
+ #include <check.h>
885
+
886
+ START_TEST(test_selection)
887
+ {
888
+ struct selection *s1, *s2, *s3, *s4;
889
+ static const expression empty_expression = {
890
+ .right = NULL, .left = NULL, .value = NULL, .type = E_SELECTION};
891
+ freesasa_structure *structure = freesasa_structure_new();
892
+ expression r, l, e, e_symbol;
893
+
894
+ freesasa_structure_add_atom(structure, " CA ", "ALA", " 1", 'A', 0, 0, 0);
895
+ freesasa_structure_add_atom(structure, " O ", "ALA", " 1", 'A', 10, 10, 10);
896
+
897
+ s1 = selection_new(freesasa_structure_n(structure));
898
+ s2 = selection_new(freesasa_structure_n(structure));
899
+ s3 = selection_new(freesasa_structure_n(structure));
900
+ s4 = selection_new(freesasa_structure_n(structure));
901
+
902
+ r = l = e = e_symbol = empty_expression;
903
+ e.type = E_PLUS;
904
+ e.right = &r;
905
+ e.left = &l;
906
+ r.value = "C";
907
+ r.type = E_ID;
908
+ l.value = "O";
909
+ l.type = E_ID;
910
+ e_symbol.type = E_SYMBOL;
911
+ e_symbol.left = &e;
912
+
913
+ /* select_symbol */
914
+ select_list(E_SYMBOL, s1, structure, &r);
915
+ ck_assert_int_eq(s1->atom[0], 1);
916
+ ck_assert_int_eq(s1->atom[1], 0);
917
+ select_list(E_SYMBOL, s2, structure, &l);
918
+ ck_assert_int_eq(s2->atom[0], 0);
919
+ ck_assert_int_eq(s2->atom[1], 1);
920
+ select_list(E_SYMBOL, s3, structure, &e);
921
+ ck_assert_int_eq(s3->atom[0], 1);
922
+ ck_assert_int_eq(s3->atom[1], 1);
923
+ select_atoms(s4, &e_symbol, structure);
924
+ ck_assert_int_eq(s4->atom[0], 1);
925
+ ck_assert_int_eq(s4->atom[1], 1);
926
+
927
+ /* selection_join */
928
+ selection_join(s3, s1, s2, E_AND);
929
+ ck_assert_int_eq(s3->atom[0], 0);
930
+ ck_assert_int_eq(s3->atom[1], 0);
931
+ selection_join(s3, s1, s2, E_OR);
932
+ ck_assert_int_eq(s3->atom[0], 1);
933
+ ck_assert_int_eq(s3->atom[1], 1);
934
+ freesasa_set_verbosity(FREESASA_V_SILENT);
935
+ ck_assert_int_eq(selection_join(NULL, s1, s2, E_OR), FREESASA_FAIL);
936
+ ck_assert_int_eq(selection_join(s3, NULL, s1, E_OR), FREESASA_FAIL);
937
+ ck_assert_int_eq(selection_join(NULL, NULL, NULL, E_OR), FREESASA_FAIL);
938
+
939
+ /* selection_not */
940
+ ck_assert_int_eq(selection_not(s3), FREESASA_SUCCESS);
941
+ ck_assert_int_eq(s3->atom[0], 0);
942
+ ck_assert_int_eq(s3->atom[1], 0);
943
+ ck_assert_int_eq(selection_not(NULL), FREESASA_FAIL);
944
+ freesasa_set_verbosity(FREESASA_V_NORMAL);
945
+ }
946
+ END_TEST
947
+
948
+ START_TEST(test_expression)
949
+ {
950
+ int i;
951
+ expression *e = get_expression("c1, symbol O+C");
952
+
953
+ ck_assert_ptr_ne(e, NULL);
954
+ ck_assert_int_eq(e->type, E_SELECTION);
955
+ ck_assert_ptr_ne(e->left, NULL);
956
+ ck_assert_ptr_eq(e->right, NULL);
957
+ ck_assert_str_eq(e->value, "c1");
958
+ ck_assert_int_eq(e->left->type, E_SYMBOL);
959
+ ck_assert_ptr_ne(e->left->left, NULL);
960
+ ck_assert_ptr_eq(e->left->right, NULL);
961
+ ck_assert_int_eq(e->left->left->type, E_PLUS);
962
+ ck_assert_int_eq(e->left->left->left->type, E_ID);
963
+ ck_assert_int_eq(e->left->left->right->type, E_ID);
964
+ ck_assert_str_eq(e->left->left->right->value, "C");
965
+ ck_assert_str_eq(e->left->left->left->value, "O");
966
+ for (i = E_SELECTION; i <= E_RANGE_OPEN_R; ++i) {
967
+ ck_assert_ptr_ne(e_str(i), NULL);
968
+ }
969
+ ck_assert_ptr_eq(e_str(E_RANGE_OPEN_R + 1), NULL);
970
+ }
971
+ END_TEST
972
+
973
+ START_TEST(test_debug) /* this test just runs the debug output code to not get artificially low coverage */
974
+ {
975
+ FILE *devnull = fopen("/dev/null", "w");
976
+ expression *e = get_expression("c1, symbol O+C");
977
+ print_expr(devnull, e, 0);
978
+ }
979
+ END_TEST
980
+
981
+ struct selection selection_dummy = {.size = 1, .name = NULL, .atom = NULL};
982
+
983
+ void *freesasa_selection_dummy_ptr = &selection_dummy;
984
+
985
+ int freesasa_wrap_select_atoms(struct selection *selection,
986
+ const expression *expr,
987
+ const freesasa_structure *structure)
988
+ {
989
+ return select_atoms(selection, expr, structure);
990
+ }
991
+
992
+ TCase *
993
+ test_selection_static()
994
+ {
995
+ TCase *tc = tcase_create("selection.c static");
996
+ tcase_add_test(tc, test_selection);
997
+ tcase_add_test(tc, test_expression);
998
+ tcase_add_test(tc, test_debug);
999
+
1000
+ return tc;
1001
+ }
1002
+
1003
+ #endif /* USE_CHECK */
.venv_haddock/include/site/python3.12/freesasa/selection.h ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef SELECTION_H
2
+ #define SELECTION_H
3
+
4
+ typedef enum { E_SELECTION,
5
+ E_SYMBOL,
6
+ E_NAME,
7
+ E_RESN,
8
+ E_RESI,
9
+ E_CHAIN,
10
+ E_ID,
11
+ E_NUMBER,
12
+ E_NEGNUM,
13
+ E_AND,
14
+ E_OR,
15
+ E_NOT,
16
+ E_PLUS,
17
+ E_RANGE,
18
+ E_RANGE_OPEN_L,
19
+ E_RANGE_OPEN_R } expression_type;
20
+
21
+ typedef struct expression {
22
+ struct expression *left;
23
+ struct expression *right;
24
+ expression_type type;
25
+ char *value;
26
+ } expression;
27
+
28
+ /** Create an atomic value E_ID or E_NUMBER */
29
+ expression *
30
+ freesasa_selection_atom(expression_type type,
31
+ const char *val);
32
+
33
+ /** Create a top level selection (E_SELECTION) */
34
+ expression *
35
+ freesasa_selection_create(expression *selection,
36
+ const char *id);
37
+
38
+ /** Create a property selector (E_SYMBOL, E_NAME, E_RESN, E_RESI or E_CHAIN) */
39
+ expression *
40
+ freesasa_selection_selector(expression_type type,
41
+ expression *list);
42
+
43
+ /** Create an operation (E_AND, E_OR, E_NOT, E_PLUS or E_RANGE) */
44
+ expression *
45
+ freesasa_selection_operation(expression_type type,
46
+ expression *left,
47
+ expression *right);
48
+
49
+ #endif /* SELECTION_H */
.venv_haddock/include/site/python3.12/freesasa/structure.c ADDED
@@ -0,0 +1,1243 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if HAVE_CONFIG_H
2
+ #include <config.h>
3
+ #endif
4
+
5
+ #include <assert.h>
6
+ #include <stdlib.h>
7
+
8
+ #include "classifier.h"
9
+ #include "coord.h"
10
+ #include "freesasa_internal.h"
11
+ #include "pdb.h"
12
+
13
+ /**
14
+ This file contains the functions that turn lines in PDB files to
15
+ atom records. It's all pretty messy because one needs to keep track
16
+ of when a new chain/new residue is encountered, and skip atoms that
17
+ are duplicates (only first of alt coordinates are used). It is both
18
+ possible to add atoms one by one, or by reading a whole file at
19
+ once, and the user can supply some options about what to do when
20
+ encountering atoms that are not recognized, whether to include
21
+ hydrogrens, hetatm, etc. The current implementation results in a
22
+ rather convoluted logic, that is difficult to maintain, debug and
23
+ extend.
24
+
25
+ TODO: Refactor.
26
+ */
27
+ #define ATOMS_CHUNK 512
28
+ #define RESIDUES_CHUNK 64
29
+ #define CHAINS_CHUNK 64
30
+
31
+ struct atom {
32
+ char *res_name;
33
+ char *res_number;
34
+ char *atom_name;
35
+ char *symbol;
36
+ char *line;
37
+ int res_index;
38
+ char chain_label;
39
+ freesasa_atom_class the_class;
40
+ };
41
+
42
+ static const struct atom empty_atom = {
43
+ NULL, /* res_name */
44
+ NULL, /* res_number */
45
+ NULL, /* atom_name */
46
+ NULL, /* symbol */
47
+ NULL, /* line */
48
+ -1, /* res_index */
49
+ '\0', /* chain_label */
50
+ FREESASA_ATOM_UNKNOWN /* the_class */
51
+ };
52
+
53
+ struct atoms {
54
+ int n;
55
+ int n_alloc;
56
+ struct atom **atom;
57
+ double *radius;
58
+ };
59
+
60
+ struct residues {
61
+ int n;
62
+ int n_alloc;
63
+ int *first_atom;
64
+ freesasa_nodearea **reference_area;
65
+ };
66
+
67
+ struct chains {
68
+ int n;
69
+ int n_alloc;
70
+ char *labels; /* all chain labels found (as string) */
71
+ int *first_atom; /* first atom of each chain */
72
+ };
73
+
74
+ struct freesasa_structure {
75
+ struct atoms atoms;
76
+ struct residues residues;
77
+ struct chains chains;
78
+ char *classifier_name;
79
+ coord_t *xyz;
80
+ int model; /* model number */
81
+ size_t cif_ref;
82
+ void (*release_cif_ref)(size_t);
83
+ };
84
+
85
+ static int
86
+ guess_symbol(char *symbol,
87
+ const char *name);
88
+
89
+ static void
90
+ atom_free(struct atom *a)
91
+ {
92
+ if (a != NULL) {
93
+ free(a->res_name);
94
+ free(a->res_number);
95
+ free(a->atom_name);
96
+ free(a->symbol);
97
+ free(a->line);
98
+ free(a);
99
+ }
100
+ }
101
+
102
+ struct atoms
103
+ atoms_init(void)
104
+ {
105
+ struct atoms atoms;
106
+ atoms.n = 0;
107
+ atoms.n_alloc = 0;
108
+ atoms.atom = NULL;
109
+ atoms.radius = NULL;
110
+ return atoms;
111
+ }
112
+
113
+ /* Allocates memory in chunks, ticks up atoms->n if allocation successful */
114
+ static int
115
+ atoms_alloc(struct atoms *atoms)
116
+ {
117
+ int i, new_size;
118
+ void *aa, *ar;
119
+
120
+ assert(atoms);
121
+ assert(atoms->n <= atoms->n_alloc);
122
+
123
+ if (atoms->n == atoms->n_alloc) {
124
+ new_size = atoms->n_alloc + ATOMS_CHUNK;
125
+ aa = atoms->atom;
126
+ ar = atoms->radius;
127
+
128
+ atoms->atom = realloc(atoms->atom, sizeof(struct atom *) * new_size);
129
+ if (atoms->atom == NULL) {
130
+ atoms->atom = aa;
131
+ return mem_fail();
132
+ }
133
+
134
+ for (i = atoms->n_alloc; i < new_size; ++i) {
135
+ atoms->atom[i] = NULL;
136
+ }
137
+
138
+ atoms->radius = realloc(atoms->radius, sizeof(double) * new_size);
139
+ if (atoms->radius == NULL) {
140
+ atoms->radius = ar;
141
+ return mem_fail();
142
+ }
143
+
144
+ atoms->n_alloc = new_size;
145
+ }
146
+ ++atoms->n;
147
+ return FREESASA_SUCCESS;
148
+ }
149
+
150
+ static void
151
+ atoms_dealloc(struct atoms *atoms)
152
+ {
153
+ int i;
154
+ struct atom **atom;
155
+
156
+ if (atoms) {
157
+ atom = atoms->atom;
158
+ if (atom) {
159
+ for (i = 0; i < atoms->n; ++i)
160
+ if (atom[i]) atom_free(atom[i]);
161
+ free(atom);
162
+ }
163
+ free(atoms->radius);
164
+ *atoms = atoms_init();
165
+ }
166
+ }
167
+
168
+ static struct atom *
169
+ atom_new(const char *residue_name,
170
+ const char *residue_number,
171
+ const char *atom_name,
172
+ const char *symbol,
173
+ char chain_label)
174
+ {
175
+ struct atom *a = malloc(sizeof(struct atom));
176
+ if (a == NULL) goto memerr;
177
+
178
+ *a = empty_atom;
179
+
180
+ a->line = NULL;
181
+ a->chain_label = chain_label;
182
+ a->res_index = -1;
183
+
184
+ a->res_name = strdup(residue_name);
185
+ a->res_number = strdup(residue_number);
186
+ a->atom_name = strdup(atom_name);
187
+ a->symbol = strdup(symbol);
188
+ a->the_class = FREESASA_ATOM_UNKNOWN;
189
+
190
+ if (!a->res_name || !a->res_number || !a->atom_name ||
191
+ !a->symbol) {
192
+ goto memerr;
193
+ }
194
+
195
+ return a;
196
+
197
+ memerr:
198
+ mem_fail();
199
+ atom_free(a);
200
+ return NULL;
201
+ }
202
+
203
+ static struct atom *
204
+ atom_new_from_line(const char *line,
205
+ char *alt_label)
206
+ {
207
+ char aname[PDB_ATOM_NAME_STRL + 1], rname[PDB_ATOM_RES_NAME_STRL + 1],
208
+ rnumber[PDB_ATOM_RES_NUMBER_STRL + 1], symbol[PDB_ATOM_SYMBOL_STRL + 1];
209
+ int flag;
210
+ struct atom *a;
211
+
212
+ assert(line);
213
+
214
+ if (alt_label) *alt_label = freesasa_pdb_get_alt_coord_label(line);
215
+
216
+ freesasa_pdb_get_atom_name(aname, line);
217
+ freesasa_pdb_get_res_name(rname, line);
218
+ freesasa_pdb_get_res_number(rnumber, line);
219
+
220
+ flag = freesasa_pdb_get_symbol(symbol, line);
221
+ if (flag == FREESASA_FAIL || (symbol[0] == ' ' && symbol[1] == ' ')) {
222
+ guess_symbol(symbol, aname);
223
+ }
224
+
225
+ a = atom_new(rname, rnumber, aname, symbol, freesasa_pdb_get_chain_label(line));
226
+
227
+ if (a != NULL) {
228
+ a->line = strdup(line);
229
+ if (a->line == NULL) {
230
+ mem_fail();
231
+ atom_free(a);
232
+ a = NULL;
233
+ }
234
+ }
235
+
236
+ return a;
237
+ }
238
+
239
+ static struct residues
240
+ residues_init(void)
241
+ {
242
+ struct residues res;
243
+
244
+ res.n = 0;
245
+ res.n_alloc = 0;
246
+ res.first_atom = 0;
247
+ res.reference_area = 0;
248
+
249
+ return res;
250
+ }
251
+
252
+ static int
253
+ residues_alloc(struct residues *residues)
254
+ {
255
+ int new_size;
256
+ void *fa, *ra;
257
+
258
+ assert(residues);
259
+ assert(residues->n <= residues->n_alloc);
260
+
261
+ if (residues->n == residues->n_alloc) {
262
+ new_size = residues->n_alloc + RESIDUES_CHUNK;
263
+ fa = residues->first_atom;
264
+ ra = residues->reference_area;
265
+
266
+ residues->first_atom = realloc(residues->first_atom,
267
+ sizeof(int) * new_size);
268
+ if (residues->first_atom == NULL) {
269
+ residues->first_atom = fa;
270
+ return mem_fail();
271
+ }
272
+
273
+ residues->reference_area = realloc(residues->reference_area,
274
+ sizeof(freesasa_nodearea *) * new_size);
275
+ if (residues->reference_area == NULL) {
276
+ residues->reference_area = ra;
277
+ return mem_fail();
278
+ }
279
+
280
+ residues->n_alloc = new_size;
281
+ }
282
+ ++residues->n;
283
+ return FREESASA_SUCCESS;
284
+ }
285
+
286
+ static void
287
+ residues_dealloc(struct residues *residues)
288
+ {
289
+ int i;
290
+
291
+ if (residues) {
292
+ free(residues->first_atom);
293
+ if (residues->reference_area) {
294
+ for (i = 0; i < residues->n; ++i) {
295
+ free(residues->reference_area[i]);
296
+ }
297
+ }
298
+ free(residues->reference_area);
299
+ *residues = residues_init();
300
+ }
301
+ }
302
+
303
+ static struct chains
304
+ chains_init(void)
305
+ {
306
+ struct chains ch;
307
+
308
+ ch.n = 0;
309
+ ch.n_alloc = 0;
310
+ ch.first_atom = NULL;
311
+ ch.labels = NULL;
312
+
313
+ return ch;
314
+ }
315
+
316
+ static int
317
+ chains_alloc(struct chains *chains)
318
+ {
319
+ int new_size;
320
+ void *fa, *lbl;
321
+
322
+ assert(chains);
323
+ assert(chains->n <= chains->n_alloc);
324
+
325
+ if (chains->n == chains->n_alloc) {
326
+ new_size = chains->n_alloc + CHAINS_CHUNK;
327
+ fa = chains->first_atom;
328
+ lbl = chains->labels;
329
+
330
+ chains->first_atom = realloc(chains->first_atom,
331
+ sizeof(int) * new_size);
332
+ if (chains->first_atom == NULL) {
333
+ chains->first_atom = fa;
334
+ return mem_fail();
335
+ }
336
+
337
+ chains->labels = realloc(chains->labels, new_size + 1);
338
+ if (chains->labels == NULL) {
339
+ chains->labels = lbl;
340
+ return mem_fail();
341
+ }
342
+
343
+ chains->n_alloc = new_size;
344
+ }
345
+ ++chains->n;
346
+ return FREESASA_SUCCESS;
347
+ }
348
+
349
+ static void
350
+ chains_dealloc(struct chains *chains)
351
+ {
352
+ if (chains) {
353
+ free(chains->first_atom);
354
+ free(chains->labels);
355
+ *chains = chains_init();
356
+ }
357
+ }
358
+
359
+ freesasa_structure *
360
+ freesasa_structure_new(void)
361
+ {
362
+ freesasa_structure *s = malloc(sizeof(freesasa_structure));
363
+
364
+ if (s == NULL) goto memerr;
365
+
366
+ s->atoms = atoms_init();
367
+ s->residues = residues_init();
368
+ s->chains = chains_init();
369
+ s->xyz = freesasa_coord_new();
370
+ s->model = 1;
371
+ s->classifier_name = NULL;
372
+ s->cif_ref = 0;
373
+ s->release_cif_ref = 0;
374
+
375
+ if (s->xyz == NULL) goto memerr;
376
+
377
+ return s;
378
+ memerr:
379
+ freesasa_structure_free(s);
380
+ mem_fail();
381
+ return NULL;
382
+ }
383
+
384
+ void freesasa_structure_free(freesasa_structure *s)
385
+ {
386
+ if (s != NULL) {
387
+ atoms_dealloc(&s->atoms);
388
+ residues_dealloc(&s->residues);
389
+ chains_dealloc(&s->chains);
390
+
391
+ if (s->xyz != NULL) {
392
+ freesasa_coord_free(s->xyz);
393
+ }
394
+
395
+ free(s->classifier_name);
396
+
397
+ if (s->cif_ref > 0 && s->release_cif_ref != NULL) {
398
+ s->release_cif_ref(s->cif_ref);
399
+ }
400
+
401
+ free(s);
402
+ }
403
+ }
404
+
405
+ /**
406
+ This function is called when either the symbol field is missing
407
+ from an ATOM record, or when an atom is added directly using
408
+ freesasa_structure_add_atom() or
409
+ freesasa_structure_add_atom_wopt(). The symbol is in turn only
410
+ used if the atom cannot be recognized by the classifier.
411
+ */
412
+ static int
413
+ guess_symbol(char *symbol,
414
+ const char *name)
415
+ {
416
+ /* if the first position is empty, or a number, assume that it is
417
+ a one letter element e.g. " C ", or "1H " */
418
+ if (name[0] == ' ' || (name[0] >= '1' && name[0] <= '9')) {
419
+ symbol[0] = ' ';
420
+ symbol[1] = name[1];
421
+ symbol[2] = '\0';
422
+ } else {
423
+ /* if the string has padding to the right, it's a
424
+ two-letter element, e.g. "FE " */
425
+ if (name[3] == ' ') {
426
+ strncpy(symbol, name, 2);
427
+ symbol[2] = '\0';
428
+ } else {
429
+ /* If it's a four-letter string, it's hard to say,
430
+ assume only the first letter signifies the element */
431
+ symbol[0] = ' ';
432
+ symbol[1] = name[0];
433
+ symbol[2] = '\0';
434
+ return freesasa_warn("guessing that atom '%s' is symbol '%s'",
435
+ name, symbol);
436
+ }
437
+ }
438
+ return FREESASA_SUCCESS;
439
+ }
440
+ static int
441
+ structure_add_chain(freesasa_structure *s,
442
+ char chain_label,
443
+ int i_latest_atom)
444
+ {
445
+ int n;
446
+ if (s->chains.n == 0 || strchr(s->chains.labels, chain_label) == NULL) {
447
+
448
+ if (chains_alloc(&s->chains) == FREESASA_FAIL)
449
+ return fail_msg("");
450
+
451
+ n = s->chains.n;
452
+ s->chains.labels[n - 1] = chain_label;
453
+ s->chains.labels[n] = '\0';
454
+
455
+ assert(strlen(s->chains.labels) == s->chains.n);
456
+
457
+ s->chains.first_atom[n - 1] = i_latest_atom;
458
+ }
459
+ return FREESASA_SUCCESS;
460
+ }
461
+
462
+ static int
463
+ structure_add_residue(freesasa_structure *s,
464
+ const freesasa_classifier *classifier,
465
+ const struct atom *a,
466
+ int i_latest_atom)
467
+ {
468
+ int n = s->residues.n + 1;
469
+ const freesasa_nodearea *reference = NULL;
470
+
471
+ /* register a new residue if it's the first atom, or if the
472
+ residue number or chain label of the current atom is different
473
+ from the previous one */
474
+ if (!(s->residues.n == 0 ||
475
+ (i_latest_atom > 0 &&
476
+ (strcmp(a->res_number, s->atoms.atom[i_latest_atom - 1]->res_number) ||
477
+ a->chain_label != s->atoms.atom[i_latest_atom - 1]->chain_label)))) {
478
+ return FREESASA_SUCCESS;
479
+ }
480
+
481
+ if (residues_alloc(&s->residues) == FREESASA_FAIL) {
482
+ return fail_msg("");
483
+ }
484
+ s->residues.first_atom[n - 1] = i_latest_atom;
485
+
486
+ s->residues.reference_area[n - 1] = NULL;
487
+ reference = freesasa_classifier_residue_reference(classifier, a->res_name);
488
+ if (reference != NULL) {
489
+ s->residues.reference_area[n - 1] = malloc(sizeof(freesasa_nodearea));
490
+ if (s->residues.reference_area[n - 1] == NULL)
491
+ return mem_fail();
492
+ *s->residues.reference_area[n - 1] = *reference;
493
+ }
494
+
495
+ return FREESASA_SUCCESS;
496
+ }
497
+
498
+ /**
499
+ Get the radius of an atom, and fail, warn and/or guess depending
500
+ on the options.
501
+ */
502
+ static int
503
+ structure_check_atom_radius(double *radius,
504
+ struct atom *a,
505
+ const freesasa_classifier *classifier,
506
+ int options)
507
+ {
508
+ *radius = freesasa_classifier_radius(classifier, a->res_name, a->atom_name);
509
+ if (*radius < 0) {
510
+ if (options & FREESASA_HALT_AT_UNKNOWN) {
511
+ return fail_msg("atom '%s %s' unknown",
512
+ a->res_name, a->atom_name);
513
+ } else if (options & FREESASA_SKIP_UNKNOWN) {
514
+ return freesasa_warn("skipping unknown atom '%s %s'",
515
+ a->res_name, a->atom_name, a->symbol, *radius);
516
+ } else {
517
+ *radius = freesasa_guess_radius(a->symbol);
518
+ if (*radius < 0) {
519
+ *radius = +0.;
520
+ freesasa_warn("atom '%s %s' unknown and "
521
+ "can't guess radius of symbol '%s', "
522
+ "assigning radius 0 A",
523
+ a->res_name, a->atom_name, a->symbol);
524
+ } else {
525
+ freesasa_warn("atom '%s %s' unknown, guessing element is '%s', "
526
+ "and radius %.3f A",
527
+ a->res_name, a->atom_name, a->symbol, *radius);
528
+ }
529
+ /* do not return FREESASA_WARN here, because we will keep the atom */
530
+ }
531
+ }
532
+ return FREESASA_SUCCESS;
533
+ }
534
+
535
+ static int
536
+ structure_register_classifier(freesasa_structure *structure,
537
+ const freesasa_classifier *classifier)
538
+ {
539
+ if (structure->classifier_name == NULL) {
540
+ structure->classifier_name = strdup(freesasa_classifier_name(classifier));
541
+ if (structure->classifier_name == NULL) {
542
+ return mem_fail();
543
+ }
544
+ } else if (strcmp(structure->classifier_name, freesasa_classifier_name(classifier)) != 0) {
545
+ structure->classifier_name = strdup(FREESASA_CONFLICTING_CLASSIFIERS);
546
+ if (structure->classifier_name == NULL) {
547
+ return mem_fail();
548
+ }
549
+ return FREESASA_WARN;
550
+ }
551
+
552
+ return FREESASA_SUCCESS;
553
+ }
554
+
555
+ /**
556
+ Adds an atom to the structure using the rules specified by
557
+ 'options'. If it includes FREESASA_RADIUS_FROM_* a dummy radius is
558
+ assigned and the caller is expected to replace it with a correct
559
+ radius later.
560
+
561
+ The atom a should be a pointer to a heap address, this will not be cloned.
562
+ */
563
+ static int
564
+ structure_add_atom(freesasa_structure *structure,
565
+ struct atom *atom,
566
+ double *xyz,
567
+ const freesasa_classifier *classifier,
568
+ int options)
569
+ {
570
+ int na, ret;
571
+ double r;
572
+
573
+ assert(structure);
574
+ assert(atom);
575
+ assert(xyz);
576
+
577
+ /* let the stricter option override if both are specified */
578
+ if (options & FREESASA_SKIP_UNKNOWN && options & FREESASA_HALT_AT_UNKNOWN)
579
+ options &= ~FREESASA_SKIP_UNKNOWN;
580
+
581
+ if (classifier == NULL) {
582
+ classifier = &freesasa_default_classifier;
583
+ }
584
+ structure_register_classifier(structure, classifier);
585
+
586
+ /* calculate radius and check if we should keep the atom (based on options) */
587
+ if (options & FREESASA_RADIUS_FROM_OCCUPANCY) {
588
+ r = 1; /* fix it later */
589
+ } else {
590
+ ret = structure_check_atom_radius(&r, atom, classifier, options);
591
+ if (ret == FREESASA_FAIL) return fail_msg("halting at unknown atom");
592
+ if (ret == FREESASA_WARN) return FREESASA_WARN;
593
+ }
594
+ assert(r >= 0);
595
+
596
+ /* If it's a keeper, allocate memory */
597
+ if (atoms_alloc(&structure->atoms) == FREESASA_FAIL)
598
+ return fail_msg("");
599
+ na = structure->atoms.n;
600
+
601
+ /* Store coordinates */
602
+ if (freesasa_coord_append(structure->xyz, xyz, 1) == FREESASA_FAIL)
603
+ return mem_fail();
604
+
605
+ /* Check if this is a new chain and if so add it */
606
+ if (structure_add_chain(structure, atom->chain_label, na - 1) == FREESASA_FAIL)
607
+ return mem_fail();
608
+
609
+ /* Check if this is a new residue, and if so add it */
610
+ if (structure_add_residue(structure, classifier, atom, na - 1) == FREESASA_FAIL)
611
+ return mem_fail();
612
+
613
+ atom->the_class = freesasa_classifier_class(classifier, atom->res_name, atom->atom_name);
614
+ atom->res_index = structure->residues.n - 1;
615
+ structure->atoms.radius[na - 1] = r;
616
+ structure->atoms.atom[na - 1] = atom;
617
+
618
+ return FREESASA_SUCCESS;
619
+ }
620
+
621
+ /**
622
+ Handles the reading of PDB-files, returns NULL if problems reading
623
+ or input or malloc failure. Error-messages should explain what
624
+ went wrong.
625
+ */
626
+ static freesasa_structure *
627
+ from_pdb_impl(FILE *pdb_file,
628
+ struct file_range it,
629
+ const freesasa_classifier *classifier,
630
+ int options)
631
+ {
632
+ char line[PDB_MAX_LINE_STRL];
633
+ char alt, the_alt = ' ';
634
+ double v[3], r;
635
+ int ret;
636
+ struct atom *a = NULL;
637
+ freesasa_structure *s = freesasa_structure_new();
638
+
639
+ assert(pdb_file);
640
+
641
+ if (s == NULL) return NULL;
642
+
643
+ fseek(pdb_file, it.begin, SEEK_SET);
644
+
645
+ while (fgets(line, PDB_MAX_LINE_STRL, pdb_file) != NULL && ftell(pdb_file) <= it.end) {
646
+
647
+ if (strncmp("ATOM", line, 4) == 0 || ((options & FREESASA_INCLUDE_HETATM) &&
648
+ (strncmp("HETATM", line, 6) == 0))) {
649
+ if (freesasa_pdb_ishydrogen(line) &&
650
+ !(options & FREESASA_INCLUDE_HYDROGEN))
651
+ continue;
652
+
653
+ a = atom_new_from_line(line, &alt);
654
+ if (a == NULL)
655
+ goto cleanup;
656
+
657
+ if ((alt != ' ' && the_alt == ' ') || (alt == ' '))
658
+ the_alt = alt;
659
+ else if (alt != ' ' && alt != the_alt) {
660
+ atom_free(a);
661
+ a = NULL;
662
+ continue;
663
+ }
664
+
665
+ ret = freesasa_pdb_get_coord(v, line);
666
+ if (ret == FREESASA_FAIL)
667
+ goto cleanup;
668
+
669
+ ret = structure_add_atom(s, a, v, classifier, options);
670
+ if (ret == FREESASA_FAIL) {
671
+ goto cleanup;
672
+ } else if (ret == FREESASA_WARN) {
673
+ atom_free(a);
674
+ a = NULL;
675
+ continue;
676
+ }
677
+
678
+ if (options & FREESASA_RADIUS_FROM_OCCUPANCY) {
679
+ ret = freesasa_pdb_get_occupancy(&r, line);
680
+ if (ret == FREESASA_FAIL)
681
+ goto cleanup;
682
+ s->atoms.radius[s->atoms.n - 1] = r;
683
+ }
684
+ }
685
+
686
+ if (!(options & FREESASA_JOIN_MODELS)) {
687
+ if (strncmp("MODEL", line, 5) == 0) sscanf(line + 10, "%d", &s->model);
688
+ if (strncmp("ENDMDL", line, 6) == 0) break;
689
+ }
690
+ }
691
+
692
+ if (s->atoms.n == 0) {
693
+ fail_msg("input had no valid ATOM or HETATM lines");
694
+ goto cleanup;
695
+ }
696
+
697
+ return s;
698
+
699
+ cleanup:
700
+ fail_msg("");
701
+ atom_free(a);
702
+ freesasa_structure_free(s);
703
+ return NULL;
704
+ }
705
+
706
+ static int
707
+ structure_add_atom_wopt_impl(freesasa_structure *structure,
708
+ const char *atom_name,
709
+ const char *residue_name,
710
+ const char *residue_number,
711
+ const char *symbol,
712
+ char chain_label,
713
+ double x, double y, double z,
714
+ const freesasa_classifier *classifier,
715
+ int options)
716
+ {
717
+ struct atom *a;
718
+ char my_symbol[PDB_ATOM_SYMBOL_STRL + 1];
719
+ double v[3] = {x, y, z};
720
+ int ret, warn = 0;
721
+
722
+ assert(structure);
723
+ assert(atom_name);
724
+ assert(residue_name);
725
+ assert(residue_number);
726
+
727
+ /* this option can not be used here, and needs to be unset */
728
+ options &= ~FREESASA_RADIUS_FROM_OCCUPANCY;
729
+
730
+ if (symbol != NULL) {
731
+ strncpy(my_symbol, symbol, sizeof(my_symbol));
732
+ } else if (guess_symbol(my_symbol, atom_name) == FREESASA_WARN &&
733
+ options & FREESASA_SKIP_UNKNOWN) {
734
+ ++warn;
735
+ }
736
+
737
+ a = atom_new(residue_name, residue_number, atom_name, my_symbol, chain_label);
738
+ if (a == NULL) return mem_fail();
739
+
740
+ ret = structure_add_atom(structure, a, v, classifier, options);
741
+
742
+ if (ret == FREESASA_FAIL ||
743
+ (ret == FREESASA_WARN && options & FREESASA_SKIP_UNKNOWN))
744
+ atom_free(a);
745
+
746
+ if (!ret && warn) return FREESASA_WARN;
747
+
748
+ return ret;
749
+ }
750
+
751
+ int freesasa_structure_add_atom_wopt(freesasa_structure *structure,
752
+ const char *atom_name,
753
+ const char *residue_name,
754
+ const char *residue_number,
755
+ char chain_label,
756
+ double x, double y, double z,
757
+ const freesasa_classifier *classifier,
758
+ int options)
759
+ {
760
+ return structure_add_atom_wopt_impl(structure, atom_name, residue_name, residue_number, NULL,
761
+ chain_label, x, y, z, classifier, options);
762
+ }
763
+ int freesasa_structure_add_atom(freesasa_structure *structure,
764
+ const char *atom_name,
765
+ const char *residue_name,
766
+ const char *residue_number,
767
+ char chain_label,
768
+ double x, double y, double z)
769
+ {
770
+ return structure_add_atom_wopt_impl(structure, atom_name, residue_name, residue_number, NULL,
771
+ chain_label, x, y, z, NULL, 0);
772
+ }
773
+
774
+ int freesasa_structure_add_cif_atom(freesasa_structure *structure,
775
+ freesasa_cif_atom *atom,
776
+ const freesasa_classifier *classifier,
777
+ int options)
778
+ {
779
+ char res_number[PDB_ATOM_RES_NUMBER_STRL + 1];
780
+
781
+ if (atom->pdbx_PDB_ins_code[0] != '?') {
782
+ snprintf(res_number, sizeof res_number, "%s%c", atom->auth_seq_id, atom->pdbx_PDB_ins_code[0]);
783
+ } else {
784
+ snprintf(res_number, sizeof res_number, "%s", atom->auth_seq_id);
785
+ }
786
+
787
+ return structure_add_atom_wopt_impl(structure, atom->auth_atom_id, atom->auth_comp_id,
788
+ res_number, atom->type_symbol, atom->auth_asym_id,
789
+ atom->Cartn_x, atom->Cartn_y, atom->Cartn_z,
790
+ classifier, options);
791
+ }
792
+
793
+ freesasa_structure *
794
+ freesasa_structure_from_pdb(FILE *pdb_file,
795
+ const freesasa_classifier *classifier,
796
+ int options)
797
+ {
798
+ assert(pdb_file);
799
+ return from_pdb_impl(pdb_file, freesasa_whole_file(pdb_file),
800
+ classifier, options);
801
+ }
802
+
803
+ freesasa_structure **
804
+ freesasa_structure_array(FILE *pdb,
805
+ int *n,
806
+ const freesasa_classifier *classifier,
807
+ int options)
808
+ {
809
+ struct file_range *models = NULL, *chains = NULL;
810
+ struct file_range whole_file;
811
+ int n_models = 0, n_chains = 0, j0, n_new_chains, i, j;
812
+ freesasa_structure **ss = NULL, **ssb;
813
+
814
+ assert(pdb);
815
+ assert(n);
816
+
817
+ if (!(options & FREESASA_SEPARATE_MODELS ||
818
+ options & FREESASA_SEPARATE_CHAINS)) {
819
+ fail_msg("options need to specify at least one of FREESASA_SEPARATE_CHAINS "
820
+ "and FREESASA_SEPARATE_MODELS");
821
+ return NULL;
822
+ }
823
+
824
+ whole_file = freesasa_whole_file(pdb);
825
+ n_models = freesasa_pdb_get_models(pdb, &models);
826
+
827
+ if (n_models == FREESASA_FAIL) {
828
+ fail_msg("problems reading PDB-file");
829
+ return NULL;
830
+ }
831
+ if (n_models == 0) {
832
+ models = &whole_file;
833
+ n_models = 1;
834
+ }
835
+
836
+ /* only keep first model if option not provided */
837
+ if (!(options & FREESASA_SEPARATE_MODELS)) n_models = 1;
838
+
839
+ /* for each model read chains if requested */
840
+ if (options & FREESASA_SEPARATE_CHAINS) {
841
+ for (i = 0; i < n_models; ++i) {
842
+ chains = NULL;
843
+ n_new_chains = freesasa_pdb_get_chains(pdb, models[i], &chains, options);
844
+
845
+ if (n_new_chains == FREESASA_FAIL) goto cleanup;
846
+ if (n_new_chains == 0) {
847
+ freesasa_warn("in %s(): no chains found (in model %d)", __func__, i + 1);
848
+ continue;
849
+ }
850
+
851
+ ssb = ss;
852
+ ss = realloc(ss, sizeof(freesasa_structure *) * (n_chains + n_new_chains));
853
+
854
+ if (!ss) {
855
+ ss = ssb;
856
+ mem_fail();
857
+ goto cleanup;
858
+ }
859
+
860
+ j0 = n_chains;
861
+ n_chains += n_new_chains;
862
+
863
+ for (j = 0; j < n_new_chains; ++j)
864
+ ss[j0 + j] = NULL;
865
+
866
+ for (j = 0; j < n_new_chains; ++j) {
867
+ ss[j0 + j] = from_pdb_impl(pdb, chains[j], classifier, options);
868
+ if (ss[j0 + j] == NULL) goto cleanup;
869
+ ss[j0 + j]->model = i + 1;
870
+ }
871
+
872
+ free(chains);
873
+ }
874
+ *n = n_chains;
875
+ } else {
876
+ ss = malloc(sizeof(freesasa_structure *) * n_models);
877
+ if (!ss) {
878
+ mem_fail();
879
+ goto cleanup;
880
+ }
881
+
882
+ for (i = 0; i < n_models; ++i)
883
+ ss[i] = NULL;
884
+ *n = n_models;
885
+
886
+ for (i = 0; i < n_models; ++i) {
887
+ ss[i] = from_pdb_impl(pdb, models[i], classifier, options);
888
+ if (ss[i] == NULL) goto cleanup;
889
+ ss[i]->model = i + 1;
890
+ }
891
+ }
892
+
893
+ if (*n == 0) goto cleanup;
894
+
895
+ if (models != &whole_file) free(models);
896
+
897
+ return ss;
898
+
899
+ cleanup:
900
+ if (ss)
901
+ for (i = 0; i < *n; ++i)
902
+ freesasa_structure_free(ss[i]);
903
+ if (models != &whole_file) free(models);
904
+ free(chains);
905
+ *n = 0;
906
+ free(ss);
907
+ return NULL;
908
+ }
909
+
910
+ freesasa_structure *
911
+ freesasa_structure_get_chains(const freesasa_structure *structure,
912
+ const char *chains,
913
+ const freesasa_classifier *classifier,
914
+ int options)
915
+ {
916
+ freesasa_structure *new_s;
917
+ struct atom *ai;
918
+ int i, res;
919
+ char c;
920
+ const double *v;
921
+
922
+ assert(structure);
923
+ if (strlen(chains) == 0) return NULL;
924
+
925
+ new_s = freesasa_structure_new();
926
+
927
+ if (!new_s) {
928
+ mem_fail();
929
+ return NULL;
930
+ }
931
+
932
+ new_s->model = structure->model;
933
+
934
+ for (i = 0; i < structure->atoms.n; ++i) {
935
+ ai = structure->atoms.atom[i];
936
+ c = ai->chain_label;
937
+ if (strchr(chains, c) != NULL) {
938
+ v = freesasa_coord_i(structure->xyz, i);
939
+ res = structure_add_atom_wopt_impl(new_s, ai->atom_name,
940
+ ai->res_name, ai->res_number, ai->symbol,
941
+ c, v[0], v[1], v[2], classifier, options);
942
+ if (res == FREESASA_FAIL) {
943
+ fail_msg("");
944
+ goto cleanup;
945
+ }
946
+ }
947
+ }
948
+
949
+ /* the following two tests could have been done by comparing the
950
+ chain-strings before the loop, but this logic is simpler. */
951
+ if (new_s->atoms.n == 0) {
952
+ goto cleanup;
953
+ }
954
+ if (strlen(new_s->chains.labels) != strlen(chains)) {
955
+ fail_msg("structure has chains '%s', but '%s' requested",
956
+ structure->chains.labels, chains);
957
+ goto cleanup;
958
+ }
959
+
960
+ return new_s;
961
+
962
+ cleanup:
963
+ freesasa_structure_free(new_s);
964
+ return NULL;
965
+ }
966
+
967
+ const char *
968
+ freesasa_structure_chain_labels(const freesasa_structure *structure)
969
+ {
970
+ assert(structure);
971
+ return structure->chains.labels;
972
+ }
973
+
974
+ const coord_t *
975
+ freesasa_structure_xyz(const freesasa_structure *structure)
976
+ {
977
+ assert(structure);
978
+ return structure->xyz;
979
+ }
980
+
981
+ int freesasa_structure_n(const freesasa_structure *structure)
982
+ {
983
+ assert(structure);
984
+ return structure->atoms.n;
985
+ }
986
+
987
+ int freesasa_structure_n_residues(const freesasa_structure *structure)
988
+ {
989
+ assert(structure);
990
+ return structure->residues.n;
991
+ }
992
+
993
+ const char *
994
+ freesasa_structure_atom_name(const freesasa_structure *structure,
995
+ int i)
996
+ {
997
+ assert(structure);
998
+ assert(i < structure->atoms.n && i >= 0);
999
+ return structure->atoms.atom[i]->atom_name;
1000
+ }
1001
+
1002
+ const char *
1003
+ freesasa_structure_atom_res_name(const freesasa_structure *structure,
1004
+ int i)
1005
+ {
1006
+ assert(structure);
1007
+ assert(i < structure->atoms.n && i >= 0);
1008
+ return structure->atoms.atom[i]->res_name;
1009
+ }
1010
+
1011
+ const char *
1012
+ freesasa_structure_atom_res_number(const freesasa_structure *structure,
1013
+ int i)
1014
+ {
1015
+ assert(structure);
1016
+ assert(i < structure->atoms.n && i >= 0);
1017
+ return structure->atoms.atom[i]->res_number;
1018
+ }
1019
+
1020
+ char freesasa_structure_atom_chain(const freesasa_structure *structure,
1021
+ int i)
1022
+ {
1023
+ assert(structure);
1024
+ assert(i < structure->atoms.n && i >= 0);
1025
+ return structure->atoms.atom[i]->chain_label;
1026
+ }
1027
+ const char *
1028
+ freesasa_structure_atom_symbol(const freesasa_structure *structure,
1029
+ int i)
1030
+ {
1031
+ assert(structure);
1032
+ assert(i < structure->atoms.n && i >= 0);
1033
+ return structure->atoms.atom[i]->symbol;
1034
+ }
1035
+
1036
+ double
1037
+ freesasa_structure_atom_radius(const freesasa_structure *structure,
1038
+ int i)
1039
+ {
1040
+ assert(structure);
1041
+ assert(i < structure->atoms.n && i >= 0);
1042
+ return structure->atoms.radius[i];
1043
+ }
1044
+
1045
+ void freesasa_structure_atom_set_radius(freesasa_structure *structure,
1046
+ int i,
1047
+ double radius)
1048
+ {
1049
+ assert(structure);
1050
+ assert(i < structure->atoms.n && i >= 0);
1051
+ structure->atoms.radius[i] = radius;
1052
+ }
1053
+
1054
+ freesasa_atom_class
1055
+ freesasa_structure_atom_class(const freesasa_structure *structure,
1056
+ int i)
1057
+ {
1058
+ assert(structure);
1059
+ assert(i < structure->atoms.n && i >= 0);
1060
+ return structure->atoms.atom[i]->the_class;
1061
+ }
1062
+
1063
+ const char *
1064
+ freesasa_structure_atom_pdb_line(const freesasa_structure *structure,
1065
+ int i)
1066
+ {
1067
+ assert(structure);
1068
+ assert(i < structure->atoms.n && i >= 0);
1069
+ return structure->atoms.atom[i]->line;
1070
+ }
1071
+ const freesasa_nodearea *
1072
+ freesasa_structure_residue_reference(const freesasa_structure *structure,
1073
+ int r_i)
1074
+ {
1075
+ assert(structure);
1076
+ assert(r_i >= 0 && r_i < structure->residues.n);
1077
+
1078
+ return structure->residues.reference_area[r_i];
1079
+ }
1080
+ int freesasa_structure_residue_atoms(const freesasa_structure *structure,
1081
+ int r_i,
1082
+ int *first,
1083
+ int *last)
1084
+ {
1085
+ int naa;
1086
+ assert(structure);
1087
+ assert(first);
1088
+ assert(last);
1089
+
1090
+ naa = structure->residues.n;
1091
+
1092
+ assert(r_i >= 0 && r_i < naa);
1093
+
1094
+ *first = structure->residues.first_atom[r_i];
1095
+ if (r_i == naa - 1)
1096
+ *last = structure->atoms.n - 1;
1097
+ else
1098
+ *last = structure->residues.first_atom[r_i + 1] - 1;
1099
+ assert(*last >= *first);
1100
+
1101
+ return FREESASA_SUCCESS;
1102
+ }
1103
+
1104
+ const char *
1105
+ freesasa_structure_residue_name(const freesasa_structure *structure,
1106
+ int r_i)
1107
+ {
1108
+ assert(structure);
1109
+ assert(r_i < structure->residues.n && r_i >= 0);
1110
+ return structure->atoms.atom[structure->residues.first_atom[r_i]]->res_name;
1111
+ }
1112
+
1113
+ const char *
1114
+ freesasa_structure_residue_number(const freesasa_structure *structure,
1115
+ int r_i)
1116
+ {
1117
+ assert(structure);
1118
+ assert(r_i < structure->residues.n && r_i >= 0);
1119
+ return structure->atoms.atom[structure->residues.first_atom[r_i]]->res_number;
1120
+ }
1121
+
1122
+ char freesasa_structure_residue_chain(const freesasa_structure *structure,
1123
+ int r_i)
1124
+ {
1125
+ assert(structure);
1126
+ assert(r_i < structure->residues.n && r_i >= 0);
1127
+
1128
+ return structure->atoms.atom[structure->residues.first_atom[r_i]]->chain_label;
1129
+ }
1130
+
1131
+ int freesasa_structure_n_chains(const freesasa_structure *structure)
1132
+ {
1133
+ return structure->chains.n;
1134
+ }
1135
+
1136
+ int freesasa_structure_chain_index(const freesasa_structure *structure,
1137
+ char chain)
1138
+ {
1139
+ int i;
1140
+
1141
+ assert(structure);
1142
+
1143
+ for (i = 0; i < structure->chains.n; ++i) {
1144
+ if (structure->chains.labels[i] == chain) return i;
1145
+ }
1146
+ return fail_msg("chain %c not found", chain);
1147
+ }
1148
+
1149
+ int freesasa_structure_chain_atoms(const freesasa_structure *structure,
1150
+ char chain,
1151
+ int *first,
1152
+ int *last)
1153
+ {
1154
+ int c_i, n;
1155
+
1156
+ assert(structure);
1157
+
1158
+ c_i = freesasa_structure_chain_index(structure, chain);
1159
+ n = freesasa_structure_n_chains(structure);
1160
+
1161
+ if (c_i < 0) return fail_msg("");
1162
+
1163
+ *first = structure->chains.first_atom[c_i];
1164
+ if (c_i == n - 1)
1165
+ *last = freesasa_structure_n(structure) - 1;
1166
+ else
1167
+ *last = structure->chains.first_atom[c_i + 1] - 1;
1168
+ assert(*last >= *first);
1169
+
1170
+ return FREESASA_SUCCESS;
1171
+ }
1172
+
1173
+ int freesasa_structure_chain_residues(const freesasa_structure *structure,
1174
+ char chain,
1175
+ int *first,
1176
+ int *last)
1177
+ {
1178
+ int first_atom, last_atom;
1179
+
1180
+ assert(structure);
1181
+
1182
+ if (freesasa_structure_chain_atoms(structure, chain, &first_atom, &last_atom))
1183
+ return fail_msg("");
1184
+
1185
+ *first = structure->atoms.atom[first_atom]->res_index;
1186
+ *last = structure->atoms.atom[last_atom]->res_index;
1187
+
1188
+ return FREESASA_SUCCESS;
1189
+ }
1190
+
1191
+ const char *
1192
+ freesasa_structure_classifier_name(const freesasa_structure *structure)
1193
+ {
1194
+ assert(structure);
1195
+ return structure->classifier_name;
1196
+ }
1197
+
1198
+ int freesasa_structure_model(const freesasa_structure *structure)
1199
+ {
1200
+ return structure->model;
1201
+ }
1202
+
1203
+ const double *
1204
+ freesasa_structure_coord_array(const freesasa_structure *structure)
1205
+ {
1206
+ return freesasa_coord_all(structure->xyz);
1207
+ }
1208
+
1209
+ const double *
1210
+ freesasa_structure_radius(const freesasa_structure *structure)
1211
+ {
1212
+ assert(structure);
1213
+ return structure->atoms.radius;
1214
+ }
1215
+
1216
+ void freesasa_structure_set_radius(freesasa_structure *structure,
1217
+ const double *radii)
1218
+ {
1219
+ assert(structure);
1220
+ assert(radii);
1221
+ memcpy(structure->atoms.radius, radii, structure->atoms.n * sizeof(double));
1222
+ }
1223
+
1224
+ void freesasa_structure_set_model(freesasa_structure *structure,
1225
+ int model)
1226
+ {
1227
+ structure->model = model;
1228
+ }
1229
+
1230
+ void freesasa_structure_set_cif_ref(freesasa_structure *structure,
1231
+ size_t ref,
1232
+ void (*release_func)(size_t))
1233
+ {
1234
+ assert(structure);
1235
+ structure->cif_ref = ref;
1236
+ structure->release_cif_ref = release_func;
1237
+ }
1238
+
1239
+ size_t freesasa_structure_cif_ref(const freesasa_structure *structure)
1240
+ {
1241
+ assert(structure);
1242
+ return structure->cif_ref;
1243
+ }
.venv_haddock/include/site/python3.12/freesasa/util.c ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #if HAVE_CONFIG_H
2
+ #include <config.h>
3
+ #endif
4
+ #include <assert.h>
5
+ #include <errno.h>
6
+ #include <stdarg.h>
7
+ #include <stdio.h>
8
+ #include <string.h>
9
+
10
+ #include "freesasa_internal.h"
11
+
12
+ #ifdef PACKAGE_NAME
13
+ const char *freesasa_name = PACKAGE_NAME;
14
+ #else
15
+ const char *freesasa_name = "freesasa";
16
+ #endif
17
+
18
+ static FILE *errlog = NULL;
19
+
20
+ struct file_range
21
+ freesasa_whole_file(FILE *file)
22
+ {
23
+ struct file_range range;
24
+
25
+ assert(file);
26
+ rewind(file);
27
+ range.begin = ftell(file);
28
+ fseek(file, 0, SEEK_END);
29
+ range.end = ftell(file);
30
+ rewind(file);
31
+ assert(range.begin <= range.end);
32
+
33
+ return range;
34
+ }
35
+
36
+ static void
37
+ freesasa_err_impl(int err,
38
+ const char *format,
39
+ va_list arg)
40
+ {
41
+ FILE *fp = stderr;
42
+
43
+ if (errlog != NULL) fp = errlog;
44
+
45
+ fprintf(fp, "%s: ", freesasa_name);
46
+ switch (err) {
47
+ case FREESASA_FAIL:
48
+ fputs("error: ", fp);
49
+ break;
50
+ case FREESASA_WARN:
51
+ fputs("warning: ", fp);
52
+ break;
53
+ default:
54
+ break;
55
+ }
56
+ vfprintf(fp, format, arg);
57
+ va_end(arg);
58
+ fputc('\n', fp);
59
+ fflush(fp);
60
+ }
61
+
62
+ int freesasa_fail(const char *format, ...)
63
+ {
64
+ va_list arg;
65
+
66
+ if (freesasa_get_verbosity() == FREESASA_V_SILENT) return FREESASA_FAIL;
67
+
68
+ va_start(arg, format);
69
+ freesasa_err_impl(FREESASA_FAIL, format, arg);
70
+ va_end(arg);
71
+
72
+ return FREESASA_FAIL;
73
+ }
74
+
75
+ int freesasa_warn(const char *format, ...)
76
+ {
77
+ va_list arg;
78
+ int v = freesasa_get_verbosity();
79
+
80
+ if (v == FREESASA_V_NOWARNINGS || v == FREESASA_V_SILENT) return FREESASA_WARN;
81
+
82
+ va_start(arg, format);
83
+ freesasa_err_impl(FREESASA_WARN, format, arg);
84
+ va_end(arg);
85
+
86
+ return FREESASA_WARN;
87
+ }
88
+
89
+ int freesasa_fail_wloc(const char *file,
90
+ int line,
91
+ const char *format,
92
+ ...)
93
+ {
94
+ FILE *fp = stderr;
95
+ va_list arg;
96
+
97
+ if (freesasa_get_verbosity() == FREESASA_V_SILENT) return FREESASA_FAIL;
98
+ if (errlog != NULL) fp = errlog;
99
+
100
+ fprintf(fp, "%s:%s:%d: error: ", freesasa_name, file, line);
101
+ va_start(arg, format);
102
+ vfprintf(fp, format, arg);
103
+ va_end(arg);
104
+ fputc('\n', fp);
105
+ fflush(fp);
106
+
107
+ return FREESASA_FAIL;
108
+ }
109
+
110
+ int freesasa_mem_fail(const char *file, int line)
111
+ {
112
+ return freesasa_fail_wloc(file, line, "Out of memory");
113
+ }
114
+
115
+ const char *
116
+ freesasa_thread_error(int error_code)
117
+ {
118
+ switch (error_code) {
119
+ case EDEADLK:
120
+ return "deadlock (EDEADLK)";
121
+ case EINVAL:
122
+ return "(EINVAL)";
123
+ case ESRCH:
124
+ return "no matching thread (ESRCH)";
125
+ case EAGAIN:
126
+ return "no resources to create thread (EAGAIN)";
127
+ }
128
+ return "Unknown thread error";
129
+ }
130
+
131
+ void freesasa_set_err_out(FILE *fp)
132
+ {
133
+ assert(fp);
134
+ errlog = fp;
135
+ }
136
+
137
+ FILE *
138
+ freesasa_get_err_out()
139
+ {
140
+ return errlog;
141
+ }
.venv_haddock/lib/._.DS_Store ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/._python3.12 ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/._.DS_Store ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/._site-packages ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._.DS_Store ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._Bio ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._BioSQL ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/.___pycache__ ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/.__distutils_hack ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/.__plotly_utils ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/.__yaml ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._biopython-1.87.dist-info ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._dateutil ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._distutils-precedence.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fbd1f4c19356c9f782c0fce50a927d08f5a7a89ae88cc56fe8e671aeba8ef15d
3
+ size 4096
.venv_haddock/lib/python3.12/site-packages/._freesasa-2.2.1.dist-info ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._freesasa.cpython-312-darwin.so ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._haddock ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._haddock3-2026.3.0.dist-info ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._jsonpickle ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._jsonpickle-4.1.1.dist-info ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._narwhals ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._narwhals-2.19.0.dist-info ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._numpy ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._numpy-2.4.4.dist-info ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._packaging ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._packaging-26.1.dist-info ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._pandas ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._pandas-2.3.3.dist-info ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._pdb_tools-2.6.1.dist-info ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._pdbtools ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._pip ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._pip-26.0.1.dist-info ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._plotly ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._plotly-6.5.2.dist-info ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._prodigy_lig ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._prodigy_lig-1.1.4.dist-info ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._prodigy_prot ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._prodigy_prot-2.3.0.dist-info ADDED
Binary file (4.1 kB). View file
 
.venv_haddock/lib/python3.12/site-packages/._python_dateutil-2.9.0.post0.dist-info ADDED
Binary file (4.1 kB). View file