Debian Codegraph @

samtools/dict.c

raw symbols SCIP
1 /* dict.c -- create a sequence dictionary file.
2
3 Copyright (C) 2015, 2020 Genome Research Ltd.
4
5 Author: Shane McCarthy <[email protected]>
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE. */
24
25 #include <config.h>
26
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <zlib.h>
31 #include <getopt.h>
32 #include "htslib/khash.h"
33 #include "htslib/kseq.h"
34 #include "htslib/hts.h"
35 #include "samtools.h"
36
37 KHASH_SET_INIT_STR(str)
38 KSEQ_INIT(gzFile, gzread)
39
40 typedef struct _args_t
41 {
42 char *output_fname, *alt_fname;
43 char *assembly, *species, *uri;
44 int alias, header;
45 khash_t(str) *is_alt;
46 }
47 args_t;
48
49 static void write_dict(const char *fn, args_t *args)
50 {
51 hts_md5_context *md5;
52 int l, i, k;
53 gzFile fp;
54 kseq_t *seq;
55 unsigned char digest[16];
56 char hex[33];
57
58 fp = strcmp(fn, "-") ? gzopen(fn, "r") : gzdopen(fileno(stdin), "r");
59 if (fp == 0) {
60 print_error_errno("dict", "Cannot open %s", fn);
61 exit(1);
62 }
63 FILE *out = stdout;
64 if (args->output_fname) {
65 out = fopen(args->output_fname, "w");
66 if (out == NULL) {
67 print_error_errno("dict", "Cannot open %s for writing", args->output_fname);
68 exit(1);
69 }
70 }
71
72 if (!(md5 = hts_md5_init()))
73 exit(1);
74
75 seq = kseq_init(fp);
76 if (args->header) fprintf(out, "@HD\tVN:1.0\tSO:unsorted\n");
77 while ((l = kseq_read(seq)) >= 0) {
78 for (i = k = 0; i < seq->seq.l; ++i) {
79 if (seq->seq.s[i] >= '!' && seq->seq.s[i] <= '~')
80 seq->seq.s[k++] = toupper(seq->seq.s[i]);
81 }
82 hts_md5_reset(md5);
83 hts_md5_update(md5, (unsigned char*)seq->seq.s, k);
84 hts_md5_final(digest, md5);
85 hts_md5_hex(hex, digest);
86 fprintf(out, "@SQ\tSN:%s\tLN:%d\tM5:%s", seq->name.s, k, hex);
87 if (args->is_alt && kh_get(str, args->is_alt, seq->name.s) != kh_end(args->is_alt))
88 fprintf(out, "\tAH:*");
89 if (args->alias) {
90 const char *name = seq->name.s;
91 if (strncmp(name, "chr", 3) == 0) {
92 name += 3;
93 fprintf(out, "\tAN:%s", name);
94 }
95 else
96 fprintf(out, "\tAN:chr%s", name);
97
98 if (strcmp(name, "M") == 0)
99 fprintf(out, ",chrMT,MT");
100 else if (strcmp(name, "MT") == 0)
101 fprintf(out, ",chrM,M");
102 }
103 if (args->uri)
104 fprintf(out, "\tUR:%s", args->uri);
105 else if (strcmp(fn, "-") != 0) {
106 #ifdef _WIN32
107 char *real_path = _fullpath(NULL, fn, PATH_MAX);
108 #else
109 char *real_path = realpath(fn, NULL);
110 #endif
111 fprintf(out, "\tUR:file://%s", real_path);
112 free(real_path);
113 }
114 if (args->assembly) fprintf(out, "\tAS:%s", args->assembly);
115 if (args->species) fprintf(out, "\tSP:%s", args->species);
116 fprintf(out, "\n");
117 }
118 kseq_destroy(seq);
119 hts_md5_destroy(md5);
120
121 if (args->output_fname) fclose(out);
122 gzclose(fp);
123 }
124
125 static void read_alt_file(khash_t(str) *is_alt, const char *fname)
126 {
127 htsFile *fp = hts_open(fname, "r");
128 if (fp == NULL) {
129 print_error_errno("dict", "Cannot open %s", fname);
130 exit(1);
131 }
132
133 // .alt files are in a SAM-like format, but we don't use sam_read1()
134 // as these files may not have a complete set of @SQ headers.
135
136 kstring_t str = KS_INITIALIZE;
137 while (hts_getline(fp, KS_SEP_LINE, &str) >= 0) {
138 if (str.l == 0 || str.s[0] == '@') continue;
139
140 char *tab = strchr(str.s, '\t');
141 if (tab) *tab = '\0';
142
143 int ret;
144 char *seqname = strdup(str.s);
145 kh_put(str, is_alt, seqname, &ret);
146 if (ret == 0) free(seqname); // Already present
147 }
148
149 ks_free(&str);
150 hts_close(fp);
151 }
152
153 static int dict_usage(void)
154 {
155 fprintf(stderr, "\n");
156 fprintf(stderr, "About: Create a sequence dictionary file from a fasta file\n");
157 fprintf(stderr, "Usage: samtools dict [options] <file.fa|file.fa.gz>\n\n");
158 fprintf(stderr, "Options: -a, --assembly STR assembly\n");
159 fprintf(stderr, " -A, --alias, --alternative-name\n");
160 fprintf(stderr, " add AN tag by adding/removing 'chr'\n");
161 fprintf(stderr, " -H, --no-header do not print @HD line\n");
162 fprintf(stderr, " -l, --alt FILE add AH:* tag to alternate locus sequences\n");
163 fprintf(stderr, " -o, --output FILE file to write out dict file [stdout]\n");
164 fprintf(stderr, " -s, --species STR species\n");
165 fprintf(stderr, " -u, --uri STR URI [file:///abs/path/to/file.fa]\n");
166 fprintf(stderr, "\n");
167 return 1;
168 }
169
170 int dict_main(int argc, char *argv[])
171 {
172 args_t *args = (args_t*) calloc(1,sizeof(args_t));
173 args->header = 1;
174
175 static const struct option loptions[] =
176 {
177 {"help", no_argument, NULL, 'h'},
178 {"no-header", no_argument, NULL, 'H'},
179 {"alias", no_argument, NULL, 'A'},
180 {"alt", required_argument, NULL, 'l'},
181 {"alternative-name", no_argument, NULL, 'A'},
182 {"assembly", required_argument, NULL, 'a'},
183 {"species", required_argument, NULL, 's'},
184 {"uri", required_argument, NULL, 'u'},
185 {"output", required_argument, NULL, 'o'},
186 {NULL, 0, NULL, 0}
187 };
188 int c;
189 while ( (c=getopt_long(argc,argv,"?AhHa:l:s:u:o:",loptions,NULL))>0 )
190 {
191 switch (c)
192 {
193 case 'A': args->alias = 1; break;
194 case 'a': args->assembly = optarg; break;
195 case 'l': args->alt_fname = optarg; break;
196 case 's': args->species = optarg; break;
197 case 'u': args->uri = optarg; break;
198 case 'o': args->output_fname = optarg; break;
199 case 'H': args->header = 0; break;
200 case 'h': return dict_usage();
201 default: return dict_usage();
202 }
203 }
204
205 char *fname = NULL;
206 if ( optind>=argc )
207 {
208 if ( !isatty(STDIN_FILENO) ) fname = "-"; // reading from stdin
209 else return dict_usage();
210 }
211 else fname = argv[optind];
212
213 if (args->alt_fname) {
214 args->is_alt = kh_init(str);
215 read_alt_file(args->is_alt, args->alt_fname);
216 }
217
218 write_dict(fname, args);
219
220 if (args->is_alt) {
221 khint_t k;
222 for (k = 0; k < kh_end(args->is_alt); ++k)
223 if (kh_exist(args->is_alt, k)) free((char *) kh_key(args->is_alt, k));
224 kh_destroy(str, args->is_alt);
225 }
226
227 free(args);
228 return 0;
229 }