Debian Codegraph @

easel/esl_alphabet.h

raw symbols SCIP
1 /* Digital representation of biosequence symbols in Easel.
2 */
3 #ifndef eslALPHABET_INCLUDED
4 #define eslALPHABET_INCLUDED
5 #include <esl_config.h>
6
7 #include <ctype.h> /* isascii() */
8 #include "easel.h"
9 #ifdef __cplusplus // magic to make C++ compilers happy
10 extern "C" {
11 #endif
12 /* Flags for alphabet types.
13 * Do not change, only add, because these codes are used in file formats.
14 */
15 #define eslUNKNOWN 0 /* 0=unknown is easel-wide convention; don't change */
16 #define eslRNA 1
17 #define eslDNA 2
18 #define eslAMINO 3
19 #define eslCOINS 4 /* for toy examples */
20 #define eslDICE 5 /* also for toy examples */
21 #define eslNONSTANDARD 6
22 /* ... if you add here, change esl_abc_ValidateType() too. */
23
24
25 /* Structure: ESL_ALPHABET
26 */
27 typedef struct {
28 int type; /* eslDNA, eslRNA, eslAMINO, eslNONSTANDARD, etc. */
29 int K; /* uniq alphabet size: 4 or 20 */
30 int Kp; /* total size: alphabet + degen + gap + missing */
31 char *sym; /* "ACGT-RYMKSWHBVDN*~", for instance [0..Kp-1] */
32 ESL_DSQ inmap[128]; /* inmap['A'] = 0, etc: dsq[] index for a symbol */
33 char **degen; /* 1/0, which syms inc which res [0..Kp-1][0..K-1] */
34 int *ndegen; /* # of degenerate residues per code [0..Kp-1] */
35 ESL_DSQ *complement; /* maps sym to complements, [0..Kp-1]; NULL if <type> not DNA/RNA */
36 } ESL_ALPHABET;
37
38
39
40
41 /* 1. An ESL_ALPHABET object.
42 */
43 extern ESL_ALPHABET *esl_alphabet_Create(int type);
44 extern ESL_ALPHABET *esl_alphabet_CreateCustom(const char *alphabet, int K, int Kp);
45 extern int esl_alphabet_SetEquiv(ESL_ALPHABET *a, char sym, char c);
46 extern int esl_alphabet_SetCaseInsensitive(ESL_ALPHABET *a);
47 extern int esl_alphabet_SetDegeneracy(ESL_ALPHABET *a, char c, char *ds);
48 extern int esl_alphabet_SetIgnored(ESL_ALPHABET *a, const char *ignoredchars);
49 extern size_t esl_alphabet_Sizeof(ESL_ALPHABET *a);
50 extern void esl_alphabet_Destroy(ESL_ALPHABET *a);
51 /* 2. Digitized sequences.
52 */
53 extern int esl_abc_CreateDsq(const ESL_ALPHABET *a, const char *seq, ESL_DSQ **ret_dsq);
54 extern int esl_abc_Digitize (const ESL_ALPHABET *a, const char *seq, ESL_DSQ *dsq);
55 extern int esl_abc_Textize (const ESL_ALPHABET *a, const ESL_DSQ *dsq, int64_t L, char *seq);
56 extern int esl_abc_TextizeN (const ESL_ALPHABET *a, const ESL_DSQ *dptr, int64_t L, char *buf);
57 extern int esl_abc_dsqcpy(const ESL_DSQ *dsq, int64_t L, ESL_DSQ *dcopy);
58 extern int esl_abc_dsqdup(const ESL_DSQ *dsq, int64_t L, ESL_DSQ **ret_dup);
59 extern int esl_abc_dsqcat (const ESL_DSQ *inmap, ESL_DSQ **dsq, int64_t *L, const char *s, esl_pos_t n);
60 extern int esl_abc_dsqcat_noalloc(const ESL_DSQ *inmap, ESL_DSQ *dsq, int64_t *L, const char *s, esl_pos_t n);
61 extern int64_t esl_abc_dsqlen(const ESL_DSQ *dsq);
62 extern int64_t esl_abc_dsqrlen(const ESL_ALPHABET *a, const ESL_DSQ *dsq);
63 extern int esl_abc_CDealign(const ESL_ALPHABET *abc, char *s, const ESL_DSQ *ref_ax, int64_t *opt_rlen);
64 extern int esl_abc_XDealign(const ESL_ALPHABET *abc, ESL_DSQ *x, const ESL_DSQ *ref_ax, int64_t *opt_rlen);
65 extern int esl_abc_ConvertDegen2X(const ESL_ALPHABET *abc, ESL_DSQ *dsq);
66 extern int esl_abc_revcomp(const ESL_ALPHABET *abc, ESL_DSQ *dsq, int n);
67
68 /* 3. Other routines in the API.
69 */
70 extern int esl_abc_ValidateType(int type);
71 extern int esl_abc_GuessAlphabet(const int64_t *ct, int *ret_type);
72 extern double esl_abc_Match (const ESL_ALPHABET *a, ESL_DSQ x, ESL_DSQ y, double *p);
73 extern int esl_abc_IAvgScore (const ESL_ALPHABET *a, ESL_DSQ x, const int *sc);
74 extern float esl_abc_FAvgScore (const ESL_ALPHABET *a, ESL_DSQ x, const float *sc);
75 extern double esl_abc_DAvgScore (const ESL_ALPHABET *a, ESL_DSQ x, const double *sc);
76 extern int esl_abc_IExpectScore(const ESL_ALPHABET *a, ESL_DSQ x, const int *sc, const float *p);
77 extern float esl_abc_FExpectScore(const ESL_ALPHABET *a, ESL_DSQ x, const float *sc, const float *p);
78 extern double esl_abc_DExpectScore(const ESL_ALPHABET *a, ESL_DSQ x, const double *sc, const double *p);
79
80 extern int esl_abc_IAvgScVec (const ESL_ALPHABET *a, int *sc);
81 extern int esl_abc_FAvgScVec (const ESL_ALPHABET *a, float *sc);
82 extern int esl_abc_DAvgScVec (const ESL_ALPHABET *a, double *sc);
83 extern int esl_abc_IExpectScVec(const ESL_ALPHABET *a, int *sc, const float *p);
84 extern int esl_abc_FExpectScVec(const ESL_ALPHABET *a, float *sc, const float *p);
85 extern int esl_abc_DExpectScVec(const ESL_ALPHABET *a, double *sc, const double *p);
86 extern int esl_abc_FCount (const ESL_ALPHABET *a, float *ct, ESL_DSQ x, float wt);
87 extern int esl_abc_DCount (const ESL_ALPHABET *a, double *ct, ESL_DSQ x, double wt);
88 extern int esl_abc_EncodeType (char *typestring);
89 extern int esl_abc_EncodeTypeMem(char *type, int n);
90 extern char *esl_abc_DecodeType (int type);
91 extern int esl_abc_ValidateSeq(const ESL_ALPHABET *a, const char *seq, int64_t L, char *errbuf);
92
93 /* In the tests below, remember the rules of order in internal alphabets:
94 * Canonical alphabet Gap Degeneracies Any None Missing
95 * 0..K-1 K K+1..Kp-4 (Kp-3) (Kp-2) (Kp-1)
96 * ACGT - RYMKSWHBVD N * ~ DNA: K=4 Kp=18
97 * ACDEFGHIKLMNPQRSTVWY - BJZOU X * ~ protein: K=20 Kp=29
98 *
99 * ESL_DSQ is an unsigned 8-bit type, so don't test for >= 0 or compilers will complain.
100 */
101 #define esl_abc_DigitizeSymbol(a, c) ((a)->inmap[(int)c])
102 #define esl_abc_XIsValid(a, x) ((x) < (a)->Kp)
103 #define esl_abc_XIsResidue(a, x) ((x) < (a)->K || ((x) > (a)->K && (x) < (a)->Kp-2))
104 #define esl_abc_XIsCanonical(a, x) ((x) < (a)->K)
105 #define esl_abc_XIsGap(a, x) ((x) == (a)->K)
106 #define esl_abc_XIsDegenerate(a, x) ((x) > (a)->K && (x) < (a)->Kp-2)
107 #define esl_abc_XIsUnknown(a, x) ((x) == (a)->Kp-3)
108 #define esl_abc_XIsNonresidue(a, x) ((x) == (a)->Kp-2)
109 #define esl_abc_XIsMissing(a, x) ((x) == (a)->Kp-1)
110 #define esl_abc_XGetGap(a) ((a)->K)
111 #define esl_abc_XGetUnknown(a) ((a)->Kp-3)
112 #define esl_abc_XGetNonresidue(a) ((a)->Kp-2)
113 #define esl_abc_XGetMissing(a) ((a)->Kp-1)
114
115
116 #define esl_abc_CIsValid(a, c) (isascii(c) && (a)->inmap[(int)c] < (a)->Kp)
117 #define esl_abc_CIsResidue(a, c) ((a)->inmap[(int)c] < (a)->K || ((a)->inmap[(int)c] > (a)->K && (a)->inmap[(int)c] < (a)->Kp-2))
118 #define esl_abc_CIsCanonical(a, c) ((a)->inmap[(int)c] < (a)->K)
119 #define esl_abc_CIsGap(a, c) ((a)->inmap[(int)c] == (a)->K)
120 #define esl_abc_CIsDegenerate(a, c) ((a)->inmap[(int)c] > (a)->K && (a)->inmap[(int)c] < (a)->Kp-2)
121 #define esl_abc_CIsUnknown(a, c) ((a)->inmap[(int)c] == (a)->Kp-3)
122 #define esl_abc_CIsNonresidue(a, c) ((a)->inmap[(int)c] == (a)->Kp-2)
123 #define esl_abc_CIsMissing(a, c) ((a)->inmap[(int)c] == (a)->Kp-1)
124 #define esl_abc_CGetGap(a) ((a)->sym[(a)->K])
125 #define esl_abc_CGetUnknown(a) ((a)->sym[(a)->Kp-3])
126 #define esl_abc_CGetNonresidue(a) ((a)->sym[(a)->Kp-2])
127 #define esl_abc_CGetMissing(a) ((a)->sym[(a)->Kp-1])
128 #ifdef __cplusplus // magic to make C++ compilers happy
129 }
130 #endif
131 #endif /*eslALPHABET_INCLUDED*/