source: tags/arb_5.3/NTREE/AP_consensus.cxx

Last change on this file was 6100, checked in by westram, 15 years ago
  • fix warning "format not a string literal and no format arguments"
    • GB_export_error → GB_export_error/GB_export_errorf
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 33.4 KB
Line 
1/* -----------------------------------------------------------------
2 * Project:                       ARB
3 *
4 * Module:                        consensus [abbrev.:CON]
5 *
6 * Exported Classes:              x
7 *
8 * Global Functions:              x
9 *
10 * Global Variables:              AWARS:
11 *                  AW_STRING, "tmp/con/alignment"     : name of alignment
12 *                  AW_STRING, "tmp/con/which_species" : only marked species ?
13 *                  AW_STRING, "con/group"         : allow Sgrouping ?
14 *                  AW_STRING, "con/countgaps"
15 *                  AW_DOUBLE, "con/fupper"
16 *                  AW_INT, "con/lower"
17 *                  AW_INT, "con/gapbound"  : result is '-' if more than
18 *                                            'gapbound' per cent gaps
19 *                  AW_DOUBLE, "con/fconsidbound" : if frequency of character is
20 *                                     more than 'fconsidbound' per cent, this
21 *                                     character can contribute to groups
22 *                  AW_STRING, "tmp/con/name"   : save with this name
23 *                  AW_STRING, "con/result" : result has one or more lines ?
24 *
25 * Global Structures:             x
26 *
27 * Private Classes:               .
28 *
29 * Private Functions:             .
30 *
31 * Private Variables:             .
32 *
33 * Dependencies:       Needs consens.fig and CON_groups.fig
34 *
35 * Description: This module calculates the consensus of sequencies of
36 *              bases or amino acids. The result can be one or more lines
37 *              of characters and it is written to the extended data of
38 *              the alignment.
39 *
40 * Integration Notes: To use this module the main function must have a
41 *                    callback to the function
42 *                    AW_window *AP_open_consensus_window( AW_root *aw_root)
43 *                    and the function void create_consensus_var
44 *                    (AW_root *aw_root, AW_default aw_def) has to be called.
45 *
46 * -----------------------------------------------------------------
47 */
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <arbdb.h>
52#include <arbdbt.h>
53// #include <malloc.h>
54#include <aw_root.hxx>
55#include <aw_device.hxx>
56#include <aw_window.hxx>
57#include <awt.hxx>
58#include <awt_sel_boxes.hxx>
59
60#define AWAR_MAX_FREQ   "tmp/CON_MAX_FREQ/"
61
62#define AWAR_MAX_FREQ_NO_GAPS AWAR_MAX_FREQ "no_gaps"
63#define AWAR_MAX_FREQ_SAI_NAME AWAR_MAX_FREQ "sai_name"
64
65
66extern GBDATA *GLOBAL_gb_main;
67enum {
68    BAS_GAP,
69    BAS_A,  BAS_C,  BAS_G,  BAS_T, BAS_N,
70
71    MAX_BASES,
72    MAX_AMINOS  =27,
73    MAX_GROUPS  =40
74};
75
76/* -----------------------------------------------------------------
77 * Function:                     CON_evaluatestatistic
78 *
79 * Arguments:                    int **statistic,char **groupflags,
80 *                               char *groupnames
81 * Returns:                      char *result
82 *
83 * Description:       This function creates one or more result strings out
84 *                    of table statistic. Memory for result is allocated
85 *                    and later freeded in function CON_calculate_cb
86 *
87 * NOTE:              Usage of groupflags and groupnames see function
88 *                    CON_makegrouptable.
89 *
90 * Global Variables referenced:  .
91 *
92 * Global Variables modified:    x
93 *
94 * AWARs referenced:             .
95 *
96 * AWARs modified:               x
97 *
98 * Dependencies:                 Always check that behavior is identical to that
99 *                               of ED4_char_table::build_consensus_string()
100 * -----------------------------------------------------------------
101 */
102void CON_evaluatestatistic(char   *&result,int **statistic,char **groupflags,
103                           char    *groupnames,int alignlength,double fupper,int lower,
104                           double   fconsidbound,int gapbound,int countgap,int numgroups )
105{
106    int row=0;
107    int j = 0;
108    int groupfr[MAX_GROUPS]; /* frequency of group */
109    int highestfr,highestgr;
110    long numentries;
111
112    aw_status("calculating result");
113
114    result=(char *)GB_calloc(alignlength+1,1);
115
116    for(int column=0;column<alignlength;column++){
117        numentries=0;
118        for (row=0;statistic[row];row++)  numentries+=statistic[row][column];
119        if (numentries==0) {
120            result[column]='.';
121            continue;
122        }
123
124        if(numentries-statistic[0][column]==0) {
125            result[column]='=';
126            continue;   /* 100 per cent `-` -> `=` */
127        }
128
129        if(!countgap)  {
130            numentries -= statistic[0][column];
131            statistic[0][column]=0;
132        }
133
134        if((statistic[0][column]*100/numentries)>gapbound) {
135            result[column]='-';
136            continue;
137        }
138
139        for(j=0;j<numgroups;j++) {
140            groupfr[j]=0;
141        }
142
143        row=0;
144        while(statistic[row])   {
145            if((statistic[row][column]*100.0)>=fconsidbound*numentries){
146                for(j=numgroups-1;j>=0;j--){
147                    if(groupflags[j][row]){
148                        groupfr[j]+= statistic[row][column];
149                    }
150                }
151            }
152            row++;
153        }
154
155        highestfr=0;highestgr=0;
156        for(j=0;j<numgroups;j++){
157            if(groupfr[j] > highestfr){
158                highestfr=groupfr[j];
159                highestgr=j;
160            }
161        }
162
163        if((highestfr*100.0/numentries)>=fupper)  {
164            result[column]=groupnames[highestgr];
165        } else if((highestfr*100/numentries)>=lower){
166            char c=groupnames[highestgr];
167            if(c>='A' && c<='Z') {
168                c=c-'A'+'a';
169            }
170            result[column]=c;
171        } else {
172            result[column]='.';
173        }
174    }
175}
176
177
178
179/* -----------------------------------------------------------------
180 * Function:                     CON_makegrouptable
181 *
182 * Arguments:                    .
183 *
184 * Returns:                      char **gf [groupflags],char *groupnames
185 *
186 * Description:       Creates table groupflags that is used in function
187 *                    CON_evaluatestatistic. E.g. gf[10][3]=1 means, that
188 *                    all characters c with convtable[c]==3 are members
189 *                    of group 10.
190 *                    Table groupnames is also created.
191 *                    E.g. c=groupnames[5] gives abbrev of 5th group.
192 *
193 * NOTE:                         .
194 *
195 * Global Variables referenced:  .
196 *
197 * Global Variables modified:    x
198 *
199 * AWARs referenced:             .
200 *
201 * AWARs modified:               x
202 *
203 * Dependencies:                 .
204 * -----------------------------------------------------------------
205 */
206int CON_makegrouptable(char **gf,char *groupnames,
207                       int isamino,int groupallowed)
208{
209    for(int j=0;j<MAX_GROUPS;j++)  {
210        gf[j]=(char *)GB_calloc(MAX_GROUPS,1);  }
211
212    if(!isamino)
213    {
214        strcpy(groupnames,"-ACGUMRWSYKVHDBN\0");
215        for(int i=1;i<MAX_BASES;i++) {
216            gf[i][i]=1; }
217        if(groupallowed)
218        {
219            gf[5][BAS_A]=1; gf[5][BAS_C]=1;
220            gf[6][BAS_A]=1; gf[6][BAS_G]=1;
221            gf[7][BAS_A]=1; gf[7][BAS_T]=1;
222            gf[8][BAS_C]=1; gf[8][BAS_G]=1;
223            gf[9][BAS_C]=1; gf[9][BAS_T]=1;
224            gf[10][BAS_G]=1; gf[10][BAS_T]=1;
225            gf[11][BAS_A]=1; gf[11][BAS_C]=1; gf[11][BAS_G]=1;
226            gf[12][BAS_A]=1; gf[12][BAS_C]=1; gf[12][BAS_T]=1;
227            gf[13][BAS_A]=1; gf[13][BAS_G]=1; gf[13][BAS_T]=1;
228            gf[14][BAS_C]=1; gf[14][BAS_G]=1; gf[14][BAS_T]=1;
229            gf[15][BAS_A]=1; gf[15][BAS_C]=1;
230            gf[15][BAS_G]=1; gf[15][BAS_T]=1;
231            return(16);
232        }
233        return(5);
234    }
235    else
236    {
237        strcpy(groupnames,"-ABCDEFGHI*KLMN.PQRST.VWXYZADHIF\0");
238        for(int i=1;i<MAX_AMINOS;i++) {
239            gf[i][i]=1; }
240        if(groupallowed)
241#define SC(x,P) gf[x][P-'A'+1] = 1
242        {
243            SC(27,'P');SC(27,'A');SC(27,'G');SC(27,'S');SC(27,'T');
244            /* PAGST */
245            SC(28,'Q');SC(28,'N');SC(28,'E');SC(28,'D');SC(28,'B');
246            SC(28,'Z');    /* QNEDBZ */
247            SC(29,'H');SC(29,'K');SC(29,'R');
248            /* HKR */
249            SC(30,'L');SC(30,'I');SC(30,'V');SC(30,'M');
250            /* LIVM */
251            SC(31,'F');SC(31,'Y');SC(31,'W');
252            /* FYW */
253            return(32);
254        }
255#undef SC
256        return(27);
257    }
258}
259
260
261/* -----------------------------------------------------------------
262 * Function:                     CON_makestatistic
263 *
264 * Arguments:                    int *convtable
265 *
266 * Returns:                      int **statistic
267 *
268 * Description:       This function fills the table statistic, that is used
269 *                    later by function CON_evaluatestatistic. The filling
270 *                    is done depending on table convtable, that is created
271 *                    in function CON_maketables. Memory for statistic is
272 *                    allocated also in function CON_maketables.
273 *
274 *
275 * NOTE:                         .
276 *
277 * Global Variables referenced:  .
278 *
279 * Global Variables modified:    x
280 *
281 * AWARs referenced:             .
282 *
283 * AWARs modified:               x
284 *
285 * Dependencies:                 .
286 * -----------------------------------------------------------------
287 */
288
289
290long CON_makestatistic(int **statistic,int *convtable,char *align,int onlymarked)
291{
292    long maxalignlen=GBT_get_alignment_len(GLOBAL_gb_main,align);
293    GBDATA *gb_species,*alidata;
294    int i,nrofspecies=0;
295
296    aw_status("reading database");
297
298    if (onlymarked) {
299        nrofspecies = GBT_count_marked_species(GLOBAL_gb_main);
300    } else {
301        nrofspecies = GBT_get_species_count(GLOBAL_gb_main);
302    }
303
304    if (onlymarked) {
305        gb_species = GBT_first_marked_species(GLOBAL_gb_main);
306    }else{
307        gb_species = GBT_first_species(GLOBAL_gb_main);
308    }
309
310    long count = 0;
311    while(gb_species)
312    {
313        if( (alidata=GBT_read_sequence(gb_species,align)) )
314        {
315            unsigned char        c;
316            const unsigned char *data = (const unsigned char *)GB_read_char_pntr(alidata);
317
318            aw_status((double)count++/(double)nrofspecies);
319            i = 0;
320            while( (c=data[i]) ){
321                if( (c=='-') || ((c>='a')&&(c<='z')) || ((c>='A')&&(c<='Z'))
322                    || (c=='*') ){
323                    if ( i > maxalignlen) break;
324                    statistic[convtable[c]][i] += 1;
325                }
326                i++;
327            }
328        }
329        if (onlymarked) {
330            gb_species = GBT_next_marked_species(gb_species);
331        }else{
332            gb_species = GBT_next_species(gb_species);
333        }
334    }
335    return(nrofspecies);
336}
337
338
339/* -----------------------------------------------------------------
340 * Function:          CON_maketables
341 *
342 * Arguments:         long maxalignlen,int isamino
343 *
344 * Returns:           return parameters: int *convtable,int **statistic
345 *
346 * Description:       Creates tables convtable and statistic, that are
347 *                    used by function CON_makestatistic. The memory
348 *                    allocated for both tables is freed in the
349 *                    function CON_calculate_cb.
350 *                    E.g. convtable['c']=k means that the character c
351 *                    is counted in table statistic in row k.
352 *
353 * NOTE:                         .
354 *
355 * Global Variables referenced:  .
356 *
357 * Global Variables modified:    x
358 *
359 * AWARs referenced:             .
360 *
361 * AWARs modified:               x
362 *
363 * Dependencies:                 .
364 * -----------------------------------------------------------------
365 */
366void CON_maketables(int *convtable,int **statistic,long maxalignlen,int isamino)
367{
368    int i;
369    for(i=0;i<256;i++)  { convtable[i]=0; }
370    if(!isamino){
371        for (i='a'; i<= 'z'; i++) convtable[i] = BAS_N;
372        for (i='A'; i<= 'Z'; i++) convtable[i] = BAS_N;
373
374        convtable['a']=BAS_A;   convtable['A']=BAS_A;
375        convtable['c']=BAS_C;   convtable['C']=BAS_C;
376        convtable['g']=BAS_G;   convtable['G']=BAS_G;
377        convtable['t']=BAS_T;   convtable['T']=BAS_T;
378        convtable['u']=BAS_T;   convtable['U']=BAS_T;
379
380
381        for(i=0;i<MAX_BASES;i++)  {
382            statistic[i]=(int*)GB_calloc((unsigned int)maxalignlen,sizeof(int));
383        }
384        statistic[MAX_BASES]=NULL;
385    }else{
386        for(i=0;i<MAX_AMINOS-1;i++)
387        {
388            convtable['a'+i]=i+1;
389            convtable['A'+i]=i+1;
390        }
391        for(i=0;i<MAX_AMINOS;i++){
392            statistic[i]=(int*)GB_calloc((unsigned int)maxalignlen,sizeof(int));
393        }
394        statistic[MAX_AMINOS]=NULL;
395        convtable['*']=10; /* 'J' */
396    }
397}
398
399/* export results into database */
400GB_ERROR CON_export(char *savename,char *align,int **statistic,char *result,int *convtable,char *groupnames,int onlymarked,long nrofspecies,long maxalignlen,int countgaps,int gapbound,int groupallowed,double fconsidbound,double fupper,int lower,int resultiscomplex)
401{
402    GB_ERROR err;
403    const char *off="off";
404    const char *on="on";
405    char *buffer=(char *)GB_calloc(2000,sizeof(char));
406//     GB_push_transaction(GLOBAL_gb_main);
407
408    GBDATA *gb_extended = GBT_find_or_create_SAI(GLOBAL_gb_main,savename);
409    GBDATA *gb_data = GBT_add_data(gb_extended, align,"data", GB_STRING);
410    err = GB_write_string(gb_data,result);
411    GBDATA *gb_options= GBT_add_data(gb_extended, align,"_TYPE", GB_STRING);
412
413    const char *allvsmarked="all";
414    if(onlymarked) allvsmarked="marked";
415    const char *countgapsstring=off;
416    if(countgaps) countgapsstring=on;
417    const char *simplifystring=off;
418    if(groupallowed) simplifystring=on;
419
420    sprintf(buffer,"CON: [species: %s]  [number: %ld]  [count gaps: %s] "
421            "[threshold for gaps: %d]  [simplify: %s] "
422            "[threshold for character: %f]  [upper: %f]  [lower: %d]",
423            allvsmarked,nrofspecies,countgapsstring,
424            gapbound,simplifystring,
425            fconsidbound,fupper,lower);
426
427    err=GB_write_string(gb_options,buffer);
428
429    GBDATA *gb_names = GB_search(GB_get_father(gb_options), "_SPECIES",GB_FIND);
430    if(gb_names) GB_delete(gb_names); /* delete old entry */
431
432    if (nrofspecies<20) {
433        GBDATA        *gb_species;
434        GBS_strstruct *strstruct = GBS_stropen(1000);
435
436        if (onlymarked) gb_species = GBT_first_marked_species(GLOBAL_gb_main);
437        else gb_species            = GBT_first_species(GLOBAL_gb_main);
438       
439        while(gb_species) {
440            if(GBT_read_sequence(gb_species,align)) {
441                GBDATA     *gb_speciesname = GB_search(gb_species,"name",GB_FIND);
442                const char *name           = GB_read_char_pntr(gb_speciesname);
443               
444                GBS_strcat(strstruct,name);
445                GBS_chrcat(strstruct,' ');
446            }
447            if (onlymarked) gb_species = GBT_next_marked_species(gb_species);
448            else gb_species            = GBT_next_species(gb_species);
449        }
450
451        char *allnames = GBS_strclose(strstruct);
452        err            = GBT_write_string(GB_get_father(gb_options), "_SPECIES", allnames);
453        free(allnames);
454    }
455
456    {
457        char buffer2[256];
458        sprintf(buffer2,"%s/FREQUENCIES", align);
459        GBDATA *gb_graph = GB_search(gb_extended, buffer2, GB_FIND);
460        if(gb_graph) GB_delete(gb_graph);   /* delete old entry */
461    }
462    /* export additional information */
463    if(resultiscomplex)
464    {
465        GBDATA *gb_graph = GBT_add_data(gb_extended, align,"FREQUENCIES", GB_DB);
466        char *charname=(char *)GB_calloc(5,sizeof(char));
467
468        float **additional=0;
469        /* problem : aminos, especially '*' -> new order*/
470
471        int *allreadycounted=(int*)GB_calloc((unsigned int)256,sizeof(char));
472        int *neworder=(int*)GB_calloc((unsigned int)256,sizeof(int));
473        int k;
474        int numdiffchars=1;  /* first additional row (nr. 0) is max-row */
475        for(int c=0;c<256;c++)
476        {
477            if( (k=convtable[c]) )
478            {
479                if(!(allreadycounted[k]))
480                {
481                    allreadycounted[k]=1;
482                    neworder[numdiffchars++]=k;
483                }
484            }
485        }
486
487        additional=(float**)GB_calloc((unsigned int)numdiffchars,sizeof(float*));
488        int group;
489        for(group=0;group<numdiffchars;group++)
490        {
491            additional[group]=(float*)GB_calloc((unsigned int)maxalignlen,
492                                                sizeof(float));
493        }
494
495        int *absolutrow=(int*)GB_calloc((unsigned int)maxalignlen,sizeof(int));
496        long col;
497        for(col=0;col<maxalignlen;col++)
498        {
499            int group2=1;
500            int colsum=0;
501            while(neworder[group2])
502            {
503                colsum+=statistic[neworder[group2++]][col];
504            }
505            if(countgaps) colsum+=statistic[0][col];
506            absolutrow[col]=colsum;
507        }
508
509        for(col=0;col<maxalignlen;col++)
510        {
511            int group2=1;
512            float highest=0,relativ;
513            int diffchar;
514            if (absolutrow[col]){
515                while( (diffchar=neworder[group2++]) ){
516                    relativ=(float)statistic[diffchar][col]
517                        /(float)absolutrow[col];
518                    if(relativ>highest) highest=relativ;
519                    additional[diffchar][col]=relativ;
520                }
521                additional[0][col]=highest;
522            }else{
523                additional[0][col]=0.0;
524            }
525        }
526
527        GBDATA *gb_relativ=GB_search(gb_graph,"MAX",GB_FLOATS);
528        err=GB_write_floats(gb_relativ,additional[0],maxalignlen);
529
530        for(group=1;group<numdiffchars;group++)
531        {
532            char ch = groupnames[neworder[group]];
533            if (ch <'A' || ch>'Z') continue;
534            sprintf(charname,"N%c",ch);
535            gb_relativ=GB_search(gb_graph,charname,GB_FLOATS);
536            err=GB_write_floats(gb_relativ,additional[group],maxalignlen);
537        }
538
539        free(charname);
540        free(neworder);
541        free(allreadycounted);
542        for(group=0;group<numdiffchars;group++) free(additional[group]);
543        free(additional);
544    }
545    free(buffer);
546    return err;
547}
548
549
550void CON_cleartables(int **statistic, int isamino){
551    int i;
552    int no_of_tables = isamino ? MAX_AMINOS : MAX_BASES;
553
554    for (i = 0; i<no_of_tables; ++i) {
555        free(statistic[i]);
556    }
557}
558
559/* -----------------------------------------------------------------
560 * Function:                     void CON_calculate_cb(AW_window *aw )
561 *
562 * Arguments:                    .
563 *
564 * Returns:                      .
565 *
566 * Description:                  main callback
567 *                               This function calculates the consensus.
568 *                               Function CON_makestatistic creates the
569 *                               statistic and function CON_evaluatestatistic
570 *                               evaluates the statistic. Then the result
571 *                               string(s) are written to the extended data of
572 *                               the alignment.
573 * NOTE:                         .
574 *
575 * Global Variables referenced:  .
576 *
577 * Global Variables modified:    x
578 *
579 * AWARs referenced:
580 *                  AW_STRING, "tmp/con/alignment"     : name of alignment
581 *                  AW_STRING, "tmp/con/which_species" : only marked species ?
582 *                  AW_STRING, "con/group"         : allow grouping ?
583 *                  AW_STRING, "con/countgaps"
584 *                  AW_DOUBLE, "con/fupper"
585 *                  AW_INT, "con/lower"
586 *                  AW_INT, "con/gapbound"  : result is '-' if more than
587 *                                            'gapbound' per cent gaps
588 *                  AW_DOUBLE, "con/fconsidbound" : if frequency of character is
589 *                                     more than 'considbound' per cent, this
590 *                                     character can contribute to groups
591 *                  AW_STRING, "tmp/intcon/name"   : save with this name
592 *                  AW_STRING, "con/result" : result has one or more lines ?
593 *
594 * AWARs modified:               x
595 *
596 * Dependencies:                 CON_maketables
597 *                               CON_makestatistic
598 *                               CON_makegrouptable
599 *                               CON_evaluatestatistic
600 * -----------------------------------------------------------------
601 */
602void CON_calculate_cb(AW_window *aw )
603{
604    AW_root  *awr   = aw->get_root();
605    char     *align = awr->awar("tmp/con/alignment")->read_string();
606    GB_ERROR  error = 0;
607
608    GB_push_transaction(GLOBAL_gb_main);
609
610    long maxalignlen = GBT_get_alignment_len(GLOBAL_gb_main,align);
611    if (maxalignlen <= 0) error = GB_export_errorf("alignment '%s' doesn't exist", align);
612
613    if (!error) {
614        int isamino         = GBT_is_alignment_protein(GLOBAL_gb_main,align);
615        int onlymarked      = 1;
616        int resultiscomplex = 1;
617
618        {
619            char *marked        = awr->awar("tmp/con/which_species")->read_string();
620            char *complexresult = awr->awar("con/result")->read_string();
621
622            if (strcmp("marked",marked)         != 0) onlymarked = 0;
623            if (strcmp("complex",complexresult) != 0) resultiscomplex = 0;
624
625            free(complexresult);
626            free(marked);
627        }
628
629
630        /* creating the table for characters and allocating memory for 'statistc' */
631        int *statistic[MAX_AMINOS+1];
632        int  convtable[256];
633        CON_maketables(convtable,statistic,maxalignlen,isamino);
634
635        /* filling the statistic table */
636        aw_openstatus("Consensus");
637        long   nrofspecies = CON_makestatistic(statistic,convtable,align,onlymarked);
638        double fupper      = awr->awar("con/fupper")->read_float();
639        int    lower       = (int)awr->awar("con/lower")->read_int();
640
641        if (fupper>100.0)   fupper = 100;
642        if (fupper<0)       fupper = 0;
643        if (lower<0)        lower  = 0;
644
645        if(lower>fupper) {
646            error = "fault: lower greater than upper";
647        }
648        else {
649
650            double fconsidbound                = awr->awar("con/fconsidbound")->read_float();
651            if (fconsidbound>100) fconsidbound = 100;
652            if (fconsidbound<0)   fconsidbound = 0;
653
654            int gapbound               = (int)awr->awar("con/gapbound")->read_int();
655            if (gapbound<0)   gapbound = 0;
656            if (gapbound>100) gapbound = 100;
657
658            int groupallowed, countgap;
659            {
660                char *group     = awr->awar("con/group")->read_string();
661                char *countgaps = awr->awar("con/countgaps")->read_string();
662
663                groupallowed = strcmp("on",group) == 0;
664                countgap     = strcmp("on",countgaps) == 0;
665
666                free(countgaps);
667                free(group);
668            }
669
670            /* creating the table for groups */
671            char *groupflags[40];
672            char  groupnames[40];
673            int   numgroups = CON_makegrouptable(groupflags,groupnames,isamino,groupallowed);
674
675            /* calculate and export the result strings */
676            char *result = 0;
677            CON_evaluatestatistic(result,statistic,groupflags,groupnames, (int)maxalignlen,fupper,lower,fconsidbound,gapbound,countgap,numgroups);
678
679            char *savename = awr->awar("tmp/con/name")->read_string();
680
681            error = CON_export(savename,align,statistic,result,convtable,groupnames,
682                               onlymarked,nrofspecies,maxalignlen,countgap,gapbound,groupallowed,
683                               fconsidbound,fupper,lower,resultiscomplex);
684
685            /* freeing allocated memory */
686            free(savename);
687            free(result);
688            for (int i=0;i<MAX_GROUPS;i++) free(groupflags[i]);
689        }
690
691        aw_closestatus();
692        CON_cleartables(statistic,isamino);
693    }
694
695    free(align);
696    GB_end_transaction_show_error(GLOBAL_gb_main, error, aw_message);
697}
698
699void create_consensus_var(AW_root *aw_root, AW_default aw_def)
700{
701    GB_transaction dummy(GLOBAL_gb_main);
702    {
703        char *defali = GBT_get_default_alignment(GLOBAL_gb_main);
704        aw_root->awar_string( "tmp/con/alignment", defali ,aw_def);
705        free(defali);
706    }
707    aw_root->awar_string( "tmp/con/which_species","marked",aw_def );
708    aw_root->awar_string( "con/group","off",aw_def);
709    aw_root->awar_string( "con/countgaps","on",aw_def);
710    aw_root->awar_float( "con/fupper",95,aw_def);
711    aw_root->awar_int( "con/lower",70,aw_def);
712    aw_root->awar_int( "con/gapbound",60,aw_def);
713    aw_root->awar_float( "con/fconsidbound",30,aw_def);
714    aw_root->awar_string( "tmp/con/name","CONSENSUS",aw_def);
715    aw_root->awar_string( "con/result","single line",aw_def );
716
717    aw_root->awar_string( AWAR_MAX_FREQ_SAI_NAME,"MAX_FREQUENCY",aw_def );
718    aw_root->awar_int( AWAR_MAX_FREQ_NO_GAPS,1,aw_def);
719
720}
721
722/* Open window to show IUPAC tables */
723AW_window *
724CON_showgroupswin_cb( AW_root *aw_root)
725{
726    AW_window_simple *aws = new AW_window_simple;
727    aws->init( aw_root, "SHOW_IUPAC", "Show IUPAC");
728    aws->load_xfig("consensus/groups.fig");
729    aws->button_length( 7 );
730
731    aws->at("ok");aws->callback((AW_CB0)AW_POPDOWN);
732    aws->create_button("CLOSE","CLOSE","O");
733
734    return (AW_window *)aws;
735}
736
737AW_window *
738AP_open_con_expert_window( AW_root *aw_root)
739{
740    AW_window_simple *aws = new AW_window_simple;
741    aws->init( aw_root, "BUILD_CONSENSUS", "CONSENSUS OF SEQUENCES");
742    aws->load_xfig("consensus/expert.fig");
743    aws->button_length( 6 );
744
745    aws->at("cancel");aws->callback((AW_CB0)AW_POPDOWN);
746    aws->create_button("CLOSE","CLOSE","C");
747
748    aws->at("help");aws->callback(AW_POPUP_HELP,(AW_CL)"consensus.hlp");
749    aws->create_button("HELP","HELP","H");
750
751    aws->button_length( 10);
752    aws->at("showgroups");aws->callback(AW_POPUP,(AW_CL)CON_showgroupswin_cb,0);
753    aws->create_button("SHOW_IUPAC", "show\nIUPAC...","s");
754    aws->button_length( 10 );
755
756    aws->at( "which_species" );
757    aws->create_toggle_field( "tmp/con/which_species", NULL ,"" );
758    aws->insert_toggle( "all", "1", "all" );
759    aws->insert_default_toggle( "marked",  "1", "marked" );
760    aws->update_toggle_field();
761
762    aws->at("which_alignment");
763    awt_create_selection_list_on_ad(GLOBAL_gb_main,
764                                    (AW_window *)aws,"tmp/con/alignment","*=");
765
766    aws->button_length( 15 );
767
768    /* activation of consensus calculation by button ... */
769    aws->at("calculate");aws->callback((AW_CB0)CON_calculate_cb);
770    aws->create_button("GO","GO","G");
771
772    aws->at("group");
773    aws->create_toggle_field("con/group", NULL ,"" );
774    aws->insert_toggle("on","1","on");
775    aws->insert_default_toggle("off","1","off");
776    aws->update_toggle_field();
777
778    aws->at("countgaps");
779    aws->create_toggle_field("con/countgaps", NULL ,"" );
780    aws->insert_toggle("on","1","on");
781    aws->insert_default_toggle("off","1","off");
782    aws->update_toggle_field();
783
784    aws->at("upper");
785    aws->create_input_field("con/fupper",4);
786
787    aws->at("lower");
788    aws->create_input_field("con/lower",4);
789
790    aws->at("considbound");
791    aws->create_input_field("con/fconsidbound",4);
792
793    aws->at("gapbound");
794    aws->create_input_field("con/gapbound",4);
795
796    aws->at("name");
797    aws->create_input_field("tmp/con/name",10);
798
799    aws->at("result");
800    aws->create_toggle_field("con/result", NULL ,"" );
801    aws->insert_toggle("single line","1","single line");
802    aws->insert_default_toggle("complex","1","complex");
803    aws->update_toggle_field();
804
805    aws->at("save_box");
806    awt_create_selection_list_on_extendeds(GLOBAL_gb_main,aws,"tmp/con/name");
807
808    return (AW_window *)aws;
809}
810
811
812/* -----------------------------------------------------------------
813 * Function:           AP_open_consensus_window( AW_root *aw_root)
814 *
815 * Arguments:
816 *
817 * Returns:
818 *
819 * Description:       Draws window, initializes callback for most important
820 *                    function, CON_calculate_cb.
821 *
822 * NOTE:
823 *
824 * Global Variables referenced:
825 *
826 * Global Variables modified:
827 *
828 * AWARs referenced:
829 *
830 * AWARs modified:
831 *
832 * Dependencies:      Needs xfig files consens.fig and CON_groups.fig
833 * -----------------------------------------------------------------
834 */
835AW_window *
836AP_open_consensus_window( AW_root *aw_root)
837{
838    AW_window_simple *aws = new AW_window_simple;
839    aws->init( aw_root, "SIMPLE_CONSENSUS", "SIMPLE CONSENSUS");
840    aws->load_xfig("consensus/main.fig");
841
842    GB_push_transaction(GLOBAL_gb_main);
843
844    aws->button_length( 6 );
845
846    aws->at("cancel");aws->callback((AW_CB0)AW_POPDOWN);
847    aws->create_button("CLOSE","CLOSE","C");
848
849    aws->at("help");aws->callback(AW_POPUP_HELP,(AW_CL)"consensus.hlp");
850    aws->create_button("HELP","HELP","H");
851
852    aws->button_length( 15);
853    aws->at("showgroups");aws->callback(AW_POPUP,(AW_CL)CON_showgroupswin_cb,0);
854    aws->create_button("SHOW_IUPAC", "show IUPAC...","s");
855    aws->button_length( 10 );
856
857    aws->at( "which_species" );
858    aws->create_toggle_field( "tmp/con/which_species", NULL ,"" );
859    aws->insert_toggle( "all", "1", "all" );
860    aws->insert_default_toggle( "marked",  "1", "marked" );
861    aws->update_toggle_field();
862
863    aws->at("which_alignment");
864    awt_create_selection_list_on_ad(GLOBAL_gb_main,
865                                    (AW_window *)aws,"tmp/con/alignment","*=");
866
867    aws->button_length( 15 );
868
869    /* activation of consensus calculation by button ... */
870    aws->at("calculate");aws->callback((AW_CB0)CON_calculate_cb);
871    aws->create_button("CALCULATE","CALCULATE","C");
872
873    aws->at("expert");aws->callback(AW_POPUP,(AW_CL)AP_open_con_expert_window,0);
874    aws->create_button("EXPERT", "expert...","e");
875
876    aws->at("group");
877    aws->create_toggle_field("con/group", NULL ,"" );
878    aws->insert_toggle("on","1","on");
879    aws->insert_default_toggle("off","1","off");
880    aws->update_toggle_field();
881
882    aws->at("name");
883    aws->create_input_field("tmp/con/name",10);
884
885    aws->at("save_box");
886    awt_create_selection_list_on_extendeds(GLOBAL_gb_main,aws,"tmp/con/name");
887
888    GB_pop_transaction(GLOBAL_gb_main);
889
890    return (AW_window *)aws;
891}
892
893
894/* -----------------------------------------------------------------
895 * Function:           CON_calc_max_freq_cb( AW_window *aw)
896 *
897 * Description:       Gets the maximum frequence for each columns.
898 *
899 * NOTE:
900 *
901 * -----------------------------------------------------------------
902 */
903
904void CON_calc_max_freq_cb(AW_window *aw){
905
906    AW_root *awr=aw->get_root();
907    long maxalignlen,i;
908    int *statistic[MAX_AMINOS+1];
909    int convtable[256];
910    int onlymarked=1,isamino=1;
911    long no_gaps;
912
913    GB_ERROR  error       = 0;
914    GB_push_transaction(GLOBAL_gb_main);
915
916    char *align = GBT_get_default_alignment(GLOBAL_gb_main);
917    maxalignlen = GBT_get_alignment_len(GLOBAL_gb_main,align);
918    no_gaps     = awr->awar(AWAR_MAX_FREQ_NO_GAPS)->read_int();
919
920    if(maxalignlen<=0) {
921        GB_pop_transaction(GLOBAL_gb_main);
922        aw_message("alignment doesn't exist!");
923        delete align;
924        return;
925    }
926    isamino = GBT_is_alignment_protein(GLOBAL_gb_main,align);
927
928    aw_openstatus("Max. Frequency");
929    long nrofspecies;
930
931    CON_maketables(convtable,statistic,maxalignlen,isamino);
932    nrofspecies = CON_makestatistic(statistic,convtable,align,onlymarked);
933
934    int   end            = MAX_BASES;
935    if (isamino) end     = MAX_AMINOS;
936    int   pos;
937    char *result         = new char[maxalignlen+1];
938    char *result2        = new char[maxalignlen+1];
939    result[maxalignlen]  = 0;
940    result2[maxalignlen] = 0;
941
942    for (pos = 0 ; pos < maxalignlen; pos++ ){
943        int sum                               = 0;
944        int max                               = 0;
945        for (i=0;i<end;i++) {
946            if (no_gaps && (i == convtable[(unsigned char)'-']) ) continue;
947            sum                              += statistic[i][pos];
948            if (statistic[i][pos] > max) max  = statistic[i][pos];
949        }
950        if (sum == 0){
951            result[pos] = '=';
952            result2[pos] = '=';
953        }else{
954            result[pos] = "01234567890"[((10*max)/sum)%11];
955            result2[pos] = "01234567890"[((100*max)/sum)%10];
956        }
957    }
958
959    char   *savename    = awr->awar(AWAR_MAX_FREQ_SAI_NAME)->read_string();
960    GBDATA *gb_extended = GBT_find_or_create_SAI(GLOBAL_gb_main,savename);
961    free(savename);
962    GBDATA *gb_data     = GBT_add_data(gb_extended, align,"data", GB_STRING);
963    GBDATA *gb_data2    = GBT_add_data(gb_extended, align,"dat2", GB_STRING);
964    error               = GB_write_string(gb_data,result);
965    if (!error) error   = GB_write_string(gb_data2,result2);
966    GBDATA *gb_options  = GBT_add_data(gb_extended, align,"_TYPE", GB_STRING);
967    delete [] result;
968    delete [] result2;
969
970    if (!error) {
971        char    buffer[2000];
972        sprintf (buffer,"MFQ: [species: %li] [exclude gaps: %li]",
973                 nrofspecies, no_gaps);
974        error=GB_write_string(gb_options,buffer);
975    }
976
977    GB_pop_transaction(GLOBAL_gb_main);
978
979    CON_cleartables(statistic,isamino);
980    aw_closestatus();
981    free(align);
982    if (error) aw_message(error);
983}
984
985AW_window *
986AP_open_max_freq_window( AW_root *aw_root)
987{
988    AW_window_simple *aws = new AW_window_simple;
989    aws->init( aw_root, "MAX_FREQUENCY", "MAX FREQUENCY");
990    aws->load_xfig("consensus/max_freq.fig");
991
992    GB_push_transaction(GLOBAL_gb_main);
993
994    aws->button_length( 6 );
995
996    aws->at("cancel");aws->callback((AW_CB0)AW_POPDOWN);
997    aws->create_button("CLOSE","CLOSE","C");
998
999    aws->at("help");aws->callback(AW_POPUP_HELP,(AW_CL)"max_freq.hlp");
1000    aws->create_button("HELP","HELP","H");
1001
1002    /* activation of consensus calculation by button ... */
1003    aws->at("go");aws->callback((AW_CB0)CON_calc_max_freq_cb);
1004    aws->create_button("GO","GO","C");
1005
1006    aws->at("save");
1007    aws->create_input_field(AWAR_MAX_FREQ_SAI_NAME,1);
1008
1009    aws->at("sai");
1010    awt_create_selection_list_on_extendeds(GLOBAL_gb_main,aws,AWAR_MAX_FREQ_SAI_NAME);
1011
1012    aws->at("gaps");
1013    aws->create_toggle(AWAR_MAX_FREQ_NO_GAPS);
1014
1015    GB_pop_transaction(GLOBAL_gb_main);
1016
1017    return (AW_window *)aws;
1018}
1019
Note: See TracBrowser for help on using the repository browser.