source: tags/initial/PHYLO/PH_data.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: 8.2 KB
Line 
1#include "include.hxx"
2
3
4PHDATA::PHDATA(AW_root *awr)
5{ memset((char *)this,0,sizeof(PHDATA));
6 aw_root = awr;
7}
8
9char *PHDATA::unload(void)
10{ struct PHENTRY *phentry;
11
12 free(use);
13 use = 0;
14 for(phentry=entries;phentry;phentry=phentry->next) 
15 { free(phentry->name);
16 free(phentry->full_name);
17 free((char *) phentry); 
18 }
19 entries = 0;
20 nentries = 0;
21 return 0;     
22}
23
24PHDATA::~PHDATA(void)
25{ unload();
26 delete matrix;
27}
28
29
30char *PHDATA::load(char *usei)
31{
32    GBDATA *gb_species,*gb_ali,*gb_name,*gb_full_name;
33    struct PHENTRY *phentry,*hentry;
34
35    this->use = strdup(usei);
36    this->gb_main = ::gb_main;
37    last_key_number=0;
38 
39    GB_push_transaction(gb_main);
40    seq_len = GBT_get_alignment_len(gb_main,use);
41    entries=NULL;
42    phentry=NULL;
43    nentries = 0;
44    for (       gb_species = GBT_first_marked_species(gb_main);
45            gb_species;
46            gb_species = GBT_next_marked_species(gb_species)) {
47        gb_ali = GB_find(gb_species, use, 0, down_level);
48        if (!gb_ali)    continue;
49        //no existing alignmnet for this species
50        hentry = new PHENTRY;
51        hentry->next = NULL;
52        hentry->gb_species_data_ptr = GB_find(gb_ali, "data", 0, down_level);
53        hentry->key = last_key_number++;
54        if (hentry->gb_species_data_ptr) {
55            gb_name = GB_find(gb_species, "name", 0, down_level);
56            hentry->name = GB_read_string(gb_name);
57            gb_full_name = GB_find(gb_species, "full_name", 0, down_level);
58            if (gb_full_name)   hentry->full_name = GB_read_string(gb_full_name);
59            else                        hentry->full_name = NULL;
60            if (!entries) {
61                entries = hentry;
62                entries->prev = NULL;
63                phentry = entries;
64            } else {
65                phentry->next = hentry;
66                hentry->prev = phentry;
67                phentry = hentry;
68            }
69            nentries++;
70        } else {
71            delete          hentry;
72        }
73    }
74    GB_pop_transaction(gb_main);
75    hash_elements=(struct PHENTRY **) calloc(nentries,sizeof(struct PHENTRY *));
76    phentry=entries;
77    {
78        unsigned int i;
79        for (i = 0; i < nentries; i++) {
80            hash_elements[i] = phentry;
81            phentry = phentry->next;
82        }
83    }
84
85    return 0;
86}
87
88GB_ERROR PHDATA::save(char *filename)
89{
90    FILE *out;
91
92    out = fopen(filename,"w");
93    if (!out) {
94        return "Cannot save your File";
95    }
96    unsigned row,col;
97    fprintf(out,"%i\n",nentries);
98    for (row = 0; row<nentries;row++){
99        fprintf(out,"%-13s",hash_elements[row]->name);
100        for (col=0; col<=row; col++) {
101            fprintf(out,"%7.4f ",matrix->get(row,col)*100.0);
102        }
103        fprintf(out,"\n");
104    }
105    fclose(out);
106    return 0;
107}
108
109void PHDATA::print()
110{
111    unsigned row,col;
112    printf("    %i\n",nentries);
113    for (row = 0; row<nentries;row++){
114        printf("%-10s ",hash_elements[row]->name);
115        for (col=0; col<row; col++) {
116            printf("%6f ",matrix->get(row,col));
117        }
118        printf("\n");
119    }
120    printf("\n");
121}
122
123
124GB_ERROR PHDATA::calculate_matrix(const char */*cancel*/,double /*alpha*/,PH_TRANSFORMATION /*transformation*/)
125{
126    if(nentries<=1) return "There are no species selected";
127 
128    char *filter;
129    matrix = new AP_smatrix(nentries);
130    long i,j,column,reference_table[256];
131    long options_vector[4];
132    const char *real_chars,*low_chars,*rest_chars;
133    char all_chars[100],*sequence_bufferi,*sequence_bufferj;
134    AW_BOOL compare[256];
135    AP_FLOAT number_of_comparisons;
136    double gauge;
137
138    AW_BOOL bases_used;     // rna oder dna sequence : nur zum testen und Entwicklung
139
140    bases_used=AW_TRUE;
141    if (!PHDATA::ROOT) return "nothing loaded yet";
142    aw_root=PH_used_windows::windowList->phylo_main_window->get_root();
143    if (bases_used) {
144        real_chars="ACGTU";
145        low_chars="acgtu";
146        rest_chars="MRWSYKVHDBXNmrwsykvhdbxn";
147       
148        strcpy(all_chars,real_chars);
149        strcat(all_chars,low_chars);
150        strcat(all_chars,rest_chars);
151    }
152    else {
153        real_chars="ABCDEFGHIKLMNPQRSTVWYZ";
154        low_chars=0;
155        rest_chars="X";
156       
157        strcpy(all_chars,real_chars);
158        strcat(all_chars,rest_chars);
159    }
160    strcat(all_chars,".-");
161 
162    // initialize variables
163
164    options_vector[0]=aw_root->awar("phyl/matrix/point")->read_int();
165    options_vector[1]=aw_root->awar("phyl/matrix/minus")->read_int();
166    options_vector[2]=aw_root->awar("phyl/matrix/rest")->read_int();
167    options_vector[3]=aw_root->awar("phyl/matrix/lower")->read_int();
168
169 
170    for(i=0;i<256;i++) compare[i]=AW_FALSE;
171    for(i=0;i<long(strlen(real_chars));i++) compare[real_chars[i]]=AW_TRUE;
172    for(i=0;i<long(strlen(all_chars));i++) reference_table[all_chars[i]]=i;
173   
174    // rna or dna sequence: set synonymes
175    if(bases_used) {
176        reference_table['U']=reference_table['T'];  /* T=U */
177        reference_table['u']=reference_table['t'];
178        reference_table['N']=reference_table['X'];
179        reference_table['n']=reference_table['x'];
180    }
181 
182    distance_table = new AP_smatrix(strlen(all_chars));
183    for(i=0;i<long(strlen(all_chars));i++) {
184        for(j=0;j<long(strlen(all_chars));j++) {
185            distance_table->set(i,j,(reference_table[i]==reference_table[j]) ? 0.0 : 1.0);
186        }
187    }
188 
189    if(bases_used)  /* set substitutions T = U ... */
190    {
191        distance_table->set(reference_table['N'],reference_table['X'],0.0);
192        distance_table->set(reference_table['n'],reference_table['x'],0.0);
193    }
194    distance_table->set(reference_table['.'],reference_table['-'],0.0);
195 
196    filter=strdup(aw_root->awar("phyl/filter/filter")->read_string());
197 
198    // set compare-table according to options_vector
199    switch(options_vector[0]) // '.' in column
200    {
201        case 0:  // forget pair
202            // do nothing: compare stays AW_FALSE
203            break;
204        case 1: 
205            compare['.']=AW_TRUE;
206            break;
207    }
208    switch(options_vector[1]) // '-' in column
209    {
210        case 0:  // forget pair
211            // do nothing: compare stays AW_FALSE
212            break;
213        case 1: 
214            compare['-']=AW_TRUE;
215            break;
216    }
217    switch(options_vector[2]) // '.' in column
218    {
219        case 0:  // forget pair
220            // do nothing: compare stays AW_FALSE
221            break;
222        case 1:
223            for(i=0;i<long(strlen(rest_chars));i++) compare[rest_chars[i]]=AW_TRUE;
224            break;
225    }
226    if(bases_used) {
227        switch(options_vector[1]) // '-' in column
228        {
229            case 0:  // forget pair
230                // do nothing: compare stays AW_FALSE
231                break;
232            case 1:
233                for(i=0;i<long(strlen(low_chars));i++) compare[low_chars[i]]=AW_TRUE;
234                break;
235        }
236    }
237   
238   
239    // counting routine
240    aw_openstatus("Calculating Matrix");
241    aw_status("Calculate the matrix");
242    sequence_bufferi = 0;
243    sequence_bufferj = 0;
244    GB_transaction dummy(PHDATA::ROOT->gb_main);
245       
246    for (i = 0; i < long(nentries); i++) {
247        gauge = (double) i / (double) nentries;
248        if (aw_status(gauge * gauge)) return 0;
249        delete sequence_bufferi;
250        sequence_bufferi = GB_read_string(hash_elements[i]->gb_species_data_ptr);
251        for (j = i + 1; j < long(nentries); j++) {
252            number_of_comparisons = 0.0;
253            delete sequence_bufferj;
254            sequence_bufferj = GB_read_string(hash_elements[j]->gb_species_data_ptr);
255            for (column = 0; column < seq_len; column++) {
256                if (compare[sequence_bufferi[column]] && compare[sequence_bufferj[column]] &&
257                    filter[column]) {
258                    matrix->set(i, j,
259                                matrix->get(i, j) +
260                                distance_table->get(reference_table[sequence_bufferi[column]],
261                                                    reference_table[sequence_bufferj[column]]));
262                    number_of_comparisons++;
263                } //if
264            } //for column
265            if (number_of_comparisons) {
266                matrix->set(i, j, (matrix->get(i, j) / number_of_comparisons));
267            }
268        } //for j
269    } //for i
270    delete sequence_bufferi;
271    delete          sequence_bufferj;
272    aw_closestatus();
273       
274    return 0;
275}
Note: See TracBrowser for help on using the repository browser.