Debian Codegraph @

bcftools/smpl_ilist.c

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