| 1 | #include <stdio.h> |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <string.h> |
|---|
| 4 | |
|---|
| 5 | #include <arbdb.h> |
|---|
| 6 | #include <arbdbt.h> |
|---|
| 7 | |
|---|
| 8 | #include <aw_awars.hxx> |
|---|
| 9 | |
|---|
| 10 | /* Input: SAI name: argv[1] |
|---|
| 11 | * fastdnaml-args: argv[2]... |
|---|
| 12 | * ALINAME: AWAR "presets/use" |
|---|
| 13 | * FILTER: AWAR_GDE_EXPORT_FILTER |
|---|
| 14 | * |
|---|
| 15 | * Output: |
|---|
| 16 | * If SAI + Sequence is found: rates in fastdnaml-format (can be piped into arb_convert_aln) |
|---|
| 17 | * Otherwise : just forward args |
|---|
| 18 | * |
|---|
| 19 | * If flag '-r' is used, weights are always printed. If no SAI given, every alignment-column |
|---|
| 20 | * is given the same weight (1). |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #define CATSCALE 0.71 // downscale factor for rates |
|---|
| 24 | #define MIO 1000000 // factor to scale rate-values to integers (for RAxML) |
|---|
| 25 | |
|---|
| 26 | int main(int argc, char **argv){ |
|---|
| 27 | argc--;argv++; |
|---|
| 28 | |
|---|
| 29 | if (argc<1 || strcmp(argv[0],"--help") == 0) { |
|---|
| 30 | fprintf(stderr, |
|---|
| 31 | "\n" |
|---|
| 32 | "arb_export_rates: Add a line to phylip format which can be used by fastdnaml for rates\n" |
|---|
| 33 | "syntax: arb_export_rates [SAI_NAME|'--none'] [other_fastdnaml_args]*\n" |
|---|
| 34 | "if other_fastdnaml_args are given they are inserted to the output\n" |
|---|
| 35 | "\n" |
|---|
| 36 | "or\n" |
|---|
| 37 | "\n" |
|---|
| 38 | "arb_export_rates: Write a weightsfile for RAxML\n" |
|---|
| 39 | "syntax: arb_export_rates -r [SAI_NAME|'--none']\n" |
|---|
| 40 | "\n" |
|---|
| 41 | ); |
|---|
| 42 | return EXIT_FAILURE; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | bool RAxML_mode = false; |
|---|
| 46 | |
|---|
| 47 | if (argc >= 2) { |
|---|
| 48 | if (strcmp(argv[0], "-r") == 0) { |
|---|
| 49 | RAxML_mode = true; |
|---|
| 50 | argc--;argv++; |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | GBDATA *gb_main = GBT_open(":","r",0); |
|---|
| 55 | if (!gb_main){ |
|---|
| 56 | GB_print_error(); |
|---|
| 57 | return EXIT_FAILURE; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | char *SAI_name = argv[0]; |
|---|
| 61 | argc--;argv++; |
|---|
| 62 | |
|---|
| 63 | { |
|---|
| 64 | char *seq = 0; |
|---|
| 65 | char *filter = 0; |
|---|
| 66 | int seq_len = 0; |
|---|
| 67 | int filter_len = 0; |
|---|
| 68 | long ali_len = 0; |
|---|
| 69 | { |
|---|
| 70 | GB_transaction ta(gb_main); |
|---|
| 71 | GBDATA *gb_sai = 0; |
|---|
| 72 | GBDATA *gb_data = 0; |
|---|
| 73 | |
|---|
| 74 | char *ali_name = GBT_get_default_alignment(gb_main); |
|---|
| 75 | ali_len = GBT_get_alignment_len(gb_main, ali_name); |
|---|
| 76 | |
|---|
| 77 | filter = GBT_read_string(gb_main,AWAR_GDE_EXPORT_FILTER); |
|---|
| 78 | filter_len = strlen(filter); |
|---|
| 79 | |
|---|
| 80 | if (SAI_name[0] != 0 || strcmp(SAI_name, "--none") != 0) { |
|---|
| 81 | gb_sai = GBT_find_SAI(gb_main,SAI_name); |
|---|
| 82 | if (gb_sai) { |
|---|
| 83 | gb_data = GBT_read_sequence(gb_sai,ali_name); |
|---|
| 84 | |
|---|
| 85 | if (gb_data) { |
|---|
| 86 | seq = GB_read_string(gb_data); |
|---|
| 87 | seq_len = strlen(seq); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | free(ali_name); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | if (!RAxML_mode) { |
|---|
| 95 | #define APPEARS_IN_HEADER(c) (strchr(not_in_header, (c)) == 0) |
|---|
| 96 | const char *not_in_header = "Y"; // these flags don't appear in header and they should be written directly after header |
|---|
| 97 | |
|---|
| 98 | { |
|---|
| 99 | char *firstline = strdup(""); |
|---|
| 100 | for (int arg = 0; arg<argc; ++arg) { // put all fastdnaml arguments to header |
|---|
| 101 | char command_char = argv[arg][0]; |
|---|
| 102 | if (!command_char) continue; // skip empty arguments |
|---|
| 103 | |
|---|
| 104 | if (APPEARS_IN_HEADER(command_char)) { |
|---|
| 105 | freeset(firstline, GBS_global_string_copy("%s %c", firstline, command_char)); |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | // print header |
|---|
| 110 | if (seq) fputc('C', stdout); // prefix with categories |
|---|
| 111 | fputs(firstline, stdout); // then other_fastdnaml_args |
|---|
| 112 | free(firstline); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | // print other_fastdnaml_args in reverse order |
|---|
| 116 | // (first those which do not appear in header, rest afterwards) |
|---|
| 117 | for (int appears_in_header = 0; appears_in_header <= 1; ++appears_in_header) { |
|---|
| 118 | for (int arg = 0; arg < argc; ++arg) { // print [other_fastdnaml_args]* |
|---|
| 119 | if (!argv[arg][0]) continue; // skip empty arguments |
|---|
| 120 | if (!argv[arg][1]) continue; // dont print single character commands again on a own line |
|---|
| 121 | if (APPEARS_IN_HEADER(argv[arg][0]) != appears_in_header) continue; |
|---|
| 122 | fputc('\n', stdout); |
|---|
| 123 | fputs(argv[arg], stdout); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | #undef APPEARS_IN_HEADER |
|---|
| 127 | |
|---|
| 128 | if (seq) { // if SAI was found |
|---|
| 129 | printf("\nC 35 "); |
|---|
| 130 | double rate = 1.0; |
|---|
| 131 | int i; |
|---|
| 132 | for (i=0;i<35;i++){ |
|---|
| 133 | printf("%f ",rate); |
|---|
| 134 | rate *= CATSCALE; |
|---|
| 135 | } |
|---|
| 136 | printf("\nCategories "); |
|---|
| 137 | |
|---|
| 138 | for (i=0; i<seq_len; i++) { |
|---|
| 139 | if (i>filter_len || filter[i] != '0') { |
|---|
| 140 | int c = seq[i]; |
|---|
| 141 | |
|---|
| 142 | arb_assert(c != '0'); // only 35 cats (1-9 and A-Z) |
|---|
| 143 | |
|---|
| 144 | if ( (c < '0' || c>'9' ) && (c < 'A' || c>'Z')) c = '1'; |
|---|
| 145 | putchar(c); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | for (; i<ali_len; i++) { |
|---|
| 149 | putchar('1'); |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | else { |
|---|
| 154 | // write RAxML weightsfile content |
|---|
| 155 | int cnt = 0; |
|---|
| 156 | |
|---|
| 157 | char *weight['Z'+1]; |
|---|
| 158 | int i; |
|---|
| 159 | |
|---|
| 160 | for (i = 0; i <= 'Z'; i++) weight[i] = 0; |
|---|
| 161 | |
|---|
| 162 | double rate = 1.0; |
|---|
| 163 | |
|---|
| 164 | for (i = '1'; i <= '9'; i++) { |
|---|
| 165 | weight[i] = GBS_global_string_copy(" %i", int(rate*MIO)); |
|---|
| 166 | rate *= CATSCALE; |
|---|
| 167 | } |
|---|
| 168 | for (i = 'A'; i <= 'Z'; i++) { |
|---|
| 169 | weight[i] = GBS_global_string_copy(" %i", int(rate*MIO)); |
|---|
| 170 | rate *= CATSCALE; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | if (!seq) { // no SAI selected -> unique weights |
|---|
| 174 | seq = (char*)malloc(ali_len+1); |
|---|
| 175 | memset(seq, '1', ali_len); |
|---|
| 176 | seq[ali_len] = 0; |
|---|
| 177 | |
|---|
| 178 | seq_len = ali_len; |
|---|
| 179 | |
|---|
| 180 | freedup(weight[int('1')], " 1"); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | for (i=0; i<seq_len; i++) { |
|---|
| 184 | if (i>filter_len || filter[i] != '0') { |
|---|
| 185 | int c = seq[i]; |
|---|
| 186 | if ( (c < '0' || c>'9' ) && (c < 'A' || c>'Z')) c = '1'; |
|---|
| 187 | fputs(weight[c], stdout); |
|---|
| 188 | if (++cnt>30) { |
|---|
| 189 | fputc('\n', stdout); |
|---|
| 190 | cnt = 0; |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | for (; i<ali_len; i++) { |
|---|
| 196 | if (i>filter_len || filter[i] != '0') { |
|---|
| 197 | fputs(weight[int('1')], stdout); |
|---|
| 198 | if (++cnt>30) { |
|---|
| 199 | fputc('\n', stdout); |
|---|
| 200 | cnt = 0; |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | for (i = 0; i <= 'Z'; i++) free(weight[i]); |
|---|
| 206 | |
|---|
| 207 | fputc('\n', stdout); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | free(filter); |
|---|
| 211 | free(seq); |
|---|
| 212 | } |
|---|
| 213 | GB_close(gb_main); |
|---|
| 214 | return EXIT_SUCCESS; |
|---|
| 215 | } |
|---|