2 Interactive access to inverse folding routines
3 c Ivo Hofacker, Peter Stadler
6 /* Last changed Time-stamp: <2000-09-28 14:58:05 ivo> */
14 #include "fold_vars.h"
16 #include "part_func.h"
18 #include "read_epars.h"
19 #include "RNAinverse_cmdl.h"
22 #include "/usr/local/include/dmalloc.h"
23 #define space(X) calloc(1,(X))
27 static char rcsid[] = "$Id: RNAinverse.c,v 1.11 2000/09/28 12:59:21 ivo Rel $";
29 #define REPEAT_DEFAULT 100
31 PRIVATE void usage(void);
32 extern int inv_verbose;
34 int main(int argc, char *argv[]){
35 struct RNAinverse_args_info args_info;
37 char *input_string, *start, *structure, *rstart, *str2, *line;
38 char *ParamFile=NULL, *c, *ns_bases;
39 int i,j, length, l, hd, sym;
50 input_string = ns_bases = NULL;
54 #############################################
55 # check the command line parameters
56 #############################################
58 if(RNAinverse_cmdline_parser (argc, argv, &args_info) != 0) exit(1);
60 if(args_info.temp_given) temperature = args_info.temp_arg;
61 /* do not take special tetra loop energies into account */
62 if(args_info.noTetra_given) tetra_loop=0;
63 /* set dangle model */
64 if(args_info.dangles_given){
65 if((args_info.dangles_arg < 0) || (args_info.dangles_arg > 3))
66 warn_user("required dangle model not implemented, falling back to default dangles=2");
68 dangles = args_info.dangles_arg;
70 /* do not allow wobble pairs (GU) */
71 if(args_info.noGU_given) noGU = 1;
72 /* do not allow weak closing pairs (AU,GU) */
73 if(args_info.noClosingGU_given) no_closingGU = 1;
74 /* set energy model */
75 if(args_info.energyModel_given) energy_set = args_info.energyModel_arg;
76 /* take another energy parameter set */
77 if(args_info.paramFile_given) ParamFile = strdup(args_info.paramFile_arg);
78 /* Allow other pairs in addition to the usual AU,GC,and GU pairs */
79 if(args_info.nsp_given) ns_bases = strdup(args_info.nsp_arg);
80 /* alter the alphabet */
81 if(args_info.alphabet_given){
82 symbolset=args_info.alphabet_arg;
83 /* symbolset should only have uppercase characters */
84 for (l = 0; l < (int)strlen(symbolset); l++)
85 symbolset[l] = toupper(symbolset[l]);
87 /* set function for optimization */
88 if(args_info.function_given){
89 if(strlen(args_info.function_arg) > 2){
90 RNAinverse_cmdline_parser_print_help(); exit(EXIT_FAILURE);
93 if((*args_info.function_arg == 'm') || (*(args_info.function_arg+1) == 'm')) mfe = 1;
94 if((*args_info.function_arg == 'p') || (*(args_info.function_arg+1) == 'p')) pf = 1;
98 if(args_info.repeat_given) repeat = args_info.repeat_arg;
100 if(args_info.final_given) final_cost = args_info.final_arg;
101 /* do we wannabe verbose */
102 if(args_info.verbose_given) inv_verbose = 1;
104 /* free allocated memory of command line data structure */
105 RNAinverse_cmdline_parser_free (&args_info);
107 kT = (temperature+273.15)*1.98717/1000.0;
109 istty = (isatty(fileno(stdout))&&isatty(fileno(stdin)));
112 read_parameter_file(ParamFile);
114 give_up = (repeat<0);
118 ########################################################
119 # handle user input from 'stdin'
120 ########################################################
123 print_tty_input_seq_str("Input structure & start string\n"
124 "(lower case letters for const positions) and 0 or empty line for random start string\n");
126 input_type = get_multi_input_line(&input_string, 0);
127 /* we are waiting for a structure (i.e. something like a constraint) so we skip all sequences, fasta-headers and misc lines */
128 while(input_type & (VRNA_INPUT_SEQUENCE | VRNA_INPUT_MISC | VRNA_INPUT_FASTA_HEADER)){
129 if(!istty && (input_type & VRNA_INPUT_FASTA_HEADER)) printf(">%s\n", input_string);
130 free(input_string); input_string = NULL;
131 input_type = get_multi_input_line(&input_string, 0);
133 if(input_type & (VRNA_INPUT_QUIT | VRNA_INPUT_ERROR)) break;
135 if(input_type & (VRNA_INPUT_CONSTRAINT)){
136 structure = (char *)space(sizeof(char) * (strlen(input_string) + 1));
137 (void)sscanf(input_string, "%s", structure); /* scanf gets rid of trailing junk */
138 length = (int)strlen(structure);
139 free(input_string); input_string = NULL;
140 input_type = get_multi_input_line(&input_string, VRNA_INPUT_NOSKIP_BLANK_LINES | VRNA_INPUT_NOSKIP_COMMENTS);
142 if(input_type & VRNA_INPUT_QUIT) break;
144 start = (char *)space(sizeof(char) * (length+1));
145 /* now we assume to get a sequence (input_string may be empty as well) */
146 if(input_type & VRNA_INPUT_SEQUENCE){
147 (void)strncpy(start, input_string, length);
148 start[length] = '\0';
149 free(input_string); input_string = NULL;
151 /* fallback to empty start sequence */
152 else start[0] = '\0';
155 ########################################################
156 # done with 'stdin' handling
157 ########################################################
160 if (ns_bases != NULL) {
161 nonstandards = space(33);
169 nonstandards[i++]=*c++;
170 nonstandards[i++]=*c;
171 if ((sym)&&(*c!=*(c-1))) {
172 nonstandards[i++]=*c;
173 nonstandards[i++]=*(c-1);
180 str2 = (char *) space((unsigned)length+1);
181 if (istty) printf("length = %d\n", length);
183 if (repeat!=0) found = (repeat>0)? repeat : (-repeat);
186 /* initialize_fold(length); <- obsolete (hopefully commenting this out does not affect anything crucial ;) */
188 rstart = (char *) space((unsigned)length+1);
191 string = (char *) space((unsigned)length+1);
192 strcpy(string, start);
193 for (i=0; i<length; i++) {
194 /* lower case characters are kept fixed, any other character
195 not in symbolset is replaced by a random character */
196 if (islower(string[i])) continue;
198 if (string[i]=='\0' || (strchr(symbolset,string[i])==NULL))
199 string[i]=symbolset[int_urn(0,strlen(symbolset)-1)];
201 strcpy(rstart, string); /* remember start string */
204 energy = inverse_fold(string, structure);
205 if( (repeat>=0) || (energy<=0.0) ) {
207 hd = hamming(rstart, string);
208 printf("%s %3d", string, hd);
209 if (energy>0) { /* no solution found */
210 printf(" d= %g\n", energy);
212 energy = fold(string,str2);
213 printf("%s\n", str2);
219 if (!(mfe && give_up && (energy>0))) {
220 /* unless we gave up in the mfe part */
221 double prob, min_en, sfact=1.07;
223 /* get a reasonable pf_scale */
224 min_en = fold(string,str2);
225 pf_scale = exp(-(sfact*min_en)/kT/length);
226 /* init_pf_fold(length); <- obsolete (hopefully commenting this out does not affect anything crucial ;) */
228 energy = inverse_pf_fold(string, structure);
229 prob = exp(-energy/kT);
230 hd = hamming(rstart, string);
231 printf("%s %3d (%g)\n", string, hd, prob);
236 (void) fflush(stdout);
245 (void) fflush(stdout);
251 PRIVATE void usage(void)
253 nrerror("usage: RNAinverse [-F[mp]] [-a ALPHABET] [-R [repeats]] [-f final]\n"
254 " [-T temp] [-4] [-d[2]] [-noGU] [-P paramfile] [-e e_set] [-v]");