source: branches/port5/NALIGNER/ali_pt.cxx

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