annotate pyPRADA_1.2/tools/samtools-0.1.16/sam.c @ 0:acc2ca1a3ba4

Uploaded
author siyuan
date Thu, 20 Feb 2014 00:44:58 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
1 #include <string.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2 #include <unistd.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3 #include "faidx.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4 #include "sam.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6 #define TYPE_BAM 1
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 #define TYPE_READ 2
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 bam_header_t *bam_header_dup(const bam_header_t *h0)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11 bam_header_t *h;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12 int i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 h = bam_header_init();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 *h = *h0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15 h->hash = h->dict = h->rg2lib = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 h->text = (char*)calloc(h->l_text + 1, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 memcpy(h->text, h0->text, h->l_text);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 h->target_len = (uint32_t*)calloc(h->n_targets, 4);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 h->target_name = (char**)calloc(h->n_targets, sizeof(void*));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 for (i = 0; i < h->n_targets; ++i) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 h->target_len[i] = h0->target_len[i];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 h->target_name[i] = strdup(h0->target_name[i]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 return h;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 static void append_header_text(bam_header_t *header, char* text, int len)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 int x = header->l_text + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 int y = header->l_text + len + 1; // 1 byte null
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30 if (text == 0) return;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 kroundup32(x);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 kroundup32(y);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 if (x < y) header->text = (char*)realloc(header->text, y);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 strncpy(header->text + header->l_text, text, len); // we cannot use strcpy() here.
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 header->l_text += len;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 header->text[header->l_text] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 samfile_t *samopen(const char *fn, const char *mode, const void *aux)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 samfile_t *fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 fp = (samfile_t*)calloc(1, sizeof(samfile_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 if (strchr(mode, 'r')) { // read
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 fp->type |= TYPE_READ;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 if (strchr(mode, 'b')) { // binary
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 fp->type |= TYPE_BAM;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 fp->x.bam = strcmp(fn, "-")? bam_open(fn, "r") : bam_dopen(fileno(stdin), "r");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 if (fp->x.bam == 0) goto open_err_ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 fp->header = bam_header_read(fp->x.bam);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 } else { // text
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51 fp->x.tamr = sam_open(fn);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 if (fp->x.tamr == 0) goto open_err_ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 fp->header = sam_header_read(fp->x.tamr);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 if (fp->header->n_targets == 0) { // no @SQ fields
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 if (aux) { // check if aux is present
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 bam_header_t *textheader = fp->header;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 fp->header = sam_header_read2((const char*)aux);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58 if (fp->header == 0) goto open_err_ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 append_header_text(fp->header, textheader->text, textheader->l_text);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 bam_header_destroy(textheader);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 if (fp->header->n_targets == 0 && bam_verbose >= 1)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63 fprintf(stderr, "[samopen] no @SQ lines in the header.\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 } else if (bam_verbose >= 2) fprintf(stderr, "[samopen] SAM header is present: %d sequences.\n", fp->header->n_targets);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 } else if (strchr(mode, 'w')) { // write
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 fp->header = bam_header_dup((const bam_header_t*)aux);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 if (strchr(mode, 'b')) { // binary
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 char bmode[3];
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 int i, compress_level = -1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 for (i = 0; mode[i]; ++i) if (mode[i] >= '0' && mode[i] <= '9') break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 if (mode[i]) compress_level = mode[i] - '0';
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 if (strchr(mode, 'u')) compress_level = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74 bmode[0] = 'w'; bmode[1] = compress_level < 0? 0 : compress_level + '0'; bmode[2] = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75 fp->type |= TYPE_BAM;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 fp->x.bam = strcmp(fn, "-")? bam_open(fn, bmode) : bam_dopen(fileno(stdout), bmode);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 if (fp->x.bam == 0) goto open_err_ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 bam_header_write(fp->x.bam, fp->header);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79 } else { // text
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 // open file
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 fp->x.tamw = strcmp(fn, "-")? fopen(fn, "w") : stdout;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82 if (fp->x.tamr == 0) goto open_err_ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 if (strchr(mode, 'X')) fp->type |= BAM_OFSTR<<2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 else if (strchr(mode, 'x')) fp->type |= BAM_OFHEX<<2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 else fp->type |= BAM_OFDEC<<2;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 // write header
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87 if (strchr(mode, 'h')) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 int i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 bam_header_t *alt;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 // parse the header text
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 alt = bam_header_init();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 alt->l_text = fp->header->l_text; alt->text = fp->header->text;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 sam_header_parse(alt);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
94 alt->l_text = 0; alt->text = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
95 // check if there are @SQ lines in the header
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
96 fwrite(fp->header->text, 1, fp->header->l_text, fp->x.tamw); // FIXME: better to skip the trailing NULL
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
97 if (alt->n_targets) { // then write the header text without dumping ->target_{name,len}
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
98 if (alt->n_targets != fp->header->n_targets && bam_verbose >= 1)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
99 fprintf(stderr, "[samopen] inconsistent number of target sequences. Output the text header.\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
100 } else { // then dump ->target_{name,len}
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
101 for (i = 0; i < fp->header->n_targets; ++i)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
102 fprintf(fp->x.tamw, "@SQ\tSN:%s\tLN:%d\n", fp->header->target_name[i], fp->header->target_len[i]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
103 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
104 bam_header_destroy(alt);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
105 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
106 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
107 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
108 return fp;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
109
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
110 open_err_ret:
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
111 free(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
112 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
113 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
114
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
115 void samclose(samfile_t *fp)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
116 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
117 if (fp == 0) return;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
118 if (fp->header) bam_header_destroy(fp->header);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
119 if (fp->type & TYPE_BAM) bam_close(fp->x.bam);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
120 else if (fp->type & TYPE_READ) sam_close(fp->x.tamr);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
121 else fclose(fp->x.tamw);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
122 free(fp);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
123 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
124
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
125 int samread(samfile_t *fp, bam1_t *b)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
126 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
127 if (fp == 0 || !(fp->type & TYPE_READ)) return -1; // not open for reading
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
128 if (fp->type & TYPE_BAM) return bam_read1(fp->x.bam, b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
129 else return sam_read1(fp->x.tamr, fp->header, b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
130 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
131
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
132 int samwrite(samfile_t *fp, const bam1_t *b)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
133 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
134 if (fp == 0 || (fp->type & TYPE_READ)) return -1; // not open for writing
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
135 if (fp->type & TYPE_BAM) return bam_write1(fp->x.bam, b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
136 else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
137 char *s = bam_format1_core(fp->header, b, fp->type>>2&3);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
138 int l = strlen(s);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
139 fputs(s, fp->x.tamw); fputc('\n', fp->x.tamw);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
140 free(s);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
141 return l + 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
142 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
143 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
144
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
145 int sampileup(samfile_t *fp, int mask, bam_pileup_f func, void *func_data)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
146 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
147 bam_plbuf_t *buf;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
148 int ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
149 bam1_t *b;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
150 b = bam_init1();
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
151 buf = bam_plbuf_init(func, func_data);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
152 bam_plbuf_set_mask(buf, mask);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
153 while ((ret = samread(fp, b)) >= 0)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
154 bam_plbuf_push(b, buf);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
155 bam_plbuf_push(0, buf);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
156 bam_plbuf_destroy(buf);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
157 bam_destroy1(b);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
158 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
159 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
160
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
161 char *samfaipath(const char *fn_ref)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
162 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
163 char *fn_list = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
164 if (fn_ref == 0) return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
165 fn_list = calloc(strlen(fn_ref) + 5, 1);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
166 strcat(strcpy(fn_list, fn_ref), ".fai");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
167 if (access(fn_list, R_OK) == -1) { // fn_list is unreadable
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
168 if (access(fn_ref, R_OK) == -1) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
169 fprintf(stderr, "[samfaipath] fail to read file %s.\n", fn_ref);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
170 } else {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
171 if (bam_verbose >= 3) fprintf(stderr, "[samfaipath] build FASTA index...\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
172 if (fai_build(fn_ref) == -1) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
173 fprintf(stderr, "[samfaipath] fail to build FASTA index.\n");
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
174 free(fn_list); fn_list = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
175 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
176 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
177 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
178 return fn_list;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
179 }