ARB
ali_pt.cxx
Go to the documentation of this file.
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) {
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) {
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 
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 
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 
304 
305  ret = family_list;
306  family_list = NULp;
307 
308  return ret;
309 }
310 
311 
314 
315  ret = extension_list;
316  extension_list = NULp;
317 
318  return ret;
319 }
320 
const char * GB_ERROR
Definition: arb_core.h:25
void ali_out_of_memory_if(bool cond)
Definition: ali_misc.hxx:52
int has_next()
Definition: ali_tlist.hxx:168
int aisc_close(aisc_com *link, AISC_Object &object)
Definition: client.c:249
int find_family(ALI_SEQUENCE *sequence, int find_type=1)
Definition: ali_pt.cxx:177
char * ARB_strdup(const char *str)
Definition: arb_string.h:27
GB_ERROR arb_look_and_start_server(long magic_number, const char *arb_tcp_env)
float percent_min
Definition: ali_pt.hxx:34
char buffer[MESSAGE_BUFFERSIZE]
Definition: seq_search.cxx:34
ALI_PT(ALI_PT_CONTEXT *context)
Definition: ali_pt.cxx:132
GB_ERROR GB_await_error()
Definition: arb_msg.cxx:342
void * CALLOC(long i, long j)
Definition: ali_misc.hxx:56
const char * GBS_read_arb_tcp(const char *env)
Definition: adtcp.cxx:325
char * data
Definition: bytestring.h:16
void ali_error(const char *message, const char *func)
Definition: ali_main.cxx:90
ALI_TLIST< ali_pt_member * > * get_family_list()
Definition: ali_pt.cxx:302
int is_empty()
Definition: ali_tlist.hxx:165
char * name()
char * string()
char * servername
Definition: ali_pt.hxx:30
#define AISC_MAGIC_NUMBER
Definition: client_privat.h:51
ALI_TLIST< ali_pt_member * > * get_extension_list()
Definition: ali_pt.cxx:312
void ali_fatal_error(const char *message, const char *func)
Definition: ali_main.cxx:85
unsigned long ext_list_max
Definition: ali_pt.hxx:36
GB_CSTR GB_getenvUSER(void)
Definition: adsocket.cxx:545
aisc_com * aisc_open(const char *path, AISC_Object &main_obj, long magic, GB_ERROR *error)
Definition: client.c:205
void ali_message(const char *message)
Definition: ali_misc.hxx:46
#define NULp
Definition: cxxforward.h:116
int aisc_get(aisc_com *link, int o_type, const AISC_Object &object,...)
Definition: client.c:266
~ALI_PT()
Definition: ali_pt.cxx:159
char * use_specified_family
Definition: ali_pt.hxx:38
void append_end(T &a)
Definition: ali_tlist.hxx:198
int matches_min
Definition: ali_pt.hxx:33
unsigned long fam_list_max
Definition: ali_pt.hxx:35
int aisc_create(aisc_com *link, int father_type, const AISC_Object &father, int attribute, int object_type, AISC_Object &object,...)
Definition: client.c:593