-
Notifications
You must be signed in to change notification settings - Fork 619
Expand file tree
/
Copy pathdict.c
More file actions
272 lines (239 loc) · 8.4 KB
/
Copy pathdict.c
File metadata and controls
272 lines (239 loc) · 8.4 KB
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
/* dict.c -- create a sequence dictionary file.
Copyright (C) 2015, 2020 Genome Research Ltd.
Author: Shane McCarthy <sm15@sanger.ac.uk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. */
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <zlib.h>
#include <getopt.h>
#include "htslib/khash.h"
#include "htslib/kseq.h"
#include "htslib/hts.h"
#include "samtools.h"
KHASH_SET_INIT_STR(str)
KSEQ_INIT(gzFile, gzread)
typedef struct _args_t
{
const char *output_fname, *alt_fname;
const char *assembly, *species, *uri;
int alias, header;
khash_t(str) *is_alt;
}
args_t;
static int write_dict(const char *fn, args_t *args)
{
hts_md5_context *md5 = NULL;
int l, i, k, retval = 1;
gzFile fp;
kseq_t *seq = NULL;
unsigned char digest[16];
char hex[33];
fp = strcmp(fn, "-") ? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
if (fp == 0) {
print_error_errno("dict", "Cannot open %s", fn);
return 1;
}
FILE *out = stdout;
if (args->output_fname) {
out = fopen(args->output_fname, "w");
if (out == NULL) {
print_error_errno("dict", "Cannot open %s for writing", args->output_fname);
goto bailout;
}
}
if (!(md5 = hts_md5_init()))
goto bailout;
seq = kseq_init(fp);
if (!seq) {
print_error_errno("dict", "Couldn't allocate sequence reader");
goto bailout;
}
if (args->header) fprintf(out, "@HD\tVN:1.0\tSO:unsorted\n");
while ((l = kseq_read(seq)) >= 0) {
for (i = k = 0; i < seq->seq.l; ++i) {
if (seq->seq.s[i] >= '!' && seq->seq.s[i] <= '~')
seq->seq.s[k++] = toupper_c(seq->seq.s[i]);
}
hts_md5_reset(md5);
hts_md5_update(md5, (unsigned char*)seq->seq.s, k);
hts_md5_final(digest, md5);
hts_md5_hex(hex, digest);
fprintf(out, "@SQ\tSN:%s\tLN:%d\tM5:%s", seq->name.s, k, hex);
if (args->is_alt && kh_get(str, args->is_alt, seq->name.s) != kh_end(args->is_alt))
fprintf(out, "\tAH:*");
if (args->alias) {
const char *name = seq->name.s;
if (strncmp(name, "chr", 3) == 0) {
name += 3;
fprintf(out, "\tAN:%s", name);
}
else
fprintf(out, "\tAN:chr%s", name);
if (strcmp(name, "M") == 0)
fprintf(out, ",chrMT,MT");
else if (strcmp(name, "MT") == 0)
fprintf(out, ",chrM,M");
}
if (args->uri)
fprintf(out, "\tUR:%s", args->uri);
else if (strcmp(fn, "-") != 0) {
#ifdef _WIN32
char *real_path = _fullpath(NULL, fn, PATH_MAX);
#else
char *real_path = realpath(fn, NULL);
#endif
if (real_path) {
fprintf(out, "\tUR:file://%s", real_path);
free(real_path);
}
}
if (args->assembly) fprintf(out, "\tAS:%s", args->assembly);
if (args->species) fprintf(out, "\tSP:%s", args->species);
fprintf(out, "\n");
}
fflush(out);
if (ferror(out)) {
print_error_errno("dict", "Error writing to \"%s\"",
args->output_fname ? args->output_fname : "stdout");
goto bailout;
}
retval = 0;
bailout:
kseq_destroy(seq);
hts_md5_destroy(md5);
if (args->output_fname) fclose(out);
gzclose(fp);
return retval;
}
static int read_alt_file(khash_t(str) *is_alt, const char *fname)
{
htsFile *fp = hts_open(fname, "r");
if (fp == NULL) {
print_error_errno("dict", "Cannot open %s", fname);
return -1;
}
// .alt files are in a SAM-like format, but we don't use sam_read1()
// as these files may not have a complete set of @SQ headers.
kstring_t str = KS_INITIALIZE;
int r;
while ((r = hts_getline(fp, KS_SEP_LINE, &str)) >= 0) {
if (str.l == 0 || str.s[0] == '@') continue;
char *tab = strchr(str.s, '\t');
if (tab) *tab = '\0';
int ret;
char *seqname = strdup(str.s);
if (!seqname) { r = -2; break; }
kh_put(str, is_alt, seqname, &ret);
if (ret <= 0) free(seqname); // Already present or not added
if (ret < 0) { r = -2; break; }
}
if (r < -1) {
print_error_errno("dict", "Error reading \"%s\"", fname);
ks_free(&str);
hts_close(fp);
return -1;
}
ks_free(&str);
hts_close(fp);
return 0;
}
static int dict_usage(void)
{
fprintf(stderr, "\n");
fprintf(stderr, "About: Create a sequence dictionary file from a fasta file\n");
fprintf(stderr, "Usage: samtools dict [options] <file.fa|file.fa.gz>\n\n");
fprintf(stderr, "Options: -a, --assembly STR assembly\n");
fprintf(stderr, " -A, --alias, --alternative-name\n");
fprintf(stderr, " add AN tag by adding/removing 'chr'\n");
fprintf(stderr, " -H, --no-header do not print @HD line\n");
fprintf(stderr, " -l, --alt FILE add AH:* tag to alternate locus sequences\n");
fprintf(stderr, " -o, --output FILE file to write out dict file [stdout]\n");
fprintf(stderr, " -s, --species STR species\n");
fprintf(stderr, " -u, --uri STR URI [file:///abs/path/to/file.fa]\n");
fprintf(stderr, "\n");
return 1;
}
int dict_main(int argc, char *argv[])
{
int retval = 1;
args_t args = {
.output_fname = NULL,
.alt_fname = NULL,
.assembly = NULL,
.species = NULL,
.uri = NULL,
.alias = 0,
.header = 1,
.is_alt = NULL
};
static const struct option loptions[] =
{
{"help", no_argument, NULL, 'h'},
{"no-header", no_argument, NULL, 'H'},
{"alias", no_argument, NULL, 'A'},
{"alt", required_argument, NULL, 'l'},
{"alternative-name", no_argument, NULL, 'A'},
{"assembly", required_argument, NULL, 'a'},
{"species", required_argument, NULL, 's'},
{"uri", required_argument, NULL, 'u'},
{"output", required_argument, NULL, 'o'},
{NULL, 0, NULL, 0}
};
int c;
while ( (c=getopt_long(argc,argv,"?AhHa:l:s:u:o:",loptions,NULL))>0 )
{
switch (c)
{
case 'A': args.alias = 1; break;
case 'a': args.assembly = optarg; break;
case 'l': args.alt_fname = optarg; break;
case 's': args.species = optarg; break;
case 'u': args.uri = optarg; break;
case 'o': args.output_fname = optarg; break;
case 'H': args.header = 0; break;
case 'h': return dict_usage();
default: return dict_usage();
}
}
char *fname = NULL;
if ( optind>=argc )
{
if ( !isatty(STDIN_FILENO) ) fname = "-"; // reading from stdin
else return dict_usage();
}
else fname = argv[optind];
if (args.alt_fname) {
args.is_alt = kh_init(str);
if (!args.is_alt) {
print_error_errno("dict", "Couldn't allocate alt names table");
goto out;
}
if (read_alt_file(args.is_alt, args.alt_fname) < 0)
goto out;
}
retval = write_dict(fname, &args);
out:
if (args.is_alt) {
khint_t k;
for (k = 0; k < kh_end(args.is_alt); ++k)
if (kh_exist(args.is_alt, k)) free((char *) kh_key(args.is_alt, k));
kh_destroy(str, args.is_alt);
}
return retval;
}