Replace Progs/RNAalifold with x64 binary and add all other programs
[jabaws.git] / binaries / src / ViennaRNA / Kinfold / main.c
1 /*
2   Last changed Time-stamp: <2010-06-24 14:50:01 ivo>
3   c  Christoph Flamm and Ivo L Hofacker
4   {xtof,ivo}@tbi.univie.ac.at
5   Kinfold: $Name:  $
6   $Id: main.c,v 1.5 2008/08/28 09:40:55 ivo Exp $
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <assert.h>
13 #include <ctype.h>
14 #include <unistd.h>
15 #include "fold_vars.h"
16 #include "fold.h"
17 #include "baum.h"
18 #include "nachbar.h"
19 #include "cache_util.h"
20 #include "utils.h"
21 #include "globals.h"
22
23 static char UNUSED rcsid[] ="$Id: main.c,v 1.5 2008/08/28 09:40:55 ivo Exp $";
24 extern void  read_parameter_file(const char fname[]);
25 extern void get_from_cache(cache_entry *c);
26
27 /* PRIVAT FUNCTIONS */
28 static void ini_energy_model(void);
29 static void read_data(void);
30 static void clean_up(void);
31
32 /**/
33 int main(int argc, char *argv[]) {
34   int i;
35   char * start;
36   
37   /*
38     process command-line optiones
39   */
40   decode_switches(argc, argv);
41
42   /*
43     initialize energy parameters
44   */
45   ini_energy_model();
46   
47   /*
48     read input file
49   */
50   read_data();
51
52   /*
53     perform GSV.num simulations
54   */
55     
56   start = strdup(GAV.startform); /* remember startform for next run */
57   for (i = 0; i < GSV.num; i++) {
58
59     /*
60       initialize or reset ringlist to start conditions
61     */
62     ini_or_reset_rl();
63     if (GSV.grow>0) {
64       if (strlen(GAV.farbe)>GSV.glen) {
65         start[GSV.glen] = '\0';
66         GAV.farbe[GSV.glen] = '\0';
67         strcpy(GAV.startform,start);
68         strcpy(GAV.currform,start);
69         GSV.len=GSV.glen;
70       }
71       clean_up_rl();
72       ini_or_reset_rl();
73     }
74
75     /*
76       perform simulation
77     */
78     for (GSV.steps = 1;; GSV.steps++) {
79       cache_entry *c;
80
81       /*
82         take neighbourhood of current structure from cache if there
83         else generate it from scratch
84       */
85       if ( (c = lookup_cache(GAV.currform)) ) get_from_cache(c);
86       else move_it();
87         
88       /*
89         select a structure from neighbourhood of current structure
90         and make it to the new current structure.
91         stop simulation if stop condition is met.
92       */
93       if ( sel_nb() > 0 ) break;
94
95       /* if (GSV.grow>0) grow_chain(); */
96     }
97   }
98   
99   /*
100     clean up memory
101   */
102   free(start);
103   clean_up();
104   return(0);
105 }
106
107 /**/
108 static void ini_energy_model(void) {
109   if ( !GTV.seed ) {
110     init_rand();
111     GAV.subi[0] = xsubi[0];
112     GAV.subi[1] = xsubi[1];
113     GAV.subi[2] = xsubi[2];
114   }
115   else {
116     xsubi[0] = GAV.subi[0];
117     xsubi[1] = GAV.subi[1];
118     xsubi[2] = GAV.subi[2];
119   }
120   logML = GTV.logML;
121   dangles = GTV.dangle;
122   temperature = GSV.Temp;
123   if( GTV.Par ) read_parameter_file(GAV.ParamFile);
124   update_fold_params();
125 }
126
127 /**/
128 static void read_data(void) {
129   char *ctmp, *c, **s;
130   int i, len;
131
132   /*
133     read sequence
134   */
135   ctmp = get_line(stdin);
136   len = strlen(ctmp);
137   if (c=strchr(ctmp, '&')) {
138     cut_point = (int)(c-ctmp)+1;
139     for (; *c; c++) *c = *(c+1); /* splice out the & */
140   }
141   GAV.farbe = (char *)calloc(len+1, sizeof(char));
142   assert(GAV.farbe != NULL);
143   sscanf(ctmp, "%s", GAV.farbe);
144   GSV.len = strlen(GAV.farbe);
145   for (i = 0; i < len; i++) GAV.farbe[i] = toupper(GAV.farbe[i]);
146   free (ctmp);
147   /* allocate some global arrays */
148   GAV.currform = (char *)calloc(GSV.len +1, sizeof(char));
149   assert(GAV.currform != NULL);
150   GAV.prevform = (char *)calloc(GSV.len +1, sizeof(char));
151   assert(GAV.prevform != NULL);
152   GAV.startform = (char *)calloc(GSV.len +1, sizeof(char));
153   assert(GAV.startform != NULL);
154
155   /*
156     read start structure
157   */
158   if (GTV.start) {
159     ctmp = get_line(stdin);
160     len = strlen(ctmp);
161     sscanf(ctmp, "%s", GAV.startform);
162     if (c=strchr(GAV.startform, '&')) {
163       for (; *c; c++) *c = *(c+1); /* splice out the & */
164     }
165
166     if (strlen(GAV.startform) != GSV.len) {
167       fprintf(stderr,
168               "read_data():\n%s\n%s\n unequal length!\n",
169               GAV.farbe, GAV.startform);
170       exit(1);
171     }
172     free (ctmp);
173   }
174   else { /* start structure is open chain */
175     for (i = 0; i< GSV.len; i++) GAV.startform[i] = '.';
176   }
177
178   /*
179     read stop structure(s)
180   */
181   if (GTV.stop) {
182     s = GAV.stopform;
183     while (( ctmp = get_line(stdin))) {
184       *s = (char *)calloc(GSV.len+1, sizeof(char));
185       sscanf(ctmp, "%s", *s);
186       if (c=strchr(ctmp, '&')) {
187         for (; *c; c++) *c = *(c+1); /* splice out the & */
188       }
189  
190       if ( (s-GAV.stopform) >= GSV.maxS ) {
191         fprintf(stderr,
192                 "WARNING: Can handle a maximum of %d stop structures\n",
193                 GSV.maxS );
194         break;
195       }
196       
197       if (strlen(*s) != GSV.len) {
198         fprintf(stderr, "read_data():\n%s\n%s\n unequal length!\n",
199                 GAV.farbe, *s);
200         exit(1);
201       }
202
203       s++;
204       free (ctmp);
205     }
206     GSV.maxS = (s-GAV.stopform);
207   }
208   else {
209     GSV.maxS = 1;
210     GAV.stopform[0] = (char *)calloc(GSV.len+1, sizeof(char));
211   }
212
213   GAV.farbe_full = strdup(GAV.farbe);
214
215   GAV.sE = (float *)calloc(GSV.maxS, sizeof(float)); 
216 }
217
218 /**/
219 void clean_up(void) {
220   clean_up_globals();
221   clean_up_rl();
222   clean_up_nbList();
223   kill_cache();
224 }