Debian Codegraph @

easel/miniapps/esl-seqrange.c

raw symbols SCIP
1 /* Determine range of sequences for one of many parallel processes.
2 *
3 * EPN, Wed Mar 24 07:23:27 2010
4 */
5 #include <esl_config.h>
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10
11 #include "easel.h"
12 #include "esl_getopts.h"
13 #include "esl_fileparser.h"
14 #include "esl_keyhash.h"
15 #include "esl_regexp.h"
16 #include "esl_ssi.h"
17 #include "esl_sq.h"
18 #include "esl_sqio.h"
19
20 static char banner[] = "determine range of sequences for one of many parallel processes";
21 static char usage[] = "[options] <sqfile> <procidx> <nproc>";
22
23 static void
24 cmdline_failure(char *argv0, char *format, ...)
25 {
26 va_list argp;
27 va_start(argp, format);
28 vfprintf(stderr, format, argp);
29 va_end(argp);
30 esl_usage(stdout, argv0, usage);
31 printf("\nTo see more help on available options, do %s -h\n\n", argv0);
32 exit(1);
33 }
34
35 static void
36 cmdline_help(char *argv0, ESL_GETOPTS *go)
37 {
38 esl_banner(stdout, argv0, banner);
39 esl_usage (stdout, argv0, usage);
40 puts("\n where general options are:");
41 esl_opt_DisplayHelp(stdout, go, 1, 2, 80);
42 exit(0);
43 }
44
45 static ESL_OPTIONS options[] = {
46 /* name type default env range togs reqs incomp help docgroup */
47 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "help; show brief info on version and usage", 1 },
48 { "--informat", eslARG_STRING, FALSE, NULL, NULL, NULL, NULL, NULL, "specify that input file is in format <s>", 1 },
49
50 { 0,0,0,0,0,0,0,0,0,0 },
51 };
52
53 static void range_by_seqnum(ESL_SQFILE *sqfp, int nproc, int64_t **ret_final_sqidx);
54
55 int
56 main(int argc, char **argv)
57 {
58 ESL_GETOPTS *go = NULL; /* application configuration */
59 char *seqfile = NULL; /* sequence file name */
60 int infmt = eslSQFILE_UNKNOWN; /* format code for seqfile */
61 ESL_SQFILE *sqfp = NULL; /* open sequence file */
62 int status; /* easel return code */
63 int procidx; /* processor index we want range for */
64 int nproc; /* total number processors (>= procidx) */
65 int64_t *final_sqidx; /* [0..p..nproc-1] index (in sqfp) of final seq for proc p+1,
66 * sequence index ranges 1..nseq_total */
67
68 /***********************************************
69 * Parse command line
70 ***********************************************/
71
72 go = esl_getopts_Create(options);
73 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) cmdline_failure(argv[0], "Failed to parse command line: %s\n", go->errbuf);
74 if (esl_opt_VerifyConfig(go) != eslOK) cmdline_failure(argv[0], "Error in configuration: %s\n", go->errbuf);
75 if (esl_opt_GetBoolean(go, "-h") ) cmdline_help (argv[0], go);
76 if (esl_opt_ArgNumber(go) != 3) cmdline_failure(argv[0], "Incorrect number of command line arguments.\n");
77
78 seqfile = esl_opt_GetArg(go, 1);
79 procidx = atoi(esl_opt_GetArg(go, 2));
80 nproc = atoi(esl_opt_GetArg(go, 3));
81
82 /* Validate command line args */
83 if(procidx > nproc) cmdline_failure(argv[0], "<procidx> must be less than or equal to <nproc>\n");
84 if(procidx < 1) cmdline_failure(argv[0], "minimum allowed value for <procidx> is 1\n");
85 if(nproc < 1) cmdline_failure(argv[0], "minimum allowed value for <nproc> is 1\n");
86
87 /* Open the sequence file */
88 if (esl_opt_GetString(go, "--informat") != NULL) {
89 infmt = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--informat"));
90 if (infmt == eslSQFILE_UNKNOWN) esl_fatal("%s is not a valid input sequence file format for --informat");
91 }
92 status = esl_sqfile_Open(seqfile, infmt, NULL, &sqfp);
93 if (status == eslENOTFOUND) cmdline_failure(argv[0], "Sequence file %s not found.\n", seqfile);
94 else if (status == eslEFORMAT) cmdline_failure(argv[0], "Format of file %s unrecognized.\n", seqfile);
95 else if (status == eslEINVAL) cmdline_failure(argv[0], "Can't autodetect stdin or .gz.\n");
96 else if (status != eslOK) cmdline_failure(argv[0], "Open failed, code %d.\n", status);
97
98 /* Exit if seqfile is an alignment, is gzipped or is piped from stdin, we can't handle this */
99 if (sqfp->data.ascii.do_gzip) cmdline_failure(argv[0], "Can't determine seq range for a .gz compressed file\n");
100 if (sqfp->data.ascii.do_stdin) cmdline_failure(argv[0], "Can't determine seq range for a standard input pipe\n");
101 if (esl_sqio_IsAlignment(sqfp->format)) cmdline_failure(argv[0], "Can't determine seq range for an alignment file\n");
102
103 /* Open the SSI index for retrieval, it is required */
104 status = esl_sqfile_OpenSSI(sqfp, NULL);
105 if (status == eslEFORMAT) cmdline_failure(argv[0], "SSI index is in incorrect format\n");
106 else if (status == eslERANGE) cmdline_failure(argv[0], "SSI index is in 64-bit format and we can't read it\n");
107 else if (status != eslOK) cmdline_failure(argv[0], "Failed to open SSI index\n");
108
109 /* Verify that we have more processors than sequences */
110 ESL_SQASCII_DATA *ascii = &sqfp->data.ascii;
111 if(ascii->ssi->nprimary < nproc) cmdline_failure(argv[0], "Fewer sequences than processors (<nproc>); %d < %d\n", ascii->ssi->nprimary, nproc);
112
113 /* Determine the range for all <nproc> processors */
114 range_by_seqnum(sqfp, nproc, &(final_sqidx));
115
116 /* Output range for desired nproc to stdout */
117 printf("%" PRId64 "-%" PRId64 "\n", (procidx == 1) ? 1 : final_sqidx[procidx-2]+1, final_sqidx[procidx-1]); /* careful: final_sqidx runs 0..nproc-1 */
118
119 free(final_sqidx);
120 esl_sqfile_Close(sqfp);
121 esl_getopts_Destroy(go);
122 return 0;
123 }
124
125
126 /* Determine seq range for <nproc> processors with goal of putting an identical
127 * number of sequences (or as close to identical as possible) in each chunk,
128 * one chunk per processor.
129 */
130 static void
131 range_by_seqnum(ESL_SQFILE *sqfp, int nproc, int64_t **ret_final_sqidx)
132 {
133 int status;
134 int64_t *final_sqidx; /* [0..p..nproc-1] index (in sqfp) of final seq for proc p+1
135 * sequence index ranges 1..nseq_total */
136 int p; /* counter over processes */
137 int64_t nseq_per_proc; /* num seqs per processor for even split */
138 int64_t nseq_total; /* total number of sequences in sqfp */
139 int64_t remainder; /* num extra seqs after even split across all procs */
140 int64_t nseq_used; /* running total of nseqs on all procs */
141 ESL_SQASCII_DATA *ascii = &sqfp->data.ascii;
142
143 ESL_ALLOC(final_sqidx, sizeof(int64_t) * nproc);
144
145 nseq_total = ascii->ssi->nprimary;
146
147 nseq_per_proc = (int) (nseq_total / nproc);
148 remainder = nseq_total - (nseq_per_proc * nproc);
149 nseq_used = 0;
150 for(p = 0; p < nproc; p++) {
151 nseq_used += nseq_per_proc;
152 if(p < remainder) nseq_used++;
153 final_sqidx[p] = nseq_used;
154 }
155 if(nseq_used != nseq_total) esl_fatal("Unable to split up sequences properly, coding error");
156
157 *ret_final_sqidx = final_sqidx;
158
159 return;
160
161 ERROR:
162 esl_fatal("Out of memory");
163 return; /* NEVERREACHED */
164 }