1 | |
---|
2 | |
---|
3 | #include <string.h> |
---|
4 | #include <stdlib.h> |
---|
5 | // #include <malloc.h> |
---|
6 | |
---|
7 | #include "ali_misc.hxx" |
---|
8 | #include "ali_sequence.hxx" |
---|
9 | |
---|
10 | |
---|
11 | |
---|
12 | /***************************************************************************** |
---|
13 | * |
---|
14 | * ALI_SEQUENCE |
---|
15 | * |
---|
16 | *****************************************************************************/ |
---|
17 | |
---|
18 | |
---|
19 | int ALI_SEQUENCE::check(void) |
---|
20 | { |
---|
21 | unsigned char *seq_buf; |
---|
22 | unsigned long i; |
---|
23 | |
---|
24 | seq_buf = seq; |
---|
25 | for (i = 0; i < seq_len; i++, seq_buf++) |
---|
26 | if (*seq_buf > 6) |
---|
27 | ali_fatal_error("STOP"); |
---|
28 | |
---|
29 | return 1; |
---|
30 | } |
---|
31 | |
---|
32 | char *ALI_SEQUENCE::string(void) |
---|
33 | { |
---|
34 | char *str, *str_buf; |
---|
35 | unsigned char *seq_buf; |
---|
36 | unsigned long i; |
---|
37 | |
---|
38 | str = (char *) CALLOC((unsigned int) seq_len + 1,sizeof(char)); |
---|
39 | |
---|
40 | str_buf = str; |
---|
41 | seq_buf = seq; |
---|
42 | for (i = 0; i < seq_len; i++) { |
---|
43 | *(str_buf++) = *(seq_buf++); |
---|
44 | } |
---|
45 | *str_buf = '\0'; |
---|
46 | ali_sequence_to_string((unsigned char*) str,seq_len); |
---|
47 | |
---|
48 | return str; |
---|
49 | } |
---|
50 | |
---|
51 | /***************************************************************************** |
---|
52 | * |
---|
53 | * ALI_NORM_SEQUENCE |
---|
54 | * |
---|
55 | *****************************************************************************/ |
---|
56 | |
---|
57 | ALI_NORM_SEQUENCE::ALI_NORM_SEQUENCE(char *Name, char *String) |
---|
58 | { |
---|
59 | unsigned long counter; |
---|
60 | unsigned char *s; |
---|
61 | int dot_flag; |
---|
62 | char *str; |
---|
63 | |
---|
64 | /* |
---|
65 | * Count only _BASES_ |
---|
66 | */ |
---|
67 | for (counter = 0, str = String; *str != '\0'; str++) |
---|
68 | if (ali_is_base(*str)) |
---|
69 | counter++; |
---|
70 | seq_len = counter; |
---|
71 | |
---|
72 | seq = (unsigned char*) CALLOC((unsigned int) seq_len,sizeof(unsigned char)); |
---|
73 | dots = (unsigned char **) CALLOC((unsigned int) (seq_len/8)+1, sizeof(unsigned char)); |
---|
74 | //dots = (unsigned char (*) [1]) CALLOC((unsigned int) (seq_len/8)+1, sizeof(unsigned char)); |
---|
75 | seq_name = strdup(Name); |
---|
76 | if (seq == 0 || dots == 0 || seq_name == 0) { |
---|
77 | ali_fatal_error("Out of memory"); |
---|
78 | } |
---|
79 | |
---|
80 | dot_flag = 0; |
---|
81 | (*dots)[0] |= (unsigned char) (1<<7); |
---|
82 | for (counter = 0, str = String, s = seq; *str != '\0'; str++) { |
---|
83 | if (ali_is_base(*str)) { |
---|
84 | *s++ = ali_base_to_number(*str); |
---|
85 | dot_flag = 0; |
---|
86 | counter++; |
---|
87 | } |
---|
88 | else { |
---|
89 | if (dot_flag == 0 && ali_is_dot(*str)) { |
---|
90 | (*dots)[(counter/8)] |= (unsigned char) (1<<(7-(counter%8))); |
---|
91 | dot_flag = 1; |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | ALI_NORM_SEQUENCE::ALI_NORM_SEQUENCE(ALI_SEQUENCE *SEQ) |
---|
98 | { |
---|
99 | unsigned long counter, pos; |
---|
100 | unsigned char *s; |
---|
101 | int dot_flag; |
---|
102 | unsigned char *str; |
---|
103 | |
---|
104 | for (counter = pos = 0, str = SEQ->sequence(); |
---|
105 | pos < SEQ->length(); pos++, str++) |
---|
106 | if (ali_is_base(*str)) |
---|
107 | counter++; |
---|
108 | seq_len = counter; |
---|
109 | |
---|
110 | seq = (unsigned char*) CALLOC((unsigned int) seq_len,sizeof(unsigned char)); |
---|
111 | dots = (unsigned char **) CALLOC((unsigned int) (seq_len/8)+1, sizeof(unsigned char)); |
---|
112 | //dots = (unsigned char (*) [1]) CALLOC((unsigned int) (seq_len/8)+1, sizeof(unsigned char)); |
---|
113 | seq_name = strdup(SEQ->name()); |
---|
114 | if (seq == 0 || dots == 0 || seq_name == 0) { |
---|
115 | ali_fatal_error("Out of memory"); |
---|
116 | } |
---|
117 | |
---|
118 | dot_flag = 0; |
---|
119 | (*dots)[0] |= (unsigned char) (1<<7); |
---|
120 | for (counter = pos = 0, str = SEQ->sequence(), s = seq; |
---|
121 | pos < SEQ->length(); str++, pos++) { |
---|
122 | if (ali_is_base(*str)) { |
---|
123 | *s++ = *str; |
---|
124 | dot_flag = 0; |
---|
125 | counter++; |
---|
126 | } |
---|
127 | else { |
---|
128 | if (dot_flag == 0 && ali_is_dot(*str)) { |
---|
129 | (*dots)[(counter/8)] |= (unsigned char) (1<<(7-(counter%8))); |
---|
130 | dot_flag = 1; |
---|
131 | } |
---|
132 | } |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | char *ALI_NORM_SEQUENCE::string() |
---|
137 | { |
---|
138 | char *str, *str_buf; |
---|
139 | unsigned char *seq_buf; |
---|
140 | unsigned long i; |
---|
141 | |
---|
142 | str = (char *) CALLOC((unsigned int) seq_len + 1,sizeof(char)); |
---|
143 | if (str == 0) |
---|
144 | ali_fatal_error("Out of memory"); |
---|
145 | |
---|
146 | for (i = seq_len, str_buf = str, seq_buf = seq; i-- > 0;) |
---|
147 | *str_buf++ = *seq_buf++; |
---|
148 | *str_buf = '\0'; |
---|
149 | |
---|
150 | ali_sequence_to_string((unsigned char*) str,seq_len); |
---|
151 | |
---|
152 | return str; |
---|
153 | } |
---|
154 | |
---|