Debian Codegraph @

hmmer/easel/esl_gencode.c

raw symbols SCIP
1 /* Genetic code tables for translation, whether canonical or noncanonical.
2 *
3 * Table of contents:
4 * 1. NCBI genetic code tables, in Easel digital form
5 * 2. ESL_GENCODE genetic code object
6 * 3. Reading and writing genetic codes in NCBI format
7 * 4. DNA->protein digital translation, allowing ambiguity chars
8 * 5. Functions for creating/destroying ESL_GENCODE_WORKSTATE
9 * 6. Functions for processing ORFs
10 * 7. Debugging/development utilities
11 * 8. Unit tests
12 * 9. Test driver
13 * 10. Examples
14 *
15 * To do:
16 * - Remove dependency on ESL_GETOPTS. Use a configuration params _CFG
17 * structure instead. (See `msaweight` for example).
18 * [xref SRE:2019/0415-easel-tech-tree-v3]
19 */
20 #include <esl_config.h>
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "easel.h"
27 #include "esl_alphabet.h"
28 #include "esl_fileparser.h"
29 #include "esl_getopts.h" // problematic. See TO DO note.
30 #include "esl_regexp.h"
31 #include "esl_sq.h"
32 #include "esl_sqio.h"
33
34 #include "esl_gencode.h"
35
36
37 /*****************************************************************
38 * 1. NCBI genetic code tables, in Easel digital form
39 *****************************************************************/
40
41 /*
42 * From: http://www.ncbi.nlm.nih.gov/Taxonomy/taxonomyhome.html/index.cgi?chapter=cgencodes
43 * NCBI text files are digitized by the esl_gencode_example driver:
44 * make esl_gencode_example
45 * ./esl_gencode_example <file>
46 *
47 * The NCBI page has useful information about these code tables, references and caveats.
48 */
49
50 static const ESL_GENCODE esl_transl_tables[] = {
51 { 1, "Standard",
52 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
53 { 8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 27, 19, 27, 19, 15, 15, 15, 15, 27, 1, 18, 1, 9, 4, 9, 4 },
54 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
55 /* K N K N T T T T R S R S I I M I Q H Q H P P P P R R R R L L L L E D E D A A A A G G G G V V V V * Y * Y S S S S * C W C L F L F */
56 NULL, NULL },
57
58 { 2, "Vertebrate mitochondrial",
59 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
60 { 8, 11, 8, 11, 16, 16, 16, 16, 27, 15, 27, 15, 10, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 27, 19, 27, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4 },
61 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
62 /* K N K N T T T T * S * S M I M I Q H Q H P P P P R R R R L L L L E D E D A A A A G G G G V V V V * Y * Y S S S S W C W C L F L F */
63 NULL, NULL },
64
65 { 3, "Yeast mitochondrial",
66 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
67 { 8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 10, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 16, 16, 16, 16, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 27, 19, 27, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4 },
68 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
69 /* K N K N T T T T R S R S M I M I Q H Q H P P P P R R R R T T T T E D E D A A A A G G G G V V V V * Y * Y S S S S W C W C L F L F */
70 NULL, NULL },
71
72 { 4, "Mold, protozoan, coelenterate mitochondrial; Mycoplasma/Spiroplasma",
73 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
74 { 8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 27, 19, 27, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4 },
75 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0 },
76 /* K N K N T T T T R S R S I I M I Q H Q H P P P P R R R R L L L L E D E D A A A A G G G G V V V V * Y * Y S S S S W C W C L F L F */
77 NULL, NULL },
78
79 { 5, "Invertebrate mitochondrial",
80 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
81 { 8, 11, 8, 11, 16, 16, 16, 16, 15, 15, 15, 15, 10, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 27, 19, 27, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4 },
82 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
83 /* K N K N T T T T S S S S M I M I Q H Q H P P P P R R R R L L L L E D E D A A A A G G G G V V V V * Y * Y S S S S W C W C L F L F */
84 NULL, NULL },
85
86 { 6, "Ciliate, dasycladacean, Hexamita nuclear",
87 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
88 { 8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 13, 19, 13, 19, 15, 15, 15, 15, 27, 1, 18, 1, 9, 4, 9, 4 },
89 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
90 /* K N K N T T T T R S R S I I M I Q H Q H P P P P R R R R L L L L E D E D A A A A G G G G V V V V Q Y Q Y S S S S * C W C L F L F */
91 NULL, NULL },
92
93 { 9, "Echinoderm and flatworm mitochondrial",
94 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
95 { 11, 11, 8, 11, 16, 16, 16, 16, 15, 15, 15, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 27, 19, 27, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4 },
96 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
97 /* N N K N T T T T S S S S I I M I Q H Q H P P P P R R R R L L L L E D E D A A A A G G G G V V V V * Y * Y S S S S W C W C L F L F */
98 NULL, NULL },
99
100 { 10, "Euplotid nuclear",
101 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
102 { 8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 27, 19, 27, 19, 15, 15, 15, 15, 1, 1, 18, 1, 9, 4, 9, 4 },
103 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
104 /* K N K N T T T T R S R S I I M I Q H Q H P P P P R R R R L L L L E D E D A A A A G G G G V V V V * Y * Y S S S S C C W C L F L F */
105 NULL, NULL },
106
107 { 11, "Bacterial, archaeal; and plant plastid",
108 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
109 { 8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 27, 19, 27, 19, 15, 15, 15, 15, 27, 1, 18, 1, 9, 4, 9, 4 },
110 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
111 /* K N K N T T T T R S R S I I M I Q H Q H P P P P R R R R L L L L E D E D A A A A G G G G V V V V * Y * Y S S S S * C W C L F L F */
112 NULL, NULL },
113
114 { 12, "Alternative yeast",
115 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
116 { 8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 15, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 27, 19, 27, 19, 15, 15, 15, 15, 27, 1, 18, 1, 9, 4, 9, 4 },
117 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
118 /* K N K N T T T T R S R S I I M I Q H Q H P P P P R R R R L L S L E D E D A A A A G G G G V V V V * Y * Y S S S S * C W C L F L F */
119 NULL, NULL },
120
121 { 13, "Ascidian mitochondrial",
122 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
123 { 8, 11, 8, 11, 16, 16, 16, 16, 5, 15, 5, 15, 10, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 27, 19, 27, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4 },
124 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
125 /* K N K N T T T T G S G S M I M I Q H Q H P P P P R R R R L L L L E D E D A A A A G G G G V V V V * Y * Y S S S S W C W C L F L F */
126 NULL, NULL },
127
128 { 14, "Alternative flatworm mitochondrial",
129 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
130 { 11, 11, 8, 11, 16, 16, 16, 16, 15, 15, 15, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 19, 19, 27, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4 },
131 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
132 /* N N K N T T T T S S S S I I M I Q H Q H P P P P R R R R L L L L E D E D A A A A G G G G V V V V Y Y * Y S S S S W C W C L F L F */
133 NULL, NULL },
134
135 { 16, "Chlorophycean mitochondrial",
136 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
137 { 8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 27, 19, 9, 19, 15, 15, 15, 15, 27, 1, 18, 1, 9, 4, 9, 4 },
138 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
139 /* K N K N T T T T R S R S I I M I Q H Q H P P P P R R R R L L L L E D E D A A A A G G G G V V V V * Y L Y S S S S * C W C L F L F */
140 NULL, NULL },
141
142 { 21, "Trematode mitochondrial",
143 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
144 { 11, 11, 8, 11, 16, 16, 16, 16, 15, 15, 15, 15, 10, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 27, 19, 27, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4 },
145 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
146 /* N N K N T T T T S S S S M I M I Q H Q H P P P P R R R R L L L L E D E D A A A A G G G G V V V V * Y * Y S S S S W C W C L F L F */
147 NULL, NULL },
148
149 { 22, "Scenedesmus obliquus mitochondrial",
150 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
151 { 8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 27, 19, 9, 19, 27, 15, 15, 15, 27, 1, 18, 1, 9, 4, 9, 4 },
152 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
153 /* K N K N T T T T R S R S I I M I Q H Q H P P P P R R R R L L L L E D E D A A A A G G G G V V V V * Y L Y * S S S * C W C L F L F */
154 NULL, NULL },
155
156 { 23, "Thraustochytrium mitochondrial",
157 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
158 { 8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 27, 19, 27, 19, 15, 15, 15, 15, 27, 1, 18, 1, 27, 4, 9, 4 },
159 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
160 /* K N K N T T T T R S R S I I M I Q H Q H P P P P R R R R L L L L E D E D A A A A G G G G V V V V * Y * Y S S S S * C W C * F L F */
161 NULL, NULL },
162
163 { 24, "Pterobranchia mitochondrial",
164 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
165 { 8, 11, 8, 11, 16, 16, 16, 16, 15, 15, 8, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 27, 19, 27, 19, 15, 15, 15, 15, 18, 1, 18, 1, 9, 4, 9, 4 },
166 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
167 /* K N K N T T T T S S K S I I M I Q H Q H P P P P R R R R L L L L E D E D A A A A G G G G V V V V * Y * Y S S S S W C W C L F L F */
168 NULL, NULL },
169
170 { 25, "Candidate Division SR1 and Gracilibacteria",
171 /* AAA AAC AAG AAT ACA ACC ACG ACT AGA AGC AGG AGT ATA ATC ATG ATT CAA CAC CAG CAT CCA CCC CCG CCT CGA CGC CGG CGT CTA CTC CTG CTT GAA GAC GAG GAT GCA GCC GCG GCT GGA GGC GGG GGT GTA GTC GTG GTT TAA TAC TAG TAT TCA TCC TCG TCT TGA TGC TGG TGT TTA TTC TTG TTT */
172 { 8, 11, 8, 11, 16, 16, 16, 16, 14, 15, 14, 15, 7, 7, 10, 7, 13, 6, 13, 6, 12, 12, 12, 12, 14, 14, 14, 14, 9, 9, 9, 9, 3, 2, 3, 2, 0, 0, 0, 0, 5, 5, 5, 5, 17, 17, 17, 17, 27, 19, 27, 19, 15, 15, 15, 15, 5, 1, 18, 1, 9, 4, 9, 4 },
173 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
174 /* K N K N T T T T R S R S I I M I Q H Q H P P P P R R R R L L L L E D E D A A A A G G G G V V V V * Y * Y S S S S G C W C L F L F */
175 NULL, NULL },
176 };
177
178
179 /*****************************************************************
180 * 2. The ESL_GENCODE genetic code object
181 *****************************************************************/
182
183 /* Function: esl_gencode_Create()
184 * Synopsis: Create a new genetic code object
185 *
186 * Purpose: Create a new genetic code object for translating DNA/RNA alphabet
187 * <nt_abc> to protein alphabet <aa_abc>, using the standard
188 * genetic code (NCBI transl_table 1).
189 *
190 * If you want a different code than transl_table 1, use
191 * <esl_gencode_Set()> to reset your <ESL_GENCODE> to a
192 * different code after you create it.
193 *
194 * Because the built-in genetic code tables have been
195 * pre-digitized with the standard Easel alphabets,
196 * <nt_abc> and <aa_abc> must generally also be standard
197 * Easel alphabets: <eslDNA> or <eslRNA> for <nt_abc>, and
198 * <eslAMINO> for <aa_abc>. The exception is if you're
199 * going to digitize NCBI data files for different Easel
200 * alphabets (for instance, if you're going to build a new,
201 * or your own version of the pre-digitized
202 * <esl_transl_tables[]>). As a special case, if either
203 * <nt_abc> or <aa_abc> are not standard Easel alphabets,
204 * the new <ESL_GENCODE> is left uninitialized, rather than
205 * setting it to transl_table 1.
206 *
207 * The <ESL_GENCODE> object keeps a copy of the two
208 * alphabet pointers. Caller is still responsible for their
209 * deallocation. They should not be deallocated until
210 * after the <ESL_GENCODE> object is.
211 *
212 * Returns: A pointer to the new object.
213 *
214 * Throws: <NULL> if allocation fails.
215 */
216 ESL_GENCODE *
217 esl_gencode_Create(const ESL_ALPHABET *nt_abc, const ESL_ALPHABET *aa_abc)
218 {
219 ESL_GENCODE *gcode = NULL;
220 int status;
221
222 ESL_ALLOC(gcode, sizeof(ESL_GENCODE));
223
224 gcode->nt_abc = nt_abc; // Keep a reference to the nucleic alphabet; caller remains responsible for it
225 gcode->aa_abc = aa_abc; // ditto for amino alphabet
226
227 if ( (nt_abc->type == eslDNA || nt_abc->type == eslRNA) && aa_abc->type == eslAMINO)
228 esl_gencode_Set(gcode, 1); // Default = standard code (NCBI trans table 1)
229 return gcode;
230
231 ERROR:
232 esl_gencode_Destroy(gcode);
233 return NULL;
234 }
235
236
237 /* Function: esl_gencode_Destroy()
238 * Synopsis: Deallocate an <ESL_GENCODE>
239 */
240 void
241 esl_gencode_Destroy(ESL_GENCODE *gcode)
242 {
243 if (gcode) free(gcode);
244 }
245
246
247
248 /* Function: esl_gencode_Set()
249 * Synopsis: Set one of the NCBI standard genetic codes
250 *
251 * Purpose: Set <gcode> to use one of the standard NCBI genetic code tables,
252 * using the NCBI identifier <ncbi_transl_table>.
253 *
254 * <ncbi_transl_table> is an integer from 1..25 (not all of
255 * which are valid). For example, 1 is the standard code,
256 * and 6 is the ciliate nuclear code.
257 *
258 * The alphabets in <gcode> must be standard Easel
259 * alphabets: <eslAMINO> for <aa_abc> and either <eslDNA>
260 * or <eslRNA> for <nt_abc>. This is because <_Set()>
261 * simply copies precomputed digitized data for the
262 * appropriate genetic code, and that precomputation is
263 * done with the standard Easel digital alphabets. If the
264 * <aa_abc> and <nt_abc> alphabet reference ptrs in <gcode>
265 * are set (and this is recommended, but not necessary)
266 * they're used to verify that the alphabets are Easel
267 * standard ones.
268 *
269 * Returns: <eslOK> on success.
270 * <eslENOTFOUND> if the <ncbi_transl_table> code is not
271 * in our available table of genetic codes.
272 *
273 * Throws: <eslEINVAL> if either of the alphabets in <gcode> are
274 * nonstandard.
275 */
276 int
277 esl_gencode_Set(ESL_GENCODE *gcode, int ncbi_transl_table)
278 {
279 int ntables = sizeof(esl_transl_tables) / sizeof(ESL_GENCODE);
280 int t, c;
281
282 if (gcode->nt_abc && (gcode->nt_abc->type != eslDNA && gcode->nt_abc->type != eslRNA))
283 ESL_EXCEPTION(eslEINVAL, "NCBI translation tables are precomputed using Easel standard alphabets; your nucleic alphabet is nonstandard");
284 if (gcode->aa_abc && gcode->aa_abc->type != eslAMINO)
285 ESL_EXCEPTION(eslEINVAL, "NCBI translation tables are precomputed using Easel standard alphabets; your amino alphabet is nonstandard");
286
287 for (t = 0; t < ntables; t++)
288 if ( esl_transl_tables[t].transl_table == ncbi_transl_table) break;
289 if (t == ntables) return eslENOTFOUND;
290
291 gcode->transl_table = esl_transl_tables[t].transl_table;
292 strcpy(gcode->desc, esl_transl_tables[t].desc);
293 for (c = 0; c < 64; c++)
294 {
295 gcode->basic[c] = esl_transl_tables[t].basic[c];
296 gcode->is_initiator[c] = esl_transl_tables[t].is_initiator[c];
297 }
298 return eslOK;
299 }
300
301
302 /* Function: esl_gencode_SetInitiatorAny()
303 * Synopsis: Set initiator field so ORFs can start with any aa
304 *
305 * Purpose: Set <gcode> to allow ORFs to start with any amino acid, as
306 * opposed to looking for initiation codons.
307 *
308 * We do this by overwriting the <is_initiator> field to be
309 * TRUE for all codons except terminators. Because we
310 * overwrite, the only way to revert a genetic code to use
311 * its official set of initiators is to reinitialize it
312 * completely.
313 *
314 * Returns: <eslOK> on success.
315 */
316 int
317 esl_gencode_SetInitiatorAny(ESL_GENCODE *gcode)
318 {
319 int c;
320 for (c = 0; c < 64; c++)
321 gcode->is_initiator[c] = (esl_abc_XIsCanonical(gcode->aa_abc, gcode->basic[c]) ? TRUE : FALSE);
322 return eslOK;
323 }
324
325
326 /* Function: esl_gencode_SetInitiatorOnlyAUG
327 * Synopsis: Set initiator field so ORFs must start with AUG
328 *
329 * Purpose: Set <gcode> so that ORFs can only start with AUG, as opposed
330 * to using the possibly larger set of plausible initiator codons
331 * associated with the standard NCBI genetic codes. (For example,
332 * the standard code 1 allows ATG, CTG, and UUG initiators.)
333 *
334 * We do this by overwriting the <is_initiator> field to be TRUE
335 * only for the ATG codon.
336 *
337 * Returns: <eslOK> on success.
338 */
339 int
340 esl_gencode_SetInitiatorOnlyAUG(ESL_GENCODE *gcode)
341 {
342 int c;
343 int atgcodon = 16 * esl_abc_DigitizeSymbol(gcode->nt_abc, 'A') +
344 4 * esl_abc_DigitizeSymbol(gcode->nt_abc, 'T') +
345 esl_abc_DigitizeSymbol(gcode->nt_abc, 'G');
346
347 for (c = 0; c < 64; c++) gcode->is_initiator[c] = FALSE;
348 gcode->is_initiator[atgcodon] = TRUE;
349 return eslOK;
350 }
351
352
353
354 /*****************************************************************
355 * 3. Reading and writing genetic codes in NCBI format
356 *****************************************************************/
357
358 /* Function: esl_gencode_Read()
359 * Synopsis: Read a genetic code in NCBI text format from a stream.
360 *
361 * Purpose: Read an NCBI genetic code text file from <efp>; parse it
362 * and convert to Easel digitized data using the nucleic
363 * acid alphabet <nt_abc> and the protein alphabet <aa_abc>;
364 * return a pointer to the newly created <ESL_GENCODE> object
365 * via <*ret_gcode>.
366 *
367 * Example of an NCBI genetic code datafile:
368 *
369 * AAs = FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG
370 * Starts = ---M---------------M---------------M----------------------------
371 * Base1 = TTTTTTTTTTTTTTTTCCCCCCCCCCCCCCCCAAAAAAAAAAAAAAAAGGGGGGGGGGGGGGGG
372 * Base2 = TTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGG
373 * Base3 = TCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAG
374 *
375 * Caller is responsible for opening the <efp> first. This
376 * allows caller to take input from files, streams, or even
377 * to have data embedded as a piece of a larger file format
378 * it's parsing.
379 *
380 * The <efp> is configured so that lines beginning with '#'
381 * are ignored as comments, and upon return, the <efp> remains
382 * configured this way.
383 *
384 * This function is and must remain independent of the
385 * order of residues in the amino and nucleic
386 * alphabets. This allows us to convert NCBI genetic code
387 * text files to digitized Easel translation tables even
388 * for other orders of the symbols in DNA/protein digital
389 * alphabets, including the case of us someday changing the
390 * order of the Easel standard alphabet(s). Once digitized,
391 * Easel encodings of the genetic code are dependent on the
392 * <eslAMINO> and <eslNUCLEIC> alphabets they were created
393 * with.
394 *
395 * Slightly confusing case: if we *did* change the order in
396 * the Easel standard alphabets, the esl_gencode module has
397 * no way to know that it changed. All it sees is the
398 * <eslDNA>, <eslRNA>, or <eslAMINO> <type>. <ESL_GENCODE>
399 * data will be corrupted, and unit testing of
400 * <esl_gencode> will fail, until the <esl_transl_tables[]>
401 * data are rebuilt for the new alphabets using the
402 * <esl_gencode_example> program.
403 *
404 * Returns: <eslOK> on success. <*ret_gcode> contains the new <ESL_GENCODE>.
405 * <efp> has been set to ignore lines beginning with '#'.
406 *
407 * On a parse error, returns <eslEFORMAT>, and an informative message is
408 * left in <efp->errbuf>. Now <*ret_gcode> is NULL, but <efp> has
409 * still been configured to ignore lines beginning with '#'.
410 */
411 int
412 esl_gencode_Read(ESL_FILEPARSER *efp, const ESL_ALPHABET *nt_abc, const ESL_ALPHABET *aa_abc, ESL_GENCODE **ret_gcode)
413 {
414 ESL_GENCODE *gcode = esl_gencode_Create(nt_abc, aa_abc);
415 ESL_REGEXP *mach = esl_regexp_Create();
416 int start, end, s, e;
417 char aas[65];
418 char mline[65];
419 char base1[65];
420 char base2[65];
421 char base3[65];
422 int aa_seen[20];
423 int stop_seen;
424 int codon_seen[64];
425 int x, codon, pos;
426 int status;
427
428 ESL_DASSERT1(( nt_abc->K == 4 )); // We're going to hardcode ncodons = 64, so "trust but verify"
429 ESL_DASSERT1(( aa_abc->K == 20 ));
430
431 if (( status = esl_fileparser_SetCommentChar(efp, '#') != eslOK)) goto ERROR;
432
433 if ((status = esl_fileparser_NextLine(efp)) != eslOK) { if (status == eslEOF) ESL_XFAIL(eslEFORMAT, efp->errbuf, "File empty or truncated? No AAs line found"); else goto ERROR; }
434 if ((status = esl_regexp_Match(mach, "^\\s*[Aa][Aa]s\\s*=\\s*(\\S+)\\s*$", efp->buf)) != eslOK) { if (status == eslEOD) ESL_XFAIL(eslEFORMAT, efp->errbuf, "First data line doesn't start with 'AAs ='"); else goto ERROR; }
435 if ((status = esl_regexp_SubmatchCoords(mach, efp->buf, 1, &start, &end)) != eslOK) goto ERROR;
436 if (end - start + 1 != 64) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Expected 64 char of AAs data");
437 strncpy(aas, efp->buf+start, 64);
438 aas[64] = '\0';
439
440 if ((status = esl_fileparser_NextLine(efp)) != eslOK) { if (status == eslEOF) ESL_XFAIL(eslEFORMAT, efp->errbuf, "File empty or truncated? No Starts line found"); else goto ERROR; }
441 if ((status = esl_regexp_Match(mach, "^\\s*[Ss]tarts\\s*=\\s*(\\S+)\\s*$", efp->buf)) != eslOK) { if (status == eslEOD) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Second data line doesn't start with 'Starts ='"); else goto ERROR; }
442 if ((status = esl_regexp_SubmatchCoords(mach, efp->buf, 1, &s, &e)) != eslOK) goto ERROR;
443 if (e - s + 1 != 64) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Expected 64 char of Starts data");
444 if (s != start) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Starts data is not aligned with AAs data above it");
445 strncpy(mline, efp->buf+start, 64);
446 mline[64] = '\0';
447
448 if ((status = esl_fileparser_NextLine(efp)) != eslOK) { if (status == eslEOF) ESL_XFAIL(eslEFORMAT, efp->errbuf, "File empty or truncated? No Base1 line found"); else goto ERROR; }
449 if ((status = esl_regexp_Match(mach, "^\\s*[Bb]ase1\\s*=\\s*(\\S+)\\s*$", efp->buf)) != eslOK) { if (status == eslEOD) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Third data line doesn't start with 'Base1 ='"); else goto ERROR; }
450 if ((status = esl_regexp_SubmatchCoords(mach, efp->buf, 1, &s, &e)) != eslOK) goto ERROR;
451 if (e - s + 1 != 64) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Expected 64 char of Base1 data");
452 if (s != start) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Base1 data is not aligned with data above it");
453 strncpy(base1, efp->buf+start, 64);
454 base1[64] = '\0';
455
456 if ((status = esl_fileparser_NextLine(efp)) != eslOK) { if (status == eslEOF) ESL_XFAIL(eslEFORMAT, efp->errbuf, "File empty or truncated? No Base2 line found"); else goto ERROR; }
457 if ((status = esl_regexp_Match(mach, "^\\s*[Bb]ase2\\s*=\\s*(\\S+)\\s*$", efp->buf)) != eslOK) { if (status == eslEOD) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Fourth data line doesn't start with 'Base2 ='"); else goto ERROR; }
458 if ((status = esl_regexp_SubmatchCoords(mach, efp->buf, 1, &s, &e)) != eslOK) goto ERROR;
459 if (e - s + 1 != 64) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Expected 64 char of Base2 data");
460 if (s != start) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Base2 data is not aligned with data above it");
461 strncpy(base2, efp->buf+start, 64);
462 base2[64] = '\0';
463
464 if ((status = esl_fileparser_NextLine(efp)) != eslOK) { if (status == eslEOF) ESL_XFAIL(eslEFORMAT, efp->errbuf, "File empty or truncated? No Base3 line found"); else goto ERROR; }
465 if ((status = esl_regexp_Match(mach, "^\\s*[Bb]ase3\\s*=\\s*(\\S+)\\s*$", efp->buf)) != eslOK) { if (status == eslEOD) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Fifth data line doesn't start with 'Base3 ='"); else goto ERROR; }
466 if ((status = esl_regexp_SubmatchCoords(mach, efp->buf, 1, &s, &e)) != eslOK) goto ERROR;
467 if (e - s + 1 != 64) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Expected 64 char of Base3 data");
468 if (s != start) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Base3 data is not aligned with data above it");
469 strncpy(base3, efp->buf+start, 64);
470 base3[64] = '\0';
471
472 stop_seen = FALSE;
473 for ( x = 0; x < 20; x++) aa_seen[x] = FALSE;
474 for (codon = 0; codon < 64; codon++) codon_seen[codon] = FALSE;
475
476 for (pos = 0; pos < 64; pos++)
477 {
478 if (! esl_abc_CIsValid(aa_abc, aas[pos]) || ! (esl_abc_CIsCanonical(aa_abc, aas[pos]) || esl_abc_CIsNonresidue(aa_abc, aas[pos]))) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Character %c on AAs line is not an amino acid or a * (stop)", aas[pos]);
479 if (! esl_abc_CIsValid(nt_abc, base1[pos]) || ! esl_abc_CIsCanonical(nt_abc, base1[pos])) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Character %c on Base1 line is not a nucleotide", base1[pos]);
480 if (! esl_abc_CIsValid(nt_abc, base2[pos]) || ! esl_abc_CIsCanonical(nt_abc, base2[pos])) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Character %c on Base2 line is not a nucleotide", base2[pos]);
481 if (! esl_abc_CIsValid(nt_abc, base3[pos]) || ! esl_abc_CIsCanonical(nt_abc, base3[pos])) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Character %c on Base3 line is not a nucleotide", base3[pos]);
482 if ( mline[pos] != '-' && mline[pos] != 'm' && mline[pos] != 'M') ESL_XFAIL(eslEFORMAT, efp->errbuf, "Character %c on Starts line is neither a - or an M", mline[pos]);
483
484 codon = 16 * esl_abc_DigitizeSymbol(nt_abc, base1[pos]) +
485 4 * esl_abc_DigitizeSymbol(nt_abc, base2[pos]) +
486 esl_abc_DigitizeSymbol(nt_abc, base3[pos]);
487 x = esl_abc_DigitizeSymbol(aa_abc, aas[pos]);
488
489 ESL_DASSERT1(( codon >= 0 && codon < 64 ));
490 ESL_DASSERT1(( x >= 0 && (x < 20 || x == esl_abc_XGetNonresidue(aa_abc))));
491
492 if (x < 20) aa_seen[x]++; else stop_seen++;
493 codon_seen[codon]++;
494
495 gcode->basic[codon] = x;
496 gcode->is_initiator[codon] = ( mline[pos] == '-' ? FALSE : TRUE ); // We already checked above that it's one of "-mM"
497 }
498
499 /* A genetic code must provide a translation for all 64 codons, and
500 * all 20 amino acids to be encoded. (No organism is yet known to
501 * encode fewer than 20 amino acids [Kawahara-Kobayashi et al, NAR
502 * 40:10576, 2012].) The code must include at least one stop codon.
503 */
504 if (! stop_seen) ESL_XFAIL(eslEFORMAT, efp->errbuf, "No stop codon found in that genetic code");
505 for (codon = 0; codon < 64; codon++)
506 if (! codon_seen[codon]) ESL_XFAIL(eslEFORMAT, efp->errbuf, "Data for fewer than 64 codons was found");
507 for (x = 0; x < 20; x++)
508 if (aa_seen[x] == 0) ESL_XFAIL(eslEFORMAT, efp->errbuf, "No codon for residue %c found", aa_abc->sym[x]);
509
510
511 esl_regexp_Destroy(mach);
512 gcode->transl_table = -1; // It was initialized to 1, the NCBI standard table; reset
513 gcode->desc[0] = '\0'; // Was initialized to desc of NCBI table 1; blank it
514 gcode->nt_abc = nt_abc;
515 gcode->aa_abc = aa_abc;
516 *ret_gcode = gcode;
517 return eslOK;
518
519 ERROR:
520 if (gcode) esl_gencode_Destroy(gcode);
521 if (mach) esl_regexp_Destroy(mach);
522 *ret_gcode = NULL;
523 return status;
524 }
525
526
527 /* Function: esl_gencode_Write()
528 * Synopsis: Write a genetic code to a stream, in NCBI format
529 *
530 * Purpose: Write the genetic code <gcode> to stream <ofp> in NCBI format.
531 *
532 * If <add_comment> is TRUE and if it's a standard NCBI genetic code
533 * (i.e. with an NCBI transl_table number), also add a comment
534 * line at the top to document which transl_table it is, and the
535 * description line. This is an Easel extension. Other programs
536 * that read NCBI genetic code files will probably not be able to
537 * parse the Easel comment line, and for such programs you'll want
538 * <add_comment> to be FALSE.
539 *
540 * Returns: <eslOK> on success.
541 *
542 * Throws: <eslEWRITE> on a write failure, such as a disk running out of space.
543 */
544 int
545 esl_gencode_Write(FILE *ofp, const ESL_GENCODE *gcode, int add_comment)
546 {
547 char order[] = "TCAG";
548 int x,c;
549
550 if (add_comment && gcode->transl_table > 0)
551 if ( fprintf(ofp, "# %d %s\n",
552 gcode->transl_table, gcode->desc) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "genetic code write failed");
553
554
555 if ( fprintf(ofp, " AAs = ") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "genetic code write failed");
556 for (x = 0; x < 64; x++) {
557 c = 16 * esl_abc_DigitizeSymbol(gcode->nt_abc, order[ x/16 ])
558 + 4 * esl_abc_DigitizeSymbol(gcode->nt_abc, order[ (x%16)/4 ])
559 + esl_abc_DigitizeSymbol(gcode->nt_abc, order[ x%4]);
560 if (fputc( gcode->aa_abc->sym[gcode->basic[c]], ofp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "genetic code write failed");
561 }
562 if ( fputc('\n', ofp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "genetic code write failed");
563
564 if ( fprintf(ofp, " Starts = ") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "genetic code write failed");
565 for (x = 0; x < 64; x++) {
566 c = 16 * esl_abc_DigitizeSymbol(gcode->nt_abc, order[ x/16 ])
567 + 4 * esl_abc_DigitizeSymbol(gcode->nt_abc, order[ (x%16)/4 ])
568 + esl_abc_DigitizeSymbol(gcode->nt_abc, order[ x%4]);
569 if (fputc( (gcode->is_initiator[c] ? 'M' : '-'), ofp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "genetic code write failed");
570 }
571 if ( fputc('\n', ofp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "genetic code write failed");
572
573 if ( fprintf(ofp, " Base1 = ") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "genetic code write failed");
574 for (x = 0; x < 64; x++) if ( fputc( order[ x/16 ], ofp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "genetic code write failed");
575 if ( fputc('\n', ofp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "genetic code write failed");
576
577 if ( fprintf(ofp, " Base2 = ") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "genetic code write failed");
578 for (x = 0; x < 64; x++) if ( fputc( order[ (x%16)/4 ], ofp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "genetic code write failed");
579 if ( fputc('\n', ofp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "genetic code write failed");
580
581 if ( fprintf(ofp, " Base3 = ") < 0) ESL_EXCEPTION_SYS(eslEWRITE, "genetic code write failed");
582 for (x = 0; x < 64; x++) if ( fputc( order[ x%4 ], ofp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "genetic code write failed");
583 if ( fputc('\n', ofp) < 0) ESL_EXCEPTION_SYS(eslEWRITE, "genetic code write failed");
584
585 return eslOK;
586 }
587
588
589
590
591 /*****************************************************************
592 * 4. DNA->protein digital translation, allowing ambiguity chars
593 *****************************************************************/
594
595
596 /* Function: esl_gencode_GetTranslation()
597 * Synopsis: Returns translation of a degenerate digital codon.
598 *
599 * Purpose: Translate the digital DNA/RNA codon sequence starting at
600 * pointer <dsqp> and return the digital amino acid code.
601 *
602 * <dsqp> is a pointer into a digital sequence,
603 * not a complete digital sequence, so there are no sentinels.
604 * Also, caller must be sure that a full codon dsqp[0..2] exists
605 * at this location.
606 *
607 * Ambiguity codes are allowed in the DNA/RNA codon. If
608 * the amino acid is unambiguous, despite codon ambiguity,
609 * the correct amino acid is still determined: for example,
610 * GGR translates as Gly, UUY as Phe, AUH as Ile. If
611 * there is no single unambiguous amino acid translation, the codon
612 * is translated as X (unknown).
613 *
614 * Other than X, no amino acid ambiguity code is
615 * returned. We do not, for example, decode SAR as Z (Q|E),
616 * MUH as J (I|L), or RAY as B (N|D), because the extra
617 * complexity needed to do this doesn't seem worthwhile.
618 *
619 * Returns: digital amino acid code (0..19 or esl_abc_XGetUnknown()) in
620 * the protein alphabet.
621 *
622 * Throws: (no abnormal error conditions)
623 */
624 int
625 esl_gencode_GetTranslation(const ESL_GENCODE *gcode, ESL_DSQ *dsqp)
626 {
627 ESL_DSQ x, y, z;
628 int codon;
629 int aa = -1;
630
631 if (esl_abc_XIsCanonical(gcode->nt_abc, dsqp[0]) && esl_abc_XIsCanonical(gcode->nt_abc, dsqp[1]) && esl_abc_XIsCanonical(gcode->nt_abc, dsqp[2]))
632 {
633 codon = 16*dsqp[0] + 4*dsqp[1] + dsqp[2];
634 return gcode->basic[codon];
635 }
636
637 for (x = 0; x < 4; x++)
638 {
639 if (! gcode->nt_abc->degen[dsqp[0]][x]) continue;
640 for (y = 0; y < 4; y++)
641 {
642 if (! gcode->nt_abc->degen[dsqp[1]][y]) continue;
643 for (z = 0; z < 4; z++)
644 {
645 if (! gcode->nt_abc->degen[dsqp[2]][z]) continue;
646 /* xyz is one possible basic codon included in the dsqp[3] degeneracy */
647 codon = x * 16 + y * 4 + z;
648 if (aa == -1) aa = gcode->basic[codon];
649 else if (aa != gcode->basic[codon]) return esl_abc_XGetUnknown(gcode->aa_abc);
650 }
651 }
652 }
653 return aa;
654 }
655
656 /* Function: esl_gencode_IsInitiator()
657 * Synopsis: Returns TRUE if degenerate codon is an initiator
658 *
659 * Purpose: Determine if all possible codons consistent with the
660 * degenerate codon sequence starting at <dsqp> are
661 * all initiation codons; return TRUE if so, else FALSE.
662 *
663 * For example, the standard code allows AUG|CUG|UUG
664 * initiators. Given HUG, MUG, or YUG, we would return
665 * TRUE.
666 *
667 * Because stop codons never have the <is_initiator> flag,
668 * even if we used <esl_gencode_SetAnyInitiator()>, NNN
669 * will never be used to initiate an open reading frame,
670 * nor will other degenerate codons that are consistent
671 * with at least one stop. This is desirable: we don't want
672 * to call all-X ORFs across long stretches of N's that
673 * are prevalent in DNA sequence assemblies.
674 *
675 * Works fine on nondegenerate codons too, but if caller
676 * knows the codon is nondegenerate, it should simply
677 * test <gcode->is_initiator[0..63]> directly.
678 *
679 * <dsqp> is a pointer into a digital sequence, not
680 * a digital sequence itself, so there are no sentinels:
681 * the codon is dsqp[0..2]. Moreover, caller must be
682 * sure that a full codon exists at this location;
683 * don't call this function at dsq[L-1] or dsq[L].
684 *
685 * Returns: TRUE|FALSE
686 */
687 int
688 esl_gencode_IsInitiator(const ESL_GENCODE *gcode, ESL_DSQ *dsqp)
689 {
690 ESL_DSQ x, y, z;
691 int codon;
692 int ncodons = 0;
693
694 /* Handle the canonical case (no degeneracies) even though it's
695 * wasteful to call esl_gencode_IsInitiator() if there's no
696 * degeneracies.
697 */
698 if (esl_abc_XIsCanonical(gcode->nt_abc, dsqp[0]) && esl_abc_XIsCanonical(gcode->nt_abc, dsqp[1]) && esl_abc_XIsCanonical(gcode->nt_abc, dsqp[2]))
699 {
700 codon = 16*dsqp[0] + 4*dsqp[1] + dsqp[2];
701 return gcode->is_initiator[codon];
702 }
703
704 /* Main case: if there's degeneracies then all possible
705 * codons must be initiators to call the ambig codon an initiator.
706 */
707 for (x = 0; x < 4; x++)
708 {
709 if (! gcode->nt_abc->degen[dsqp[0]][x]) continue;
710 for (y = 0; y < 4; y++)
711 {
712 if (! gcode->nt_abc->degen[dsqp[1]][y]) continue;
713 for (z = 0; z < 4; z++)
714 {
715 if (! gcode->nt_abc->degen[dsqp[2]][z]) continue;
716 /* xyz is one possible basic codon included in the dsqp[3] degeneracy */
717 codon = x * 16 + y * 4 + z;
718 ncodons++;
719 if (! gcode->is_initiator[codon]) return FALSE;
720 }
721 }
722 }
723
724 /* I can't imagine a degeneracy that doesn't correspond to at least one codon,
725 * but it creeps me out to leave the door open to this returning TRUE if it
726 * hasn't seen any. Hence, <ncodons> test.
727 */
728 return (ncodons ? TRUE : FALSE);
729 }
730
731
732 /*****************************************************************
733 * 5. Functions for creating/destroying ESL_GENCODE_WORKSTATE
734 *****************************************************************/
735 void
736 esl_gencode_WorkstateDestroy(ESL_GENCODE_WORKSTATE *wrk)
737 {
738 int f;
739 if (wrk)
740 {
741 for (f = 0; f < 3; f++) esl_sq_Destroy(wrk->psq[f]);
742
743 if(wrk->orf_block != NULL)
744 {
745 esl_sq_DestroyBlock(wrk->orf_block);
746 wrk->orf_block = NULL;
747 }
748
749 free(wrk);
750 }
751 }
752
753 ESL_GENCODE_WORKSTATE *
754 esl_gencode_WorkstateCreate(ESL_GETOPTS *go, ESL_GENCODE *gcode)
755 {
756 ESL_GENCODE_WORKSTATE *wrk = NULL;
757 int f;
758 int status;
759
760 ESL_ALLOC(wrk, sizeof(ESL_GENCODE_WORKSTATE));
761 for (f = 0; f < 3; f++) wrk->psq[f] = NULL;
762
763 for (f = 0; f < 3; f++)
764 {
765 wrk->psq[f] = esl_sq_CreateDigital(gcode->aa_abc);
766 wrk->psq[f]->dsq[0] = eslDSQ_SENTINEL;
767 wrk->in_orf[f] = FALSE;
768 }
769
770 wrk->apos = 1;
771 wrk->frame = 0;
772 wrk->codon = 0;
773 wrk->inval = 0;
774 wrk->is_revcomp = FALSE;
775 wrk->orfcount = 0;
776
777 wrk->orf_block = NULL;
778
779 wrk->do_watson = (esl_opt_GetBoolean(go, "--crick") ? FALSE : TRUE);
780 wrk->do_crick = (esl_opt_GetBoolean(go, "--watson") ? FALSE : TRUE);
781 wrk->using_initiators = ((esl_opt_GetBoolean(go, "-m") || esl_opt_GetBoolean(go, "-M")) ? TRUE : FALSE);
782 wrk->minlen = esl_opt_GetInteger(go, "-l");
783 wrk->outfp = stdout;
784 wrk->outformat = eslSQFILE_FASTA;
785
786 return wrk;
787
788 ERROR:
789 esl_gencode_WorkstateDestroy(wrk);
790 return NULL;
791 }
792
793 /*****************************************************************
794 * 6. Functions for processing ORFs
795 *****************************************************************/
796
797 int
798 esl_gencode_ProcessOrf(ESL_GENCODE_WORKSTATE *wrk, ESL_SQ *sq)
799 {
800
801 int status = eslOK;
802 ESL_SQ *psq = wrk->psq[wrk->frame];
803 psq->end = (wrk->is_revcomp ? wrk->apos+1 : wrk->apos-1);
804 if (wrk->in_orf[wrk->frame] && psq->n >= wrk->minlen)
805 {
806 wrk->orfcount++;
807 if (psq->n+2 > psq->salloc)
808 esl_sq_Grow(psq, /*opt_nsafe=*/NULL);
809 psq->dsq[1+psq->n] = eslDSQ_SENTINEL;
810
811 esl_sq_FormatName(psq, "orf%d", wrk->orfcount);
812 esl_sq_FormatDesc(psq, "source=%s coords=%" PRId64 "..%" PRId64 " length=%" PRId64 " frame=%d desc=%s", psq->source, psq->start, psq->end, psq->n, wrk->frame + 1 + (wrk->is_revcomp ? 3 : 0), sq->desc);
813 /* if we do not have a block to write ORFs to then write ORFs to file */
814 if (wrk->orf_block == NULL)
815 {
816 esl_sqio_Write(wrk->outfp, psq, wrk->outformat, /*sq ssi offset update=*/FALSE);
817 }
818 else
819 {
820 if (wrk->orf_block->count == wrk->orf_block->listSize)
821 {
822 status = esl_sq_BlockGrowTo(wrk->orf_block, wrk->orf_block->listSize + 128, TRUE, psq->abc);
823 if (status != eslOK) ESL_XEXCEPTION(eslEMEM, "Cannot increase size of ORF sequence block");
824 }
825 //printf("adding seq to block list num %d\n",wrk->orf_block->count);
826 //esl_sqio_Write(stdout, psq, eslSQFILE_FASTA, 0);
827 //printf("\n");
828 esl_sq_Copy(psq, &(wrk->orf_block->list[wrk->orf_block->count]));
829 //printf("incrementing block count to %d\n",wrk->orf_block->count+1);
830
831 wrk->orf_block->count++;
832 }
833 }
834
835 esl_sq_Reuse(psq);
836 esl_sq_SetSource(psq, sq->name);
837 wrk->in_orf[wrk->frame] = FALSE;
838
839 ERROR:
840 return status;
841 }
842
843 void
844 esl_gencode_ProcessStart(ESL_GENCODE *gcode, ESL_GENCODE_WORKSTATE *wrk, ESL_SQ *sq)
845 {
846 int f;
847
848 ESL_DASSERT1(( sq->n >= 3 ));
849
850 for (f = 0; f < 3; f++)
851 {
852 esl_sq_SetSource(wrk->psq[f], sq->name);
853 wrk->in_orf[f] = FALSE;
854 }
855 wrk->frame = 0;
856 wrk->codon = 0;
857 wrk->inval = 0;
858 wrk->is_revcomp = (sq->end > sq->start ? FALSE : TRUE ); // this test fails for seqs of length 1, but we know that L>=3
859 wrk->apos = (wrk->is_revcomp ? sq->L : 1 );
860
861 if (esl_abc_XIsCanonical(gcode->nt_abc, sq->dsq[1])) wrk->codon += 4 * sq->dsq[1]; else wrk->inval = 1;
862 if (esl_abc_XIsCanonical(gcode->nt_abc, sq->dsq[2])) wrk->codon += sq->dsq[2]; else wrk->inval = 2;
863 }
864
865
866 int
867 esl_gencode_ProcessPiece(ESL_GENCODE *gcode, ESL_GENCODE_WORKSTATE *wrk, ESL_SQ *sq)
868 {
869 ESL_DSQ aa;
870 int rpos;
871
872 for (rpos = 1; rpos <= sq->n-2; rpos++)
873 {
874 wrk->codon = (wrk->codon * 4) % 64;
875 if ( esl_abc_XIsCanonical(gcode->nt_abc, sq->dsq[rpos+2])) wrk->codon += sq->dsq[rpos+2];
876 else wrk->inval = 3;
877
878 /* Translate the current codon starting at <pos>;
879 * see if it's an acceptable initiator
880 */
881 if (wrk->inval > 0) // degenerate codon: needs special, tedious handling
882 {
883 aa = esl_gencode_GetTranslation(gcode, sq->dsq+rpos); // This function can deal with any degeneracy
884 if (! wrk->in_orf[wrk->frame] && esl_gencode_IsInitiator(gcode, sq->dsq+rpos)) // ...as can IsInitiator.
885 {
886 if (wrk->using_initiators) // If we're using initiation codons, initial codon translates to M even if it's something like UUG or CUG
887 aa = esl_abc_DigitizeSymbol(gcode->aa_abc, 'M');
888 wrk->in_orf[wrk->frame] = TRUE;
889 wrk->psq[wrk->frame]->start = wrk->apos;
890 }
891 wrk->inval--;
892 }
893 else
894 {
895 aa = gcode->basic[wrk->codon]; // If we know the digitized codon has no degeneracy, translation is a simple lookup
896 if (gcode->is_initiator[wrk->codon] && ! wrk->in_orf[wrk->frame])
897 {
898 if (wrk->using_initiators) // If we're using initiation codons, initial codon translates to M even if it's something like UUG or CUG
899 aa = esl_abc_DigitizeSymbol(gcode->aa_abc, 'M');
900 wrk->psq[wrk->frame]->start = wrk->apos;
901 wrk->in_orf[wrk->frame] = TRUE;
902 }
903 }
904
905 /* Stop codon: deal with this ORF sequence and reinitiate */
906 if ( esl_abc_XIsNonresidue(gcode->aa_abc, aa))
907 esl_gencode_ProcessOrf(wrk, sq);
908
909 /* Otherwise: we have a residue. If we're in an orf (if we've
910 * seen a suitable initiator), add this residue, reallocating as needed.
911 */
912 if (wrk->in_orf[wrk->frame])
913 {
914 if (wrk->psq[wrk->frame]->n + 2 > wrk->psq[wrk->frame]->salloc)
915 esl_sq_Grow(wrk->psq[wrk->frame], /*opt_nsafe=*/NULL);
916 wrk->psq[wrk->frame]->dsq[1+ wrk->psq[wrk->frame]->n] = aa;
917 wrk->psq[wrk->frame]->n++;
918 }
919
920 /* Advance +1 */
921 if (wrk->is_revcomp) wrk->apos--; else wrk->apos++;
922 wrk->frame = (wrk->frame + 1) % 3;
923 }
924 return eslOK;
925 }
926
927
928 int
929 esl_gencode_ProcessEnd(ESL_GENCODE_WORKSTATE *wrk, ESL_SQ *sq)
930 {
931 int f;
932
933 /* Done with the sequence. Now terminate all the orfs we were working on.
934 * <apos> is sitting at L-1 (or 2, if revcomp) and we're in some <frame>
935 * there.
936 */
937 ESL_DASSERT1(( (wrk->is_revcomp && wrk->apos == 2) || (! wrk->is_revcomp && wrk->apos == sq->L-1) ));
938 for (f = 0; f < 3; f++) // f counts 0..2, but it is *not* the <frame> index; <frame> is stateful
939 {
940 esl_gencode_ProcessOrf(wrk, sq);
941 if (wrk->is_revcomp) wrk->apos--; else wrk->apos++;
942 wrk->frame = (wrk->frame + 1) % 3;
943 }
944 return eslOK;
945 }
946
947
948 /*****************************************************************
949 * 7. Debugging/development utilities
950 *****************************************************************/
951
952 /* Function: esl_gencode_DecodeDigicodon()
953 * Synopsis: Convert digital codon code 0..63 to a text string
954 *
955 * Purpose: Routines in the gencode module encode unambiguous codons
956 * as an index 0..63, by 16 x_0 + 4 x_1 + x_2. Convert
957 * <digicodon> (an index 0..63) to a NUL-terminated codon
958 * string in <codon>, where caller provides allocated space
959 * for the <codon> string for at least 4 characters.
960 *
961 * Returns: <codon> ptr itself; this allows <esl_gencode_DecodeDigicodon()>
962 * to be called directly as a function in printf() arguments,
963 * for example.
964 */
965 char *
966 esl_gencode_DecodeDigicodon(const ESL_GENCODE *gcode, int digicodon, char *codon)
967 {
968 codon[0] = gcode->nt_abc->sym[ digicodon / 16 ];
969 codon[1] = gcode->nt_abc->sym[ (digicodon % 16) / 4 ];
970 codon[2] = gcode->nt_abc->sym[ digicodon % 4 ];
971 codon[3] = '\0';
972 return codon;
973 }
974
975
976 /* Function: esl_gencode_DumpAltCodeTable()
977 * Synopsis: Dump a table of available alternative genetic codes
978 *
979 * Purpose: Write a table of the available options for alternative
980 * genetic codes: the NCBI transl_table index number and a
981 * brief description for each.
982 *
983 * Main use of this function is to format help messages,
984 * listing what the options for transl_table indices are.
985 */
986 int
987 esl_gencode_DumpAltCodeTable(FILE *ofp)
988 {
989 int ntables = sizeof(esl_transl_tables) / sizeof(ESL_GENCODE);
990 int t;
991
992 fprintf(ofp, "id description\n");
993 fprintf(ofp, "--- -----------------------------------\n");
994 for (t = 0; t < ntables; t++)
995 fprintf(ofp, "%3d %s\n", esl_transl_tables[t].transl_table, esl_transl_tables[t].desc);
996 return eslOK;
997 }
998
999
1000 /* Function: esl_gencode_Compare()
1001 * Synopsis: Compare two genetic codes for equality.
1002 *
1003 * Purpose: Compare the two genetic codes <gc1> and <gc2>. Return
1004 * <eslOK> if they are identical, <eslFAIL> if they differ.
1005 */
1006 int
1007 esl_gencode_Compare(const ESL_GENCODE *gc1, const ESL_GENCODE *gc2, int metadata_too)
1008 {
1009 int x;
1010
1011
1012 if (gc1->nt_abc->type != gc2->nt_abc->type) return eslFAIL;
1013 if (gc1->aa_abc->type != gc2->aa_abc->type) return eslFAIL;
1014
1015 if (metadata_too) {
1016 if (gc1->transl_table != gc2->transl_table) return eslFAIL;
1017 if (strcmp(gc1->desc, gc2->desc) != 0) return eslFAIL;
1018 }
1019
1020 for (x = 0; x < 64; x++)
1021 {
1022 if (gc1->basic[x] != gc2->basic[x]) return eslFAIL;
1023 if (gc1->is_initiator[x] != gc2->is_initiator[x]) return eslFAIL;
1024 }
1025 return eslOK;
1026 }
1027
1028
1029 /*****************************************************************
1030 * 8. Unit tests
1031 *****************************************************************/
1032 #ifdef eslGENCODE_TESTDRIVE
1033
1034 static void
1035 utest_ReadWrite(void)
1036 {
1037 char msg[] = "esl_gencode :: Read/Write unit test failed";
1038 char tmpfile[16] = "esltmpXXXXXX";
1039 int ntables = sizeof(esl_transl_tables) / sizeof(ESL_GENCODE);
1040 ESL_ALPHABET *nt_abc = esl_alphabet_Create(eslDNA);
1041 ESL_ALPHABET *aa_abc = esl_alphabet_Create(eslAMINO);
1042 ESL_GENCODE *gc1 = NULL;
1043 ESL_GENCODE *gc2 = NULL;
1044 FILE *ofp = NULL;
1045 ESL_FILEPARSER *efp = NULL;
1046 int t;
1047
1048 for (t = 0; t < ntables; t++)
1049 {
1050 strcpy(tmpfile, "esltmpXXXXXX");
1051 if ( (gc1 = esl_gencode_Create(nt_abc, aa_abc)) == NULL) esl_fatal(msg);
1052 if ( esl_gencode_Set(gc1, esl_transl_tables[t].transl_table) != eslOK) esl_fatal(msg);
1053
1054 if ( esl_tmpfile_named(tmpfile, &ofp) != eslOK) esl_fatal(msg);
1055 if ( esl_gencode_Write(ofp, gc1, /*add_comment=*/TRUE) != eslOK) esl_fatal(msg);
1056 fclose(ofp);
1057
1058 if ( esl_fileparser_Open(tmpfile, /*envvar=*/NULL, &efp) != eslOK) esl_fatal(msg);
1059 if ( esl_gencode_Read(efp, nt_abc, aa_abc, &gc2) != eslOK) esl_fatal(msg);
1060 if ( esl_gencode_Compare(gc1, gc2, /*metadata_too=*/FALSE) != eslOK) esl_fatal(msg); // _Read() does not read the metadata (transl_table, desc)
1061
1062 esl_gencode_Destroy(gc1);
1063 esl_gencode_Destroy(gc2);
1064 esl_fileparser_Close(efp);
1065 remove(tmpfile);
1066 }
1067 esl_alphabet_Destroy(nt_abc);
1068 esl_alphabet_Destroy(aa_abc);
1069 }
1070
1071 #endif /*eslGENCODE_TESTDRIVE*/
1072
1073
1074 /*****************************************************************
1075 * 9. Test driver
1076 *****************************************************************/
1077 #ifdef eslGENCODE_TESTDRIVE
1078
1079 #include <esl_config.h>
1080
1081 #include "esl_gencode.h"
1082
1083 int
1084 main(int argc, char **argv)
1085 {
1086 utest_ReadWrite();
1087 return eslOK;
1088 }
1089 #endif /*eslGENCODE_TESTDRIVE*/
1090
1091
1092 /****************************************************************
1093 * 10. Example
1094 ****************************************************************/
1095
1096 #ifdef eslGENCODE_EXAMPLE
1097 #include "easel.h"
1098 #include "esl_alphabet.h"
1099 #include "esl_fileparser.h"
1100 #include "esl_gencode.h"
1101
1102 #include <stdio.h>
1103
1104 /* The esl_gencode_example driver isn't an example so much as it's a tool.
1105 * It's for digitizing NCBI genetic code tables into the form that
1106 * we keep in esl_transl_tables[]. This program does the hard work;
1107 * you then just have to add the transl_table index and the short
1108 * description manually.
1109 */
1110 int
1111 main(int argc, char **argv)
1112 {
1113 char *codefile = argv[1];
1114 ESL_FILEPARSER *efp = NULL;
1115 ESL_GENCODE *gcode = NULL;
1116 ESL_ALPHABET *nt_abc = esl_alphabet_Create(eslDNA);
1117 ESL_ALPHABET *aa_abc = esl_alphabet_Create(eslAMINO);
1118 int digicodon;
1119 char codon[4];
1120 int status;
1121
1122 if (esl_fileparser_Open(codefile, /*env=*/NULL, &efp) != eslOK) esl_fatal("Failed to open code file %s", codefile);
1123 esl_fileparser_SetCommentChar(efp, '#');
1124
1125 status = esl_gencode_Read(efp, nt_abc, aa_abc, &gcode);
1126 if (status == eslEFORMAT) esl_fatal("Failed to parse genetic code datafile %s\n %s\n", codefile, efp->errbuf);
1127 else if (status != eslOK) esl_fatal("Unexpected failure parsing genetic code datafile %s : code %d\n", codefile, status);
1128
1129 printf("/* ");
1130 for (digicodon = 0; digicodon < 64; digicodon++)
1131 printf("%3s ", esl_gencode_DecodeDigicodon(gcode, digicodon, codon));
1132 printf("*/\n");
1133
1134 printf(" {");
1135 for (digicodon = 0; digicodon < 64; digicodon++)
1136 printf("%3d%c", gcode->basic[digicodon], (digicodon < 63 ? ',' : ' '));
1137 printf("},\n");
1138
1139 printf(" {");
1140 for (digicodon = 0; digicodon < 64; digicodon++)
1141 printf("%3d%c", gcode->is_initiator[digicodon], (digicodon < 63 ? ',' : ' '));
1142 printf("},\n");
1143
1144 printf("/* ");
1145 for (digicodon = 0; digicodon < 64; digicodon++)
1146 printf(" %c ", gcode->aa_abc->sym [gcode->basic[digicodon]]);
1147 printf("*/\n");
1148
1149 esl_alphabet_Destroy(aa_abc);
1150 esl_alphabet_Destroy(nt_abc);
1151 esl_gencode_Destroy(gcode);
1152 esl_fileparser_Close(efp);
1153 }
1154 #endif /*eslGENCODE_EXAMPLE*/
1155
1156
1157 #ifdef eslGENCODE_EXAMPLE2
1158 #include "easel.h"
1159 #include "esl_alphabet.h"
1160 #include "esl_fileparser.h"
1161 #include "esl_gencode.h"
1162
1163 #include <stdio.h>
1164
1165 /* The second example, esl_gencode_example2, is the reverse of the first;
1166 * it's a little utility for writing the standard code in NCBI format.
1167 */
1168 int
1169 main(int argc, char **argv)
1170 {
1171 ESL_ALPHABET *nt_abc = esl_alphabet_Create(eslDNA);
1172 ESL_ALPHABET *aa_abc = esl_alphabet_Create(eslAMINO);
1173 ESL_GENCODE *gcode = esl_gencode_Create(nt_abc, aa_abc);
1174
1175 esl_gencode_Write(stdout, gcode, TRUE);
1176
1177 esl_gencode_Destroy(gcode);
1178 return eslOK;
1179 }
1180 #endif /*eslGENCODE_EXAMPLE2*/
1181