Debian Codegraph @

hmmer/easel/miniapps/esl-translate.c

raw symbols SCIP
1 /* Translate DNA sequence into six frames, into individual ORFs.
2 *
3 */
4 #include <esl_config.h>
5
6 #include "easel.h"
7 #include "esl_alphabet.h"
8 #include "esl_gencode.h"
9 #include "esl_getopts.h"
10
11
12 /*****************************************************************
13 * 1. Main loop for reading complete sequences with ReadSeq()
14 *****************************************************************/
15
16 static int
17 do_by_sequences(ESL_GENCODE *gcode, ESL_GENCODE_WORKSTATE *wrk, ESL_SQFILE *sqfp)
18 {
19 ESL_SQ *sq = esl_sq_CreateDigital(gcode->nt_abc);
20 int status;
21
22 while (( status = esl_sqio_Read(sqfp, sq )) == eslOK)
23 {
24 if (sq->n < 3) continue;
25
26 if (wrk->do_watson) {
27 esl_gencode_ProcessStart(gcode, wrk, sq);
28 esl_gencode_ProcessPiece(gcode, wrk, sq);
29 esl_gencode_ProcessEnd(wrk, sq);
30 }
31
32 if (wrk->do_crick) {
33 esl_sq_ReverseComplement(sq);
34 esl_gencode_ProcessStart(gcode, wrk, sq);
35 esl_gencode_ProcessPiece(gcode, wrk, sq);
36 esl_gencode_ProcessEnd(wrk, sq);
37 }
38
39 esl_sq_Reuse(sq);
40 }
41 if (status == eslEFORMAT) esl_fatal("Parse failed (sequence file %s)\n%s\n",
42 sqfp->filename, sqfp->get_error(sqfp));
43 else if (status != eslEOF) esl_fatal("Unexpected error %d reading sequence file %s",
44 status, sqfp->filename);
45
46 esl_sq_Destroy(sq);
47 return eslOK;
48 }
49
50
51 static int
52 do_by_windows(ESL_GENCODE *gcode, ESL_GENCODE_WORKSTATE *wrk, ESL_SQFILE *sqfp)
53 {
54 ESL_SQ *sq = esl_sq_CreateDigital(gcode->nt_abc);
55 int windowsize = 4092; // can be any value, but a multiple of 3 makes most sense. windowsize can be +/-; + means reading top strand; - means bottom strand.
56 int contextsize = 2; // contextsize (adjacent window overlap) must be 2, or translation won't work properly.
57 int wstatus;
58
59 ESL_DASSERT1(( windowsize % 3 == 0 ));
60
61 while (( wstatus = esl_sqio_ReadWindow(sqfp, contextsize, windowsize, sq)) != eslEOF)
62 {
63 if (wstatus == eslEOD)
64 {
65 if ( (windowsize > 0 && wrk->do_watson) || (windowsize < 0 && wrk->do_crick))
66 esl_gencode_ProcessEnd(wrk, sq);
67
68 if (windowsize > 0 && ! wrk->do_crick) { esl_sq_Reuse(sq); continue; } // Don't switch to revcomp if we don't need do. Allows -W --watson to work on nonrewindable streams
69 if (windowsize < 0) esl_sq_Reuse(sq); // Do not Reuse the sq on the switch from watson to crick; ReadWindow needs sq->L
70 windowsize = -windowsize; // switch to other strand.
71 continue;
72 }
73 else if (wstatus == eslEFORMAT) esl_fatal("Parsing failed in sequence file %s:\n%s", sqfp->filename, esl_sqfile_GetErrorBuf(sqfp));
74 else if (wstatus == eslEINVAL) esl_fatal("Invalid residue(s) found in sequence file %s\n%s", sqfp->filename, esl_sqfile_GetErrorBuf(sqfp));
75 else if (wstatus != eslOK) esl_fatal("Unexpected error %d reading sequence file %s", wstatus, sqfp->filename);
76
77 /* If we're the first window in this input DNA sequence
78 * (or the first window in its revcomp), then initialize.
79 * sq->C is the actual context overlap; 0=1st window; 2 (i.e. C)= subsequent.
80 */
81 if (sq->C == 0)
82 {
83 if (sq->n < 3) continue; // DNA sequence too short; skip it, don't even bother to revcomp, go to next sequence.
84 if ( (windowsize > 0 && wrk->do_watson) || (windowsize < 0 && wrk->do_crick))
85 esl_gencode_ProcessStart(gcode, wrk, sq);
86 }
87
88 if ( (windowsize > 0 && wrk->do_watson) || (windowsize < 0 && wrk->do_crick))
89 esl_gencode_ProcessPiece(gcode, wrk, sq);
90 }
91 esl_sq_Destroy(sq);
92 return eslOK;
93 }
94
95
96 /*****************************************************************
97 * x. main() for the esl-translate program
98 *****************************************************************/
99
100 static ESL_OPTIONS options[] = {
101 /* name type default env range toggles reqs incomp help docgroup*/
102 { "-h", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "show brief help on version and usage", 0 },
103 { "-c", eslARG_INT, "1", NULL, NULL, NULL, NULL, NULL, "use alt genetic code of NCBI transl table <n>", 0 },
104 { "-l", eslARG_INT, "20", NULL, NULL, NULL, NULL, NULL, "minimum ORF length", 0 },
105 { "-m", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-M", "ORFs must initiate with AUG only", 0 },
106 { "-M", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, "-m", "ORFs must start with allowed initiation codon", 0 },
107 { "-W", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "use windowed, memory-efficient seq reading", 0 },
108 { "--informat", eslARG_STRING, FALSE, NULL, NULL, NULL, NULL, NULL, "specify that input file is in format <s>", 0 },
109 { "--watson", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "only translate top strand", 0 },
110 { "--crick", eslARG_NONE, FALSE, NULL, NULL, NULL, NULL, NULL, "only translate bottom strand", 0 },
111 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
112 };
113 static char usage[] = "[-options] <seqfile>";
114 static char banner[] = "six-frame translation of nucleic acid seq to ORFs";
115
116
117 int
118 main(int argc, char **argv)
119 {
120 ESL_GETOPTS *go = NULL;
121 ESL_ALPHABET *nt_abc = esl_alphabet_Create(eslDNA);
122 ESL_ALPHABET *aa_abc = esl_alphabet_Create(eslAMINO);
123 ESL_GENCODE *gcode = NULL;
124 ESL_GENCODE_WORKSTATE *wrk = NULL;
125 char *seqfile = NULL;
126 int informat = eslSQFILE_UNKNOWN;
127 ESL_SQFILE *sqfp = NULL;
128 int status;
129
130 /*****************************************************************
131 * command line parsing
132 *****************************************************************/
133
134 go = esl_getopts_Create(options);
135 if (esl_opt_ProcessCmdline(go, argc, argv) != eslOK ||
136 esl_opt_VerifyConfig(go) != eslOK)
137 {
138 printf("Failed to parse command line: %s\n", go->errbuf);
139 esl_usage(stdout, argv[0], usage);
140 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
141 exit(1);
142 }
143
144 if (esl_opt_GetBoolean(go, "-h") )
145 {
146 esl_banner(stdout, argv[0], banner);
147 esl_usage (stdout, argv[0], usage);
148 puts("\n where options are:");
149 esl_opt_DisplayHelp(stdout, go, /*docgroup=*/0, /*indent=*/2, /*textwidth=*/80);
150
151 puts("\nAvailable NCBI genetic code tables (for -c <id>):");
152 esl_gencode_DumpAltCodeTable(stdout);
153
154 exit(0);
155 }
156
157 if (esl_opt_ArgNumber(go) != 1)
158 {
159 printf("Incorrect number of command line arguments.\n");
160 esl_usage(stdout, argv[0], usage);
161 printf("\nTo see more help on available options, do %s -h\n\n", argv[0]);
162 exit(1);
163 }
164
165 seqfile = esl_opt_GetArg(go, 1);
166
167 if (esl_opt_IsOn(go, "--informat") &&
168 (informat = esl_sqio_EncodeFormat(esl_opt_GetString(go, "--informat"))) == eslMSAFILE_UNKNOWN)
169 esl_fatal("%s is not a valid input sequence file format for --informat", esl_opt_GetString(go, "--informat"));
170
171 status = esl_sqfile_OpenDigital(nt_abc, seqfile, informat, /*env=*/NULL, &sqfp);
172 if (status == eslENOTFOUND) esl_fatal("Failed to find (or open) sequence file %s", seqfile);
173 else if (status == eslEFORMAT) esl_fatal("Failed to recognize format of sequence file %s", seqfile);
174 else if (status != eslOK) esl_fatal("Failure in opening sequence file %s; code %d", seqfile, status);
175
176 /* A limitation. The esl_sqio_ReadWindow() interface needs to use SSI positioning
177 * to read reverse complement, and that doesn't work on nonrewindable streams.
178 */
179 if ( esl_opt_GetBoolean(go, "-W") && ! esl_sqfile_IsRewindable(sqfp) && ! esl_opt_GetBoolean(go, "--watson"))
180 esl_fatal("esl-translate can't read reverse complement from a nonrewindable stream (stdin pipe, .gz file, etc).");
181
182 /* Set up the genetic code. Default = NCBI 1, the standard code; allow ORFs to start at any aa
183 */
184 gcode = esl_gencode_Create(nt_abc, aa_abc);
185 esl_gencode_Set(gcode, esl_opt_GetInteger(go, "-c")); // default = 1, the standard genetic code
186
187 if (esl_opt_GetBoolean(go, "-m")) esl_gencode_SetInitiatorOnlyAUG(gcode);
188 else if (! esl_opt_GetBoolean(go, "-M")) esl_gencode_SetInitiatorAny(gcode); // note this is the default, if neither -m or -M are set
189
190
191 /* Set up the workstate structure, which contains both stateful
192 * info about our position in <sqfp> and the DNA <sq>, as well as
193 * one-time config info from options
194 */
195 wrk = esl_gencode_WorkstateCreate(go, gcode);
196
197
198 /* The two styles of main processing loop:
199 */
200 if (esl_opt_GetBoolean(go, "-W")) do_by_windows(gcode, wrk, sqfp);
201 else do_by_sequences(gcode, wrk, sqfp);
202
203
204 esl_gencode_WorkstateDestroy(wrk);
205 esl_sqfile_Close(sqfp);
206 esl_gencode_Destroy(gcode);
207 esl_alphabet_Destroy(aa_abc);
208 esl_alphabet_Destroy(nt_abc);
209 esl_getopts_Destroy(go);
210 return 0;
211 }
212
213
214
215