Debian Codegraph @

easel/esl_wuss.c

raw symbols SCIP
1 /* RNA secondary structure markup in WUSS notation.
2 *
3 * <> : base pairs in stem-loops, i.e. <<<___>>>
4 * () : base pairs in helices enclosing multifurcation of all <> stems
5 * [] : base pairs in helices enclosing multifurc of at least one () helix
6 * {} : base pairs in helices enclosing even deeper multifurcations
7 *
8 * _ : (i.e. underscore) nucleotide in a hairpin loop
9 * - : (i.e. dash) nucleotide in a bulge or interior loop
10 * , : nucleotide in a multifurcation loop (mnemonic: "stem1, stem2," )
11 * : : nucleotide in external single strand
12 *
13 * Aa : (and Bb, Cc, etc) pseudoknotted base pairs, upper case on left, lower case on right.
14 *
15 * and in alignments of a seq to an RNA structure profile, as in Infernal:
16 * . : insertion relative to a known consensus structure
17 * ~ : nucleotide is unaligned to a structure profile, because of local structure alignment
18 *
19 */
20 #include <esl_config.h>
21
22 #include <string.h>
23 #include <ctype.h>
24
25 #include "easel.h"
26 #include "esl_stack.h"
27 #include "esl_vectorops.h"
28 #include "esl_wuss.h"
29
30 /* Function: esl_wuss2ct()
31 * Incept: SRE, Tue Feb 15 08:44:54 2005 [St. Louis]
32 *
33 * Purpose: Given a secondary structure string <ss>, <0..len-1>,
34 * in WUSS notation, convert it to a CT array, <1..len>,
35 * in <ct>. Caller provides a <ct> allocated for at least
36 * <len+1> ints. <ct[i]> is the position that residue i
37 * base pairs to, or 0 if i is unpaired. <ct[0]> is undefined
38 * (but if you care: it is set to 0).
39 *
40 * WUSS notation is interpreted loosely here, as input
41 * WUSS. Any matching bracket pair or upper/lower case
42 * alphabetic pair is interpreted as a base pair; any other
43 * WUSS annotation is interpreted as unpaired.
44 *
45 * Returns: <eslOK> on success. Returns <eslESYNTAX> if the WUSS
46 * string isn't valid.
47 *
48 * Throws: <eslEMEM> on allocation failure.
49 */
50 int
51 esl_wuss2ct(char *ss, int len, int *ct)
52 {
53 ESL_STACK *pda[27]; /* 1 secondary structure + up to 26 levels of pk's */
54 int i;
55 int pos, pair;
56 int status; /* success or failure return status */
57
58 /* Initialization: always initialize the main pda (0);
59 * we'll init the pk pda's on demand.
60 */
61 for (i = 1; i <= 26; i++) pda[i] = NULL;
62 if ((pda[0] = esl_stack_ICreate()) == NULL) goto FINISH;
63
64 for (pos = 0; pos <= len; pos++) ct[pos] = 0;
65
66 for (pos = 1; pos <= len; pos++)
67 {
68 if (!isprint((int) ss[pos-1])) /* armor against garbage */
69 { status = eslESYNTAX; goto FINISH; }
70
71 /* left side of a pair: push position onto stack 0 (pos = 1..L) */
72 else if (ss[pos-1] == '<' ||
73 ss[pos-1] == '(' ||
74 ss[pos-1] == '[' ||
75 ss[pos-1] == '{')
76 {
77 if ((status = esl_stack_IPush(pda[0], pos)) != eslOK) goto FINISH;
78 }
79
80 /* right side of a pair; resolve pair; check for agreement */
81 else if (ss[pos-1] == '>' ||
82 ss[pos-1] == ')' ||
83 ss[pos-1] == ']' ||
84 ss[pos-1] == '}')
85 {
86 if (esl_stack_IPop(pda[0], &pair) == eslEOD)
87 { status = eslESYNTAX; goto FINISH;} /* no closing bracket */
88 else if ((ss[pair-1] == '<' && ss[pos-1] != '>') ||
89 (ss[pair-1] == '(' && ss[pos-1] != ')') ||
90 (ss[pair-1] == '[' && ss[pos-1] != ']') ||
91 (ss[pair-1] == '{' && ss[pos-1] != '}'))
92 { status = eslESYNTAX; goto FINISH; } /* brackets don't match */
93 else
94 {
95 ct[pos] = pair;
96 ct[pair] = pos;
97 }
98 }
99 /* same stuff for pseudoknots */
100 else if (isupper((int) ss[pos-1]))
101 {
102 /* Create the PK stacks on demand.
103 */
104 i = ss[pos-1] - 'A' + 1;
105 if (pda[i] == NULL)
106 if ((pda[i] = esl_stack_ICreate()) == NULL)
107 { status = eslEMEM; goto FINISH; }
108
109 if ((status = esl_stack_IPush(pda[i], pos)) != eslOK) goto FINISH;
110 }
111 else if (islower((int) ss[pos-1]))
112 {
113 i = ss[pos-1] - 'a' + 1;
114 if (pda[i] == NULL ||
115 esl_stack_IPop(pda[i], &pair) == eslEOD)
116 { status = eslESYNTAX; goto FINISH;}
117 else
118 {
119 ct[pos] = pair;
120 ct[pair] = pos;
121 }
122 }
123 else if (strchr(":,_-.~", ss[pos-1]) == NULL)
124 { status = eslESYNTAX; goto FINISH; } /* bogus character */
125 }
126 status = eslOK;
127
128 FINISH:
129 for (i = 0; i <= 26; i++)
130 if (pda[i] != NULL)
131 { /* nothing should be left on stacks */
132 if (esl_stack_ObjectCount(pda[i]) != 0)
133 status = eslESYNTAX;
134 esl_stack_Destroy(pda[i]);
135 }
136 return status;
137 }
138
139
140 /* Function: esl_ct2wuss()
141 * Incept: SRE, Wed Feb 16 11:22:53 2005 [St. Louis]
142 *
143 * Purpose: Convert a CT array <ct> for <n> residues (1..n) to a WUSS
144 * format string <ss>. <ss> must be allocated for at least
145 * n+1 chars (+1 for the terminal NUL).
146 *
147 * ER, Sat Aug 18 13:22:03 EDT 2012
148 * esl\_ct2wuss() extended to deal with pseudoknots structures.
149 * Pseudoknots are annotated as AA...aa, BB...bb,..., ZZ..zz.
150 * Attemting to convert a <ct> that requires more letters
151 * than [A-Z] will return an <eslEINVAL> error.
152 *
153 * Attempting to convert a <ct> that involves triplet interactions
154 * will return an <eslEINVAL> error.
155 *
156 * Returns: <eslOK> on success.
157 *
158 * Throws: <eslEMEM> on allocation failure.
159 * <eslEINCONCEIVABLE> on internal failure.
160 */
161 int
162 esl_ct2wuss(int *ct, int n, char *ss)
163 {
164 int rb[26]; // array that delimits the right bound of a pseudoknot character
165 ESL_STACK *pda = NULL; // stack for "main" secondary structure
166 ESL_STACK *auxpk = NULL; // aux stack for pseudoknot
167 ESL_STACK *auxss = NULL; // aux stack for single stranded
168 int *cct = NULL; // copy of ct vector
169 int nfaces; // number of faces in a cWW structure
170 int minface; // max depth of faces in a cWW structure
171 int leftbound, rightbound; // left and right bound to find basepairs belonging to a given pseudoknot
172 int xpk = 0; // number of pseudoknot characters used
173 // int npk = 0; // number of pseudoknots; commented out because we don't use it for anything right now
174 int npairs = 0; // total number of basepairs
175 int npairs_reached = 0; // number of basepairs found so far
176 int found_partner; // true if we've found left partner of a given base in stack pda
177 int i,j,k; // sequence indices
178 int x; // index for pseudoknot characters
179 int status = eslEMEM; // exit status 'til proven otherwise
180
181 /* total number of basepairs */
182 for (j = 1; j <= n; j ++) { if (ct[j] > 0 && j < ct[j]) npairs ++; }
183
184 /* Copy of ct; if a pseudoknotted structure, cct will be modified later.
185 */
186 ESL_ALLOC(cct, sizeof(int)*(n+1));
187 esl_vec_ICopy(ct, (n+1), cct);
188
189 /* Initialize rightbounds for all 26 pseudoknot indices */
190 for (x = 0; x < 26; x ++) rb[x] = -1;
191
192 /* init ss[] to single stranded */
193 for (j = 0; j < n; j ++) { ss[j] = ':'; }
194 ss[n] = '\0';
195
196 /* Initialization*/
197 if ((pda = esl_stack_ICreate()) == NULL) goto FINISH;
198 if ((auxpk = esl_stack_ICreate()) == NULL) goto FINISH;
199 if ((auxss = esl_stack_ICreate()) == NULL) goto FINISH;
200
201 for (j = 1; j <= n; j++)
202 {
203 if (cct[j] == 0) /* unpaired: push j. */
204 {
205 if (esl_stack_IPush(pda, j) != eslOK) goto FINISH;
206 }
207 else if (cct[j] > j) /* left side of a bp: push j. */
208 {
209 if (esl_stack_IPush(pda, j) != eslOK) goto FINISH;
210 }
211 else /* right side of a bp; main routine: fingh the left partner */
212 {
213 found_partner = FALSE;
214 /* Pop back until we find the left partner of j;
215 * In case this is not a nested structure, finding
216 * the left partner of j will require to put bases
217 * aside into stack auxpk.
218 *
219 * After we find the left partner of j,
220 * store single stranded residues in auxss;
221 * keep track of #faces and the maximum face depth.
222 */
223 nfaces = 0;
224 minface = -1;
225
226 while (esl_stack_ObjectCount(pda))
227 {
228 if (esl_stack_IPop(pda, &i) != eslOK) goto FINISH;
229
230 if (i < 0) /* a face counter */
231 {
232 nfaces++;
233 if (i < minface) minface = i;
234 }
235
236 else if (cct[i] == j) /* we found the i,j pair. */
237 {
238 found_partner = TRUE;
239 npairs_reached ++;
240 /* Now we know i,j pair; and we know how many faces are
241 * above them; and we know the max depth of those faces.
242 * That's enough to label the pair in WUSS notation.
243 * if nfaces == 0, minface is -1; <> a closing bp of a hairpin.
244 * if nfaces == 1, inherit minface, we're continuing a stem.
245 * if nfaces > 1, bump minface in depth; we're closing a bifurc.
246 */
247 if (nfaces > 1 && minface > -4) minface--;
248 switch (minface) {
249 case -1: ss[i-1] = '<'; ss[j-1] = '>'; break;
250 case -2: ss[i-1] = '('; ss[j-1] = ')'; break;
251 case -3: ss[i-1] = '['; ss[j-1] = ']'; break;
252 case -4: ss[i-1] = '{'; ss[j-1] = '}'; break;
253 default:
254 esl_stack_Destroy(pda); esl_stack_Destroy(auxpk); esl_stack_Destroy(auxss); free(cct);
255 ESL_EXCEPTION(eslEINCONCEIVABLE, "no such face code");
256 }
257 if (esl_stack_IPush(pda, minface) != eslOK) goto FINISH;
258
259 /* Now, aux contains all the unpaired residues we need to label,
260 * according to the # of faces "above" them:
261 * nfaces = 0: hairpin loop
262 * nfaces = 1: bulge or interior loop
263 * nfaces > 1: multifurc
264 */
265 while (esl_stack_IPop(auxss, &i) == eslOK)
266 {
267 switch (nfaces) {
268
269 case 0: ss[i-1] = '_'; break;
270 case 1: ss[i-1] = '-'; break;
271 default: ss[i-1] = ','; break; /* nfaces > 1 */
272 }
273 }
274 break;
275 }
276
277 else if (cct[i] == 0)
278 {
279 /* add to auxss only if originally sigle stranded */
280 if (ct[i] == 0) { if (esl_stack_IPush(auxss, i) != eslOK) goto FINISH; }
281 }
282
283 else /* cct[i]>0, != j: i is paired, but not to j: pseudoknot! */
284 {
285 /* i is in the way to find j's left partner.
286 * Move i to stack auxpk; resolve pseudoknot(s) after we've found partern for j.
287 */
288 if (esl_stack_IPush(auxpk, i) != eslOK) goto FINISH;
289 }
290 }
291
292 if (!found_partner) {
293 esl_stack_Destroy(pda); esl_stack_Destroy(auxpk); esl_stack_Destroy(auxss); free(cct);
294 ESL_EXCEPTION(eslEINVAL, "Cannot find left partner (%d) of base %d. Likely a triplet", ct[j], j);
295 }
296 } /* finished finding the left partner of j */
297
298 /* After we've found the left partner of j, resolve pks found along the way.
299 * Then, remove the pseudoknotted based from cct so we can find the rest of the structure.
300 */
301 if (esl_stack_ObjectCount(auxpk)) {
302
303 /* init for first pseudoknot */
304 leftbound = cct[j];
305 rightbound = leftbound + 1;
306 xpk = -1; /* start with 'A' if possible again */
307
308 while (esl_stack_IPop(auxpk, &i) == eslOK) {
309
310 for (k = rightbound-1; k > leftbound; k --)
311 {
312 if (cct[k] == 0) { continue; }
313 else if (cct[k] > rightbound) { continue; }
314 else if (cct[k] == i) { break; } /* i continues the given pseudoknot */
315 else { k = leftbound; break; } /* a new pseudoknot */
316 }
317
318 if (k == leftbound) /* a new pseudoknot */
319 {
320 // npk ++; // if you wanted to count PK's, here's where to do it
321 xpk ++;
322 /* figure out if we can use this alphabet index, or bump it up if necessary */
323 while (i < rb[xpk]) { xpk ++; }
324
325 leftbound = (rightbound < cct[i])? rightbound : cct[j];
326 rightbound = cct[i];
327 }
328
329 npairs_reached ++;
330 if (xpk+(int)('a') <= (int)('z')) {
331
332 /* update the rightbound of this pk index if necessary */
333 if (cct[i] > rb[xpk]) rb[xpk] = cct[i];
334
335 /* Add pk indices for this basepair */
336 ss[i-1] = (char)(xpk+(int)('A'));
337 ss[cct[i]-1] = (char)(xpk+(int)('a'));
338
339 /* remove pseudoknotted pair from cct */
340 cct[i] = 0;
341 cct[ct[i]] = 0;
342 }
343 else ESL_EXCEPTION(eslEINVAL, "Don't have enough letters to describe all different pseudoknots.");
344
345 }
346 } /* while there is something in auxpk stack */
347
348 } /* finished loop over j: end position on seq, 1..n*/
349
350 status = eslOK;
351
352 ERROR:
353 FINISH:
354 if (npairs != npairs_reached)
355 ESL_EXCEPTION(eslFAIL, "found %d out of %d pairs.", npairs_reached, npairs);
356 if (pda != NULL) esl_stack_Destroy(pda);
357 if (auxpk != NULL) esl_stack_Destroy(auxpk);
358 if (auxss != NULL) esl_stack_Destroy(auxss);
359 if (cct != NULL) free(cct);
360 return status;
361 }
362
363 /* Function: esl_ct2simplewuss()
364 * Incept: ER, Wed Aug 22 13:31:54 EDT 2012 [Janelia]
365 *
366 * Purpose: Convert a CT array <ct> for <n> residues (1..n) to a simple WUSS
367 * format string <ss>. <ss> must be allocated for at least
368 * n+1 chars (+1 for the terminal NUL).
369 *
370 * This function can be used with the <ct> of a secondary
371 * structure including arbitrary pseudoknots, or for the
372 * <ct> or a tertiary structure (say cWH, tWH, cSS,... H bonds).
373 *
374 * The string <ss> has basepairs annotated as <>, Aa, Bb, ..., Zz;
375 * unpaired bases are annotated as '.'.
376 *
377 * Attemting to convert a <ct> that requires more letters
378 * than [A-Z] will return an <eslEINVAL> error.
379 *
380 * Attempting to convert a <ct> that involves triplet interactions
381 * will return an <eslEINVAL> error.
382 *
383 * Returns: <eslOK> on success.
384 *
385 * Throws: <eslEMEM> on allocation failure.
386 * <eslEINCONCEIVABLE> on internal failure.
387 */
388 int
389 esl_ct2simplewuss(int *ct, int n, char *ss)
390 {
391 int rb[26]; // array that delimits the right bound of a pseudoknot character
392 ESL_STACK *pda = NULL; // stack for "main" secondary structure
393 ESL_STACK *auxpk = NULL; // aux stack for pseudoknot
394 int *cct = NULL; // copy of ct vector
395 int leftbound, rightbound; // left and right bound to find basepairs belonging to a given pseudoknot
396 int xpk = 0; // number of pseudoknot characters used
397 // int npk = 0; // number of pseudoknots; commented out because we don't use it for anything right now
398 int npairs = 0; // total number of basepairs
399 int npairs_reached = 0; // number of basepairs found so far
400 int found_partner; // true if we've found left partner of a given base in stack pda
401 int i,j,k; // sequence indices
402 int x; // index for pseudoknot characters
403 int status = eslEMEM; // exit status 'til proven otherwise
404
405 /* total number of basepairs */
406 for (j = 1; j <= n; j ++) { if (ct[j] > 0 && j < ct[j]) npairs ++; }
407
408 /* Copy of ct; if a pseudoknotted structure, cct will be modified later.
409 */
410 ESL_ALLOC(cct, sizeof(int)*(n+1));
411 esl_vec_ICopy(ct, (n+1), cct);
412
413 /* Initialize rightbounds for all 26 pseudoknot indices */
414 for (x = 0; x < 26; x ++) rb[x] = -1;
415
416 /* init ss[] to single stranded */
417 for (j = 0; j < n; j ++) { ss[j] = '.'; }
418 ss[n] = '\0';
419
420 /* Initialization*/
421 if ((pda = esl_stack_ICreate()) == NULL) goto FINISH;
422 if ((auxpk = esl_stack_ICreate()) == NULL) goto FINISH;
423
424 for (j = 1; j <= n; j++)
425 {
426 if (cct[j] == 0) /* unpaired: push j. */
427 {
428 if (esl_stack_IPush(pda, j) != eslOK) goto FINISH;
429 }
430 else if (cct[j] > j) /* left side of a bp: push j. */
431 {
432 if (esl_stack_IPush(pda, j) != eslOK) goto FINISH;
433 }
434 else /* right side of a bp; main routine: fingh the left partner */
435 {
436 found_partner = FALSE;
437
438 /* Pop back until we find the left partner of j;
439 * In case this is not a nested structure, finding
440 * the left partner of j will require to put bases
441 * aside into stack auxpk.
442 */
443 while (esl_stack_ObjectCount(pda))
444 {
445 if (esl_stack_IPop(pda, &i) != eslOK) goto FINISH;
446
447 if (cct[i] == j) /* we found the i,j pair. */
448 {
449 found_partner = TRUE;
450 npairs_reached ++;
451
452 ss[i-1] = '<';
453 ss[j-1] = '>';
454 break;
455 }
456
457 else if (cct[i] == 0)
458 {
459 if (ct[i] == 0) ss[i-1] = '.';
460 }
461
462 else /* cct[i]>0, != j: i is paired, but not to j: pseudoknot! */
463 {
464 /* i is in the way to find j's left partner.
465 * Move i to stack auxpk; resolve pseudoknot(s) after we've found partern for j.
466 */
467 if (esl_stack_IPush(auxpk, i) != eslOK) goto FINISH;
468 }
469 }
470
471 if (!found_partner) {
472 esl_stack_Destroy(pda); esl_stack_Destroy(auxpk); free(cct);
473 ESL_EXCEPTION(eslEINVAL, "Cannot find left partner (%d) of base %d. Likely a triplet", ct[j], j);
474 }
475 } /* finished finding the left partner of j */
476
477 /* After we've found the left partner of j, resolve pks found along the way.
478 * Then, remove the pseudoknotted based from cct so we can find the rest of the structure.
479 */
480 if (esl_stack_ObjectCount(auxpk)) {
481
482 /* init for first pseudoknot */
483 leftbound = cct[j];
484 rightbound = leftbound + 1;
485 xpk = -1; /* start with 'A' if possible again */
486
487 while (esl_stack_IPop(auxpk, &i) == eslOK) {
488
489 for (k = rightbound-1; k > leftbound; k --)
490 {
491 if (cct[k] == 0) { continue; }
492 else if (cct[k] > rightbound) { continue; }
493 else if (cct[k] == i) { break; } /* i continues the given pseudoknot */
494 else { k = leftbound; break; } /* a new pseudoknot */
495 }
496
497 if (k == leftbound) /* a new pseudoknot */
498 {
499 // npk ++; // if you want to count PK's, here's where to do it
500 xpk ++;
501 /* figure out if we can use this alphabet index, or bump it up if necessary */
502 while (i < rb[xpk]) { xpk ++; }
503
504 leftbound = (rightbound < cct[i])? rightbound : cct[j];
505 rightbound = cct[i];
506 }
507
508 npairs_reached ++;
509 if (xpk+(int)('a') <= (int)('z')) {
510
511 /* update the rightbound of this pk index if necessary */
512 if (cct[i] > rb[xpk]) rb[xpk] = cct[i];
513
514 /* Add pk indices for this basepair */
515 ss[i-1] = (char)(xpk+(int)('A'));
516 ss[cct[i]-1] = (char)(xpk+(int)('a'));
517
518 /* remove pseudoknotted pair from cct */
519 cct[i] = 0;
520 cct[ct[i]] = 0;
521 }
522 else ESL_EXCEPTION(eslEINVAL, "Don't have enough letters to describe all different pseudoknots.");
523
524 }
525 } /* while there is something in auxpk stack */
526
527 } /* finished loop over j: end position on seq, 1..n*/
528
529 status = eslOK;
530
531 ERROR:
532 FINISH:
533 if (npairs != npairs_reached)
534 ESL_EXCEPTION(eslFAIL, "found %d out of %d pairs.", npairs_reached, npairs);
535 if (pda != NULL) esl_stack_Destroy(pda);
536 if (auxpk != NULL) esl_stack_Destroy(auxpk);
537 if (cct != NULL) free(cct);
538 return status;
539 }
540
541 /* Function: esl_wuss2kh()
542 * Incept: SRE, Tue Feb 15 10:05:35 2005 [St. Louis]
543 *
544 * Purpose: Converts a secondary structure string <ss> in
545 * WUSS notation back to old KHS format in <kh>.
546 * <kh> must be allocated for at least as much
547 * space as <ss>. <kh> may be the same as <ss>,
548 * in which case the conversion is done in-place.
549 *
550 * Note: Left bp chars are converted to > (left base of base pairs)
551 * Right bp chars are converted to < (right base of base pairs)
552 * Characters _-,:~ are converted to . (unpaired bases)
553 * Character . is untouched (unpaired)
554 * Everything else is untouched, including any pseudoknot notation.
555 *
556 * Returns: <eslOK> on success.
557 */
558 int
559 esl_wuss2kh(char *ss, char *kh)
560 {
561 while (*ss != '\0')
562 {
563 if (*ss == '<') *kh = '>';
564 else if (*ss == '(') *kh = '>';
565 else if (*ss == '[') *kh = '>';
566 else if (*ss == '{') *kh = '>';
567 else if (*ss == '>') *kh = '<';
568 else if (*ss == ')') *kh = '<';
569 else if (*ss == ']') *kh = '<';
570 else if (*ss == '}') *kh = '<';
571 else if (*ss == '_') *kh = '.';
572 else if (*ss == '-') *kh = '.';
573 else if (*ss == ',') *kh = '.';
574 else if (*ss == ':') *kh = '.';
575 else if (*ss == '~') *kh = '.';
576 else *kh = *ss;
577 ss++;
578 kh++;
579 }
580 *kh = '\0';
581 return eslOK;
582 }
583
584
585 /* Function: esl_kh2wuss()
586 * Incept: SRE, Tue Feb 15 10:10:40 2005 [St. Louis]
587 *
588 * Purpose: Converts an old format secondary structure string <kh>
589 * to shorthand WUSS format <ss>. <ss> must be allocated at least
590 * as large as <kh>. <ss> can be identical to <kh>, in which
591 * case the conversion is done in-place.
592 *
593 * Note: Character > is converted to < (left base of base pairs)
594 * Character < is converted to > (right base of base pairs)
595 * A space is converted to . (just in case)
596 *
597 * Returns: <eslOK> on success.
598 */
599 int
600 esl_kh2wuss(char *kh, char *ss)
601 {
602 while (*kh != '\0')
603 {
604 if (*kh == '>') *ss = '<';
605 else if (*kh == '<') *ss = '>';
606 else if (*kh == ' ') *ss = '.';
607 else *ss = *kh;
608 kh++;
609 ss++;
610 }
611 *ss = '\0';
612 return eslOK;
613 }
614
615
616 /* Function: esl_wuss_full()
617 * Incept: SRE, Mon Feb 28 09:44:40 2005 [St. Louis]
618 *
619 * Purpose: Given a simple ("input") WUSS format annotation string <oldss>,
620 * convert it to full ("output") WUSS format in <newss>.
621 * <newss> must be allocated by the caller to be at least as
622 * long as <oldss>. <oldss> and <newss> can be the same,
623 * to convert a secondary structure string in place.
624 *
625 * Pseudoknot annotation is preserved, if <oldss> had it.
626 *
627 * Returns: <eslSYNTAX> if <oldss> isn't in valid WUSS format.
628 *
629 * Throws: <eslEMEM> on allocation failure.
630 * <eslEINCONCEIVABLE> on internal error that can't happen.
631 */
632 int
633 esl_wuss_full(char *oldss, char *newss)
634 {
635 char *tmp = NULL;
636 int *ct = NULL;
637 int n;
638 int i;
639 int status;
640
641 /* We can use the ct2wuss algorithm to generate a full WUSS string -
642 * convert to ct, then back to WUSS. ct2wuss doesn't deal with pk's
643 * though, and we want to propagate pk annotation if it's there. So
644 * we need two workspaces: ct array, and a temporary ss string that
645 * we use to hold non-pk annotation. As a final step, we overlay
646 * the pk annotation from the original oldss annotation.
647 */
648 n = strlen(oldss);
649 ESL_ALLOC(ct, sizeof(int) * (n+1));
650 ESL_ALLOC(tmp, sizeof(char) * (n+1));
651
652 esl_wuss_nopseudo(oldss, tmp);/* tmp = nonpseudoknotted oldss */
653
654 status = esl_wuss2ct(tmp, n, ct); /* ct = oldss in ct format, no pks */
655 if (status != eslOK) goto ERROR;
656
657 status = esl_ct2wuss(ct, n, tmp); /* now tmp is a full WUSS string */
658 if (status == eslEINVAL) { status = eslEINCONCEIVABLE; goto ERROR; }/* we're sure, no pk's */
659 else if (status != eslOK) goto ERROR; /* EMEM, EINCONCEIVABLE */
660
661 for (i = 0; i < n; i++)
662 if (isalpha(oldss[i])) newss[i] = oldss[i]; /* transfer pk annotation */
663 else newss[i] = tmp[i]; /* transfer new WUSS */
664
665 free(ct);
666 free(tmp);
667 return eslOK;
668
669 ERROR:
670 free(ct);
671 free(tmp);
672 return status;
673 }
674
675
676
677 /* Function: esl_wuss_nopseudo()
678 * Incept: SRE, Tue Feb 15 11:02:43 2005 [St. Louis]
679 *
680 * Purpose: Given a WUSS format annotation string <ss1>,
681 * removes all pseudoknot annotation to create a new
682 * WUSS string <ss2> that contains only a "canonical"
683 * (nonpseudoknotted) structure. <ss2> must be allocated to
684 * be at least as large as <ss1>. <ss1> and <ss2>
685 * may be the same, in which case the conversion is
686 * done in place. Pseudoknot annotation in <ss1> is
687 * simply replaced by <.> in <ss2>; the resulting
688 * <ss2> WUSS string is therefore in valid simplified format,
689 * but may not be valid full format WUSS.
690 *
691 * Returns: <eslOK>.
692 */
693 int
694 esl_wuss_nopseudo(char *ss1, char *ss2)
695 {
696 while (*ss1 != '\0')
697 {
698 if (isalpha(*ss1)) *ss2 = '.';
699 else *ss2 = *ss1;
700 ss1++;
701 ss2++;
702 }
703 *ss2 = '\0';
704 return eslOK;
705 }
706
707
708 /* Function: esl_wuss_reverse()
709 * Synopsis: "Reverse complement" a WUSS annotation
710 * Incept: SRE, Wed Feb 10 12:46:51 2016 [JB251 BOS-MCO]
711 *
712 * Purpose: If we need to reverse complement a structure-annotated RNA
713 * sequence, we need to "reverse complement" the WUSS
714 * annotation string. Reverse complement the annotation string
715 * <ss> into caller-provided space <new>. To revcomp an annotation
716 * in place, use <esl_wuss_reverse(ss, ss)>.
717 *
718 * Old SELEX files use a different structure annotation
719 * format, with angle brackets pointing the opposite
720 * direction: \ccode{><} for a base pair. As a convenient
721 * side effect, <esl_wuss_reverse()> will also reverse
722 * complement SELEX annotation lines.
723 *
724 * Returns: <eslOK> on success.
725 */
726 int
727 esl_wuss_reverse(char *ss, char *new)
728 {
729 int i, n;
730
731 /* first, "complement" the annotation */
732 for (i = 0; ss[i] != '\0'; i++)
733 {
734 if (isupper(ss[i])) new[i] = tolower(ss[i]);
735 else if (islower(ss[i])) new[i] = toupper(ss[i]);
736 else {
737 switch (ss[i]) {
738 case '<': new[i] = '>'; break;
739 case '>': new[i] = '<'; break;
740 case '(': new[i] = ')'; break;
741 case ')': new[i] = '('; break;
742 case '[': new[i] = ']'; break;
743 case ']': new[i] = '['; break;
744 case '{': new[i] = '}'; break;
745 case '}': new[i] = '{'; break;
746 default: new[i] = ss[i]; break;
747 }
748 }
749 }
750 n = i;
751 /* Then, reverse it in place. */
752 for (i = 0; i < n/2; i++)
753 ESL_SWAP(new[i], new[n-i-1], char);
754
755 return eslOK;
756 }
757
758
759 #ifdef eslWUSS_TESTDRIVE
760 /* gcc -g -Wall -o testwuss -I. -DeslWUSS_TESTDRIVE esl_wuss.c esl_random.c esl_stack.c esl_vectorops.c easel.c
761 * ./testwuss
762 */
763
764 #include <stdlib.h>
765
766 #include "easel.h"
767 #include "esl_random.h"
768 #include "esl_stack.h"
769 #include "esl_vectorops.h"
770 #include "esl_wuss.h"
771
772 int
773 main(int argc, char **argv)
774 {
775 /* The example is E. coli RNase P, w/ and w/o pks.
776 * J Brown figure 10.3.00 shows 1 too many bp for pk stem A.
777 */
778 char ss[] = "\
779 {{{{{{{{{{{{{{{{{{,<<<<<<<<<<<<<-<<<<<____>>>>>>>>>->>>>>>>>\
780 >,,,,AAA-AAAAA[[[[---BBBB-[[[[[<<<<<_____>>>>><<<<____>>>->(\
781 (---(((((,,,,,,,,,,,,<<<<<--<<<<<<<<____>>>>>->>>>>>-->>,,,,\
782 ,,,<<<<<<_______>>>>>><<<<<<<<<____>>>->>>>>->,,)))--))))]]]\
783 ]]]]]],,,<<<<------<<<<<<----<<<<<_bbbb>>>>>>>>>>>----->>>>,\
784 ,,,,,<<<<<<<<____>>>>>>>>,,,,,,,,,,}}}}}}}----------aaaaaaaa\
785 -}-}}}}}}}}}}::::";
786 char ss_nopk[] = "\
787 {{{{{{{{{{{{{{{{{{,<<<<<<<<<<<<<-<<<<<____>>>>>>>>>->>>>>>>>\
788 >,,,,,,,,,,,,,[[[[--------[[[[[<<<<<_____>>>>><<<<____>>>->(\
789 (---(((((,,,,,,,,,,,,<<<<<--<<<<<<<<____>>>>>->>>>>>-->>,,,,\
790 ,,,<<<<<<_______>>>>>><<<<<<<<<____>>>->>>>>->,,)))--))))]]]\
791 ]]]]]],,,<<<<------<<<<<<----<<<<<_____>>>>>>>>>>>----->>>>,\
792 ,,,,,<<<<<<<<____>>>>>>>>,,,,,,,,,,}}}}}}}------------------\
793 -}-}}}}}}}}}}::::";
794 int len;
795 int *ct1 = NULL;
796 int *ct2 = NULL;
797 char *ss2 = NULL;
798 char *ss3 = NULL;
799 int i;
800 int nbp, nbp_true, npk;
801 int status;
802
803 len = strlen(ss);
804 ESL_ALLOC(ct1, sizeof(int) * (len+1));
805 ESL_ALLOC(ct2, sizeof(int) * (len+1));
806 ESL_ALLOC(ss2, sizeof(char) * (len+1));
807 ESL_ALLOC(ss3, sizeof(char) * (len+1));
808 nbp_true = npk = 0;
809 for (i = 0; i < len; i++)
810 {
811 if (strchr("{[(<", ss[i]) != NULL)
812 nbp_true++;
813 if (isupper(ss[i]))
814 npk++;
815 }
816
817 if (esl_wuss2ct(ss, len, ct1) != eslOK) abort();
818 nbp = 0;
819 for (i = 1; i <= len; i++)
820 if (ct1[i] > i) nbp++;
821 if (nbp != nbp_true + npk) abort();
822
823 if (esl_wuss2kh(ss, ss) != eslOK) abort();
824 if (esl_kh2wuss(ss, ss) != eslOK) abort();
825 if (esl_wuss2ct(ss, len, ct2) != eslOK) abort();
826 for (i = 1; i <= len; i++)
827 if (ct1[i] != ct2[i]) abort();
828
829 /* test of pseudoknots */
830 if (esl_ct2wuss(ct1, len, ss2) != eslOK) abort();
831 if (esl_wuss2ct(ss2, len, ct2) != eslOK) abort();
832 for (i = 1; i <= len; i++)
833 if (ct1[i] != ct2[i]) abort();
834
835 if (esl_ct2simplewuss(ct1, len, ss2) != eslOK) abort();
836 if (esl_wuss2ct(ss2, len, ct2) != eslOK) abort();
837 for (i = 1; i <= len; i++)
838 if (ct1[i] != ct2[i]) abort();
839
840 if (esl_wuss_nopseudo(ss, ss) != eslOK) abort();
841 if (esl_wuss2ct(ss, len, ct1) != eslOK) abort();
842 if (esl_wuss2ct(ss_nopk, len, ct2) != eslOK) abort();
843 for (i = 1; i <= len; i++)
844 if (ct1[i] != ct2[i]) abort();
845
846 if (esl_wuss2ct(ss_nopk, len, ct1) != eslOK) abort();
847 if (esl_ct2wuss(ct1, len, ss3) != eslOK) abort();
848 if (strcmp(ss_nopk, ss3) != 0) abort();
849
850 free(ct1);
851 free(ct2);
852 free(ss2);
853 free(ss3);
854 return 0;
855
856 ERROR:
857 free(ct1);
858 free(ct2);
859 free(ss2);
860 free(ss3);
861 return status;
862 }
863 #endif /*eslWUSS_TESTDRIVE*/
864
865
866
867