source: tags/cvs_2_svn/AWTI/AWTI_import.cxx

Last change on this file was 5350, checked in by westram, 16 years ago
  • fixed use of GBS_strstruct functions
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 37.2 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <string.h>
5#include <memory.h>
6#include <limits.h>
7
8#include <map>
9#include <string>
10
11#include <arbdb.h>
12#include <arbdbt.h>
13#include <aw_root.hxx>
14#include <aw_device.hxx>
15#include <aw_window.hxx>
16#include <aw_global.hxx>
17#include <awt.hxx>
18#include <awt_advice.hxx>
19#include <awt_changekey.hxx>
20#include <awt_sel_boxes.hxx>
21#include <GEN.hxx>
22#include <GAGenomImport.h>
23#include <AW_rename.hxx>
24#include <awti_import.hxx>
25#include <awti_imp_local.hxx>
26
27#define awti_assert(cond) arb_assert(cond)
28
29using namespace std;
30
31struct awtcig_struct awtcig;
32#define MAX_COMMENT_LINES 2000
33
34// -------------------------------------------------------------------
35//      static char * awtc_fgets(char *s, int size, FILE *stream)
36// -------------------------------------------------------------------
37// same as fgets but also works with file in MACOS format
38
39static char *awtc_fgets(char *s, int size, FILE *stream) {
40    int i;
41    for (i = 0; i<(size-1); ++i) {
42        int byte = fgetc(stream);
43        if (byte == EOF) {
44            if (i == 0) return 0;
45            break;
46        }
47
48        s[i] = byte;
49        if (byte == '\n' || byte == '\r') break;
50    }
51    s[i] = 0;
52
53    return s;
54}
55
56AW_BOOL  awtc_read_string_pair(FILE *in, char *&s1,char *&s2)
57{
58    const int BUFSIZE = 8000;
59    char buffer[BUFSIZE];
60    char *res = awtc_fgets(&buffer[0], BUFSIZE-1, in);
61    free(s1);
62    free(s2);
63    s1 = 0;
64    s2 = 0;
65    if (!res) return AW_TRUE;
66
67    char *p = buffer;
68    while (*p == ' ' || *p == '\t') p++;
69    if (*p == '#') return AW_FALSE;
70
71    int len = strlen(p)-1;
72    while  (len >= 0 && (
73                         p[len] == '\n' || p[len] == ' ' || p[len] == '\t' || p[len] == 13) ) p[len--] = 0;
74
75    if (!*p) return AW_FALSE;
76    char *e = 0;
77    e = strpbrk(p," \t");
78
79    if (e) {
80        *(e++) = 0;
81        s1 = strdup(p);
82        while (*e == ' ' || *e == '\t') e++;
83        if (*e == '"') {
84            char *k = strrchr(e,'"');
85            if (k!=e) {
86                e++;
87                *k=0;
88            }
89        }
90        s2 = strdup(e);
91    }else{
92        s1 = strdup(p);
93    }
94
95    return AW_FALSE;
96}
97
98
99GB_ERROR awtc_read_import_format(char *file)
100{
101    FILE *in = 0;
102    {
103        char *fullfile = AWT_unfold_path(file,"ARBHOME");
104
105        in = fopen(fullfile,"r");
106        free(fullfile);
107    }
108
109    if (!in) return "Form not readable (select a form or check permissions)";
110    struct input_format_struct   *ifo;
111    struct input_format_per_line *pl = 0;
112    char                         *s1 = 0,*s2=0;
113
114    if (awtcig.ifo) { delete awtcig.ifo; awtcig.ifo = 0; }
115    ifo = awtcig.ifo = new input_format_struct;
116
117    int lineNumber = 0;
118
119    while (!awtc_read_string_pair(in,s1,s2)){
120        lineNumber++;
121        if (!s1) {
122            continue;
123        }else if (!strcmp(s1,"AUTODETECT")) {
124            ifo->autodetect = s2; s2 = 0;
125        }else if (!strcmp(s1,"SYSTEM")) {
126            ifo->system = s2; s2 = 0;
127        }else if (!strcmp(s1,"NEW_FORMAT")) {
128            ifo->new_format = s2; s2 = 0;
129        }else if (!strcmp(s1,"KEYWIDTH")) {
130            ifo->tab = atoi(s2);
131        }else if (!strcmp(s1,"BEGIN")) {
132            ifo->begin = s2; s2 = 0;
133        }else if (!strcmp(s1,"SEQUENCESTART")) {
134            ifo->sequencestart = s2; s2 = 0;
135            ifo->read_this_sequence_line_too = 1;
136        }else if (!strcmp(s1,"SEQUENCEAFTER")) {
137            ifo->sequencestart = s2; s2 = 0;
138            ifo->read_this_sequence_line_too = 0;
139        }else if (!strcmp(s1,"FILETAG")) {
140            ifo->filetag = s2; s2 = 0;
141        }else if (!strcmp(s1,"SEQUENCESRT")) {
142            ifo->sequencesrt = s2; s2 = 0;
143        }else if (!strcmp(s1,"SEQUENCEACI")) {
144            ifo->sequenceaci = s2; s2 = 0;
145        }else if (!strcmp(s1,"SEQUENCECOLUMN")) {
146            ifo->sequencecolumn = atoi(s2);
147        }else if (!strcmp(s1,"SEQUENCEEND")) {
148            ifo->sequenceend = s2; s2 = 0;
149        }else if (!strcmp(s1,"END")) {
150            ifo->end = s2; s2 = 0;
151        }else if (!strcmp(s1,"CREATE_ACC_FROM_SEQUENCE")) {
152            ifo->autocreateacc = 1;
153        }else if (!strcmp(s1,"DONT_GEN_NAMES")) {
154            ifo->noautonames = 1;
155        }else if (!strcmp(s1,"MATCH")) {
156            pl             = (struct input_format_per_line *)GB_calloc(1, sizeof(struct input_format_per_line));
157            pl->start_line = lineNumber;
158            pl->next       = ifo->pl; // this concatenates the filters to the front -> that is corrected below
159            ifo->pl        = pl;
160            pl->match      = GBS_remove_escape(s2); free(s2); s2 = 0;
161        }else if (pl && !strcmp(s1,"SRT")) {
162            pl->srt = s2;s2= 0;
163        }else if (pl && !strcmp(s1,"ACI")) {
164            pl->aci = s2;s2= 0;
165        }else if (pl && !strcmp(s1,"WRITE")) {
166            pl->write = s2;s2= 0;
167        }else if (pl && !strcmp(s1,"APPEND")) {
168            pl->append = s2;s2= 0;
169        }else if (pl && !strcmp(s1,"SETVAR")) {
170            pl->setvar      = s2;
171            if (strlen(s2) != 1 || s2[0]<'a' || s2[0]>'z') aw_message("Allowed variable names are a-z");
172            s2= 0;
173        }else if (pl && !strcmp(s1,"TAG")) {
174            pl->tag = GB_strdup(s2);
175            //          pl->tag = GBS_string_2_key_with_exclusions(s2, "$"); // allow $ to occur in string
176            if (!strlen(pl->tag)){
177                delete pl->tag;
178                pl->tag = 0;
179            }
180        }else{
181            char *fullfile = AWT_unfold_path(file,"ARBHOME");
182
183            aw_message(GBS_global_string("Unknown%s command in import format file '%s'  command '%s'\n",
184                                         (pl ? "" : " (or wrong placed)"), fullfile, s1));
185            free(fullfile);
186        }
187    }
188
189    free(s2);
190    free(s1);
191
192    // reverse order of match list (was appended backwards during creation)
193    if (ifo->pl) ifo->pl = ifo->pl->reverse(0);
194
195    fclose(in);
196    return 0;
197}
198
199input_format_struct::input_format_struct(void){
200    memset((char *)this,0,sizeof(input_format_struct));
201}
202
203input_format_struct::~input_format_struct(void)
204{
205    struct input_format_struct *ifo= this;
206    struct input_format_per_line *pl1 = 0;
207    struct input_format_per_line *pl2 = 0;
208
209    for (pl1 = ifo->pl; pl1; pl1=pl2){
210        pl2 = pl1->next;
211        free(pl1->match);
212        free(pl1->srt);
213        free(pl1->aci);
214        free(pl1->tag);
215        free(pl1->append);
216        free(pl1->write);
217        free(pl1);
218    }
219
220    free(ifo->autodetect);
221    free(ifo->system);
222    free(ifo->new_format);
223    free(ifo->begin);
224    free(ifo->sequencestart);
225    free(ifo->sequenceend);
226    free(ifo->sequencesrt);
227    free(ifo->sequenceaci);
228    free(ifo->end);
229    free(ifo->b1);
230    free(ifo->b2);
231}
232
233void awtc_check_input_format(AW_window *aww)
234{
235    AW_root *root = aww->get_root();
236    char **files = GBS_read_dir(0,"import/*.ift");
237    char **file;
238    char    buffer[AWTC_IMPORT_CHECK_BUFFER_SIZE+10];
239    for (file = files; *file; file++) {
240        root->awar(AWAR_FORM"/file_name")->write_string(*file);
241
242        GB_ERROR error     = awtc_read_import_format(*file);
243        if (error) {
244            aw_message(error);
245        }
246        if (!awtcig.ifo->autodetect) continue;      // not detectable
247        char *autodetect = GBS_remove_escape(awtcig.ifo->autodetect);
248        delete awtcig.ifo; awtcig.ifo = 0;
249
250        FILE *in = 0;
251        {
252            char com[1024];
253            char *f = root->awar(AWAR_FILE)->read_string();
254
255            if (f[0]) {
256                sprintf(com,"cat %s 2>/dev/null",f);
257                in = popen(com,"r");
258            }
259            free(f);
260            if (!in) {
261                aw_message( "Cannot open any input file");
262                *file = 0;
263                break;
264            }
265        }
266        if (in) {
267            int size = fread(buffer,1,AWTC_IMPORT_CHECK_BUFFER_SIZE,in);
268            pclose(in);
269            if (size>=0){
270                buffer[size] = 0;
271                if (GBS_string_matches(buffer,autodetect,GB_MIND_CASE)){  // format found
272                    free(autodetect);
273                    break;
274                }
275            }
276        }
277        free(autodetect);
278    }
279    if (!*file) {       // nothing found
280        root->awar(AWAR_FORM"/file_name")->write_string("unknown.ift");
281    }
282
283    GBT_free_names(files);
284}
285
286static const char *awtc_next_file(void){
287    if (awtcig.in) fclose(awtcig.in);
288    if (!awtcig.current_file) awtcig.current_file = awtcig.filenames;
289    while (*awtcig.current_file) {
290        char *origin_file_name;
291        origin_file_name = *(awtcig.current_file++);
292        char *sorigin= strrchr(origin_file_name,'/');
293        if (!sorigin) sorigin= origin_file_name;
294        else sorigin++;
295        char mid_file_name[1024];
296        char dest_file_name[1024];
297        char srt[1024];
298
299        if (awtcig.ifo2 && awtcig.ifo2->system){
300            {
301                const char *sorigin_nameonly        = strrchr(sorigin, '/');
302                if (!sorigin_nameonly) sorigin_nameonly = sorigin;
303                sprintf(mid_file_name,"/tmp/%s_%i",sorigin_nameonly, getpid());
304            }
305            sprintf(srt,"$<=%s:$>=%s",origin_file_name, mid_file_name);
306            char *sys = GBS_string_eval(awtcig.ifo2->system,srt,0);
307            sprintf(AW_ERROR_BUFFER,"exec '%s'",awtcig.ifo2->system);
308            aw_status(AW_ERROR_BUFFER);
309            if (system(sys)) {
310                sprintf(AW_ERROR_BUFFER,"Error in '%s'",sys);
311                aw_message();
312                delete sys; continue;
313            }
314            free(sys);
315            origin_file_name = mid_file_name;
316        }else{
317            mid_file_name[0] = 0;
318        }
319
320        if (awtcig.ifo->system){
321            {
322                const char *sorigin_nameonly        = strrchr(sorigin, '/');
323                if (!sorigin_nameonly) sorigin_nameonly = sorigin;
324                sprintf(dest_file_name,"/tmp/%s_2_%i",sorigin, getpid());
325            }
326            sprintf(srt,"$<=%s:$>=%s",origin_file_name, dest_file_name);
327            char *sys = GBS_string_eval(awtcig.ifo->system,srt,0);
328            sprintf(AW_ERROR_BUFFER,"Converting File %s",awtcig.ifo->system);
329            aw_status(AW_ERROR_BUFFER);
330            if (system(sys)) {
331                sprintf(AW_ERROR_BUFFER,"Error in '%s'",sys);
332                aw_message();
333                delete sys; continue;
334            }
335            delete sys;
336            origin_file_name = dest_file_name;
337        }else{
338            dest_file_name[0] = 0;
339        }
340
341        awtcig.in = fopen(origin_file_name,"r");
342        if (strlen(mid_file_name)) unlink(mid_file_name);
343        if (strlen(dest_file_name)) unlink(dest_file_name);
344
345        if (awtcig.in) return 0;
346        sprintf(AW_ERROR_BUFFER,"Error: Cannot open file %s\n",awtcig.current_file[-1]);
347        aw_message();
348    }
349    return "last file reached";
350}
351char *awtc_read_line(int tab,char *sequencestart, char *sequenceend){
352    /* two modes:   tab == 0 -> read single lines,
353       different files are seperated by sequenceend,
354       tab != 0 join lines that start after position tab,
355       joined lines are seperated by '|'
356       except lines that match sequencestart
357       (they may be part of sequence if read_this_sequence_line_too = 1 */
358
359    static char *in_queue = 0;      // read data
360    static int b2offset = 0;
361    const int BUFSIZE = 8000;
362    const char *SEPERATOR = "|";    // line seperator
363    struct input_format_struct *ifo;
364    ifo = awtcig.ifo;
365    char *p;
366
367    if (!ifo->b1) ifo->b1 = (char*)calloc(BUFSIZE,1);
368    if (!ifo->b2) ifo->b2 = (char*)calloc(BUFSIZE,1);
369    if (!awtcig.in){
370        if (awtc_next_file()) {
371            if (in_queue) {
372                char *s = in_queue;
373                in_queue = 0;
374                return s;
375            }
376            return 0;
377        }
378    }
379
380
381    if (!tab) {
382        if (in_queue) {
383            char *s = in_queue;
384            in_queue = 0;
385            return s;
386        }
387        p = awtc_fgets(ifo->b1, BUFSIZE-3, awtcig.in);
388        if (!p){
389            sprintf(ifo->b1,"%s",sequenceend);
390            if (awtcig.in) fclose(awtcig.in);awtcig.in= 0;
391            p = ifo->b1;
392        }
393        int len = strlen(p)-1;
394        while (len>=0){
395            if (p[len] =='\n' || p[len] == 13) p[len--] = 0;
396            else break;
397        }
398        return ifo->b1;
399    }
400
401    b2offset = 0;
402    ifo->b2[0] = 0;
403
404    if (in_queue) {
405        b2offset = 0;
406        strncpy(ifo->b2+b2offset, in_queue, BUFSIZE - 4- b2offset);
407        b2offset += strlen(ifo->b2+b2offset);
408        in_queue = 0;
409        if (GBS_string_matches(ifo->b2,sequencestart,GB_MIND_CASE)) return ifo->b2;
410    }
411    while (1) {
412        p = awtc_fgets(ifo->b1, BUFSIZE-3, awtcig.in);
413        if (!p){
414            if (awtcig.in) fclose(awtcig.in);awtcig.in= 0;
415            break;
416        }
417        int len = strlen(p)-1;
418        while (len>=0){
419            if (p[len] =='\n' || p[len] == 13) p[len--] = 0;
420            else break;
421        }
422
423
424        if (GBS_string_matches(ifo->b1,sequencestart,GB_MIND_CASE)){
425            in_queue = ifo->b1;
426            return ifo->b2;
427        }
428
429        int i;
430        for (i=0;i<=tab;i++) if (ifo->b1[i] != ' ') break;
431
432        if (i < tab) {
433            in_queue = ifo->b1;
434            return ifo->b2;
435        }
436        strncpy(ifo->b2+b2offset, SEPERATOR, BUFSIZE - 4- b2offset);
437        b2offset += strlen(ifo->b2+b2offset);
438
439        p = ifo->b1;
440        if (b2offset>0) while (*p==' ') p++;    // delete spaces in second line
441
442        strncpy(ifo->b2+b2offset, p, BUFSIZE - 4- b2offset);
443        b2offset += strlen(ifo->b2+b2offset);
444    }
445    in_queue = 0;
446    return ifo->b2;
447}
448
449static void awtc_write_entry(GBDATA *gbd,const char *key,char *str,const char *tag,int append)
450{
451    GBDATA *gbk;
452    int len, taglen;
453    char *buf;
454    if (!gbd) return;
455    while (str[0] == ' '|| str[0] == '\t'|| str[0] == '|') str++;
456    len = strlen(str) -1;
457    while (len >=0 && (
458                       str[len] == ' '|| str[len] == '\t'|| str[len] == '|' || str[len] == 13)) len--;
459    str[len+1] = 0;
460    if (!str[0]) return;
461
462    gbk = GB_entry(gbd,key);
463    if (!gbk || !append){
464        if (!gbk) gbk=GB_create(gbd,key,GB_STRING);
465
466        if (tag){
467            GBS_strstruct *s = GBS_stropen(10000);
468            GBS_chrcat(s,'[');
469            GBS_strcat(s,tag);
470            GBS_strcat(s,"] ");
471            GBS_strcat(s,str);
472            char *val = GBS_strclose(s);
473            GB_write_string(gbk,val);
474            free(val);
475        }else{
476            if (!strcmp(key,"name")){
477                char *nstr = GBT_create_unique_species_name(awtcig.gb_main,str);
478                GB_write_string(gbk,nstr);
479                free(nstr);
480            }else{
481                GB_write_string(gbk,str);
482            }
483        }
484        return;
485    }
486
487    const char *strin  = GB_read_char_pntr(gbk);
488    len    = strlen(str) + strlen(strin);
489    taglen = tag ? (strlen(tag)+2) : 0;
490    buf    = (char *)GB_calloc(sizeof(char),len+2+taglen+1);
491
492    if (tag) {
493        char *regexp = (char*)GB_calloc(sizeof(char), taglen+3);
494        sprintf(regexp, "*[%s]*", tag);
495
496        if (!GBS_string_matches(strin, regexp, GB_IGNORE_CASE)) { // if tag does not exist yet
497            sprintf(buf,"%s [%s] %s", strin, tag, str); // prefix with tag
498        }
499        free(regexp);
500    }
501
502    if (buf[0] == 0) {
503        sprintf(buf,"%s %s",strin,str);
504    }
505
506    GB_write_string(gbk,buf);
507    free(buf);
508    return;
509}
510
511typedef map<char, string> SetVariables;
512
513static string expandSetVariables(const SetVariables& variables, const string& source, bool& error_occurred) {
514    string                 dest;
515    string::const_iterator norm_start = source.begin();
516    string::const_iterator p          = norm_start;
517    error_occurred                    = false;
518
519    while (p != source.end()) {
520        if (*p == '$') {
521            ++p;
522            if (*p == '$') { // '$$' -> '$'
523                dest.append(1, *p);
524            }
525            else { // real variable
526                SetVariables::const_iterator var = variables.find(*p);
527                if (var == variables.end()) {
528                    char *error    = GBS_global_string_copy("Undefined variable '$%c'", *p);
529                    dest.append(GBS_global_string("<%s>", error));
530                    GB_export_error(error);
531                    free(error);
532                    error_occurred = true;
533                    // return "<error>";
534                }
535                else {
536                    dest.append(var->second);
537                }
538            }
539            ++p;
540        }
541        else {
542            dest.append(1, *p);
543            ++p;
544        }
545    }
546    return dest;
547}
548
549GB_ERROR awtc_read_data(char *ali_name)
550{
551    char num[6];
552    char text[100];
553    static int  counter = 0;
554    GBDATA *gb_species_data = GB_search(GB_MAIN,"species_data",GB_CREATE_CONTAINER);
555    GBDATA *gb_species;
556    char *p;
557    struct input_format_struct *ifo;
558    struct input_format_per_line *pl = 0;
559    ifo = awtcig.ifo;
560
561    while (1){              // go to the start
562        p = awtc_read_line(0,ifo->sequencestart,ifo->sequenceend);
563        if (!p || !ifo->begin || GBS_string_matches(p,ifo->begin,GB_MIND_CASE)) break;
564    }
565    if (!p) return "Cannot find start of file: Wrong format or empty file";
566
567    // lets start !!!!!
568    while(p){
569        SetVariables variables;
570        counter++;
571        sprintf(text,"Reading species %i",counter);
572        if (counter %10 == 0){
573            if (aw_status(text)) break;
574        }
575
576        gb_species = GB_create_container(gb_species_data,"species");
577        sprintf(text,"spec%i",counter);
578        free(GBT_read_string2(gb_species,"name",text));
579        if ( awtcig.filenames[1] ) {    // multiple files !!!
580            char *f= strrchr(awtcig.current_file[-1],'/');
581            if (!f) f= awtcig.current_file[-1];
582            else f++;
583            awtc_write_entry(gb_species,"file",f,ifo->filetag,0);
584        }
585        int line;
586        static bool never_warn = false;
587        int max_line = never_warn ? INT_MAX : MAX_COMMENT_LINES;
588
589        for(line=0;line<=max_line;line++){
590            sprintf(num,"%i  ",line);
591            if (line == max_line){
592                char *file = 0;
593                if (awtcig.current_file[0]) file = awtcig.current_file[0];
594                GB_ERROR msg = GB_export_error("A database entry in file '%s' is longer than %i lines.\n"
595                                               "    This might be the result of a wrong input format\n"
596                                               "    or a long comment in a sequence\n",file,line);
597
598                switch (aw_message(msg,"Continue Reading,Continue Reading (Never ask again),Abort"))  {
599                    case 0:
600                        max_line *= 2;
601                        break;
602                    case 1:
603                        max_line = INT_MAX;
604                        never_warn = true;
605                        break;
606                    case 2:
607                        break;
608                }
609            }
610            GB_ERROR    error      = 0;
611            if (strlen(p) > ifo->tab){
612                for (pl = ifo->pl; !error && pl; pl=pl->next) {
613                    const char *what_error = 0;
614                    if (GBS_string_matches(p,pl->match,GB_MIND_CASE)){
615                        char *dup = p+ifo->tab;
616                        while (*dup == ' ' || *dup == '\t') dup++;
617
618                        char *s              = 0;
619                        char *dele           = 0;
620
621                        if (pl->srt){
622                            bool   err_flag;
623                            string expanded = expandSetVariables(variables, pl->srt, err_flag);
624                            if (err_flag) error = GB_get_error();
625                            else {
626                                dele           = s = GBS_string_eval(dup,expanded.c_str(),gb_species);
627                                if (!s) error  = GB_get_error();
628                            }
629                            if (error) what_error = "SRT";
630                        }
631                        else {
632                            s = dup;
633                        }
634
635                        if (!error && pl->aci){
636                            bool   err_flag;
637                            string expanded = expandSetVariables(variables, pl->aci, err_flag);
638                            if (err_flag) error = GB_get_error();
639                            else {
640                                dup           = dele;
641                                dele          = s = GB_command_interpreter(GB_MAIN, s,expanded.c_str(),gb_species, 0);
642                                if (!s) error = GB_get_error();
643                                free(dup);
644                            }
645                            if (error) what_error = "ACI";
646                        }
647
648                        if (!error && (pl->append || pl->write)) {
649                            char *field = 0;
650                            char *tag   = 0;
651
652                            {
653                                bool   err_flag;
654                                string expanded_field = expandSetVariables(variables, string(pl->append ? pl->append : pl->write), err_flag);
655                                if (err_flag) error   = GB_get_error();
656                                else   field          = GBS_string_2_key(expanded_field.c_str());
657                                if (error) what_error = "APPEND or WRITE";
658                            }
659
660                            if (!error && pl->tag) {
661                                bool   err_flag;
662                                string expanded_tag = expandSetVariables(variables, string(pl->tag), err_flag);
663                                if (err_flag) error = GB_get_error();
664                                else   tag          = GBS_string_2_key(expanded_tag.c_str());
665                                if (error) what_error = "TAG";
666                            }
667
668                            if (!error) {
669                                awtc_write_entry(gb_species, field, s, tag, pl->append != 0);
670                            }
671                            free(tag);
672                            free(field);
673                        }
674
675                        if (!error && pl->setvar) {
676                            variables[pl->setvar[0]]  = s;
677                        }
678                        free(dele);
679                    }
680
681                    if (error) {
682                        error =  GBS_global_string("'%s' in %s of MATCH at #%i", error, what_error, pl->start_line);
683                    }
684                }
685            }
686
687            if (error) {
688                return GBS_global_string("\"%s\" at line #%i of species #%i", error, line, counter);
689            }
690
691            if (GBS_string_matches(p,ifo->sequencestart,GB_MIND_CASE)) goto read_sequence;
692
693            p = awtc_read_line(ifo->tab,ifo->sequencestart,ifo->sequenceend);
694            if (!p) break;
695        }
696        return GB_export_error("No Start of Sequence found (%i lines read)", max_line);
697
698    read_sequence:
699        {
700            char          *sequence;
701            GBS_strstruct *strstruct = GBS_stropen(5000);
702            int            linecnt;
703           
704            for(linecnt = 0;;linecnt++) {
705                if (linecnt || !ifo->read_this_sequence_line_too){
706                    p = awtc_read_line(0,ifo->sequencestart,ifo->sequenceend);
707                }
708                if (!p) break;
709                if (ifo->sequenceend && GBS_string_matches(p,ifo->sequenceend,GB_MIND_CASE)) break;
710                if (strlen(p) <= ifo->sequencecolumn) continue;
711                GBS_strcat(strstruct,p+ifo->sequencecolumn);
712            }
713            sequence = GBS_strclose(strstruct);
714
715            GBDATA *gb_data = GBT_add_data(gb_species,ali_name,"data", GB_STRING);
716            if (ifo->sequencesrt) {
717                char *h = GBS_string_eval(sequence,ifo->sequencesrt,gb_species);
718                if (!h) return GB_get_error();
719                free(sequence);
720                sequence = h;
721            }
722
723            if (ifo->sequenceaci) {
724                char *h = GB_command_interpreter(GB_MAIN, sequence,ifo->sequenceaci,gb_species, 0);
725                free(sequence);
726                if (!h) return GB_get_error();
727                sequence = h;
728            }
729
730            GB_write_string(gb_data,sequence);
731            GB_write_security_write(gb_data,4);
732
733            GBDATA *gb_acc = GB_search(gb_species,"acc",GB_FIND);
734            if (!gb_acc && ifo->autocreateacc) {
735                char buf[100];
736                long id = GBS_checksum(sequence,1,".-");
737                sprintf(buf,"ARB_%lX",id);
738                gb_acc = GB_search(gb_species,"acc",GB_STRING);
739                GB_write_string(gb_acc,buf);
740            }
741            free(sequence);
742        }
743        while (1){              // go to the start of an species
744            if (!p || !ifo->begin || GBS_string_matches(p,ifo->begin,GB_MIND_CASE)) break;
745            p = awtc_read_line(0,ifo->sequencestart,ifo->sequenceend);
746        }
747    }
748    return 0;
749}
750
751void AWTC_import_go_cb(AW_window *aww) // Import sequences into new or existing database
752{
753    char     buffer[1024];
754    AW_root *awr         = aww->get_root();
755    bool      is_genom_db = false;
756    {
757        bool           read_genom_db = (awr->awar(AWAR_READ_GENOM_DB)->read_int() != IMP_PLAIN_SEQUENCE);
758        GB_transaction ta(GB_MAIN);
759       
760        is_genom_db = GEN_is_genome_db(GB_MAIN, read_genom_db);
761
762        if (read_genom_db!=is_genom_db) {
763            if (is_genom_db) {
764                aw_message("You can only import whole genom sequences into a genom database.");
765            }
766            else {
767                aw_message("You can't import whole genom sequences into a non-genom ARB database.");
768            }
769            return;
770        }
771    }
772
773    GB_ERROR error = 0;
774    char *file = 0;
775    if (!is_genom_db) {
776        file = awr->awar(AWAR_FORM"/file_name")->read_string();
777        if (!strlen(file)) {
778            delete file;
779            aw_message("Please select a form");
780            return;
781        }
782
783        error = awtc_read_import_format(file);
784        if (error) {
785            delete awtcig.ifo; awtcig.ifo = 0;
786            aw_message(error);
787            return;
788        }
789
790        if (awtcig.ifo->new_format) {
791            awtcig.ifo2 = awtcig.ifo;
792            awtcig.ifo = 0;
793            error = awtc_read_import_format(awtcig.ifo2->new_format);
794            if (error) {
795                delete awtcig.ifo; awtcig.ifo = 0;
796                delete awtcig.ifo2; awtcig.ifo2 = 0;
797                aw_message(error);
798                return;
799            }
800        }
801    }
802
803    GB_change_my_security(GB_MAIN,6,"");
804    GB_begin_transaction(GB_MAIN);                      //first transaction start
805    char *ali_name;
806    {
807        char *ali = awr->awar(AWAR_ALI)->read_string();
808        ali_name = GBS_string_eval(ali,"*=ali_*1:ali_ali_=ali_",0);
809        free(ali);
810    }
811
812    error = GBT_check_alignment_name(ali_name);
813
814    if (!error) {
815        char *ali_type;
816        ali_type = awr->awar(AWAR_ALI_TYPE)->read_string();
817
818        if (is_genom_db && strcmp(ali_type, "dna")!=0) {
819            error = "You must set the alignment type to dna when importing genom sequences.";
820        }
821        else {
822            int ali_protection = awr->awar(AWAR_ALI_PROTECTION)->read_int();
823            GBT_create_alignment(GB_MAIN,ali_name,2000,0,ali_protection,ali_type);
824        }
825        free(ali_type);
826    }
827
828    bool ask_generate_names = true;
829
830    if (!error) {
831        if (is_genom_db) {
832            // import genome flatfile into ARB-genome database :
833
834            char  *mask   = awr->awar(AWAR_FILE)->read_string();
835            char **fnames = GBS_read_dir(mask, 0);
836
837            if (fnames[0] == 0) {
838                error = GB_export_error("Cannot find selected file");
839            }
840            else {
841                int successfull_imports = 0;
842                int failed_imports      = 0;
843                int count;
844
845                for (count = 0; fnames[count]; ++count) ; // count filenames
846
847                GBDATA             *gb_species_data = GB_search(GB_MAIN, "species_data", GB_CREATE_CONTAINER);
848                UniqueNameDetector  und_species(gb_species_data, count*10);
849
850                // close the above transaction and do each importfile in separate transaction
851                // to avoid that all imports are undone by transaction abort in case of error
852                GB_commit_transaction(GB_MAIN);
853
854                aw_openstatus("Reading input files");
855
856                for (count = 0; !error && fnames[count]; ++count) {
857                    GB_ERROR error_this_file =  0;
858
859                    GB_begin_transaction(GB_MAIN);
860
861                    // error_this_file = GI_importGenomeFile(gb_species_data, fnames[count], ali_name, und_species);
862                    error_this_file = GI_importGenomeFile(GB_MAIN, fnames[count], ali_name);
863
864                    if (!error_this_file) {
865                        GB_commit_transaction(GB_MAIN);
866                        GB_warning("File '%s' successfully imported", fnames[count]);
867                        successfull_imports++;
868                    }
869                    else { // error occurred during import
870                        error_this_file = GBS_global_string("'%s' not imported\nReason: %s", fnames[count], error_this_file);
871                        GB_warning("Import error: %s", error_this_file);
872                        GB_abort_transaction(GB_MAIN);
873                        failed_imports++;
874                    }
875                }
876
877                if (!successfull_imports) {
878                    error = "Nothing has been imported";
879                }
880                else {
881                    GB_warning("%i of %i files were imported with success", successfull_imports, (successfull_imports+failed_imports));
882                }
883
884                aw_closestatus();
885
886                // now open another transaction to "undo" the transaction close above
887                GB_begin_transaction(GB_MAIN);
888            }
889
890            GBT_free_names(fnames);
891            free(mask);
892        }
893        else {
894            // import to non-genome ARB-db :
895
896            char *f          = awr->awar(AWAR_FILE)->read_string();
897            awtcig.filenames = GBS_read_dir(f,0);
898            free(f);
899
900            if (awtcig.filenames[0] == 0){
901                error = GB_export_error("Cannot find selected file");
902            }
903
904            aw_openstatus("Reading input files");
905            if (!error) {
906                error = awtc_read_data(ali_name);
907                if (error) {
908                    sprintf(buffer,"Error: %s reading file %s",error,awtcig.current_file[-1]);
909                    error = buffer;
910                }
911            }
912
913            if (awtcig.ifo->noautonames || (awtcig.ifo2 && awtcig.ifo2->noautonames)) {
914                ask_generate_names = false;
915            }
916            else {
917                ask_generate_names = true;
918            }
919
920            delete awtcig.ifo; awtcig.ifo = 0;
921            delete awtcig.ifo2; awtcig.ifo2 = 0;
922
923            if (awtcig.in)  fclose(awtcig.in);
924            awtcig.in = 0;
925            GBT_free_names(awtcig.filenames);
926            awtcig.filenames = 0;
927            awtcig.current_file = 0;
928
929            aw_closestatus();
930        }
931    }
932    free(ali_name);
933
934    if (error) {
935        aw_message(error);
936        GB_abort_transaction(GB_MAIN);
937        return;
938    }
939
940    aww->hide();
941
942    aw_openstatus("Checking and Scanning database");
943    aw_status("Pass 1: Check entries");
944
945    // scan for hidden/unknown fields :
946    awt_selection_list_rescan(GB_MAIN, AWT_NDS_FILTER, AWT_RS_UPDATE_FIELDS);
947    if (is_genom_db) awt_gene_field_selection_list_rescan(GB_MAIN, AWT_NDS_FILTER, AWT_RS_UPDATE_FIELDS);
948
949    GBT_mark_all(GB_MAIN,1);
950    sleep(1);
951    aw_status("Pass 2: Check sequence lengths");
952    GBT_check_data(GB_MAIN,0);
953    sleep(1);
954
955    GB_commit_transaction(GB_MAIN);
956
957    if (ask_generate_names) {
958        if (aw_message("You may generate short names using the full_name and accession entry of the species",
959                       "Generate new short names (recommended),Use found names")==0)
960        {
961            aw_status("Pass 3: Generate unique names");
962            error = AW_select_nameserver(GB_MAIN, awtcig.gb_other_main);
963            if (!error) {
964                error = AWTC_pars_names(GB_MAIN,1);
965            }
966        }
967    }
968
969    aw_closestatus();
970    if (error) aw_message(error);
971
972    GB_begin_transaction(GB_MAIN);
973    GB_change_my_security(GB_MAIN,0,"");
974    GB_commit_transaction(GB_MAIN);
975
976    awtcig.func(awr, awtcig.cd1,awtcig.cd2);
977}
978
979class AliNameAndType {
980    string name_, type_;
981public:
982    AliNameAndType(const char *ali_name, const char *ali_type) : name_(ali_name), type_(ali_type) {}
983    AliNameAndType(const AliNameAndType& other) : name_(other.name_), type_(other.type_) {}
984   
985    const char *name() const { return name_.c_str(); }
986    const char *type() const { return type_.c_str(); }
987};
988
989static AliNameAndType last_ali("ali_new", "rna"); // last selected ali for plain import (aka non-flatfile import)
990
991
992void AWTC_import_set_ali_and_type(AW_root *awr, const char *ali_name, const char *ali_type, GBDATA *gbmain) {
993    bool           switching_to_GENOM_ALIGNMENT = strcmp(ali_name, GENOM_ALIGNMENT) == 0;
994    static GBDATA *last_valid_gbmain            = 0;
995
996    if (gbmain) last_valid_gbmain = gbmain;
997
998    AW_awar *awar_name = awr->awar(AWAR_ALI);
999    AW_awar *awar_type = awr->awar(AWAR_ALI_TYPE);
1000
1001    if (switching_to_GENOM_ALIGNMENT) {
1002        // read and store current settings
1003        char *curr_ali_name = awar_name->read_string();
1004        char *curr_ali_type = awar_type->read_string();
1005
1006        last_ali = AliNameAndType(curr_ali_name, curr_ali_type);
1007
1008        free(curr_ali_name);
1009        free(curr_ali_type);
1010    }
1011
1012    awar_name->write_string(ali_name);
1013    awar_type->write_string(ali_type);
1014
1015    if (last_valid_gbmain) { // detect default write protection for alignment
1016        GB_transaction  ta(last_valid_gbmain);
1017        GBDATA         *gb_ali            = GBT_get_alignment(last_valid_gbmain, ali_name);
1018        int             protection_to_use = 4; // default protection
1019
1020        if (gb_ali) {
1021            GBDATA *gb_write_security = GB_entry(gb_ali,"alignment_write_security");
1022            if (gb_write_security) {
1023                protection_to_use = GB_read_int(gb_write_security);
1024            }
1025        }
1026        awr->awar(AWAR_ALI_PROTECTION)->write_int(protection_to_use);
1027    }
1028}
1029
1030static void genom_flag_changed(AW_root *awr) {
1031    if (awr->awar(AWAR_READ_GENOM_DB)->read_int() == IMP_PLAIN_SEQUENCE) {
1032        AWTC_import_set_ali_and_type(awr, last_ali.name(), last_ali.type(), 0);
1033        awr->awar_string(AWAR_FORM"/filter",".ift");
1034    }
1035    else {
1036        AWTC_import_set_ali_and_type(awr, GENOM_ALIGNMENT, "dna", 0);
1037        awr->awar_string(AWAR_FORM"/filter",".fit"); // *hack* to hide normal import filters
1038    }
1039}
1040
1041static void import_window_close_cb(AW_window *aww) {
1042    if (awtcig.doExit) exit(EXIT_SUCCESS);
1043    else AW_POPDOWN(aww);
1044}
1045
1046GBDATA *open_AWTC_import_window(AW_root *awr,const char *defname, bool do_exit, GBDATA *gb_main, AWTC_RCB(func), AW_CL cd1, AW_CL cd2)
1047{
1048    static AW_window_simple *aws = 0;
1049
1050    awtcig.gb_main = GB_open("noname.arb","wc");
1051    awtcig.func = func;
1052    awtcig.cd1 = cd1;
1053    awtcig.cd2 = cd2;
1054
1055    awtcig.gb_other_main = gb_main;
1056
1057    awtcig.doExit = do_exit; // change/set behavior of CLOSE button
1058
1059    aw_create_selection_box_awars(awr, AWAR_FILE_BASE, ".", "", defname);
1060    aw_create_selection_box_awars(awr, AWAR_FORM, AWT_path_in_ARBHOME("lib/import"), ".ift", "*");
1061
1062    awr->awar_string(AWAR_ALI,"dummy"); // these defaults are never used
1063    awr->awar_string(AWAR_ALI_TYPE,"dummy"); // they are overwritten by AWTC_import_set_ali_and_type
1064    awr->awar_int(AWAR_ALI_PROTECTION, 0); // which is called via genom_flag_changed() below
1065
1066    awr->awar(AWAR_READ_GENOM_DB)->add_callback(genom_flag_changed);
1067    genom_flag_changed(awr);
1068
1069    if (aws){
1070        aws->show();
1071        return GB_MAIN;
1072    }
1073
1074    aws = new AW_window_simple;
1075
1076    aws->init( awr, "ARB_IMPORT","ARB IMPORT");
1077    aws->load_xfig("awt/import_db.fig");
1078
1079    aws->at("close");
1080    aws->callback(import_window_close_cb);
1081    aws->create_button("CLOSE", "CLOSE","C");
1082
1083    aws->at("help");
1084    aws->callback(AW_POPUP_HELP,(AW_CL)"arb_import.hlp");
1085    aws->create_button("HELP", "HELP","H");
1086
1087    awt_create_selection_box(aws, AWAR_FILE_BASE, "imp_", "PWD", AW_TRUE, AW_TRUE); // select import filename
1088    awt_create_selection_box(aws, AWAR_FORM, "", "ARBHOME", AW_FALSE, AW_FALSE); // select import filter
1089
1090    aws->at("auto");
1091    aws->callback(awtc_check_input_format);
1092    aws->create_autosize_button("AUTO_DETECT", "AUTO DETECT","A");
1093
1094    aws->at("ali");
1095    aws->create_input_field(AWAR_ALI,4);
1096
1097    aws->at("type");
1098    aws->create_option_menu(AWAR_ALI_TYPE);
1099    aws->insert_option("dna","d","dna");
1100    aws->insert_option("rna","r","rna");
1101    aws->insert_option("protein","p","ami");
1102    aws->update_option_menu();
1103
1104    aws->at("protect");
1105    aws->create_option_menu(AWAR_ALI_PROTECTION);
1106    aws->insert_option("0", "0", 0);
1107    aws->insert_option("1", "1", 1);
1108    aws->insert_option("2", "2", 2);
1109    aws->insert_option("3", "3", 3);
1110    aws->insert_default_option("4", "4", 4);
1111    aws->insert_option("5", "5", 5);
1112    aws->insert_option("6", "6", 6);
1113    aws->update_option_menu();
1114
1115    aws->at("genom");
1116    aws->create_toggle_field(AWAR_READ_GENOM_DB);
1117    aws->insert_toggle("Import genome data in EMBL, GenBank and DDBJ format","e", IMP_GENOME_FLATFILE);
1118    aws->insert_toggle("Import selected format","f",IMP_PLAIN_SEQUENCE);
1119    aws->update_toggle_field();
1120
1121    aws->at("go");
1122    aws->callback(AWTC_import_go_cb);
1123    aws->highlight();
1124    aws->create_button("GO", "GO","G");
1125
1126    aws->show();
1127    return GB_MAIN;
1128}
Note: See TracBrowser for help on using the repository browser.