forked from lh3/samtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbamtk.c
148 lines (141 loc) · 5.75 KB
/
bamtk.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
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>
#include "bam.h"
#ifdef _USE_KNETFILE
#include "knetfile.h"
#endif
#ifndef PACKAGE_VERSION
#define PACKAGE_VERSION "0.1.12-11 (r896:110)"
#endif
int bam_taf2baf(int argc, char *argv[]);
int bam_pileup(int argc, char *argv[]);
int bam_mpileup(int argc, char *argv[]);
int bam_merge(int argc, char *argv[]);
int bam_index(int argc, char *argv[]);
int bam_sort(int argc, char *argv[]);
int bam_tview_main(int argc, char *argv[]);
int bam_mating(int argc, char *argv[]);
int bam_rmdup(int argc, char *argv[]);
int bam_flagstat(int argc, char *argv[]);
int bam_fillmd(int argc, char *argv[]);
int bam_idxstats(int argc, char *argv[]);
int main_samview(int argc, char *argv[]);
int main_import(int argc, char *argv[]);
int main_reheader(int argc, char *argv[]);
int main_cut_target(int argc, char *argv[]);
int main_phase(int argc, char *argv[]);
int faidx_main(int argc, char *argv[]);
int glf3_view_main(int argc, char *argv[]);
int bam_tagview(int argc, char *argv[])
{
bamFile fp;
bam_header_t *header;
bam1_t *b;
char tag[2];
int ret;
if (argc < 3) {
fprintf(stderr, "Usage: samtools tagview <in.bam> <tag>\n");
return 1;
}
fp = strcmp(argv[1], "-")? bam_open(argv[1], "r") : bam_dopen(fileno(stdin), "r");
assert(fp);
header = bam_header_read(fp);
if (header == 0) {
fprintf(stderr, "[bam_view] fail to read the BAM header. Abort!\n");
return 1;
}
tag[0] = argv[2][0]; tag[1] = argv[2][1];
b = (bam1_t*)calloc(1, sizeof(bam1_t));
while ((ret = bam_read1(fp, b)) >= 0) {
uint8_t *d = bam_aux_get(b, tag);
if (d) {
printf("%s\t%d\t", bam1_qname(b), b->core.flag);
if (d[0] == 'Z' || d[0] == 'H') printf("%s\n", bam_aux2Z(d));
else if (d[0] == 'f') printf("%f\n", bam_aux2f(d));
else if (d[0] == 'd') printf("%lf\n", bam_aux2d(d));
else if (d[0] == 'A') printf("%c\n", bam_aux2A(d));
else if (d[0] == 'c' || d[0] == 's' || d[0] == 'i') printf("%d\n", bam_aux2i(d));
else if (d[0] == 'C' || d[0] == 'S' || d[0] == 'I') printf("%u\n", bam_aux2i(d));
else printf("\n");
}
}
if (ret < -1) fprintf(stderr, "[bam_view] truncated file? Continue anyway. (%d)\n", ret);
free(b->data); free(b);
bam_header_destroy(header);
bam_close(fp);
return 0;
}
static int usage()
{
fprintf(stderr, "\n");
fprintf(stderr, "Program: samtools (Tools for alignments in the SAM format)\n");
fprintf(stderr, "Version: %s\n\n", PACKAGE_VERSION);
fprintf(stderr, "Usage: samtools <command> [options]\n\n");
fprintf(stderr, "Command: view SAM<->BAM conversion\n");
fprintf(stderr, " sort sort alignment file\n");
fprintf(stderr, " pileup generate pileup output\n");
fprintf(stderr, " mpileup multi-way pileup\n");
fprintf(stderr, " faidx index/extract FASTA\n");
#if _CURSES_LIB != 0
fprintf(stderr, " tview text alignment viewer\n");
#endif
fprintf(stderr, " index index alignment\n");
fprintf(stderr, " idxstats BAM index stats (r595 or later)\n");
fprintf(stderr, " fixmate fix mate information\n");
fprintf(stderr, " glfview print GLFv3 file\n");
fprintf(stderr, " flagstat simple stats\n");
fprintf(stderr, " calmd recalculate MD/NM tags and '=' bases\n");
fprintf(stderr, " merge merge sorted alignments\n");
fprintf(stderr, " rmdup remove PCR duplicates\n");
fprintf(stderr, " reheader replace BAM header\n");
fprintf(stderr, " targetcut cut fosmid regions (for fosmid pool only)\n");
fprintf(stderr, " phase phase heterozygotes\n");
fprintf(stderr, "\n");
#ifdef _WIN32
fprintf(stderr, "\
Note: The Windows version of SAMtools is mainly designed for read-only\n\
operations, such as viewing the alignments and generating the pileup.\n\
Binary files generated by the Windows version may be buggy.\n\n");
#endif
return 1;
}
int main(int argc, char *argv[])
{
#ifdef _WIN32
setmode(fileno(stdout), O_BINARY);
setmode(fileno(stdin), O_BINARY);
#ifdef _USE_KNETFILE
knet_win32_init();
#endif
#endif
if (argc < 2) return usage();
if (strcmp(argv[1], "view") == 0) return main_samview(argc-1, argv+1);
else if (strcmp(argv[1], "import") == 0) return main_import(argc-1, argv+1);
else if (strcmp(argv[1], "pileup") == 0) return bam_pileup(argc-1, argv+1);
else if (strcmp(argv[1], "mpileup") == 0) return bam_mpileup(argc-1, argv+1);
else if (strcmp(argv[1], "merge") == 0) return bam_merge(argc-1, argv+1);
else if (strcmp(argv[1], "sort") == 0) return bam_sort(argc-1, argv+1);
else if (strcmp(argv[1], "index") == 0) return bam_index(argc-1, argv+1);
else if (strcmp(argv[1], "idxstats") == 0) return bam_idxstats(argc-1, argv+1);
else if (strcmp(argv[1], "faidx") == 0) return faidx_main(argc-1, argv+1);
else if (strcmp(argv[1], "fixmate") == 0) return bam_mating(argc-1, argv+1);
else if (strcmp(argv[1], "rmdup") == 0) return bam_rmdup(argc-1, argv+1);
else if (strcmp(argv[1], "glfview") == 0) return glf3_view_main(argc-1, argv+1);
else if (strcmp(argv[1], "flagstat") == 0) return bam_flagstat(argc-1, argv+1);
else if (strcmp(argv[1], "tagview") == 0) return bam_tagview(argc-1, argv+1);
else if (strcmp(argv[1], "calmd") == 0) return bam_fillmd(argc-1, argv+1);
else if (strcmp(argv[1], "fillmd") == 0) return bam_fillmd(argc-1, argv+1);
else if (strcmp(argv[1], "reheader") == 0) return main_reheader(argc-1, argv+1);
else if (strcmp(argv[1], "targetcut") == 0) return main_cut_target(argc-1, argv+1);
else if (strcmp(argv[1], "phase") == 0) return main_phase(argc-1, argv+1);
#if _CURSES_LIB != 0
else if (strcmp(argv[1], "tview") == 0) return bam_tview_main(argc-1, argv+1);
#endif
else {
fprintf(stderr, "[main] unrecognized command '%s'\n", argv[1]);
return 1;
}
return 0;
}