1 /* Last Changed Time-stamp: <2001-09-07 10:17:47 ivo> */
2 /* This program converts the bracket notation for RNA secondary structures
3 produced by RNAfold to .ct files used by Michael Zukers Program.
7 b2ct < structure_file > ct_file.ct
9 RNAfold < sequence_file | b2ct > ct_file.ct
15 #define MAXLENGTH 30000
17 void write_ct_file(char *fname, char *sequence, char *structure, char *name,
19 short *make_pair_table(const char *structure);
20 void *space(unsigned size);
21 void nrerror(char *message);
25 char line[MAXLENGTH+1];
26 char *string=NULL, *structure=NULL, *name=NULL;
30 while (fgets(line, MAXLENGTH, stdin)!=NULL) {
31 if (strcmp(line,"@")==0) break;
34 case '>': name = (char *) space(strlen(line));
35 sscanf(line,"> %s", name);
39 case ')': structure = (char *) space(strlen(line));
40 if (sscanf(line,"%s (%f)", structure, &energy)!=2) {
41 free(structure); structure=NULL; break;
45 default: string = (char *) space(strlen(line)+1);
46 sscanf(line, "%s", string);
48 if (structure!=NULL) {
50 name = (char *) space(10);
53 write_ct_file("-", string, structure, name, energy);
57 string = structure = name = NULL;
63 void write_ct_file(char *fname, char *sequence, char *structure, char *name,
70 length = strlen(structure);
71 if ((table = make_pair_table(structure))==NULL) {
74 if (length!=strlen(sequence))
75 nrerror("sequence and structure have unequal length");
77 if (strcmp(fname,"-")==0)
80 ct = fopen(fname, "a");
81 if (ct==NULL) nrerror("can't open .ct file");
84 fprintf(ct, "%5d ENERGY = %7.1f %s\n", length, energy, name);
85 for (i=1; i<=length; i++)
86 fprintf(ct, "%5d %c %5d %4d %4d %4d\n",
87 i, sequence[i-1], i-1, (i+1)%(length+1), table[i], i);
88 if (strcmp(fname,"-"))
93 short *make_pair_table(const char *structure)
95 /* returns array representation of structure.
96 table[i] is 0 if unpaired or j if (i.j) pair. */
102 length = strlen(structure);
103 stack = (short *) space(sizeof(short)*(length+1));
104 table = (short *) space(sizeof(short)*(length+2));
107 for (hx=0, i=1; i<=length; i++) {
108 switch (structure[i-1]) {
115 fprintf(stderr, "unbalanced brackets in %s\n", structure);
116 free(stack); free(table); return NULL;
121 default: /* unpaired base, usually '.' */
128 fprintf(stderr, "unbalanced brackets %s\n", structure);
135 void *space(unsigned size)
139 if ( (pointer = (void *) calloc(1, size)) == NULL) {
140 fprintf(stderr,"SPACE: requested size: %d\n", size);
141 nrerror("SPACE allocation failure -> no memory");
147 void nrerror(char *message) /* output message upon error */
149 fprintf(stderr, "\n%s\n", message);