1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : adali.cxx // |
---|
4 | // Purpose : alignments // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #include <arbdbt.h> |
---|
12 | #include <adGene.h> |
---|
13 | |
---|
14 | #include "gb_local.h" |
---|
15 | |
---|
16 | #include <arb_strarray.h> |
---|
17 | #include <arb_str.h> |
---|
18 | #include <arb_global_defs.h> |
---|
19 | |
---|
20 | static void check_for_species_without_data(const char *species_name, long value, void *counterPtr) { |
---|
21 | if (value == 1) { |
---|
22 | long cnt = *((long*)counterPtr); |
---|
23 | if (cnt<40) { |
---|
24 | GB_warningf("Species '%s' has no data in any alignment", species_name); |
---|
25 | } |
---|
26 | *((long*)counterPtr) = cnt+1; |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | GBDATA *GBT_get_presets(GBDATA *gb_main) { |
---|
31 | return GBT_find_or_create(gb_main, "presets", 7); |
---|
32 | } |
---|
33 | |
---|
34 | int GBT_count_alignments(GBDATA *gb_main) { |
---|
35 | int count = 0; |
---|
36 | GBDATA *gb_presets = GBT_get_presets(gb_main); |
---|
37 | for (GBDATA *gb_ali = GB_entry(gb_presets, "alignment"); |
---|
38 | gb_ali; |
---|
39 | gb_ali = GB_nextEntry(gb_ali)) |
---|
40 | { |
---|
41 | ++count; |
---|
42 | } |
---|
43 | return count; |
---|
44 | } |
---|
45 | |
---|
46 | static GB_ERROR GBT_check_alignment(GBDATA *gb_main, GBDATA *preset_alignment, GB_HASH *species_name_hash) { |
---|
47 | /* check |
---|
48 | * - whether alignment has correct size and |
---|
49 | * - whether all data is present. |
---|
50 | * |
---|
51 | * Sets the security deletes and writes. |
---|
52 | * |
---|
53 | * If 'species_name_hash' is not NULp, |
---|
54 | * - it initially has to contain value == 1 for each existing species. |
---|
55 | * - afterwards it will contain value == 2 for each species where an alignment has been found. |
---|
56 | */ |
---|
57 | |
---|
58 | GBDATA *gb_species_data = GBT_get_species_data(gb_main); |
---|
59 | GBDATA *gb_extended_data = GBT_get_SAI_data(gb_main); |
---|
60 | |
---|
61 | GB_ERROR error = NULp; |
---|
62 | char *ali_name = GBT_read_string(preset_alignment, "alignment_name"); |
---|
63 | if (!ali_name) error = "Alignment w/o 'alignment_name'"; |
---|
64 | |
---|
65 | if (!error) { |
---|
66 | long security_write = -1; |
---|
67 | long stored_ali_len = -1; |
---|
68 | long found_ali_len = -1; |
---|
69 | long aligned = 1; |
---|
70 | GBDATA *gb_ali_len = NULp; |
---|
71 | |
---|
72 | { |
---|
73 | GBDATA *gb_ali_wsec = GB_entry(preset_alignment, "alignment_write_security"); |
---|
74 | if (!gb_ali_wsec) { |
---|
75 | error = "has no 'alignment_write_security' entry"; |
---|
76 | } |
---|
77 | else { |
---|
78 | security_write = GB_read_int(gb_ali_wsec); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | if (!error) { |
---|
84 | gb_ali_len = GB_entry(preset_alignment, "alignment_len"); |
---|
85 | if (!gb_ali_len) { |
---|
86 | error = "has no 'alignment_len' entry"; |
---|
87 | } |
---|
88 | else { |
---|
89 | stored_ali_len = GB_read_int(gb_ali_len); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | if (!error) { |
---|
94 | GBDATA *gb_species; |
---|
95 | for (gb_species = GBT_first_species_rel_species_data(gb_species_data); |
---|
96 | gb_species && !error; |
---|
97 | gb_species = GBT_next_species(gb_species)) |
---|
98 | { |
---|
99 | GBDATA *gb_name = GB_entry(gb_species, "name"); |
---|
100 | const char *name = NULp; |
---|
101 | int alignment_seen = 0; |
---|
102 | |
---|
103 | if (!gb_name) { |
---|
104 | // fatal: name is missing -> create a unique name |
---|
105 | char *unique = GBT_create_unique_species_name(gb_main, "autoname."); |
---|
106 | error = GBT_write_string(gb_species, "name", unique); |
---|
107 | |
---|
108 | if (!error) { |
---|
109 | gb_name = GB_entry(gb_species, "name"); |
---|
110 | GBS_write_hash(species_name_hash, unique, 1); // not seen before |
---|
111 | GB_warningf("Seen unnamed species (gave name '%s')", unique); |
---|
112 | } |
---|
113 | free(unique); |
---|
114 | } |
---|
115 | |
---|
116 | if (!error) { |
---|
117 | name = GB_read_char_pntr(gb_name); |
---|
118 | if (species_name_hash) { |
---|
119 | int seen = GBS_read_hash(species_name_hash, name); |
---|
120 | |
---|
121 | gb_assert(seen != 0); // species_name_hash not initialized correctly |
---|
122 | if (seen == 2) alignment_seen = 1; // already seen an alignment |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | if (!error) { |
---|
127 | GB_topSecurityLevel unsecured(gb_name); |
---|
128 | |
---|
129 | error = GB_write_security_delete(gb_name, 7); |
---|
130 | if (!error) error = GB_write_security_write(gb_name, 6); |
---|
131 | |
---|
132 | if (!error) { |
---|
133 | GBDATA *gb_ali = GB_entry(gb_species, ali_name); |
---|
134 | if (gb_ali) { |
---|
135 | GBDATA *gb_data = GB_entry(gb_ali, "data"); |
---|
136 | if (!gb_data) { |
---|
137 | error = GBT_write_string(gb_ali, "data", "Error: entry 'data' was missing and therefore was filled with this text."); |
---|
138 | GB_warningf("No '%s/data' entry for species '%s' (has been filled with dummy data)", ali_name, name); |
---|
139 | } |
---|
140 | else { |
---|
141 | if (GB_read_type(gb_data) != GB_STRING) { |
---|
142 | GB_delete(gb_data); |
---|
143 | error = GBS_global_string("'%s/data' of species '%s' had wrong DB-type (%s) and has been deleted!", |
---|
144 | ali_name, name, GB_read_key_pntr(gb_data)); |
---|
145 | } |
---|
146 | else { |
---|
147 | long data_len = GB_read_string_count(gb_data); |
---|
148 | if (found_ali_len != data_len) { |
---|
149 | if (found_ali_len>0) aligned = 0; |
---|
150 | if (found_ali_len<data_len) found_ali_len = data_len; |
---|
151 | } |
---|
152 | |
---|
153 | error = GB_write_security_delete(gb_data, 7); |
---|
154 | |
---|
155 | if (!alignment_seen && species_name_hash) { // mark as seen |
---|
156 | GBS_write_hash(species_name_hash, name, 2); // 2 means "species has data in at least 1 alignment" |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | if (!error) error = GB_write_security_delete(gb_species, security_write); |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | if (!error) { |
---|
169 | GBDATA *gb_sai; |
---|
170 | for (gb_sai = GBT_first_SAI_rel_SAI_data(gb_extended_data); |
---|
171 | gb_sai && !error; |
---|
172 | gb_sai = GBT_next_SAI(gb_sai)) |
---|
173 | { |
---|
174 | GBDATA *gb_sai_name = GB_entry(gb_sai, "name"); |
---|
175 | GBDATA *gb_ali; |
---|
176 | |
---|
177 | if (!gb_sai_name) continue; |
---|
178 | |
---|
179 | GB_write_security_delete(gb_sai_name, 7); |
---|
180 | |
---|
181 | gb_ali = GB_entry(gb_sai, ali_name); |
---|
182 | if (gb_ali) { |
---|
183 | GBDATA *gb_sai_data; |
---|
184 | for (gb_sai_data = GB_child(gb_ali); |
---|
185 | gb_sai_data; |
---|
186 | gb_sai_data = GB_nextChild(gb_sai_data)) |
---|
187 | { |
---|
188 | long type = GB_read_type(gb_sai_data); |
---|
189 | long data_len; |
---|
190 | |
---|
191 | if (type == GB_DB || type < GB_BITS) continue; |
---|
192 | if (GB_read_key_pntr(gb_sai_data)[0] == '_') continue; // e.g. _STRUCT (of secondary structure) |
---|
193 | |
---|
194 | data_len = GB_read_count(gb_sai_data); |
---|
195 | |
---|
196 | if (found_ali_len != data_len) { |
---|
197 | if (found_ali_len>0) aligned = 0; |
---|
198 | if (found_ali_len<data_len) found_ali_len = data_len; |
---|
199 | } |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | if (!error && stored_ali_len != found_ali_len) error = GB_write_int(gb_ali_len, found_ali_len); |
---|
206 | if (!error) error = GBT_write_int(preset_alignment, "aligned", aligned); |
---|
207 | |
---|
208 | if (error) { |
---|
209 | error = GBS_global_string("Error checking alignment '%s':\n%s\n", ali_name, error); |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | free(ali_name); |
---|
214 | |
---|
215 | return error; |
---|
216 | } |
---|
217 | |
---|
218 | GB_ERROR GBT_check_data(GBDATA *Main, const char *alignment_name) { |
---|
219 | /* alignment_name |
---|
220 | * == 0 -> check all existing alignments |
---|
221 | * otherwise -> check only one alignment |
---|
222 | */ |
---|
223 | |
---|
224 | GB_ERROR error = NULp; |
---|
225 | GBDATA *gb_sd = GBT_get_species_data(Main); |
---|
226 | GBDATA *gb_presets = GBT_get_presets(Main); |
---|
227 | GB_HASH *species_name_hash = NULp; |
---|
228 | |
---|
229 | // create rest of main containers |
---|
230 | GBT_get_SAI_data(Main); |
---|
231 | GBT_get_tree_data(Main); |
---|
232 | |
---|
233 | if (alignment_name) { |
---|
234 | GBDATA *gb_ali_name = GB_find_string(gb_presets, "alignment_name", alignment_name, GB_IGNORE_CASE, SEARCH_GRANDCHILD); |
---|
235 | if (!gb_ali_name) { |
---|
236 | error = GBS_global_string("Alignment '%s' does not exist - it can't be checked.", alignment_name); |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | if (!error) { |
---|
241 | // check whether we have an default alignment |
---|
242 | GBDATA *gb_use = GB_entry(gb_presets, "use"); |
---|
243 | if (!gb_use) { |
---|
244 | // if we have no default alignment -> look for any alignment |
---|
245 | GBDATA *gb_ali_name = GB_find_string(gb_presets, "alignment_name", alignment_name, GB_IGNORE_CASE, SEARCH_GRANDCHILD); |
---|
246 | |
---|
247 | error = gb_ali_name ? |
---|
248 | GBT_write_string(gb_presets, "use", GB_read_char_pntr(gb_ali_name)) : |
---|
249 | "No alignment defined"; |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | if (!alignment_name && !error) { |
---|
254 | // if all alignments are checked -> use species_name_hash to detect duplicated species and species w/o data |
---|
255 | long duplicates = 0; |
---|
256 | long unnamed = 0; |
---|
257 | |
---|
258 | species_name_hash = GBS_create_hash(GBT_get_species_count(Main), GB_IGNORE_CASE); |
---|
259 | |
---|
260 | if (!error) { |
---|
261 | for (GBDATA *gb_species = GBT_first_species_rel_species_data(gb_sd); |
---|
262 | gb_species; |
---|
263 | gb_species = GBT_next_species(gb_species)) |
---|
264 | { |
---|
265 | const char *name = GBT_get_name(gb_species); |
---|
266 | if (name) { |
---|
267 | if (GBS_read_hash(species_name_hash, name)) duplicates++; |
---|
268 | GBS_incr_hash(species_name_hash, name); |
---|
269 | } |
---|
270 | else { |
---|
271 | unnamed++; |
---|
272 | } |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | if (duplicates) { |
---|
277 | error = GBS_global_string("Database is corrupted:\n" |
---|
278 | "Found %li duplicated species with identical names!\n" |
---|
279 | "Fix the problem using\n" |
---|
280 | " 'Search For Equal Fields and Mark Duplicates'\n" |
---|
281 | "in ARB_NTREE search tool, save DB and restart ARB.", |
---|
282 | duplicates); |
---|
283 | } |
---|
284 | else if (unnamed) { |
---|
285 | error = GBS_global_string("Database is corrupted:\n" |
---|
286 | "Found %li species without IDs ('name')!\n" |
---|
287 | "This is a sewere problem!\n" |
---|
288 | "ARB will not work as expected!", |
---|
289 | unnamed); |
---|
290 | } |
---|
291 | } |
---|
292 | |
---|
293 | if (!error) { |
---|
294 | for (GBDATA *gb_ali = GB_entry(gb_presets, "alignment"); |
---|
295 | gb_ali && !error; |
---|
296 | gb_ali = GB_nextEntry(gb_ali)) |
---|
297 | { |
---|
298 | error = GBT_check_alignment(Main, gb_ali, species_name_hash); |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | if (species_name_hash) { |
---|
303 | if (!error) { |
---|
304 | long counter = 0; |
---|
305 | GBS_hash_do_const_loop(species_name_hash, check_for_species_without_data, &counter); |
---|
306 | if (counter>0) { |
---|
307 | GB_warningf("Found %li species without alignment data (only some were listed)", counter); |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | GBS_free_hash(species_name_hash); |
---|
312 | } |
---|
313 | |
---|
314 | return error; |
---|
315 | } |
---|
316 | |
---|
317 | void GBT_get_alignment_names(ConstStrArray& names, GBDATA *gbd) { |
---|
318 | /* Get names of existing alignments from database. |
---|
319 | * |
---|
320 | * Returns: array of strings, the last element is NULp |
---|
321 | */ |
---|
322 | |
---|
323 | GBDATA *presets = GBT_get_presets(gbd); |
---|
324 | for (GBDATA *ali = GB_entry(presets, "alignment"); ali; ali = GB_nextEntry(ali)) { |
---|
325 | GBDATA *name = GB_entry(ali, "alignment_name"); |
---|
326 | names.put(name ? GB_read_char_pntr(name) : "<unnamed alignment>"); |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | static char *gbt_nonexisting_alignment(GBDATA *gbMain) { |
---|
331 | char *ali_other = NULp; |
---|
332 | int counter; |
---|
333 | |
---|
334 | for (counter = 1; !ali_other; ++counter) { |
---|
335 | ali_other = GBS_global_string_copy("ali_x%i", counter); |
---|
336 | if (GBT_get_alignment(gbMain, ali_other)) freenull(ali_other); // exists -> continue |
---|
337 | } |
---|
338 | |
---|
339 | GB_clear_error(); // from GBT_get_alignment |
---|
340 | return ali_other; |
---|
341 | } |
---|
342 | |
---|
343 | GB_ERROR GBT_check_alignment_name(const char *alignment_name) { |
---|
344 | GB_ERROR error = GB_check_key(alignment_name); |
---|
345 | if (!error && !ARB_strBeginsWith(alignment_name, "ali_")) { |
---|
346 | error = GBS_global_string("alignment name '%s' has to start with 'ali_'", alignment_name); |
---|
347 | } |
---|
348 | return error; |
---|
349 | } |
---|
350 | |
---|
351 | static GB_ERROR create_ali_strEntry(GBDATA *gb_ali, const char *field, const char *strval, long write_protection) { |
---|
352 | GB_ERROR error = NULp; |
---|
353 | GBDATA *gb_sub = GB_create(gb_ali, field, GB_STRING); |
---|
354 | |
---|
355 | if (!gb_sub) error = GB_await_error(); |
---|
356 | else { |
---|
357 | error = GB_write_string(gb_sub, strval); |
---|
358 | if (!error) error = GB_write_security_delete(gb_sub, 7); |
---|
359 | if (!error) error = GB_write_security_write(gb_sub, write_protection); |
---|
360 | } |
---|
361 | |
---|
362 | if (error) { |
---|
363 | error = GBS_global_string("failed to create alignment subentry '%s'\n" |
---|
364 | "(Reason: %s)", field, error); |
---|
365 | } |
---|
366 | |
---|
367 | return error; |
---|
368 | } |
---|
369 | static GB_ERROR create_ali_intEntry(GBDATA *gb_ali, const char *field, int intval, long write_protection) { |
---|
370 | GB_ERROR error = NULp; |
---|
371 | GBDATA *gb_sub = GB_create(gb_ali, field, GB_INT); |
---|
372 | |
---|
373 | if (!gb_sub) error = GB_await_error(); |
---|
374 | else { |
---|
375 | error = GB_write_int(gb_sub, intval); |
---|
376 | if (!error) error = GB_write_security_delete(gb_sub, 7); |
---|
377 | if (!error) error = GB_write_security_write(gb_sub, write_protection); |
---|
378 | } |
---|
379 | |
---|
380 | if (error) { |
---|
381 | error = GBS_global_string("failed to create alignment subentry '%s'\n" |
---|
382 | "(Reason: %s)", field, error); |
---|
383 | } |
---|
384 | |
---|
385 | return error; |
---|
386 | } |
---|
387 | |
---|
388 | GBDATA *GBT_create_alignment(GBDATA *gb_main, const char *name, long len, long aligned, long security, const char *type, const char *why_created) { |
---|
389 | /* create alignment |
---|
390 | * |
---|
391 | * returns pointer to alignment or |
---|
392 | * NULp (in this case an error has been exported) |
---|
393 | */ |
---|
394 | GB_ERROR error = NULp; |
---|
395 | GBDATA *gb_presets = GBT_get_presets(gb_main); |
---|
396 | GBDATA *result = NULp; |
---|
397 | |
---|
398 | if (!gb_presets) { |
---|
399 | error = GBS_global_string("can't find/create 'presets' (Reason: %s)", GB_await_error()); |
---|
400 | } |
---|
401 | else { |
---|
402 | error = GBT_check_alignment_name(name); |
---|
403 | if (!error && (security<0 || security>6)) { |
---|
404 | error = GBS_global_string("Illegal security value %li (allowed 0..6)", security); |
---|
405 | } |
---|
406 | if (!error) { |
---|
407 | const char *allowed_types = ":dna:rna:ami:usr:"; |
---|
408 | int tlen = strlen(type); |
---|
409 | const char *found = strstr(allowed_types, type); |
---|
410 | if (!found || found == allowed_types || found[-1] != ':' || found[tlen] != ':') { |
---|
411 | error = GBS_global_string("Invalid alignment type '%s'", type); |
---|
412 | } |
---|
413 | } |
---|
414 | |
---|
415 | if (!error) { |
---|
416 | GBDATA *gb_name = GB_find_string(gb_presets, "alignment_name", name, GB_IGNORE_CASE, SEARCH_GRANDCHILD); |
---|
417 | |
---|
418 | if (gb_name) error = GBS_global_string("Alignment '%s' already exists", name); |
---|
419 | else { |
---|
420 | GBDATA *gb_ali = GB_create_container(gb_presets, "alignment"); |
---|
421 | if (!gb_ali) error = GB_await_error(); |
---|
422 | else { |
---|
423 | char *remark = GBS_global_string_copy("%s: alignment created %s", ARB_date_string(), why_created); |
---|
424 | |
---|
425 | error = GB_write_security_delete(gb_ali, 6); |
---|
426 | if (!error) error = create_ali_strEntry(gb_ali, "alignment_name", name, 6); |
---|
427 | if (!error) error = create_ali_intEntry(gb_ali, "alignment_len", len, 0); |
---|
428 | if (!error) error = create_ali_intEntry(gb_ali, "aligned", aligned <= 0 ? 0 : 1, 0); |
---|
429 | if (!error) error = create_ali_intEntry(gb_ali, "alignment_write_security", security, 6); |
---|
430 | if (!error) error = create_ali_strEntry(gb_ali, "alignment_type", type, 0); |
---|
431 | if (!error) error = create_ali_strEntry(gb_ali, "alignment_rem", remark, 0); |
---|
432 | |
---|
433 | free(remark); |
---|
434 | } |
---|
435 | |
---|
436 | if (!error) result = gb_ali; |
---|
437 | } |
---|
438 | } |
---|
439 | } |
---|
440 | |
---|
441 | if (!result) { |
---|
442 | gb_assert(error); |
---|
443 | GB_export_errorf("in GBT_create_alignment: %s", error); |
---|
444 | } |
---|
445 | #if defined(DEBUG) |
---|
446 | else gb_assert(!error); |
---|
447 | #endif // DEBUG |
---|
448 | |
---|
449 | return result; |
---|
450 | } |
---|
451 | |
---|
452 | enum CopyMoveDelMode { COPY, MOVE, DELETE }; |
---|
453 | |
---|
454 | static GB_ERROR gbt_rename_alignment_of_item(GBDATA *gb_item_container, const char *item_name, const char *item_entry_name, |
---|
455 | const char *source, const char *dest, CopyMoveDelMode mode) |
---|
456 | { |
---|
457 | GB_ERROR error = NULp; |
---|
458 | GBDATA *gb_item; |
---|
459 | |
---|
460 | for (gb_item = GB_entry(gb_item_container, item_entry_name); |
---|
461 | gb_item && !error; |
---|
462 | gb_item = GB_nextEntry(gb_item)) |
---|
463 | { |
---|
464 | GBDATA *gb_ali = GB_entry(gb_item, source); |
---|
465 | if (!gb_ali) continue; |
---|
466 | |
---|
467 | if (mode != DELETE) { |
---|
468 | GBDATA *gb_new = GB_entry(gb_item, dest); |
---|
469 | if (gb_new) { |
---|
470 | error = GBS_global_string("Entry '%s' already exists", dest); |
---|
471 | } |
---|
472 | else { |
---|
473 | gb_new = GB_create_container(gb_item, dest); |
---|
474 | if (!gb_new) error = GB_await_error(); |
---|
475 | else error = GB_copy_dropProtectMarksAndTempstate(gb_new, gb_ali); |
---|
476 | } |
---|
477 | } |
---|
478 | if (mode != COPY) error = GB_delete(gb_ali); |
---|
479 | } |
---|
480 | |
---|
481 | if (error && gb_item) { |
---|
482 | UNCOVERED(); |
---|
483 | error = GBS_global_string("%s\n(while renaming alignment for %s '%s')", error, item_name, GBT_get_name_or_description(gb_item)); |
---|
484 | } |
---|
485 | |
---|
486 | return error; |
---|
487 | } |
---|
488 | |
---|
489 | static GB_ERROR copy_move_del_alignment(GBDATA *gbMain, const char *source, const char *dest, CopyMoveDelMode mode) { |
---|
490 | // copies, moves or deletes alignments. |
---|
491 | // affects all data of that alignment in species and SAIs. |
---|
492 | |
---|
493 | gb_assert(!GB_have_error()); // illegal to enter this function when an error is exported! |
---|
494 | |
---|
495 | GB_ERROR error = NULp; |
---|
496 | bool is_case_error = false; |
---|
497 | |
---|
498 | GB_transaction ta(gbMain); |
---|
499 | |
---|
500 | gb_assert(correlated(mode == DELETE, dest == NULp)); // dest shall be NULp for DELETE + !NULp otherwise. |
---|
501 | |
---|
502 | GBDATA *gb_presets = GBT_get_presets(gbMain); |
---|
503 | GBDATA *gb_species_data = gb_presets ? GBT_get_species_data(gbMain) : NULp; |
---|
504 | GBDATA *gb_extended_data = gb_species_data ? GBT_get_SAI_data(gbMain) : NULp; |
---|
505 | |
---|
506 | if (!gb_extended_data) error = GB_await_error(); |
---|
507 | |
---|
508 | // create copy and/or delete old alignment description |
---|
509 | if (!error) { |
---|
510 | GBDATA *gb_old_alignment = GBT_get_alignment(gbMain, source); |
---|
511 | |
---|
512 | if (!gb_old_alignment) { |
---|
513 | error = GB_await_error(); |
---|
514 | } |
---|
515 | else { |
---|
516 | if (mode != DELETE) { |
---|
517 | // copy source -> dest |
---|
518 | GBDATA *gbh = GBT_get_alignment(gbMain, dest); |
---|
519 | if (gbh) { |
---|
520 | error = GBS_global_string("destination alignment '%s' already exists", dest); |
---|
521 | is_case_error = (strcasecmp(source, dest) == 0); // test for case-only difference |
---|
522 | } |
---|
523 | else { |
---|
524 | GB_clear_error(); // from GBT_get_alignment |
---|
525 | error = GBT_check_alignment_name(dest); |
---|
526 | if (!error) { |
---|
527 | GBDATA *gb_new_alignment = GB_create_container(gb_presets, "alignment"); |
---|
528 | error = GB_copy_dropProtectMarksAndTempstate(gb_new_alignment, gb_old_alignment); |
---|
529 | if (!error) error = GBT_write_string(gb_new_alignment, "alignment_name", dest); |
---|
530 | } |
---|
531 | } |
---|
532 | } |
---|
533 | |
---|
534 | if (mode != COPY && !error) { |
---|
535 | // delete source |
---|
536 | error = GB_delete(gb_old_alignment); |
---|
537 | } |
---|
538 | } |
---|
539 | } |
---|
540 | |
---|
541 | // change default alignment |
---|
542 | if (!error && mode == MOVE) { |
---|
543 | error = GBT_write_string(gb_presets, "use", dest); |
---|
544 | } |
---|
545 | |
---|
546 | // copy and/or delete alignment entries in species |
---|
547 | if (!error) { |
---|
548 | error = gbt_rename_alignment_of_item(gb_species_data, "Species", "species", source, dest, mode); |
---|
549 | } |
---|
550 | |
---|
551 | // copy and/or delete alignment entries in SAIs |
---|
552 | if (!error) { |
---|
553 | error = gbt_rename_alignment_of_item(gb_extended_data, "SAI", "extended", source, dest, mode); |
---|
554 | } |
---|
555 | |
---|
556 | if (is_case_error) { |
---|
557 | gb_assert(mode != DELETE); |
---|
558 | if (mode==COPY) { |
---|
559 | error = "Cannot copy alignment if destination name only differs in case."; |
---|
560 | } |
---|
561 | else { |
---|
562 | gb_assert(!GB_have_error()); |
---|
563 | |
---|
564 | // alignments source and dest only differ in case |
---|
565 | char *ali_other = gbt_nonexisting_alignment(gbMain); |
---|
566 | |
---|
567 | gb_assert(mode==MOVE); |
---|
568 | gb_assert(!GB_have_error()); |
---|
569 | |
---|
570 | printf("Renaming alignment '%s' -> '%s' -> '%s' (to avoid case-problem)\n", source, ali_other, dest); |
---|
571 | |
---|
572 | error = copy_move_del_alignment(gbMain, source, ali_other, MOVE); |
---|
573 | if (!error) error = copy_move_del_alignment(gbMain, ali_other, dest, MOVE); |
---|
574 | |
---|
575 | free(ali_other); |
---|
576 | } |
---|
577 | } |
---|
578 | |
---|
579 | error = ta.close(error); |
---|
580 | |
---|
581 | return error; |
---|
582 | } |
---|
583 | |
---|
584 | GB_ERROR GBT_copy_alignment(GBDATA *gbMain, const char *source, const char *dest) { |
---|
585 | return copy_move_del_alignment(gbMain, source, dest, COPY); |
---|
586 | } |
---|
587 | GB_ERROR GBT_rename_alignment(GBDATA *gbMain, const char *source, const char *dest) { |
---|
588 | return copy_move_del_alignment(gbMain, source, dest, MOVE); |
---|
589 | } |
---|
590 | GB_ERROR GBT_delete_alignment(GBDATA *gbMain, const char *source) { |
---|
591 | return copy_move_del_alignment(gbMain, source, NULp, DELETE); |
---|
592 | } |
---|
593 | |
---|
594 | // ----------------------------------------- |
---|
595 | // alignment related item functions |
---|
596 | |
---|
597 | NOT4PERL GBDATA *GBT_add_data(GBDATA *species, const char *ali_name, const char *key, GB_TYPES type) { |
---|
598 | // goes to header: __ATTR__DEPRECATED_TODO("better use GBT_create_sequence_data()") |
---|
599 | |
---|
600 | /* replace this function by GBT_create_sequence_data |
---|
601 | * the same as GB_search(species, 'ali_name/key', GB_CREATE) |
---|
602 | * |
---|
603 | * Note: The behavior is weird, cause it does sth special for GB_STRING (write default content "...") |
---|
604 | * |
---|
605 | * returns create database entry (or NULp; exports an error in this case) |
---|
606 | */ |
---|
607 | |
---|
608 | GB_ERROR error = GB_check_key(ali_name); |
---|
609 | if (error) { |
---|
610 | error = GBS_global_string("Invalid alignment name '%s' (Reason: %s)", ali_name, error); |
---|
611 | } |
---|
612 | else { |
---|
613 | error = GB_check_hkey(key); |
---|
614 | if (error) { |
---|
615 | error = GBS_global_string("Invalid field name '%s' (Reason: %s)", key, error); |
---|
616 | } |
---|
617 | } |
---|
618 | |
---|
619 | GBDATA *gb_data = NULp; |
---|
620 | if (error) { |
---|
621 | GB_export_error(error); |
---|
622 | } |
---|
623 | else { |
---|
624 | GBDATA *gb_gb = GB_entry(species, ali_name); |
---|
625 | if (!gb_gb) gb_gb = GB_create_container(species, ali_name); |
---|
626 | |
---|
627 | if (gb_gb) { |
---|
628 | if (type == GB_STRING) { |
---|
629 | gb_data = GB_search(gb_gb, key, GB_FIND); |
---|
630 | if (!gb_data) { |
---|
631 | gb_data = GB_search(gb_gb, key, GB_STRING); |
---|
632 | GB_write_string(gb_data, "..."); |
---|
633 | } |
---|
634 | } |
---|
635 | else { |
---|
636 | gb_data = GB_search(gb_gb, key, type); |
---|
637 | } |
---|
638 | } |
---|
639 | } |
---|
640 | return gb_data; |
---|
641 | } |
---|
642 | |
---|
643 | NOT4PERL GBDATA *GBT_create_sequence_data(GBDATA *species, const char *ali_name, const char *key, GB_TYPES type, int security_write) { |
---|
644 | GBDATA *gb_data = GBT_add_data(species, ali_name, key, type); |
---|
645 | if (gb_data) { |
---|
646 | GB_ERROR error = GB_write_security_write(gb_data, security_write); |
---|
647 | if (error) { |
---|
648 | GB_export_error(error); |
---|
649 | gb_data = NULp; |
---|
650 | } |
---|
651 | } |
---|
652 | return gb_data; |
---|
653 | } |
---|
654 | |
---|
655 | GBDATA *GBT_gen_accession_number(GBDATA *gb_species, const char *ali_name) { |
---|
656 | GBDATA *gb_acc = GB_entry(gb_species, "acc"); |
---|
657 | if (!gb_acc) { |
---|
658 | GBDATA *gb_data = GBT_find_sequence(gb_species, ali_name); |
---|
659 | if (gb_data) { // found a valid alignment |
---|
660 | GB_CSTR sequence = GB_read_char_pntr(gb_data); |
---|
661 | long id = GBS_checksum(sequence, 1, ".-"); |
---|
662 | const char *acc = GBS_global_string("ARB_%lX", id); |
---|
663 | GB_ERROR error = GBT_write_string(gb_species, "acc", acc); |
---|
664 | |
---|
665 | if (error) GB_export_error(error); |
---|
666 | } |
---|
667 | } |
---|
668 | return gb_acc; |
---|
669 | } |
---|
670 | |
---|
671 | |
---|
672 | int GBT_is_partial(GBDATA *gb_species, int default_value, bool define_if_undef) { |
---|
673 | // checks whether a species has a partial or full sequence |
---|
674 | // |
---|
675 | // Note: partial sequences should not be used for tree calculations |
---|
676 | // |
---|
677 | // returns: 0 if sequence is full |
---|
678 | // 1 if sequence is partial |
---|
679 | // -1 in case of error (which is exported in this case) |
---|
680 | // |
---|
681 | // if the sequence has no 'ARB_partial' entry it returns 'default_value' |
---|
682 | // if 'define_if_undef' is true then create an 'ARB_partial'-entry with the default value |
---|
683 | |
---|
684 | int result = -1; |
---|
685 | GB_ERROR error = NULp; |
---|
686 | GBDATA *gb_partial = GB_entry(gb_species, "ARB_partial"); |
---|
687 | |
---|
688 | if (gb_partial) { |
---|
689 | result = GB_read_int(gb_partial); |
---|
690 | if (result != 0 && result != 1) { |
---|
691 | error = "Illegal value for 'ARB_partial' (only 1 or 0 allowed)"; |
---|
692 | } |
---|
693 | } |
---|
694 | else { |
---|
695 | if (define_if_undef) { |
---|
696 | error = GBT_write_int(gb_species, "ARB_partial", default_value); |
---|
697 | } |
---|
698 | result = default_value; |
---|
699 | } |
---|
700 | |
---|
701 | if (error) { |
---|
702 | GB_export_error(error); |
---|
703 | return -1; |
---|
704 | } |
---|
705 | return result; |
---|
706 | } |
---|
707 | |
---|
708 | GBDATA *GBT_find_sequence(GBDATA *gb_species, const char *aliname) { |
---|
709 | GBDATA *gb_ali = GB_entry(gb_species, aliname); |
---|
710 | return gb_ali ? GB_entry(gb_ali, "data") : NULp; |
---|
711 | } |
---|
712 | |
---|
713 | static char *get_default_alignment(GBDATA *gb_main, bool use_startup_ali) { |
---|
714 | /*! behavior @see GBT_get_default_alignment. |
---|
715 | * @param use_startup_ali if 'true' -> always return the value seen when first called with 'true' + make calls with 'false' always return an error. |
---|
716 | * The intention is to be able to force that an applications sticks with the |
---|
717 | * alignment found at startup and ignores if alignment is changed in arb_ntree. |
---|
718 | */ |
---|
719 | gb_assert(!GB_have_error()); // illegal to enter this function when an error is exported! |
---|
720 | |
---|
721 | static SmartCharPtr startup_ali_name; // set once (when function is first called with 'use_startup_ali'==true) |
---|
722 | |
---|
723 | char *ali_name = NULp; |
---|
724 | if (startup_ali_name.isSet()) { |
---|
725 | ali_name = ARB_strdup(&*startup_ali_name); |
---|
726 | } |
---|
727 | else { |
---|
728 | ali_name = GBT_read_string(gb_main, GB_DEFAULT_ALIGNMENT); |
---|
729 | if (ali_name) { |
---|
730 | if (strcmp(ali_name, NO_ALI_SELECTED) == 0) { |
---|
731 | GB_export_error("No alignment is selected."); |
---|
732 | freenull(ali_name); |
---|
733 | } |
---|
734 | else if (!ARB_strBeginsWith(ali_name, "ali_")) { |
---|
735 | GB_export_errorf("Selected alignment '%s' is not valid!", ali_name); |
---|
736 | freenull(ali_name); |
---|
737 | } |
---|
738 | } |
---|
739 | if (ali_name && use_startup_ali) { |
---|
740 | // first sucessful call with 'use_startup_ali==true' |
---|
741 | startup_ali_name = ARB_strdup(ali_name); // this will remain default until program terminates |
---|
742 | } |
---|
743 | } |
---|
744 | gb_assert(contradicted(ali_name, GB_have_error())); // either result or error! |
---|
745 | return ali_name; |
---|
746 | } |
---|
747 | char *GBT_get_default_alignment(GBDATA *gb_main) { |
---|
748 | /*! @return default alignment |
---|
749 | * returns |
---|
750 | * - the alignment that was selected in ARB alignment admin, when GBT_get_startup_alignment was called first OR |
---|
751 | * - the alignment currently selected in ARB alignment admin OR |
---|
752 | * - NULp if NO (valid) alignment selected. |
---|
753 | * An error is exported in that case! |
---|
754 | */ |
---|
755 | return get_default_alignment(gb_main, false); |
---|
756 | } |
---|
757 | char *GBT_get_startup_alignment(GBDATA *gb_main) { |
---|
758 | /*! same interface as GBT_get_default_alignment. |
---|
759 | * When this was called once w/o error, calls to GBT_get_default_alignment always return |
---|
760 | * the alignment which was default then. |
---|
761 | */ |
---|
762 | return get_default_alignment(gb_main, true); |
---|
763 | } |
---|
764 | |
---|
765 | GB_ERROR GBT_set_default_alignment(GBDATA *gb_main, const char *alignment_name) { |
---|
766 | return GBT_write_string(gb_main, GB_DEFAULT_ALIGNMENT, alignment_name); |
---|
767 | } |
---|
768 | GB_ERROR GBT_set_startup_alignment(GBDATA *gb_main, const char *alignment_name) { |
---|
769 | // Similar to GBT_set_default_alignment, but does not change database default alignment. |
---|
770 | // Instead it changes the application-local alignment which is derived from default |
---|
771 | // alignment and "freezed" on first request (by using GBT_get_startup_alignment). |
---|
772 | // |
---|
773 | // Warning: GBT_set_startup_alignment fails if GBT_get_startup_alignment was already called! |
---|
774 | |
---|
775 | GB_ERROR error = NULp; |
---|
776 | char *default_ali = GBT_get_default_alignment(gb_main); |
---|
777 | |
---|
778 | bool freeze_and_reset = false; |
---|
779 | if (!default_ali) { |
---|
780 | GB_clear_error(); |
---|
781 | default_ali = ARB_strdup(NO_ALI_SELECTED); |
---|
782 | freeze_and_reset = true; |
---|
783 | } |
---|
784 | else { |
---|
785 | freeze_and_reset = strcmp(default_ali, alignment_name) != 0; |
---|
786 | } |
---|
787 | if (freeze_and_reset) { |
---|
788 | error = GBT_set_default_alignment(gb_main, alignment_name); |
---|
789 | if (!error) { |
---|
790 | char *startup_ali = GBT_get_startup_alignment(gb_main); |
---|
791 | if (!startup_ali) error = GB_await_error(); // most unlikely |
---|
792 | else { |
---|
793 | if (strcmp(startup_ali, alignment_name) != 0) { |
---|
794 | error = GBS_global_string("Cannot freeze alignment to '%s' (already was frozen to '%s' before)", alignment_name, startup_ali); |
---|
795 | } |
---|
796 | // fine, the correct alignment is freezed! |
---|
797 | free(startup_ali); |
---|
798 | } |
---|
799 | } |
---|
800 | // undo (tmp) change to default alignment: |
---|
801 | GB_ERROR minerr = GBT_set_default_alignment(gb_main, default_ali); |
---|
802 | error = error ? error : minerr; |
---|
803 | } |
---|
804 | free(default_ali); |
---|
805 | return error; |
---|
806 | } |
---|
807 | |
---|
808 | GBDATA *GBT_get_alignment(GBDATA *gb_main, const char *aliname) { |
---|
809 | /*! @return global alignment container for alignment 'aliname' or |
---|
810 | * NULp if alignment not found (error exported in that case) |
---|
811 | */ |
---|
812 | |
---|
813 | GBDATA *gb_ali = NULp; |
---|
814 | if (!aliname || strcmp(aliname, NO_ALI_SELECTED) == 0) { |
---|
815 | GB_export_error("no alignment selected"); |
---|
816 | } |
---|
817 | else { |
---|
818 | GBDATA *gb_presets = GBT_get_presets(gb_main); |
---|
819 | GBDATA *gb_alignment_name = GB_find_string(gb_presets, "alignment_name", aliname, GB_IGNORE_CASE, SEARCH_GRANDCHILD); |
---|
820 | |
---|
821 | if (!gb_alignment_name) { |
---|
822 | GB_export_errorf("alignment '%s' not found", aliname); |
---|
823 | } |
---|
824 | else { |
---|
825 | gb_ali = GB_get_father(gb_alignment_name); |
---|
826 | } |
---|
827 | } |
---|
828 | gb_assert(contradicted(gb_ali, GB_have_error())); // either result or error! |
---|
829 | return gb_ali; |
---|
830 | } |
---|
831 | |
---|
832 | // @@@ recode and change result type to long* ? |
---|
833 | long GBT_get_alignment_len(GBDATA *gb_main, const char *aliname) { |
---|
834 | /*! @return length (>0) of alignment 'aliname' or |
---|
835 | * -1 if alignment not found (error exported in that case) |
---|
836 | * 0 if alignment length is zero (=invalid; error exported in that case) |
---|
837 | */ |
---|
838 | GBDATA *gb_alignment = GBT_get_alignment(gb_main, aliname); |
---|
839 | long len = gb_alignment ? *GBT_read_int(gb_alignment, "alignment_len") : -1; |
---|
840 | |
---|
841 | if (len == 0) GB_export_errorf("alignment '%s' has no data", aliname); |
---|
842 | |
---|
843 | gb_assert(contradicted(len>0, GB_have_error())); // either valid result or error! |
---|
844 | |
---|
845 | return len; |
---|
846 | } |
---|
847 | |
---|
848 | GB_ERROR GBT_set_alignment_len(GBDATA *gb_main, const char *aliname, long new_len) { |
---|
849 | GB_ERROR error = NULp; |
---|
850 | GBDATA *gb_alignment = GBT_get_alignment(gb_main, aliname); |
---|
851 | |
---|
852 | if (!gb_alignment) { |
---|
853 | error = GB_await_error(); |
---|
854 | } |
---|
855 | else { |
---|
856 | GB_topSecurityLevel unsecured(gb_main); |
---|
857 | error = GBT_write_int(gb_alignment, "alignment_len", new_len); // write new len |
---|
858 | if (!error) error = GBT_write_int(gb_alignment, "aligned", 0); // mark as unaligned |
---|
859 | } |
---|
860 | return error; |
---|
861 | } |
---|
862 | |
---|
863 | char *GBT_get_alignment_type_string(GBDATA *gb_main, const char *aliname) { |
---|
864 | /*! @return type-string of alignment 'aliname' or |
---|
865 | * NULp if alignment not found (error exported in that case) |
---|
866 | */ |
---|
867 | GBDATA *gb_alignment = GBT_get_alignment(gb_main, aliname); |
---|
868 | char *result = gb_alignment ? GBT_read_string(gb_alignment, "alignment_type") : NULp; |
---|
869 | |
---|
870 | if (!result && !GB_have_error()) { |
---|
871 | GB_export_errorf("Alignment '%s' has no alignment_type defined", aliname); |
---|
872 | } |
---|
873 | |
---|
874 | gb_assert(contradicted(result, GB_have_error())); // either result or error! |
---|
875 | return result; |
---|
876 | } |
---|
877 | |
---|
878 | GB_alignment_type GBT_get_alignment_type(GBDATA *gb_main, const char *aliname) { |
---|
879 | /*! @return type of alignment as enum type (GB_alignment_type). |
---|
880 | * GB_AT_UNKNOWN if alignment not found (error exported in that case) |
---|
881 | */ |
---|
882 | char *ali_type = GBT_get_alignment_type_string(gb_main, aliname); |
---|
883 | GB_alignment_type at = GB_AT_UNKNOWN; |
---|
884 | |
---|
885 | if (ali_type) { |
---|
886 | switch (ali_type[0]) { |
---|
887 | case 'r': if (strcmp(ali_type, "rna")==0) at = GB_AT_RNA; break; |
---|
888 | case 'd': if (strcmp(ali_type, "dna")==0) at = GB_AT_DNA; break; |
---|
889 | case 'a': if (strcmp(ali_type, "ami")==0) at = GB_AT_AA; break; |
---|
890 | case 'p': if (strcmp(ali_type, "pro")==0) at = GB_AT_AA; break; |
---|
891 | default: gb_assert(0); break; |
---|
892 | } |
---|
893 | free(ali_type); |
---|
894 | } |
---|
895 | return at; |
---|
896 | } |
---|
897 | |
---|
898 | bool GBT_is_alignment_protein(GBDATA *gb_main, const char *alignment_name) { |
---|
899 | GB_alignment_type ali_type = GBT_get_alignment_type(gb_main, alignment_name); |
---|
900 | gb_assert(ali_type != GB_AT_UNKNOWN); |
---|
901 | return ali_type == GB_AT_AA; |
---|
902 | } |
---|
903 | |
---|
904 | // ----------------------- |
---|
905 | // gene sequence |
---|
906 | |
---|
907 | static const char *gb_cache_genome(GBDATA *gb_genome) { |
---|
908 | static GBDATA *gb_last_genome = NULp; |
---|
909 | static char *last_genome = NULp; |
---|
910 | |
---|
911 | if (gb_genome != gb_last_genome) { |
---|
912 | free(last_genome); |
---|
913 | |
---|
914 | last_genome = GB_read_string(gb_genome); |
---|
915 | gb_last_genome = gb_genome; |
---|
916 | } |
---|
917 | |
---|
918 | return last_genome; |
---|
919 | } |
---|
920 | |
---|
921 | struct gene_part_pos { |
---|
922 | int parts; // initialized for parts |
---|
923 | unsigned char *certain; // contains parts "=" chars |
---|
924 | char offset[256]; |
---|
925 | }; |
---|
926 | |
---|
927 | static gene_part_pos *gpp = NULp; |
---|
928 | |
---|
929 | static void init_gpp(int parts) { |
---|
930 | if (!gpp) { |
---|
931 | int i; |
---|
932 | ARB_alloc(gpp, 1); |
---|
933 | gpp->certain = NULp; |
---|
934 | |
---|
935 | for (i = 0; i<256; ++i) gpp->offset[i] = 0; |
---|
936 | |
---|
937 | gpp->offset[(int)'+'] = 1; |
---|
938 | gpp->offset[(int)'-'] = -1; |
---|
939 | } |
---|
940 | else { |
---|
941 | if (parts>gpp->parts) freenull(gpp->certain); |
---|
942 | } |
---|
943 | |
---|
944 | if (!gpp->certain) { |
---|
945 | int forParts = parts+10; |
---|
946 | ARB_alloc(gpp->certain, forParts+1); |
---|
947 | memset(gpp->certain, '=', forParts); |
---|
948 | gpp->certain[forParts] = 0; |
---|
949 | gpp->parts = forParts; |
---|
950 | } |
---|
951 | } |
---|
952 | |
---|
953 | static void getPartPositions(const GEN_position *pos, int part, size_t *startPos, size_t *stopPos) { |
---|
954 | // returns 'startPos' and 'stopPos' of one part of a gene |
---|
955 | gb_assert(part<pos->parts); |
---|
956 | *startPos = pos->start_pos[part]+gpp->offset[(pos->start_uncertain ? pos->start_uncertain : gpp->certain)[part]]; |
---|
957 | *stopPos = pos->stop_pos [part]+gpp->offset[(pos->stop_uncertain ? pos->stop_uncertain : gpp->certain)[part]]; |
---|
958 | } |
---|
959 | |
---|
960 | NOT4PERL char *GBT_read_gene_sequence_and_length(GBDATA *gb_gene, bool use_revComplement, char partSeparator, size_t *gene_length) { |
---|
961 | // return the sequence data of a gene |
---|
962 | // |
---|
963 | // if use_revComplement is true -> use data from complementary strand (if complement is set for gene) |
---|
964 | // otherwise -> use data from primary strand (sort+merge parts by position) |
---|
965 | // |
---|
966 | // if partSeparator not is 0 -> insert partSeparator between single (non-merged) parts |
---|
967 | // |
---|
968 | // returns sequence as result (and length of sequence if 'gene_length' points to something) |
---|
969 | // |
---|
970 | // if 'pos_certain' contains '+', start behind position (or end at position) |
---|
971 | // '-', start at position (or end before position) |
---|
972 | // |
---|
973 | // For zero-length genes (e.g. "711^712") this function returns an empty string. |
---|
974 | |
---|
975 | GB_ERROR error = NULp; |
---|
976 | char *result = NULp; |
---|
977 | GBDATA *gb_species = GB_get_grandfather(gb_gene); |
---|
978 | GEN_position *pos = GEN_read_position(gb_gene); |
---|
979 | |
---|
980 | if (!pos) error = GB_await_error(); |
---|
981 | else { |
---|
982 | GBDATA *gb_seq = GBT_find_sequence(gb_species, "ali_genom"); |
---|
983 | unsigned long seq_length = GB_read_count(gb_seq); |
---|
984 | int p; |
---|
985 | int parts = pos->parts; |
---|
986 | int resultlen = 0; |
---|
987 | int separatorSize = partSeparator ? 1 : 0; |
---|
988 | |
---|
989 | init_gpp(parts); |
---|
990 | |
---|
991 | // test positions and calculate overall result length |
---|
992 | for (p = 0; p<parts && !error; p++) { |
---|
993 | size_t start; |
---|
994 | size_t stop; |
---|
995 | getPartPositions(pos, p, &start, &stop); |
---|
996 | |
---|
997 | if (start<1 || start>(stop+1) || stop > seq_length) { // do not reject zero-length genes (start == stop+1) |
---|
998 | error = GBS_global_string("Illegal gene position(s): start=%zu, end=%zu, seq.length=%li", |
---|
999 | start, stop, seq_length); |
---|
1000 | } |
---|
1001 | else { |
---|
1002 | resultlen += stop-start+1; |
---|
1003 | } |
---|
1004 | } |
---|
1005 | |
---|
1006 | if (separatorSize) resultlen += (parts-1)*separatorSize; |
---|
1007 | |
---|
1008 | if (!error) { |
---|
1009 | char T_or_U = 0; |
---|
1010 | if (use_revComplement) { |
---|
1011 | error = GBT_determine_T_or_U(GB_AT_DNA, &T_or_U, "reverse-complement"); |
---|
1012 | } |
---|
1013 | else if (parts>1) { |
---|
1014 | GEN_sortAndMergeLocationParts(pos); |
---|
1015 | parts = pos->parts; // may have changed |
---|
1016 | } |
---|
1017 | |
---|
1018 | if (!error) { |
---|
1019 | const char *seq_data = gb_cache_genome(gb_seq); |
---|
1020 | char *resultpos; |
---|
1021 | |
---|
1022 | ARB_alloc(result, resultlen+1); |
---|
1023 | resultpos = result; |
---|
1024 | |
---|
1025 | if (gene_length) *gene_length = resultlen; |
---|
1026 | |
---|
1027 | for (p = 0; p<parts; ++p) { |
---|
1028 | size_t start; |
---|
1029 | size_t stop; |
---|
1030 | getPartPositions(pos, p, &start, &stop); |
---|
1031 | |
---|
1032 | int len = stop-start+1; |
---|
1033 | |
---|
1034 | if (separatorSize && p>0) *resultpos++ = partSeparator; |
---|
1035 | |
---|
1036 | memcpy(resultpos, seq_data+start-1, len); |
---|
1037 | if (T_or_U && pos->complement[p]) { |
---|
1038 | GBT_reverseComplementNucSequence(resultpos, len, T_or_U); |
---|
1039 | } |
---|
1040 | resultpos += len; |
---|
1041 | } |
---|
1042 | |
---|
1043 | resultpos[0] = 0; |
---|
1044 | } |
---|
1045 | } |
---|
1046 | GEN_free_position(pos); |
---|
1047 | } |
---|
1048 | |
---|
1049 | gb_assert(contradicted(result, error)); // either result or error! |
---|
1050 | |
---|
1051 | if (error) { |
---|
1052 | char *id = GEN_global_gene_identifier(gb_gene, gb_species); |
---|
1053 | error = GB_export_errorf("Can't read sequence of '%s' (Reason: %s)", id, error); |
---|
1054 | free(id); |
---|
1055 | free(result); |
---|
1056 | result = NULp; |
---|
1057 | } |
---|
1058 | |
---|
1059 | return result; |
---|
1060 | } |
---|
1061 | |
---|
1062 | char *GBT_read_gene_sequence(GBDATA *gb_gene, bool use_revComplement, char partSeparator) { |
---|
1063 | return GBT_read_gene_sequence_and_length(gb_gene, use_revComplement, partSeparator, NULp); |
---|
1064 | } |
---|
1065 | |
---|
1066 | // -------------------------------------------------------------------------------- |
---|
1067 | |
---|
1068 | #ifdef UNIT_TESTS |
---|
1069 | #include <test_unit.h> |
---|
1070 | |
---|
1071 | #define TEST_EXPECT_EXISTING_ALIGNMENTS(expected) do{ \ |
---|
1072 | names.erase(); \ |
---|
1073 | GBT_get_alignment_names(names, gb_main); \ |
---|
1074 | TEST_EXPECT_STRARRAY_CONTAINS(names, '*', expected); \ |
---|
1075 | }while(0) |
---|
1076 | |
---|
1077 | |
---|
1078 | #define TEST_EXPECT_EXISTING_ALIGNMENTS__BROKEN(want,got) do{ \ |
---|
1079 | names.erase(); \ |
---|
1080 | GBT_get_alignment_names(names, gb_main); \ |
---|
1081 | TEST_EXPECT_STRARRAY_CONTAINS__BROKEN(names, '*', want,got); \ |
---|
1082 | }while(0) |
---|
1083 | |
---|
1084 | |
---|
1085 | |
---|
1086 | #define TEST_EXPECT_DEFAULT_ALIGNMENT(expected) \ |
---|
1087 | TEST_EXPECT_EQUAL_STRINGCOPY__NOERROREXPORTED(GBT_get_default_alignment(gb_main), expected); |
---|
1088 | |
---|
1089 | |
---|
1090 | #define TEST_EXPECT_ALIGNMENT_STATE(expected_default,expected_existing) do{ \ |
---|
1091 | GB_initial_transaction ta(gb_main); \ |
---|
1092 | TEST_EXPECT_DEFAULT_ALIGNMENT(expected_default); \ |
---|
1093 | TEST_EXPECT_EXISTING_ALIGNMENTS(expected_existing); \ |
---|
1094 | }while(0) |
---|
1095 | |
---|
1096 | void TEST_alignment() { |
---|
1097 | GB_shell shell; |
---|
1098 | GBDATA *gb_main = GB_open("TEST_prot.arb", "r"); // ../UNIT_TESTER/run/TEST_prot.arb |
---|
1099 | |
---|
1100 | ConstStrArray names; |
---|
1101 | { |
---|
1102 | GB_transaction ta(gb_main); |
---|
1103 | |
---|
1104 | TEST_EXPECT_EQUAL(GBT_count_alignments(gb_main), 2); |
---|
1105 | |
---|
1106 | TEST_EXPECT_DEFAULT_ALIGNMENT("ali_tuf_dna"); |
---|
1107 | TEST_EXPECT_EXISTING_ALIGNMENTS("ali_tuf_pro*ali_tuf_dna"); |
---|
1108 | |
---|
1109 | for (int i = 0; names[i]; ++i) { |
---|
1110 | long len = GBT_get_alignment_len(gb_main, names[i]); |
---|
1111 | TEST_EXPECT_EQUAL(len, i ? 1462 : 487); |
---|
1112 | |
---|
1113 | char *type_name = GBT_get_alignment_type_string(gb_main, names[i]); |
---|
1114 | TEST_EXPECT_EQUAL(type_name, i ? "dna" : "ami"); |
---|
1115 | free(type_name); |
---|
1116 | |
---|
1117 | GB_alignment_type type = GBT_get_alignment_type(gb_main, names[i]); |
---|
1118 | TEST_EXPECT_EQUAL(type, i ? GB_AT_DNA : GB_AT_AA); |
---|
1119 | TEST_EXPECT_EQUAL(GBT_is_alignment_protein(gb_main, names[i]), !i); |
---|
1120 | } |
---|
1121 | |
---|
1122 | // test functions called with aliname == NULp |
---|
1123 | TEST_EXPECT_NORESULT__ERROREXPORTED_CONTAINS(GBT_get_alignment(gb_main, NULp), "no alignment"); |
---|
1124 | TEST_EXPECT_EQUAL(GBT_get_alignment_len(gb_main, NULp), -1); |
---|
1125 | TEST_EXPECT_CONTAINS(GB_await_error(), "no alignment"); |
---|
1126 | TEST_EXPECT_NORESULT__ERROREXPORTED_CONTAINS(GBT_get_alignment_type_string(gb_main, NULp), "no alignment"); |
---|
1127 | |
---|
1128 | // test functions called with aliname == NO_ALI_SELECTED |
---|
1129 | TEST_EXPECT_NORESULT__ERROREXPORTED_CONTAINS(GBT_get_alignment(gb_main, NO_ALI_SELECTED), "no alignment"); |
---|
1130 | TEST_EXPECT_EQUAL(GBT_get_alignment_len(gb_main, NO_ALI_SELECTED), -1); |
---|
1131 | TEST_EXPECT_CONTAINS(GB_await_error(), "no alignment"); |
---|
1132 | TEST_EXPECT_NORESULT__ERROREXPORTED_CONTAINS(GBT_get_alignment_type_string(gb_main, NO_ALI_SELECTED), "no alignment"); |
---|
1133 | } |
---|
1134 | |
---|
1135 | // test copy, move(rename), delete alignments |
---|
1136 | TEST_EXPECT_ALIGNMENT_STATE("ali_tuf_dna", "ali_tuf_pro*ali_tuf_dna"); |
---|
1137 | |
---|
1138 | // copy ali + test names: |
---|
1139 | { |
---|
1140 | GB_transaction ta(gb_main); |
---|
1141 | |
---|
1142 | TEST_EXPECT_NO_ERROR(GBT_copy_alignment(gb_main, "ali_tuf_pro", "ali_copied_pro")); |
---|
1143 | TEST_EXPECT_NO_ERROR(GBT_copy_alignment(gb_main, "ali_tuf_dna", "ali_copied_dna")); |
---|
1144 | } |
---|
1145 | TEST_EXPECT_ALIGNMENT_STATE("ali_tuf_dna", "ali_tuf_pro*ali_tuf_dna*ali_copied_pro*ali_copied_dna"); |
---|
1146 | |
---|
1147 | // rename ali + test names: |
---|
1148 | { |
---|
1149 | GB_transaction ta(gb_main); |
---|
1150 | TEST_EXPECT_ERROR_CONTAINS(GBT_rename_alignment(gb_main, "ali_tuf_pro", "ali_moved_pro"), "Security error: deleting entry 'alignment' not permitted"); |
---|
1151 | } |
---|
1152 | TEST_EXPECT_ALIGNMENT_STATE("ali_tuf_dna", "ali_tuf_pro*ali_tuf_dna*ali_copied_pro*ali_copied_dna"); |
---|
1153 | |
---|
1154 | { |
---|
1155 | GB_transaction ta(gb_main); |
---|
1156 | GB_securityLevel raise(gb_main, 6); |
---|
1157 | TEST_EXPECT_NO_ERROR(GBT_rename_alignment(gb_main, "ali_tuf_pro", "ali_moved_pro")); |
---|
1158 | TEST_EXPECT_NO_ERROR(GBT_rename_alignment(gb_main, "ali_tuf_dna", "ali_moved_dna")); |
---|
1159 | } |
---|
1160 | TEST_EXPECT_ALIGNMENT_STATE("ali_moved_dna", "ali_copied_pro*ali_copied_dna*ali_moved_pro*ali_moved_dna"); |
---|
1161 | |
---|
1162 | // delete ali + test names: |
---|
1163 | { |
---|
1164 | GB_transaction ta(gb_main); |
---|
1165 | TEST_EXPECT_NO_ERROR(GBT_delete_alignment(gb_main, "ali_copied_pro")); |
---|
1166 | TEST_EXPECT_NO_ERROR(GBT_delete_alignment(gb_main, "ali_moved_dna")); // @@@ the rename-test (above) lowered the security of 'ali_tuf_dna' (from 6 to 0?). unwanted behavior! |
---|
1167 | } |
---|
1168 | TEST_EXPECT_ALIGNMENT_STATE("ali_moved_dna", // @@@ unwanted behavior (deleted alignment is selected as default) |
---|
1169 | "ali_copied_dna*ali_moved_pro"); |
---|
1170 | |
---|
1171 | // trigger error cases: |
---|
1172 | { |
---|
1173 | GB_transaction ta(gb_main); |
---|
1174 | TEST_EXPECT_ERROR_CONTAINS(GBT_rename_alignment(gb_main, "ali_copied_dna", "ali_moved_pro"), "destination alignment 'ali_moved_pro' already exists"); |
---|
1175 | TEST_EXPECT_ERROR_CONTAINS(GBT_copy_alignment(gb_main, "ali_copied_dna", "ali_copied_DNA"), "Cannot copy alignment if destination name only differs in case."); |
---|
1176 | } |
---|
1177 | |
---|
1178 | // test case-only rename |
---|
1179 | { |
---|
1180 | GB_transaction ta(gb_main); |
---|
1181 | TEST_EXPECT_NO_ERROR(GBT_rename_alignment(gb_main, "ali_copied_dna", "ali_copied_DNA")); |
---|
1182 | } |
---|
1183 | TEST_EXPECT_ALIGNMENT_STATE("ali_copied_DNA", "ali_moved_pro*ali_copied_DNA"); |
---|
1184 | |
---|
1185 | GB_close(gb_main); |
---|
1186 | } |
---|
1187 | TEST_PUBLISH(TEST_alignment); |
---|
1188 | |
---|
1189 | #endif // UNIT_TESTS |
---|