-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.c
261 lines (237 loc) · 6.37 KB
/
config.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
void load_config( configuration* cfg)
{
FILE* config;
char* next_line = NULL;
char config_filename[MAX_LENGTH];
size_t len = 0;
int bytes_read;
int i;
/* Initialize the executable paths to empty */
cfg->path_samtools = NULL;
cfg->path_bcftools = NULL;
cfg->path_mrfast = NULL;
cfg->path_gnuplot = NULL;
cfg->path_megablast = NULL;
/* Combine the home directory path with the name of the configuration file */
sprintf( config_filename, "%s/%s", getenv( "HOME"), CONFIG_FILE);
/* Open the configuration file for reading. Do not use safe_fopen here, we will create if it doesn't exist */
config = fopen( config_filename, "r");
if( config == NULL)
{
/* Create new config file */
create_config( cfg, config_filename);
}
else
{
/* Get the paths from the pre-existing configuration file */
i = 0;
while( ( bytes_read = getline( &next_line, &len, config)) != -1)
{
/* Ignore comments, which begin with the '#' character */
if( next_line[0] != '#')
{
next_line[strlen(next_line)-1] = 0; // get rid of \n
if( strstr( next_line, "SAMTOOLS"))
{
set_str( &( cfg->path_samtools), next_line + strlen("SAMTOOLS = "));
}
else if( strstr( next_line, "BCFTOOLS"))
{
set_str( &( cfg->path_bcftools), next_line + strlen("BCFTOOLS = "));
}
else if( strstr( next_line, "MRFAST"))
{
set_str( &( cfg->path_mrfast), next_line + strlen("MRFAST = "));
}
else if( strstr( next_line, "GNUPLOT"))
{
set_str( &( cfg->path_gnuplot), next_line + strlen("GNUPLOT = "));
}
else if( strstr( next_line, "MEGABLAST"))
{
set_str( &( cfg->path_megablast), next_line + strlen("MEGABLAST = "));
}
else
{
fprintf( stderr, "Configuration file has wrong format or unspecified external tools used.\n");
}
i = i + 1;
}
}
/* Free memory allocated internally by getline */
free( next_line);
/* If the paths are still NULL, then they are either not installed, or not in the PATH. */
/*
if( cfg->path_samtools == NULL)
{
fprintf( stderr, "Warning: samtools path is not in the configuration file.\n");
}
else
{
fprintf( stderr, "samtools path: %s\n", cfg->path_samtools);
}
*/
/*
if( cfg->path_bcftools == NULL)
{
fprintf( stderr, "Warning: bcftools path is not in the configuration file.\n");
}
else
{
fprintf( stderr, "bcftools path: %s\n", cfg->path_bcftools);
}
*/
if( cfg->path_mrfast == NULL)
{
fprintf( stderr, "Warning: mrfast path is not in the configuration file.\n");
}
else
{
fprintf( stderr, "mrfast path: %s\n", cfg->path_mrfast);
}
if( cfg->path_gnuplot == NULL)
{
fprintf( stderr, "Warning: gnuplot path is not in the configuration file.\n");
}
else
{
fprintf( stderr, "gnuplot path: %s\n", cfg->path_gnuplot);
}
/*
if( cfg->path_megablast == NULL)
{
fprintf( stderr, "Warning: megablast path is not in the configuration file.\n");
}
else
{
fprintf( stderr, "megablast path: %s\n", cfg->path_megablast);
}
*/
}
}
void create_config( configuration* cfg, char* config_filename)
{
FILE* config;
FILE* pipe;
char executable_path[MAX_LENGTH];
/* popen(...) executes shell commands */
/* "which" finds the path to the specified executable if it exists */
/* "2>/dev/null" redirects the output into a virtual black hole */
pipe = popen( "which samtools 2>/dev/null", "r");
if( pipe == NULL)
{
fprintf( stderr, "Error opening pipe\n");
}
else
{
if( fgets( executable_path, MAX_LENGTH, pipe) == NULL)
{
fprintf( stderr, "samtools not found in PATH. Install it or manually configure the %s file.\n", config_filename);
}
else
{
set_str( &( cfg->path_samtools), executable_path);
}
pclose( pipe);
}
pipe = popen( "which bcftools 2>/dev/null", "r");
if( pipe == NULL)
{
fprintf( stderr, "Error opening pipe\n");
}
else
{
if( fgets( executable_path, MAX_LENGTH, pipe) == NULL)
{
fprintf( stderr, "bcftools not found in PATH. Install it or manually configure the %s file.\n", config_filename);
}
else
{
set_str( &( cfg->path_bcftools), executable_path);
}
pclose( pipe);
}
pipe = popen( "which mrfast 2>/dev/null", "r");
if( pipe == NULL)
{
fprintf( stderr, "Error opening pipe\n");
}
else
{
if( fgets( executable_path, MAX_LENGTH, pipe) == NULL)
{
fprintf( stderr, "mrfast not found in PATH. Install it or manually configure the %s file.\n", config_filename);
}
else
{
set_str( &( cfg->path_mrfast), executable_path);
}
pclose( pipe);
}
pipe = popen( "which gnuplot 2>/dev/null", "r");
if( pipe == NULL)
{
fprintf( stderr, "Error opening pipe\n");
}
else
{
if( fgets( executable_path, MAX_LENGTH, pipe) == NULL)
{
fprintf( stderr, "gnuplot not found in PATH. Install it or manually configure the %s file.\n", config_filename);
}
else
{
set_str( &( cfg->path_gnuplot), executable_path);
}
pclose( pipe);
}
pipe = popen( "which megablast 2>/dev/null", "r");
if( pipe == NULL)
{
fprintf( stderr, "Error opening pipe\n");
}
else
{
if( fgets( executable_path, MAX_LENGTH, pipe) == NULL)
{
fprintf( stderr, "megablast not found in PATH. Install it or manually configure the %s file.\n", config_filename);
}
else
{
set_str( &( cfg->path_megablast), executable_path);
}
pclose( pipe);
}
config = safe_fopen( config_filename, "w");
if( cfg->path_samtools != NULL)
{
fprintf( config, "SAMTOOLS = %s", cfg->path_samtools);
}
if( cfg->path_bcftools != NULL)
{
fprintf( config, "BCFTOOLS = %s", cfg->path_bcftools);
}
if( cfg->path_mrfast != NULL)
{
fprintf( config, "MRFAST = %s", cfg->path_mrfast);
}
if( cfg->path_gnuplot != NULL)
{
fprintf( config, "GNUPLOT = %s", cfg->path_gnuplot);
}
if( cfg->path_megablast != NULL)
{
fprintf( config, "MEGABLAST = %s", cfg->path_megablast);
}
fclose( config);
fprintf( stderr,"\n");
fprintf( stderr, "*************************************************************************\n");
fprintf( stderr, "* Config file $HOME/.valor_config is created. *\n");
fprintf( stderr, "* Check the configuration file for any errors, *\n");
fprintf( stderr, "* then run VALOR again to load the configuration and normal operation. *\n");
fprintf( stderr, "*************************************************************************\n");
}