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