Debian Codegraph @

samtools/dict.c.pysam.c

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