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