-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsam.c
266 lines (240 loc) · 7.5 KB
/
sam.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <string.h>
#include "faidx.h"
#include "sam.h"
#define TYPE_BAM 1
#define TYPE_READ 2
#ifdef _MSC_VER
#define access _access
#include <io.h>
const int W_OK = 2;
const int R_OK = 4;
#endif
bam_header_t *bam_header_dup(const bam_header_t *h0)
{
bam_header_t *h;
int i;
h = bam_header_init();
*h = *h0;
h->hash = h->dict = h->rg2lib = 0;
h->text = (char*)calloc(h->l_text + 1, 1);
memcpy(h->text, h0->text, h->l_text);
h->target_len = (uint32_t*)calloc(h->n_targets, 4);
h->target_name = (char**)calloc(h->n_targets, sizeof(void*));
for (i=0; i < h->n_targets; ++i)
{
h->target_len[i] = h0->target_len[i];
h->target_name[i] = strdup(h0->target_name[i]);
}
return h;
}
static void append_header_text(bam_header_t *header, char* text, int len)
{
int x = header->l_text + 1;
int y = header->l_text + len + 1;
if (text == 0)
return;
kroundup32(x);
kroundup32(y);
if (x < y)
header->text = (char*)realloc(header->text, y);
// we cannot use strcpy() here.
strncpy(header->text + header->l_text, text, len);
header->l_text += len;
header->text[header->l_text] = 0;
}
samfile_t *samopen(const char *fn, const char *mode, const void *aux)
{
samfile_t *fp;
fp = (samfile_t*)calloc(1, sizeof(samfile_t));
if (strchr(mode, 'r'))
{
fp->type |= TYPE_READ;
if (strchr(mode, 'b'))
{
fp->type |= TYPE_BAM;
fp->x.bam = strcmp(fn, "-") ? bam_open(fn, "r") : bam_dopen(fileno(stdin), "r");
if (fp->x.bam == 0)
goto open_err_ret;
fp->header = bam_header_read(fp->x.bam);
}
else
{
fp->x.tamr = sam_open(fn);
if (fp->x.tamr == 0)
goto open_err_ret;
fp->header = sam_header_read(fp->x.tamr);
if (fp->header->n_targets == 0)
{
// no @SQ fields
if (aux)
{
// check if aux is present
bam_header_t *textheader = fp->header;
fp->header = sam_header_read2((const char*)aux);
if (fp->header == 0)
goto open_err_ret;
append_header_text(fp->header, textheader->text, textheader->l_text);
bam_header_destroy(textheader);
}
if ((fp->header->n_targets == 0) && (bam_verbose >= 1))
fprintf(stderr, "[samopen] no @SQ lines in the header.\n");
}
else if (bam_verbose >= 2)
fprintf(stderr, "[samopen] SAM header is present: %d sequences.\n", fp->header->n_targets);
}
}
else if (strchr(mode, 'w'))
{
// write
fp->header = bam_header_dup((const bam_header_t*)aux);
if (strchr(mode, 'b'))
{
// binary
char bmode[3];
int i;
int compress_level = -1;
for (i=0; mode[i]; ++i)
if ((mode[i] >= '0') && (mode[i] <= '9'))
break;
if (mode[i])
compress_level = mode[i] - '0';
if (strchr(mode, 'u'))
compress_level = 0;
bmode[0] = 'w';
bmode[1] = compress_level < 0 ? 0 : compress_level + '0';
bmode[2] = 0;
fp->type |= TYPE_BAM;
fp->x.bam = strcmp(fn, "-") ? bam_open(fn, bmode) : bam_dopen(fileno(stdout), bmode);
if (fp->x.bam == 0)
goto open_err_ret;
bam_header_write(fp->x.bam, fp->header);
}
else
{
// text
// open file
fp->x.tamw = strcmp(fn, "-") ? fopen(fn, "w") : stdout;
if (fp->x.tamw == 0)
goto open_err_ret;
if (strchr(mode, 'X'))
fp->type |= BAM_OFSTR << 2;
else if (strchr(mode, 'x'))
fp->type |= BAM_OFHEX << 2;
else
fp->type |= BAM_OFDEC << 2;
// write header
if (strchr(mode, 'h'))
{
int i;
bam_header_t *alt;
// parse the header text
alt = bam_header_init();
alt->l_text = fp->header->l_text;
alt->text = fp->header->text;
sam_header_parse(alt);
alt->l_text = 0;
alt->text = 0;
// check if there are @SQ lines in the header
// FIXME: better to skip the trailing NULL
fwrite(fp->header->text, 1, fp->header->l_text, fp->x.tamw);
if (alt->n_targets)
{
// then write the header text without dumping ->target_{name,len}
if ((alt->n_targets != fp->header->n_targets) && (bam_verbose >= 1))
fprintf(stderr, "[samopen] inconsistent number of target sequences. Output the text header.\n");
}
else
{
// then dump ->target_{name,len}
for (i=0; i < fp->header->n_targets; ++i)
fprintf(fp->x.tamw, "@SQ\tSN:%s\tLN:%d\n", fp->header->target_name[i], fp->header->target_len[i]);
}
bam_header_destroy(alt);
}
}
}
return fp;
open_err_ret:
free(fp);
return 0;
}
void samclose(samfile_t *fp)
{
if (fp == 0)
return;
if (fp->header)
bam_header_destroy(fp->header);
if (fp->type & TYPE_BAM)
bam_close(fp->x.bam);
else if (fp->type & TYPE_READ)
sam_close(fp->x.tamr);
else
fclose(fp->x.tamw);
free(fp);
}
int samread(samfile_t *fp, bam1_t *b)
{
if ((fp == 0) || !(fp->type & TYPE_READ))
return -1; // not open for reading
if (fp->type & TYPE_BAM)
return bam_read1(fp->x.bam, b);
else
return sam_read1(fp->x.tamr, fp->header, b);
}
int samwrite(samfile_t *fp, const bam1_t *b)
{
if ((fp == 0) || (fp->type & TYPE_READ))
return -1; // not open for writing
if (fp->type & TYPE_BAM)
return bam_write1(fp->x.bam, b);
else
{
char *s = bam_format1_core(fp->header, b, fp->type>>2&3);
int l = strlen(s);
fputs(s, fp->x.tamw);
fputc('\n', fp->x.tamw);
free(s);
return l+1;
}
}
int sampileup(samfile_t *fp, int mask, bam_pileup_f func, void *func_data)
{
bam_plbuf_t *buf;
int ret;
bam1_t *b;
b = bam_init1();
buf = bam_plbuf_init(func, func_data);
bam_plbuf_set_mask(buf, mask);
while ((ret = samread(fp, b)) >= 0)
bam_plbuf_push(b, buf);
bam_plbuf_push(0, buf);
bam_plbuf_destroy(buf);
bam_destroy1(b);
return 0;
}
char *samfaipath(const char *fn_ref)
{
char *fn_list = NULL;
if (fn_ref == 0)
return 0;
fn_list = (char*)calloc(strlen(fn_ref)+5, 1);
strcat(strcpy(fn_list, fn_ref), ".fai");
if (access(fn_list, R_OK) == -1)
{
// fn_list is unreadable
if (access(fn_ref, R_OK) == -1)
fprintf(stderr, "[samfaipath] fail to read file %s.\n", fn_ref);
else
{
if (bam_verbose >= 3)
fprintf(stderr, "[samfaipath] build FASTA index...\n");
if (fai_build(fn_ref) == -1)
{
fprintf(stderr, "[samfaipath] fail to build FASTA index.\n");
free(fn_list);
fn_list = 0;
}
}
}
return fn_list;
}