1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : ali_pt.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #include "ali_pt.hxx" |
---|
12 | |
---|
13 | int ALI_PT::init_communication() { |
---|
14 | // Initialize the communication with the pt server |
---|
15 | const char *user = GB_getenvUSER(); |
---|
16 | if (aisc_create(link, PT_MAIN, com, |
---|
17 | MAIN_LOCS, PT_LOCS, locs, |
---|
18 | LOCS_USER, user, |
---|
19 | NULL)) |
---|
20 | { |
---|
21 | return 1; |
---|
22 | } |
---|
23 | return 0; |
---|
24 | } |
---|
25 | |
---|
26 | char *ALI_PT::get_family_member(char *specifiedfamily, unsigned long number) |
---|
27 | { |
---|
28 | char *ptr = specifiedfamily; |
---|
29 | char *end; |
---|
30 | char *buffer, *dest; |
---|
31 | |
---|
32 | while (number > 0 && *ptr != '\0' && *ptr != ';') { |
---|
33 | while (*ptr != '\0' && *ptr != ';' && *ptr != ',') |
---|
34 | ptr++; |
---|
35 | if (*ptr == ',') |
---|
36 | ptr++; |
---|
37 | number--; |
---|
38 | } |
---|
39 | |
---|
40 | if (*ptr != '\0' && *ptr != ';') { |
---|
41 | end = ptr; |
---|
42 | while (*end != '\0' && *end != ',' && *end != ';') |
---|
43 | end++; |
---|
44 | |
---|
45 | buffer = dest = (char *) CALLOC((unsigned int) |
---|
46 | (end - ptr) + 1, sizeof(char)); |
---|
47 | if (buffer == 0) |
---|
48 | ali_fatal_error("Out of memory"); |
---|
49 | |
---|
50 | while (ptr != end) |
---|
51 | *dest++ = *ptr++; |
---|
52 | *dest = '\0'; |
---|
53 | |
---|
54 | return buffer; |
---|
55 | } |
---|
56 | return 0; |
---|
57 | } |
---|
58 | |
---|
59 | char *ALI_PT::get_extension_member(char *specifiedfamily, unsigned long number) |
---|
60 | { |
---|
61 | char *ptr = specifiedfamily; |
---|
62 | char *end; |
---|
63 | char *buffer, *dest; |
---|
64 | |
---|
65 | while (*ptr != '\0' && *ptr != ';') |
---|
66 | ptr++; |
---|
67 | if (*ptr == ';') |
---|
68 | ptr++; |
---|
69 | |
---|
70 | while (number > 0 && *ptr != '\0') { |
---|
71 | while (*ptr != '\0' && *ptr != ',') |
---|
72 | ptr++; |
---|
73 | if (*ptr == ',') |
---|
74 | ptr++; |
---|
75 | number--; |
---|
76 | } |
---|
77 | |
---|
78 | if (*ptr != '\0') { |
---|
79 | end = ptr; |
---|
80 | while (*end != '\0' && *end != ',') |
---|
81 | end++; |
---|
82 | |
---|
83 | buffer = dest = (char *) CALLOC((unsigned int) (end - ptr) + 1, sizeof(char)); |
---|
84 | if (buffer == 0) |
---|
85 | ali_fatal_error("Out of memory"); |
---|
86 | |
---|
87 | while (ptr != end) |
---|
88 | *dest++ = *ptr++; |
---|
89 | *dest = '\0'; |
---|
90 | |
---|
91 | return buffer; |
---|
92 | } |
---|
93 | |
---|
94 | return 0; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | int ALI_PT::open(char *servername) |
---|
99 | { |
---|
100 | if (arb_look_and_start_server(AISC_MAGIC_NUMBER, servername)) { |
---|
101 | ali_message ("Cannot contact Probe bank server"); |
---|
102 | return -1; |
---|
103 | } |
---|
104 | |
---|
105 | const char *socketid = GBS_read_arb_tcp(servername); |
---|
106 | |
---|
107 | if (!socketid) { |
---|
108 | ali_message(GB_await_error()); |
---|
109 | return -1; |
---|
110 | } |
---|
111 | |
---|
112 | GB_ERROR openerr = NULL; |
---|
113 | link = aisc_open(socketid, com, AISC_MAGIC_NUMBER, &openerr); |
---|
114 | |
---|
115 | if (openerr) { |
---|
116 | ali_message (openerr); |
---|
117 | return -1; |
---|
118 | } |
---|
119 | |
---|
120 | if (!link) { |
---|
121 | ali_message ("Cannot contact Probe bank server "); |
---|
122 | return -1; |
---|
123 | } |
---|
124 | |
---|
125 | if (init_communication()) { |
---|
126 | ali_message ("Cannot contact Probe bank server (2)"); |
---|
127 | return -1; |
---|
128 | } |
---|
129 | |
---|
130 | return 0; |
---|
131 | } |
---|
132 | |
---|
133 | void ALI_PT::close() |
---|
134 | { |
---|
135 | if (link) aisc_close(link, com); |
---|
136 | link = 0; |
---|
137 | } |
---|
138 | |
---|
139 | ALI_PT::ALI_PT(ALI_PT_CONTEXT *context) |
---|
140 | { |
---|
141 | link = 0; |
---|
142 | |
---|
143 | fam_list_max = context->fam_list_max; |
---|
144 | ext_list_max = context->ext_list_max; |
---|
145 | percent_min = context->percent_min; |
---|
146 | matches_min = context->matches_min; |
---|
147 | |
---|
148 | family_list = 0; |
---|
149 | extension_list = 0; |
---|
150 | |
---|
151 | if (context->use_specified_family != 0) { |
---|
152 | mode = SpecifiedMode; |
---|
153 | specified_family = strdup(context->use_specified_family); |
---|
154 | } |
---|
155 | else { |
---|
156 | mode = ServerMode; |
---|
157 | specified_family = 0; |
---|
158 | |
---|
159 | ali_message("Connecting to PT server"); |
---|
160 | if (open(context->servername) != 0) { |
---|
161 | ali_fatal_error("Can't connect to PT server"); |
---|
162 | } |
---|
163 | ali_message("Connection established"); |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | ALI_PT::~ALI_PT() |
---|
168 | { |
---|
169 | close(); |
---|
170 | |
---|
171 | if (family_list != 0 && !family_list->is_empty()) { |
---|
172 | delete family_list->first(); |
---|
173 | while (family_list->is_next()) |
---|
174 | delete family_list->next(); |
---|
175 | delete family_list; |
---|
176 | } |
---|
177 | if (extension_list != 0 && !extension_list->is_empty()) { |
---|
178 | delete extension_list->first(); |
---|
179 | while (extension_list->is_next()) |
---|
180 | delete extension_list->next(); |
---|
181 | delete extension_list; |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | int ALI_PT::find_family(ALI_SEQUENCE *sequence, int find_type) |
---|
187 | { |
---|
188 | unsigned long number; |
---|
189 | int matches, max_matches; |
---|
190 | char *seq_name; |
---|
191 | T_PT_FAMILYLIST f_list; |
---|
192 | ali_pt_member *pt_member; |
---|
193 | char *species; |
---|
194 | |
---|
195 | bytestring bs; |
---|
196 | bs.data = sequence->string(); |
---|
197 | bs.size = strlen(bs.data)+1; |
---|
198 | |
---|
199 | family_list = new ALI_TLIST<ali_pt_member *>; |
---|
200 | extension_list = new ALI_TLIST<ali_pt_member *>; |
---|
201 | if (family_list == 0 || extension_list == 0) |
---|
202 | ali_fatal_error("Out of memory"); |
---|
203 | |
---|
204 | if (mode == ServerMode) { |
---|
205 | /* Start find_family() at the PT_SERVER |
---|
206 | * |
---|
207 | * Here we have to make a loop, until the match count of the |
---|
208 | * first member is big enought |
---|
209 | */ |
---|
210 | |
---|
211 | T_PT_FAMILYFINDER ffinder; |
---|
212 | if (aisc_create(link, PT_LOCS, locs, |
---|
213 | LOCS_FFINDER, PT_FAMILYFINDER, ffinder, |
---|
214 | FAMILYFINDER_FIND_TYPE, find_type, |
---|
215 | FAMILYFINDER_FIND_FAMILY, &bs, |
---|
216 | NULL)) |
---|
217 | { |
---|
218 | ali_message ("Communication Error (2)"); |
---|
219 | return -1; |
---|
220 | } |
---|
221 | |
---|
222 | // Read family list |
---|
223 | aisc_get(link, PT_FAMILYFINDER, ffinder, |
---|
224 | FAMILYFINDER_FAMILY_LIST, f_list.as_result_param(), |
---|
225 | NULL); |
---|
226 | if (!f_list.exists()) |
---|
227 | ali_error("Family not found in PT Server"); |
---|
228 | |
---|
229 | aisc_get(link, PT_FAMILYLIST, f_list, |
---|
230 | FAMILYLIST_NAME, &seq_name, |
---|
231 | FAMILYLIST_MATCHES, &matches, |
---|
232 | FAMILYLIST_NEXT, f_list.as_result_param(), |
---|
233 | NULL); |
---|
234 | |
---|
235 | while (strcmp(seq_name, sequence->name()) == 0) { |
---|
236 | free(seq_name); |
---|
237 | if (!f_list.exists()) |
---|
238 | ali_error("Family too small in PT Server"); |
---|
239 | aisc_get(link, PT_FAMILYLIST, f_list, |
---|
240 | FAMILYLIST_NAME, &seq_name, |
---|
241 | FAMILYLIST_MATCHES, &matches, |
---|
242 | FAMILYLIST_NEXT, f_list.as_result_param(), |
---|
243 | NULL); |
---|
244 | } |
---|
245 | // found the first element |
---|
246 | |
---|
247 | // make the family list |
---|
248 | max_matches = matches; |
---|
249 | number = 0; |
---|
250 | do { |
---|
251 | pt_member = new ali_pt_member(seq_name, matches); |
---|
252 | family_list->append_end(pt_member); |
---|
253 | number++; |
---|
254 | do { |
---|
255 | if (!f_list.exists()) |
---|
256 | ali_error("Family too small in PT Server"); |
---|
257 | aisc_get(link, PT_FAMILYLIST, f_list, |
---|
258 | FAMILYLIST_NAME, &seq_name, |
---|
259 | FAMILYLIST_MATCHES, &matches, |
---|
260 | FAMILYLIST_NEXT, f_list.as_result_param(), |
---|
261 | NULL); |
---|
262 | if (strcmp(seq_name, sequence->name()) == 0) |
---|
263 | free(seq_name); |
---|
264 | } while (strcmp(seq_name, sequence->name()) == 0); |
---|
265 | } while (number < fam_list_max && |
---|
266 | (float) matches / (float) max_matches > percent_min); |
---|
267 | |
---|
268 | // make the extension list |
---|
269 | number = 0; |
---|
270 | while (number < ext_list_max) { |
---|
271 | pt_member = new ali_pt_member(seq_name, matches); |
---|
272 | extension_list->append_end(pt_member); |
---|
273 | number++; |
---|
274 | do { |
---|
275 | if (!f_list.exists()) |
---|
276 | ali_error("Family too small in PT Server"); |
---|
277 | aisc_get(link, PT_FAMILYLIST, f_list, |
---|
278 | FAMILYLIST_NAME, &seq_name, |
---|
279 | FAMILYLIST_MATCHES, &matches, |
---|
280 | FAMILYLIST_NEXT, f_list.as_result_param(), |
---|
281 | NULL); |
---|
282 | if (strcmp(seq_name, sequence->name()) == 0) |
---|
283 | free(seq_name); |
---|
284 | } while (strcmp(seq_name, sequence->name()) == 0); |
---|
285 | } |
---|
286 | } |
---|
287 | else { |
---|
288 | number = 0; |
---|
289 | while ((species = get_family_member(specified_family, number)) != 0) { |
---|
290 | pt_member = new ali_pt_member(species, matches_min); |
---|
291 | if (pt_member == 0) |
---|
292 | ali_fatal_error("Out of memory"); |
---|
293 | family_list->append_end(pt_member); |
---|
294 | number++; |
---|
295 | } |
---|
296 | |
---|
297 | if (number == 0) |
---|
298 | ali_fatal_error("Specified family too small"); |
---|
299 | |
---|
300 | number = 0; |
---|
301 | while ((species = get_extension_member(specified_family, number)) != 0) { |
---|
302 | pt_member = new ali_pt_member(species, |
---|
303 | (int) (matches_min * percent_min) - 1); |
---|
304 | if (pt_member == 0) |
---|
305 | ali_fatal_error("Out of memory"); |
---|
306 | extension_list->append_end(pt_member); |
---|
307 | number++; |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | free(bs.data); |
---|
312 | return 0; |
---|
313 | } |
---|
314 | |
---|
315 | |
---|
316 | ALI_TLIST<ali_pt_member *> *ALI_PT::get_family_list() |
---|
317 | { |
---|
318 | ALI_TLIST<ali_pt_member *> *ret; |
---|
319 | |
---|
320 | ret = family_list; |
---|
321 | family_list = 0; |
---|
322 | |
---|
323 | return ret; |
---|
324 | } |
---|
325 | |
---|
326 | |
---|
327 | ALI_TLIST<ali_pt_member *> *ALI_PT::get_extension_list() |
---|
328 | { |
---|
329 | ALI_TLIST<ali_pt_member *> *ret; |
---|
330 | |
---|
331 | ret = extension_list; |
---|
332 | extension_list = 0; |
---|
333 | |
---|
334 | return ret; |
---|
335 | } |
---|
336 | |
---|