Debian Codegraph @

bcftools/smpl_ilist.c.pysam.c

raw symbols SCIP
1 #include "bcftools.pysam.h"
2
3 /*
4 Copyright (C) 2016-2021 Genome Research Ltd.
5
6 Author: Petr Danecek <[email protected]>
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25 */
26
27 #include <ctype.h>
28 #include "bcftools.h"
29 #include "smpl_ilist.h"
30
31 void smpl_ilist_destroy(smpl_ilist_t *smpl)
32 {
33 int i;
34 if ( smpl->pair )
35 {
36 for (i=0; i<smpl->n; i++) free(smpl->pair[i]);
37 free(smpl->pair);
38 }
39 free(smpl->idx);
40 free(smpl);
41 }
42
43 static inline int is_space_or_escaped(const char *min, const char *str)
44 {
45 if ( !isspace(*str) ) return 0;
46 int n = 0;
47 while ( --str>=min && *str=='\\' ) n++;
48 return n%2 ? 0 : 1;
49 }
50 smpl_ilist_t *smpl_ilist_init(bcf_hdr_t *hdr, char *sample_list, int is_file, int flags)
51 {
52 smpl_ilist_t *smpl = (smpl_ilist_t*) calloc(1,sizeof(smpl_ilist_t));
53
54 int i;
55 if ( !sample_list )
56 {
57 smpl->n = bcf_hdr_nsamples(hdr);
58 smpl->idx = (int*) malloc(sizeof(int)*smpl->n);
59 for (i=0; i<smpl->n; i++) smpl->idx[i] = i;
60 return smpl;
61 }
62
63 int negate = sample_list[0]=='^' ? 1 : 0;
64 int nlist;
65 char **list = hts_readlist(negate?sample_list+1:sample_list, is_file, &nlist);
66 if ( !list ) error("Could not parse %s\n", sample_list);
67
68 if ( negate && (flags&SMPL_REORDER) ) flags &= ~SMPL_REORDER;
69
70 // preserve the VCF order unless flags&SMPL_REORDER is set
71 int j = 0;
72 int *tmp = (int*)calloc(bcf_hdr_nsamples(hdr),sizeof(int));
73 char **pair = NULL;
74 for (i=0; i<nlist; i++)
75 {
76 char *smpl1 = list[i];
77 char *smpl2 = NULL;
78
79 char *ptr = list[i];
80 while ( *ptr && !is_space_or_escaped(list[i], ptr) ) ptr++;
81 if ( *ptr )
82 {
83 *ptr = 0;
84 smpl2 = ptr+1;
85 }
86
87 char *smpl_name = flags&SMPL_PAIR2 && smpl2 ? smpl2 : smpl1;
88 int idx = bcf_hdr_id2int(hdr, BCF_DT_SAMPLE, smpl_name);
89 if ( idx<0 )
90 {
91 if ( !(flags&SMPL_STRICT) )
92 {
93 if ( flags&SMPL_VERBOSE ) fprintf(bcftools_stderr,"No such sample: \"%s\"\n",smpl_name);
94 continue;
95 }
96 error("No such sample: \"%s\"\n", smpl_name);
97 }
98
99 if ( flags & SMPL_REORDER )
100 {
101 tmp[j++] = idx;
102 }
103 else
104 {
105 tmp[idx] = 1;
106 if ( smpl2 )
107 {
108 if ( !pair ) pair = (char**)calloc(bcf_hdr_nsamples(hdr),sizeof(char*));
109 if ( flags&SMPL_PAIR2 ) pair[idx] = strdup(smpl1);
110 else if ( flags&SMPL_PAIR1 ) pair[idx] = strdup(smpl2);
111 }
112 }
113 smpl->n++;
114 }
115
116 if ( flags & SMPL_REORDER )
117 {
118 smpl->idx = tmp;
119 for (i=0; i<nlist; i++) free(list[i]);
120 free(list);
121 return smpl;
122 }
123
124 if ( negate ) smpl->n = bcf_hdr_nsamples(hdr) - smpl->n;
125 smpl->idx = (int*) malloc(sizeof(int)*smpl->n);
126
127 j = 0;
128 if ( !negate )
129 {
130 if ( pair ) smpl->pair = (char**) calloc(smpl->n,sizeof(char*));
131 for (i=0; i<bcf_hdr_nsamples(hdr); i++)
132 {
133 if ( !tmp[i] ) continue;
134 smpl->idx[j] = i;
135 if ( pair && pair[i] ) smpl->pair[j] = pair[i];
136 j++;
137 }
138 }
139 else
140 {
141 for (i=0; i<bcf_hdr_nsamples(hdr); i++)
142 if ( !tmp[i] ) smpl->idx[j++] = i;
143 }
144
145 free(tmp);
146 free(pair);
147 for (i=0; i<nlist; i++) free(list[i]);
148 free(list);
149
150 return smpl;
151 }
152
153 smpl_ilist_t *smpl_ilist_map(bcf_hdr_t *hdr_a, bcf_hdr_t *hdr_b, int flags)
154 {
155 if ( flags&SMPL_STRICT && bcf_hdr_nsamples(hdr_a)!=bcf_hdr_nsamples(hdr_b) )
156 error("Different number of samples: %d vs %d\n", bcf_hdr_nsamples(hdr_a),bcf_hdr_nsamples(hdr_b));
157
158 smpl_ilist_t *smpl = (smpl_ilist_t*) calloc(1,sizeof(smpl_ilist_t));
159
160 int i;
161 smpl->n = bcf_hdr_nsamples(hdr_a);
162 smpl->idx = (int*) malloc(sizeof(int)*smpl->n);
163 for (i=0; i<smpl->n; i++)
164 {
165 const char *name = bcf_hdr_int2id(hdr_a, BCF_DT_SAMPLE, i);
166 smpl->idx[i] = bcf_hdr_id2int(hdr_b, BCF_DT_SAMPLE, name);
167 if ( flags&SMPL_STRICT && smpl->idx[i]<0 )
168 error("The sample %s is not present in the second file\n", name);
169 }
170 return smpl;
171 }
172