| 1 | #include <stdio.h> |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <memory.h> |
|---|
| 4 | // #include <malloc.h> |
|---|
| 5 | #include <string.h> |
|---|
| 6 | |
|---|
| 7 | #include <PT_com.h> |
|---|
| 8 | #include <client.h> |
|---|
| 9 | |
|---|
| 10 | #include <arbdb.h> |
|---|
| 11 | #include <servercntrl.h> |
|---|
| 12 | |
|---|
| 13 | struct apd_sequence { |
|---|
| 14 | apd_sequence *next; |
|---|
| 15 | const char *sequence; |
|---|
| 16 | }; |
|---|
| 17 | |
|---|
| 18 | struct Params{ |
|---|
| 19 | int DESIGNCPLIPOUTPUT; |
|---|
| 20 | int SERVERID; |
|---|
| 21 | const char *DESINGNAMES; |
|---|
| 22 | int DESIGNPROBELENGTH; |
|---|
| 23 | const char *DESIGNSEQUENCE; |
|---|
| 24 | |
|---|
| 25 | int MINTEMP; |
|---|
| 26 | int MAXTEMP; |
|---|
| 27 | int MINGC; |
|---|
| 28 | int MAXGC; |
|---|
| 29 | int MAXBOND; |
|---|
| 30 | int MINPOS; |
|---|
| 31 | int MAXPOS; |
|---|
| 32 | int MISHIT; |
|---|
| 33 | int MINTARGETS; |
|---|
| 34 | const char *SEQUENCE; |
|---|
| 35 | int MISMATCHES; |
|---|
| 36 | int COMPLEMENT; |
|---|
| 37 | int WEIGHTED; |
|---|
| 38 | |
|---|
| 39 | apd_sequence *sequence; |
|---|
| 40 | } P; |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | struct gl_struct { |
|---|
| 44 | aisc_com *link; |
|---|
| 45 | T_PT_LOCS locs; |
|---|
| 46 | T_PT_MAIN com; |
|---|
| 47 | int pd_design_id; |
|---|
| 48 | } pd_gl; |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | int init_local_com_struct() |
|---|
| 52 | { |
|---|
| 53 | const char *user = GB_getenvUSER(); |
|---|
| 54 | |
|---|
| 55 | if( aisc_create(pd_gl.link, PT_MAIN, pd_gl.com, |
|---|
| 56 | MAIN_LOCS, PT_LOCS, &pd_gl.locs, |
|---|
| 57 | LOCS_USER, user, |
|---|
| 58 | NULL)){ |
|---|
| 59 | return 1; |
|---|
| 60 | } |
|---|
| 61 | return 0; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | void aw_message(const char *error){ |
|---|
| 65 | printf("%s\n",error); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | static const char *AP_probe_pt_look_for_server() |
|---|
| 69 | { |
|---|
| 70 | char choice[256]; |
|---|
| 71 | sprintf(choice,"ARB_PT_SERVER%i",P.SERVERID); |
|---|
| 72 | |
|---|
| 73 | GB_ERROR error = arb_look_and_start_server(AISC_MAGIC_NUMBER,choice,0); |
|---|
| 74 | if (error) { |
|---|
| 75 | aw_message(error); |
|---|
| 76 | return 0; |
|---|
| 77 | } |
|---|
| 78 | return GBS_read_arb_tcp(choice); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | int probe_design_send_data(T_PT_PDC pdc) { |
|---|
| 83 | // int i; |
|---|
| 84 | // char buffer[256]; |
|---|
| 85 | |
|---|
| 86 | if (aisc_put(pd_gl.link,PT_PDC, pdc, PDC_CLIPRESULT, P.DESIGNCPLIPOUTPUT, NULL)) |
|---|
| 87 | return 1; |
|---|
| 88 | |
|---|
| 89 | return 0; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | void AP_probe_design_event() { |
|---|
| 93 | T_PT_PDC pdc; |
|---|
| 94 | T_PT_TPROBE tprobe; |
|---|
| 95 | bytestring bs; |
|---|
| 96 | char *match_info; |
|---|
| 97 | |
|---|
| 98 | { |
|---|
| 99 | const char *servername = AP_probe_pt_look_for_server(); |
|---|
| 100 | |
|---|
| 101 | if (!servername) return; |
|---|
| 102 | pd_gl.link = (aisc_com *)aisc_open(servername, &pd_gl.com,AISC_MAGIC_NUMBER); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | if (!pd_gl.link) { |
|---|
| 106 | aw_message ("Cannot contact Probe bank server "); |
|---|
| 107 | return; |
|---|
| 108 | } |
|---|
| 109 | if (init_local_com_struct() ) { |
|---|
| 110 | aw_message ("Cannot contact Probe bank server (2)"); |
|---|
| 111 | return; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | bs.data = (char*)(P.DESINGNAMES); |
|---|
| 115 | bs.size = strlen(bs.data)+1; |
|---|
| 116 | |
|---|
| 117 | aisc_create(pd_gl.link,PT_LOCS, pd_gl.locs, |
|---|
| 118 | LOCS_PROBE_DESIGN_CONFIG, PT_PDC, &pdc, |
|---|
| 119 | PDC_PROBELENGTH,P.DESIGNPROBELENGTH, |
|---|
| 120 | PDC_MINTEMP, (double)P.MINTEMP, |
|---|
| 121 | PDC_MAXTEMP, (double)P.MAXTEMP, |
|---|
| 122 | PDC_MINGC, P.MINGC/100.0, |
|---|
| 123 | PDC_MAXGC, P.MAXGC/100.0, |
|---|
| 124 | PDC_MAXBOND, (double)P.MAXBOND, |
|---|
| 125 | NULL); |
|---|
| 126 | aisc_put(pd_gl.link,PT_PDC, pdc, |
|---|
| 127 | PDC_MINPOS, P.MINPOS, |
|---|
| 128 | PDC_MAXPOS, P.MAXPOS, |
|---|
| 129 | PDC_MISHIT, P.MISHIT, |
|---|
| 130 | PDC_MINTARGETS, P.MINTARGETS/100.0, |
|---|
| 131 | NULL); |
|---|
| 132 | |
|---|
| 133 | if (probe_design_send_data(pdc)) { |
|---|
| 134 | aw_message ("Connection to PT_SERVER lost (1)"); |
|---|
| 135 | return; |
|---|
| 136 | } |
|---|
| 137 | apd_sequence *s; |
|---|
| 138 | for ( s= P.sequence; s; s = s->next){ |
|---|
| 139 | bytestring bs_seq; |
|---|
| 140 | T_PT_SEQUENCE pts; |
|---|
| 141 | bs_seq.data = (char*)s->sequence; |
|---|
| 142 | bs_seq.size = strlen(bs_seq.data)+1; |
|---|
| 143 | aisc_create(pd_gl.link, PT_PDC, pdc, |
|---|
| 144 | PDC_SEQUENCE, PT_SEQUENCE, &pts, |
|---|
| 145 | SEQUENCE_SEQUENCE, &bs_seq, |
|---|
| 146 | NULL); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | aisc_put(pd_gl.link,PT_PDC, pdc, |
|---|
| 150 | PDC_NAMES,&bs, |
|---|
| 151 | PDC_GO, 0, |
|---|
| 152 | NULL); |
|---|
| 153 | |
|---|
| 154 | { |
|---|
| 155 | char *locs_error = 0; |
|---|
| 156 | if (aisc_get( pd_gl.link, PT_LOCS, pd_gl.locs, |
|---|
| 157 | LOCS_ERROR ,&locs_error, |
|---|
| 158 | NULL)){ |
|---|
| 159 | aw_message ("Connection to PT_SERVER lost (1)"); |
|---|
| 160 | return; |
|---|
| 161 | } |
|---|
| 162 | if (*locs_error) { |
|---|
| 163 | aw_message(locs_error); |
|---|
| 164 | } |
|---|
| 165 | delete locs_error; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | aisc_get( pd_gl.link, PT_PDC, pdc, |
|---|
| 169 | PDC_TPROBE, &tprobe, |
|---|
| 170 | NULL); |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | if (tprobe) { |
|---|
| 174 | aisc_get( pd_gl.link, PT_TPROBE, tprobe, |
|---|
| 175 | TPROBE_INFO_HEADER, &match_info, |
|---|
| 176 | NULL); |
|---|
| 177 | printf("%s\n",match_info); |
|---|
| 178 | free(match_info); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | while ( tprobe ){ |
|---|
| 183 | if (aisc_get( pd_gl.link, PT_TPROBE, tprobe, |
|---|
| 184 | TPROBE_NEXT, &tprobe, |
|---|
| 185 | TPROBE_INFO, &match_info, |
|---|
| 186 | NULL)) break; |
|---|
| 187 | printf("%s\n",match_info); |
|---|
| 188 | } |
|---|
| 189 | aisc_close(pd_gl.link); pd_gl.link = 0; |
|---|
| 190 | return; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | void AP_probe_match_event() |
|---|
| 194 | { |
|---|
| 195 | T_PT_PDC pdc; |
|---|
| 196 | T_PT_MATCHLIST match_list; |
|---|
| 197 | char *probe = 0; |
|---|
| 198 | char *locs_error; |
|---|
| 199 | |
|---|
| 200 | { |
|---|
| 201 | const char *servername = AP_probe_pt_look_for_server(); |
|---|
| 202 | |
|---|
| 203 | if (!servername) return; |
|---|
| 204 | pd_gl.link = (aisc_com *)aisc_open(servername, &pd_gl.com,AISC_MAGIC_NUMBER); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | if (!pd_gl.link) { |
|---|
| 208 | aw_message ("Cannot contact Probe bank server "); |
|---|
| 209 | return; |
|---|
| 210 | } |
|---|
| 211 | if (init_local_com_struct() ) { |
|---|
| 212 | aw_message ("Cannot contact Probe bank server (2)"); |
|---|
| 213 | return; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | aisc_create(pd_gl.link,PT_LOCS, pd_gl.locs, |
|---|
| 217 | LOCS_PROBE_DESIGN_CONFIG, PT_PDC, &pdc, |
|---|
| 218 | NULL); |
|---|
| 219 | if (probe_design_send_data(pdc)) { |
|---|
| 220 | aw_message ("Connection to PT_SERVER lost (2)"); |
|---|
| 221 | return; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | if (aisc_nput(pd_gl.link,PT_LOCS, pd_gl.locs, |
|---|
| 227 | LOCS_MATCH_REVERSED, P.COMPLEMENT, |
|---|
| 228 | LOCS_MATCH_SORT_BY, P.WEIGHTED, |
|---|
| 229 | LOCS_MATCH_COMPLEMENT, 0, |
|---|
| 230 | LOCS_MATCH_MAX_MISMATCHES, P.MISMATCHES, |
|---|
| 231 | LOCS_MATCH_MAX_SPECIES, 100000, |
|---|
| 232 | LOCS_SEARCHMATCH, P.SEQUENCE, |
|---|
| 233 | NULL)){ |
|---|
| 234 | free(probe); |
|---|
| 235 | aw_message ("Connection to PT_SERVER lost (2)"); |
|---|
| 236 | return; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | long match_list_cnt; |
|---|
| 240 | bytestring bs; |
|---|
| 241 | bs.data = 0; |
|---|
| 242 | aisc_get( pd_gl.link, PT_LOCS, pd_gl.locs, |
|---|
| 243 | LOCS_MATCH_LIST, &match_list, |
|---|
| 244 | LOCS_MATCH_LIST_CNT, &match_list_cnt, |
|---|
| 245 | LOCS_MATCH_STRING, &bs, |
|---|
| 246 | LOCS_ERROR, &locs_error, |
|---|
| 247 | NULL); |
|---|
| 248 | if (*locs_error) { |
|---|
| 249 | aw_message(locs_error); |
|---|
| 250 | } |
|---|
| 251 | free(locs_error); |
|---|
| 252 | |
|---|
| 253 | char toksep[2]; |
|---|
| 254 | toksep[0] = 1; |
|---|
| 255 | toksep[1] = 0; |
|---|
| 256 | printf("%s\n",bs.data); |
|---|
| 257 | |
|---|
| 258 | aisc_close(pd_gl.link); |
|---|
| 259 | free(bs.data); |
|---|
| 260 | |
|---|
| 261 | return; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | int pargc; |
|---|
| 265 | char **pargv; |
|---|
| 266 | int helpflag; |
|---|
| 267 | |
|---|
| 268 | int getInt(const char *param, int val, int min, int max, const char *description){ |
|---|
| 269 | if (helpflag){ |
|---|
| 270 | printf(" %s=%3i [%3i:%3i] %s\n",param,val,min,max,description); |
|---|
| 271 | return 0; |
|---|
| 272 | } |
|---|
| 273 | int i; |
|---|
| 274 | char *s = 0; |
|---|
| 275 | |
|---|
| 276 | arb_assert(pargc >= 1); // otherwise s stays 0 |
|---|
| 277 | |
|---|
| 278 | for (i=1;i<pargc;i++){ |
|---|
| 279 | s = pargv[i]; |
|---|
| 280 | if (*s == '-') s++; |
|---|
| 281 | if (!strncasecmp(s,param,strlen(param))) break; |
|---|
| 282 | } |
|---|
| 283 | if (i==pargc) return val; |
|---|
| 284 | s+= strlen(param); |
|---|
| 285 | if (*s != '=') return val; |
|---|
| 286 | s++; |
|---|
| 287 | val = atoi(s); |
|---|
| 288 | pargc--; // remove parameter |
|---|
| 289 | for (;i<pargc;i++){ |
|---|
| 290 | pargv[i] = pargv[i+1]; |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | if (val<min) val = min; |
|---|
| 294 | if (val>max) val = max; |
|---|
| 295 | return val; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | const char *getString(const char *param, const char *val, const char *description){ |
|---|
| 299 | if (helpflag){ |
|---|
| 300 | if (!val) val = ""; |
|---|
| 301 | printf(" %s=%s %s\n",param,val,description); |
|---|
| 302 | return 0; |
|---|
| 303 | } |
|---|
| 304 | int i; |
|---|
| 305 | char *s = 0; |
|---|
| 306 | |
|---|
| 307 | arb_assert(pargc >= 1); // otherwise s stays 0 |
|---|
| 308 | |
|---|
| 309 | for (i=1;i<pargc;i++){ |
|---|
| 310 | s = pargv[i]; |
|---|
| 311 | if (*s == '-') s++; |
|---|
| 312 | if (!strncasecmp(s,param,strlen(param))) break; |
|---|
| 313 | } |
|---|
| 314 | if (i==pargc) return val; |
|---|
| 315 | s+= strlen(param); |
|---|
| 316 | if (*s != '=') return val; |
|---|
| 317 | s++; |
|---|
| 318 | pargc--; // remove parameter |
|---|
| 319 | for (;i<pargc;i++){ |
|---|
| 320 | pargv[i] = pargv[i+1]; |
|---|
| 321 | } |
|---|
| 322 | return s; |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | int main(int argc,char ** argv){ |
|---|
| 326 | pargc = argc; |
|---|
| 327 | pargv = argv; |
|---|
| 328 | if (argc<=1) helpflag = 1; |
|---|
| 329 | else helpflag = 0; |
|---|
| 330 | |
|---|
| 331 | P.SERVERID = getInt("serverid" ,0,0,100, "Server Id, look into $ARBHOME/lib/arb_tcp.dat"); |
|---|
| 332 | |
|---|
| 333 | P.DESIGNCPLIPOUTPUT = getInt("designmaxhits",100,10,10000, "Maximum Number of Probe Design Suggestions"); |
|---|
| 334 | P.DESINGNAMES = getString("designnames","", "List of short names separated by '#'"); |
|---|
| 335 | P.sequence = 0; |
|---|
| 336 | while ((P.DESIGNSEQUENCE = getString("designsequence",0, "Additional Sequences, will be added to the target group"))) { |
|---|
| 337 | apd_sequence *s = new apd_sequence; |
|---|
| 338 | s->next = P.sequence; |
|---|
| 339 | P.sequence = s; |
|---|
| 340 | s->sequence = P.DESIGNSEQUENCE; |
|---|
| 341 | P.DESIGNSEQUENCE = 0; |
|---|
| 342 | } |
|---|
| 343 | P.DESIGNPROBELENGTH = getInt("designprobelength",18,10,100, "Length of probe"); |
|---|
| 344 | P.MINTEMP = getInt("designmintemp",0,0,400, "Minimum melting temperature of probe"); |
|---|
| 345 | P.MAXTEMP = getInt("designmaxtemp",400,0,400, "Maximum melting temperature of probe"); |
|---|
| 346 | P.MINGC = getInt("desingmingc",30,0,100, "Minimum gc content"); |
|---|
| 347 | P.MAXGC = getInt("desingmaxgc",80,0,100, "Maximum gc content"); |
|---|
| 348 | P.MAXBOND = getInt("desingmaxbond",0,0,10, "Not implemented"); |
|---|
| 349 | |
|---|
| 350 | P.MINPOS = getInt("desingminpos",0,0,10000, "Minimum ecoli position"); |
|---|
| 351 | P.MAXPOS = getInt("desingmaxpos",10000,0,10000, "Maximum ecoli position"); |
|---|
| 352 | |
|---|
| 353 | P.MISHIT = getInt("designmishit",0,0,10000, "Number of allowed hits outside the selected group"); |
|---|
| 354 | P.MINTARGETS = getInt("designmintargets",50,0,100, "Minimum percentage of hits within the selected species"); |
|---|
| 355 | |
|---|
| 356 | P.SEQUENCE = getString("matchsequence","agtagtagt","The sequence to search for"); |
|---|
| 357 | P.MISMATCHES = getInt("matchmismatches", 0,0,5, "Maximum Number of allowed mismatches"); |
|---|
| 358 | P.COMPLEMENT = getInt("matchcomplement", 0,0,1, "Match reversed and complemented probe"); |
|---|
| 359 | P.WEIGHTED = getInt("matchweighted", 0,0,1, "Use weighted mismatches"); |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | if (pargc>1){ |
|---|
| 363 | printf("Unknown Parameter %s\n", pargv[1]); |
|---|
| 364 | exit(-1); |
|---|
| 365 | } |
|---|
| 366 | if (helpflag) return 0; |
|---|
| 367 | |
|---|
| 368 | if (*P.DESINGNAMES || P.sequence){ |
|---|
| 369 | AP_probe_design_event(); |
|---|
| 370 | }else{ |
|---|
| 371 | AP_probe_match_event(); |
|---|
| 372 | } |
|---|
| 373 | return 0; |
|---|
| 374 | } |
|---|