File: thal_main.c

package info (click to toggle)
primer3 2.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 134,560 kB
  • sloc: ansic: 16,336; perl: 1,595; makefile: 262; sh: 54
file content (339 lines) | stat: -rwxr-xr-x 10,441 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 Copyright (c) 1996,1997,1998,1999,2000,2001,2004,2006,2007,2008,2009
 Whitehead Institute for Biomedical Research, Steve Rozen
 (http://purl.com/STEVEROZEN/), and Helen Skaletsky
 All rights reserved.
 
       This file is part of primer3 software suite.
   
       This software suite is is free software;
       you can redistribute it and/or modify it under the terms
       of the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at
       your option) any later version.
   
       This software is distributed in the hope that it will be useful,
       but WITHOUT ANY WARRANTY; without even the implied warranty of
       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       GNU General Public License for more details.
   
       You should have received a copy of the GNU General Public License
       along with this software (file gpl-2.0.txt in the source
       distribution); if not, write to the Free Software
       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 OWNERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON A THEORY 
 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <limits.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#include <ctype.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "thal.h"

/* Check on which OS we compile */
#if defined(_WIN32) || defined(WIN32) || defined (__WIN32__) || defined(__CYGWIN__) || defined(__MINGW32__)
#define OS_WIN
#endif

#define DEBUG

char *endptr; /* reading input */
int i; /* index */
const unsigned char *oligo1, *oligo2; /* inserted oligo sequences */
char *path = NULL; /* path to the parameter files */

