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