annotate PsiCLASS-1.0.2/samtools-0.1.19/bcftools/main.c @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #include <stdlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 #include <sys/stat.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 #include <unistd.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #include "knetfile.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 #include "bcf.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 #include "kseq.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 KSTREAM_INIT(gzFile, gzread, 0x10000)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 int bcfview(int argc, char *argv[]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 int bcf_main_index(int argc, char *argv[]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 #define BUF_SIZE 0x10000
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 int bcf_cat(int n, char * const *fn)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 bcf_t *out;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 uint8_t *buf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 buf = malloc(BUF_SIZE);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 out = bcf_open("-", "w");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 for (i = 0; i < n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 bcf_t *in;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 bcf_hdr_t *h;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 off_t end;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 struct stat s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 in = bcf_open(fn[i], "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 h = bcf_hdr_read(in);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 if (i == 0) bcf_hdr_write(out, h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 bcf_hdr_destroy(h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 #ifdef _USE_KNETFILE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 fstat(knet_fileno((knetFile*)in->fp->fp), &s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 end = s.st_size - 28;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 while (knet_tell((knetFile*)in->fp->fp) < end) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 int size = knet_tell((knetFile*)in->fp->fp) + BUF_SIZE < end? BUF_SIZE : end - knet_tell((knetFile*)in->fp->fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 knet_read(in->fp->fp, buf, size);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 fwrite(buf, 1, size, out->fp->fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 abort(); // FIXME: not implemented
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 bcf_close(in);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 bcf_close(out);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 free(buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 extern double bcf_pair_freq(const bcf1_t *b0, const bcf1_t *b1, double f[4]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 int bcf_main_ldpair(int argc, char *argv[])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 bcf_t *fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 bcf_hdr_t *h;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 bcf1_t *b0, *b1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 bcf_idx_t *idx;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 kstring_t str;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 void *str2id;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 gzFile fplist;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 kstream_t *ks;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 int dret, lineno = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 if (argc < 3) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 fprintf(stderr, "Usage: bcftools ldpair <in.bcf> <in.list>\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 fplist = gzopen(argv[2], "rb");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 ks = ks_init(fplist);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 memset(&str, 0, sizeof(kstring_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 fp = bcf_open(argv[1], "rb");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 h = bcf_hdr_read(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 str2id = bcf_build_refhash(h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 idx = bcf_idx_load(argv[1]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 if (idx == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 fprintf(stderr, "[%s] No bcf index is found. Abort!\n", __func__);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 b0 = calloc(1, sizeof(bcf1_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 b1 = calloc(1, sizeof(bcf1_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 while (ks_getuntil(ks, '\n', &str, &dret) >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 char *p, *q;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 int k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 int tid0 = -1, tid1 = -1, pos0 = -1, pos1 = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 ++lineno;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 for (p = q = str.s, k = 0; *p; ++p) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 if (*p == ' ' || *p == '\t') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 *p = '\0';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 if (k == 0) tid0 = bcf_str2id(str2id, q);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 else if (k == 1) pos0 = atoi(q) - 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 else if (k == 2) tid1 = strcmp(q, "=")? bcf_str2id(str2id, q) : tid0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 else if (k == 3) pos1 = atoi(q) - 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 q = p + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 ++k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 if (k == 3) pos1 = atoi(q) - 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 if (tid0 >= 0 && tid1 >= 0 && pos0 >= 0 && pos1 >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 uint64_t off;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 double r, f[4];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 off = bcf_idx_query(idx, tid0, pos0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 bgzf_seek(fp->fp, off, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 while (bcf_read(fp, h, b0) >= 0 && b0->pos != pos0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 off = bcf_idx_query(idx, tid1, pos1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 bgzf_seek(fp->fp, off, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 while (bcf_read(fp, h, b1) >= 0 && b1->pos != pos1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 r = bcf_pair_freq(b0, b1, f);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 r *= r;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 printf("%s\t%d\t%s\t%d\t%.4g\t%.4g\t%.4g\t%.4g\t%.4g\n", h->ns[tid0], pos0+1, h->ns[tid1], pos1+1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 r, f[0], f[1], f[2], f[3]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 } //else fprintf(stderr, "[%s] Parse error at line %d.\n", __func__, lineno);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 bcf_destroy(b0); bcf_destroy(b1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 bcf_idx_destroy(idx);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 bcf_str2id_destroy(str2id);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 bcf_hdr_destroy(h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 bcf_close(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 free(str.s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 ks_destroy(ks);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 gzclose(fplist);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 int bcf_main_ld(int argc, char *argv[])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 bcf_t *fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 bcf_hdr_t *h;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 bcf1_t **b, *b0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 int i, j, m, n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 double f[4];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 if (argc == 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 fprintf(stderr, "Usage: bcftools ld <in.bcf>\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 fp = bcf_open(argv[1], "rb");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 h = bcf_hdr_read(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 // read the entire BCF
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 m = n = 0; b = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 b0 = calloc(1, sizeof(bcf1_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 while (bcf_read(fp, h, b0) >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 if (m == n) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 m = m? m<<1 : 16;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 b = realloc(b, sizeof(void*) * m);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 b[n] = calloc(1, sizeof(bcf1_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 bcf_cpy(b[n++], b0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 bcf_destroy(b0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 // compute pair-wise r^2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 printf("%d\n", n); // the number of loci
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 for (i = 0; i < n; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 printf("%s:%d", h->ns[b[i]->tid], b[i]->pos + 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 for (j = 0; j < i; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 double r = bcf_pair_freq(b[i], b[j], f);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 printf("\t%.3f", r*r);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 printf("\t1.000\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 // free
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 for (i = 0; i < n; ++i) bcf_destroy(b[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 free(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 bcf_hdr_destroy(h);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 bcf_close(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 int main(int argc, char *argv[])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 if (argc == 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 fprintf(stderr, "\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 fprintf(stderr, "Program: bcftools (Tools for data in the VCF/BCF formats)\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 fprintf(stderr, "Version: %s\n\n", BCF_VERSION);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 fprintf(stderr, "Usage: bcftools <command> <arguments>\n\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 fprintf(stderr, "Command: view print, extract, convert and call SNPs from BCF\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 fprintf(stderr, " index index BCF\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 fprintf(stderr, " cat concatenate BCFs\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 fprintf(stderr, " ld compute all-pair r^2\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 fprintf(stderr, " ldpair compute r^2 between requested pairs\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 fprintf(stderr, "\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 if (strcmp(argv[1], "view") == 0) return bcfview(argc-1, argv+1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 else if (strcmp(argv[1], "index") == 0) return bcf_main_index(argc-1, argv+1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 else if (strcmp(argv[1], "ld") == 0) return bcf_main_ld(argc-1, argv+1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 else if (strcmp(argv[1], "ldpair") == 0) return bcf_main_ldpair(argc-1, argv+1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 else if (strcmp(argv[1], "cat") == 0) return bcf_cat(argc-2, argv+2); // cat is different ...
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 fprintf(stderr, "[main] Unrecognized command.\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 return 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 }