source: tags/svn.1.5.4/TOOLS/arb_export_rates.cxx

Last change on this file was 8247, checked in by westram, 12 years ago
  • completes [8246]
    • main-wrapper-objects
      • compile with normal c-flags (fixes nightly cross-compilation error)
      • compile for C and C++ (allows to link vs leftover C-compilations (e.g. arb_a2ps))
    • changed main→ARB_main in TOOLS (fixes other broken nightly builds)
    • remove experimental entry-point from unit-test-executables (leftover from failed approach, execution via custom entry-point does not init static data at all)
    • corrected link printouts
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : arb_export_rates.cxx                              //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include <arbdbt.h>
12#include <aw_awar_defs.hxx>
13
14/* Input: SAI name from CL
15 *        fastdnaml-args from CL
16 *        ALINAME:  AWAR "presets/use"
17 *        FILTER:   AWAR_GDE_EXPORT_FILTER
18 *
19 * Output:
20 *        If SAI + Sequence is found: rates in fastdnaml-format (can be piped into arb_convert_aln)
21 *        Otherwise : just forward args
22 *
23 *        If flag '-r' is used, weights are always printed. If no SAI given, every alignment-column
24 *        is given the same weight (1).
25 */
26
27#define CATSCALE 0.71           // downscale factor for rates
28#define MIO      1000000        // factor to scale rate-values to integers (for RAxML)
29
30int ARB_main(int argc, const char *argv[]) {
31    argc--; argv++;
32
33    if (argc<1 || strcmp(argv[0], "--help") == 0) {
34        fprintf(stderr,
35                "\n"
36                "arb_export_rates: Add a line to phylip format which can be used by fastdnaml for rates\n"
37                "syntax: arb_export_rates [-d dbname] [SAI_NAME|'--none'] [other_fastdnaml_args]*\n"
38                "if other_fastdnaml_args are given they are inserted to the output\n"
39                "\n"
40                "or\n"
41                "\n"
42                "arb_export_rates: Write a weightsfile for RAxML\n"
43                "syntax: arb_export_rates [-d dbname] -r [SAI_NAME|'--none']\n"
44                "\n"
45                "Note: if no DB is specified using -d, the default server ':' will be used.\n"
46                );
47        return EXIT_FAILURE;
48    }
49
50    bool        RAxML_mode = false;
51    const char *dbname     = ":";
52    const char *SAI_name   = NULL;
53
54    if (argc >= 2) {
55        if (strcmp(argv[0], "-d") == 0) {
56            dbname  = argv[1];
57            argc   -= 2;
58            argv   += 2;
59        }
60    }
61    if (argc >= 2) {
62        if (strcmp(argv[0], "-r") == 0) {
63            RAxML_mode = true;
64            argc--; argv++;
65        }
66    }
67
68    GB_ERROR error = NULL;
69    if (argc >= 1) {
70        SAI_name = argv[0];
71        argc--; argv++;
72    }
73    else {
74        error = "Missing argument 'SAI_NAME'";
75    }
76
77    if (!error) {
78        GB_shell  shell;
79        GBDATA   *gb_main = GBT_open(dbname, "r");
80        if (!gb_main) {
81            error = GB_await_error();
82        }
83        else {
84            char *seq        = 0;
85            char *filter     = 0;
86            int   seq_len    = 0;
87            int   filter_len = 0;
88            long  ali_len    = 0;
89           
90            {
91                GB_transaction  ta(gb_main);
92
93                char *ali_name = GBT_get_default_alignment(gb_main);
94                ali_len        = GBT_get_alignment_len(gb_main, ali_name);
95
96                filter = GBT_read_string(gb_main, AWAR_GDE_EXPORT_FILTER);
97                if (!filter) {
98                    error = "Expected entry '" AWAR_GDE_EXPORT_FILTER "' does not exist";
99                }
100                else {
101                    filter_len = strlen(filter);
102
103                    bool have_no_sai = SAI_name[0] == 0 || strcmp(SAI_name, "--none") == 0;
104                    if (!have_no_sai) {
105                        GBDATA *gb_sai = GBT_find_SAI(gb_main, SAI_name);
106                        if (gb_sai) {
107                            GBDATA *gb_data = GBT_read_sequence(gb_sai, ali_name);
108                            if (gb_data) {
109                                seq = GB_read_string(gb_data);
110                                if (!seq) {
111                                    error = GBS_global_string("SAI '%s' has no data in '%s'", SAI_name, ali_name);
112                                }
113                                else {
114                                    seq_len = strlen(seq);
115                                }
116                            }
117                        }
118                        else {
119                            error = GBS_global_string("No such SAI '%s'", SAI_name);
120                        }
121                    }
122                }
123                free(ali_name);
124            }
125
126            if (!RAxML_mode) {
127#define APPEARS_IN_HEADER(c) (strchr(not_in_header, (c)) == 0)
128                const char *not_in_header = "Y"; // these flags don't appear in header and they should be written directly after header
129
130                {
131                    char *firstline = strdup("");
132                    for (int arg = 0; arg<argc; ++arg) { // put all fastdnaml arguments to header
133                        char command_char = argv[arg][0];
134                        if (!command_char) continue; // skip empty arguments
135
136                        if (APPEARS_IN_HEADER(command_char)) {
137                            freeset(firstline, GBS_global_string_copy("%s %c", firstline, command_char));
138                        }
139                    }
140
141                    // print header
142                    if (seq) fputc('C', stdout); // prefix with categories
143                    fputs(firstline, stdout); // then other_fastdnaml_args
144                    free(firstline);
145                }
146
147                // print other_fastdnaml_args in reverse order
148                // (first those which do not appear in header, rest afterwards)
149                for (int appears_in_header = 0; appears_in_header <= 1; ++appears_in_header) {
150                    for (int arg = 0; arg < argc; ++arg) { // print [other_fastdnaml_args]*
151                        if (!argv[arg][0]) continue; // skip empty arguments
152                        if (!argv[arg][1]) continue; // don't print single character commands again on a own line
153                        if (APPEARS_IN_HEADER(argv[arg][0]) != appears_in_header) continue;
154                        fputc('\n', stdout);
155                        fputs(argv[arg], stdout);
156                    }
157                }
158#undef APPEARS_IN_HEADER
159
160                if (seq) { // if SAI was found
161                    printf("\nC 35 ");
162                    double rate = 1.0;
163                    int    i;
164                    for (i=0; i<35; i++) {
165                        printf("%f ", rate);
166                        rate *= CATSCALE;
167                    }
168                    printf("\nCategories ");
169
170                    for (i=0; i<seq_len; i++) {
171                        if (i>filter_len || filter[i] != '0') {
172                            int c = seq[i];
173
174                            arb_assert(c != '0'); // only 35 cats (1-9 and A-Z)
175
176                            if ((c < '0' || c>'9') &&    (c < 'A' || c>'Z')) c = '1';
177                            putchar(c);
178                        }
179                    }
180                    for (; i<ali_len; i++) {
181                        putchar('1');
182                    }
183                }
184            }
185            else {
186                // write RAxML weightsfile content
187                int cnt = 0;
188
189                char *weight['Z'+1];
190                int i;
191
192                for (i = 0; i <= 'Z'; i++) weight[i] = 0;
193
194                double rate = 1.0;
195
196                for (i = '1'; i <= '9'; i++) {
197                    weight[i] = GBS_global_string_copy(" %i", int(rate*MIO+0.5));
198                    rate *= CATSCALE;
199                }
200                for (i = 'A'; i <= 'Z'; i++) {
201                    weight[i] = GBS_global_string_copy(" %i", int(rate*MIO+0.5));
202                    rate *= CATSCALE;
203                }
204
205                if (!seq) { // no SAI selected -> unique weights
206                    seq          = (char*)malloc(ali_len+1);
207                    memset(seq, '1', ali_len);
208                    seq[ali_len] = 0;
209
210                    seq_len = ali_len;
211
212                    freedup(weight[safeCharIndex('1')], " 1");
213                }
214
215                for (i=0; i<seq_len; i++) {
216                    if (i>filter_len || filter[i] != '0') {
217                        int c = seq[i];
218                        if ((c < '0' || c>'9') &&    (c < 'A' || c>'Z')) c = '1';
219                        fputs(weight[c], stdout);
220                        if (++cnt>30) {
221                            fputc('\n', stdout);
222                            cnt = 0;
223                        }
224                    }
225                }
226
227                for (; i<ali_len; i++) {
228                    if (i>filter_len || filter[i] != '0') {
229                        fputs(weight[safeCharIndex('1')], stdout);
230                        if (++cnt>30) {
231                            fputc('\n', stdout);
232                            cnt = 0;
233                        }
234                    }
235                }
236
237                for (i = 0; i <= 'Z'; i++) free(weight[i]);
238
239                fputc('\n', stdout);
240            }
241
242            free(filter);
243            free(seq);
244            GB_close(gb_main);
245        }
246    }
247
248    if (error) {
249        fprintf(stderr, "Error in arb_export_rates: %s\n", error);
250        return EXIT_FAILURE;
251    }
252    return EXIT_SUCCESS;
253}
Note: See TracBrowser for help on using the repository browser.