1 | // ********************* INCLUDE |
---|
2 | #include <stdio.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <string.h> |
---|
5 | #include <unistd.h> |
---|
6 | |
---|
7 | #include "ali_misc.hxx" |
---|
8 | #include "ali_pt.hxx" |
---|
9 | |
---|
10 | #define TMP_FILE "/tmp/.pt_client_%i" |
---|
11 | |
---|
12 | |
---|
13 | /* |
---|
14 | * Initialize the communication with the pt server |
---|
15 | */ |
---|
16 | int ALI_PT::init_communication(void) |
---|
17 | { |
---|
18 | const char *user; |
---|
19 | if (!(user = (char *)getenv("USER"))) user = "unknown user"; |
---|
20 | |
---|
21 | /*** create and init local com structure ***/ |
---|
22 | if( aisc_create(link, PT_MAIN, com, MAIN_LOCS, PT_LOCS, &locs, LOCS_USER, user, NULL)){ |
---|
23 | return 1; |
---|
24 | } |
---|
25 | return 0; |
---|
26 | } |
---|
27 | |
---|
28 | char *ALI_PT::get_family_member(char *specifiedfamily,unsigned long number) |
---|
29 | { |
---|
30 | char *ptr = specifiedfamily; |
---|
31 | char *end; |
---|
32 | char *buffer, *dest; |
---|
33 | |
---|
34 | while (number > 0 && *ptr != '\0' && *ptr != ';') { |
---|
35 | while (*ptr != '\0' && *ptr != ';' && *ptr != ',') |
---|
36 | ptr++; |
---|
37 | if (*ptr == ',') |
---|
38 | ptr++; |
---|
39 | number--; |
---|
40 | } |
---|
41 | |
---|
42 | if (*ptr != '\0' && *ptr != ';') { |
---|
43 | end = ptr; |
---|
44 | while (*end != '\0' && *end != ',' && *end != ';') |
---|
45 | end++; |
---|
46 | |
---|
47 | buffer = dest = (char *) CALLOC((unsigned int) |
---|
48 | (end - ptr) + 1, sizeof(char)); |
---|
49 | if (buffer == 0) |
---|
50 | ali_fatal_error("Out of memory"); |
---|
51 | |
---|
52 | while (ptr != end) |
---|
53 | *dest++ = *ptr++; |
---|
54 | *dest = '\0'; |
---|
55 | |
---|
56 | return buffer; |
---|
57 | } |
---|
58 | return 0; |
---|
59 | } |
---|
60 | |
---|
61 | char *ALI_PT::get_extension_member(char *specifiedfamily, unsigned long number) |
---|
62 | { |
---|
63 | char *ptr = specifiedfamily; |
---|
64 | char *end; |
---|
65 | char *buffer, *dest; |
---|
66 | |
---|
67 | while (*ptr != '\0' && *ptr != ';') |
---|
68 | ptr++; |
---|
69 | if (*ptr == ';') |
---|
70 | ptr++; |
---|
71 | |
---|
72 | while (number > 0 && *ptr != '\0') { |
---|
73 | while (*ptr != '\0' && *ptr != ',') |
---|
74 | ptr++; |
---|
75 | if (*ptr == ',') |
---|
76 | ptr++; |
---|
77 | number--; |
---|
78 | } |
---|
79 | |
---|
80 | if (*ptr != '\0') { |
---|
81 | end = ptr; |
---|
82 | while (*end != '\0' && *end != ',') |
---|
83 | end++; |
---|
84 | |
---|
85 | buffer = dest = (char *) CALLOC((unsigned int) (end - ptr) + 1,sizeof(char)); |
---|
86 | if (buffer == 0) |
---|
87 | ali_fatal_error("Out of memory"); |
---|
88 | |
---|
89 | while (ptr != end) |
---|
90 | *dest++ = *ptr++; |
---|
91 | *dest = '\0'; |
---|
92 | |
---|
93 | return buffer; |
---|
94 | } |
---|
95 | |
---|
96 | return 0; |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | int ALI_PT::open(char *servername,GBDATA *gb_main) |
---|
101 | { |
---|
102 | char *socketid; |
---|
103 | |
---|
104 | if (arb_look_and_start_server(AISC_MAGIC_NUMBER,servername,gb_main)){ |
---|
105 | ali_message ("Cannot contact Probe bank server"); |
---|
106 | return -1; |
---|
107 | } |
---|
108 | |
---|
109 | socketid = GBS_read_arb_tcp(servername); |
---|
110 | |
---|
111 | if (!socketid) { |
---|
112 | ali_message ("Cannot find entry in $ARBHOME/arb_tcp.dat"); |
---|
113 | return -1; |
---|
114 | } |
---|
115 | |
---|
116 | link = (aisc_com *)aisc_open(socketid,&com,AISC_MAGIC_NUMBER); |
---|
117 | |
---|
118 | if (!link) { |
---|
119 | ali_message ("Cannot contact Probe bank server "); |
---|
120 | return -1; |
---|
121 | } |
---|
122 | |
---|
123 | if (init_communication()) { |
---|
124 | ali_message ("Cannot contact Probe bank server (2)"); |
---|
125 | return -1; |
---|
126 | } |
---|
127 | |
---|
128 | free(socketid); |
---|
129 | return 0; |
---|
130 | } |
---|
131 | |
---|
132 | void ALI_PT::close(void) |
---|
133 | { |
---|
134 | if (link) aisc_close(link); |
---|
135 | link = 0; |
---|
136 | } |
---|
137 | |
---|
138 | /***************************************************************************** |
---|
139 | * |
---|
140 | * Public funktions |
---|
141 | * |
---|
142 | *****************************************************************************/ |
---|
143 | |
---|
144 | ALI_PT::ALI_PT(ALI_PT_CONTEXT *context) |
---|
145 | { |
---|
146 | link = 0; |
---|
147 | |
---|
148 | fam_list_max = context->fam_list_max; |
---|
149 | ext_list_max = context->ext_list_max; |
---|
150 | percent_min = context->percent_min; |
---|
151 | matches_min = context->matches_min; |
---|
152 | |
---|
153 | family_list = 0; |
---|
154 | extension_list = 0; |
---|
155 | |
---|
156 | if (context->use_specified_family != 0) { |
---|
157 | mode = SpecifiedMode; |
---|
158 | specified_family = strdup(context->use_specified_family); |
---|
159 | } |
---|
160 | else { |
---|
161 | mode = ServerMode; |
---|
162 | specified_family = 0; |
---|
163 | |
---|
164 | ali_message("Connecting to PT server"); |
---|
165 | if (open(context->servername,context->gb_main) != 0) { |
---|
166 | ali_fatal_error("Can't connect to PT server"); |
---|
167 | } |
---|
168 | ali_message("Connection established"); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | ALI_PT::~ALI_PT(void) |
---|
173 | { |
---|
174 | close(); |
---|
175 | |
---|
176 | if (family_list != 0 && !family_list->is_empty()) { |
---|
177 | delete family_list->first(); |
---|
178 | while (family_list->is_next()) |
---|
179 | delete family_list->next(); |
---|
180 | delete family_list; |
---|
181 | } |
---|
182 | if (extension_list != 0 && !extension_list->is_empty()) { |
---|
183 | delete extension_list->first(); |
---|
184 | while (extension_list->is_next()) |
---|
185 | delete extension_list->next(); |
---|
186 | delete extension_list; |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | int ALI_PT::find_family(ALI_SEQUENCE *sequence, int find_type) |
---|
192 | { |
---|
193 | unsigned long number; |
---|
194 | int matches, max_matches; |
---|
195 | char *seq_name; |
---|
196 | T_PT_FAMILYLIST f_list; |
---|
197 | ali_pt_member *pt_member; |
---|
198 | char *species; |
---|
199 | |
---|
200 | bytestring bs; |
---|
201 | bs.data = sequence->string(); |
---|
202 | bs.size = strlen(bs.data)+1; |
---|
203 | |
---|
204 | family_list = new ALI_TLIST<ali_pt_member *>; |
---|
205 | extension_list = new ALI_TLIST<ali_pt_member *>; |
---|
206 | if (family_list == 0 || extension_list == 0) |
---|
207 | ali_fatal_error("Out of memory"); |
---|
208 | |
---|
209 | if (mode == ServerMode) { |
---|
210 | /* |
---|
211 | * Start find_family() at the PT_SERVER |
---|
212 | * |
---|
213 | * Here we have to make a loop, until the match count of the |
---|
214 | * first member is big enought |
---|
215 | * |
---|
216 | */ |
---|
217 | |
---|
218 | if (aisc_put(link, PT_LOCS, locs, |
---|
219 | LOCS_FIND_TYPE, find_type, |
---|
220 | LOCS_FIND_FAMILY, &bs,0)){ |
---|
221 | ali_message ("Communication Error (2)"); |
---|
222 | return -1; |
---|
223 | } |
---|
224 | |
---|
225 | /* |
---|
226 | * Read family list |
---|
227 | */ |
---|
228 | aisc_get(link,PT_LOCS, locs,LOCS_FAMILY_LIST, &f_list, 0); |
---|
229 | if (f_list == 0) |
---|
230 | ali_error("Family not found in PT Server"); |
---|
231 | |
---|
232 | aisc_get(link, PT_FAMILYLIST, f_list, |
---|
233 | FAMILYLIST_NAME,&seq_name, |
---|
234 | FAMILYLIST_MATCHES,&matches, |
---|
235 | FAMILYLIST_NEXT, &f_list,0); |
---|
236 | |
---|
237 | while (strcmp(seq_name,sequence->name()) == 0) { |
---|
238 | free(seq_name); |
---|
239 | if (f_list == 0) |
---|
240 | ali_error("Family too small in PT Server"); |
---|
241 | aisc_get(link, PT_FAMILYLIST, f_list, |
---|
242 | FAMILYLIST_NAME,&seq_name, |
---|
243 | FAMILYLIST_MATCHES,&matches, |
---|
244 | FAMILYLIST_NEXT, &f_list,0); |
---|
245 | } |
---|
246 | /* found the first element */ |
---|
247 | |
---|
248 | /* make the family list */ |
---|
249 | max_matches = matches; |
---|
250 | number = 0; |
---|
251 | do { |
---|
252 | pt_member = new ali_pt_member(seq_name,matches); |
---|
253 | family_list->append_end(pt_member); |
---|
254 | number++; |
---|
255 | do { |
---|
256 | if (f_list == 0) |
---|
257 | ali_error("Family too small in PT Server"); |
---|
258 | aisc_get(link, PT_FAMILYLIST, f_list, |
---|
259 | FAMILYLIST_NAME,&seq_name, |
---|
260 | FAMILYLIST_MATCHES,&matches, |
---|
261 | FAMILYLIST_NEXT, &f_list,0); |
---|
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 == 0) |
---|
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,0); |
---|
281 | if (strcmp(seq_name,sequence->name()) == 0) |
---|
282 | free(seq_name); |
---|
283 | } while (strcmp(seq_name,sequence->name()) == 0); |
---|
284 | } |
---|
285 | } |
---|
286 | else { |
---|
287 | number = 0; |
---|
288 | while ((species = get_family_member(specified_family, number)) != 0) { |
---|
289 | pt_member = new ali_pt_member(species,matches_min); |
---|
290 | if (pt_member == 0) |
---|
291 | ali_fatal_error("Out of memory"); |
---|
292 | family_list->append_end(pt_member); |
---|
293 | number++; |
---|
294 | } |
---|
295 | |
---|
296 | if (number == 0) |
---|
297 | ali_fatal_error("Specified family too small"); |
---|
298 | |
---|
299 | number = 0; |
---|
300 | while ((species = get_extension_member(specified_family, number)) != 0) { |
---|
301 | pt_member = new ali_pt_member(species, |
---|
302 | (int) (matches_min * percent_min) - 1); |
---|
303 | if (pt_member == 0) |
---|
304 | ali_fatal_error("Out of memory"); |
---|
305 | extension_list->append_end(pt_member); |
---|
306 | number++; |
---|
307 | } |
---|
308 | } |
---|
309 | |
---|
310 | free((char *) bs.data); |
---|
311 | return 0; |
---|
312 | } |
---|
313 | |
---|
314 | |
---|
315 | ALI_TLIST<ali_pt_member *> *ALI_PT::get_family_list(void) |
---|
316 | { |
---|
317 | ALI_TLIST<ali_pt_member *> *ret; |
---|
318 | |
---|
319 | ret = family_list; |
---|
320 | family_list = 0; |
---|
321 | |
---|
322 | return ret; |
---|
323 | } |
---|
324 | |
---|
325 | |
---|
326 | ALI_TLIST<ali_pt_member *> *ALI_PT::get_extension_list(void) |
---|
327 | { |
---|
328 | ALI_TLIST<ali_pt_member *> *ret; |
---|
329 | |
---|
330 | ret = extension_list; |
---|
331 | extension_list = 0; |
---|
332 | |
---|
333 | return ret; |
---|
334 | } |
---|
335 | |
---|
336 | |
---|
337 | |
---|
338 | /* |
---|
339 | int ALI_PT::first_family_(char **seq_name, int *matches) |
---|
340 | { |
---|
341 | if (mode == ServerMode) { |
---|
342 | aisc_get(link,PT_LOCS, locs,LOCS_FAMILY_LIST, &f_list, 0); |
---|
343 | if (f_list == 0) |
---|
344 | return -1; |
---|
345 | |
---|
346 | aisc_get(link, PT_FAMILYLIST, f_list, |
---|
347 | FAMILYLIST_NAME,seq_name, |
---|
348 | FAMILYLIST_MATCHES,matches, |
---|
349 | FAMILYLIST_NEXT, &f_list,0); |
---|
350 | } |
---|
351 | else { |
---|
352 | *seq_name = strdup(family_list.first()); |
---|
353 | *matches = 1000; |
---|
354 | first_extension_call_flag = 1; |
---|
355 | } |
---|
356 | |
---|
357 | return 0; |
---|
358 | } |
---|
359 | |
---|
360 | int ALI_PT::next_family_(char **seq_name, int *matches) |
---|
361 | { |
---|
362 | if (mode == ServerMode) { |
---|
363 | |
---|
364 | if (f_list == 0) |
---|
365 | return -1; |
---|
366 | |
---|
367 | aisc_get(link, PT_FAMILYLIST, f_list, |
---|
368 | FAMILYLIST_NAME,seq_name, |
---|
369 | FAMILYLIST_MATCHES,matches, |
---|
370 | FAMILYLIST_NEXT, &f_list,0); |
---|
371 | } |
---|
372 | else { |
---|
373 | if (family_list.is_next()) { |
---|
374 | *seq_name = strdup(family_list.next()); |
---|
375 | *matches = 1000; |
---|
376 | first_extension_call_flag = 1; |
---|
377 | } |
---|
378 | else { |
---|
379 | if (extension_list.is_empty()) |
---|
380 | return -1; |
---|
381 | if (first_extension_call_flag == 1) { |
---|
382 | *seq_name = strdup(extension_list.first()); |
---|
383 | *matches = (int) (1000 * percent_for_family) - 1; |
---|
384 | first_extension_call_flag = 0; |
---|
385 | } |
---|
386 | else { |
---|
387 | if (extension_list.is_next()) { |
---|
388 | *seq_name = strdup(extension_list.next()); |
---|
389 | *matches = (int) (1000 * percent_for_family) - 1; |
---|
390 | } |
---|
391 | else { |
---|
392 | return -1; |
---|
393 | } |
---|
394 | } |
---|
395 | } |
---|
396 | } |
---|
397 | |
---|
398 | return 0; |
---|
399 | } |
---|
400 | |
---|
401 | |
---|
402 | */ |
---|