1 | |
---|
2 | #ifndef _ALI_MISC_INC_ |
---|
3 | #define _ALI_MISC_INC_ |
---|
4 | |
---|
5 | #ifndef _CPP_CSTDIO |
---|
6 | #include <cstdio> |
---|
7 | #endif |
---|
8 | #ifndef _CPP_CSTDLIB |
---|
9 | #include <cstdlib> |
---|
10 | #endif |
---|
11 | #ifndef _UNISTD_H |
---|
12 | #include <unistd.h> |
---|
13 | #endif |
---|
14 | #ifndef _MEMORY_H |
---|
15 | #include <memory.h> |
---|
16 | #endif |
---|
17 | |
---|
18 | #ifndef ATTRIBUTES_H |
---|
19 | #include <attributes.h> |
---|
20 | #endif |
---|
21 | |
---|
22 | #define ALI_A_CODE 0 |
---|
23 | #define ALI_C_CODE 1 |
---|
24 | #define ALI_G_CODE 2 |
---|
25 | #define ALI_U_CODE 3 |
---|
26 | #define ALI_GAP_CODE 4 |
---|
27 | #define ALI_N_CODE 5 |
---|
28 | #define ALI_DOT_CODE 6 |
---|
29 | #define ALI_UNDEF_CODE 200 |
---|
30 | |
---|
31 | /***************************************************************************** |
---|
32 | * |
---|
33 | * Some Error Funktions |
---|
34 | * |
---|
35 | *****************************************************************************/ |
---|
36 | |
---|
37 | inline void ali_message(const char *message, const char *func = "") |
---|
38 | { |
---|
39 | fprintf(stdout,"%s %s\n",func,message); |
---|
40 | } |
---|
41 | |
---|
42 | inline void ali_warning(const char *message, const char *func = "") |
---|
43 | { |
---|
44 | fprintf(stderr,"WARNING %s: %s\n",func,message); |
---|
45 | } |
---|
46 | |
---|
47 | void ali_error(const char *message, const char *func = "") __ATTR__NORETURN; |
---|
48 | void ali_fatal_error(const char *message, const char *func = "") __ATTR__NORETURN; |
---|
49 | |
---|
50 | inline void *CALLOC(long i,long j) { |
---|
51 | char *v = (char *)malloc(i*j); |
---|
52 | if (!v) { |
---|
53 | ali_fatal_error("Out of Memory"); |
---|
54 | } |
---|
55 | memset(v,0,i*j); |
---|
56 | return v; |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | /***************************************************************************** |
---|
61 | * |
---|
62 | * Some Converters |
---|
63 | * |
---|
64 | *****************************************************************************/ |
---|
65 | |
---|
66 | inline int ali_is_base(char c) |
---|
67 | { |
---|
68 | return (c == 'a' || c == 'A' || c == 'c' || c == 'C' || |
---|
69 | c == 'g' || c == 'G' || c == 'u' || c == 'U' || |
---|
70 | c == 't' || c == 'T' || c == 'n' || c == 'N'); |
---|
71 | } |
---|
72 | |
---|
73 | inline int ali_is_base(unsigned char c) |
---|
74 | { |
---|
75 | return ( (c <= 3) || (c == 5)); |
---|
76 | } |
---|
77 | |
---|
78 | inline int ali_is_real_base(char c) |
---|
79 | { |
---|
80 | return (c == 'a' || c == 'A' || c == 'c' || c == 'C' || |
---|
81 | c == 'g' || c == 'G' || c == 'u' || c == 'U' || |
---|
82 | c == 't' || c == 'T'); |
---|
83 | } |
---|
84 | |
---|
85 | inline int ali_is_real_base(unsigned char c) |
---|
86 | { |
---|
87 | return ( c <= 3); |
---|
88 | } |
---|
89 | |
---|
90 | inline int ali_is_real_base_or_gap(char c) |
---|
91 | { |
---|
92 | return (c == 'a' || c == 'A' || c == 'c' || c == 'C' || |
---|
93 | c == 'g' || c == 'G' || c == 'u' || c == 'U' || |
---|
94 | c == 't' || c == 'T' || c == '-'); |
---|
95 | } |
---|
96 | |
---|
97 | inline int ali_is_real_base_or_gap(unsigned char c) |
---|
98 | { |
---|
99 | return ( c <= 4); |
---|
100 | } |
---|
101 | |
---|
102 | inline int ali_is_dot(char c) |
---|
103 | { |
---|
104 | return (c == '.'); |
---|
105 | } |
---|
106 | |
---|
107 | inline int ali_is_dot(unsigned char c) |
---|
108 | { |
---|
109 | return (c == 6); |
---|
110 | } |
---|
111 | |
---|
112 | inline int ali_is_nbase(char c) |
---|
113 | { |
---|
114 | return (c == 'n'); |
---|
115 | } |
---|
116 | |
---|
117 | inline int ali_is_nbase(unsigned char c) |
---|
118 | { |
---|
119 | return (c == 5); |
---|
120 | } |
---|
121 | |
---|
122 | inline int ali_is_gap(char c) |
---|
123 | { |
---|
124 | return (c == '-'); |
---|
125 | } |
---|
126 | |
---|
127 | inline int ali_is_gap(unsigned char c) |
---|
128 | { |
---|
129 | return (c == 4); |
---|
130 | } |
---|
131 | |
---|
132 | inline unsigned char ali_base_to_number(char c, int no_gap_flag = 0) |
---|
133 | { |
---|
134 | switch (c) { |
---|
135 | case 'a': case 'A': return(0); |
---|
136 | case 'c': case 'C': return(1); |
---|
137 | case 'g': case 'G': return(2); |
---|
138 | case 'u': case 'U': case 't': case 'T': return(3); |
---|
139 | case '-': if (no_gap_flag == 0) |
---|
140 | return(4); |
---|
141 | else |
---|
142 | return(6); |
---|
143 | case 'n': case 'N': return(5); |
---|
144 | case '.': return(6); |
---|
145 | default: |
---|
146 | ali_warning("Replace unknowen Base by 'n'"); |
---|
147 | return(5); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | inline char ali_number_to_base(unsigned char n) |
---|
152 | { |
---|
153 | switch(n) { |
---|
154 | case 0: return 'a'; |
---|
155 | case 1: return 'c'; |
---|
156 | case 2: return 'g'; |
---|
157 | case 3: return 'u'; |
---|
158 | case 4: return '-'; |
---|
159 | case 5: return 'n'; |
---|
160 | case 6: return '.'; |
---|
161 | default: |
---|
162 | ali_warning("Replace unknowen Number by '.'"); |
---|
163 | printf("received %d\n",n); |
---|
164 | ali_fatal_error("STOP"); |
---|
165 | return '.'; |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | inline void ali_string_to_sequence(char *sequence) |
---|
170 | { |
---|
171 | for (; *sequence != '\0' && !ali_is_base(*sequence); sequence++) |
---|
172 | *sequence = (char) ali_base_to_number(*sequence,1); |
---|
173 | |
---|
174 | for (; *sequence != '\0'; sequence++) |
---|
175 | *sequence = (char) ali_base_to_number(*sequence); |
---|
176 | } |
---|
177 | |
---|
178 | inline void ali_sequence_to_string(unsigned char *sequence, unsigned long length) |
---|
179 | { |
---|
180 | for (; length-- > 0; sequence++) |
---|
181 | *sequence = (unsigned char) ali_number_to_base(*sequence); |
---|
182 | } |
---|
183 | |
---|
184 | inline void ali_sequence_to_postree_sequence(unsigned char *sequence, unsigned long length) |
---|
185 | { |
---|
186 | for (; length-- > 0; sequence++) |
---|
187 | if (ali_is_base(*sequence)) { |
---|
188 | if (ali_is_nbase(*sequence)) |
---|
189 | *sequence = 4; |
---|
190 | } |
---|
191 | else { |
---|
192 | ali_warning("Unknowen symbol replaced by 'n'"); |
---|
193 | *sequence = 4; |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | inline void ali_print_sequence(unsigned char *sequence, unsigned long length) |
---|
198 | { |
---|
199 | for (; length-- > 0; sequence++) |
---|
200 | printf("%d ",*sequence); |
---|
201 | } |
---|
202 | |
---|
203 | #endif |
---|