-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimport.c
267 lines (246 loc) · 8.52 KB
/
import.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
262
263
264
265
266
267
/* uproc-import and uproc-export
* Convert database from portable to 'native' format and vice-versa.
*
* Copyright 2014 Peter Meinicke, Robin Martinjak
*
* This file is part of uproc.
*
* uproc 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 3 of the License, or
* (at your option) any later version.
*
* uproc 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 uproc. If not, see <http://www.gnu.org/licenses/>.
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include "common.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <uproc.h>
#include "ppopts.h"
#ifdef EXPORT
#define PROGNAME "uproc-export"
#else
#define PROGNAME "uproc-import"
#endif
void make_opts(struct ppopts *o, const char *progname)
{
#define O(...) ppopts_add(o, __VA_ARGS__)
ppopts_add_text(o, PROGNAME ", version " UPROC_VERSION);
#ifdef EXPORT
ppopts_add_text(o, "USAGE: %s [options] SRCDIR DEST", progname);
ppopts_add_text(o, "Export database from SRCDIR to DEST.");
#else
ppopts_add_text(o, "USAGE: %s [options] SRC DESTDIR", progname);
ppopts_add_text(o,
"Import database from SRC to DESTDIR"
#if !defined(HAVE_MKDIR) && !defined(HAVE__MKDIR)
" (which must already exist)"
#endif
".");
#endif
ppopts_add_header(o, "GENERAL OPTIONS:");
O('h', "help", "", "Print this message and exit.");
O('v', "version", "", "Print version and exit.");
O('V', "libversion", "", "Print libuproc version/features and exit.");
#ifdef EXPORT
if (uproc_features_zlib()) {
O('n', "nocompress", "", "Store without gzip compression.");
}
#endif
#undef O
}
void progress_cb(double p)
{
progress(uproc_stderr, NULL, p);
}
#ifdef EXPORT
#else
#endif
#ifdef EXPORT
#define EXPORT_FUNC(name, type, load, store, destroy) \
int name(const char *dir, const char *name, uproc_io_stream *stream) \
{ \
int res; \
type *thing; \
fprintf(stderr, "exporting %s/%s\n", dir, name); \
thing = load(UPROC_IO_GZIP, "%s/%s", dir, name); \
if (!thing) { \
return -1; \
} \
res = store(thing, stream); \
destroy(thing); \
return res; \
}
uproc_ecurve *wrap_load(enum uproc_io_type iotype, const char *fmt,
const char *dir, const char *name)
{
progress(uproc_stderr, "Loading", -1);
return uproc_ecurve_loadp(UPROC_ECURVE_BINARY, iotype, progress_cb, fmt,
dir, name);
}
int wrap_store(uproc_ecurve *ec, uproc_io_stream *stream)
{
progress(uproc_stderr, "Storing", -1);
return uproc_ecurve_storeps(ec, UPROC_ECURVE_PLAIN, progress_cb, stream);
}
EXPORT_FUNC(ecurve, uproc_ecurve, wrap_load, wrap_store, uproc_ecurve_destroy)
EXPORT_FUNC(matrix, uproc_matrix, uproc_matrix_load, uproc_matrix_stores,
uproc_matrix_destroy)
EXPORT_FUNC(idmap, uproc_idmap, uproc_idmap_load, uproc_idmap_stores,
uproc_idmap_destroy)
#else
#define IMPORT_FUNC(name, type, load, store, destroy) \
int name(const char *dir, const char *name, uproc_io_stream *stream) \
{ \
int res; \
type *thing; \
fprintf(stderr, "importing %s/%s\n", dir, name); \
thing = load(stream); \
if (!thing) { \
return -1; \
} \
res = store(thing, UPROC_IO_GZIP, "%s/%s", dir, name); \
destroy(thing); \
return res; \
}
uproc_ecurve *wrap_load(uproc_io_stream *stream)
{
progress(uproc_stderr, "Loading", -1);
return uproc_ecurve_loadps(UPROC_ECURVE_PLAIN, progress_cb, stream);
}
int wrap_store(uproc_ecurve *ec, enum uproc_io_type iotype, const char *fmt,
const char *dir, const char *name)
{
progress(uproc_stderr, "Storing", -1);
return uproc_ecurve_storep(ec, UPROC_ECURVE_BINARY, iotype, progress_cb,
fmt, dir, name);
}
IMPORT_FUNC(ecurve, uproc_ecurve, wrap_load, wrap_store, uproc_ecurve_destroy)
IMPORT_FUNC(matrix, uproc_matrix, uproc_matrix_loads, uproc_matrix_store,
uproc_matrix_destroy)
IMPORT_FUNC(idmap, uproc_idmap, uproc_idmap_loads, uproc_idmap_store,
uproc_idmap_destroy)
#endif
int db_info(const char *dir, uproc_io_stream *stream)
{
char *line = NULL;
size_t sz;
uproc_io_stream *in, *out;
#ifdef EXPORT
fprintf(stderr, "exporting %s/info.txt\n", dir);
in = uproc_io_open("r", UPROC_IO_GZIP, "%s/info.txt", dir);
/* info.txt is not crucial, so ignore this error */
if (!in) {
return 0;
}
out = stream;
#else
fprintf(stderr, "importing %s/info.txt\n", dir);
in = stream;
out = uproc_io_open("w", UPROC_IO_STDIO, "%s/info.txt", dir);
if (!out) {
return -1;
}
#endif
while (uproc_io_getline(&line, &sz, in) != -1) {
uproc_io_printf(out, "%s", line);
}
free(line);
return 0;
}
enum nonopt_args { SOURCE, DEST, ARGC };
int main(int argc, char **argv)
{
int res;
uproc_io_stream *stream;
const char *dir, *file;
#ifdef EXPORT
enum uproc_io_type iotype = UPROC_IO_GZIP;
#endif
int opt;
struct ppopts opts = PPOPTS_INITIALIZER;
make_opts(&opts, argv[0]);
while ((opt = ppopts_getopt(&opts, argc, argv)) != -1) {
switch (opt) {
case 'h':
ppopts_print(&opts, stdout, 80, 0);
return EXIT_SUCCESS;
case 'v':
print_version(PROGNAME);
return EXIT_SUCCESS;
case 'V':
uproc_features_print(uproc_stdout);
return EXIT_SUCCESS;
#ifdef EXPORT
case 'n':
iotype = UPROC_IO_STDIO;
break;
#endif
case '?':
return EXIT_FAILURE;
}
}
if (argc < optind + ARGC) {
ppopts_print(&opts, stdout, 80, 0);
return EXIT_FAILURE;
}
#ifdef EXPORT
#define IMEX "ex"
dir = argv[optind + SOURCE];
file = argv[optind + DEST];
stream = open_write(file, iotype);
#else
#define IMEX "im"
dir = argv[optind + DEST];
make_dir(dir);
file = argv[optind + SOURCE];
stream = open_read(file);
#endif
if (!stream) {
uproc_perror("error opening %s", file);
return EXIT_FAILURE;
}
res = matrix(dir, "prot_thresh_e2", stream);
if (res) {
uproc_perror("error " IMEX "porting matrix");
return EXIT_FAILURE;
}
res = matrix(dir, "prot_thresh_e3", stream);
if (res) {
uproc_perror("error " IMEX "porting matrix");
return EXIT_FAILURE;
}
res = idmap(dir, "idmap", stream);
if (res) {
uproc_perror("error " IMEX "porting idmap");
return EXIT_FAILURE;
}
res = ecurve(dir, "fwd.ecurve", stream);
if (res) {
uproc_perror("error " IMEX "porting forward ecurve");
return EXIT_FAILURE;
}
res = ecurve(dir, "rev.ecurve", stream);
if (res) {
uproc_perror("error " IMEX "porting reverse ecurve");
return EXIT_FAILURE;
}
res = db_info(dir, stream);
if (res) {
uproc_perror("error " IMEX "porting info.txt");
return EXIT_FAILURE;
}
uproc_io_close(stream);
return EXIT_SUCCESS;
}