1 | |
---|
2 | |
---|
3 | #ifndef _ALI_SOLUTION_INC_ |
---|
4 | #define _ALI_SOLUTION_INC_ |
---|
5 | |
---|
6 | |
---|
7 | |
---|
8 | // #include <malloc.h> |
---|
9 | |
---|
10 | #include "ali_profile.hxx" |
---|
11 | #include "ali_sequence.hxx" |
---|
12 | #include "ali_tlist.hxx" |
---|
13 | |
---|
14 | class ALI_MAP { |
---|
15 | unsigned long first_seq_base, last_seq_base; |
---|
16 | unsigned long first_ref_base, last_ref_base; |
---|
17 | long **mapping; |
---|
18 | unsigned char **inserted; |
---|
19 | unsigned char **undefined; |
---|
20 | unsigned long insert_counter; |
---|
21 | public: |
---|
22 | ALI_MAP(unsigned long first_seq_base, unsigned long last_seq_base, |
---|
23 | unsigned long first_ref_base, unsigned long last_ref_base); |
---|
24 | ALI_MAP(ALI_MAP *map); |
---|
25 | ~ALI_MAP(void) { |
---|
26 | if (mapping) |
---|
27 | free((char *) mapping); |
---|
28 | if (inserted) |
---|
29 | free((char *) inserted); |
---|
30 | if (undefined) |
---|
31 | free((char *) undefined); |
---|
32 | } |
---|
33 | unsigned long first_base(void) { |
---|
34 | return first_seq_base; |
---|
35 | } |
---|
36 | unsigned long last_base(void) { |
---|
37 | return last_seq_base; |
---|
38 | } |
---|
39 | unsigned long first_reference_base(void) { |
---|
40 | return first_ref_base; |
---|
41 | } |
---|
42 | unsigned long last_reference_base(void) { |
---|
43 | return last_ref_base; |
---|
44 | } |
---|
45 | /* |
---|
46 | * Set position of base to position (relative to first_ref_base) |
---|
47 | */ |
---|
48 | void set(unsigned long base, unsigned long pos, int insert = -1) { |
---|
49 | unsigned long b; |
---|
50 | |
---|
51 | if (base < first_seq_base || base > last_seq_base) |
---|
52 | ali_fatal_error("Base number out of range","ALI_MAP::set()"); |
---|
53 | |
---|
54 | if (pos > last_ref_base - first_ref_base) |
---|
55 | ali_fatal_error("Position out of range","ALI_MAP::set()"); |
---|
56 | |
---|
57 | b = base - first_seq_base; |
---|
58 | (*mapping)[b] = pos; |
---|
59 | (*undefined)[b/8] &= (unsigned char) ~(0x01<<(7-(b%8))); |
---|
60 | switch(insert) { |
---|
61 | case 0: |
---|
62 | if ((*inserted)[b/8]>>(7-(b%8)) & 0x01) { |
---|
63 | if (insert_counter > 0) |
---|
64 | insert_counter--; |
---|
65 | else |
---|
66 | ali_fatal_error("Inconsistent insert_counter", |
---|
67 | "ALI_MAP::set()"); |
---|
68 | (*inserted)[b/8] &= (unsigned char) ~(0x01<<(7-(b%8))); |
---|
69 | } |
---|
70 | break; |
---|
71 | case 1: |
---|
72 | if (!((*inserted)[b/8]>>(7-(b%8)) & 0x01)) { |
---|
73 | (*inserted)[b/8] |= (unsigned char) (0x01<<(7-(b%8))); |
---|
74 | insert_counter++; |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | long position(unsigned long base) { |
---|
79 | if (base < first_seq_base || base > last_seq_base) |
---|
80 | ali_fatal_error("Out of range","ALI_MAP::position()"); |
---|
81 | return (*mapping)[base - first_seq_base]; |
---|
82 | } |
---|
83 | unsigned long insertations(void) { |
---|
84 | return insert_counter; |
---|
85 | } |
---|
86 | int is_inserted(unsigned long base) { |
---|
87 | unsigned long b; |
---|
88 | if (base < first_seq_base && base > last_seq_base) |
---|
89 | ali_fatal_error("Out of range","ALI_MAP::inserted"); |
---|
90 | b = base - first_seq_base; |
---|
91 | if (((*inserted)[b/8]>>(7-(b%8))) & 0x01) |
---|
92 | return 1; |
---|
93 | else |
---|
94 | return 0; |
---|
95 | } |
---|
96 | void undefine(unsigned long base) { |
---|
97 | unsigned long b; |
---|
98 | if (base < first_seq_base && base > last_seq_base) |
---|
99 | ali_fatal_error("Out of range","ALI_MAP::undefine()"); |
---|
100 | b = base - first_seq_base; |
---|
101 | (*undefined)[b/8] |= (unsigned char) (0x01<<(7-(b%8))); |
---|
102 | } |
---|
103 | void unundefine(unsigned long base) { |
---|
104 | unsigned long b; |
---|
105 | if (base < first_seq_base && base > last_seq_base) |
---|
106 | ali_fatal_error("Out of range","ALI_MAP::unundefine()"); |
---|
107 | b = base - first_seq_base; |
---|
108 | (*undefined)[b/8] &= (unsigned char) ~(0x01<<(7-(b%8))); |
---|
109 | } |
---|
110 | int is_undefined(unsigned long base) { |
---|
111 | unsigned long b; |
---|
112 | if (base < first_seq_base && base > last_seq_base) |
---|
113 | ali_fatal_error("Out of range","ALI_MAP::undefined()"); |
---|
114 | b = base - first_seq_base; |
---|
115 | if (((*undefined)[b/8]>>(7-(b%8))) & 0x01) |
---|
116 | return 1; |
---|
117 | else |
---|
118 | return 0; |
---|
119 | } |
---|
120 | int have_undefined(void) { |
---|
121 | unsigned long b; |
---|
122 | for (b = first_seq_base; b <= last_seq_base; b++) |
---|
123 | if (is_undefined(b)) |
---|
124 | return 1; |
---|
125 | return 0; |
---|
126 | } |
---|
127 | |
---|
128 | int is_konsistent(void); |
---|
129 | int is_equal(ALI_MAP *map) { |
---|
130 | unsigned long i; |
---|
131 | if (first_seq_base != map->first_seq_base || |
---|
132 | last_seq_base != map->last_seq_base || |
---|
133 | first_ref_base != map->first_ref_base || |
---|
134 | last_ref_base != map->last_ref_base) |
---|
135 | return 0; |
---|
136 | for (i = 0; i < last_seq_base - first_seq_base + 1; i++) |
---|
137 | if ((*mapping)[i] != (*map->mapping)[i]) |
---|
138 | return 0; |
---|
139 | for (i = 0; i < ((last_seq_base - first_seq_base) / 8) + 1; i++) |
---|
140 | if ((*inserted)[i] != (*map->inserted)[i]) |
---|
141 | return 0; |
---|
142 | return 1; |
---|
143 | } |
---|
144 | ALI_SEQUENCE *sequence(ALI_NORM_SEQUENCE *ref_seq); |
---|
145 | ALI_SEQUENCE *sequence_without_inserts(ALI_NORM_SEQUENCE *ref_seq); |
---|
146 | ALI_MAP *inverse_without_inserts(void); |
---|
147 | char *insert_marker(void); |
---|
148 | void print(void) { |
---|
149 | unsigned long i; |
---|
150 | printf("Map: Bases %ld to %ld, Positions %ld to %ld\n", |
---|
151 | first_seq_base,last_seq_base,first_ref_base,last_ref_base); |
---|
152 | printf("Undefined : "); |
---|
153 | for (i = first_seq_base; i <= last_seq_base; i++) |
---|
154 | if (is_undefined(i)) |
---|
155 | printf("%ld ",i); |
---|
156 | printf("\n"); |
---|
157 | /* |
---|
158 | for (i = 0; i <= (last_seq_base - first_seq_base); i++) |
---|
159 | printf("%d, ",first_ref_base + (*mapping)[i]); |
---|
160 | printf("\n"); |
---|
161 | */ |
---|
162 | } |
---|
163 | }; |
---|
164 | |
---|
165 | class ALI_SUB_SOLUTION { |
---|
166 | ALI_PROFILE *profile; |
---|
167 | ALI_TLIST<ALI_MAP *> map_list; |
---|
168 | |
---|
169 | public: |
---|
170 | |
---|
171 | ALI_SUB_SOLUTION(ALI_PROFILE *prof) : map_list() { |
---|
172 | profile = prof; |
---|
173 | } |
---|
174 | ALI_SUB_SOLUTION(ALI_PROFILE *prof, ALI_MAP *map) : map_list(map) { |
---|
175 | profile = prof; |
---|
176 | } |
---|
177 | ALI_SUB_SOLUTION(ALI_SUB_SOLUTION *solution); |
---|
178 | ~ALI_SUB_SOLUTION(void); |
---|
179 | int free_area(unsigned long *start, unsigned long *end, |
---|
180 | unsigned long *start_ref, unsigned long *end_ref, |
---|
181 | unsigned long area_number = 0); |
---|
182 | unsigned long number_of_free_areas(void); |
---|
183 | int is_konsistent(ALI_MAP *map); |
---|
184 | int insert(ALI_MAP *map); |
---|
185 | int delete_map(ALI_MAP *map); |
---|
186 | ALI_MAP *make_one_map(void); |
---|
187 | void print(void); |
---|
188 | }; |
---|
189 | |
---|
190 | |
---|
191 | #endif |
---|