source: tags/arb-6.0/NTREE/ad_spec.cxx

Last change on this file was 11354, checked in by westram, 10 years ago
  • fix several cppcheck warnings
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.8 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : ad_spec.cxx                                       //
4//   Purpose   : Create and modify species and SAI.                //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include "NT_local.h"
12#include "map_viewer.h"
13
14#include <dbui.h>
15
16#include <awt_www.hxx>
17#include <awt_canvas.hxx>
18
19#include <aw_awars.hxx>
20#include <aw_msg.hxx>
21#include <aw_root.hxx>
22
23#include <arb_progress.h>
24#include <arb_defs.h>
25
26#include <cctype>
27
28static const char * const SAI_COUNTED_CHARS = "COUNTED_CHARS";
29
30void NT_count_different_chars(AW_window *, AW_CL cl_gb_main, AW_CL) {
31    // @@@ extract algorithm and call extracted from testcode
32    ARB_ERROR  error;
33    GBDATA    *gb_main = (GBDATA*)cl_gb_main;
34
35    error = GB_begin_transaction(gb_main);                      // open transaction
36
37    char *alignment_name = GBT_get_default_alignment(gb_main);  // info about sequences
38    int   alignment_len  = GBT_get_alignment_len(gb_main, alignment_name);
39    int   is_amino       = GBT_is_alignment_protein(gb_main, alignment_name);
40
41    if (!error) {
42        const int MAXLETTER   = 256;
43        const int FIRSTLETTER = 0; // cppcheck-suppress variableScope
44
45        typedef bool letterOccurs[MAXLETTER];
46
47        letterOccurs *occurs = (letterOccurs*)malloc(sizeof(*occurs)*alignment_len);
48        for (int i = 0; i<MAXLETTER; ++i) {
49            for (int p = 0; p<alignment_len; ++p) {
50                occurs[p][i] = false;
51            }
52        }
53
54        // loop over all marked species
55        {
56            int          all_marked = GBT_count_marked_species(gb_main);
57            arb_progress progress("Counting different characters", all_marked);
58
59            for (GBDATA *gb_species = GBT_first_marked_species(gb_main);
60                 gb_species && !error;
61                 gb_species = GBT_next_marked_species(gb_species))
62            {
63                GBDATA *gb_ali = GB_entry(gb_species, alignment_name); // search the sequence database entry ( ali_xxx/data )
64                if (gb_ali) {
65                    GBDATA *gb_data = GB_entry(gb_ali, "data");
66                    if (gb_data) {
67                        const char * const seq = GB_read_char_pntr(gb_data);
68                        if (seq) {
69                            for (int i=0; i< alignment_len; ++i) {
70                                unsigned char c = seq[i];
71                                if (!c) break;
72
73                                occurs[i][c-FIRSTLETTER] = true;
74                            }
75                        }
76                    }
77                }
78                progress.inc_and_check_user_abort(error);
79            }
80        }
81
82        if (!error) {
83
84            char filter[256];
85            if (is_amino) for (int c = 0; c<256; ++c) filter[c] = isupper(c) && (strchr("BJOUZ", c) == 0);
86            else          for (int c = 0; c<256; ++c) filter[c] = (strchr("ACGTU", c) != 0);
87
88            char result[alignment_len+1];
89            for (int i=0; i<alignment_len; i++) {
90                int sum = 0;
91                for (int c = 'A'; c < 'Z'; ++c) {
92                    if (filter[c]) {
93                        sum += (occurs[i][c] || occurs[i][tolower(c)]);
94                    }
95                }
96                result[i] = sum<10 ? '0'+sum : 'A'-10+sum;
97            }
98            result[alignment_len] = 0;
99
100            {
101                GBDATA *gb_sai     = GBT_find_or_create_SAI(gb_main, SAI_COUNTED_CHARS);
102                if (!gb_sai) error = GB_await_error();
103                else {
104                    GBDATA *gb_data     = GBT_add_data(gb_sai, alignment_name, "data", GB_STRING);
105                    if (!gb_data) error = GB_await_error();
106                    else    error       = GB_write_string(gb_data, result);
107                }
108            }
109        }
110
111        free(occurs);
112    }
113
114    free(alignment_name);
115
116    GB_end_transaction_show_error(gb_main, error, aw_message);
117}
118
119void NT_create_sai_from_pfold(AW_window *aww, AW_CL ntw, AW_CL) {
120    /*! \brief Creates an SAI from protein secondary structure of a selected species.
121     *
122     *  \param[in] aww AW_window
123     *  \param[in] ntw AWT_canvas
124     *
125     *  The function takes the currently selected species and searches for the field
126     *  "sec_struct". A new SAI is created using the data in this field. A simple input
127     *  window allows the user to change the default name ([species name]_pfold) for
128     *  the new SAI.
129     *
130     *  \note The import filter "dssp_all.ift" allows for importing the amino acid sequence
131     *        as well as the protein secondary structure from a dssp file and the structure
132     *        is stored in the field "sec_struct". That way, secondary structure can be
133     *        aligned along with the sequence manually and can later be extracted to create
134     *        an SAI.
135     *
136     *  \attention The import filter "dssp_2nd_struct.ift" extracts only the protein
137     *             secondary structure which is stored as alignment data. SAIs can simply
138     *             be created from these species via move_species_to_extended().
139     */
140
141    GB_ERROR  error      = 0;
142    GB_begin_transaction(GLOBAL.gb_main);
143    char     *sai_name   = 0;
144    char     *sec_struct = 0;
145    bool      canceled   = false;
146
147    // get the selected species
148    char *species_name = aww->get_root()->awar(AWAR_SPECIES_NAME)->read_string();
149    GBDATA *gb_species = 0;
150    if (!strcmp(species_name, "") || !(gb_species = GBT_find_species(GLOBAL.gb_main, species_name))) {
151        error = "Please select a species first.";
152    }
153    else {
154        // search for the field "sec_struct"
155        GBDATA *gb_species_sec_struct = GB_entry(gb_species, "sec_struct");
156        if (!gb_species_sec_struct) {
157            error = "Field \"sec_struct\" not found or empty. Please select another species.";
158        }
159        else if (!(sec_struct = GB_read_string(gb_species_sec_struct))) {
160            error = "Couldn't read field \"sec_struct\". Is it empty?";
161        }
162        else {
163            // generate default name and name input field for the new SAI
164            {
165                char *sai_default_name = GBS_global_string_copy("%s%s", species_name, strstr(species_name, "_pfold") ? "" : "_pfold");
166                sai_name         = aw_input("Name of SAI to create:", sai_default_name);
167                free(sai_default_name);
168            }
169
170            if (!sai_name) {
171                canceled = true;
172            }
173            else if (strspn(sai_name, " ") == strlen(sai_name)) {
174                error = "Name of SAI is empty. Please enter a valid name.";
175            }
176            else {
177                GBDATA *gb_sai_data = GBT_get_SAI_data(GLOBAL.gb_main);
178                GBDATA *gb_sai      = GBT_find_SAI_rel_SAI_data(gb_sai_data, sai_name);
179                char   *ali_name    = GBT_get_default_alignment(GLOBAL.gb_main);
180
181                if (gb_sai) {
182                    error = "SAI with the same name already exists. Please enter another name.";
183                }
184                else {
185                    // create SAI container and copy fields from the species to the SAI
186                    gb_sai                   = GB_create_container(gb_sai_data, "extended");
187                    GBDATA *gb_species_field = GB_child(gb_species);
188
189                    while (gb_species_field && !error) {
190                        char   *key          = GB_read_key(gb_species_field);
191                        GBDATA *gb_sai_field = GB_search(gb_sai, GB_read_key(gb_species_field), GB_read_type(gb_species_field));
192
193                        if (strcmp(key, "name") == 0) { // write the new name
194                            error = GB_write_string(gb_sai_field, sai_name);
195                        }
196                        else if (strcmp(key, "sec_struct") == 0) { // write contents from the field "sec_struct" to the alignment data
197                            GBDATA *gb_sai_ali     = GB_search(gb_sai, ali_name, GB_CREATE_CONTAINER);
198                            if (!gb_sai_ali) error = GB_await_error();
199                            else    error          = GBT_write_string(gb_sai_ali, "data", sec_struct);
200                        }
201                        else if (strcmp(key, "acc") != 0 && strcmp(key, ali_name) != 0) { // don't copy "acc" and the old alignment data
202                            error = GB_copy(gb_sai_field, gb_species_field);
203                        }
204                        gb_species_field = GB_nextChild(gb_species_field);
205                        free(key);
206                    }
207
208                    // generate accession number and delete field "sec_struct" from the SAI
209                    if (!error) {
210                        // TODO: is it necessary that a new acc is generated here?
211                        GBDATA *gb_sai_acc = GB_search(gb_sai, "acc", GB_FIND);
212                        if (gb_sai_acc) {
213                            GB_delete(gb_sai_acc);
214                            GBT_gen_accession_number(gb_sai, ali_name);
215                        }
216                        GBDATA *gb_sai_sec_struct = GB_search(gb_sai, "sec_struct", GB_FIND);
217                        if (gb_sai_sec_struct) GB_delete(gb_sai_sec_struct);
218                        aww->get_root()->awar(AWAR_SAI_NAME)->write_string(sai_name);
219                    }
220                }
221            }
222        }
223    }
224
225    if (canceled) error = "Aborted by user";
226
227    GB_end_transaction_show_error(GLOBAL.gb_main, error, aw_message);
228
229    if (!error) {
230        AW_window *sai_info = NT_create_extendeds_window(aww->get_root());
231        // TODO: why doesn't info box show anything on first startup? proper refresh needed?
232        sai_info->activate();
233        ((AWT_canvas *)ntw)->refresh(); // refresh doesn't work, I guess...
234    }
235
236    free(species_name);
237    free(sai_name);
238    free(sec_struct);
239}
240
241void launch_MapViewer_cb(GBDATA *gbd, AD_MAP_VIEWER_TYPE type) {
242    GB_ERROR error = GB_push_transaction(GLOBAL.gb_main);
243
244    if (!error) {
245        const char *species_name    = "";
246        GBDATA     *gb_species_data = GBT_get_species_data(GLOBAL.gb_main);
247
248        if (gbd && GB_get_father(gbd) == gb_species_data) {
249            species_name = GBT_read_name(gbd);
250        }
251
252        AW_root::SINGLETON->awar(AWAR_SPECIES_NAME)->write_string(species_name);
253    }
254
255    if (!error && gbd && type == ADMVT_WWW) {
256        GBDATA *gb_name       = GB_entry(gbd, "name");
257        if (!gb_name) gb_name = GB_entry(gbd, "group_name"); // bad hack, should work
258
259        const char *name = gb_name ? GB_read_char_pntr(gb_name) : "noname";
260        error = awt_openURL_by_gbd(GLOBAL.aw_root, GLOBAL.gb_main, gbd, name);
261    }
262
263    error = GB_end_transaction(GLOBAL.gb_main, error);
264    if (error) aw_message(error);
265}
266
267
268// --------------------------------------------------------------------------------
269
270#ifdef UNIT_TESTS
271#include <test_unit.h>
272#include <arb_unit_test.h>
273
274static uint32_t counted_chars_checksum(GBDATA *gb_main)  {
275    GB_transaction ta(gb_main);
276
277    GBDATA *gb_sai;
278    GBDATA *gb_ali;
279    GBDATA *gb_counted_chars;
280
281    char *ali_name = GBT_get_default_alignment(gb_main);
282
283    TEST_EXPECT_RESULT__NOERROREXPORTED(gb_sai = GBT_expect_SAI(gb_main, SAI_COUNTED_CHARS));
284    TEST_EXPECT_RESULT__NOERROREXPORTED(gb_ali = GB_entry(gb_sai, ali_name));
285    TEST_EXPECT_RESULT__NOERROREXPORTED(gb_counted_chars = GB_entry(gb_ali, "data"));
286
287    const char *data = GB_read_char_pntr(gb_counted_chars);
288
289    free(ali_name);
290
291    return GBS_checksum(data, 0, NULL);
292}
293
294void TEST_count_chars() {
295    // calculate SAI for test DBs
296
297    arb_suppress_progress silence;
298    GB_shell              shell;
299
300    for (int prot = 0; prot<2; ++prot) {
301        GBDATA *gb_main;
302        TEST_EXPECT_RESULT__NOERROREXPORTED(gb_main = GB_open(prot ? "TEST_prot.arb" : "TEST_nuc.arb", "rw"));
303
304        GBT_mark_all(gb_main, 1);
305        NT_count_different_chars(NULL, (AW_CL)gb_main, 0);
306
307        uint32_t expected = prot ? 0x4fa63fa0 : 0xefb05e4e;
308        TEST_EXPECT_EQUAL(counted_chars_checksum(gb_main), expected);
309
310        GB_close(gb_main);
311    }
312}
313void TEST_SLOW_count_chars() {
314    // calculate a real big test alignment
315    //
316    // the difference to TEST_count_chars() is just in size of alignment.
317    // NT_count_different_chars() crashes for big alignments when running in gdb
318    arb_suppress_progress silence;
319    GB_shell              shell;
320    {
321        arb_unit_test::test_alignment_data data_source[] = {
322            { 1, "s1", "ACGT" },
323            { 1, "s2", "ACGTN" },
324            { 1, "s3", "NANNAN" },
325            { 1, "s4", "GATTACA" },
326        };
327
328        const int alilen = 50000;
329        const int count  = ARRAY_ELEMS(data_source);
330
331        char *longSeq[count];
332        for (int c = 0; c<count; ++c) {
333            char *dest = longSeq[c] = (char*)malloc(alilen+1);
334
335            const char *source = data_source[c].data;
336            int         len    = strlen(source);
337
338            for (int p = 0; p<alilen; ++p) {
339                dest[p] = source[p%len];
340            }
341            dest[alilen] = 0;
342           
343            data_source[c].data = dest;
344        }
345
346        ARB_ERROR  error;
347        GBDATA    *gb_main = TEST_CREATE_DB(error, "ali_test", data_source, false);
348
349        TEST_EXPECT_NO_ERROR(error.deliver());
350
351        NT_count_different_chars(NULL, (AW_CL)gb_main, 0);
352
353        // TEST_EXPECT_EQUAL(counted_chars_checksum(gb_main), 0x1d34a14f);
354        // TEST_EXPECT_EQUAL(counted_chars_checksum(gb_main), 0x609d788b);
355        TEST_EXPECT_EQUAL(counted_chars_checksum(gb_main), 0xccdfa527);
356
357        for (int c = 0; c<count; ++c) {
358            free(longSeq[c]);
359        }
360
361        GB_close(gb_main);
362    }
363}
364
365#endif // UNIT_TESTS
366
Note: See TracBrowser for help on using the repository browser.