Debian Codegraph @

hmmer/easel/miniapps/esl-mask.c

raw symbols SCIP
1 /* Mask sequences in a sequence flatfile.
2 *
3 * SRE, Sat Oct 31 09:58:56 2009 [Janelia]
4 */
5 #include <esl_config.h>
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <ctype.h>
11
12 #include "easel.h"
13 #include "esl_fileparser.h"
14 #include "esl_getopts.h"
15 #include "esl_sq.h"
16 #include "esl_sqio.h"
17
18 static char banner[] = "mask sequences in a sequence file";
19 static char usage[] = "[options] <sqfile> <maskfile>";
20
21 static void
22 cmdline_failure(char *argv0, char *format, ...)
23 {
24 va_list argp;
25 va_start(argp, format);
26 vfprintf(stderr, format, argp);
27 va_end(argp);
28 esl_usage(stdout, argv0, usage);
29 printf("\nTo see more help on available options, do %s -h\n\n", argv0);
30 exit(1);
31 }
32
33 static void
34 cmdline_help(char *argv0, ESL_GETOPTS *go)
35 {
36 esl_banner(stdout, argv0, banner);
37 esl_usage (stdout, argv0, usage);
38
39 puts("\n where general options are:");
40 esl_opt_DisplayHelp(stdout, go, 1, 2, 80);
41
42 puts("");
43 puts("The <seqfile> is a sequence file in any accepted format, such as FASTA.");
44 puts("It may be indexed (see esl-sfetch --index) for faster performance.");
45 puts("");
46 puts("The <maskfile> is a space-delimited file; each data line with 3 columns:");
47 puts(" field 1: <seqname> to fetch from <sqfile>");
48 puts(" field 2: <start> coordinate for mask operation, 1..n");
49 puts(" field 3: <end> coordinate for mask operation, 1..n");
50 puts("Lines starting with # are comments, and ignored.)");
51 puts("");
52 exit(0);
53 }
54
55 static ESL_OPTIONS options[] = {
56 /* name type default env range togs reqs incomp help docgroup */
57 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "help; show brief info on version and usage", 1 },
58 { "-o", eslARG_OUTFILE,FALSE, NULL, NULL, NULL, NULL, NULL, "output masked sequences to file <f> instead of stdout", 1 },
59 { "-r", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "reverse: mask exclusive of <start>..<end>, not inclusive", 1 },
60 { "-R", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "random access: fetch seqs from ssi-indexed <sqfile>", 1 },
61 { "-l", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-m", "convert masked residues to lower case", 1 },
62 { "-m", eslARG_CHAR, NULL, NULL, NULL, NULL, NULL, "-l", "convert masked residues to character <c>", 1 },
63 { "-x", eslARG_INT, NULL, NULL, NULL, NULL, NULL, NULL, "mask additional <n> residues beyond <start>,<end>", 1 },
64 { "--informat", eslARG_STRING, FALSE, NULL, NULL, NULL, NULL, NULL, "specify that input file is in format <s>", 1 },
65 { 0,0,0,0,0,0,0,0,0,0 },
66 };
67
68
69 int
70 main(int argc, char **argv)
71 {
72 ESL_GETOPTS *go = NULL; /* application configuration */
73 char *seqfile = NULL; /* sequence file name */
74 char *maskfile = NULL; /* mask coordinate file name */
75 int infmt = eslSQFILE_UNKNOWN; /* format code for seqfile */
76 int outfmt = eslSQFILE_FASTA; /* format code for output seqs */
77 ESL_SQFILE *sqfp = NULL; /* open sequence file */
78 ESL_SQ *sq = NULL; /* current sequence */
79 ESL_FILEPARSER *maskefp = NULL; /* open mask coord file */
80 FILE *ofp = NULL; /* output stream for masked seqs */
81 char *source = NULL; /* name of current seq to mask */
82 char *p1, *p2; /* pointers used in parsing */
83 int64_t start, end; /* start, end coord for masking */
84 int64_t i, j, pos; /* coords in a sequence */
85 int64_t overmask; /* # of extra residues to mask */
86
87 int do_fetching;
88 int do_lowercase;
89 int maskchar;
90 int status; /* easel return code */
91
92
93 /****************************************************************************
94 * Parse command line
95 ****************************************************************************/
96
97 go = esl_getopts_Create(options);
98 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK) cmdline_failure(argv[0], "Failed to parse command line: %s\n", go->errbuf);
99 if (esl_opt_VerifyConfig(go) != eslOK) cmdline_failure(argv[0], "Error in configuration: %s\n", go->errbuf);
100 if (esl_opt_GetBoolean(go, "-h") ) cmdline_help (argv[0], go);
101 if (esl_opt_ArgNumber(go) != 2) cmdline_failure(argv[0], "Incorrect number of command line arguments.\n");
102
103 do_fetching = esl_opt_GetBoolean(go, "-R");
104 do_lowercase = esl_opt_GetBoolean(go, "-l");
105 overmask = (esl_opt_IsOn(go, "-x") ? esl_opt_GetInteger(go, "-x") : 0);
106 maskchar = (esl_opt_IsOn(go, "-m") ? esl_opt_GetChar(go, "-m") : 'X');
107
108 seqfile = esl_opt_GetArg(go, 1);
109 maskfile = esl_opt_GetArg(go, 2);
110
111 /* Open the <seqfile>: text mode, not digital */
112 if (esl_opt_GetString(go, "--informat") != NULL) {
113 infmt = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--informat"));
114 if (infmt == eslSQFILE_UNKNOWN) cmdline_failure(argv[0], "%s is not a valid input sequence file format for --informat");
115 }
116 sq = esl_sq_Create();
117 status = esl_sqfile_Open(seqfile, infmt, NULL, &sqfp);
118 if (status == eslENOTFOUND) cmdline_failure(argv[0], "Sequence file %s not found.\n", seqfile);
119 else if (status == eslEFORMAT) cmdline_failure(argv[0], "Format of file %s unrecognized.\n", seqfile);
120 else if (status == eslEINVAL) cmdline_failure(argv[0], "Can't autodetect stdin or .gz.\n");
121 else if (status != eslOK) cmdline_failure(argv[0], "Open failed, code %d.\n", status);
122
123 if (do_fetching && sqfp->data.ascii.ssi == NULL)
124 cmdline_failure(argv[0], "-R option (random access/fetching) requires %s to be SSI indexed\n", seqfile);
125
126 /* Open the <maskfile> */
127 if (esl_fileparser_Open(maskfile, NULL, &maskefp) != eslOK)
128 cmdline_failure(argv[0], "Failed to open mask coordinate file %s\n", maskfile);
129 esl_fileparser_SetCommentChar(maskefp, '#');
130
131 /* Open the output file, if any */
132 if (esl_opt_GetString(go, "-o") != NULL)
133 {
134 if ((ofp = fopen(esl_opt_GetString(go, "-o"), "w")) == NULL)
135 cmdline_failure(argv[0], "Failed to open output file %s\n", esl_opt_GetString(go, "-o"));
136 }
137 else ofp = stdout;
138
139
140 /****************************************************************************
141 * Main loop over lines in <maskfile>
142 ****************************************************************************/
143
144 /* Read one data line at a time from the <maskfile>;
145 * parse into data fields <seqname> <start> <end>
146 */
147 while (esl_fileparser_NextLine(maskefp) == eslOK)
148 {
149 /* First field is sequence name */
150 if (esl_fileparser_GetTokenOnLine(maskefp, &source, NULL) != eslOK)
151 esl_fatal("Failed to read source seq name on line %d of file %s\n", maskefp->linenumber, maskfile);
152
153 /* Get the sequence */
154 if (do_fetching)
155 { /* If the <seqfile> is SSI indexed, try to reposition it and read <source> seq by random access */
156 status = esl_sqio_Fetch(sqfp, source, sq);
157 if (status == eslENOTFOUND) esl_fatal("seq %s not found in SSI index for file %s\n", source, sqfp->filename);
158 else if (status == eslEINVAL) esl_fatal("No SSI index or can't reposition in file %s\n", sqfp->filename);
159 else if (status == eslEFORMAT) esl_fatal("Parse failed:\n%s\n", esl_sqfile_GetErrorBuf(sqfp));
160 else if (status != eslOK) esl_fatal("Unexpected failure in fetching %s from file %s\n", source, sqfp->filename);
161 }
162 else
163 { /* else, assume we're reading sequentially; <sqfile> and <maskfile> have seqs in same order */
164 status = esl_sqio_Read(sqfp, sq);
165 if (status == eslEOF) esl_fatal("File %s ended prematurely; didn't find %s\n", sqfp->filename, source);
166 else if (status == eslEFORMAT) esl_fatal("Parse failed:\n%s\n", esl_sqfile_GetErrorBuf(sqfp));
167 else if (status != eslOK) esl_fatal("Unexpected error reading sequence file %s\n", sqfp->filename);
168
169 if ((strcmp(sq->name, source) != 0) && (strcmp(sq->acc, source) != 0))
170 esl_fatal("Sequences in <sqfile> and <maskfile> aren't in same order; try -R");
171 }
172
173 /* If we're masking by lowercase, first make sure everything's uppercase */
174 if (do_lowercase)
175 for (pos = 0; pos < sq->n; pos++)
176 if (isalpha(sq->seq[pos]))
177 sq->seq[pos] = toupper(sq->seq[pos]);
178
179 /* Next two fields are <start>, <end> for the masking */
180 /* possible future extension: wrap loop around this, enable multiple masked regions */
181 if (esl_fileparser_GetTokenOnLine(maskefp, &p1, NULL) != eslOK)
182 esl_fatal("Failed to read start coord on line %d of file %s\n", maskefp->linenumber, maskfile);
183 start = strtoll(p1, &p2, 0) - 1;
184
185 if (esl_fileparser_GetTokenOnLine(maskefp, &p2, NULL) != eslOK)
186 esl_fatal("Failed to read end coord on line %d of file %s\n", maskefp->linenumber, maskfile);
187 end = strtoll(p2, &p1, 0) - 1;
188
189 /* Do the masking */
190 if (esl_opt_GetBoolean(go, "-r")) /* Reverse masking */
191 { /* leave start..end unmasked; mask prefix 0..start-1, end+1..L-1 */
192 i = 0;
193 j = ESL_MIN(sq->n-1, start - 1 + overmask);
194 for (pos = i; pos <= j; pos++)
195 if (isalpha(sq->seq[pos]))
196 sq->seq[pos] = (do_lowercase ? tolower(sq->seq[pos]) : maskchar);
197
198 i = ESL_MAX(0, end + 1 - overmask);
199 j = sq->n-1;
200 for (pos = i; pos <= j; pos++)
201 if (isalpha(sq->seq[pos]))
202 sq->seq[pos] = (do_lowercase ? tolower(sq->seq[pos]) : maskchar);
203 }
204 else
205 { /* normal: mask start..end */
206 i = ESL_MAX(0, start - overmask);
207 j = ESL_MIN(sq->n-1, end + overmask);
208 for (pos = i; pos <= j; pos++)
209 if (isalpha(sq->seq[pos]))
210 sq->seq[pos] = (do_lowercase ? tolower(sq->seq[pos]) : maskchar);
211 }
212
213 esl_sqio_Write(ofp, sq, outfmt, FALSE);
214 esl_sq_Reuse(sq);
215 }
216
217 esl_sq_Destroy(sq);
218 esl_fileparser_Close(maskefp);
219 esl_sqfile_Close(sqfp);
220 esl_getopts_Destroy(go);
221 if (ofp != stdout) fclose(ofp);
222 return 0;
223 }
224
225
226