source: tags/svn.1.5.4/NALIGNER/ali_pt.cxx

Last change on this file was 8100, checked in by westram, 14 years ago

merge from ptpan_back [8016] [8017] [8021] [8051] [8067]

  • added method to get status of GB_shell object
  • removed possibility to execute command via DB-server - always use GB_system instead
  • valgrinded external tools called by arb_test
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 KB
Line 
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
13int 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
26char *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
59char *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
98int 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    link = aisc_open(socketid, &com, AISC_MAGIC_NUMBER);
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
127void ALI_PT::close()
128{
129    if (link) aisc_close(link);
130    link = 0;
131}
132
133ALI_PT::ALI_PT(ALI_PT_CONTEXT *context)
134{
135    link = 0;
136
137    fam_list_max = context->fam_list_max;
138    ext_list_max = context->ext_list_max;
139    percent_min = context->percent_min;
140    matches_min = context->matches_min;
141
142    family_list = 0;
143    extension_list = 0;
144
145    if (context->use_specified_family != 0) {
146        mode = SpecifiedMode;
147        specified_family = strdup(context->use_specified_family);
148    }
149    else {
150        mode = ServerMode;
151        specified_family = 0;
152
153        ali_message("Connecting to PT server");
154        if (open(context->servername) != 0) {
155            ali_fatal_error("Can't connect to PT server");
156        }
157        ali_message("Connection established");
158    }
159}
160
161ALI_PT::~ALI_PT()
162{
163    close();
164
165    if (family_list != 0 && !family_list->is_empty()) {
166        delete family_list->first();
167        while (family_list->is_next())
168            delete family_list->next();
169        delete family_list;
170    }
171    if (extension_list != 0 && !extension_list->is_empty()) {
172        delete extension_list->first();
173        while (extension_list->is_next())
174            delete extension_list->next();
175        delete extension_list;
176    }
177}
178
179
180int ALI_PT::find_family(ALI_SEQUENCE *sequence, int find_type)
181{
182    unsigned long number;
183    int matches, max_matches;
184    char *seq_name;
185    T_PT_FAMILYLIST f_list;
186    ali_pt_member *pt_member;
187    char *species;
188
189    bytestring bs;
190    bs.data = sequence->string();
191    bs.size = strlen(bs.data)+1;
192
193    family_list = new ALI_TLIST<ali_pt_member *>;
194    extension_list = new ALI_TLIST<ali_pt_member *>;
195    if (family_list == 0 || extension_list == 0)
196        ali_fatal_error("Out of memory");
197
198    if (mode == ServerMode) {
199        /* Start find_family() at the PT_SERVER
200         *
201         * Here we have to make a loop, until the match count of the
202         * first member is big enought
203         */
204
205        T_PT_FAMILYFINDER ffinder;
206        if (aisc_create(link, PT_LOCS, locs,
207                        LOCS_FFINDER, PT_FAMILYFINDER, &ffinder, 
208                        FAMILYFINDER_FIND_TYPE,   find_type,
209                        FAMILYFINDER_FIND_FAMILY, &bs,
210                        NULL))
211        {
212            ali_message ("Communication Error (2)");
213            return -1;
214        }
215
216        // Read family list
217        aisc_get(link, PT_FAMILYFINDER, ffinder, FAMILYFINDER_FAMILY_LIST, &f_list, NULL);
218        if (f_list == 0)
219            ali_error("Family not found in PT Server");
220
221        aisc_get(link, PT_FAMILYLIST, f_list,
222                 FAMILYLIST_NAME, &seq_name,
223                 FAMILYLIST_MATCHES, &matches,
224                 FAMILYLIST_NEXT, &f_list, NULL);
225
226        while (strcmp(seq_name, sequence->name()) == 0) {
227            free(seq_name);
228            if (f_list == 0)
229                ali_error("Family too small in PT Server");
230            aisc_get(link, PT_FAMILYLIST, f_list,
231                     FAMILYLIST_NAME, &seq_name,
232                     FAMILYLIST_MATCHES, &matches,
233                     FAMILYLIST_NEXT, &f_list, NULL);
234        }
235        // found the first element
236
237        // make the family list
238        max_matches = matches;
239        number = 0;
240        do {
241            pt_member = new ali_pt_member(seq_name, matches);
242            family_list->append_end(pt_member);
243            number++;
244            do {
245                if (f_list == 0)
246                    ali_error("Family too small in PT Server");
247                aisc_get(link, PT_FAMILYLIST, f_list,
248                         FAMILYLIST_NAME, &seq_name,
249                         FAMILYLIST_MATCHES, &matches,
250                         FAMILYLIST_NEXT, &f_list, NULL);
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 == 0)
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, NULL);
270                if (strcmp(seq_name, sequence->name()) == 0)
271                    free(seq_name);
272            } while (strcmp(seq_name, sequence->name()) == 0);
273        }
274    }
275    else {
276        number = 0;
277        while ((species = get_family_member(specified_family, number)) != 0) {
278            pt_member = new ali_pt_member(species, matches_min);
279            if (pt_member == 0)
280                ali_fatal_error("Out of memory");
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)) != 0) {
290            pt_member = new ali_pt_member(species,
291                                          (int) (matches_min * percent_min) - 1);
292            if (pt_member == 0)
293                ali_fatal_error("Out of memory");
294            extension_list->append_end(pt_member);
295            number++;
296        }
297    }
298
299    free(bs.data);
300    return 0;
301}
302
303
304ALI_TLIST<ali_pt_member *> *ALI_PT::get_family_list()
305{
306    ALI_TLIST<ali_pt_member *> *ret;
307
308    ret = family_list;
309    family_list = 0;
310
311    return ret;
312}
313
314
315ALI_TLIST<ali_pt_member *> *ALI_PT::get_extension_list()
316{
317    ALI_TLIST<ali_pt_member *> *ret;
318
319    ret = extension_list;
320    extension_list = 0;
321
322    return ret;
323}
324
Note: See TracBrowser for help on using the repository browser.