source: branches/port5/NALIGNER/ali_profile.hxx

Last change on this file was 4912, checked in by westram, 18 years ago
  • untabified + indented
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 KB
Line 
1#ifndef _ALI_PROFILE_INC_
2#define _ALI_PROFILE_INC_
3
4#include "ali_sequence.hxx"
5#include "ali_arbdb.hxx"
6#include "ali_pt.hxx"
7#include "ali_tlist.hxx"
8#include "ali_misc.hxx"
9
10#define ALI_PROFILE_BORDER_LEFT '['
11#define ALI_PROFILE_BORDER_RIGHT ']'
12
13typedef void ALI_MAP_; /* make modul independent */
14
15
16typedef struct {
17    ALI_ARBDB *arbdb;
18    ALI_PT *pt;
19       
20    int find_family_mode;
21
22    int exclusive_flag;
23    int mark_family_flag;
24    int mark_family_extension_flag;
25
26    int min_family_size;
27    int max_family_size;
28
29    float min_weight;
30    float ext_max_weight;
31
32    float multi_gap_factor;
33
34    float insert_factor;
35    float multi_insert_factor;
36
37    double substitute_matrix[5][5]; /* a c g u - */
38    double binding_matrix[5][5]; /* a c g u - */
39
40} ALI_PROFILE_CONTEXT;
41
42
43/*
44 * Class for a family member
45 */
46class ali_family_member {
47public:
48    ALI_SEQUENCE *sequence;
49    float matches;
50    float weight;
51 
52    ali_family_member(ALI_SEQUENCE *seq, float m, float w = 0.0) {
53        sequence = seq;
54        matches = m;
55        weight = w;
56    }
57};
58
59
60/*
61 * Class for the profiling
62 */
63class ALI_PROFILE {
64    ALI_NORM_SEQUENCE *norm_sequence;
65    unsigned long prof_len;
66
67    long **helix;                    /* base to base connection */
68    char **helix_borders;            /* borders of the helix '[' and ']' */
69    unsigned long helix_len;       
70
71    float (**base_weights)[4];         /* relative weight of base i */
72    float (**sub_costs)[6];            /* costs to substitute with base i */
73    float sub_costs_maximum;
74
75    float insert_cost;
76    float multi_insert_cost;
77
78    float multi_gap_factor;
79
80    float (*binding_costs)[5][5];       /* Matrix for binding costs a c g u - */
81    float w_bind_maximum;
82
83    unsigned long *lmin, *lmax; 
84    float ***gap_costs;
85    float ***gap_percents;
86
87    int is_binding_marker(char c);
88
89    ALI_TLIST<ali_family_member *> *find_family(ALI_SEQUENCE *sequence, 
90                                                ALI_PROFILE_CONTEXT *context);
91    void calculate_costs(ALI_TLIST<ali_family_member *> *family_list,
92                         ALI_PROFILE_CONTEXT *context);
93
94    int find_next_helix(char h[], unsigned long h_len, unsigned long pos,
95                        unsigned long *helix_nr,
96                        unsigned long *start, unsigned long *end);
97    int find_comp_helix(char h[], unsigned long h_len, unsigned long pos,
98                        unsigned long helix_nr,
99                        unsigned long *start, unsigned long *end);
100    void delete_comp_helix(char h1[], char h2[], unsigned long h_len,
101                           unsigned long start, unsigned long end);
102    int map_helix(char h[], unsigned long h_len,
103                  unsigned long start1, unsigned long end1,
104                  unsigned long start2, unsigned long end2);
105    void initialize_helix(ALI_PROFILE_CONTEXT *context);
106
107public:
108
109    ALI_PROFILE(ALI_SEQUENCE *sequence, ALI_PROFILE_CONTEXT *context);
110    ~ALI_PROFILE(void);
111
112    void print(int start = -1, int end = -1)
113    {
114        int i;
115        size_t j;
116
117        if (start < 0 || (unsigned long)(start) > prof_len - 1)
118            start = 0;
119
120        if ((unsigned long)(end) > prof_len - 1 || end < 0)
121            end = (int)prof_len - 1;
122
123        if (start > end) {
124            i = start;
125            start = end;
126            end = i;
127        }
128                       
129
130        printf("\nProfile from %d to %d\n",start,end);
131        printf("Substitutions kosten:\n");
132        for (i = start; i <= end; i++) {
133            printf("%2d: ",i);
134            for (j = 0; j < 6; j++)
135                printf("%4.2f ",(*sub_costs)[i][j]);
136            printf("\n");
137        }
138
139        printf("\nGap Bereiche:\n");
140        for (i = start; i <= end; i++) {
141            printf("%2d: %3ld %3ld\n",i,lmin[i],lmax[i]);
142            printf("  : ");
143            for (j = 0; j <= lmax[i] - lmin[i] + 1; j++)
144                printf("%4.2f ",(*gap_percents)[i][j]);
145            printf("\n  : ");
146            for (j = 0; j <= lmax[i] - lmin[i] + 1; j++)
147                printf("%4.2f ",(*gap_costs)[i][j]);
148            printf("\n");
149        }
150
151    }
152
153    int is_in_helix(unsigned long pos, 
154                    unsigned long *first, unsigned long *last);
155    int is_outside_helix(unsigned long pos, 
156                         unsigned long *first, unsigned long *last);
157    int complement_position(unsigned long pos) {
158        if (pos >= helix_len)
159            return -1;
160        else
161            return (*helix)[pos];
162    }
163    int is_in_helix(unsigned long pos) {
164        long l;
165        if ((*helix_borders)[pos] == ALI_PROFILE_BORDER_LEFT ||
166            (*helix_borders)[pos] == ALI_PROFILE_BORDER_RIGHT)
167            return 1;
168        for (l = (long) pos - 1; l >= 0; l--)
169            switch ((*helix_borders)[l]) {
170                case ALI_PROFILE_BORDER_LEFT:
171                    return 1;
172                case ALI_PROFILE_BORDER_RIGHT:
173                    return 0;
174            }
175        for (l = (long) pos + 1; l < (long) prof_len; l++)
176            switch((*helix_borders)[l]) {
177                case ALI_PROFILE_BORDER_LEFT:
178                    return 0;
179                case ALI_PROFILE_BORDER_RIGHT:
180                    return 1;
181            }
182        return 0;
183    }
184
185    int is_internal_last(unsigned long pos) {
186        return norm_sequence->is_begin(pos + 1);
187    }
188                         
189    char *cheapest_sequence(void);     
190    char *borders_sequence(void) {
191        unsigned long i;
192        char *str, *str_buffer;
193
194        str = (char *) calloc((unsigned int) prof_len, sizeof(char));
195        if (str == 0) ali_fatal_error("Out of memory");
196
197        str_buffer = str;
198        for (i = 0; i < prof_len; i++)
199            switch ((*helix_borders)[i]) {
200                case ALI_PROFILE_BORDER_LEFT: *str++ = '['; break;
201                case ALI_PROFILE_BORDER_RIGHT: *str++ = ']'; break;
202                default: *str++ = ' ';
203            }
204        return str_buffer;
205    }
206
207    unsigned long length(void) {
208        return prof_len;
209    }
210    ALI_NORM_SEQUENCE *sequence(void) {
211        return norm_sequence;
212    }
213    unsigned long sequence_length(void) {
214        return norm_sequence->length();
215    }
216
217    float base_weight(unsigned long pos, unsigned char c) {
218        if (c > 3)
219            ali_fatal_error("Out of range","ALI_PROFILE::base_weight()");
220        return (*base_weights)[pos][c];
221    }
222
223    float w_sub(unsigned long positiony, unsigned long positionx) {
224        return (*sub_costs)[positiony][norm_sequence->base(positionx)];
225    }
226    float w_sub_gap(unsigned long positiony) {
227        return (*sub_costs)[positiony][4];
228    }
229    float w_sub_gap_cheap(unsigned long positiony) {
230        return (*sub_costs)[positiony][4] * multi_gap_factor;
231    }
232    float w_sub_multi_gap_cheap(unsigned long start, unsigned long end) {
233        float costs = 0.0;
234        unsigned long i;
235        for (i = start; i <= end; i++)
236            costs += w_sub_gap(i);
237        return costs * multi_gap_factor;
238    }
239
240    float w_sub_maximum(void) {
241        return sub_costs_maximum;
242    }
243
244    float w_del(unsigned long begin, unsigned long end) {
245        if (begin <= lmin[end])
246            return (*gap_costs)[end][0];
247        else {
248            if (begin <= lmax[end])
249                return (*gap_costs)[end][begin - lmin[end]];
250            else
251                return (*gap_costs)[end][lmax[end] - lmin[end] + 1];
252        }
253    }
254    float w_del_cheap(unsigned long position) {
255        return (*gap_costs)[position][0];
256    }
257    float w_del_multi(unsigned long start, unsigned long end) {
258        float costs = 0.0;
259        unsigned long i;
260        for (i = start; i <= end; i++)
261            costs += w_del(start,i);
262        return costs;
263    }
264    float w_del_multi_cheap(unsigned long start, unsigned long end) {
265        float costs = 0.0;
266        unsigned long i;
267        for (i = start; i <= end; i++)
268            costs += w_del_cheap(i);
269        return costs;
270    }
271    float w_del_multi_unweighted(unsigned long start, unsigned long end) {
272        float costs = 0.0;
273        unsigned long i;
274        for (i = start; i <= end; i++)
275            costs += w_del(i,i);
276        return costs;
277    }
278
279    float w_ins(unsigned long /*positionx*/, unsigned long /*positiony*/) {
280        return insert_cost;
281    }
282    float w_ins_cheap(unsigned long /*positionx*/, unsigned long /*positiony*/) {
283        return multi_insert_cost;
284    }
285    float w_ins_multi_unweighted(unsigned long startx, unsigned long endx) {
286        return (endx - startx + 1) * insert_cost;
287    }
288    float w_ins_multi(unsigned long startx, unsigned long endx) {
289        return insert_cost + (endx - startx)*multi_insert_cost;
290    }
291    float w_ins_multi_cheap(unsigned long startx, unsigned long endx) {
292        return (endx - startx + 1)*multi_insert_cost;
293    }
294
295
296    float gap_percent(unsigned long begin, unsigned long end) {
297        if (begin <= lmin[end])
298            return (*gap_percents)[end][0];
299        else {
300            if (begin <= lmax[end])
301                return (*gap_percents)[end][begin - lmin[end]];
302            else
303                return (*gap_percents)[end][lmax[end] - lmin[end] + 1];
304        }
305    }
306
307    float w_bind(unsigned long pos_1, unsigned char base_1, 
308                 unsigned long pos_2, unsigned char base_2) {
309        int i, j;
310        float cost_in, cost = 0;
311        if (ali_is_real_base_or_gap(base_1)) {
312            if (ali_is_real_base_or_gap(base_2)) {
313                return (*binding_costs)[base_1][base_2];
314            }
315            for (i = 0; i < 4; i++)
316                cost += (*base_weights)[pos_2][i] * (*binding_costs)[base_1][i];
317            return cost;
318        }
319        if (ali_is_real_base_or_gap(base_2)) {
320            for (i = 0; i < 4; i++)
321                cost += (*base_weights)[pos_1][i] * (*binding_costs)[i][base_2];
322            return cost;
323        }
324        for (i = 0; i < 4; i++) {
325            for (j = 0, cost_in = 0; j < 4; j++)
326                cost_in += (*base_weights)[pos_2][j] * (*binding_costs)[i][j];
327            cost += (*base_weights)[pos_1][i] * cost_in;
328        }
329        return cost;
330    }
331               
332    float w_binding(unsigned long first_position_of_sequence,
333                    ALI_SEQUENCE *sequence);
334};
335
336
337#endif
Note: See TracBrowser for help on using the repository browser.