Debian Codegraph @

src/alphabet.c

raw symbols SCIP
1 /* alphabet.c
2 * SRE, Tue Aug 1 10:31:11 2000 [St. Louis]
3 *
4 * Stuff having to do with manipulating symbols in the (RNA) alphabet.
5 */
6
7 #include <esl_config.h>
8 #include <p7_config.h>
9 #include "config.h"
10
11 #include <string.h>
12 #include <ctype.h>
13 #include <math.h>
14
15 #include "easel.h"
16 #include "esl_alphabet.h"
17 #include "esl_random.h"
18 #include "esl_randomseq.h"
19 #include "esl_vectorops.h"
20
21 #include "infernal.h"
22
23 /* Function: PairCount()
24 * Date: SRE, Tue Aug 1 10:34:20 2000 [St. Louis]
25 *
26 * Purpose: Given a possibly degenerate symbol code for left
27 * and right symbols in a pair, increment a symbol
28 * counter array appropriately.
29 *
30 * Args: abc - pointer to the internal alphabet
31 * counters - vector to count into [0..abc->K^2-1]
32 * syml - index of left symbol [0..abc->sym_iupac-1]
33 * symr - index of right symbol [0..abc->sym_iupac-1]
34 * wt - weight to use for the count (often 1.0).
35 *
36 * Returns: void
37 */
38 void
39 PairCount(const ESL_ALPHABET *abc, float *counters, ESL_DSQ syml, ESL_DSQ symr, float wt)
40 {
41 if (syml < abc->K && symr < abc->K)
42 counters[(int) (syml * abc->K + symr)] += wt;
43 else {
44 int status;
45 float *left = NULL;
46 float *right = NULL;
47 int l,r;
48 ESL_ALLOC(left, (sizeof(float) * abc->K));
49 ESL_ALLOC(right, (sizeof(float) * abc->K));
50
51 /* Be careful to set weights as 1.0 for the FCount calls (not <wt>),
52 * because what we want to know is the fraction of all possible
53 * residues that agree with the degenerate for each residue, i.e.:
54 *
55 * if syml == 'N': left[A] == left[C] == left[G] == left[U] == 0.25
56 * and symr == 'R': right[A] == right[G] == 0.5
57 *
58 * Then when we increment counters by left[l] * left[r] * <wt>.
59 * That way if <wt> = 0.8, counters is set as follows:
60 *
61 * if syml == 'N' && symr == 'R':
62 * counters['AA'] == counters['CA'] == counters ['GA'] == counters['UA'] ==
63 * counters['AG'] == counters['CG'] == counters ['GG'] == counters['UG'] == 0.1
64 * and all others are = 0.0.
65 *
66 * Previously (Infernal versions 0.55 (maybe even earlier) until
67 * 1.0.2) this was done incorrectly by passing <wt> to the
68 * FCount() calls and setting counters[] = left[l] *
69 * right[r]. Somehow, Stefan Janssen tracked this down. Thanks
70 * Stefan! The bug is i23, logged in Bugs/BUGTRAX.
71 * EPN, Mon Dec 6 13:09:52 2010
72 */
73 esl_vec_FSet(left, abc->K, 0.);
74 esl_vec_FSet(right, abc->K, 0.);
75 esl_abc_FCount(abc, left, syml, 1.0);
76 esl_abc_FCount(abc, right, symr, 1.0);
77
78 for (l = 0; l < abc->K; l++)
79 for (r = 0; r < abc->K; r++)
80 counters[l*abc->K +r] += left[l] * right[r] * wt;
81
82 free(left);
83 free(right);
84 }
85 return;
86
87 ERROR:
88 cm_Fail("Memory allocation error.");
89 return; /* never reached */
90 }
91
92 /* Function: PairCountMarginal()
93 * Date: EPN, Fri Nov 4 10:43:58 2022
94 *
95 * Purpose: Marginal-aware version of PairCount in which
96 * exactly one of syml and symr is missing data '~'
97 * due to a sequence truncation. We partition <wt>
98 * across the 4 possible values that '~' could be
99 * based on a mean posterior estimate using the
100 * Dirichlet prior <pri> and the existing counts
101 * from full MP states in <counters>.
102 *
103 * <counters> should already be filled with *all* MP counts
104 * for non-truncated MP states that emitted both a left and
105 * right symbol so that the Dirichlet prior can use those
106 * counts when determining mean posterior estimates.
107 *
108 * Args: abc - pointer to the internal alphabet
109 * counters - vector to count into [0..abc->K^2-1]
110 * syml - index of left symbol [0..abc->sym_iupac-1] or abc->Kp-1 if missing (~)
111 * symr - index of right symbol [0..abc->sym_iupac-1] or abc->Kp-1 if missing (~)
112 * wt - weight to use for the count (often 1.0).
113 * pri - the dirichlet prior to use
114 *
115 * Returns: void
116 */
117 void
118 PairCountMarginal(const ESL_ALPHABET *abc, double *nonmarg_counters, float *updated_counters, ESL_DSQ syml, ESL_DSQ symr, float wt, const Prior_t *pri)
119 {
120 int status; /* easel status */
121 double *probs = NULL; /* double copy of probability parameters for each of the 16 possible basepairs */
122 double sum; /* sum of probs that include syml or symr */
123 int l, r; /* counters */
124 float *left = NULL; /* only used in case syml is degenerate */
125 float *right = NULL; /* only used in case symr is degenerate */
126
127 ESL_ALLOC(probs, sizeof(double) * pri->maxnalpha);
128
129 /* organization of this function:
130 * handle case where symr is missing:
131 * - handle case where syml is not degenerate
132 * - handle case where syml is degenerate
133 * handle case where syml is missing:
134 * - handle case where symr is not degenerate
135 * - handle case where symr is degenerate
136 */
137
138 /* handle case where symr is missing */
139 if(esl_abc_XIsMissing(abc, symr)) {
140 if(esl_abc_XIsMissing(abc, syml)) {
141 cm_Fail("PairCountMarginal() entered with both syml and symr missing");
142 }
143 esl_mixdchlet_MPParameters(pri->mbp, nonmarg_counters, probs);
144 if(syml < abc->K) { /* syml is not degenerate */
145 /* sum Dirichlet MP probs for all basepairs that include syml as the left half
146 * then partition out <wt> by that proportion */
147 sum = 0.;
148 for (r = 0; r < abc->K; r++) {
149 sum += probs[syml * abc->K + r];
150 }
151 for (r = 0; r < abc->K; r++) {
152 updated_counters[syml * abc->K + r] += (probs[syml * abc->K + r] / sum) * wt;
153 }
154 }
155 else { /* syml is degenerate, need to also spread wt across the canonical nt the ambiguity is consistent with */
156 /* the logic here is similar to in PairCount() */
157 ESL_ALLOC(left, (sizeof(float) * abc->K));
158 esl_vec_FSet(left, abc->K, 0.);
159 esl_abc_FCount(abc, left, syml, 1.0);
160 /* 1.0 above could actually be any number, b/c we compute sum in 1st nested
161 * for loop and then divide by it it second nested for loop below */
162
163 /* as above for non-degenerate case, sum Dirichlet MP probs for
164 * all basepairs that include syml as the left half then
165 * partition out <wt> by that proportion */
166 sum = 0.;
167 for (l = 0; l < abc->K; l++) {
168 for (r = 0; r < abc->K; r++) {
169 sum += probs[l * abc->K + r] * left[l]; /* left[l] == 0. for any canonical nt syml is inconsistent with */
170 }
171 }
172 for (l = 0; l < abc->K; l++) {
173 for (r = 0; r < abc->K; r++) {
174 updated_counters[l * abc->K + r] += ((probs[l * abc->K + r] * left[l]) / sum) * wt;
175 }
176 }
177 }
178 }
179 /* handle case where syml is missing */
180 else if(esl_abc_XIsMissing(abc, syml)) {
181 if(esl_abc_XIsMissing(abc, symr)) {
182 cm_Fail("PairCountMarginal() entered with both syml and symr missing");
183 }
184 esl_mixdchlet_MPParameters(pri->mbp, nonmarg_counters, probs);
185 if(symr < abc->K) { /* symr is not degenerate */
186 /* sum Dirichlet MP probs for all basepairs that include symr as the right half
187 * then partition out <wt> by that proportion */
188 sum = 0.;
189 for (l = 0; l < abc->K; l++) {
190 sum += probs[l * abc->K + symr];
191 }
192 for (l = 0; l < abc->K; l++) {
193 updated_counters[l * abc->K + symr] += (probs[l * abc->K + symr] / sum) * wt;
194 }
195 }
196 else { /* symr is degenerate, need to also spread wt across the canonical nt the ambiguity is consistent with */
197 /* the logic here is similar to in PairCount() */
198 ESL_ALLOC(right, (sizeof(float) * abc->K));
199 esl_vec_FSet(right, abc->K, 0.);
200 esl_abc_FCount(abc, right, symr, 1.0);
201 /* 1.0 above could actually be any number, b/c we compute sum in 1st nested
202 * for loop and then divide by it it second nested for loop below */
203
204 /* as above for non-degenerate case, sum Dirichlet MP probs for
205 * all basepairs that include symr as the right half then
206 * partition out <wt> by that proportion */
207 sum = 0.;
208 for (l = 0; l < abc->K; l++) {
209 for (r = 0; r < abc->K; r++) {
210 sum += probs[l * abc->K + r] * right[r]; /* right[r] == 0. for any canonical nt syml is inconsistent with */
211 }
212 }
213 for (l = 0; l < abc->K; l++) {
214 for (r = 0; r < abc->K; r++) {
215 updated_counters[l * abc->K + r] += ((probs[l * abc->K + r] * right[r]) / sum) * wt;
216 }
217 }
218 }
219 }
220 else {
221 cm_Fail("PairCountMarginal() entered with neither syml or symr missing");
222 }
223 if(probs != NULL) free(probs);
224 if(left != NULL) free(left);
225 if(right != NULL) free(right);
226
227 return;
228
229 ERROR:
230 if(probs != NULL) free(probs);
231 if(left != NULL) free(left);
232 if(right != NULL) free(right);
233
234 cm_Fail("Memory allocation error.");
235 return; /* never reached */
236 }
237
238 float
239 DegeneratePairScore(const ESL_ALPHABET *abc, float *esc, ESL_DSQ syml, ESL_DSQ symr)
240 {
241 float *left = NULL;
242 float *right = NULL;
243 int status;
244 ESL_ALLOC(left, (sizeof(float) * abc->K));
245 ESL_ALLOC(right, (sizeof(float) * abc->K));
246 int l,r;
247 float sc;
248
249 if (syml < abc->K && symr < abc->K) /* canonical pair */
250 {
251 free(left);
252 free(right);
253 return esc[(int) (syml*abc->K+symr)];
254 }
255
256 if (syml == abc->K || symr == abc->K) /* gap, this gets an IMPOSSIBLE sc */
257 {
258 free(left);
259 free(right);
260 return IMPOSSIBLE;
261 }
262
263 if (syml == (abc->Kp-1) || symr == (abc->Kp-1)) /* missing data, this gets an IMPOSSIBLE sc */
264 {
265 free(left);
266 free(right);
267 return IMPOSSIBLE;
268 }
269
270 esl_vec_FSet(left, abc->K, 0.);
271 esl_vec_FSet(right, abc->K, 0.);
272 esl_abc_FCount(abc, left, syml, 1.);
273 esl_abc_FCount(abc, right, symr, 1.);
274
275 sc = 0.;
276 for (l = 0; l < abc->K; l++)
277 for (r = 0; r < abc->K; r++)
278 sc += esc[l*abc->K+r] * left[l] * right[r];
279
280 free(left);
281 free(right);
282
283 return sc;
284
285 ERROR:
286 cm_Fail("Memory allocation error.");
287 return 0.0; /* never reached */
288 }
289 int
290 iDegeneratePairScore(const ESL_ALPHABET *abc, int *iesc, ESL_DSQ syml, ESL_DSQ symr)
291 {
292 float *left = NULL;
293 float *right = NULL;
294 int status;
295 ESL_ALLOC(left, (sizeof(float) * abc->K));
296 ESL_ALLOC(right, (sizeof(float) * abc->K));
297 int l,r;
298 float sc;
299
300 if (syml < abc->K && symr < abc->K)
301 return iesc[(int) (syml*abc->K+symr)];
302
303 if (syml == abc->K || symr == abc->K) /* gap, this gets an -INFTY sc */
304 return -INFTY;
305
306 if (syml == (abc->Kp-1) || symr == (abc->Kp-1)) /* missing data, this gets an -INFTY sc */
307 return -INFTY;
308
309 esl_vec_FSet(left, abc->K, 0.);
310 esl_vec_FSet(right, abc->K, 0.);
311 esl_abc_FCount(abc, left, syml, 1.);
312 esl_abc_FCount(abc, right, symr, 1.);
313
314 sc = 0.;
315 for (l = 0; l < abc->K; l++)
316 for (r = 0; r < abc->K; r++)
317 sc += iesc[l*abc->K+r] * left[l] * right[r];
318
319 free(left);
320 free(right);
321
322 return (int) sc;
323
324 ERROR:
325 cm_Fail("Memory allocation error.");
326 return status; /* never reached */
327 }
328 /* EPN, Wed Aug 20 13:44:16 2008
329 * FastPairScore*() functions:
330 * Written to calculate base pairs scores involving 1 or 2 canonical
331 * residues as efficiently as I know how.
332 * Calculating 'optimized' emission scores, which calculates all possible
333 * base pair scores (including non-canonicals) in cm_mx.c:{F,I}CalcOptimizedEmitScores()
334 * was a time bottleneck in alignment with P7 and CP9 bands during sub CM construction.
335 * sub CMs are created for each target seq, and optimized scores must be
336 * calc'ed for each sub CM.
337 */
338
339 /* Function: FastPairScoreBothDegenerate() (float version)
340 * iFastPairScoreBothDegenerate() (int version)
341 * Incept: EPN, Wed Aug 20 13:18:15 2008
342 *
343 * Purpose: Score a base pair given left and right vectors denoting
344 * fractional weight of each canonical for left residue
345 * and right residue. This function should only be called if
346 * at least 2 values in both left and right are non-zero,
347 * that is, if both left and right correspond to non-canonical
348 * residues.
349 *
350 * Example: if we're calculating for left char 'S' (C or G),
351 * and right char 'N' (A,C,G,U), then we'd have:
352 * left = [0.00, 0.50, 0.50, 0.00]
353 * right = [0.25, 0.25, 0.25, 0.25]
354 *
355 * K - alphabet size, abc->K
356 * esc - emission vector, canonical pair scores are used to calc <sc>
357 * left - [0..l..K-1] fraction of each canonical residue
358 * right - [0..r..K-1] fraction of each canonical residue
359 *
360 * Returns: <sc> the score of the pair
361 */
362
363 float
364 FastPairScoreBothDegenerate(int K, float *esc, float *left, float *right)
365 {
366 int l,r;
367 float sc;
368
369 sc = 0.;
370 for (l = 0; l < K; l++)
371 for (r = 0; r < K; r++)
372 sc += esc[l*K+r] * left[l] * right[r];
373 return sc;
374 }
375 int
376 iFastPairScoreBothDegenerate(int K, int *iesc, float *left, float *right)
377 {
378 int l,r;
379 float sc;
380
381 sc = 0.;
382 for (l = 0; l < K; l++)
383 for (r = 0; r < K; r++)
384 sc += iesc[l*K+r] * left[l] * right[r];
385 return (int) sc;
386 }
387 /* Function: FastPairScoreLeftOnlyDegenerate() (float version)
388 * iFastPairScoreLeftOnlyDegenerate() (int version)
389 * Incept: EPN, Wed Aug 20 13:18:15 2008
390 *
391 * Purpose: Score a base pair with left residue non-canonical and
392 * right residue canonical, given a <left> vector denoting
393 * fractional weight of each canonical for left residue.
394 * This function should only be called if at least 2 values
395 * left are non-zero, that is, if left corresponds to a
396 * non-canonical residue.
397 *
398 * Example: if we're calculating for left char 'S' (C or G),
399 * left = [0.00, 0.50, 0.50, 0.00]
400 *
401 * K - alphabet size, abc->K
402 * esc - emission vector, canonical pair scores are used to calc <sc>
403 * left - [0..l..K-1] fraction of each canonical residue
404 * symr - canonical right residue must be in range 0..K-1
405 *
406 * Returns: <sc> the score of the pair
407 */
408 float
409 FastPairScoreLeftOnlyDegenerate(int K, float *esc, float *left, ESL_DSQ symr)
410 {
411 int l;
412 float sc;
413
414 sc = 0.;
415 for (l = 0; l < K; l++) sc += esc[l*K+symr] * left[l];
416 return sc;
417 }
418 int
419 iFastPairScoreLeftOnlyDegenerate(int K, int *iesc, float *left, ESL_DSQ symr)
420 {
421 int l;
422 float sc;
423
424 sc = 0.;
425 for (l = 0; l < K; l++) sc += iesc[l*K+symr] * left[l];
426 return (int) sc;
427 }
428 /* Function: FastPairScoreRightOnlyDegenerate() (float version)
429 * iFastPairScoreRightOnlyDegenerate() (int version)
430 * Incept: EPN, Wed Aug 20 13:18:15 2008
431 *
432 * Purpose: Score a base pair with right residue non-canonical and
433 * right residue canonical, given a <right> vector denoting
434 * fractional weight of each canonical for right residue.
435 * This function should only be called if at least 2 values
436 * left are non-zero, that is, if right corresponds to a
437 * non-canonical residue.
438 *
439 * Example: if we're calculating for right char 'M' (A or C),
440 * right = [0.50, 0.50, 0.00, 0.00]
441 *
442 * K - alphabet size, abc->K
443 * esc - emission vector, canonical pair scores are used to calc <sc>
444 * right - [0..l..K-1] fraction of each canonical residue
445 * syml - canonical left residue, must be in range 0..K-1
446 *
447 * Returns: <sc> the score of the pair
448 */
449 float
450 FastPairScoreRightOnlyDegenerate(int K, float *esc, float *right, ESL_DSQ syml)
451 {
452 int r;
453 float sc;
454
455 sc = 0.;
456 for (r = 0; r < K; r++) sc += esc[syml*K+r] * right[r];
457 return sc;
458 }
459 float
460 iFastPairScoreRightOnlyDegenerate(int K, int *iesc, float *right, ESL_DSQ syml)
461 {
462 int r;
463 float sc;
464
465 sc = 0.;
466 for (r = 0; r < K; r++) sc += iesc[syml*K+r] * right[r];
467 return (int) sc;
468 }
469
470 /* Following funcs from RSEARCH, only used by RSEARCH code */
471
472 static char random_from_string (ESL_RANDOMNESS *r, char *s);
473 /* Function: resolve_degenerate
474 * Date: September, 1998 (from hmmgcc)
475 * This function resolves "degnerate" nucleotides by selecting a random
476 * A, C, G, or T as appropriate by the code present there. Returns
477 * the character passed in if that character does not represent a
478 * non-degnerate nucleotide (either A, C, G, or T or not representative
479 * at all of a nucleotide.
480 *
481 * The degenerate code used here is:
482 * (taken from http://www.neb.com/neb/products/REs/RE_code.html
483 *
484 * R = G or A
485 * K = G or T
486 * B = not A (C or G or T)
487 * V = not T (A or C or G)
488 * Y = C or T
489 * S = G or C
490 * D = not C (A or G or T)
491 * N = A or C or G or T
492 * M = A or C
493 * W = A or T
494 * H = not G (A or C or T)
495 *
496 * This function assumes all letters are already uppercased via toupper
497 * before calling. In other words, it will return a "n" if passed an "n"
498 * because it will assume that the symbol for all nucleotides will be passed
499 * in as "N".
500 */
501 char resolve_degenerate (ESL_RANDOMNESS *r, char c) {
502 c = toupper(c);
503 switch (c) {
504 case 'A' : return(c);
505 case 'C' : return(c);
506 case 'G' : return(c);
507 case 'T' : return(c);
508 case 'R' : return(random_from_string(r, "GA"));
509 case 'K' : return(random_from_string(r, "GT"));
510 case 'B' : return(random_from_string(r, "CGT"));
511 case 'V' : return(random_from_string(r, "ACG"));
512 case 'Y' : return(random_from_string(r, "CT"));
513 case 'S' : return(random_from_string(r, "GC"));
514 case 'D' : return(random_from_string(r, "AGT"));
515 case 'N' : return(random_from_string(r, "ACGT"));
516 case 'M' : return(random_from_string(r, "AC"));
517 case 'W' : return(random_from_string(r, "AT"));
518 case 'H' : return(random_from_string(r, "ACT"));
519 }
520 return(c);
521 }
522
523
524 /*
525 * Function: random_from_string
526 * Date: September, 1998 (approx.) -- from hmmgcc
527 * This function returns a character randomly chosen from the string.
528 * Used in conjunction with the function below that resolves degenerate code
529 * nucleotides.
530 */
531 char random_from_string (ESL_RANDOMNESS *r, char *s) {
532 int i;
533 do
534 {
535 /*i = (int) ((float)(strlen(s)-1)*esl_random(r)/(RAND_MAX+1.0));*/
536 i = (int) ((float)(strlen(s)) * esl_random(r));
537 } while (i<0 || i>=strlen(s));
538 return(s[i]);
539 }
540
541
542 /* Function: revcomp()
543 * Incept: EPN, Tue Aug 7 10:05:14 2007
544 * based on Squid's revcomp()
545 *
546 * Purpose: Reverse complement ESL_SQ seq; store in comp.
547 * Can revcomp "in place" (revcomp(seq, seq)).
548 * sq can be in digital or text form.
549 *
550 * Args: comp - destination for reverse complement of sq
551 * seq - sequence to reverse complement
552 *
553 * Returns: eslOK on success;
554 * Dies immediately if any error occurs.
555 */
556 int
557 revcomp(const ESL_ALPHABET *abc, ESL_SQ *comp, ESL_SQ *sq)
558 {
559 int status;
560 int do_digital = FALSE;
561 int i;
562
563 /* contract checks */
564 if (comp == NULL)
565 cm_Fail("ERROR in revcomp, comp is NULL.");
566 if(sq == NULL)
567 cm_Fail("ERROR in revcomp, sq is NULL.");
568 if(sq->dsq != NULL && comp->dsq == NULL)
569 cm_Fail("ERROR in revcomp, sq is digital, comp is not.");
570 if(sq->dsq == NULL && comp->dsq != NULL)
571 cm_Fail("ERROR in revcomp, comp is digital, sq is not.");
572 if(abc->type != eslRNA && abc->type != eslDNA)
573 cm_Fail("ERROR in revcomp, alphabet type must be RNA or DNA.");
574 if(comp->n < sq->n)
575 cm_Fail("ERROR in revcomp, comp->n is smaller than sq->n.");
576
577 if(sq->dsq != NULL) do_digital = TRUE;
578
579 if(do_digital) {
580 if((status = esl_rsq_XReverse(sq->dsq, sq->n, comp->dsq)) != eslOK)
581 goto ERROR;
582 }
583 else {
584 if((status = esl_rsq_CReverse(sq->seq, comp->seq) != eslOK))
585 goto ERROR;
586 }
587
588 if(do_digital)
589 {
590 for(i = 1; i <= sq->n; i++)
591 {
592 if(sq->dsq[i] >= abc->Kp) { status = eslEINVAL; goto ERROR; }
593 switch (abc->sym[sq->dsq[i]]) {
594 case 'A':
595 if(abc->type == eslRNA)
596 comp->dsq[i] = abc->inmap[(int) 'U'];
597 else
598 comp->dsq[i] = abc->inmap[(int) 'T'];
599 break;
600 case 'C': comp->dsq[i] = abc->inmap[(int) 'G']; break;
601 case 'G': comp->dsq[i] = abc->inmap[(int) 'C']; break;
602 case 'T': comp->dsq[i] = abc->inmap[(int) 'A']; break;
603 case 'U': comp->dsq[i] = abc->inmap[(int) 'A']; break;
604 case 'R': comp->dsq[i] = abc->inmap[(int) 'Y']; break;
605 case 'Y': comp->dsq[i] = abc->inmap[(int) 'R']; break;
606 case 'M': comp->dsq[i] = abc->inmap[(int) 'K']; break;
607 case 'K': comp->dsq[i] = abc->inmap[(int) 'M']; break;
608 case 'S': comp->dsq[i] = abc->inmap[(int) 'S']; break;
609 case 'W': comp->dsq[i] = abc->inmap[(int) 'W']; break;
610 case 'H': comp->dsq[i] = abc->inmap[(int) 'D']; break;
611 case 'D': comp->dsq[i] = abc->inmap[(int) 'H']; break;
612 case 'B': comp->dsq[i] = abc->inmap[(int) 'V']; break;
613 case 'V': comp->dsq[i] = abc->inmap[(int) 'B']; break;
614 default: break; /* anything else? leave it; it's prob a gap or an X */
615 }
616 }
617 }
618 else
619 {
620 for(i = 0; i < sq->n; i++)
621 {
622 if(islower(sq->seq[i])) { status = eslEINVAL; goto ERROR; }
623 switch (sq->seq[i]) {
624 case 'A':
625 if(abc->type == eslRNA)
626 comp->seq[i] = 'U';
627 else
628 comp->seq[i] = 'T';
629 break;
630 case 'C': comp->seq[i] = 'G'; break;
631 case 'G': comp->seq[i] = 'C'; break;
632 case 'T': comp->seq[i] = 'A'; break;
633 case 'U': comp->seq[i] = 'A'; break;
634 case 'R': comp->seq[i] = 'Y'; break;
635 case 'Y': comp->seq[i] = 'R'; break;
636 case 'M': comp->seq[i] = 'K'; break;
637 case 'K': comp->seq[i] = 'M'; break;
638 case 'S': comp->seq[i] = 'S'; break;
639 case 'W': comp->seq[i] = 'W'; break;
640 case 'H': comp->seq[i] = 'D'; break;
641 case 'D': comp->seq[i] = 'H'; break;
642 case 'B': comp->seq[i] = 'V'; break;
643 case 'V': comp->seq[i] = 'B'; break;
644 default: break; /* anything else? leave it; it's prob a gap or an X */
645 }
646 }
647 }
648 return eslOK;
649
650 ERROR:
651 cm_Fail("Unexpected error code: %d in revcomp().", status);
652 return status; /* NOTREACHED */
653 }
654