| 1 | #include <stdio.h> |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <string.h> |
|---|
| 4 | #include <ctype.h> |
|---|
| 5 | |
|---|
| 6 | #include <arbdb.h> |
|---|
| 7 | #include <arbdbt.h> |
|---|
| 8 | #include <aw_root.hxx> |
|---|
| 9 | #include <aw_device.hxx> |
|---|
| 10 | #include <aw_awars.hxx> |
|---|
| 11 | #include <aw_window.hxx> |
|---|
| 12 | #include <awt.hxx> |
|---|
| 13 | #include <awt_pro_a_nucs.hxx> |
|---|
| 14 | #include <awt_codon_table.hxx> |
|---|
| 15 | |
|---|
| 16 | extern GBDATA *gb_main; |
|---|
| 17 | |
|---|
| 18 | int awt_pro_a_nucs_convert(char *data, long size, int pos) |
|---|
| 19 | { |
|---|
| 20 | char *p,c; |
|---|
| 21 | int C; |
|---|
| 22 | char *dest; |
|---|
| 23 | char buffer[4]; |
|---|
| 24 | long i; |
|---|
| 25 | int stops = 0; |
|---|
| 26 | for (p = data; *p ; p++) { |
|---|
| 27 | c = *p; |
|---|
| 28 | if ((c>='a') &&(c<='z') ) c=c+'A'-'a'; |
|---|
| 29 | if (c=='U') c= 'T'; |
|---|
| 30 | *p = c; |
|---|
| 31 | } |
|---|
| 32 | buffer[3] = 0; |
|---|
| 33 | dest = data; |
|---|
| 34 | for (p = data+pos,i=pos;i+2<size ;p+=3,i+=3){ |
|---|
| 35 | buffer[0] = p[0]; |
|---|
| 36 | buffer[1] = p[1]; |
|---|
| 37 | buffer[2] = p[2]; |
|---|
| 38 | int spro = (int)GBS_read_hash(awt_pro_a_nucs->t2i_hash,buffer); |
|---|
| 39 | if (!spro) { |
|---|
| 40 | C = 'X'; |
|---|
| 41 | }else{ |
|---|
| 42 | if (spro == '*') stops++; |
|---|
| 43 | C = spro; |
|---|
| 44 | if (spro == 's') C = 'S'; |
|---|
| 45 | } |
|---|
| 46 | *(dest++) = (char)C; |
|---|
| 47 | } |
|---|
| 48 | *(dest++) = 0; |
|---|
| 49 | return stops; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | GB_ERROR arb_r2a(GBDATA *gbmain, int startpos, char *ali_source, char *ali_dest) |
|---|
| 53 | { |
|---|
| 54 | GBDATA *gb_source; |
|---|
| 55 | GBDATA *gb_dest; |
|---|
| 56 | GBDATA *gb_species; |
|---|
| 57 | GBDATA *gb_source_data; |
|---|
| 58 | GBDATA *gb_dest_data; |
|---|
| 59 | GB_ERROR error = 0; |
|---|
| 60 | char *data; |
|---|
| 61 | int count = 0; |
|---|
| 62 | int stops = 0; |
|---|
| 63 | |
|---|
| 64 | gb_source = GBT_get_alignment(gbmain,ali_source); |
|---|
| 65 | if (!gb_source) return "Please select a valid source alignment"; |
|---|
| 66 | gb_dest = GBT_get_alignment(gbmain,ali_dest); |
|---|
| 67 | if (!gb_dest) { |
|---|
| 68 | char *msg = strdup(GBS_global_string( |
|---|
| 69 | "You have not selected a destination alingment\n" |
|---|
| 70 | "May I create one ('%s_pro') for you",ali_source)); |
|---|
| 71 | if (aw_message(msg,"CREATE,CANCEL")){ |
|---|
| 72 | delete msg; |
|---|
| 73 | return "Cancelled"; |
|---|
| 74 | } |
|---|
| 75 | long slen = GBT_get_alignment_len(gbmain,ali_source); |
|---|
| 76 | delete msg; |
|---|
| 77 | ali_dest = strdup(GBS_global_string("%s_pro",ali_source)); |
|---|
| 78 | gb_dest = GBT_create_alignment(gbmain,ali_dest,slen/3+1,0,1,"ami"); |
|---|
| 79 | { |
|---|
| 80 | char *fname = strdup(GBS_global_string("%s/data",ali_dest)); |
|---|
| 81 | awt_add_new_changekey( gbmain,fname,GB_STRING); |
|---|
| 82 | delete fname; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | if (!gb_dest){ |
|---|
| 86 | aw_closestatus(); |
|---|
| 87 | return GB_get_error(); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | aw_openstatus("Translating"); |
|---|
| 92 | int spec_count = GBT_count_species(gbmain); |
|---|
| 93 | int spec_i = 0; |
|---|
| 94 | |
|---|
| 95 | for ( gb_species = GBT_first_marked_species(gbmain); |
|---|
| 96 | gb_species; |
|---|
| 97 | gb_species = GBT_next_marked_species(gb_species) ){ |
|---|
| 98 | if (aw_status(double(spec_i++)/double(spec_count))){ |
|---|
| 99 | error = "Aborted"; |
|---|
| 100 | break; |
|---|
| 101 | } |
|---|
| 102 | gb_source = GB_find(gb_species,ali_source,0,down_level); |
|---|
| 103 | if (!gb_source) continue; |
|---|
| 104 | gb_source_data = GB_find(gb_source,"data",0,down_level); |
|---|
| 105 | if (!gb_source_data) continue; |
|---|
| 106 | data = GB_read_string(gb_source_data); |
|---|
| 107 | if (!data) { |
|---|
| 108 | GB_print_error(); |
|---|
| 109 | continue; |
|---|
| 110 | } |
|---|
| 111 | stops += awt_pro_a_nucs_convert( |
|---|
| 112 | data, |
|---|
| 113 | GB_read_string_count(gb_source_data), |
|---|
| 114 | startpos); |
|---|
| 115 | count ++; |
|---|
| 116 | gb_dest_data = GBT_add_data(gb_species,ali_dest,"data", GB_STRING); |
|---|
| 117 | if (!gb_dest_data) return GB_get_error(); |
|---|
| 118 | error = GB_write_string(gb_dest_data,data); |
|---|
| 119 | free(data); |
|---|
| 120 | if (error) break; |
|---|
| 121 | } |
|---|
| 122 | aw_closestatus(); |
|---|
| 123 | if (!error){ |
|---|
| 124 | char msg[256]; |
|---|
| 125 | sprintf(msg, |
|---|
| 126 | "%i taxa converted\n" |
|---|
| 127 | " %f stops per sequence found",count,(double)stops/(double)count); |
|---|
| 128 | aw_message(msg); |
|---|
| 129 | } |
|---|
| 130 | return error; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | void transpro_event(AW_window *aww){ |
|---|
| 134 | AW_root *aw_root = aww->get_root(); |
|---|
| 135 | GB_ERROR error; |
|---|
| 136 | GB_begin_transaction(gb_main); |
|---|
| 137 | |
|---|
| 138 | #if defined(DEBUG) && 0 |
|---|
| 139 | test_AWT_get_codons(); |
|---|
| 140 | #endif |
|---|
| 141 | awt_pro_a_nucs_convert_init(gb_main); |
|---|
| 142 | char *ali_source = aw_root->awar("transpro/source")->read_string(); |
|---|
| 143 | char *ali_dest = aw_root->awar("transpro/dest")->read_string(); |
|---|
| 144 | int startpos = (int)aw_root->awar("transpro/pos")->read_int(); |
|---|
| 145 | |
|---|
| 146 | error = arb_r2a(gb_main, startpos,ali_source,ali_dest); |
|---|
| 147 | if (error) { |
|---|
| 148 | GB_abort_transaction(gb_main); |
|---|
| 149 | aw_message(error); |
|---|
| 150 | }else{ |
|---|
| 151 | GBT_check_data(gb_main,0); |
|---|
| 152 | GB_commit_transaction(gb_main); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | delete ali_source; |
|---|
| 156 | delete ali_dest; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | void nt_trans_cursorpos_changed(AW_root *awr) { |
|---|
| 160 | int pos = awr->awar(AWAR_CURSOR_POSITION)->read_int()-1; |
|---|
| 161 | pos = pos %3; |
|---|
| 162 | awr->awar("transpro/pos")->write_int(pos); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | AW_window *create_dna_2_pro_window(AW_root *root) { |
|---|
| 166 | AWUSE(root); |
|---|
| 167 | GB_transaction dummy(gb_main); |
|---|
| 168 | |
|---|
| 169 | AW_window_simple *aws = new AW_window_simple; |
|---|
| 170 | aws->init( root, "TRANSLATE_DNA_TO_PRO", "TRANSLATE DNA TO PRO", 10, 10 ); |
|---|
| 171 | |
|---|
| 172 | aws->load_xfig("transpro.fig"); |
|---|
| 173 | |
|---|
| 174 | aws->callback( (AW_CB0)AW_POPDOWN); |
|---|
| 175 | aws->at("close"); |
|---|
| 176 | aws->create_button("CLOSE","CLOSE","C"); |
|---|
| 177 | |
|---|
| 178 | aws->callback( AW_POPUP_HELP,(AW_CL)"translate_dna_2_pro.hlp"); |
|---|
| 179 | aws->at("help"); |
|---|
| 180 | aws->create_button("HELP","HELP","H"); |
|---|
| 181 | |
|---|
| 182 | aws->at("source"); |
|---|
| 183 | awt_create_selection_list_on_ad(gb_main,(AW_window *)aws, |
|---|
| 184 | "transpro/source","dna=:rna="); |
|---|
| 185 | |
|---|
| 186 | aws->at("dest"); |
|---|
| 187 | awt_create_selection_list_on_ad(gb_main,(AW_window *)aws, |
|---|
| 188 | "transpro/dest","pro=:ami="); |
|---|
| 189 | |
|---|
| 190 | root->awar_int(AP_PRO_TYPE_AWAR, 0, gb_main); |
|---|
| 191 | aws->at("table"); |
|---|
| 192 | aws->create_option_menu(AP_PRO_TYPE_AWAR); |
|---|
| 193 | for (int code_nr=0; code_nr<AWT_CODON_CODES; code_nr++) { |
|---|
| 194 | aws->insert_option(AWT_get_codon_code_name(code_nr), "", code_nr); |
|---|
| 195 | } |
|---|
| 196 | aws->update_option_menu(); |
|---|
| 197 | |
|---|
| 198 | // { |
|---|
| 199 | // aws->insert_option("Universal","U",AP_UNIVERSAL); |
|---|
| 200 | // aws->insert_option("Mito","M",AP_MITO); |
|---|
| 201 | // aws->insert_option("Vert-Mito","V",AP_VERTMITO); |
|---|
| 202 | // aws->insert_option("Fly-Mito","F",AP_FLYMITO); |
|---|
| 203 | // aws->insert_option("Yeast-Mito","Y",AP_YEASTMITO); |
|---|
| 204 | // aws->update_option_menu(); |
|---|
| 205 | // } |
|---|
| 206 | |
|---|
| 207 | aws->at("pos"); |
|---|
| 208 | aws->create_option_menu("transpro/pos",0,""); |
|---|
| 209 | aws->insert_option( "1", "1", 0 ); |
|---|
| 210 | aws->insert_option( "2", "2", 1 ); |
|---|
| 211 | aws->insert_option( "3", "3", 2 ); |
|---|
| 212 | aws->update_option_menu(); |
|---|
| 213 | |
|---|
| 214 | aws->get_root()->awar_int(AWAR_CURSOR_POSITION)->add_callback(nt_trans_cursorpos_changed); |
|---|
| 215 | |
|---|
| 216 | aws->at("translate"); |
|---|
| 217 | aws->callback(transpro_event); |
|---|
| 218 | aws->highlight(); |
|---|
| 219 | aws->create_button("TRANSLATE","TRANSLATE","T"); |
|---|
| 220 | |
|---|
| 221 | return (AW_window *)aws; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | // Realign a dna alignment with a given protein source |
|---|
| 225 | |
|---|
| 226 | #if 1 |
|---|
| 227 | |
|---|
| 228 | static int synchronizeCodons(const char *proteins, const char *dna, int minCatchUp, int maxCatchUp, int *foundCatchUp, |
|---|
| 229 | const AWT_allowedCode& initially_allowed_code, AWT_allowedCode& allowed_code_left) { |
|---|
| 230 | |
|---|
| 231 | for (int catchUp=minCatchUp; catchUp<=maxCatchUp; catchUp++) { |
|---|
| 232 | const char *dna_start = dna+catchUp; |
|---|
| 233 | AWT_allowedCode allowed_code; |
|---|
| 234 | allowed_code = initially_allowed_code; |
|---|
| 235 | |
|---|
| 236 | for (int p=0; ; p++) { |
|---|
| 237 | char prot = proteins[p]; |
|---|
| 238 | |
|---|
| 239 | if (!prot) { // all proteins were synchronized |
|---|
| 240 | *foundCatchUp = catchUp; |
|---|
| 241 | return 1; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | if (!AWT_is_codon(prot, dna_start, allowed_code, allowed_code_left)) break; |
|---|
| 245 | |
|---|
| 246 | allowed_code = allowed_code_left; // if synchronized: use left codes as allowed codes! |
|---|
| 247 | dna_start += 3; |
|---|
| 248 | } |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | return 0; |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | #define SYNC_LENGTH 4 |
|---|
| 255 | // every X in amino-alignment, it represents 1 to 3 bases in DNA-Alignment |
|---|
| 256 | // SYNC_LENGTH is the # of codons which will be syncronized (ahead!) |
|---|
| 257 | // before deciding "X was realigned correctly" |
|---|
| 258 | |
|---|
| 259 | GB_ERROR arb_transdna(GBDATA *gbmain, char *ali_source, char *ali_dest) |
|---|
| 260 | { |
|---|
| 261 | // GBDATA *gb_source; |
|---|
| 262 | // GBDATA *gb_dest; |
|---|
| 263 | // GBDATA *gb_species; |
|---|
| 264 | // GBDATA *gb_source_data; |
|---|
| 265 | // GBDATA *gb_dest_data; |
|---|
| 266 | // GB_ERROR error; |
|---|
| 267 | // char *source; |
|---|
| 268 | // char *dest; |
|---|
| 269 | // char *buffer; |
|---|
| 270 | |
|---|
| 271 | AWT_initialize_codon_tables(); |
|---|
| 272 | #if defined(DEBUG) && 0 |
|---|
| 273 | AWT_dump_codons(); |
|---|
| 274 | #endif |
|---|
| 275 | |
|---|
| 276 | GBDATA *gb_source = GBT_get_alignment(gbmain,ali_source); if (!gb_source) return "Please select a valid source alignment"; |
|---|
| 277 | GBDATA *gb_dest = GBT_get_alignment(gbmain,ali_dest); if (!gb_dest) return "Please select a valid destination alignment"; |
|---|
| 278 | |
|---|
| 279 | long ali_len = GBT_get_alignment_len(gbmain,ali_dest); |
|---|
| 280 | GB_ERROR error = 0; |
|---|
| 281 | |
|---|
| 282 | aw_openstatus("Re-aligner"); |
|---|
| 283 | int no_of_marked_species = GBT_count_marked_species(gbmain); |
|---|
| 284 | int no_of_realigned_species = 0; |
|---|
| 285 | int ignore_fail_pos = 0; |
|---|
| 286 | |
|---|
| 287 | for (GBDATA *gb_species = GBT_first_marked_species(gbmain); |
|---|
| 288 | !error && gb_species; |
|---|
| 289 | gb_species = GBT_next_marked_species(gb_species) ) { |
|---|
| 290 | |
|---|
| 291 | { |
|---|
| 292 | char stat[200]; |
|---|
| 293 | |
|---|
| 294 | sprintf(stat, "Re-aligning #%i of %i ...", no_of_realigned_species+1, no_of_marked_species); |
|---|
| 295 | aw_status(stat); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | gb_source = GB_find(gb_species,ali_source,0,down_level); if (!gb_source) continue; |
|---|
| 299 | GBDATA *gb_source_data = GB_find(gb_source,"data",0,down_level);if (!gb_source_data) continue; |
|---|
| 300 | gb_dest = GB_find(gb_species,ali_dest,0,down_level); if (!gb_dest) continue; |
|---|
| 301 | GBDATA *gb_dest_data = GB_find(gb_dest,"data",0,down_level); if (!gb_dest_data) continue; |
|---|
| 302 | |
|---|
| 303 | char *source = GB_read_string(gb_source_data); if (!source) { GB_print_error(); continue; } |
|---|
| 304 | char *dest = GB_read_string(gb_dest_data); if (!dest) { GB_print_error(); continue; } |
|---|
| 305 | |
|---|
| 306 | long source_len = GB_read_string_count(gb_source_data); |
|---|
| 307 | long dest_len = GB_read_string_count(gb_dest_data); |
|---|
| 308 | |
|---|
| 309 | // compress destination DNA (=remove align-characters): |
|---|
| 310 | char *compressed_dest = (char*)malloc(dest_len+1); |
|---|
| 311 | { |
|---|
| 312 | char *f = dest; |
|---|
| 313 | char *t = compressed_dest; |
|---|
| 314 | |
|---|
| 315 | while (1) { |
|---|
| 316 | char c = *f++; |
|---|
| 317 | if (!c) break; |
|---|
| 318 | if (c!='.' && c!='-') *t++ = c; |
|---|
| 319 | } |
|---|
| 320 | *t = 0; |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | int failed = 0; |
|---|
| 324 | const char *fail_reason = 0; |
|---|
| 325 | |
|---|
| 326 | char *buffer = (char*)malloc(ali_len+1); |
|---|
| 327 | if (ali_len<source_len*3) { |
|---|
| 328 | failed = 1; |
|---|
| 329 | fail_reason = GBS_global_string("Alignment '%s' is too short (increase its length at ARB_NT/Sequence/Admin to %li)", ali_dest, source_len*3L); |
|---|
| 330 | ignore_fail_pos = 1; |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | AWT_allowedCode allowed_code; |
|---|
| 334 | |
|---|
| 335 | char *d = compressed_dest; |
|---|
| 336 | char *s = source; |
|---|
| 337 | |
|---|
| 338 | if (!failed) { |
|---|
| 339 | char *p = buffer; |
|---|
| 340 | int x_count = 0; |
|---|
| 341 | const char *x_start = 0; |
|---|
| 342 | |
|---|
| 343 | for (;;) { |
|---|
| 344 | char c = *s++; |
|---|
| 345 | if (!c) { |
|---|
| 346 | if (x_count) { |
|---|
| 347 | int off = -(x_count*3); |
|---|
| 348 | while (d[0]) { |
|---|
| 349 | p[off++] = *d++; |
|---|
| 350 | } |
|---|
| 351 | } |
|---|
| 352 | break; |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | if (c=='.' || c=='-') { |
|---|
| 356 | p[0] = p[1] = p[2] = c; |
|---|
| 357 | p += 3; |
|---|
| 358 | } |
|---|
| 359 | else if (toupper(c)=='X') { // one X represents 1 to 3 DNAs |
|---|
| 360 | x_start = s-1; |
|---|
| 361 | x_count = 1; |
|---|
| 362 | int gap_count = 0; |
|---|
| 363 | |
|---|
| 364 | for (;;) { |
|---|
| 365 | char c2 = toupper(s[0]); |
|---|
| 366 | |
|---|
| 367 | if (c2=='X') { |
|---|
| 368 | x_count++; |
|---|
| 369 | } |
|---|
| 370 | else { |
|---|
| 371 | if (c2!='.' && c2!='-') break; |
|---|
| 372 | gap_count++; |
|---|
| 373 | } |
|---|
| 374 | s++; |
|---|
| 375 | } |
|---|
| 376 | |
|---|
| 377 | int setgap = (x_count+gap_count)*3; |
|---|
| 378 | memset(p, '.', setgap); |
|---|
| 379 | p += setgap; |
|---|
| 380 | } |
|---|
| 381 | else { |
|---|
| 382 | AWT_allowedCode allowed_code_left; |
|---|
| 383 | |
|---|
| 384 | if (x_count) { // synchronize |
|---|
| 385 | char protein[SYNC_LENGTH+1]; |
|---|
| 386 | int count; |
|---|
| 387 | { |
|---|
| 388 | int off; |
|---|
| 389 | |
|---|
| 390 | protein[0] = toupper(c); |
|---|
| 391 | for (count=1,off=0; count<SYNC_LENGTH; off++) { |
|---|
| 392 | char c2 = s[off]; |
|---|
| 393 | |
|---|
| 394 | if (c2!='.' && c2!='-') { |
|---|
| 395 | c2 = toupper(c2); |
|---|
| 396 | if (c2=='X') break; // can't sync X |
|---|
| 397 | protein[count++] = c2; |
|---|
| 398 | } |
|---|
| 399 | } |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | gb_assert(count>=1); |
|---|
| 403 | protein[count] = 0; |
|---|
| 404 | |
|---|
| 405 | int catchUp; |
|---|
| 406 | if (count<SYNC_LENGTH) { |
|---|
| 407 | int sync_possibilities = 0; |
|---|
| 408 | int *sync_possible_with_catchup = new int[x_count*3+1]; |
|---|
| 409 | int maxCatchup = x_count*3; |
|---|
| 410 | |
|---|
| 411 | catchUp = x_count-1; |
|---|
| 412 | for (;;) { |
|---|
| 413 | if (!synchronizeCodons(protein, d, catchUp+1, maxCatchup, &catchUp, allowed_code, allowed_code_left)) { |
|---|
| 414 | break; |
|---|
| 415 | } |
|---|
| 416 | sync_possible_with_catchup[sync_possibilities++] = catchUp; |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | if (sync_possibilities==0) { |
|---|
| 420 | delete sync_possible_with_catchup; |
|---|
| 421 | failed = 1; |
|---|
| 422 | fail_reason = "Can't syncronize after 'X'"; |
|---|
| 423 | break; |
|---|
| 424 | } |
|---|
| 425 | if (sync_possibilities>1) { |
|---|
| 426 | delete sync_possible_with_catchup; |
|---|
| 427 | failed = 1; |
|---|
| 428 | fail_reason = "Not enough data behind 'X' (please contact ARB-Support)"; |
|---|
| 429 | break; |
|---|
| 430 | } |
|---|
| 431 | |
|---|
| 432 | gb_assert(sync_possibilities==1); |
|---|
| 433 | catchUp = sync_possible_with_catchup[0]; |
|---|
| 434 | delete sync_possible_with_catchup; |
|---|
| 435 | } |
|---|
| 436 | else if (!synchronizeCodons(protein, d, x_count, x_count*3, &catchUp, allowed_code, allowed_code_left)) { |
|---|
| 437 | failed = 1; |
|---|
| 438 | fail_reason = "Can't syncronize after 'X'"; |
|---|
| 439 | break; |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | allowed_code = allowed_code_left; |
|---|
| 443 | |
|---|
| 444 | // copy 'catchUp' characters (they are the content of the found Xs): |
|---|
| 445 | { |
|---|
| 446 | const char *after = s-1; |
|---|
| 447 | const char *i; |
|---|
| 448 | int off = int(after-x_start); |
|---|
| 449 | gb_assert(off>=x_count); |
|---|
| 450 | off = -(off*3); |
|---|
| 451 | int x_rest = x_count; |
|---|
| 452 | |
|---|
| 453 | for (i=x_start; i<after; i++) { |
|---|
| 454 | switch (i[0]) { |
|---|
| 455 | case 'x': |
|---|
| 456 | case 'X': |
|---|
| 457 | { |
|---|
| 458 | int take_per_X = catchUp/x_rest; |
|---|
| 459 | int o; |
|---|
| 460 | for (o=0; o<3; o++) { |
|---|
| 461 | if (o<take_per_X) { |
|---|
| 462 | p[off++] = *d++; |
|---|
| 463 | } |
|---|
| 464 | else { |
|---|
| 465 | p[off++] = '.'; |
|---|
| 466 | } |
|---|
| 467 | } |
|---|
| 468 | x_rest--; |
|---|
| 469 | break; |
|---|
| 470 | } |
|---|
| 471 | case '.': |
|---|
| 472 | case '-': |
|---|
| 473 | p[off++] = i[0]; |
|---|
| 474 | p[off++] = i[0]; |
|---|
| 475 | p[off++] = i[0]; |
|---|
| 476 | break; |
|---|
| 477 | default: |
|---|
| 478 | gb_assert(0); |
|---|
| 479 | break; |
|---|
| 480 | } |
|---|
| 481 | } |
|---|
| 482 | } |
|---|
| 483 | x_count = 0; |
|---|
| 484 | } |
|---|
| 485 | else { |
|---|
| 486 | if (!AWT_is_codon(c, d, allowed_code, allowed_code_left)) { |
|---|
| 487 | failed = 1; |
|---|
| 488 | fail_reason = "Not a codon"; |
|---|
| 489 | break; |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | allowed_code = allowed_code_left; |
|---|
| 493 | } |
|---|
| 494 | |
|---|
| 495 | // copy one codon: |
|---|
| 496 | p[0] = d[0]; |
|---|
| 497 | p[1] = d[1]; |
|---|
| 498 | p[2] = d[2]; |
|---|
| 499 | |
|---|
| 500 | p += 3; |
|---|
| 501 | d += 3; |
|---|
| 502 | } |
|---|
| 503 | } |
|---|
| 504 | |
|---|
| 505 | if (!failed) { |
|---|
| 506 | int len = p-buffer; |
|---|
| 507 | int rest = ali_len-len; |
|---|
| 508 | |
|---|
| 509 | memset(p, '.', rest); |
|---|
| 510 | p += rest; |
|---|
| 511 | p[0] = 0; |
|---|
| 512 | } |
|---|
| 513 | } |
|---|
| 514 | |
|---|
| 515 | if (failed) { |
|---|
| 516 | int source_fail_pos = (s-1)-source+1; |
|---|
| 517 | int dest_fail_pos = 0; |
|---|
| 518 | { |
|---|
| 519 | int fail_d_base_count = d-compressed_dest; |
|---|
| 520 | char *dp = dest; |
|---|
| 521 | |
|---|
| 522 | for (;;) { |
|---|
| 523 | char c = *dp++; |
|---|
| 524 | |
|---|
| 525 | if (!c) { |
|---|
| 526 | gb_assert(c); |
|---|
| 527 | break; |
|---|
| 528 | } |
|---|
| 529 | if (c!='.' && c!='-') { |
|---|
| 530 | if (!fail_d_base_count) { |
|---|
| 531 | dest_fail_pos = (dp-1)-dest+1; |
|---|
| 532 | break; |
|---|
| 533 | } |
|---|
| 534 | fail_d_base_count--; |
|---|
| 535 | } |
|---|
| 536 | } |
|---|
| 537 | } |
|---|
| 538 | |
|---|
| 539 | { |
|---|
| 540 | char *name = GBT_read_name(gb_species); |
|---|
| 541 | if (!name) name = strdup("(unknown species)"); |
|---|
| 542 | |
|---|
| 543 | char *dup_fail_reason = strdup(fail_reason); // otherwise it may be destroyed by GBS_global_string |
|---|
| 544 | aw_message(GBS_global_string("Automatic re-align failed for '%s'", name)); |
|---|
| 545 | |
|---|
| 546 | if (ignore_fail_pos) { |
|---|
| 547 | aw_message(GBS_global_string("Reason: %s", dup_fail_reason)); |
|---|
| 548 | } |
|---|
| 549 | else { |
|---|
| 550 | aw_message(GBS_global_string("Reason: %s at %s:%i / %s:%i", dup_fail_reason, ali_source, source_fail_pos, ali_dest, dest_fail_pos)); |
|---|
| 551 | } |
|---|
| 552 | |
|---|
| 553 | free(dup_fail_reason); |
|---|
| 554 | free(name); |
|---|
| 555 | } |
|---|
| 556 | } |
|---|
| 557 | else { |
|---|
| 558 | gb_assert(strlen(buffer)==(unsigned)ali_len); |
|---|
| 559 | error = GB_write_string(gb_dest_data,buffer); |
|---|
| 560 | } |
|---|
| 561 | |
|---|
| 562 | free(buffer); |
|---|
| 563 | free(compressed_dest); |
|---|
| 564 | free(dest); |
|---|
| 565 | free(source); |
|---|
| 566 | |
|---|
| 567 | no_of_realigned_species++; |
|---|
| 568 | GB_status(double(no_of_realigned_species)/double(no_of_marked_species)); |
|---|
| 569 | } |
|---|
| 570 | aw_closestatus(); |
|---|
| 571 | if (error) return error; |
|---|
| 572 | |
|---|
| 573 | GBT_check_data(gbmain,ali_dest); |
|---|
| 574 | |
|---|
| 575 | return 0; |
|---|
| 576 | } |
|---|
| 577 | |
|---|
| 578 | #undef SYNC_LENGTH |
|---|
| 579 | |
|---|
| 580 | |
|---|
| 581 | #else |
|---|
| 582 | // old version: |
|---|
| 583 | GB_ERROR arb_transdna(GBDATA *gbmain, char *ali_source, char *ali_dest) |
|---|
| 584 | { |
|---|
| 585 | GBDATA *gb_source; |
|---|
| 586 | GBDATA *gb_dest; |
|---|
| 587 | GBDATA *gb_species; |
|---|
| 588 | GBDATA *gb_source_data; |
|---|
| 589 | GBDATA *gb_dest_data; |
|---|
| 590 | GB_ERROR error; |
|---|
| 591 | char *source; |
|---|
| 592 | char *dest; |
|---|
| 593 | char *buffer; |
|---|
| 594 | |
|---|
| 595 | gb_source = GBT_get_alignment(gbmain,ali_source); |
|---|
| 596 | if (!gb_source) return "Please select a valid source alignment"; |
|---|
| 597 | gb_dest = GBT_get_alignment(gbmain,ali_dest); |
|---|
| 598 | if (!gb_dest) return "Please select a valid destination alignment"; |
|---|
| 599 | long dest_len = GBT_get_alignment_len(gbmain,ali_dest); |
|---|
| 600 | |
|---|
| 601 | for (gb_species = GBT_first_marked_species(gbmain); |
|---|
| 602 | gb_species; |
|---|
| 603 | gb_species = GBT_next_marked_species(gb_species) ){ |
|---|
| 604 | |
|---|
| 605 | gb_source = GB_find(gb_species,ali_source,0,down_level); if (!gb_source) continue; |
|---|
| 606 | gb_source_data = GB_find(gb_source,"data",0,down_level); if (!gb_source_data) continue; |
|---|
| 607 | gb_dest = GB_find(gb_species,ali_dest,0,down_level); if (!gb_dest) continue; |
|---|
| 608 | gb_dest_data = GB_find(gb_dest,"data",0,down_level); if (!gb_dest_data) continue; |
|---|
| 609 | |
|---|
| 610 | source = GB_read_string(gb_source_data); if (!source) { GB_print_error(); continue; } |
|---|
| 611 | dest = GB_read_string(gb_dest_data); if (!dest) { GB_print_error(); continue; } |
|---|
| 612 | |
|---|
| 613 | buffer = (char *)calloc(sizeof(char), (size_t)(GB_read_string_count(gb_source_data)*3 + GB_read_string_count(gb_dest_data) + dest_len ) ); |
|---|
| 614 | |
|---|
| 615 | char *p = buffer; |
|---|
| 616 | char *s = source; |
|---|
| 617 | char *d = dest; |
|---|
| 618 | |
|---|
| 619 | char *lastDNA = 0; // pointer to buffer |
|---|
| 620 | |
|---|
| 621 | for (; *s; s++){ /* source is pro */ |
|---|
| 622 | /* insert triple '.' for every '.' |
|---|
| 623 | insert triple '-' for every - |
|---|
| 624 | insert triple for rest */ |
|---|
| 625 | switch (*s){ |
|---|
| 626 | case '.': |
|---|
| 627 | case '-': |
|---|
| 628 | p[0] = p[1] = p[2] = s[0]; |
|---|
| 629 | p += 3; |
|---|
| 630 | break; |
|---|
| 631 | |
|---|
| 632 | default: |
|---|
| 633 | while (*d && (*d=='.' || *d =='-')) d++; |
|---|
| 634 | if(!*d) break; |
|---|
| 635 | |
|---|
| 636 | lastDNA = p; |
|---|
| 637 | *(p++) = *(d++); /* copy dna to dna */ |
|---|
| 638 | |
|---|
| 639 | while (*d && (*d=='.' || *d =='-')) d++; |
|---|
| 640 | if(!*d) break; |
|---|
| 641 | |
|---|
| 642 | lastDNA = p; |
|---|
| 643 | *(p++) = *(d++); /* copy dna to dna */ |
|---|
| 644 | |
|---|
| 645 | while (*d && (*d=='.' || *d =='-')) d++; |
|---|
| 646 | if(!*d) break; |
|---|
| 647 | |
|---|
| 648 | lastDNA = p; |
|---|
| 649 | *(p++) = *(d++); /* copy dna to dna */ |
|---|
| 650 | |
|---|
| 651 | break; |
|---|
| 652 | } |
|---|
| 653 | } |
|---|
| 654 | |
|---|
| 655 | p = lastDNA+1; |
|---|
| 656 | while (*d) { |
|---|
| 657 | if (*d == '.' || *d == '-') { |
|---|
| 658 | d++; |
|---|
| 659 | }else{ |
|---|
| 660 | *(p++) = *(d++); // append rest characters |
|---|
| 661 | } |
|---|
| 662 | } |
|---|
| 663 | |
|---|
| 664 | while ((p-buffer)<dest_len) *(p++) = '.'; // append . |
|---|
| 665 | *p = 0; |
|---|
| 666 | |
|---|
| 667 | gb_assert(strlen(buffer)==dest_len); |
|---|
| 668 | |
|---|
| 669 | error = GB_write_string(gb_dest_data,buffer); |
|---|
| 670 | |
|---|
| 671 | free(source); |
|---|
| 672 | free(dest); |
|---|
| 673 | free(buffer); |
|---|
| 674 | |
|---|
| 675 | if (error) return error; |
|---|
| 676 | } |
|---|
| 677 | |
|---|
| 678 | GBT_check_data(gbmain,ali_dest); |
|---|
| 679 | |
|---|
| 680 | return 0; |
|---|
| 681 | } |
|---|
| 682 | |
|---|
| 683 | #endif |
|---|
| 684 | |
|---|
| 685 | |
|---|
| 686 | void transdna_event(AW_window *aww) |
|---|
| 687 | { |
|---|
| 688 | AW_root *aw_root = aww->get_root(); |
|---|
| 689 | GB_ERROR error; |
|---|
| 690 | |
|---|
| 691 | char *ali_source = aw_root->awar("transpro/dest")->read_string(); |
|---|
| 692 | char *ali_dest = aw_root->awar("transpro/source")->read_string(); |
|---|
| 693 | GB_begin_transaction(gb_main); |
|---|
| 694 | error = arb_transdna(gb_main,ali_source,ali_dest); |
|---|
| 695 | if (error) { |
|---|
| 696 | GB_abort_transaction(gb_main); |
|---|
| 697 | aw_message(error,"OK"); |
|---|
| 698 | }else{ |
|---|
| 699 | GBT_check_data(gb_main,ali_dest); |
|---|
| 700 | GB_commit_transaction(gb_main); |
|---|
| 701 | } |
|---|
| 702 | delete ali_source; |
|---|
| 703 | delete ali_dest; |
|---|
| 704 | |
|---|
| 705 | } |
|---|
| 706 | |
|---|
| 707 | AW_window *create_realign_dna_window(AW_root *root) |
|---|
| 708 | { |
|---|
| 709 | AWUSE(root); |
|---|
| 710 | |
|---|
| 711 | AW_window_simple *aws = new AW_window_simple; |
|---|
| 712 | aws->init( root, "REALIGN_DNA", "REALIGN DNA", 10, 10 ); |
|---|
| 713 | |
|---|
| 714 | aws->load_xfig("transdna.fig"); |
|---|
| 715 | |
|---|
| 716 | aws->callback( (AW_CB0)AW_POPDOWN); |
|---|
| 717 | aws->at("close"); |
|---|
| 718 | aws->create_button("CLOSE","CLOSE","C"); |
|---|
| 719 | |
|---|
| 720 | aws->callback( AW_POPUP_HELP,(AW_CL)"realign_dna.hlp"); |
|---|
| 721 | aws->at("help"); |
|---|
| 722 | aws->create_button("HELP","HELP","H"); |
|---|
| 723 | |
|---|
| 724 | aws->at("source"); |
|---|
| 725 | awt_create_selection_list_on_ad(gb_main,(AW_window *)aws, |
|---|
| 726 | "transpro/source","dna=:rna="); |
|---|
| 727 | aws->at("dest"); |
|---|
| 728 | awt_create_selection_list_on_ad(gb_main,(AW_window *)aws, |
|---|
| 729 | "transpro/dest","pro=:ami="); |
|---|
| 730 | |
|---|
| 731 | aws->at("realign"); |
|---|
| 732 | aws->callback(transdna_event); |
|---|
| 733 | aws->highlight(); |
|---|
| 734 | aws->create_button("REALIGN","REALIGN","T"); |
|---|
| 735 | |
|---|
| 736 | return (AW_window *)aws; |
|---|
| 737 | } |
|---|
| 738 | |
|---|
| 739 | |
|---|
| 740 | void create_transpro_menus(AW_window *awmm) |
|---|
| 741 | { |
|---|
| 742 | awmm->insert_menu_topic("dna_2_pro", "Translate Nucleic to Amino Acid ...","T","translate_dna_2_pro.hlp", AWM_PRO, AW_POPUP, (AW_CL)create_dna_2_pro_window, 0 ); |
|---|
| 743 | awmm->insert_menu_topic("realign_dna", "Realign Nucleic Acid according to Aligned Protein ...","r","realign_dna.hlp", AWM_PRO, AW_POPUP, (AW_CL)create_realign_dna_window, 0 ); |
|---|
| 744 | } |
|---|
| 745 | |
|---|
| 746 | void create_transpro_variables(AW_root *root,AW_default db1) |
|---|
| 747 | { |
|---|
| 748 | root->awar_string( "transpro/source", "" , db1); |
|---|
| 749 | root->awar_string( "transpro/dest", "" , db1); |
|---|
| 750 | root->awar_int( "transpro/pos", 0 , db1); |
|---|
| 751 | } |
|---|