source: tags/initial/ARB_GDE/GDE_event.cxx

Last change on this file was 2, checked in by oldcode, 23 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.5 KB
Line 
1#include <unistd.h>
2#include <netdb.h>
3#include <sys/time.h>
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9//#include <xview/xview.h>
10//#include <xview/panel.h>
11#include <arbdb.h>
12#include <arbdbt.h>
13#include <malloc.h>
14#include <aw_root.hxx>
15#include <aw_device.hxx>
16#include <aw_window.hxx>
17#include <awt.hxx>
18#include <awt_tree.hxx>
19#include <gde.hxx>
20#include "GDE_menu.h"
21#include "GDE_def.h"
22#include "GDE_extglob.h"
23
24#define DEFAULT_COLOR 8
25extern AW_CL agde_filtercd;
26
27//char *ReplaceArgs(char *Action,GmenuItemArg arg);
28char *ReplaceArgs(AW_root *awr,char *Action,AWwindowinfo *AWinfo,int number);
29 
30
31long LMAX(long a,long b)
32{
33    if(a>b) return a;
34    return b;
35}
36 
37void GDE_free(void **p)
38{
39    if(*p!=0) 
40    {
41        //delete *p;
42        free(*p);
43        *p=0;
44    }
45}
46
47/*void GDE_freemask(GMask *mask)
48{
49        if(mask==0) return;
50        GDE_free((void**)&mask->name);
51        for(long i=0;i<mask->listlen;i++)
52        {
53                NumList numl=mask->list[i];
54                GDE_free((void**)&numl.valu);
55        }
56        GDE_free((void**)&mask->list);
57}*/
58
59char *ReplaceFile(char *Action,GfileFormat file)
60{
61    char *symbol,*method,*temp;
62    int i,newlen;
63    symbol = file.symbol;
64    method = file.name;
65
66    for(; (i=Find2(Action,symbol)) != -1;)
67    {
68        newlen = strlen(Action)-strlen(symbol) + strlen(method)+1;
69        temp = (char *)calloc(newlen,1);
70        if (temp == NULL)
71            Error("ReplaceFile():Error in calloc");
72        strncat(temp,Action,i);
73        strncat(temp,method,strlen(method));
74        strcat( temp,&(Action[i+strlen(symbol)]) );
75        free(Action);
76        Action = temp;
77    }
78    return(Action);
79}
80
81char *ReplaceString(char *Action,const char *old,const char *news)
82{
83    const char *symbol;
84    const char *method;
85    char *temp;
86    int i,newlen;
87   
88    symbol = old;
89    method = news;
90
91    for(; (i=Find2(Action,symbol)) != -1;)
92    {
93        newlen = strlen(Action)-strlen(symbol) + strlen(method)+1;
94        temp = (char *)calloc(newlen,1);
95        if (temp == NULL)
96            Error("ReplaceFile():Error in calloc");
97        strncat(temp,Action,i);
98        strncat(temp,method,strlen(method));
99        strcat( temp,&(Action[i+strlen(symbol)]) );
100        free(Action);
101        Action = temp;
102    }
103    return(Action);
104}
105
106
107void GDE_freesequ(NA_Sequence *sequ)
108{
109    if(sequ==0) return;
110    GDE_free((void**)&sequ->comments);
111    /* GDE_free((void**)&sequ->col_lut);        OLIVER STRUNK           */
112    GDE_free((void**)&sequ->cmask);
113    /*GDE_freemask(sequ->mask);*/
114    GDE_free((void**)&sequ->baggage);
115    GDE_free((void**)&sequ->sequence);
116    /*if(sequ->groupf)
117      {
118      GDE_freesequ(sequ->groupf);
119      GDE_free((void**)&sequ->groupf);
120      }*/
121}
122
123void GDE_freeali(NA_Alignment *dataset)
124{
125    if(dataset==0) return;
126    GDE_free((void**)&dataset->id);
127    GDE_free((void**)&dataset->description);
128    GDE_free((void**)&dataset->authority);
129    GDE_free((void**)&dataset->cmask);
130    GDE_free((void**)&dataset->selection_mask);
131    GDE_free((void**)&dataset->alignment_name);
132
133    /* maybe not correct:
134       GMask *mask=dataset->mask;
135       GDE_freemask(dataset->mask);
136       GDE_free((void**)&dataset->mask);*/
137
138    unsigned long i;
139    // **maybe not correct:
140    //NA_Sequence **group=dataset->group;
141    //for(long i=0;i<dataset->numgroups;i++)
142    //  GDE_freesequ(dataset->group[i]);
143    //GDE_free((void**)&dataset->group);
144    // **correction: this was not correct
145
146    for(i=0;i<dataset->numelements;i++)
147        GDE_freesequ(&(dataset->element[i]));
148}
149
150void GDE_export(NA_Alignment *dataset,char *align,long oldnumelements)
151{
152    GB_begin_transaction(gb_main);
153    long maxalignlen=GBT_get_alignment_len(gb_main,align);
154    long isdefaultalign=0;
155    int load_all = 0;
156    if(maxalignlen<=0)
157    {
158        align=GBT_get_default_alignment(gb_main);
159        isdefaultalign=1;
160    }
161
162    maxalignlen=GBT_get_alignment_len(gb_main,align);
163    long lotyp=0;
164    //  char *type=GBT_get_alignment_type(gb_main,align);       
165    //  if(strcmp(type,"dna")==0) lotyp=DNA;
166    //  if(strcmp(type,"rna")==0) lotyp=RNA;
167    //  if(strcmp(type,"ami")==0) lotyp=PROTEIN;
168    //  if(strcmp(type,"pro")==0) lotyp=PROTEIN;
169       
170    {
171        GB_alignment_type at = GBT_get_alignment_type(gb_main, align);
172           
173        switch (at)
174        {
175            case GB_AT_DNA: lotyp = DNA; break;
176            case GB_AT_RNA: lotyp = RNA; break;
177            case GB_AT_AMI:
178            case GB_AT_PRO: lotyp = PROTEIN; break;
179            case GB_AT_UNKNOWN: lotyp = DNA; break;
180        }
181    }
182       
183    GB_ERROR error = 0;
184    unsigned long i;
185    for(i=oldnumelements;!error && i<dataset->numelements;i++)
186    {
187        NA_Sequence *sequ=&(dataset->element[i]);
188        int seqtyp,issame=0;
189        seqtyp=sequ->elementtype;
190        if( (seqtyp==lotyp)||((seqtyp==DNA)&&(lotyp==RNA))||((seqtyp==RNA)&&(lotyp==DNA)) ){
191            issame=1;
192        }else{
193            aw_message(GBS_global_string("Warning: sequence type of species '%s' changed",sequ->short_name));
194        }
195
196        if(sequ->tmatrix)
197            for(long j=0;j<sequ->seqlen;j++)
198                sequ->sequence[j]=(char)sequ->tmatrix[sequ->sequence[j]];
199        char *savename=GBS_string_2_key(sequ->short_name);
200        sequ->gb_species = 0;
201        if(!issame)  /* save as extended */
202        {
203
204            GBDATA *gb_extended =       GBT_create_SAI(gb_main,savename);
205            sequ->gb_species=gb_extended;
206            GBDATA *gb_data = GBT_add_data(gb_extended, align,"data", GB_STRING);
207            error = GBT_write_sequence(gb_data,align,maxalignlen,(char *)sequ->sequence);
208        }else /* save as sequence */
209        {
210            GBDATA *gb_species_data = GB_search(gb_main,"species_data",GB_CREATE_CONTAINER);
211            GBDATA *gb_species;
212            gb_species = GBT_find_species_rel_species_data(gb_species_data, savename);
213            GB_push_my_security(gb_main);
214
215            if (gb_species) {   /* new element that already exists !!!!*/
216                int select_mode;
217                const char *msg = GBS_global_string(
218                                                    "You importet a species '%s' which already exists\n"
219                                                    "   You may:\n"
220                                                    "           - delete old species\n"
221                                                    "           - overwrite sequence of old species\n"
222                                                    "           - not change this species\n"
223                                                    "           - overwrite sequence of all species\n"
224                                                    "   Remember: There is still an undo button",
225                                                    savename);
226                if (!load_all){
227                    select_mode = aw_message(msg,
228                                             "delete old,"
229                                             "sequence only,"
230                                             "skip,"
231                                             "all sequences"
232                                             );
233                }else{
234                    select_mode = 1;
235                }
236
237                switch(select_mode) {
238                    case 0:
239                        GB_delete(gb_species);
240                    case 1:
241                        gb_species =    GBT_create_species_rel_species_data(gb_species_data, savename);
242                        break;
243                    case 2:
244                        gb_species = 0;
245                        continue;
246                    case 3:
247                        load_all = 1;
248                        gb_species =    GBT_create_species_rel_species_data(gb_species_data, savename);
249                        break;
250                }
251            }else{
252                gb_species = GBT_create_species_rel_species_data(gb_species_data, savename);
253            }
254            if (!gb_species) continue;
255            sequ->gb_species=gb_species;
256            GBDATA *gb_data = GBT_add_data(gb_species, align,"data", GB_STRING);
257            error = GBT_write_sequence(gb_data,align,maxalignlen,(char *)sequ->sequence);
258            GB_pop_my_security(gb_main);
259
260        }
261        delete savename;
262    }
263
264    /* colormasks */
265    for(i=0;!error && i<dataset->numelements;i++)
266    {
267        NA_Sequence *sequ=&(dataset->element[i]);
268        if(sequ->cmask)
269        {
270            GBDATA *gb_color;
271            maxalignlen=LMAX(maxalignlen,sequ->seqlen);
272            char *resstring=(char *)calloc((unsigned int)maxalignlen+1,sizeof(char));
273
274            char *dummy=resstring;
275            for(long j=0;j<maxalignlen-sequ->seqlen;j++)
276                *resstring++=DEFAULT_COLOR; 
277
278            for(long k=0;k<sequ->seqlen;k++)
279                *resstring++=(char)sequ->cmask[i];
280            *resstring='\0';
281
282            GBDATA *gb_ali=GB_search(sequ->gb_species,
283                                     align,GB_CREATE_CONTAINER);
284            gb_color=GB_search(gb_ali,"colmask",GB_BYTES);
285            error = GB_write_bytes(gb_color,dummy,maxalignlen);
286            delete dummy;
287
288        }
289    }
290    if(!error && dataset->cmask)
291    {
292        GBDATA *gb_color;
293        maxalignlen=LMAX(maxalignlen,dataset->cmask_len);
294        char *resstring=(char *)calloc((unsigned int)maxalignlen+1,sizeof(char));
295
296        char *dummy=resstring;
297        long k;
298        for(k=0;k<maxalignlen-dataset->cmask_len;k++)
299            *resstring++=DEFAULT_COLOR; 
300
301        for(k=0;k<dataset->cmask_len;k++)
302            *resstring++=(char)dataset->cmask[k];
303        *resstring='\0';
304
305
306        GBDATA *gb_extended =   GBT_create_SAI(gb_main,"COLMASK");
307        gb_color = GBT_add_data(gb_extended, align,"colmask", GB_BYTES);
308        error = GB_write_bytes(gb_color,dummy,maxalignlen);
309
310        delete dummy;
311    }
312    if (error) {
313        GB_abort_transaction(gb_main);
314        aw_message(error);
315    }else{
316        GB_commit_transaction(gb_main);
317    }
318    if(isdefaultalign) delete align;
319}
320
321void GDE_startaction_cb(AW_window *aw,AWwindowinfo *AWinfo,AW_CL cd)
322{
323    long oldnumelements=0;
324    AWUSE(cd);
325    AW_root *aw_root=aw->get_root();
326
327    long compress=aw_root->awar("gde/compression")->read_int();
328    AP_filter *filter2 = awt_get_filter(aw_root,agde_filtercd);
329    char *filter_name  = aw_root->awar("gde/filter/name")->read_string();
330    char *alignment_name=strdup("ali_unknown");
331    long marked=aw_root->awar("gde/species")->read_int();
332
333    GmenuItem *current_item;
334    current_item=AWinfo->gmenuitem;
335
336    aw_openstatus(current_item->label);
337    aw_status((double)0);
338
339    int i,j,k,flag,select_mode =0;
340    static int fileindx = 0;
341    char *Action,buffer[GBUFSIZ];
342    i=0;k=0;
343    if(current_item->numinputs>0)
344    {
345        DataSet->gb_main = gb_main;
346        GB_begin_transaction(DataSet->gb_main);
347        delete DataSet->alignment_name;
348        DataSet->alignment_name = GBT_get_default_alignment(DataSet->gb_main);
349        delete alignment_name;
350        alignment_name = strdup(DataSet->alignment_name);
351
352        aw_status("reading database");
353        int stop;
354        if (gde_cgss.get_sequences) {
355            stop=ReadArbdb2(DataSet,filter2,compress);
356        }else{
357            stop=ReadArbdb(DataSet,marked,filter2,compress);
358        }
359        GB_commit_transaction(DataSet->gb_main);
360
361        if(stop) goto startaction_end; 
362
363        if(DataSet->numelements==0) 
364        {
365            aw_message("no sequences selected");
366            goto startaction_end;
367        }
368    }
369
370    flag = AW_FALSE;
371    for(j=0;j<current_item->numinputs;j++)
372        if(current_item->input[j].format != STATUS_FILE)
373            flag = AW_TRUE;
374
375    if(flag && DataSet)
376        select_mode = ALL; //TestSelection();
377
378    //chdir(current_dir);
379    for(j=0;j<current_item->numinputs;j++)
380    {
381        sprintf(buffer,"/tmp/gde%d_%d",(int)getpid(),fileindx++); 
382        current_item->input[j].name = String(buffer);
383        switch(current_item->input[j].format)
384        {
385            case COLORMASK:
386                WriteCMask(DataSet,buffer,select_mode,
387                           current_item->input[j].maskable);
388                break;
389            case GENBANK:
390                WriteGen(DataSet,buffer,select_mode,
391                         current_item->input[j].maskable);
392                break;
393            case NA_FLAT:
394                WriteNA_Flat(DataSet,buffer,select_mode,
395                             current_item->input[j].maskable);
396                break;
397            case STATUS_FILE:
398                WriteStatus(DataSet,buffer,select_mode);
399                break;
400            case GDE:
401                WriteGDE(DataSet,buffer,select_mode,
402                         current_item->input[j].maskable);
403                break;
404            default:
405                break;
406        }
407    }
408
409    for(j=0;j<current_item->numoutputs;j++)
410    {
411        sprintf(buffer,"/tmp/gde%d_%d",(int)getpid(),fileindx++);
412        current_item->output[j].name = String(buffer);
413    }
414
415    /*
416     *  Create the command line for external the function call
417     */
418    Action = (char*)strdup(current_item->method);
419    if(Action == NULL)
420        Error("DO(): Error in duplicating method string");
421    for(j=0;j<current_item->numargs;j++)
422        Action = ReplaceArgs(aw_root,Action,AWinfo,j);
423
424    for(j=0;j<current_item->numinputs;j++)
425        Action = ReplaceFile(Action,current_item->input[j]);
426
427    for(j=0;j<current_item->numoutputs;j++)
428        Action = ReplaceFile(Action,current_item->output[j]);
429
430    //    Action = ReplaceString(Action,"$FILTER",filter_name);
431    Action = ReplaceString(Action,"$FILTER",AWT_get_combined_filter_name(aw_root));
432       
433
434    /*
435     *  call and go...
436     */
437
438    aw_status("calling external program");
439    printf("Action: %s\n",Action);     
440    system(Action);
441    free(Action);
442
443    oldnumelements=DataSet->numelements;
444
445    BlockInput = AW_FALSE;
446
447    for(j=0;j<current_item->numoutputs;j++)
448    {
449        if(current_item->output[j].overwrite)
450        {
451            if(current_item->output[j].format == GDE)
452                OVERWRITE = AW_TRUE;
453            else
454                Warning("Overwrite mode only available for GDE format");
455        }
456        switch(current_item->output[j].format)
457        {
458            /*
459             *     The LoadData routine must be reworked so that
460             *     OpenFileName uses it, and so I can remove the
461             *     major kluge in OpenFileName().
462             */
463            case GENBANK:
464            case NA_FLAT:
465            case GDE:
466                /*ARB-change:*/
467                /*OpenFileName(current_item->output[j].name,NULL);*/
468                LoadData(current_item->output[j].name);
469                break;
470            case COLORMASK:
471                ReadCMask(current_item->output[j].name);
472                break;
473            case STATUS_FILE:
474                ReadStatus(current_item->output[j].name);
475                break;
476            default:
477                break;
478        }
479        OVERWRITE = AW_FALSE;
480    }
481    for(j=0;j<current_item->numoutputs;j++)
482    {
483        if(!current_item->output[j].save)
484        {
485            unlink(current_item->output[j].name);
486        }
487    }
488
489    for(j=0;j<current_item->numinputs;j++)
490    {
491        if(!current_item->input[j].save)
492        {
493            unlink(current_item->input[j].name);
494        }
495    }
496
497    aw_closestatus();
498    GDE_export(DataSet,alignment_name,oldnumelements);
499
500 startaction_end:
501    aw_closestatus();
502    delete alignment_name;
503    delete filter2;
504    delete filter_name;
505
506    GDE_freeali(DataSet);
507    delete DataSet;
508    DataSet=0;
509    DataSet = (NA_Alignment *) Calloc(1,sizeof(NA_Alignment));
510    DataSet->rel_offset = 0;
511       
512    aw->hide();
513}
514
515/*
516ReplaceArgs():
517        Replace all command line arguements with the appropriate values
518stored for the chosen menu item.
519
520Copyright (c) 1989-1990, University of Illinois board of trustees.  All
521rights reserved.  Written by Steven Smith at the Center for Prokaryote Genome
522Analysis.  Design and implementation guidance by Dr. Gary Olsen and Dr.
523Carl Woese.
524
525Copyright (c) 1990,1991,1992 Steven Smith at the Harvard Genome Laboratory.
526All rights reserved.
527
528*/
529
530
531//char *ReplaceArgs(char *Action,GmenuItemArg arg)
532char *ReplaceArgs(AW_root *awr,char *Action,AWwindowinfo *AWinfo,int number)
533{
534    /*
535     *  The basic idea is to replace all of the symbols in the method
536     *  string with the values picked in the dialog box.  The method
537     *  is the general command line structure.  All arguements have three
538     *  parts, a label, a method, and a value.  The method never changes, and
539     *  is used to represent '-flag's for a given function.  Values are the
540     *  associated arguements that some flags require.  All symbols that
541     *  require argvalue replacement should have a '$' infront of the symbol
542     *  name in the itemmethod definition.  All symbols without the '$' will
543     *  be replaced by their argmethod.  There is currently no way to do a label
544     *  replacement, as the label is considered to be for use in the dialog
545     *  box only.  An example command line replacement would be:
546*
547*               itemmethod=>            "lpr arg1 $arg1 $arg2"
548*
549*               arglabel arg1=>         "To printer?"
550*               argmethod arg1=>        "-P"
551*               argvalue arg1=>         "lw"
552*
553*               arglabel arg2=>         "File name?"
554*               argvalue arg2=>         "foobar"
555*               argmethod arg2=>        ""
556*
557*       final command line:
558*
559*               lpr -P lw foobar
560*
561*       At this point, the chooser dialog type only supports the arglabel and
562*       argmethod field.  So if an argument is of type chooser, and
563*       its symbol is "this", then "$this" has no real meaning in the
564*       itemmethod definition.  Its format in the .GDEmenu file is slighty
565*       different as well.  A choice from a chooser field looks like:
566*
567*               argchoice:Argument_label:Argument_method
568*
569*       
570*/
571    const char *symbol=0;
572    char *method=0;
573    char *textvalue=0;
574    char *temp;
575    int i,newlen,type;
576    symbol = AWinfo->gmenuitem->arg[number].symbol;
577    //method = AWinfo->gmenuitem->arg[number]->method;
578    //textvalue = AWinfo->gmenuitem->arg[number]->textvalue;
579    type = AWinfo->gmenuitem->arg[number].type;
580    if( (type == SLIDER) )
581    {
582        char *awarname=GDE_makeawarname(AWinfo,number);
583        textvalue = new(char[GBUFSIZ]);
584        char *awalue=awr->awar(awarname)->read_as_string();
585        sprintf(textvalue,"%s",awalue);
586        delete awalue;
587    }
588    else if((type == CHOOSER) ||
589            (type == CHOICE_TREE) ||
590            (type == CHOICE_SAI) ||
591            (type == CHOICE_MENU) ||
592            (type == CHOICE_LIST) ||
593            (type == CHOICE_WEIGHTS) ||
594            (type == TEXTFIELD))
595    {
596        char *awarname=GDE_makeawarname(AWinfo,number);
597        method=awr->awar(awarname)->read_string();
598        textvalue=awr->awar(awarname)->read_string();
599    }
600
601    if(textvalue == NULL)       textvalue=(char *)calloc(1,sizeof(char)); 
602    if(method == NULL)          method=(char *)calloc(1,sizeof(char)); 
603    if(symbol == NULL)          symbol="";
604
605    for(; (i=Find2(Action,symbol)) != -1;)
606    {
607        if(i>0 && Action[i-1] =='$' )
608        {
609            newlen = strlen(Action)-strlen(symbol)
610                +strlen(textvalue);
611            temp = (char *)calloc(newlen,1);
612            if (temp == NULL)
613                Error("ReplaceArgs():Error in calloc");
614            strncat(temp,Action,i-1);
615            strncat(temp,textvalue,strlen(textvalue));
616            strcat( temp,&(Action[i+strlen(symbol)]) );
617            free(Action);
618            Action = temp;
619        }
620        else
621        {
622            newlen = strlen(Action)-strlen(symbol)
623                +strlen(method)+1;
624            temp = (char *)calloc(newlen,1);
625            if (temp == NULL)
626                Error("ReplaceArgs():Error in calloc");
627            strncat(temp,Action,i);
628            strncat(temp,method,strlen(method));
629            strcat( temp,&(Action[i+strlen(symbol)]) );
630            free(Action);
631            Action = temp;
632        }
633    }
634    delete textvalue;
635    delete method;
636    return(Action);
637}
638
Note: See TracBrowser for help on using the repository browser.