Debian Codegraph @

tests/test_pdb.c

raw
1 #include <check.h>
2 #include <math.h>
3 #include <pdb.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include "tools.h"
9
10 START_TEST(test_pdb_empty_lines)
11 {
12 char buf[80];
13 double x[3];
14
15 // check string parsing
16 ck_assert_int_eq(freesasa_pdb_get_atom_name(buf, ""), FREESASA_FAIL);
17 ck_assert_str_eq(buf, "");
18 ck_assert_int_eq(freesasa_pdb_get_res_name(buf, ""), FREESASA_FAIL);
19 ck_assert_str_eq(buf, "");
20 ck_assert_int_eq(freesasa_pdb_get_res_number(buf, ""), FREESASA_FAIL);
21 ck_assert_str_eq(buf, "");
22 ck_assert_int_eq(freesasa_pdb_get_symbol(buf, ""), FREESASA_FAIL);
23 ck_assert_str_eq(buf, "");
24
25 // check coordinate parsing
26 ck_assert_int_eq(freesasa_pdb_get_coord(x, ""), FREESASA_FAIL);
27
28 // check label parsing
29 ck_assert_int_eq(freesasa_pdb_get_chain_label(""), '\0');
30 ck_assert_int_eq(freesasa_pdb_get_alt_coord_label(""), '\0');
31
32 // check element parsing
33 ck_assert_int_eq(freesasa_pdb_ishydrogen(""), FREESASA_FAIL);
34 }
35 END_TEST
36
37 START_TEST(test_pdb_lines)
38 {
39 char buf[80];
40 double x[3], v;
41 const char *lines[] = {
42 "ATOM 585 C ARG A 74 41.765 34.829 30.944 0.45 36.22 C",
43 "ATOM 573 NH1AARG A 72 34.110 28.437 27.768 1.00 35.02 N ",
44 "HETATM 610 O HOH A 83 27.707 15.908 4.653 1.00 20.30 O ",
45 "ATOM 573 H ARG A 72 34.110 28.437 27.768 1.00 35.02 H ",
46 "ATOM 585 C ARG A 74 41.765 34.829 0.45 36.22 C",
47 "HETATM 7673 CD CD A1978 30.426 14.804 -3.685 1.00 39.42 CD",
48 "ATOM 573 H ARG A 72 34.110 28.437 27.768 1.00 35.02 ",
49 "ATOM 573 D ARG A 72 34.110 28.437 27.768 1.00 35.02 D",
50 "ATOM 573 D ARG A 72 34.110 28.437 27.768 1.00 35.02 ",
51 };
52
53 //Atom-name
54 freesasa_pdb_get_atom_name(buf, lines[0]);
55 ck_assert_str_eq(buf, " C ");
56 freesasa_pdb_get_atom_name(buf, lines[1]);
57 ck_assert_str_eq(buf, " NH1");
58 freesasa_pdb_get_atom_name(buf, lines[2]);
59 ck_assert_str_eq(buf, " O ");
60
61 //Res-name
62 freesasa_pdb_get_res_name(buf, lines[0]);
63 ck_assert_str_eq(buf, "ARG");
64 freesasa_pdb_get_res_name(buf, lines[2]);
65 ck_assert_str_eq(buf, "HOH");
66
67 //Res-number
68 freesasa_pdb_get_res_number(buf, lines[0]);
69 ck_assert_str_eq(buf, " 74 ");
70 freesasa_pdb_get_res_number(buf, lines[1]);
71 ck_assert_str_eq(buf, " 72 ");
72 freesasa_pdb_get_res_number(buf, lines[2]);
73 ck_assert_str_eq(buf, " 83 ");
74
75 //coordinates
76 freesasa_pdb_get_coord(x, lines[0]);
77 ck_assert(float_eq(x[0], 41.765, 1e-6) &&
78 float_eq(x[1], 34.829, 1e-6) &&
79 float_eq(x[2], 30.944, 1e-6));
80 freesasa_set_verbosity(FREESASA_V_SILENT);
81 ck_assert_int_eq(freesasa_pdb_get_coord(x, lines[4]), FREESASA_FAIL);
82 freesasa_set_verbosity(FREESASA_V_NORMAL);
83
84 //chain label
85 ck_assert_int_eq(freesasa_pdb_get_chain_label(lines[0]), 'A');
86
87 // alt coord labels
88 ck_assert_int_eq(freesasa_pdb_get_alt_coord_label(lines[0]), ' ');
89 ck_assert_int_eq(freesasa_pdb_get_alt_coord_label(lines[1]), 'A');
90
91 // is hydrogen
92 ck_assert(!freesasa_pdb_ishydrogen(lines[0]));
93 ck_assert(freesasa_pdb_ishydrogen(lines[3]));
94 ck_assert(!freesasa_pdb_ishydrogen(lines[5]));
95 ck_assert(freesasa_pdb_ishydrogen(lines[6]));
96 ck_assert(freesasa_pdb_ishydrogen(lines[7]));
97 ck_assert(freesasa_pdb_ishydrogen(lines[8]));
98
99 // symbol
100 ck_assert_int_eq(freesasa_pdb_get_symbol(buf, lines[0]), FREESASA_SUCCESS);
101 ck_assert_str_eq(buf, " C");
102 ck_assert_int_eq(freesasa_pdb_get_symbol(buf, lines[1]), FREESASA_SUCCESS);
103 ck_assert_str_eq(buf, " N");
104
105 // B-factor
106 ck_assert_int_eq(freesasa_pdb_get_bfactor(&v, lines[0]), FREESASA_SUCCESS);
107 ck_assert(float_eq(v, 36.22, 1e-4));
108
109 // Occupancy
110 ck_assert_int_eq(freesasa_pdb_get_occupancy(&v, lines[0]), FREESASA_SUCCESS);
111 ck_assert(float_eq(v, 0.45, 1e-4));
112 }
113 END_TEST
114
115 START_TEST(test_get_models)
116 {
117 // FILE without models
118 FILE *pdb = fopen(DATADIR "1ubq.pdb", "r");
119 struct file_range *it;
120 int n = freesasa_pdb_get_models(pdb, &it);
121 ck_assert_int_eq(n, 0);
122 ck_assert(it == NULL);
123 fclose(pdb);
124
125 pdb = fopen(DATADIR "model_mismatch.pdb", "r");
126 freesasa_set_verbosity(FREESASA_V_SILENT);
127 ck_assert_int_eq(freesasa_pdb_get_models(pdb, &it), FREESASA_FAIL);
128 freesasa_set_verbosity(FREESASA_V_NORMAL);
129 fclose(pdb);
130
131 // this file has models
132 pdb = fopen(DATADIR "2jo4.pdb", "r");
133 n = freesasa_pdb_get_models(pdb, &it);
134 ck_assert_int_eq(n, 10);
135 for (int i = 0; i < n; ++i) {
136 char *line = NULL;
137 size_t len;
138 ck_assert_int_gt(it[i].end, it[i].begin);
139 fseek(pdb, it[i].begin, 0);
140 ck_assert(getline(&line, &len, pdb) > 0);
141 // each segment should begin with MODEL
142 ck_assert(strncmp(line, "MODEL", 5) == 0);
143 while (1) {
144 ck_assert(getline(&line, &len, pdb) > 0);
145 // there should be only one MODEL per model
146 ck_assert(strncmp(line, "MODEL", 5) != 0);
147 if (ftell(pdb) >= it[i].end) break;
148 }
149 // the last line of the segment should be ENDMDL
150 ck_assert(strncmp(line, "ENDMDL", 6) == 0);
151 free(line);
152 }
153 free(it);
154 fclose(pdb);
155 }
156 END_TEST
157
158 START_TEST(test_get_chains)
159 {
160 // Test a non PDB file
161 FILE *pdb = fopen(DATADIR "err.config", "r");
162 struct file_range whole_file = freesasa_whole_file(pdb), *it = NULL;
163 int nc = freesasa_pdb_get_chains(pdb, whole_file, &it, 0);
164 fclose(pdb);
165 ck_assert_int_eq(nc, 0);
166 ck_assert_ptr_eq(it, NULL);
167
168 // This file only has one chain
169 pdb = fopen(DATADIR "1ubq.pdb", "r");
170 whole_file = freesasa_whole_file(pdb);
171 nc = freesasa_pdb_get_chains(pdb, whole_file, &it, 0);
172 fclose(pdb);
173 ck_assert_int_eq(nc, 1);
174 ck_assert_ptr_ne(it, NULL);
175 free(it);
176
177 // This file has 4 chains
178 pdb = fopen(DATADIR "2jo4.pdb", "r");
179 int nm = freesasa_pdb_get_models(pdb, &it);
180 ck_assert_int_eq(nm, 10);
181 ck_assert_ptr_ne(it, NULL);
182 for (int i = 0; i < nm; ++i) {
183 struct file_range *jt = NULL;
184 nc = freesasa_pdb_get_chains(pdb, it[i], &jt, 0);
185 ck_assert_int_eq(nc, 4);
186 ck_assert_ptr_ne(jt, NULL);
187 for (int j = 1; j < nc; ++j) {
188 ck_assert_int_ge(jt[j].begin, jt[j - 1].end);
189 }
190 free(jt);
191 }
192 free(it);
193 }
194 END_TEST
195
196 extern TCase *test_pdb_static();
197
198 Suite *pdb_suite()
199 {
200 Suite *s = suite_create("PDB-parser");
201 TCase *tc_core = tcase_create("Core");
202 tcase_add_test(tc_core, test_pdb_empty_lines);
203 tcase_add_test(tc_core, test_pdb_lines);
204 tcase_add_test(tc_core, test_get_models);
205 tcase_add_test(tc_core, test_get_chains);
206
207 TCase *tc_static = test_pdb_static();
208
209 suite_add_tcase(s, tc_core);
210 suite_add_tcase(s, tc_static);
211
212 return s;
213 }