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