1 | // ********************* INCLUDE |
---|
2 | #include <stdio.h> |
---|
3 | |
---|
4 | |
---|
5 | #include "ali_misc.hxx" |
---|
6 | #include "ali_global.hxx" |
---|
7 | #include "ali_sequence.hxx" |
---|
8 | #include "ali_profile.hxx" |
---|
9 | #include "ali_aligner.hxx" |
---|
10 | #include "ali_prealigner.hxx" |
---|
11 | |
---|
12 | |
---|
13 | #define HELIX_PAIRS "helix_pairs" |
---|
14 | #define HELIX_LINE "helix_line" |
---|
15 | #define ALI_CONSENSUS "ALI_CON" |
---|
16 | #define ALI_ERROR "ALI_ERR" |
---|
17 | #define ALI_INTERVALLS "ALI_INT" |
---|
18 | |
---|
19 | ALI_GLOBAL aligs; |
---|
20 | |
---|
21 | |
---|
22 | void message(char *errortext); |
---|
23 | |
---|
24 | |
---|
25 | const char *ali_version = |
---|
26 | "\nALIGNER V2.0 (Boris Reichel 5/95)\n"; |
---|
27 | |
---|
28 | const char *ali_man_line[] = { |
---|
29 | "Parameter:", |
---|
30 | "-s<species> aligne the sequence of <species>", |
---|
31 | "-f<species_1>,...,<species_n>[;<extension_1>,...,<extension_k>] use specified family and family extension", |
---|
32 | "-P<pt_server> use the PT_server <pt_server>", |
---|
33 | "[-D<db_server>] use the DB_server <db_server>", |
---|
34 | "[-nx] not exclusive mode (profile)", |
---|
35 | "[-ms] mark species (profile)", |
---|
36 | "[-mf] mark used family (profile)", |
---|
37 | "[-mfe] mark used family extension (profile)", |
---|
38 | "[-mgf] multi gap factor (0.1) (profile)", |
---|
39 | "[-if] insert factor (2.0) (profile)", |
---|
40 | "[-mif] multi insert factor (0.5) (profile)", |
---|
41 | "[-m] mark all (-m == -ms -mf -mfe) (profile)", |
---|
42 | "[-d<filename>] use <filename> for default values", |
---|
43 | "[-msubX1,X2,...,X25] use X1,...,X25 for the substitute matrix: (profile)", |
---|
44 | " a c g u -", |
---|
45 | " a X1 X2 X3 X4 X5", |
---|
46 | " c X6 X7 X8 X9 X10", |
---|
47 | " g X11 X12 X13 X14 X15", |
---|
48 | " u X16 X17 X18 X19 X20", |
---|
49 | " - X21 X22 X23 X24 X25", |
---|
50 | "[-mbindX1,X2,...,X25] use X1,...,X25 for the binding matrix: (profile)", |
---|
51 | " a c g u -", |
---|
52 | " a X1 X2 X3 X4 X5", |
---|
53 | " c X6 X7 X8 X9 X10", |
---|
54 | " g X11 X12 X13 X14 X15", |
---|
55 | " u X16 X17 X18 X19 X20", |
---|
56 | " - X21 X22 X23 X24 X25", |
---|
57 | "[-maxf] maximal number of family members (10) (profile)", |
---|
58 | "[-minf] minimal number of family members (5) (profile)", |
---|
59 | "[-minw] minimal weight for family members (0.7) (profile)", |
---|
60 | "[-maxew] maximal weight for family extension members (0.2) (profile)", |
---|
61 | "unused [-cl] cost threshold (low) (0.25) ALI_ERR = ','", |
---|
62 | "unused [-cm] cost threshold (middle) (0.5) ALI_ERR = '-'", |
---|
63 | "unused [-ch] cost threshold (high) (0.8) ALI_ERR = '='", |
---|
64 | "[-mm] maximal number of maps for solution (prealigner) (1000)", |
---|
65 | "[-mma] maximal number of maps for aligner (prealigner) (2)", |
---|
66 | "[-csub] cost threshold for substitution (prealigner) (0.5)", |
---|
67 | "[-chel] cost threshold for helix binding (prealigner) (2.0)", |
---|
68 | "[-ec] error count (prealigner) (2)", |
---|
69 | "[-ib] interval border (prealigner) (5)", |
---|
70 | "[-ic] interval center (prealigner) (5)", |
---|
71 | 0 |
---|
72 | }; |
---|
73 | |
---|
74 | |
---|
75 | /* |
---|
76 | * Print a short parameter description |
---|
77 | */ |
---|
78 | void print_man() |
---|
79 | { |
---|
80 | int i; |
---|
81 | |
---|
82 | for (i = 0; ali_man_line[i] != 0; i++) |
---|
83 | fprintf(stderr,"%s\n",ali_man_line[i]); |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | void ali_fatal_error(const char *message, const char *func) { |
---|
88 | fprintf(stderr,"FATAL ERROR %s: %s\n",func,message); |
---|
89 | exit(-1); |
---|
90 | } |
---|
91 | |
---|
92 | void ali_error(const char *message, const char *func) { |
---|
93 | fprintf(stderr,"ERROR %s: %s\n",func,message); |
---|
94 | exit(-1); |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | /******************** |
---|
99 | void del_test(ALI_PROFILE *prof,long begin, long end) |
---|
100 | { |
---|
101 | long i; |
---|
102 | |
---|
103 | printf("**********************\n"); |
---|
104 | for (i = begin; i <= end; i++) { |
---|
105 | printf("W_DEL(%d,%d)\t\t%f\n",i,end,prof->w_del(i,end)); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | void perc_test(ALI_PROFILE *prof,long begin, long end) |
---|
110 | { |
---|
111 | long i; |
---|
112 | |
---|
113 | printf("**********************\n"); |
---|
114 | for (i = begin; i <= end; i++) { |
---|
115 | printf("GAP_PERC(%d,%d)\t\t%f\n",i,end,prof->gap_percent(i,end)); |
---|
116 | } |
---|
117 | } |
---|
118 | ********************/ |
---|
119 | |
---|
120 | /* |
---|
121 | * Get one species of a list |
---|
122 | */ |
---|
123 | int get_species(char *species_string, unsigned int species_number, char *buffer) |
---|
124 | { |
---|
125 | while (species_number > 0 && *species_string != '\0') { |
---|
126 | while (*species_string != '\0' && *species_string != ',') |
---|
127 | species_string++; |
---|
128 | if (*species_string != '\0') |
---|
129 | species_string++; |
---|
130 | species_number--; |
---|
131 | } |
---|
132 | |
---|
133 | if (*species_string != '\0') { |
---|
134 | while (*species_string != '\0' && *species_string != ',') |
---|
135 | *buffer++ = *species_string++; |
---|
136 | *buffer = '\0'; |
---|
137 | } |
---|
138 | else { |
---|
139 | return 0; |
---|
140 | } |
---|
141 | |
---|
142 | return 1; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | int check_base_invariance(char *seq1, char *seq2) |
---|
147 | { |
---|
148 | while (*seq1 != '\0' && !ali_is_base(*seq1)) |
---|
149 | seq1++; |
---|
150 | while (*seq2 != '\0' && !ali_is_base(*seq2)) |
---|
151 | seq2++; |
---|
152 | while (*seq1 != '\0' && *seq2 != '\0') { |
---|
153 | if (*seq1 != *seq2) |
---|
154 | return 0; |
---|
155 | seq1++; |
---|
156 | seq2++; |
---|
157 | while (*seq1 != '\0' && !ali_is_base(*seq1)) |
---|
158 | seq1++; |
---|
159 | while (*seq2 != '\0' && !ali_is_base(*seq2)) |
---|
160 | seq2++; |
---|
161 | } |
---|
162 | |
---|
163 | if (*seq1 == *seq2) |
---|
164 | return 1; |
---|
165 | |
---|
166 | return 0; |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | /* |
---|
171 | * Convert the working sequenz into the original bases |
---|
172 | */ |
---|
173 | int convert_for_back_write(char *seq_new, char *seq_orig) |
---|
174 | { |
---|
175 | |
---|
176 | while (*seq_new != '\0' && (ali_is_dot(*seq_new) || ali_is_gap(*seq_new))) |
---|
177 | seq_new++; |
---|
178 | while (*seq_orig != '\0' && (ali_is_dot(*seq_orig) || ali_is_gap(*seq_orig))) |
---|
179 | seq_orig++; |
---|
180 | |
---|
181 | while (*seq_new != '\0' && *seq_orig != '\0') { |
---|
182 | if (*seq_new != *seq_orig) { |
---|
183 | switch (*seq_new) { |
---|
184 | case 'a': |
---|
185 | switch (*seq_orig) { |
---|
186 | case 'A': |
---|
187 | *seq_new = 'A'; |
---|
188 | break; |
---|
189 | default: |
---|
190 | ali_error("Unexpected character in original sequence"); |
---|
191 | } |
---|
192 | break; |
---|
193 | case 'c': |
---|
194 | switch (*seq_orig) { |
---|
195 | case 'C': |
---|
196 | *seq_new = 'C'; |
---|
197 | break; |
---|
198 | default: |
---|
199 | ali_error("Unexpected character in original sequence"); |
---|
200 | } |
---|
201 | break; |
---|
202 | case 'g': |
---|
203 | switch (*seq_orig) { |
---|
204 | case 'G': |
---|
205 | *seq_new = 'G'; |
---|
206 | break; |
---|
207 | default: |
---|
208 | ali_error("Unexpected character in original sequence"); |
---|
209 | } |
---|
210 | break; |
---|
211 | case 'u': |
---|
212 | switch (*seq_orig) { |
---|
213 | case 'U': |
---|
214 | *seq_new = 'U'; |
---|
215 | break; |
---|
216 | case 't': |
---|
217 | *seq_new = 't'; |
---|
218 | break; |
---|
219 | case 'T': |
---|
220 | *seq_new = 'T'; |
---|
221 | break; |
---|
222 | } |
---|
223 | break; |
---|
224 | case 'n': |
---|
225 | *seq_new = *seq_orig; |
---|
226 | break; |
---|
227 | default: |
---|
228 | ali_fatal_error("Unexpected character in generated sequence"); |
---|
229 | } |
---|
230 | } |
---|
231 | seq_new++; |
---|
232 | seq_orig++; |
---|
233 | while (*seq_new != '\0' && (ali_is_dot(*seq_new) || ali_is_gap(*seq_new))) |
---|
234 | seq_new++; |
---|
235 | while (*seq_orig != '\0' && (ali_is_dot(*seq_orig) || ali_is_gap(*seq_orig))) |
---|
236 | seq_orig++; |
---|
237 | } |
---|
238 | |
---|
239 | if (*seq_new == *seq_orig) |
---|
240 | return 1; |
---|
241 | |
---|
242 | return 0; |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | |
---|
247 | int main(int argc, char **argv) |
---|
248 | { |
---|
249 | int i; |
---|
250 | char message_buffer[100]; |
---|
251 | char species_name[100], species_number; |
---|
252 | ALI_PREALIGNER *align_prealigner; |
---|
253 | ali_prealigner_approx_element *approx_elem; |
---|
254 | ALI_SEQUENCE *sequence; |
---|
255 | |
---|
256 | |
---|
257 | ali_message(ali_version); |
---|
258 | |
---|
259 | aligs.init(&argc, argv); |
---|
260 | |
---|
261 | if (!aligs.species_name || argc > 1) { |
---|
262 | printf("Unknowen : "); |
---|
263 | for (i = 1; i < argc; i++) |
---|
264 | printf("%s ",argv[i]); |
---|
265 | printf("\n\n"); |
---|
266 | print_man(); |
---|
267 | exit (-1); |
---|
268 | } |
---|
269 | |
---|
270 | /* |
---|
271 | * Main loop |
---|
272 | */ |
---|
273 | species_number = 0; |
---|
274 | while (get_species(aligs.species_name,species_number,species_name)) { |
---|
275 | species_number++; |
---|
276 | |
---|
277 | sprintf(message_buffer, |
---|
278 | "\nStarting alignment of sequence: %s",species_name); |
---|
279 | ali_message(message_buffer); |
---|
280 | |
---|
281 | /* |
---|
282 | * Get all information of the sequence |
---|
283 | */ |
---|
284 | aligs.arbdb.begin_transaction(); |
---|
285 | ALI_SEQUENCE *align_sequence; |
---|
286 | align_sequence = aligs.arbdb.get_sequence(species_name, |
---|
287 | aligs.mark_species_flag); |
---|
288 | if (align_sequence == 0) { |
---|
289 | ali_error("Can't read sequence from database"); |
---|
290 | ali_message("Aborting alignment of sequence"); |
---|
291 | }else { |
---|
292 | char *align_string; |
---|
293 | char *align_string_original; |
---|
294 | |
---|
295 | align_string = align_sequence->string(); |
---|
296 | align_string_original = aligs.arbdb.get_sequence_string(species_name, |
---|
297 | aligs.mark_species_flag); |
---|
298 | aligs.arbdb.commit_transaction(); |
---|
299 | |
---|
300 | if (align_sequence == 0) |
---|
301 | ali_warning("Can't read sequence from database"); |
---|
302 | else { |
---|
303 | /* |
---|
304 | * make profile for sequence |
---|
305 | */ |
---|
306 | ALI_PROFILE *align_profile; |
---|
307 | align_profile = new ALI_PROFILE(align_sequence,&aligs.prof_context); |
---|
308 | |
---|
309 | /* |
---|
310 | * write information about the profile to the database |
---|
311 | */ |
---|
312 | aligs.arbdb.begin_transaction(); |
---|
313 | char *String = align_profile->cheapest_sequence(); |
---|
314 | aligs.arbdb.put_SAI("ALI_CON",String); |
---|
315 | freeset(String, align_profile->borders_sequence()); |
---|
316 | // aligs.arbdb.put_SAI("ALI_BOR",string,0); |
---|
317 | free(String); |
---|
318 | aligs.arbdb.commit_transaction(); |
---|
319 | |
---|
320 | /* |
---|
321 | * make prealignment |
---|
322 | */ |
---|
323 | align_prealigner = new ALI_PREALIGNER(&aligs.preali_context, |
---|
324 | align_profile, |
---|
325 | 0, align_profile->sequence_length() - 1 , |
---|
326 | 0, align_profile->length() - 1); |
---|
327 | ALI_SEQUENCE *align_pre_sequence_i, *align_pre_sequence; |
---|
328 | ALI_SUB_SOLUTION *align_pre_solution; |
---|
329 | ALI_TLIST<ali_prealigner_approx_element *> *align_pre_approx; |
---|
330 | align_pre_sequence_i = align_prealigner->sequence(); |
---|
331 | align_pre_sequence = align_prealigner->sequence_without_inserts(); |
---|
332 | align_pre_solution = align_prealigner->solution(); |
---|
333 | align_pre_approx = align_prealigner->approximation(); |
---|
334 | delete align_prealigner; |
---|
335 | |
---|
336 | align_pre_solution->print(); |
---|
337 | |
---|
338 | /* |
---|
339 | * write result of alignment into database |
---|
340 | */ |
---|
341 | aligs.arbdb.begin_transaction(); |
---|
342 | String = align_pre_sequence_i->string(); |
---|
343 | aligs.arbdb.put_SAI("ALI_PRE_I",String); |
---|
344 | freeset(String, align_pre_sequence->string()); |
---|
345 | aligs.arbdb.put_SAI("ALI_PRE",String); |
---|
346 | free(String); |
---|
347 | aligs.arbdb.commit_transaction(); |
---|
348 | |
---|
349 | |
---|
350 | sprintf(message_buffer,"%d solutions generated (taking the first)", |
---|
351 | align_pre_approx->cardinality()); |
---|
352 | ali_message(message_buffer); |
---|
353 | |
---|
354 | if (align_pre_approx->is_empty()) |
---|
355 | ali_fatal_error("List of approximations is empty"); |
---|
356 | |
---|
357 | /* |
---|
358 | * Write result back to the database |
---|
359 | */ |
---|
360 | |
---|
361 | approx_elem = align_pre_approx->first(); |
---|
362 | |
---|
363 | sequence = approx_elem->map->sequence(align_profile->sequence()); |
---|
364 | String = sequence->string(); |
---|
365 | |
---|
366 | if (!check_base_invariance(String,align_string)) |
---|
367 | ali_error("Bases changed in output sequence"); |
---|
368 | |
---|
369 | if (!convert_for_back_write(String,align_string_original)) |
---|
370 | ali_fatal_error("Can't convert correctly"); |
---|
371 | |
---|
372 | aligs.arbdb.begin_transaction(); |
---|
373 | aligs.arbdb.put_sequence_string(species_name,String); |
---|
374 | aligs.arbdb.put_SAI("ALI_INSERTS",approx_elem->ins_marker); |
---|
375 | aligs.arbdb.commit_transaction(); |
---|
376 | delete sequence; |
---|
377 | |
---|
378 | /* |
---|
379 | * Delete all Objects |
---|
380 | */ |
---|
381 | free(align_string); |
---|
382 | free(align_string_original); |
---|
383 | delete align_pre_solution; |
---|
384 | delete align_pre_approx; |
---|
385 | delete align_profile; |
---|
386 | delete align_pre_sequence; |
---|
387 | delete align_pre_sequence_i; |
---|
388 | } |
---|
389 | delete align_sequence; |
---|
390 | } |
---|
391 | } /* main loop */ |
---|
392 | |
---|
393 | ali_message("Aligner terminated\n"); |
---|
394 | return 0; |
---|
395 | } |
---|
396 | |
---|
397 | |
---|