source: branches/port5/PHYLO/PH_data.cxx

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