source: tags/arb-6.0.3/ARBDB/adali.cxx

Last change on this file was 12303, checked in by westram, 10 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 34.9 KB
Line 
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#include <arb_strarray.h>
14
15#include "gb_local.h"
16
17static long check_for_species_without_data(const char *species_name, long value, void *counterPtr) {
18    if (value == 1) {
19        long cnt = *((long*)counterPtr);
20        if (cnt<40) {
21            GB_warningf("Species '%s' has no data in any alignment", species_name);
22        }
23        *((long*)counterPtr) = cnt+1;
24    }
25    return value; // new hash value
26}
27
28GBDATA *GBT_get_presets(GBDATA *gb_main) {
29    return GBT_find_or_create(gb_main, "presets", 7);
30}
31
32int GBT_count_alignments(GBDATA *gb_main) {
33    int     count      = 0;
34    GBDATA *gb_presets = GBT_get_presets(gb_main);
35    for (GBDATA *gb_ali = GB_entry(gb_presets, "alignment");
36         gb_ali;
37         gb_ali = GB_nextEntry(gb_ali))
38    {
39        ++count;
40    }
41    return count;
42}
43
44static GB_ERROR GBT_check_alignment(GBDATA *gb_main, GBDATA *preset_alignment, GB_HASH *species_name_hash) {
45    /* check
46     * - whether alignment has correct size and
47     * - whether all data is present.
48     *
49     * Sets the security deletes and writes.
50     *
51     * If 'species_name_hash' is not NULL,
52     * - it initially has to contain value == 1 for each existing species.
53     * - afterwards it will contain value  == 2 for each species where an alignment has been found.
54     */
55
56    GBDATA *gb_species_data  = GBT_get_species_data(gb_main);
57    GBDATA *gb_extended_data = GBT_get_SAI_data(gb_main);
58
59    GB_ERROR  error      = 0;
60    char     *ali_name   = GBT_read_string(preset_alignment, "alignment_name");
61    if (!ali_name) error = "Alignment w/o 'alignment_name'";
62
63    if (!error) {
64        long    security_write = -1;
65        long    stored_ali_len = -1;
66        long    found_ali_len  = -1;
67        long    aligned        = 1;
68        GBDATA *gb_ali_len     = 0;
69
70        {
71            GBDATA *gb_ali_wsec = GB_entry(preset_alignment, "alignment_write_security");
72            if (!gb_ali_wsec) {
73                error = "has no 'alignment_write_security' entry";
74            }
75            else {
76                security_write = GB_read_int(gb_ali_wsec);
77            }
78        }
79
80
81        if (!error) {
82            gb_ali_len = GB_entry(preset_alignment, "alignment_len");
83            if (!gb_ali_len) {
84                error = "has no 'alignment_len' entry";
85            }
86            else {
87                stored_ali_len = GB_read_int(gb_ali_len);
88            }
89        }
90
91        if (!error) {
92            GBDATA *gb_species;
93            for (gb_species = GBT_first_species_rel_species_data(gb_species_data);
94                 gb_species && !error;
95                 gb_species = GBT_next_species(gb_species))
96            {
97                GBDATA     *gb_name        = GB_entry(gb_species, "name");
98                const char *name           = 0;
99                int         alignment_seen = 0;
100
101                if (!gb_name) {
102                    // fatal: name is missing -> create a unique name
103                    char *unique = GBT_create_unique_species_name(gb_main, "autoname.");
104                    error        = GBT_write_string(gb_species, "name", unique);
105
106                    if (!error) {
107                        gb_name = GB_entry(gb_species, "name");
108                        GBS_write_hash(species_name_hash, unique, 1); // not seen before
109                        GB_warningf("Seen unnamed species (gave name '%s')", unique);
110                    }
111                    free(unique);
112                }
113
114                if (!error) {
115                    name = GB_read_char_pntr(gb_name);
116                    if (species_name_hash) {
117                        int seen = GBS_read_hash(species_name_hash, name);
118
119                        gb_assert(seen != 0); // species_name_hash not initialized correctly
120                        if (seen == 2) alignment_seen = 1; // already seen an alignment
121                    }
122                }
123
124                if (!error) {
125                    GB_push_my_security(gb_name);
126
127                    error             = GB_write_security_delete(gb_name, 7);
128                    if (!error) error = GB_write_security_write(gb_name, 6);
129
130                    if (!error) {
131                        GBDATA *gb_ali = GB_entry(gb_species, ali_name);
132                        if (gb_ali) {
133                            GBDATA *gb_data = GB_entry(gb_ali, "data");
134                            if (!gb_data) {
135                                error = GBT_write_string(gb_ali, "data", "Error: entry 'data' was missing and therefore was filled with this text.");
136                                GB_warningf("No '%s/data' entry for species '%s' (has been filled with dummy data)", ali_name, name);
137                            }
138                            else {
139                                if (GB_read_type(gb_data) != GB_STRING) {
140                                    GB_delete(gb_data);
141                                    error = GBS_global_string("'%s/data' of species '%s' had wrong DB-type (%s) and has been deleted!",
142                                                              ali_name, name, GB_read_key_pntr(gb_data));
143                                }
144                                else {
145                                    long data_len = GB_read_string_count(gb_data);
146                                    if (found_ali_len != data_len) {
147                                        if (found_ali_len>0)        aligned       = 0;
148                                        if (found_ali_len<data_len) found_ali_len = data_len;
149                                    }
150
151                                    error = GB_write_security_delete(gb_data, 7);
152
153                                    if (!alignment_seen && species_name_hash) { // mark as seen
154                                        GBS_write_hash(species_name_hash, name, 2); // 2 means "species has data in at least 1 alignment"
155                                    }
156                                }
157                            }
158                        }
159                    }
160
161                    if (!error) error = GB_write_security_delete(gb_species, security_write);
162
163                    GB_pop_my_security(gb_name);
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
218GB_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             = 0;
225    GBDATA   *gb_sd             = GBT_get_species_data(Main);
226    GBDATA   *gb_presets        = GBT_get_presets(Main);
227    GB_HASH  *species_name_hash = 0;
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        species_name_hash  = GBS_create_hash(GBT_get_species_count(Main), GB_IGNORE_CASE);
257
258        if (!error) {
259            for (GBDATA *gb_species = GBT_first_species_rel_species_data(gb_sd);
260                 gb_species;
261                 gb_species = GBT_next_species(gb_species))
262            {
263                const char *name = GBT_read_name(gb_species);
264
265                if (GBS_read_hash(species_name_hash, name)) duplicates++;
266                GBS_incr_hash(species_name_hash, name);
267            }
268        }
269
270        if (duplicates) {
271            error = GBS_global_string("Database is corrupted:\n"
272                                      "Found %li duplicated species with identical names!\n"
273                                      "Fix the problem using\n"
274                                      "   'Search For Equal Fields and Mark Duplicates'\n"
275                                      "in ARB_NTREE search tool, save DB and restart ARB."
276                                      , duplicates);
277        }
278    }
279
280    if (!error) {
281        for (GBDATA *gb_ali = GB_entry(gb_presets, "alignment");
282             gb_ali && !error;
283             gb_ali = GB_nextEntry(gb_ali))
284        {
285            error = GBT_check_alignment(Main, gb_ali, species_name_hash);
286        }
287    }
288
289    if (species_name_hash) {
290        if (!error) {
291            long counter = 0;
292            GBS_hash_do_loop(species_name_hash, check_for_species_without_data, &counter);
293            if (counter>0) {
294                GB_warningf("Found %li species without alignment data (only some were listed)", counter);
295            }
296        }
297
298        GBS_free_hash(species_name_hash);
299    }
300
301    return error;
302}
303
304void GBT_get_alignment_names(ConstStrArray& names, GBDATA *gbd) {
305    /* Get names of existing alignments from database.
306     *
307     * Returns: array of strings, the last element is NULL
308     * (Note: use GBT_free_names() to free result)
309     */
310
311    GBDATA *presets = GBT_get_presets(gbd);
312    for (GBDATA *ali = GB_entry(presets, "alignment"); ali; ali = GB_nextEntry(ali)) {
313        GBDATA *name = GB_entry(ali, "alignment_name");
314        names.put(name ? GB_read_char_pntr(name) : "<unnamed alignment>");
315    }
316}
317
318static char *gbt_nonexisting_alignment(GBDATA *gbMain) {
319    char  *ali_other = 0;
320    int    counter;
321
322    for (counter = 1; !ali_other; ++counter) {
323        ali_other = GBS_global_string_copy("ali_x%i", counter);
324        if (GBT_get_alignment(gbMain, ali_other) != 0) freenull(ali_other); // exists -> continue
325    }
326
327    return ali_other;
328}
329
330GB_ERROR GBT_check_alignment_name(const char *alignment_name)
331{
332    GB_ERROR error;
333    if ((error = GB_check_key(alignment_name))) return error;
334    if (strncmp(alignment_name, "ali_", 4)) {
335        return GB_export_errorf("your alignment_name '%s' must start with 'ali_'",
336                                alignment_name);
337    }
338    return 0;
339}
340
341static GB_ERROR create_ali_strEntry(GBDATA *gb_ali, const char *field, const char *strval, long write_protection) {
342    GB_ERROR  error  = 0;
343    GBDATA   *gb_sub = GB_create(gb_ali, field, GB_STRING);
344
345    if (!gb_sub) error = GB_await_error();
346    else {
347        error             = GB_write_string(gb_sub, strval);
348        if (!error) error = GB_write_security_delete(gb_sub, 7);
349        if (!error) error = GB_write_security_write(gb_sub, write_protection);
350    }
351
352    if (error) {
353        error = GBS_global_string("failed to create alignment subentry '%s'\n"
354                                  "(Reason: %s)", field, error);
355    }
356
357    return error;
358}
359static GB_ERROR create_ali_intEntry(GBDATA *gb_ali, const char *field, int intval, long write_protection) {
360    GB_ERROR  error  = 0;
361    GBDATA   *gb_sub = GB_create(gb_ali, field, GB_INT);
362
363    if (!gb_sub) error = GB_await_error();
364    else {
365        error             = GB_write_int(gb_sub, intval);
366        if (!error) error = GB_write_security_delete(gb_sub, 7);
367        if (!error) error = GB_write_security_write(gb_sub, write_protection);
368    }
369
370    if (error) {
371        error = GBS_global_string("failed to create alignment subentry '%s'\n"
372                                  "(Reason: %s)", field, error);
373    }
374
375    return error;
376}
377
378GBDATA *GBT_create_alignment(GBDATA *gbd, const char *name, long len, long aligned, long security, const char *type) {
379    /* create alignment
380     *
381     * returns pointer to alignment or
382     * NULL (in this case an error has been exported)
383     */
384    GB_ERROR  error      = NULL;
385    GBDATA   *gb_presets = GBT_get_presets(gbd);
386    GBDATA   *result     = NULL;
387
388    if (!gb_presets) {
389        error = GBS_global_string("can't find/create 'presets' (Reason: %s)", GB_await_error());
390    }
391    else {
392        error = GBT_check_alignment_name(name);
393        if (!error && (security<0 || security>6)) {
394            error = GBS_global_string("Illegal security value %li (allowed 0..6)", security);
395        }
396        if (!error) {
397            const char *allowed_types = ":dna:rna:ami:usr:";
398            int         tlen          = strlen(type);
399            const char *found         = strstr(allowed_types, type);
400            if (!found || found == allowed_types || found[-1] != ':' || found[tlen] != ':') {
401                error = GBS_global_string("Invalid alignment type '%s'", type);
402            }
403        }
404
405        if (!error) {
406            GBDATA *gb_name = GB_find_string(gb_presets, "alignment_name", name, GB_IGNORE_CASE, SEARCH_GRANDCHILD);
407
408            if (gb_name) error = GBS_global_string("Alignment '%s' already exists", name);
409            else {
410                GBDATA *gb_ali     = GB_create_container(gb_presets, "alignment");
411                if (!gb_ali) error = GB_await_error();
412                else {
413                    error = GB_write_security_delete(gb_ali, 6);
414                    if (!error) error = create_ali_strEntry(gb_ali, "alignment_name",           name, 6);
415                    if (!error) error = create_ali_intEntry(gb_ali, "alignment_len",            len, 0);
416                    if (!error) error = create_ali_intEntry(gb_ali, "aligned",                  aligned <= 0 ? 0 : 1, 0);
417                    if (!error) error = create_ali_intEntry(gb_ali, "alignment_write_security", security, 6);
418                    if (!error) error = create_ali_strEntry(gb_ali, "alignment_type",           type, 0);
419                }
420
421                if (!error) result = gb_ali;
422            }
423        }
424    }
425
426    if (!result) {
427        gb_assert(error);
428        GB_export_errorf("in GBT_create_alignment: %s", error);
429    }
430#if defined(DEBUG)
431    else gb_assert(!error);
432#endif // DEBUG
433
434    return result;
435}
436
437
438static GB_ERROR gbt_rename_alignment_of_item(GBDATA *gb_item_container, const char *item_name, const char *item_entry_name,
439                                             const char *source, const char *dest, int copy, int dele)
440{
441    GB_ERROR  error = 0;
442    GBDATA   *gb_item;
443
444    for (gb_item = GB_entry(gb_item_container, item_entry_name);
445         gb_item && !error;
446         gb_item = GB_nextEntry(gb_item))
447    {
448        GBDATA *gb_ali = GB_entry(gb_item, source);
449        if (!gb_ali) continue;
450
451        if (copy) {
452            GBDATA *gb_new = GB_entry(gb_item, dest);
453            if (gb_new) {
454                error = GBS_global_string("Entry '%s' already exists", dest);
455            }
456            else {
457                gb_new             = GB_create_container(gb_item, dest);
458                if (!gb_new) error = GB_await_error();
459                else error         = GB_copy(gb_new, gb_ali);
460            }
461        }
462        if (dele) error = GB_delete(gb_ali);
463    }
464
465    if (error && gb_item) {
466        error = GBS_global_string("%s\n(while renaming alignment for %s '%s')", error, item_name, GBT_read_name(gb_item));
467    }
468
469    return error;
470}
471
472GB_ERROR GBT_rename_alignment(GBDATA *gbMain, const char *source, const char *dest, int copy, int dele)
473{
474    /* if copy == 1 then create a copy
475     * if dele == 1 then delete old
476     */
477
478    GB_ERROR  error            = 0;
479    int       is_case_error    = 0;
480    GBDATA   *gb_presets       = GBT_get_presets(gbMain);
481    GBDATA   *gb_species_data  = GBT_get_species_data(gbMain);
482    GBDATA   *gb_extended_data = GBT_get_SAI_data(gbMain);
483
484    if (!gb_presets || !gb_species_data || !gb_extended_data) error = GB_await_error();
485
486    // create copy and/or delete old alignment description
487    if (!error) {
488        GBDATA *gb_old_alignment = GBT_get_alignment(gbMain, source);
489
490        if (!gb_old_alignment) {
491            error = GB_await_error();
492        }
493        else {
494            if (copy) {
495                GBDATA *gbh = GBT_get_alignment(gbMain, dest);
496                if (gbh) {
497                    error         = GBS_global_string("destination alignment '%s' already exists", dest);
498                    is_case_error = (strcasecmp(source, dest) == 0); // test for case-only difference
499                }
500                else {
501                    GB_clear_error();
502                    error = GBT_check_alignment_name(dest);
503                    if (!error) {
504                        GBDATA *gb_new_alignment = GB_create_container(gb_presets, "alignment");
505                        error                    = GB_copy(gb_new_alignment, gb_old_alignment);
506                        if (!error) error        = GBT_write_string(gb_new_alignment, "alignment_name", dest);
507                    }
508                }
509            }
510
511            if (dele && !error) {
512                error = GB_delete(gb_old_alignment);
513            }
514        }
515    }
516
517    // change default alignment
518    if (!error && dele && copy) {
519        error = GBT_write_string(gb_presets, "use", dest);
520    }
521
522    // copy and/or delete alignment entries in species
523    if (!error) {
524        error = gbt_rename_alignment_of_item(gb_species_data, "Species", "species", source, dest, copy, dele);
525    }
526
527    // copy and/or delete alignment entries in SAIs
528    if (!error) {
529        error = gbt_rename_alignment_of_item(gb_extended_data, "SAI", "extended", source, dest, copy, dele);
530    }
531
532    if (is_case_error) {
533        // alignments source and dest only differ in case
534        char *ali_other = gbt_nonexisting_alignment(gbMain);
535        gb_assert(copy);
536
537        printf("Renaming alignment '%s' -> '%s' -> '%s' (to avoid case-problem)\n", source, ali_other, dest);
538
539        error             = GBT_rename_alignment(gbMain, source, ali_other, 1, dele);
540        if (!error) error = GBT_rename_alignment(gbMain, ali_other, dest, 1, 1);
541
542        free(ali_other);
543    }
544
545    return error;
546}
547
548// -----------------------------------------
549//      alignment related item functions
550
551NOT4PERL GBDATA *GBT_add_data(GBDATA *species, const char *ali_name, const char *key, GB_TYPES type) {
552    // goes to header: __ATTR__DEPRECATED_TODO("better use GBT_create_sequence_data()")
553
554    /* replace this function by GBT_create_sequence_data
555     * the same as GB_search(species, 'ali_name/key', GB_CREATE)
556     *
557     * Note: The behavior is weird, cause it does sth special for GB_STRING (write default content "...")
558     *
559     * returns create database entry (or NULL; exports an error in this case)
560     */
561
562    GB_ERROR error = GB_check_key(ali_name);
563    if (error) {
564        error = GBS_global_string("Invalid alignment name '%s' (Reason: %s)", ali_name, error);
565    }
566    else {
567        error = GB_check_hkey(key);
568        if (error) {
569            error = GBS_global_string("Invalid field name '%s' (Reason: %s)", key, error);
570        }
571    }
572
573    GBDATA *gb_data = NULL;
574    if (error) {
575        GB_export_error(error);
576    }
577    else {
578        GBDATA *gb_gb     = GB_entry(species, ali_name);
579        if (!gb_gb) gb_gb = GB_create_container(species, ali_name);
580
581        if (gb_gb) {
582            if (type == GB_STRING) {
583                gb_data = GB_search(gb_gb, key, GB_FIND);
584                if (!gb_data) {
585                    gb_data = GB_search(gb_gb, key, GB_STRING);
586                    GB_write_string(gb_data, "...");
587                }
588            }
589            else {
590                gb_data = GB_search(gb_gb, key, type);
591            }
592        }
593    }
594    return gb_data;
595}
596
597NOT4PERL GBDATA *GBT_create_sequence_data(GBDATA *species, const char *ali_name, const char *key, GB_TYPES type, int security_write) {
598    GBDATA *gb_data = GBT_add_data(species, ali_name, key, type);
599    if (gb_data) {
600        GB_ERROR error = GB_write_security_write(gb_data, security_write);
601        if (error) {
602            GB_export_error(error);
603            gb_data = 0;
604        }
605    }
606    return gb_data;
607}
608
609
610GB_ERROR GBT_write_sequence(GBDATA *gb_data, const char *ali_name, long ali_len, const char *sequence) {
611    /* writes a sequence which is generated by GBT_add_data,
612     * cuts sequence after alignment len only if bases e ".-nN"
613     */
614    int slen = strlen(sequence);
615    int old_char = 0;
616    GB_ERROR error = 0;
617    if (slen > ali_len) {
618        int i;
619        for (i = slen -1; i>=ali_len; i--) {
620            if (!strchr("-.nN", sequence[i])) break;    // real base after end of alignment
621        }
622        i++;                            // points to first 0 after alignment
623        if (i > ali_len) {
624            GBDATA *gb_main = GB_get_root(gb_data);
625            ali_len = GBT_get_alignment_len(gb_main, ali_name);
626            if (slen > ali_len) {               // check for modified alignment len
627                GBT_set_alignment_len(gb_main, ali_name, i);
628                ali_len = i;
629            }
630        }
631        if (slen > ali_len) {
632            old_char = sequence[ali_len];
633            ((char*)sequence)[ali_len] = 0;
634        }
635    }
636    error = GB_write_string(gb_data, sequence);
637    if (slen> ali_len) ((char*)sequence)[ali_len] = old_char;
638    return error;
639}
640
641
642GBDATA *GBT_gen_accession_number(GBDATA *gb_species, const char *ali_name) {
643    GBDATA *gb_acc = GB_entry(gb_species, "acc");
644    if (!gb_acc) {
645        GBDATA *gb_data = GBT_read_sequence(gb_species, ali_name);
646        if (gb_data) {                                     // found a valid alignment
647            GB_CSTR     sequence = GB_read_char_pntr(gb_data);
648            long        id       = GBS_checksum(sequence, 1, ".-");
649            const char *acc      = GBS_global_string("ARB_%lX", id);
650            GB_ERROR    error    = GBT_write_string(gb_species, "acc", acc);
651
652            if (error) GB_export_error(error);
653        }
654    }
655    return gb_acc;
656}
657
658
659int GBT_is_partial(GBDATA *gb_species, int default_value, bool define_if_undef) {
660    // checks whether a species has a partial or full sequence
661    //
662    // Note: partial sequences should not be used for tree calculations
663    //
664    // returns: 0 if sequence is full
665    //          1 if sequence is partial
666    //          -1 in case of error (which is exported in this case)
667    //
668    // if the sequence has no 'ARB_partial' entry it returns 'default_value'
669    // if 'define_if_undef' is true then create an 'ARB_partial'-entry with the default value
670
671    int       result     = -1;
672    GB_ERROR  error      = 0;
673    GBDATA   *gb_partial = GB_entry(gb_species, "ARB_partial");
674
675    if (gb_partial) {
676        result = GB_read_int(gb_partial);
677        if (result != 0 && result != 1) {
678            error = "Illegal value for 'ARB_partial' (only 1 or 0 allowed)";
679        }
680    }
681    else {
682        if (define_if_undef) {
683            error = GBT_write_int(gb_species, "ARB_partial", default_value);
684        }
685        result = default_value;
686    }
687
688    if (error) {
689        GB_export_error(error);
690        return -1;
691    }
692    return result;
693}
694
695#if defined(WARN_TODO)
696#warning rename GBT_read_sequence - it does not read the sequence itself
697#endif
698GBDATA *GBT_read_sequence(GBDATA *gb_species, const char *aliname) {
699    GBDATA *gb_ali = GB_entry(gb_species, aliname);
700    return gb_ali ? GB_entry(gb_ali, "data") : 0;
701}
702
703char *GBT_get_default_alignment(GBDATA *gb_main) {
704    gb_assert(!GB_have_error()); // illegal to enter this function when an error is exported!
705    return GBT_read_string(gb_main, GB_DEFAULT_ALIGNMENT);
706}
707
708GB_ERROR GBT_set_default_alignment(GBDATA *gb_main, const char *alignment_name) {
709    return GBT_write_string(gb_main, GB_DEFAULT_ALIGNMENT, alignment_name);
710}
711
712GBDATA *GBT_get_alignment(GBDATA *gb_main, const char *aliname) {
713    GBDATA *gb_presets        = GBT_get_presets(gb_main);
714    GBDATA *gb_alignment_name = GB_find_string(gb_presets, "alignment_name", aliname, GB_IGNORE_CASE, SEARCH_GRANDCHILD);
715
716    if (!gb_alignment_name) {
717        GB_export_errorf("alignment '%s' not found", aliname);
718        return NULL;
719    }
720    return GB_get_father(gb_alignment_name);
721}
722
723#if defined(WARN_TODO)
724#warning recode and change result type to long* ?
725#endif
726long GBT_get_alignment_len(GBDATA *gb_main, const char *aliname) {
727    GBDATA *gb_alignment = GBT_get_alignment(gb_main, aliname);
728    return gb_alignment ? *GBT_read_int(gb_alignment, "alignment_len") : -1;
729}
730
731GB_ERROR GBT_set_alignment_len(GBDATA *gb_main, const char *aliname, long new_len) {
732    GB_ERROR  error        = 0;
733    GBDATA   *gb_alignment = GBT_get_alignment(gb_main, aliname);
734
735    if (gb_alignment) {
736        GB_push_my_security(gb_main);
737        error             = GBT_write_int(gb_alignment, "alignment_len", new_len); // write new len
738        if (!error) error = GBT_write_int(gb_alignment, "aligned", 0);             // mark as unaligned
739        GB_pop_my_security(gb_main);
740    }
741    else error = GB_export_errorf("Alignment '%s' not found", aliname);
742    return error;
743}
744
745char *GBT_get_alignment_type_string(GBDATA *gb_main, const char *aliname) {
746    char   *result       = NULL;
747    GBDATA *gb_alignment = GBT_get_alignment(gb_main, aliname);
748    if (gb_alignment) {
749        result = GBT_read_string(gb_alignment, "alignment_type");
750        gb_assert(result);
751    }
752    return result;
753}
754
755GB_alignment_type GBT_get_alignment_type(GBDATA *gb_main, const char *aliname) {
756    char              *ali_type = GBT_get_alignment_type_string(gb_main, aliname);
757    GB_alignment_type  at       = GB_AT_UNKNOWN;
758
759    if (ali_type) {
760        switch (ali_type[0]) {
761            case 'r': if (strcmp(ali_type, "rna")==0) at = GB_AT_RNA; break;
762            case 'd': if (strcmp(ali_type, "dna")==0) at = GB_AT_DNA; break;
763            case 'a': if (strcmp(ali_type, "ami")==0) at = GB_AT_AA; break;
764            case 'p': if (strcmp(ali_type, "pro")==0) at = GB_AT_AA; break;
765            default: gb_assert(0); break;
766        }
767        free(ali_type);
768    }
769    return at;
770}
771
772bool GBT_is_alignment_protein(GBDATA *gb_main, const char *alignment_name) {
773    return GBT_get_alignment_type(gb_main, alignment_name) == GB_AT_AA;
774}
775
776// -----------------------
777//      gene sequence
778
779static const char *gb_cache_genome(GBDATA *gb_genome) {
780    static GBDATA *gb_last_genome = 0;
781    static char   *last_genome    = 0;
782
783    if (gb_genome != gb_last_genome) {
784        free(last_genome);
785
786        last_genome    = GB_read_string(gb_genome);
787        gb_last_genome = gb_genome;
788    }
789
790    return last_genome;
791}
792
793struct gene_part_pos {
794    int            parts;       // initialized for parts
795    unsigned char *certain;     // contains parts "=" chars
796    char           offset[256];
797};
798
799static gene_part_pos *gpp = 0;
800
801static void init_gpp(int parts) {
802    if (!gpp) {
803        int i;
804        gpp          = (gene_part_pos*)malloc(sizeof(*gpp));
805        gpp->certain = 0;
806
807        for (i = 0; i<256; ++i) gpp->offset[i] = 0;
808
809        gpp->offset[(int)'+'] = 1;
810        gpp->offset[(int)'-'] = -1;
811    }
812    else {
813        if (parts>gpp->parts) freenull(gpp->certain);
814    }
815
816    if (!gpp->certain) {
817        int forParts           = parts+10;
818        gpp->certain           = (unsigned char *)malloc(forParts+1);
819        memset(gpp->certain, '=', forParts);
820        gpp->certain[forParts] = 0;
821        gpp->parts             = forParts;
822    }
823}
824
825static void getPartPositions(const GEN_position *pos, int part, size_t *startPos, size_t *stopPos) {
826    // returns 'startPos' and 'stopPos' of one part of a gene
827    gb_assert(part<pos->parts);
828    *startPos = pos->start_pos[part]+gpp->offset[(pos->start_uncertain ? pos->start_uncertain : gpp->certain)[part]];
829    *stopPos  = pos->stop_pos [part]+gpp->offset[(pos->stop_uncertain  ? pos->stop_uncertain  : gpp->certain)[part]];
830}
831
832NOT4PERL char *GBT_read_gene_sequence_and_length(GBDATA *gb_gene, bool use_revComplement, char partSeparator, size_t *gene_length) {
833    // return the sequence data of a gene
834    //
835    // if use_revComplement is true -> use data from complementary strand (if complement is set for gene)
836    //                    otherwise -> use data from primary strand (sort+merge parts by position)
837    //
838    // if partSeparator not is 0 -> insert partSeparator between single (non-merged) parts
839    //
840    // returns sequence as result (and length of sequence if 'gene_length' points to something)
841    //
842    // if 'pos_certain' contains '+', start behind position (or end at position)
843    //                           '-', start at position (or end before position)
844    //
845    // For zero-length genes (e.g. "711^712") this function returns an empty string.
846
847    GB_ERROR      error      = 0;
848    char         *result     = 0;
849    GBDATA       *gb_species = GB_get_grandfather(gb_gene);
850    GEN_position *pos        = GEN_read_position(gb_gene);
851
852    if (!pos) error = GB_await_error();
853    else {
854        GBDATA        *gb_seq        = GBT_read_sequence(gb_species, "ali_genom");
855        unsigned long  seq_length    = GB_read_count(gb_seq);
856        int            p;
857        int            parts         = pos->parts;
858        int            resultlen     = 0;
859        int            separatorSize = partSeparator ? 1 : 0;
860
861        init_gpp(parts);
862
863        // test positions and calculate overall result length
864        for (p = 0; p<parts && !error; p++) {
865            size_t start;
866            size_t stop;
867            getPartPositions(pos, p, &start, &stop);
868
869            if (start<1 || start>(stop+1) || stop > seq_length) { // do not reject zero-length genes (start == stop+1)
870                error = GBS_global_string("Illegal gene position(s): start=%zu, end=%zu, seq.length=%li",
871                                          start, stop, seq_length);
872            }
873            else {
874                resultlen += stop-start+1;
875            }
876        }
877
878        if (separatorSize) resultlen += (parts-1)*separatorSize;
879
880        if (!error) {
881            char T_or_U = 0;
882            if (use_revComplement) {
883                error = GBT_determine_T_or_U(GB_AT_DNA, &T_or_U, "reverse-complement");
884            }
885            else if (parts>1) {
886                GEN_sortAndMergeLocationParts(pos);
887                parts = pos->parts; // may have changed
888            }
889
890            if (!error) {
891                const char *seq_data = gb_cache_genome(gb_seq);
892                char       *resultpos;
893
894                result    = (char*)malloc(resultlen+1);
895                resultpos = result;
896
897                if (gene_length) *gene_length = resultlen;
898
899                for (p = 0; p<parts; ++p) {
900                    size_t start;
901                    size_t stop;
902                    getPartPositions(pos, p, &start, &stop);
903
904                    int len = stop-start+1;
905
906                    if (separatorSize && p>0) *resultpos++ = partSeparator;
907
908                    memcpy(resultpos, seq_data+start-1, len);
909                    if (T_or_U && pos->complement[p]) {
910                        GBT_reverseComplementNucSequence(resultpos, len, T_or_U);
911                    }
912                    resultpos += len;
913                }
914
915                resultpos[0] = 0;
916            }
917        }
918        GEN_free_position(pos);
919    }
920
921    gb_assert(result || error);
922
923    if (error) {
924        char *id = GEN_global_gene_identifier(gb_gene, gb_species);
925        error    = GB_export_errorf("Can't read sequence of '%s' (Reason: %s)", id, error);
926        free(id);
927        free(result);
928        result   = 0;
929    }
930
931    return result;
932}
933
934char *GBT_read_gene_sequence(GBDATA *gb_gene, bool use_revComplement, char partSeparator) {
935    return GBT_read_gene_sequence_and_length(gb_gene, use_revComplement, partSeparator, 0);
936}
937
938// --------------------------------------------------------------------------------
939
940#ifdef UNIT_TESTS
941#include <test_unit.h>
942
943void TEST_alignment() {
944    GB_shell  shell;
945    GBDATA   *gb_main = GB_open("TEST_prot.arb", "r");
946
947    {
948        GB_transaction ta(gb_main);
949
950        TEST_EXPECT_EQUAL(GBT_count_alignments(gb_main), 2);
951
952        char *def_ali_name = GBT_get_default_alignment(gb_main);
953        TEST_EXPECT_EQUAL(def_ali_name, "ali_tuf_dna");
954
955        {
956            ConstStrArray names;
957            GBT_get_alignment_names(names, gb_main);
958            {
959                char *joined = GBT_join_names(names, '*');
960                TEST_EXPECT_EQUAL(joined, "ali_tuf_pro*ali_tuf_dna");
961                free(joined);
962            }
963
964            for (int i = 0; names[i]; ++i) {
965                long len = GBT_get_alignment_len(gb_main, names[i]);
966                TEST_EXPECT_EQUAL(len, !i ? 473 : 1426);
967
968                char *type_name = GBT_get_alignment_type_string(gb_main, names[i]);
969                TEST_EXPECT_EQUAL(type_name, !i ? "ami" : "dna");
970                free(type_name);
971
972                GB_alignment_type type = GBT_get_alignment_type(gb_main, names[i]);
973                TEST_EXPECT_EQUAL(type, !i ? GB_AT_AA : GB_AT_DNA);
974                TEST_EXPECT_EQUAL(GBT_is_alignment_protein(gb_main, names[i]), !i);
975            }
976        }
977
978        free(def_ali_name);
979    }
980
981    GB_close(gb_main);
982}
983
984#endif // UNIT_TESTS
Note: See TracBrowser for help on using the repository browser.