Debian Codegraph @

samtools/sample.c.pysam.c

raw symbols SCIP
1 #include "samtools.pysam.h"
2
3 /* sample.c -- group data by sample.
4
5 Copyright (C) 2010, 2011 Broad Institute.
6 Copyright (C) 2013 Genome Research Ltd.
7
8 Author: Heng Li <[email protected]>
9
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE. */
27
28 #include <config.h>
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include "sample.h"
33 #include "htslib/khash.h"
34 KHASH_MAP_INIT_STR(sm, int)
35
36 bam_sample_t *bam_smpl_init(void)
37 {
38 bam_sample_t *s;
39 s = calloc(1, sizeof(bam_sample_t));
40 s->rg2smid = kh_init(sm);
41 s->sm2id = kh_init(sm);
42 return s;
43 }
44
45 void bam_smpl_destroy(bam_sample_t *sm)
46 {
47 int i;
48 khint_t k;
49 khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
50 if (sm == 0) return;
51 for (i = 0; i < sm->n; ++i) free(sm->smpl[i]);
52 free(sm->smpl);
53 for (k = kh_begin(rg2smid); k != kh_end(rg2smid); ++k)
54 if (kh_exist(rg2smid, k)) free((char*)kh_key(rg2smid, k));
55 kh_destroy(sm, sm->rg2smid);
56 kh_destroy(sm, sm->sm2id);
57 free(sm);
58 }
59
60 static void add_pair(bam_sample_t *sm, khash_t(sm) *sm2id, const char *key, const char *val)
61 {
62 khint_t k_rg, k_sm;
63 int ret;
64 khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
65 k_rg = kh_get(sm, rg2smid, key);
66 if (k_rg != kh_end(rg2smid)) return; // duplicated @RG-ID
67 k_rg = kh_put(sm, rg2smid, strdup(key), &ret);
68 k_sm = kh_get(sm, sm2id, val);
69 if (k_sm == kh_end(sm2id)) { // absent
70 if (sm->n == sm->m) {
71 sm->m = sm->m? sm->m<<1 : 1;
72 sm->smpl = realloc(sm->smpl, sizeof(char*) * sm->m);
73 }
74 sm->smpl[sm->n] = strdup(val);
75 k_sm = kh_put(sm, sm2id, sm->smpl[sm->n], &ret);
76 kh_val(sm2id, k_sm) = sm->n++;
77 }
78 kh_val(rg2smid, k_rg) = kh_val(sm2id, k_sm);
79 }
80
81 int bam_smpl_add(bam_sample_t *sm, const char *fn, const char *txt)
82 {
83 const char *p = txt, *q, *r;
84 kstring_t buf, first_sm;
85 int n = 0;
86 khash_t(sm) *sm2id = (khash_t(sm)*)sm->sm2id;
87 if (txt == 0) {
88 add_pair(sm, sm2id, fn, fn);
89 return 0;
90 }
91 memset(&buf, 0, sizeof(kstring_t));
92 memset(&first_sm, 0, sizeof(kstring_t));
93 while ((q = strstr(p, "@RG")) != 0) {
94 p = q + 3;
95 r = q = 0;
96 if ((q = strstr(p, "\tID:")) != 0) q += 4;
97 if ((r = strstr(p, "\tSM:")) != 0) r += 4;
98 if (r && q) {
99 char *u, *v;
100 int oq, or;
101 for (u = (char*)q; *u && *u != '\t' && *u != '\n'; ++u);
102 for (v = (char*)r; *v && *v != '\t' && *v != '\n'; ++v);
103 oq = *u; or = *v; *u = *v = '\0';
104 buf.l = 0; kputs(fn, &buf); kputc('/', &buf); kputs(q, &buf);
105 add_pair(sm, sm2id, buf.s, r);
106 if ( !first_sm.s )
107 kputs(r,&first_sm);
108 *u = oq; *v = or;
109 } else break;
110 p = q > r? q : r;
111 ++n;
112 }
113 if (n == 0) add_pair(sm, sm2id, fn, fn);
114 // If there is only one RG tag present in the header and reads are not annotated, don't refuse to work but
115 // use the tag instead.
116 else if ( n==1 && first_sm.s )
117 add_pair(sm,sm2id,fn,first_sm.s);
118 if ( first_sm.s )
119 free(first_sm.s);
120
121 // add_pair(sm, sm2id, fn, fn);
122 free(buf.s);
123 return 0;
124 }
125
126 int bam_smpl_rg2smid(const bam_sample_t *sm, const char *fn, const char *rg, kstring_t *str)
127 {
128 khint_t k;
129 khash_t(sm) *rg2smid = (khash_t(sm)*)sm->rg2smid;
130 if (rg) {
131 str->l = 0;
132 kputs(fn, str); kputc('/', str); kputs(rg, str);
133 k = kh_get(sm, rg2smid, str->s);
134 } else k = kh_get(sm, rg2smid, fn);
135 return k == kh_end(rg2smid)? -1 : kh_val(rg2smid, k);
136 }