6 for (i=0; i<line->
n_allele; i++) ac[i]=0;
14 int i, an=-1, ac_len=0, ac_type=0;
16 if ( an_id>=0 && ac_id>=0 )
18 for (i=0; i<line->
n_info; i++)
21 if ( z->
key == an_id ) an = z->
v1.
i;
22 else if ( z->
key == ac_id ) { ac_ptr = z->
vptr; ac_len = z->
len; ac_type = z->
type; }
25 if ( an>=0 && ac_ptr )
28 #define BRANCH_INT(type_t) { \
29 type_t *p = (type_t *) ac_ptr; \
30 for (i=0; i<ac_len; i++) \
40 default: fprintf(stderr,
"[E::%s] todo: %d at %s:%d\n", __func__, ac_type, header->
id[
BCF_DT_CTG][line->
rid].
key, line->
pos+1); exit(1);
break;
53 if ( gt_id<0 )
return 0;
56 for (i=0; i<(int)line->
n_fmt; i++)
57 if ( line->
d.
fmt[i].
id==gt_id ) { fmt_gt = &line->
d.
fmt[i];
break; }
58 if ( !fmt_gt )
return 0;
59 #define BRANCH_INT(type_t,missing,vector_end) { \
60 for (i=0; i<line->n_sample; i++) \
62 type_t *p = (type_t*) (fmt_gt->p + i*fmt_gt->size); \
64 for (ial=0; ial<fmt_gt->n; ial++) \
66 if ( p[ial]==vector_end ) break; \
67 if ( !(p[ial]>>1) || p[ial]==missing ) continue; \
68 ac[(p[ial]>>1)-1]++; \
72 switch (fmt_gt->
type) {
76 default: fprintf(stderr,
"[E::%s] todo: %d at %s:%d\n", __func__, fmt_gt->
type, header->
id[
BCF_DT_CTG][line->
rid].
key, line->
pos+1); exit(1);
break;
86 int i, nals = 0, has_ref = 0, has_alt = 0, ial = 0, jal = 0;
87 #define BRANCH_INT(type_t,missing,vector_end) { \
88 type_t *p = (type_t*) (fmt_ptr->p + isample*fmt_ptr->size); \
89 for (i=0; i<fmt_ptr->n; i++) \
91 if ( p[i] == vector_end ) break; \
92 if ( !p[i] || p[i] == missing ) continue; \
96 if ( !ial ) { ial = tmp; has_alt = 1; } \
97 else if ( tmp!=ial ) \
115 switch (fmt_ptr->
type) {
119 default: fprintf(stderr,
"[E::%s] todo: fmt_type %d\n", __func__, fmt_ptr->
type); exit(1);
break;
123 if ( _ial ) *_ial = ial>0 ? ial-1 : ial;
124 if ( _jal ) *_jal = jal>0 ? jal-1 : jal;
141 int *ac = (
int*) calloc(line->
n_allele,
sizeof(
int));
144 #define BRANCH(type_t,missing,vector_end) { \
145 for (i=0; i<line->n_sample; i++) \
147 type_t *p = (type_t*) (gt->p + i*gt->size); \
149 for (ial=0; ial<gt->size; ial++) \
151 if ( p[ial]==vector_end ) break; \
152 if ( !(p[ial]>>1) || p[ial]==missing ) continue; \
153 if ( (p[ial]>>1)-1 >= line->n_allele ) return -1; \
154 ac[(p[ial]>>1)-1]++; \
162 default: fprintf(stderr,
"[E::%s] todo: %d at %s:%d\n", __func__, gt->
type, header->
id[
BCF_DT_CTG][line->
rid].
key, line->
pos+1); exit(1);
break;
166 int rm_als = 0, nrm = 0;
169 if ( !ac[i] ) { rm_als |= 1<<i; nrm++; }
179 int *
map = (
int*) calloc(line->
n_allele,
sizeof(
int));
183 kputs(line->
d.
allele[0], &str);
186 for (i=1, j=1; i<line->
n_allele; i++)
188 if ( rm_mask & 1<<i )
196 kputs(line->
d.
allele[i], &str);
200 if ( !nrm ) { free(map); free(str.
s);
return; }
205 int nA_ori = nR_ori-1;
206 int nA_new = nR_new-1;
208 int nG_ori = nR_ori*(nR_ori + 1)/2;
209 int nG_new = nR_new*(nR_new + 1)/2;
215 int mdat = 0, ndat = 0, mdat_bytes = 0, nret;
216 for (i=0; i<line->
n_info; i++)
228 mdat = mdat_bytes / size;
230 mdat_bytes = mdat * size;
233 fprintf(stderr,
"[%s:%d %s] Could not access INFO/%s at %s:%d [%d]\n", __FILE__,__LINE__,__FUNCTION__,
240 char *ss = (
char*) dat, *se = (
char*) dat;
251 for (j=0; j<nexp; j++)
254 while ( *se && *se!=
',' ) se++;
255 if ( rm_mask & 1<<(j+inc) )
261 if ( str.
l ) kputc(
',',&str);
262 kputsn(ss,se-ss,&str);
271 for (j=0; j<nR_ori; j++)
276 while ( *se && *se!=
',' ) se++;
278 if ( rm_mask & 1<<j || rm_mask & 1<<k )
284 if ( str.
l ) kputc(
',',&str);
285 kputsn(ss,se-ss,&str);
297 fprintf(stderr,
"[%s:%d %s] Could not update INFO/%s at %s:%d [%d]\n", __FILE__,__LINE__,__FUNCTION__,
309 assert( nret==nA_ori );
316 assert( nret==nR_ori );
322 #define BRANCH(type_t,is_vector_end) \
324 type_t *ptr = (type_t*) dat; \
325 int size = sizeof(type_t); \
326 for (j=0; j<ntop; j++) \
328 if ( is_vector_end ) { memcpy(dat+k*size, dat+j*size, size); break; } \
329 if ( rm_mask & 1<<(j+inc) ) continue; \
330 if ( j!=k ) memcpy(dat+k*size, dat+j*size, size); \
343 assert( nret==nG_ori );
344 int k, l_ori = -1, l_new = 0;
347 #define BRANCH(type_t,is_vector_end) \
349 type_t *ptr = (type_t*) dat; \
350 int size = sizeof(type_t); \
351 for (j=0; j<nR_ori; j++) \
353 for (k=0; k<=j; k++) \
356 if ( is_vector_end ) { memcpy(dat+l_new*size, dat+l_ori*size, size); break; } \
357 if ( rm_mask & 1<<j || rm_mask & 1<<k ) continue; \
358 if ( l_ori!=l_new ) memcpy(dat+l_new*size, dat+l_ori*size, size); \
374 fprintf(stderr,
"[%s:%d %s] Could not update INFO/%s at %s:%d [%d]\n", __FILE__,__LINE__,__FUNCTION__,
381 for (i=1; i<line->
n_allele; i++)
if ( map[i]!=i )
break;
382 if ( i<line->n_allele )
384 mdat = mdat_bytes / 4;
386 mdat_bytes = mdat * 4;
393 for (j=0; j<nret; j++)
398 assert( al<nR_ori && map[al]>=0 );
399 ptr[j] = (map[al]+1)<<1 | (ptr[j]&1);
409 for (i=0; i<line->
n_fmt; i++)
422 mdat = mdat_bytes / size;
424 mdat_bytes = mdat * size;
427 fprintf(stderr,
"[%s:%d %s] Could not access FORMAT/%s at %s:%d [%d]\n", __FILE__,__LINE__,__FUNCTION__,
448 char *ss = ((
char*)dat) + j*size, *se = ss + size, *ptr = ss;
449 int k_src = 0, k_dst = 0, l = str.
l;
450 for (k_src=0; k_src<nexp; k_src++)
452 if ( ptr>=se || !*ptr)
break;
453 while ( ptr<se && *ptr && *ptr!=
',' ) ptr++;
454 if ( rm_mask & 1<<(k_src+inc) )
459 if ( k_dst ) kputc(
',',&str);
460 kputsn(ss,ptr-ss,&str);
464 assert( k_src==nexp );
466 for (; l<size; l++) kputc(0, &str);
473 char *ss = ((
char*)dat) + j*size, *se = ss + size, *ptr = ss;
474 int k_src = 0, k_dst = 0, l = str.
l;
479 if ( *ptr==
',' ) nexp++;
482 if ( ptr!=ss ) nexp++;
483 assert( nexp==nG_ori || nexp==nR_ori );
488 for (ia=0; ia<nR_ori; ia++)
490 for (ib=0; ib<=ia; ib++)
492 if ( ptr>=se || !*ptr )
break;
493 while ( ptr<se && *ptr && *ptr!=
',' ) ptr++;
494 if ( rm_mask & 1<<ia || rm_mask & 1<<ib )
499 if ( k_dst ) kputc(
',',&str);
500 kputsn(ss,ptr-ss,&str);
504 if ( ptr>=se || !*ptr )
break;
509 for (k_src=0; k_src<nR_ori; k_src++)
511 if ( ptr>=se || !*ptr )
break;
512 while ( ptr<se && *ptr && *ptr!=
',' ) ptr++;
513 if ( rm_mask & 1<<k_src )
518 if ( k_dst ) kputc(
',',&str);
519 kputsn(ss,ptr-ss,&str);
523 assert( k_src==nR_ori );
525 for (; l<size; l++) kputc(0, &str);
532 fprintf(stderr,
"[%s:%d %s] Could not update FORMAT/%s at %s:%d [%d]\n", __FILE__,__LINE__,__FUNCTION__,
545 assert( nori==nA_ori );
552 assert( nori==nR_ori );
557 #define BRANCH(type_t,is_vector_end) \
559 for (j=0; j<line->n_sample; j++) \
561 type_t *ptr_src = ((type_t*)dat) + j*nori; \
562 type_t *ptr_dst = ((type_t*)dat) + j*nA_new; \
563 int size = sizeof(type_t); \
564 int k_src, k_dst = 0; \
565 for (k_src=0; k_src<ntop; k_src++) \
567 if ( is_vector_end ) { memcpy(ptr_dst+k_dst, ptr_src+k_src, size); break; } \
568 if ( rm_mask & 1<<(k_src+inc) ) continue; \
569 if ( k_src!=k_dst ) memcpy(ptr_dst+k_dst, ptr_src+k_src, size); \
583 assert( nori==nG_ori );
586 #define BRANCH(type_t,is_vector_end) \
588 for (j=0; j<line->n_sample; j++) \
590 type_t *ptr_src = ((type_t*)dat) + j*nori; \
591 type_t *ptr_dst = ((type_t*)dat) + j*nG_new; \
592 int size = sizeof(type_t); \
593 int ia, ib, k_dst = 0, k_src; \
595 for (k_src=0; k_src<nG_ori; k_src++) { if ( is_vector_end ) break; nset++; } \
596 if ( nset==nR_ori ) \
598 for (k_src=0; k_src<nR_ori; k_src++) \
600 if ( rm_mask & 1<<k_src ) continue; \
601 if ( k_src!=k_dst ) memcpy(ptr_dst+k_dst, ptr_src+k_src, size); \
604 memcpy(ptr_dst+k_dst, ptr_src+k_src, size); \
609 for (ia=0; ia<nR_ori; ia++) \
611 for (ib=0; ib<=ia; ib++) \
614 if ( is_vector_end ) { memcpy(ptr_dst+k_dst, ptr_src+k_src, size); ia = nR_ori; break; } \
615 if ( rm_mask & 1<<ia || rm_mask & 1<<ib ) continue; \
616 if ( k_src!=k_dst ) memcpy(ptr_dst+k_dst, ptr_src+k_src, size); \
633 fprintf(stderr,
"[%s:%d %s] Could not update FORMAT/%s at %s:%d [%d]\n", __FILE__,__LINE__,__FUNCTION__,