annotate pyPRADA_1.2/tools/samtools-0.1.16/sample.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 <stdlib.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
2 #include <string.h>
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
3 #include "sample.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
4 #include "khash.h"
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
5 KHASH_MAP_INIT_STR(sm, int)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
6
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
7 bam_sample_t *bam_smpl_init(void)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
8 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
9 bam_sample_t *s;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
10 s = calloc(1, sizeof(bam_sample_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
11 s->rg2smid = kh_init(sm);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
12 s->sm2id = kh_init(sm);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
13 return s;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
14 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
15
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
16 void bam_smpl_destroy(bam_sample_t *sm)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
17 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
18 int i;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
19 khint_t k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
20 khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
21 if (sm == 0) return;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
22 for (i = 0; i < sm->n; ++i) free(sm->smpl[i]);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
23 free(sm->smpl);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
24 for (k = kh_begin(rg2smid); k != kh_end(rg2smid); ++k)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
25 if (kh_exist(rg2smid, k)) free((char*)kh_key(rg2smid, k));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
26 kh_destroy(sm, sm->rg2smid);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
27 kh_destroy(sm, sm->sm2id);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
28 free(sm);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
29 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
30
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
31 static void add_pair(bam_sample_t *sm, khash_t(sm) *sm2id, const char *key, const char *val)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
32 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
33 khint_t k_rg, k_sm;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
34 int ret;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
35 khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
36 k_rg = kh_get(sm, rg2smid, key);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
37 if (k_rg != kh_end(rg2smid)) return; // duplicated @RG-ID
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
38 k_rg = kh_put(sm, rg2smid, strdup(key), &ret);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
39 k_sm = kh_get(sm, sm2id, val);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
40 if (k_sm == kh_end(sm2id)) { // absent
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
41 if (sm->n == sm->m) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
42 sm->m = sm->m? sm->m<<1 : 1;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
43 sm->smpl = realloc(sm->smpl, sizeof(void*) * sm->m);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
44 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
45 sm->smpl[sm->n] = strdup(val);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
46 k_sm = kh_put(sm, sm2id, sm->smpl[sm->n], &ret);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
47 kh_val(sm2id, k_sm) = sm->n++;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
48 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
49 kh_val(rg2smid, k_rg) = kh_val(sm2id, k_sm);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
50 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
51
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
52 int bam_smpl_add(bam_sample_t *sm, const char *fn, const char *txt)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
53 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
54 const char *p = txt, *q, *r;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
55 kstring_t buf;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
56 int n = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
57 khash_t(sm) *sm2id = (khash_t(sm)*)sm->sm2id;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
58 memset(&buf, 0, sizeof(kstring_t));
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
59 while ((q = strstr(p, "@RG")) != 0) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
60 p = q + 3;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
61 r = q = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
62 if ((q = strstr(p, "\tID:")) != 0) q += 4;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
63 if ((r = strstr(p, "\tSM:")) != 0) r += 4;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
64 if (r && q) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
65 char *u, *v;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
66 int oq, or;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
67 for (u = (char*)q; *u && *u != '\t' && *u != '\n'; ++u);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
68 for (v = (char*)r; *v && *v != '\t' && *v != '\n'; ++v);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
69 oq = *u; or = *v; *u = *v = '\0';
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
70 buf.l = 0; kputs(fn, &buf); kputc('/', &buf); kputs(q, &buf);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
71 add_pair(sm, sm2id, buf.s, r);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
72 *u = oq; *v = or;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
73 } else break;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
74 p = q > r? q : r;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
75 ++n;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
76 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
77 if (n == 0) add_pair(sm, sm2id, fn, fn);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
78 // add_pair(sm, sm2id, fn, fn);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
79 free(buf.s);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
80 return 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
81 }
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
82
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
83 int bam_smpl_rg2smid(const bam_sample_t *sm, const char *fn, const char *rg, kstring_t *str)
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
84 {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
85 khint_t k;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
86 khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
87 if (rg) {
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
88 str->l = 0;
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
89 kputs(fn, str); kputc('/', str); kputs(rg, str);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
90 k = kh_get(sm, rg2smid, str->s);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
91 } else k = kh_get(sm, rg2smid, fn);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
92 return k == kh_end(rg2smid)? -1 : kh_val(rg2smid, k);
acc2ca1a3ba4 Uploaded
siyuan
parents:
diff changeset
93 }