/* Beginning of main */
int main(int argc, char** argv) 
{   
   const char* usage;
   int tmp_ret;
   thal_args a;
   thal_results o;
   set_thal_default_args(&a);
   a.temponly=0; /* by default print only melting temperature, 
		  do not draw structure or print any additional parameters */
if(a.debug == 0) {
#undef DEBUG
   } else {
#define DEBUG
   }

   usage = "USAGE: %s OPTIONS oligo\n"
     "-mv monovalent_conc  - concentration of monovalent cations in mM, by default 50 mM\n"
     "\n"
     "-dv divalent_conc    - concentration of divalent cations in mM, by default 0 mM\n"
     "\n"
     "-n  dNTP_conc        - concentration of deoxynycleotide triphosphate in mM, by default 0 mM\n"
     "\n"
     "-d  dna_conc         - concentration of DNA strands in nM, by default 50 nM\n"
     "\n"
     "-a  mode             - alignment type, END1, END2, ANY and HAIRPIN, by default ANY (when duplex)\n"
     "\n"
     "-t  temp             - temperature at which duplex is calculated, by default 37C\n"
     "\n"
     "-r                   - causes the alignment NOT to be displayed on stderr, _only_ Tm is printed\n"
     "\n"
     "-maxloop size        - the maximum size of secondary structures loops.\n" 
     "                       Default is 30 (this is maximum allowed length, currently).\n"
     "\n"
     "-path <path>         - the path to the thermodynamic parameter files\n"
     "\n"
     "-s1 DNA_oligomer\n"
     "\n"
     "-s2 DNA_oligomer\n"
     "\n";
   if(argc < 2) {
#ifdef DEBUG
      fprintf(stderr, usage, argv[0]);
#endif
      return -1;
   }
   /* BEGIN: READ the INPUT */
   for(i = 1; i < argc; ++i) {
      if (!strncmp("-mv", argv[i], 3)) { /* conc of monovalent cations */
	 if(argv[i+1]==NULL) {
#ifdef DEBUG
	    fprintf(stderr, usage, argv[0]);
#endif
	    exit(-1);
	 }
	 a.mv = strtod(argv[i+1], &endptr);
	 if ('\0' != *endptr || a.mv < 0.0) {
#ifdef DEBUG
	    fprintf(stderr, usage, argv[0]);
#endif
	    exit(-1);
	 }
	 i++;
      } else if (!strncmp("-dv", argv[i], 3)) { /* conc of divalent cations */
	 if(argv[i+1]==NULL) {
#ifdef DEBUG
	    fprintf(stderr, usage, argv[0]);
#endif
	    exit(-1);
	 }
	 a.dv = strtod(argv[i+1], &endptr);
	 if('\0' != *endptr || a.dv < 0.0) {
#ifdef DEBUG
	    fprintf(stderr, usage, argv[0]);
#endif
	    exit(-1);
	 }
	 i++;
      } else if (!strcmp("-path", argv[i])) {
	if(argv[i+1]==NULL) {
#ifdef DEBUG
            fprintf(stderr, usage, argv[0]);
#endif
            exit(-1);
         }
         path = (char*)argv[i+1];
         i++;
      } else if (!strncmp("-s1", argv[i], 3)) { /* first sequence in 5'->3' direction */
	 if(argv[i+1]==NULL) {
#ifdef DEBUG
	    fprintf(stderr, usage, argv[0]);
#endif
	    exit(-1);
	 }
	 oligo1 = (const unsigned char*)argv[i+1];
	 i++;	 
      } else if (!strncmp("-s2", argv[i], 3)) { /* second sequence in 5'->3' direction */
	 if(argv[i+1]==NULL) {
#ifdef DEBUG
	    fprintf(stderr, usage, argv[0]);
#endif
	    exit(-1);
	 }
	 oligo2 = (const unsigned char*)argv[i+1];
	 i++;
      } else if (!strncmp("-a", argv[i], 2)) { 	 /* annealing type END1, END2, ANY, considered only when duplexis; 
						  by default ANY  */
	 if(argv[i+1]==NULL) {
#ifdef DEBUG
	    fprintf(stderr, usage, argv[0]);
#endif
	    exit(-1);
	 }
	 if(strcmp(argv[i+1],"END1")==0) {
	    a.type = thal_end1;
	 } else if(strcmp(argv[i+1],"END2")==0) {
	    a.type = thal_end2;
	 } else if(strcmp(argv[i+1],"HAIRPIN")==0) {
	    a.type = thal_hairpin;
	    a.dimer = 0;
	 } else if (strcmp(argv[i+1], "ANY")==0) {
   	    a.type = thal_any; /* ANY */  
	 } else {
#ifdef DEBUG
	    fprintf(stderr, usage, argv[0]);
#endif
	    exit(-1);
	 }
	 i++;
      } else if (!strncmp("-d", argv[i], 2)) { /* dna conc */
	 if(argv[i+1]==NULL) {
#ifdef DEBUG
	    fprintf(stderr, usage, argv[0]);
#endif
	    exit(-1);
	 }
	 a.dna_conc = strtod(argv[i+1], &endptr);
	 if('\0' != *endptr || a.dna_conc < 0 || a.dna_conc == 0) {
#ifdef DEBUG
	    fprintf(stderr, usage, argv[0]);
#endif
	    exit(-1);
	 }
	 i++;
      } else if (!strncmp("-r", argv[i], 2)) { /* only temp is calculated */
	 a.temponly = 1;
      } else if (!strncmp("-t", argv[i], 2)) { /* temperature at which sec str are calculated */
	 if(argv[i+1]==NULL) {
#ifdef DEBUG
	    fprintf(stderr, usage, argv[0]);
#endif
	    exit(-1);
	 }
	 a.temp = strtod(argv[i+1], &endptr) + ABSOLUTE_ZERO;
	 if('\0' != *endptr) {
#ifdef DEBUG
	    fprintf(stderr, usage, argv[0]);
#endif
	    exit(-1);
	 }
	 i++;
      } else if (!strncmp("-n", argv[i], 2)) { /* concentration of dNTPs */
	 if(argv[i+1]==NULL) {
#ifdef DEBUG
	    fprintf(stderr, usage, argv[0]);
#endif
	    exit(-1);
	 }
	 a.dntp = strtod(argv[i+1], &endptr);
	 if('\0' != *endptr || a.dntp < 0.0) {
#ifdef DEBUG
	    fprintf(stderr, usage, argv[0]);
#endif
	    exit(-1);
	 }
	 i++;
      } else if (!strncmp("-maxloop", argv[i], 8)) { /* maximum size of loop calculated; 
						      this value can not be larger than 30 */
	 if(argv[i+1]==NULL) {
#ifdef DEBUG
	    fprintf(stderr, usage, argv[0]);
#endif
	    exit(-1);
	 }
	 a.maxLoop = (int) (strtod(argv[i+1], &endptr));
		 
	 if(a.maxLoop > MAX_LOOP ) {
	    a.maxLoop = MAX_LOOP;
#ifdef DEBUG
	    fputs("Warning: the maximum size of secondary structures loop is set to default (30)\n", stderr);
#endif
	 }  else if(a.maxLoop < MIN_LOOP) {	 
	    a.maxLoop = MIN_LOOP;
#ifdef DEBUG
	    fputs("Warning: the maximum size of secondary structures loop was set to minimum size of allowed loop length (0)\n", stderr);
#endif
	 } 
	 if('\0' != *endptr || a.maxLoop < 0) {
#ifdef DEBUG
	    fprintf(stderr, usage, argv[0]);
#endif
	    exit(-1);
	 }
	 i++;
      } else if(!strncmp("-", argv[i], 1)) { /* Unknown option. */
#ifdef DEBUG
	 fprintf(stderr, usage, argv[0]);
#endif
	 exit(-1);
      } else {
	 break;
      }
   }
   /* END reading INPUT */
   
   /* check the input correctness */
   if(a.dimer!=0 && (oligo2==NULL || oligo1==NULL)) { /* if user wants to calculate structure 
						       of dimer then two sequences must be defined*/
      fprintf(stderr, usage, argv[0]);
      exit(-1);
   }
   if(a.dimer==0 && (oligo2==NULL && oligo1==NULL)) { /* if user wants to calculate structure
						       of monomer then only one sequence must be defined */
      fprintf(stderr, usage, argv[0]);
      exit(-1);
   }
   
   /* read thermodynamic parameters */
   if (path == NULL) {
     /* check for the default paths */
     struct stat st;
#ifdef OS_WIN
     if ((stat(".\\primer3_config", &st) == 0) && S_ISDIR(st.st_mode)) {
       tmp_ret = get_thermodynamic_values(".\\primer3_config\\", &o);
     } else {
       /* no default directory found, error */
       fprintf(stderr, "Error: thermodynamic approach chosen, but path to thermodynamic parameters not specified\n");
       exit(-1);
     }
#else 
     if ((stat("./primer3_config", &st) == 0) && S_ISDIR(st.st_mode)) {
       tmp_ret = get_thermodynamic_values("./primer3_config/", &o);
     } else if ((stat("/etc/primer3_config", &st) == 0)  && S_ISDIR(st.st_mode)) {
       tmp_ret = get_thermodynamic_values("/etc/primer3_config/", &o);
     } else {
       /* no default directory found, error */
       fprintf(stderr, "Error: thermodynamic approach chosen, but path to thermodynamic parameters not specified\n");
       exit(-1);
     }
#endif
   } else tmp_ret = get_thermodynamic_values(path, &o);

   if (tmp_ret) {
     fprintf(stderr, "%s\n", o.msg);
     exit(-1);
   }

   /* execute thermodynamical alignemnt */
   if(a.dimer==0 && oligo1!=NULL){
      thal(oligo1,oligo1,&a,&o);   
   } else if(a.dimer==0 && oligo1==NULL && oligo2!=NULL) {
      thal(oligo2,oligo2,&a,&o);
   } else {
      thal(oligo1,oligo2,&a,&o);   
   }
   /* encountered error during thermodynamical calc */
   if (o.temp == THAL_ERROR_SCORE) {
      tmp_ret = fprintf(stderr, "Error: %s\n", o.msg);
      exit(-1);
   }
   if(a.temponly==1)
     printf("%f\n",o.temp);
   /* cleanup */
   destroy_thal_structures();
   return EXIT_SUCCESS;
}