source: tags/initial/NALIGNER/ali_profile.hxx

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