Add missing binaty and statis library
[jabaws.git] / binaries / src / ViennaRNA / Progs / RNAplot.c
1 /*
2   Plot RNA structures using different layout algorithms
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <math.h>
8 #include <ctype.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include "utils.h"
12 #include "PS_dot.h"
13 #include "RNAplot_cmdl.h"
14
15 #define PRIVATE static
16
17 int main(int argc, char *argv[]){
18   struct        RNAplot_args_info args_info;
19   char          *structure, *pre, *post;
20   char          fname[FILENAME_MAX_LENGTH], ffname[FILENAME_MAX_LENGTH];
21   char          *rec_sequence, *rec_id, **rec_rest;
22   int           i, length;
23   int           istty;
24   char          format[5]="ps";
25   unsigned int  rec_type, read_opt;
26
27   structure = pre = post = NULL;
28   length = 0;
29
30   /*
31   #############################################
32   # check the command line parameters
33   #############################################
34   */
35   if(RNAplot_cmdline_parser (argc, argv, &args_info) != 0) exit(1);
36
37   if(args_info.layout_type_given) rna_plot_type = args_info.layout_type_arg;
38   if(args_info.pre_given)         pre           = strdup(args_info.pre_arg);
39   if(args_info.post_given)        post          = strdup(args_info.post_arg);
40   if(args_info.output_format_given){
41     strncpy(format, args_info.output_format_arg, 4); format[4] = '\0';
42   }
43
44   /* free allocated memory of command line data structure */
45   RNAplot_cmdline_parser_free (&args_info);
46
47   /*
48   #############################################
49   # begin initializing
50   #############################################
51   */
52   rec_type      = read_opt = 0;
53   rec_id        = rec_sequence = NULL;
54   rec_rest      = NULL;
55   istty         = isatty(fileno(stdout)) && isatty(fileno(stdin));
56
57   /* set options we wanna pass to read_record */
58   if(istty){
59     read_opt |= VRNA_INPUT_NOSKIP_BLANK_LINES;
60     print_tty_input_seq_str("Input sequence (upper or lower case) followed by structure");
61   }
62
63   /* print user help if we get input from tty */
64   if(istty){
65     print_tty_input_seq();
66     read_opt |= VRNA_INPUT_NOSKIP_BLANK_LINES;
67   }
68
69   /*
70   #############################################
71   # main loop: continue until end of file
72   #############################################
73   */
74   while(
75     !((rec_type = read_record(&rec_id, &rec_sequence, &rec_rest, read_opt))
76         & (VRNA_INPUT_ERROR | VRNA_INPUT_QUIT))){
77
78     if(rec_id){
79       (void) sscanf(rec_id, ">%" XSTR(FILENAME_ID_LENGTH) "s", fname);
80     }
81     else fname[0] = '\0';
82
83     length = (int)strlen(rec_sequence);
84
85     structure = extract_record_rest_structure((const char **)rec_rest, 0, (rec_id) ? VRNA_OPTION_MULTILINE : 0);
86
87     if(!structure) nrerror("structure missing");
88     if((int)strlen(structure) != length) nrerror("structure and sequence differ in length!");
89
90     if (fname[0]!='\0'){
91       strcpy(ffname, fname);
92       strcat(ffname, "_ss");
93     } else
94       strcpy(ffname, "rna");
95
96     structure = NULL;
97     unsigned int struct_options = (rec_id) ? VRNA_CONSTRAINT_MULTILINE : 0;
98     struct_options |= VRNA_CONSTRAINT_ALL;
99     getConstraint(&structure, (const char **)rec_rest, struct_options);
100
101     if(strlen(rec_sequence) != strlen(structure))
102       nrerror("sequence and structure have unequal length");
103
104     switch (format[0]) {
105       case 'p':
106         strcat(ffname, ".ps");
107
108         (void) PS_rna_plot_a_gquad(rec_sequence, structure, ffname, pre, post);
109
110         /* PS_rna_plot_a(rec_sequence, structure, ffname, pre, post); */
111
112         break;
113       case 'g':
114         strcat(ffname, ".gml");
115         gmlRNA(rec_sequence, structure, ffname, 'x');
116         break;
117       case 'x':
118         strcat(ffname, ".ss");
119         xrna_plot(rec_sequence, structure, ffname);
120         break;
121       case 's':
122         strcat(ffname, ".svg");
123         svg_rna_plot(rec_sequence, structure, ffname);
124         break;
125       default:
126         RNAplot_cmdline_parser_print_help(); exit(EXIT_FAILURE);
127     }
128
129     fflush(stdout);
130
131     /* clean up */
132     if(rec_id) free(rec_id);
133     free(rec_sequence);
134     free(structure);
135     /* free the rest of current dataset */
136     if(rec_rest){
137       for(i=0;rec_rest[i];i++) free(rec_rest[i]);
138       free(rec_rest);
139     }
140     rec_id = rec_sequence = structure = NULL;
141     rec_rest = NULL;
142
143     /* print user help for the next round if we get input from tty */
144     if(istty){
145       print_tty_input_seq_str("Input sequence (upper or lower case) followed by structure");
146     }
147   }
148
149   return EXIT_SUCCESS;
150 }