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