| 1 |
|
| 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 |
|
| 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; |
| 56 |
int contextsize = 2; |
| 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; } |
| 69 |
if (windowsize < 0) esl_sq_Reuse(sq); |
| 70 |
windowsize = -windowsize; |
| 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 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
if (sq->C == 0) |
| 82 |
{ |
| 83 |
if (sq->n < 3) continue; |
| 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 |
|
| 98 |
|
| 99 |
|
| 100 |
static ESL_OPTIONS options[] = { |
| 101 |
|
| 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 |
|
| 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, 0, 2, 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, 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 |
|
| 177 |
|
| 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 |
|
| 183 |
|
| 184 |
gcode = esl_gencode_Create(nt_abc, aa_abc); |
| 185 |
esl_gencode_Set(gcode, esl_opt_GetInteger(go, "-c")); |
| 186 |
|
| 187 |
if (esl_opt_GetBoolean(go, "-m")) esl_gencode_SetInitiatorOnlyAUG(gcode); |
| 188 |
else if (! esl_opt_GetBoolean(go, "-M")) esl_gencode_SetInitiatorAny(gcode); |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
wrk = esl_gencode_WorkstateCreate(go, gcode); |
| 196 |
|
| 197 |
|
| 198 |
|
| 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 |
|