Debian Codegraph @

hmmer/src/hmmc2.c

raw symbols SCIP
1 /* daemon client.
2 */
3 #include <p7_config.h>
4
5 #ifdef HMMER_THREADS
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.h>
11 #include <unistd.h>
12 #include <signal.h>
13 #include <ctype.h>
14
15 #include <sys/socket.h>
16 #ifdef HAVE_NETINET_IN_H
17 #include <netinet/in.h> /* On FreeBSD, you need netinet/in.h for struct sockaddr_in */
18 #endif /* On OpenBSD, netinet/in.h is required for (must precede) arpa/inet.h */
19 #include <arpa/inet.h>
20
21 #include "easel.h"
22 #include "esl_alphabet.h"
23 #include "esl_getopts.h"
24 #include "esl_sq.h"
25 #include "esl_sqio.h"
26 #include "esl_stopwatch.h"
27
28 #include "hmmer.h"
29 #include "hmmpgmd.h"
30
31 #define SERVER_PORT 51371
32 #define MAX_READ_LEN 4096
33
34 #define REPOPTS "-E,-T,--cut_ga,--cut_nc,--cut_tc"
35 #define DOMREPOPTS "--domE,--domT,--cut_ga,--cut_nc,--cut_tc"
36 #define INCOPTS "--incE,--incT,--cut_ga,--cut_nc,--cut_tc"
37 #define INCDOMOPTS "--incdomE,--incdomT,--cut_ga,--cut_nc,--cut_tc"
38 #define THRESHOPTS "-E,-T,--domE,--domT,--incE,--incT,--incdomE,--incdomT,--cut_ga,--cut_nc,--cut_tc"
39
40 static ESL_OPTIONS searchOpts[] = {
41 /* name type default env range toggles reqs incomp help docgroup*/
42 /* Control of output */
43 { "--acc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "prefer accessions over names in output", 2 },
44 { "--noali", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "don't output alignments, so output is smaller", 2 },
45 { "--notextw", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "--textw", "unlimit ASCII text output line width", 2 },
46 { "--textw", eslARG_INT, "120", NULL, "n>=120",NULL, NULL, "--notextw", "set max width of ASCII text output lines", 2 },
47 /* Control of scoring system */
48 { "--popen", eslARG_REAL, "0.02", NULL, "0<=x<0.5",NULL, NULL, NULL, "gap open probability", 3 },
49 { "--pextend", eslARG_REAL, "0.4", NULL, "0<=x<1", NULL, NULL, NULL, "gap extend probability", 3 },
50 { "--mx", eslARG_STRING, "BLOSUM62", NULL, NULL, NULL, NULL, "--mxfile", "substitution score matrix choice (of some built-in matrices)", 3 },
51 { "--mxfile", eslARG_INFILE, NULL, NULL, NULL, NULL, NULL, "--mx", "read substitution score matrix from file <f>", 3 },
52 /* Control of reporting thresholds */
53 { "-E", eslARG_REAL, "10.0", NULL, "x>0", NULL, NULL, REPOPTS, "report sequences <= this E-value threshold in output", 4 },
54 { "-T", eslARG_REAL, FALSE, NULL, NULL, NULL, NULL, REPOPTS, "report sequences >= this score threshold in output", 4 },
55 { "--domE", eslARG_REAL, "10.0", NULL, "x>0", NULL, NULL, DOMREPOPTS, "report domains <= this E-value threshold in output", 4 },
56 { "--domT", eslARG_REAL, FALSE, NULL, NULL, NULL, NULL, DOMREPOPTS, "report domains >= this score cutoff in output", 4 },
57 /* Control of inclusion (significance) thresholds */
58 { "--incE", eslARG_REAL, "0.01", NULL, "x>0", NULL, NULL, INCOPTS, "consider sequences <= this E-value threshold as significant", 5 },
59 { "--incT", eslARG_REAL, FALSE, NULL, NULL, NULL, NULL, INCOPTS, "consider sequences >= this score threshold as significant", 5 },
60 { "--incdomE", eslARG_REAL, "0.01", NULL, "x>0", NULL, NULL, INCDOMOPTS, "consider domains <= this E-value threshold as significant", 5 },
61 { "--incdomT", eslARG_REAL, FALSE, NULL, NULL, NULL, NULL, INCDOMOPTS, "consider domains >= this score threshold as significant", 5 },
62 /* Model-specific thresholding for both reporting and inclusion */
63 { "--cut_ga", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, THRESHOPTS, "use profile's GA gathering cutoffs to set all thresholding", 6 },
64 { "--cut_nc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, THRESHOPTS, "use profile's NC noise cutoffs to set all thresholding", 6 },
65 { "--cut_tc", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, THRESHOPTS, "use profile's TC trusted cutoffs to set all thresholding", 6 },
66 /* Control of acceleration pipeline */
67 { "--max", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--F1,--F2,--F3", "Turn all heuristic filters off (less speed, more power)", 7 },
68 { "--F1", eslARG_REAL, "0.02", NULL, NULL, NULL, NULL, "--max", "Stage 1 (MSV) threshold: promote hits w/ P <= F1", 7 },
69 { "--F2", eslARG_REAL, "1e-3", NULL, NULL, NULL, NULL, "--max", "Stage 2 (Vit) threshold: promote hits w/ P <= F2", 7 },
70 { "--F3", eslARG_REAL, "1e-5", NULL, NULL, NULL, NULL, "--max", "Stage 3 (Fwd) threshold: promote hits w/ P <= F3", 7 },
71 { "--nobias", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, "--max", "turn off composition bias filter", 7 },
72 /* Control of E-value calibration */
73 { "--EmL", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "length of sequences for MSV Gumbel mu fit", 11 },
74 { "--EmN", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "number of sequences for MSV Gumbel mu fit", 11 },
75 { "--EvL", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "length of sequences for Viterbi Gumbel mu fit", 11 },
76 { "--EvN", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "number of sequences for Viterbi Gumbel mu fit", 11 },
77 { "--EfL", eslARG_INT, "100", NULL,"n>0", NULL, NULL, NULL, "length of sequences for Forward exp tail tau fit", 11 },
78 { "--EfN", eslARG_INT, "200", NULL,"n>0", NULL, NULL, NULL, "number of sequences for Forward exp tail tau fit", 11 },
79 { "--Eft", eslARG_REAL, "0.04", NULL,"0<x<1", NULL, NULL, NULL, "tail mass for Forward exponential tail tau fit", 11 },
80 /* Other options */
81 { "--seed", eslARG_INT, "42", NULL, "n>=0", NULL, NULL, NULL, "set RNG seed to <n> (if 0: one-time arbitrary seed)", 12 },
82 { "--nonull2", eslARG_NONE, NULL, NULL, NULL, NULL, NULL, NULL, "turn off biased composition score corrections", 12 },
83 { "-Z", eslARG_REAL, FALSE, NULL, "x>0", NULL, NULL, NULL, "set # of comparisons done, for E-value calculation", 12 },
84 { "--domZ", eslARG_REAL, FALSE, NULL, "x>0", NULL, NULL, NULL, "set # of significant seqs, for domain E-value calculation", 12 },
85 { "--hmmdb", eslARG_INT, NULL, NULL, "n>0", NULL, NULL, "--seqdb", "hmm database to search", 12 },
86 { "--seqdb", eslARG_INT, NULL, NULL, "n>0", NULL, NULL, "--hmmdb", "protein database to search", 12 },
87 { "--seqdb_ranges",eslARG_STRING, NULL, NULL, NULL, NULL, "--seqdb", NULL, "range(s) of sequences within --seqdb that will be searched", 12 },
88
89 /* name type default env range toggles reqs incomp help docgroup*/
90 { "-c", eslARG_INT, "1", NULL, NULL, NULL, NULL, "--seqdb", "use alt genetic code of NCBI transl table <n>", 15 },
91 { "-l", eslARG_INT, "20", NULL, NULL, NULL, NULL, "--seqdb", "minimum ORF length", 15 },
92 { "-m", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-M,--seqdb", "ORFs must initiate with AUG only", 15 },
93 { "-M", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-m,--seqdb", "ORFs must start with allowed initiation codon", 15 },
94 { "--informat", eslARG_STRING, FALSE, NULL, NULL, NULL, NULL, "--seqdb", "specify that input file is in format <s>", 15 },
95 { "--watson", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--seqdb", "only translate top strand", 15 },
96 { "--crick", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "--seqdb", "only translate bottom strand", 15 },
97
98 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
99 };
100
101 //static char banner_seq[] = "search a protein sequence against a sequence database";
102 //static char banner_hmm[] = "search profile(s) against a sequence database";
103
104 static void
105 sig_int(int signo)
106 {
107 fprintf(stderr, "Exiting due to ctrl-c\n");
108 exit(1);
109 }
110
111 #if 0
112 static int
113 output_header(FILE *ofp, const ESL_GETOPTS *sopt, char *seqfile, char *banner)
114 {
115 int status;
116
117 p7_banner(ofp, "hmmpgmd client", banner);
118
119 if (fprintf(ofp, "# target sequence database: %s\n", seqfile) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
120
121 if (esl_opt_IsUsed(sopt, "--acc") && fprintf(ofp, "# prefer accessions over names: yes\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
122 if (esl_opt_IsUsed(sopt, "--noali") && fprintf(ofp, "# show alignments in output: no\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
123 if (esl_opt_IsUsed(sopt, "--notextw") && fprintf(ofp, "# max ASCII text line length: unlimited\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
124 if (esl_opt_IsUsed(sopt, "--textw") && fprintf(ofp, "# max ASCII text line length: %d\n", esl_opt_GetInteger(sopt, "--textw")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
125 if (esl_opt_IsUsed(sopt, "--popen") && fprintf(ofp, "# gap open probability: %f\n", esl_opt_GetReal (sopt, "--popen")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
126 if (esl_opt_IsUsed(sopt, "--pextend") && fprintf(ofp, "# gap extend probability: %f\n", esl_opt_GetReal (sopt, "--pextend")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
127 if (esl_opt_IsUsed(sopt, "--mx") && fprintf(ofp, "# subst score matrix (built-in): %s\n", esl_opt_GetString (sopt, "--mx")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
128 if (esl_opt_IsUsed(sopt, "--mxfile") && fprintf(ofp, "# subst score matrix (file): %s\n", esl_opt_GetString (sopt, "--mxfile")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
129 if (esl_opt_IsUsed(sopt, "-E") && fprintf(ofp, "# sequence reporting threshold: E-value <= %g\n", esl_opt_GetReal (sopt, "-E")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
130 if (esl_opt_IsUsed(sopt, "-T") && fprintf(ofp, "# sequence reporting threshold: score >= %g\n", esl_opt_GetReal (sopt, "-T")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
131 if (esl_opt_IsUsed(sopt, "--domE") && fprintf(ofp, "# domain reporting threshold: E-value <= %g\n", esl_opt_GetReal (sopt, "--domE")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
132 if (esl_opt_IsUsed(sopt, "--domT") && fprintf(ofp, "# domain reporting threshold: score >= %g\n", esl_opt_GetReal (sopt, "--domT")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
133 if (esl_opt_IsUsed(sopt, "--incE") && fprintf(ofp, "# sequence inclusion threshold: E-value <= %g\n", esl_opt_GetReal (sopt, "--incE")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
134 if (esl_opt_IsUsed(sopt, "--incT") && fprintf(ofp, "# sequence inclusion threshold: score >= %g\n", esl_opt_GetReal (sopt, "--incT")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
135 if (esl_opt_IsUsed(sopt, "--incdomE") && fprintf(ofp, "# domain inclusion threshold: E-value <= %g\n", esl_opt_GetReal (sopt, "--incdomE")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
136 if (esl_opt_IsUsed(sopt, "--incdomT") && fprintf(ofp, "# domain inclusion threshold: score >= %g\n", esl_opt_GetReal (sopt, "--incdomT")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
137 if (esl_opt_IsUsed(sopt, "--cut_ga") && fprintf(ofp, "# model-specific thresholding: GA cutoffs\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
138 if (esl_opt_IsUsed(sopt, "--cut_nc") && fprintf(ofp, "# model-specific thresholding: NC cutoffs\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
139 if (esl_opt_IsUsed(sopt, "--cut_tc") && fprintf(ofp, "# model-specific thresholding: TC cutoffs\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
140 if (esl_opt_IsUsed(sopt, "--max") && fprintf(ofp, "# Max sensitivity mode: on [all heuristic filters off]\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
141 if (esl_opt_IsUsed(sopt, "--F1") && fprintf(ofp, "# MSV filter P threshold: <= %g\n", esl_opt_GetReal(sopt, "--F1")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
142 if (esl_opt_IsUsed(sopt, "--F2") && fprintf(ofp, "# Vit filter P threshold: <= %g\n", esl_opt_GetReal(sopt, "--F2")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
143 if (esl_opt_IsUsed(sopt, "--F3") && fprintf(ofp, "# Fwd filter P threshold: <= %g\n", esl_opt_GetReal(sopt, "--F3")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
144 if (esl_opt_IsUsed(sopt, "--nobias") && fprintf(ofp, "# biased composition HMM filter: off\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
145 if (esl_opt_IsUsed(sopt, "--nonull2") && fprintf(ofp, "# null2 bias corrections: off\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
146 if (esl_opt_IsUsed(sopt, "--EmL") && fprintf(ofp, "# seq length, MSV Gumbel mu fit: %d\n", esl_opt_GetInteger(sopt, "--EmL")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
147 if (esl_opt_IsUsed(sopt, "--EmN") && fprintf(ofp, "# seq number, MSV Gumbel mu fit: %d\n", esl_opt_GetInteger(sopt, "--EmN")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
148 if (esl_opt_IsUsed(sopt, "--EvL") && fprintf(ofp, "# seq length, Vit Gumbel mu fit: %d\n", esl_opt_GetInteger(sopt, "--EvL")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
149 if (esl_opt_IsUsed(sopt, "--EvN") && fprintf(ofp, "# seq number, Vit Gumbel mu fit: %d\n", esl_opt_GetInteger(sopt, "--EvN")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
150 if (esl_opt_IsUsed(sopt, "--EfL") && fprintf(ofp, "# seq length, Fwd exp tau fit: %d\n", esl_opt_GetInteger(sopt, "--EfL")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
151 if (esl_opt_IsUsed(sopt, "--EfN") && fprintf(ofp, "# seq number, Fwd exp tau fit: %d\n", esl_opt_GetInteger(sopt, "--EfN")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
152 if (esl_opt_IsUsed(sopt, "--Eft") && fprintf(ofp, "# tail mass for Fwd exp tau fit: %f\n", esl_opt_GetReal (sopt, "--Eft")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
153 if (esl_opt_IsUsed(sopt, "-Z") && fprintf(ofp, "# sequence search space set to: %.0f\n", esl_opt_GetReal (sopt, "-Z")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
154 if (esl_opt_IsUsed(sopt, "--domZ") && fprintf(ofp, "# domain search space set to: %.0f\n", esl_opt_GetReal (sopt, "--domZ")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
155 if (esl_opt_IsUsed(sopt, "--seed")) {
156 if (esl_opt_GetInteger(sopt, "--seed") == 0 && fprintf(ofp, "# random number seed: one-time arbitrary\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
157 else if ( fprintf(ofp, "# random number seed set to: %d\n", esl_opt_GetInteger(sopt, "--seed")) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
158 }
159
160 if (fprintf(ofp, "# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
161 return eslOK;
162 }
163 #endif /*0; SRE removed to silence compiler warning, looks like MSF wasn't using it*/
164
165 static int
166 usage(char *pgm)
167 {
168 if (fprintf(stderr, "Usage: %s [-i addr] [-p port] [-A] [-S]\n", pgm) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
169 if (fprintf(stderr, " -S : print sequence scores\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
170 if (fprintf(stderr, " -A : print sequence alignments\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
171 if (fprintf(stderr, " -i addr : ip address running daemon (default: 127.0.0.1)\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
172 if (fprintf(stderr, " -p port : port daemon listens to clients on (default: 51371)\n") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "write failed");
173 exit(1);
174 }
175
176 int main(int argc, char *argv[])
177 {
178 int i;
179 uint64_t n;
180 int eod;
181 int size;
182
183 char *seq;
184 char opts[MAX_READ_LEN];
185 int seqlen;
186
187 int ali;
188 int scores;
189
190 char buffer[MAX_READ_LEN];
191
192 int status = eslOK;
193
194 ESL_GETOPTS *go = NULL;
195 ESL_STOPWATCH *w = NULL;
196 P7_PIPELINE *pli = NULL;
197 P7_TOPHITS *th = NULL;
198
199 HMMD_SEARCH_STATS *stats;
200 HMMD_SEARCH_STATUS sstatus;
201 uint8_t *buf;
202 uint32_t buf_offset, hits_start;
203 char *ptr;
204 int sock;
205 char serv_ip[64];
206 unsigned short serv_port;
207
208 struct sockaddr_in serv_addr;
209
210 buf = NULL;
211 buf_offset = 0;
212
213 /* set up defaults */
214 strcpy(serv_ip, "127.0.0.1");
215 serv_port = SERVER_PORT;
216 scores = 0;
217 ali = 0;
218
219 i = 1;
220 while (i < argc) {
221 if (argv[i][0] != '-') usage(argv[0]);
222 if (argv[i][1] == 0 || argv[i][2] != 0) usage(argv[0]);
223 switch (argv[i][1]) {
224 case 'p':
225 if (i + 1 >= argc) {
226 printf("Missing port number\n");
227 usage(argv[0]);
228 }
229 serv_port = atoi(argv[i+1]);
230 ++i;
231 break;
232 case 'i':
233 if (i + 1 >= argc) {
234 printf("Missing ip address\n");
235 usage(argv[0]);
236 }
237 strcpy(serv_ip, argv[i+1]);
238 ++i;
239 break;
240 case 'A':
241 ali = 1;
242 scores = 1;
243 break;
244 case 'S':
245 scores = 1;
246 break;
247 default:
248 usage(argv[0]);
249 }
250 ++i;
251 }
252
253 seqlen = MAX_READ_LEN;
254 if ((seq = malloc(seqlen)) == NULL) {
255 fprintf(stderr, "[%s:%d] malloc error %d - %s\n", __FILE__, __LINE__, errno, strerror(errno));
256 exit(1);
257 }
258
259 /* set up a signal handler for broken pipes */
260 if (signal(SIGINT, sig_int) == SIG_ERR) {
261 fprintf(stderr, "[%s:%d] signal error %d - %s\n", __FILE__, __LINE__, errno, strerror(errno));
262 exit(1);
263 }
264
265 /* Create a reliable, stream socket using TCP */
266 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
267 fprintf(stderr, "[%s:%d] socket error %d - %s\n", __FILE__, __LINE__, errno, strerror(errno));
268 exit(1);
269 }
270
271 /* Construct the server address structure */
272 memset(&serv_addr, 0, sizeof(serv_addr));
273 serv_addr.sin_family = AF_INET;
274 serv_addr.sin_addr.s_addr = inet_addr(serv_ip);
275 serv_addr.sin_port = htons(serv_port);
276 if ((inet_pton(AF_INET, serv_ip, &serv_addr.sin_addr)) < 0) {
277 fprintf(stderr, "[%s:%d] inet_pton error %d - %s\n", __FILE__, __LINE__, errno, strerror(errno));
278 exit(1);
279 }
280
281 /* Establish the connection to the echo server */
282 if (connect(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
283 fprintf(stderr, "[%s:%d] connect error %d - %s\n", __FILE__, __LINE__, errno, strerror(errno));
284 exit(1);
285 }
286
287 w = esl_stopwatch_Create();
288 go = esl_getopts_Create(searchOpts);
289
290 seq[0] = 0;
291 while (strncmp(seq, "//", 2) != 0) {
292 int rem;
293
294 eod = 0;
295 seq[0] = 0;
296 rem = seqlen - 1;
297 fprintf(stdout, "\n\nEnter next sequence:\n");
298 while (!eod) {
299 if (fgets(buffer, MAX_READ_LEN, stdin) != NULL) {
300 int n = strlen(buffer);
301 if (n >= rem) {
302 if ((seq = realloc(seq, seqlen * 2)) == NULL) {
303 fprintf(stderr, "[%s:%d] realloc error %d - %s\n", __FILE__, __LINE__, errno, strerror(errno));
304 exit(1);
305 }
306 rem += seqlen;
307 seqlen *= 2;
308 }
309 strcat(seq, buffer);
310 rem -= n;
311
312 eod = (strncmp(buffer, "//", 2) == 0);
313 } else {
314 eod = 1;
315 }
316 }
317
318 /* skip all leading white spaces */
319 ptr = seq;
320 while (*ptr && isspace(*ptr)) ++ptr;
321
322 /* process server commands */
323 if (*ptr == '!') {
324
325 /* Send the string to the server */
326 n = strlen(seq);
327 printf ("Sending data %" PRIu64 ":\n", n);
328 if (writen(sock, seq, n) != n) {
329 fprintf(stderr, "[%s:%d] write (size %" PRIu64 ") error %d - %s\n", __FILE__, __LINE__, n, errno, strerror(errno));
330 exit(1);
331 }
332
333 n = sizeof(sstatus);
334 if ((size = readn(sock, &sstatus, n)) == -1) {
335 //printf("MY ERRNO IS %d\n", errno);
336 if(errno == ECONNRESET || errno == ESRCH || errno == EPERM || errno == 0) {
337 // when daemon is shut down normally, the readn() is expected to fail - but w/ various errors, depending on OS, etc.
338 // our design for this should be rethought - I added the ESRCH test to hack around a unit test failure. [SRE 7/25/20]
339 // ... and EPERM, ditto, for OS/X Big Sur with MPI [SRE 4/19/21, xref H10/106]
340 fprintf(stderr, "Daemon exited, shutting down\n");
341 exit(0);
342 }
343 fprintf(stderr, "[%s:%d] read error %d - %s\n", __FILE__, __LINE__, errno, strerror(errno));
344 exit(1);
345 }
346
347 if (sstatus.status != eslOK) {
348 char *ebuf;
349 n = sstatus.msg_size;
350 ebuf = malloc(n);
351 if ((size = readn(sock, ebuf, n)) == -1) {
352 fprintf(stderr, "[%s:%d] read error %d - %s\n", __FILE__, __LINE__, errno, strerror(errno));
353 exit(1);
354 }
355 fprintf(stderr, "ERROR (%d): %s\n", sstatus.status, ebuf);
356 free(ebuf);
357 }
358
359 continue;
360 }
361
362 /* process search specific options */
363 if (*ptr == '@') {
364 char t;
365 char *s = ++ptr;
366
367 /* skip to the end of the line */
368 while (*ptr && (*ptr != '\n' && *ptr != '\r')) ++ptr;
369 t = *ptr;
370 *ptr = 0;
371
372 /* create a commandline string with dummy program name for
373 * the esl_opt_ProcessSpoof() function to parse.
374 */
375 snprintf(opts, sizeof(opts), "X %s\n", s);
376
377 if (esl_getopts_Reuse(go) != eslOK) p7_Die("Internal failure reusing options object");
378 if (esl_opt_ProcessSpoof(go, opts) != eslOK) {
379 printf("Failed to parse options string: %s\n", go->errbuf);
380 continue;
381 }
382 if (esl_opt_VerifyConfig(go) != eslOK) {
383 printf("Failed to parse options string: %s\n", go->errbuf);
384 continue;
385 }
386
387 /* the options string can handle an optional database */
388 if (esl_opt_ArgNumber(go) != 0) {
389 printf("Incorrect number of command line arguments.");
390 continue;
391 }
392
393 /* skip remaining white spaces */
394 *ptr = t;
395 while (*ptr && isspace(*ptr)) ++ptr;
396 }
397
398
399 if (*ptr && strncmp(ptr, "//", 2) != 0) {
400 P7_HMM *hmm = NULL; /* query HMM */
401 // P7_HMMFILE *hfp = NULL; /* open input HMM file */
402 ESL_SQ *sq = NULL; /* one target sequence (digital) */
403 ESL_ALPHABET *abc = NULL; /* digital alphabet */
404
405 status = eslOK;
406 abc = esl_alphabet_Create(eslAMINO);
407
408 if (status == eslOK) {
409 /* Send the string to the server */
410 n = strlen(seq);
411 printf ("Sending data %" PRIu64 ":\n", n);
412 if (writen(sock, seq, n) != n) {
413 fprintf(stderr, "[%s:%d] write (size %" PRIu64 ") error %d - %s\n", __FILE__, __LINE__, n, errno, strerror(errno));
414 exit(1);
415 }
416
417 // Get the status structure back from the server
418 buf = malloc(HMMD_SEARCH_STATUS_SERIAL_SIZE);
419 buf_offset = 0;
420 n = HMMD_SEARCH_STATUS_SERIAL_SIZE;
421 if(buf == NULL){
422 printf("Unable to allocate memory for search status structure\n");
423 exit(1);
424 }
425
426 if ((size = readn(sock, buf, n)) == -1) {
427 fprintf(stderr, "[%s:%d] read error %d - %s\n", __FILE__, __LINE__, errno, strerror(errno));
428 exit(1);
429 }
430
431 if(hmmd_search_status_Deserialize(buf, &buf_offset, &sstatus) != eslOK){
432 printf("Unable to deserialize search status object \n");
433 exit(1);
434 }
435
436 if (sstatus.status != eslOK) {
437 char *ebuf;
438 n = sstatus.msg_size;
439 ebuf = malloc(n);
440 if ((size = readn(sock, ebuf, n)) == -1) {
441 fprintf(stderr, "[%s:%d] read error %d - %s\n", __FILE__, __LINE__, errno, strerror(errno));
442 exit(1);
443 }
444 fprintf(stderr, "ERROR (%d): %s\n", sstatus.status, ebuf);
445 free(ebuf);
446 goto COMPLETE;
447 }
448
449 free(buf); // clear this out
450 buf_offset = 0; // reset to beginning for next serialized object
451 n = sstatus.msg_size;
452
453 if ((buf = malloc(n)) == NULL) {
454 fprintf(stderr, "[%s:%d] malloc error %d - %s\n", __FILE__, __LINE__, errno, strerror(errno));
455 exit(1);
456 }
457 // Grab the serialized search results
458 if ((size = readn(sock, buf, n)) == -1) {
459 fprintf(stderr, "[%s:%d] read error %d - %s\n", __FILE__, __LINE__, errno, strerror(errno));
460 exit(1);
461 }
462
463 if ((stats = malloc(sizeof(HMMD_SEARCH_STATS))) == NULL) {
464 fprintf(stderr, "[%s:%d] malloc error %d - %s\n", __FILE__, __LINE__, errno, strerror(errno));
465 exit(1);
466 }
467 stats->hit_offsets = NULL; // force allocation of memory for this in _Deserialize
468 if(p7_hmmd_search_stats_Deserialize(buf, &buf_offset, stats) != eslOK){
469 printf("Unable to deserialize search stats object \n");
470 exit(1);
471 }
472
473 // Create the structures we'll deserialize the hits into
474 pli = p7_pipeline_Create(go, 100, 100, FALSE, (esl_opt_IsUsed(go, "--seqdb")) ? p7_SEARCH_SEQS : p7_SCAN_MODELS);
475
476 /* copy the search stats */
477 w->elapsed = stats->elapsed;
478 w->user = stats->user;
479 w->sys = stats->sys;
480
481 pli->nmodels = stats->nmodels;
482 pli->nseqs = stats->nseqs;
483 pli->n_past_msv = stats->n_past_msv;
484 pli->n_past_bias = stats->n_past_bias;
485 pli->n_past_vit = stats->n_past_vit;
486 pli->n_past_fwd = stats->n_past_fwd;
487
488 pli->Z = stats->Z;
489 pli->domZ = stats->domZ;
490 pli->Z_setby = stats->Z_setby;
491 pli->domZ_setby = stats->domZ_setby;
492
493 th = p7_tophits_Create();
494
495 free(th->unsrt);
496 free(th->hit);
497
498 th->N = stats->nhits;
499 if ((th->unsrt = malloc(stats-> nhits *sizeof(P7_HIT))) == NULL) {
500 fprintf(stderr, "[%s:%d] malloc error %d - %s\n", __FILE__, __LINE__, errno, strerror(errno));
501 exit(1);
502 }
503 th->nreported = stats->nreported;
504 th->nincluded = stats->nincluded;
505 th->is_sorted_by_seqidx = FALSE;
506 th->is_sorted_by_sortkey = TRUE;
507
508 if ((th->hit = malloc(sizeof(void *) * stats->nhits)) == NULL) {
509 fprintf(stderr, "[%s:%d] malloc error %d - %s\n", __FILE__, __LINE__, errno, strerror(errno));
510 exit(1);
511 }
512 hits_start = buf_offset;
513 // deserialize the hits
514 for (i = 0; i < stats->nhits; ++i) {
515 // set all internal pointers of the hit to NULL before deserializing into it
516 th->unsrt[i].name = NULL;
517 th->unsrt[i].acc = NULL;
518 th->unsrt[i].desc = NULL;
519 th->unsrt[i].dcl = NULL;
520 if((buf_offset -hits_start) != stats->hit_offsets[i]){
521 printf("Hit offset %d did not match expected. Found %d, expected %" PRIu64 "\n", i, (buf_offset-hits_start), stats->hit_offsets[i]);
522 }
523 if(p7_hit_Deserialize(buf, &buf_offset, &(th->unsrt[i])) != eslOK){
524 printf("Unable to deserialize hit %d\n", i);
525 exit(0);
526 }
527 th->hit[i] = &(th->unsrt[i]);
528 }
529
530 /* adjust the reported and included hits */
531 //th->is_sorted = FALSE;
532 //p7_tophits_Sort(th);
533
534 /* Print the results. */
535 if (scores) { p7_tophits_Targets(stdout, th, pli, 120); fprintf(stdout, "\n\n"); }
536 if (ali) { p7_tophits_Domains(stdout, th, pli, 120); fprintf(stdout, "\n\n"); }
537 p7_pli_Statistics(stdout, pli, w);
538
539 p7_pipeline_Destroy(pli);
540 p7_tophits_Destroy(th);
541 free(buf);
542
543 fprintf(stdout, "//\n"); fflush(stdout);
544
545 fprintf(stdout, "Total bytes received %" PRId64 "\n", sstatus.msg_size);
546 } else {
547 printf("Error parsing input query\n");
548 }
549
550 COMPLETE:
551 if (abc) esl_alphabet_Destroy(abc);
552 if (hmm) p7_hmm_Destroy(hmm);
553 if (sq) esl_sq_Destroy(sq);
554
555 }
556 }
557
558 free(seq);
559 esl_getopts_Destroy(go);
560 esl_stopwatch_Destroy(w);
561
562 close(sock);
563 return 0;
564 }
565
566 #endif /*HMMER_THREADS*/
567
568
569
570