annotate PsiCLASS-1.0.2/samtools-0.1.19/bam_import.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 <zlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #include <stdio.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 #include <ctype.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #include <stdlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 #include <unistd.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 #include <assert.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 #ifdef _WIN32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 #include <fcntl.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 #include "kstring.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 #include "bam.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 #include "sam_header.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 #include "kseq.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 #include "khash.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 KSTREAM_INIT(gzFile, gzread, 16384)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 KHASH_MAP_INIT_STR(ref, uint64_t)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 void bam_init_header_hash(bam_header_t *header);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 void bam_destroy_header_hash(bam_header_t *header);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 int32_t bam_get_tid(const bam_header_t *header, const char *seq_name);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 unsigned char bam_nt16_table[256] = {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 1, 2, 4, 8, 15,15,15,15, 15,15,15,15, 15, 0 /*=*/,15,15,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 15,15, 5, 6, 8,15, 7, 9, 15,10,15,15, 15,15,15,15,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 15,15, 5, 6, 8,15, 7, 9, 15,10,15,15, 15,15,15,15,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 unsigned short bam_char2flag_table[256] = {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 0,BAM_FREAD1,BAM_FREAD2,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 BAM_FPROPER_PAIR,0,BAM_FMREVERSE,0, 0,BAM_FMUNMAP,0,0, 0,0,0,0, 0,0,0,0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 0,0,0,0, BAM_FDUP,0,BAM_FQCFAIL,0, 0,0,0,0, 0,0,0,0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 BAM_FPAIRED,0,BAM_FREVERSE,BAM_FSECONDARY, 0,BAM_FUNMAP,0,0, 0,0,0,0, 0,0,0,0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 char *bam_nt16_rev_table = "=ACMGRSVTWYHKDBN";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 struct __tamFile_t {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 gzFile fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 kstream_t *ks;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 kstring_t *str;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 uint64_t n_lines;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 int is_first;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 char **__bam_get_lines(const char *fn, int *_n) // for bam_plcmd.c only
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 char **list = 0, *s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 int n = 0, dret, m = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 gzFile fp = (strcmp(fn, "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(fn, "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 kstream_t *ks;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 kstring_t *str;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 str = (kstring_t*)calloc(1, sizeof(kstring_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 ks = ks_init(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 while (ks_getuntil(ks, '\n', str, &dret) > 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 if (n == m) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 m = m? m << 1 : 16;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 list = (char**)realloc(list, m * sizeof(char*));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 if (str->s[str->l-1] == '\r')
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 str->s[--str->l] = '\0';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 s = list[n++] = (char*)calloc(str->l + 1, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 strcpy(s, str->s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 ks_destroy(ks);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 gzclose(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 free(str->s); free(str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 *_n = n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 return list;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 static bam_header_t *hash2header(const kh_ref_t *hash)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 bam_header_t *header;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 khiter_t k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 header = bam_header_init();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 header->n_targets = kh_size(hash);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 header->target_name = (char**)calloc(kh_size(hash), sizeof(char*));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 header->target_len = (uint32_t*)calloc(kh_size(hash), 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 for (k = kh_begin(hash); k != kh_end(hash); ++k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 if (kh_exist(hash, k)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 int i = (int)kh_value(hash, k);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 header->target_name[i] = (char*)kh_key(hash, k);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 header->target_len[i] = kh_value(hash, k)>>32;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 bam_init_header_hash(header);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 return header;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 bam_header_t *sam_header_read2(const char *fn)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 bam_header_t *header;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 int c, dret, ret, error = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 gzFile fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 kstream_t *ks;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 kstring_t *str;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 kh_ref_t *hash;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 khiter_t k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 if (fn == 0) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 fp = (strcmp(fn, "-") == 0)? gzdopen(fileno(stdin), "r") : gzopen(fn, "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 if (fp == 0) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 hash = kh_init(ref);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 ks = ks_init(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 str = (kstring_t*)calloc(1, sizeof(kstring_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 while (ks_getuntil(ks, 0, str, &dret) > 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 char *s = strdup(str->s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 int len, i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 i = kh_size(hash);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 ks_getuntil(ks, 0, str, &dret);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 len = atoi(str->s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 k = kh_put(ref, hash, s, &ret);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 if (ret == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 fprintf(stderr, "[sam_header_read2] duplicated sequence name: %s\n", s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 error = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 kh_value(hash, k) = (uint64_t)len<<32 | i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 if (dret != '\n')
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 while ((c = ks_getc(ks)) != '\n' && c != -1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 ks_destroy(ks);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 gzclose(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 free(str->s); free(str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 fprintf(stderr, "[sam_header_read2] %d sequences loaded.\n", kh_size(hash));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 if (error) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 header = hash2header(hash);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 kh_destroy(ref, hash);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 return header;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 static inline uint8_t *alloc_data(bam1_t *b, int size)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 if (b->m_data < size) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 b->m_data = size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 kroundup32(b->m_data);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 b->data = (uint8_t*)realloc(b->data, b->m_data);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 return b->data;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 static inline void parse_error(int64_t n_lines, const char * __restrict msg)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 fprintf(stderr, "Parse error at line %lld: %s\n", (long long)n_lines, msg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 abort();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 static inline void append_text(bam_header_t *header, kstring_t *str)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 size_t x = header->l_text, y = header->l_text + str->l + 2; // 2 = 1 byte dret + 1 byte null
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 kroundup32(x); kroundup32(y);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 if (x < y)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 header->n_text = y;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 header->text = (char*)realloc(header->text, y);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 if ( !header->text )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 fprintf(stderr,"realloc failed to alloc %ld bytes\n", y);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 abort();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 // Sanity check
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 if ( header->l_text+str->l+1 >= header->n_text )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 fprintf(stderr,"append_text FIXME: %ld>=%ld, x=%ld,y=%ld\n", header->l_text+str->l+1,(long)header->n_text,x,y);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 abort();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 strncpy(header->text + header->l_text, str->s, str->l+1); // we cannot use strcpy() here.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 header->l_text += str->l + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 header->text[header->l_text] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 int sam_header_parse(bam_header_t *h)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 char **tmp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 free(h->target_len); free(h->target_name);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 h->n_targets = 0; h->target_len = 0; h->target_name = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 if (h->l_text < 3) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 if (h->dict == 0) h->dict = sam_header_parse2(h->text);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 tmp = sam_header2list(h->dict, "SQ", "SN", &h->n_targets);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 if (h->n_targets == 0) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 h->target_name = calloc(h->n_targets, sizeof(void*));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 for (i = 0; i < h->n_targets; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 h->target_name[i] = strdup(tmp[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 free(tmp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 tmp = sam_header2list(h->dict, "SQ", "LN", &h->n_targets);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 h->target_len = calloc(h->n_targets, 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 for (i = 0; i < h->n_targets; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 h->target_len[i] = atoi(tmp[i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 free(tmp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 return h->n_targets;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 bam_header_t *sam_header_read(tamFile fp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 int ret, dret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 bam_header_t *header = bam_header_init();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 kstring_t *str = fp->str;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 while ((ret = ks_getuntil(fp->ks, KS_SEP_TAB, str, &dret)) >= 0 && str->s[0] == '@') { // skip header
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 str->s[str->l] = dret; // note that str->s is NOT null terminated!!
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 append_text(header, str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 if (dret != '\n') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 ret = ks_getuntil(fp->ks, '\n', str, &dret);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 str->s[str->l] = '\n'; // NOT null terminated!!
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 append_text(header, str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 ++fp->n_lines;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 sam_header_parse(header);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 bam_init_header_hash(header);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 fp->is_first = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 return header;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 int sam_read1(tamFile fp, bam_header_t *header, bam1_t *b)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239 int ret, doff, doff0, dret, z = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 bam1_core_t *c = &b->core;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 kstring_t *str = fp->str;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 kstream_t *ks = fp->ks;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 if (fp->is_first) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 fp->is_first = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 ret = str->l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 do { // special consideration for empty lines
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 ret = ks_getuntil(fp->ks, KS_SEP_TAB, str, &dret);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 if (ret >= 0) z += str->l + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 } while (ret == 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253 if (ret < 0) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 ++fp->n_lines;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 doff = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 { // name
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 c->l_qname = strlen(str->s) + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 memcpy(alloc_data(b, doff + c->l_qname) + doff, str->s, c->l_qname);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 doff += c->l_qname;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 { // flag
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 long flag;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 char *s;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 flag = strtol((char*)str->s, &s, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 if (*s) { // not the end of the string
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268 flag = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 for (s = str->s; *s; ++s)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 flag |= bam_char2flag_table[(int)*s];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 c->flag = flag;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 { // tid, pos, qual
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; c->tid = bam_get_tid(header, str->s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276 if (c->tid < 0 && strcmp(str->s, "*")) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 if (header->n_targets == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 fprintf(stderr, "[sam_read1] missing header? Abort!\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279 exit(1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 } else fprintf(stderr, "[sam_read1] reference '%s' is recognized as '*'.\n", str->s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; c->pos = isdigit(str->s[0])? atoi(str->s) - 1 : -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1; c->qual = isdigit(str->s[0])? atoi(str->s) : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 if (ret < 0) return -2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 { // cigar
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 char *s, *t;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 int i, op;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 long x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 c->n_cigar = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 if (ks_getuntil(ks, KS_SEP_TAB, str, &dret) < 0) return -3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 z += str->l + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 if (str->s[0] != '*') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294 uint32_t *cigar;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 for (s = str->s; *s; ++s) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 if ((isalpha(*s)) || (*s=='=')) ++c->n_cigar;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 else if (!isdigit(*s)) parse_error(fp->n_lines, "invalid CIGAR character");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 b->data = alloc_data(b, doff + c->n_cigar * 4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 cigar = bam1_cigar(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 for (i = 0, s = str->s; i != c->n_cigar; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 x = strtol(s, &t, 10);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 op = toupper(*t);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 if (op == 'M') op = BAM_CMATCH;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 else if (op == 'I') op = BAM_CINS;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 else if (op == 'D') op = BAM_CDEL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 else if (op == 'N') op = BAM_CREF_SKIP;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 else if (op == 'S') op = BAM_CSOFT_CLIP;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 else if (op == 'H') op = BAM_CHARD_CLIP;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 else if (op == 'P') op = BAM_CPAD;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 else if (op == '=') op = BAM_CEQUAL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 else if (op == 'X') op = BAM_CDIFF;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313 else if (op == 'B') op = BAM_CBACK;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 else parse_error(fp->n_lines, "invalid CIGAR operation");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 s = t + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 cigar[i] = bam_cigar_gen(x, op);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 if (*s) parse_error(fp->n_lines, "unmatched CIGAR operation");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 c->bin = bam_reg2bin(c->pos, bam_calend(c, cigar));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 doff += c->n_cigar * 4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 if (!(c->flag&BAM_FUNMAP)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 fprintf(stderr, "Parse warning at line %lld: mapped sequence without CIGAR\n", (long long)fp->n_lines);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 c->flag |= BAM_FUNMAP;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 c->bin = bam_reg2bin(c->pos, c->pos + 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 { // mtid, mpos, isize
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 c->mtid = strcmp(str->s, "=")? bam_get_tid(header, str->s) : c->tid;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 c->mpos = isdigit(str->s[0])? atoi(str->s) - 1 : -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 ret = ks_getuntil(ks, KS_SEP_TAB, str, &dret); z += str->l + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 c->isize = (str->s[0] == '-' || isdigit(str->s[0]))? atoi(str->s) : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 if (ret < 0) return -4;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338 { // seq and qual
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 uint8_t *p = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 if (ks_getuntil(ks, KS_SEP_TAB, str, &dret) < 0) return -5; // seq
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 z += str->l + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 if (strcmp(str->s, "*")) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344 c->l_qseq = strlen(str->s);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345 if (c->n_cigar && c->l_qseq != (int32_t)bam_cigar2qlen(c, bam1_cigar(b))) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346 fprintf(stderr, "Line %ld, sequence length %i vs %i from CIGAR\n",
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347 (long)fp->n_lines, c->l_qseq, (int32_t)bam_cigar2qlen(c, bam1_cigar(b)));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348 parse_error(fp->n_lines, "CIGAR and sequence length are inconsistent");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 p = (uint8_t*)alloc_data(b, doff + c->l_qseq + (c->l_qseq+1)/2) + doff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351 memset(p, 0, (c->l_qseq+1)/2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352 for (i = 0; i < c->l_qseq; ++i)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 p[i/2] |= bam_nt16_table[(int)str->s[i]] << 4*(1-i%2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354 } else c->l_qseq = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355 if (ks_getuntil(ks, KS_SEP_TAB, str, &dret) < 0) return -6; // qual
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356 z += str->l + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 if (strcmp(str->s, "*") && c->l_qseq != strlen(str->s))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358 parse_error(fp->n_lines, "sequence and quality are inconsistent");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 p += (c->l_qseq+1)/2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 if (strcmp(str->s, "*") == 0) for (i = 0; i < c->l_qseq; ++i) p[i] = 0xff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361 else for (i = 0; i < c->l_qseq; ++i) p[i] = str->s[i] - 33;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362 doff += c->l_qseq + (c->l_qseq+1)/2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364 doff0 = doff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365 if (dret != '\n' && dret != '\r') { // aux
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366 while (ks_getuntil(ks, KS_SEP_TAB, str, &dret) >= 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367 uint8_t *s, type, key[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 z += str->l + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
369 if (str->l < 6 || str->s[2] != ':' || str->s[4] != ':')
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
370 parse_error(fp->n_lines, "missing colon in auxiliary data");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
371 key[0] = str->s[0]; key[1] = str->s[1];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
372 type = str->s[3];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
373 s = alloc_data(b, doff + 3) + doff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
374 s[0] = key[0]; s[1] = key[1]; s += 2; doff += 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
375 if (type == 'A' || type == 'a' || type == 'c' || type == 'C') { // c and C for backward compatibility
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
376 s = alloc_data(b, doff + 2) + doff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
377 *s++ = 'A'; *s = str->s[5];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
378 doff += 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
379 } else if (type == 'I' || type == 'i') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
380 long long x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
381 s = alloc_data(b, doff + 5) + doff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
382 x = (long long)atoll(str->s + 5);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
383 if (x < 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
384 if (x >= -127) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
385 *s++ = 'c'; *(int8_t*)s = (int8_t)x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
386 s += 1; doff += 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
387 } else if (x >= -32767) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
388 *s++ = 's'; *(int16_t*)s = (int16_t)x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
389 s += 2; doff += 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
390 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
391 *s++ = 'i'; *(int32_t*)s = (int32_t)x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
392 s += 4; doff += 5;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
393 if (x < -2147483648ll)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
394 fprintf(stderr, "Parse warning at line %lld: integer %lld is out of range.",
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
395 (long long)fp->n_lines, x);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
396 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
397 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
398 if (x <= 255) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
399 *s++ = 'C'; *s++ = (uint8_t)x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
400 doff += 2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
401 } else if (x <= 65535) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
402 *s++ = 'S'; *(uint16_t*)s = (uint16_t)x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
403 s += 2; doff += 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
404 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
405 *s++ = 'I'; *(uint32_t*)s = (uint32_t)x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
406 s += 4; doff += 5;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
407 if (x > 4294967295ll)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
408 fprintf(stderr, "Parse warning at line %lld: integer %lld is out of range.",
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
409 (long long)fp->n_lines, x);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
410 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
411 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
412 } else if (type == 'f') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
413 s = alloc_data(b, doff + 5) + doff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
414 *s++ = 'f';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
415 *(float*)s = (float)atof(str->s + 5);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
416 s += 4; doff += 5;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
417 } else if (type == 'd') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
418 s = alloc_data(b, doff + 9) + doff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
419 *s++ = 'd';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
420 *(float*)s = (float)atof(str->s + 9);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
421 s += 8; doff += 9;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
422 } else if (type == 'Z' || type == 'H') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
423 int size = 1 + (str->l - 5) + 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
424 if (type == 'H') { // check whether the hex string is valid
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
425 int i;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
426 if ((str->l - 5) % 2 == 1) parse_error(fp->n_lines, "length of the hex string not even");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
427 for (i = 0; i < str->l - 5; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
428 int c = toupper(str->s[5 + i]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
429 if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
430 parse_error(fp->n_lines, "invalid hex character");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
431 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
432 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
433 s = alloc_data(b, doff + size) + doff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
434 *s++ = type;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
435 memcpy(s, str->s + 5, str->l - 5);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
436 s[str->l - 5] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
437 doff += size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
438 } else if (type == 'B') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
439 int32_t n = 0, Bsize, k = 0, size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
440 char *p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
441 if (str->l < 8) parse_error(fp->n_lines, "too few values in aux type B");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
442 Bsize = bam_aux_type2size(str->s[5]); // the size of each element
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
443 for (p = (char*)str->s + 6; *p; ++p) // count the number of elements in the array
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
444 if (*p == ',') ++n;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
445 p = str->s + 7; // now p points to the first number in the array
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
446 size = 6 + Bsize * n; // total number of bytes allocated to this tag
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
447 s = alloc_data(b, doff + 6 * Bsize * n) + doff; // allocate memory
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
448 *s++ = 'B'; *s++ = str->s[5];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
449 memcpy(s, &n, 4); s += 4; // write the number of elements
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
450 if (str->s[5] == 'c') while (p < str->s + str->l) ((int8_t*)s)[k++] = (int8_t)strtol(p, &p, 0), ++p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
451 else if (str->s[5] == 'C') while (p < str->s + str->l) ((uint8_t*)s)[k++] = (uint8_t)strtol(p, &p, 0), ++p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
452 else if (str->s[5] == 's') while (p < str->s + str->l) ((int16_t*)s)[k++] = (int16_t)strtol(p, &p, 0), ++p; // FIXME: avoid unaligned memory
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
453 else if (str->s[5] == 'S') while (p < str->s + str->l) ((uint16_t*)s)[k++] = (uint16_t)strtol(p, &p, 0), ++p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
454 else if (str->s[5] == 'i') while (p < str->s + str->l) ((int32_t*)s)[k++] = (int32_t)strtol(p, &p, 0), ++p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
455 else if (str->s[5] == 'I') while (p < str->s + str->l) ((uint32_t*)s)[k++] = (uint32_t)strtol(p, &p, 0), ++p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
456 else if (str->s[5] == 'f') while (p < str->s + str->l) ((float*)s)[k++] = (float)strtod(p, &p), ++p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
457 else parse_error(fp->n_lines, "unrecognized array type");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
458 s += Bsize * n; doff += size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
459 } else parse_error(fp->n_lines, "unrecognized type");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
460 if (dret == '\n' || dret == '\r') break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
461 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
462 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
463 b->l_aux = doff - doff0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
464 b->data_len = doff;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
465 if (bam_no_B) bam_remove_B(b);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
466 return z;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
467 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
468
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
469 tamFile sam_open(const char *fn)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
470 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
471 tamFile fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
472 gzFile gzfp = (strcmp(fn, "-") == 0)? gzdopen(fileno(stdin), "rb") : gzopen(fn, "rb");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
473 if (gzfp == 0) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
474 fp = (tamFile)calloc(1, sizeof(struct __tamFile_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
475 fp->str = (kstring_t*)calloc(1, sizeof(kstring_t));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
476 fp->fp = gzfp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
477 fp->ks = ks_init(fp->fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
478 return fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
479 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
480
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
481 void sam_close(tamFile fp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
482 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
483 if (fp) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
484 ks_destroy(fp->ks);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
485 gzclose(fp->fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
486 free(fp->str->s); free(fp->str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
487 free(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
488 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
489 }