Debian Codegraph @

hmmer/src/fm_alphabet.c

raw symbols SCIP
1 #include <p7_config.h>
2
3 #include <ctype.h>
4
5 #include "easel.h"
6 #include "esl_mem.h"
7 #include "hmmer.h"
8
9
10 /* Function: fm_alphabetCreate()
11 *
12 * Synopsis: Produce an alphabet for FMindex.
13 *
14 * Purpose: Produce an alphabet for FMindex. This may end up being
15 * replaced with easel alphabet functions, but the easel
16 * requirement of having a gap-character between
17 * cannonical and degenerate symbols poses a problem
18 * from a bit-packing perspective
19 *
20 * Args: meta - metadata object already initialized with the alphabet type.
21 * This will hold the alphabet (and corresponding reverse alphabet)
22 * created here.
23 * alph_bits - pointer to an int that this function sets equal to the
24 * number of bits required to store the alphabet (log of alph size)
25 *
26 * Returns: <eslOK> on success.
27 */
28 int
29 fm_alphabetCreate (FM_METADATA *meta, uint8_t *alph_bits) {
30
31 int i = 0;
32 int status;
33
34
35 if ( meta->alph_type == fm_DNA) {
36 meta->alph_size = 4;
37 if (alph_bits) *alph_bits = 2;
38 /*
39 } else if ( meta->alph_type == fm_DNA_full) {
40 meta->alph_size = 15;
41 if (alph_bits) *alph_bits = 4;
42 */
43 } else if ( meta->alph_type == fm_AMINO) {
44 meta->alph_size = 26;
45 if (alph_bits) *alph_bits = 5;
46 } else {
47 esl_fatal("Unknown alphabet type\n%s", "");
48 }
49
50 ESL_ALLOC(meta->alph, (1+meta->alph_size)*sizeof(char));
51 ESL_ALLOC(meta->inv_alph, 256*sizeof(char));
52
53 if ( meta->alph_type == fm_DNA /*|| meta->alph_type == fm_DNA_full*/)
54 ESL_ALLOC(meta->compl_alph, (1+meta->alph_size)*sizeof(int));
55 else
56 meta->compl_alph = NULL;
57
58
59 if ( meta->alph_type == fm_DNA) {
60 esl_memstrcpy("ACGT", 4, meta->alph);
61 for (i=0; i<4; i++)
62 meta->compl_alph[i] = 3-i;
63
64 /* TODO: fm_DNA_full has currently been disabled because of problems with how the
65 * FM index handles very long runs of the same character (in this case, Ns).
66 * See wheelert/notebook/2013/12-11-FM-alphabet-speed notes on 12/12.
67 *
68 } else if ( meta->alph_type == fm_DNA_full) {
69 esl_memstrcpy("ACGTRYMKSWHBVDN", 15, meta->alph);
70 meta->compl_alph[0] = 3; // A->T
71 meta->compl_alph[1] = 2; // C->G
72 meta->compl_alph[2] = 1; // G->C
73 meta->compl_alph[3] = 0; // T->A
74 meta->compl_alph[4] = 5; // R->Y
75 meta->compl_alph[5] = 4; // Y->R
76 meta->compl_alph[6] = 7; // M->K
77 meta->compl_alph[7] = 6; // K->M
78 meta->compl_alph[8] = 8; // S S
79 meta->compl_alph[9] = 9; // W W
80 meta->compl_alph[10]= 13; // H->D
81 meta->compl_alph[11]= 12; // B->V
82 meta->compl_alph[12]= 11; // V->B
83 meta->compl_alph[13]= 10; // D->H
84 meta->compl_alph[14]= 14; // N N
85 */
86 } else if ( meta->alph_type == fm_AMINO) {
87 esl_memstrcpy("ACDEFGHIKLMNPQRSTVWYBJZOUX", meta->alph_size, meta->alph);
88 }
89
90 for (i=0; i<256; i++)
91 meta->inv_alph[i] = -1;
92
93 for (i=0; i<meta->alph_size; i++) {
94 meta->inv_alph[tolower(meta->alph[i])] = meta->inv_alph[toupper(meta->alph[i])] = i;
95
96 //special case for RNA, equate U to T:
97 if ( (meta->alph_type == fm_DNA /*|| meta->alph_type == fm_DNA_full*/) && toupper(meta->alph[i]) == 'T')
98 meta->inv_alph['u'] = meta->inv_alph['U'] = i;
99 }
100
101
102
103 return eslOK;
104
105 ERROR:
106 esl_fatal("error allocating space for alphabet\n");
107 return eslFAIL;
108 }
109
110 /* Function: fm_alphabetDestroy()
111 *
112 * Synopsis: Free the alphabet for an FMindex metadata object
113 *
114 * Purpose: Free both the alphabet and corresponding inverse alphabet
115 * (inv_alph) held within <meta>.
116 *
117 * Returns: <eslOK> on success.
118 */
119 int
120 fm_alphabetDestroy (FM_METADATA *meta) {
121 if (meta != NULL){
122 if (meta->alph != NULL) free (meta->alph);
123 if (meta->inv_alph != NULL) free (meta->inv_alph);
124 if (meta->compl_alph != NULL) free (meta->compl_alph);
125 }
126
127 return eslOK;
128 }
129
130
131 /* Function: fm_reverseString()
132 *
133 * Synopsis: Take as input a string and its length, and reverse the
134 * string in place.
135 *
136 * Returns: <eslOK> on success.
137 */
138 int
139 fm_reverseString (char* str, int N)
140 {
141 int end = N-1;
142 int start = 0;
143
144 while( start<end )
145 {
146 str[start] ^= str[end];
147 str[end] ^= str[start];
148 str[start] ^= str[end];
149
150 ++start;
151 --end;
152 }
153
154 return eslOK;
155 }
156
157 /* Function: fm_getComplement()
158 *
159 * Synopsis: convert a character c to its complement
160 *
161 * Returns: <eslOK> on success.
162 */
163 int
164 fm_getComplement (char c, uint8_t alph_type)
165 {
166 if ( alph_type == fm_DNA ) {
167 return 3-c;
168 /*
169 } else if ( alph_type == fm_DNA_full) {
170 esl_fatal("complement for DNA_full not yet implemented\n");
171 */
172 } else if ( alph_type == fm_AMINO) {
173 esl_fatal("complement for amino acids is undefined\n");
174 } else {
175 esl_fatal("Unknown alphabet type\n%s", "");
176 }
177 return -1;
178 }