| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <strings.h> |
| 27 |
#include <unistd.h> |
| 28 |
#include <getopt.h> |
| 29 |
#include <assert.h> |
| 30 |
#include <ctype.h> |
| 31 |
#include <string.h> |
| 32 |
#include <errno.h> |
| 33 |
#include <sys/stat.h> |
| 34 |
#include <sys/types.h> |
| 35 |
#include <dirent.h> |
| 36 |
#include <math.h> |
| 37 |
#include <inttypes.h> |
| 38 |
#include <htslib/vcf.h> |
| 39 |
#include <htslib/synced_bcf_reader.h> |
| 40 |
#include <htslib/kseq.h> |
| 41 |
#include <htslib/khash_str2int.h> |
| 42 |
#include "bcftools.h" |
| 43 |
#include "vcmp.h" |
| 44 |
#include "filter.h" |
| 45 |
#include "convert.h" |
| 46 |
#include "smpl_ilist.h" |
| 47 |
#include "regidx.h" |
| 48 |
#include "dbuf.h" |
| 49 |
|
| 50 |
struct _args_t; |
| 51 |
|
| 52 |
typedef struct _rm_tag_t |
| 53 |
{ |
| 54 |
char *key; |
| 55 |
int hdr_id; |
| 56 |
void (*handler)(struct _args_t *, bcf1_t *, struct _rm_tag_t *); |
| 57 |
} |
| 58 |
rm_tag_t; |
| 59 |
|
| 60 |
typedef struct |
| 61 |
{ |
| 62 |
char **cols; |
| 63 |
int ncols, mcols; |
| 64 |
char **als; |
| 65 |
int nals, mals; |
| 66 |
kstring_t line; |
| 67 |
int rid, start, end; |
| 68 |
} |
| 69 |
annot_line_t; |
| 70 |
|
| 71 |
#define REPLACE_MISSING (1<<0) // -c +TAG .. replace only missing values |
| 72 |
#define REPLACE_ALL (1<<1) // -c TAG .. replace both missing and existing values |
| 73 |
#define REPLACE_NON_MISSING (1<<2) // -c -TAG .. replace only if tgt is not missing |
| 74 |
#define SET_OR_APPEND (1<<3) // -c =TAG .. set new value if missing or non-existent, append otherwise |
| 75 |
#define MATCH_VALUE (1<<4) // -c ~ID .. do not set, just match the value |
| 76 |
#define CARRY_OVER_MISSING (1<<5) // -c .TAG .. carry over source missing values as well |
| 77 |
#define MM_FIRST 0 // if multiple annotation lines overlap a VCF record, use the first, discarding the rest |
| 78 |
#define MM_APPEND 1 // append, possibly multiple times |
| 79 |
#define MM_UNIQUE 2 // append, only unique values |
| 80 |
#define MM_SUM 3 |
| 81 |
#define MM_AVG 4 |
| 82 |
#define MM_MIN 5 |
| 83 |
#define MM_MAX 6 |
| 84 |
#define MM_APPEND_MISSING 7 // missing values will be transferred as well |
| 85 |
typedef struct _annot_col_t |
| 86 |
{ |
| 87 |
int icol, replace, number; |
| 88 |
char *hdr_key_src, *hdr_key_dst; |
| 89 |
|
| 90 |
|
| 91 |
int (*setter)(struct _args_t *, bcf1_t *dst, struct _annot_col_t *, void *src); |
| 92 |
int (*getter)(struct _args_t *, bcf1_t *src, struct _annot_col_t *, void **ptr, int *mptr); |
| 93 |
int merge_method; |
| 94 |
khash_t(str2int) *mm_str_hash; |
| 95 |
kstring_t mm_kstr; |
| 96 |
size_t |
| 97 |
mm_dbl_nalloc, |
| 98 |
mm_dbl_nused, |
| 99 |
mm_dbl_ndat; |
| 100 |
double |
| 101 |
*mm_dbl; |
| 102 |
void *ptr; |
| 103 |
int mptr, done; |
| 104 |
} |
| 105 |
annot_col_t; |
| 106 |
|
| 107 |
typedef struct |
| 108 |
{ |
| 109 |
char *name; |
| 110 |
int ht_type; |
| 111 |
int icol; |
| 112 |
union { |
| 113 |
int i; |
| 114 |
float f; |
| 115 |
char *s; |
| 116 |
}; |
| 117 |
} |
| 118 |
ext_t; |
| 119 |
|
| 120 |
|
| 121 |
#define FLT_INCLUDE 1 |
| 122 |
#define FLT_EXCLUDE 2 |
| 123 |
|
| 124 |
#define MARK_LISTED 1 |
| 125 |
#define MARK_UNLISTED 2 |
| 126 |
|
| 127 |
typedef struct _args_t |
| 128 |
{ |
| 129 |
bcf_srs_t *files; |
| 130 |
bcf_hdr_t *hdr, *hdr_out, *tgts_hdr; |
| 131 |
htsFile *out_fh; |
| 132 |
int output_type, n_threads, clevel; |
| 133 |
bcf_sr_regions_t *tgts; |
| 134 |
char *index_fn; |
| 135 |
int write_index; |
| 136 |
|
| 137 |
regidx_t *tgt_idx; |
| 138 |
regitr_t *tgt_itr; |
| 139 |
int tgt_is_bed; |
| 140 |
|
| 141 |
filter_t *filter, *filter_ext; |
| 142 |
char *filter_str; |
| 143 |
int filter_logic; |
| 144 |
int keep_sites; |
| 145 |
|
| 146 |
rm_tag_t *rm; |
| 147 |
int nrm; |
| 148 |
int flt_keep_pass; |
| 149 |
|
| 150 |
vcmp_t *vcmp; |
| 151 |
annot_line_t *alines; |
| 152 |
annot_line_t *aline_missing; |
| 153 |
uint32_t *srt_alines; |
| 154 |
int nalines, malines, nsrt_alines, msrt_alines; |
| 155 |
int ref_idx, alt_idx, chr_idx, beg_idx, end_idx; |
| 156 |
annot_col_t *cols; |
| 157 |
int ncols; |
| 158 |
int match_id; |
| 159 |
int match_end; |
| 160 |
|
| 161 |
char *set_ids_fmt; |
| 162 |
convert_t *set_ids; |
| 163 |
int set_ids_replace; |
| 164 |
|
| 165 |
|
| 166 |
int n_ext; |
| 167 |
ext_t *ext; |
| 168 |
void **ext_ptr; |
| 169 |
|
| 170 |
int nsmpl_annot; |
| 171 |
int *sample_map, nsample_map, sample_is_file; |
| 172 |
uint8_t *src_smpl_pld, *dst_smpl_pld; |
| 173 |
int mtmpi, mtmpf, mtmps; |
| 174 |
int mtmpi2, mtmpf2, mtmps2; |
| 175 |
int mtmpi3, mtmpf3, mtmps3; |
| 176 |
int32_t *tmpi, *tmpi2, *tmpi3; |
| 177 |
float *tmpf, *tmpf2, *tmpf3; |
| 178 |
char *tmps, *tmps2, **tmpp, **tmpp2; |
| 179 |
kstring_t tmpks; |
| 180 |
|
| 181 |
char **argv, *output_fname, *targets_fname, *regions_list, *header_fname; |
| 182 |
char *remove_annots, *columns, *rename_chrs, *rename_annots, *sample_names, *mark_sites; |
| 183 |
char **rename_annots_map; |
| 184 |
char *min_overlap_str; |
| 185 |
float min_overlap_ann, min_overlap_vcf; |
| 186 |
int rename_annots_nmap; |
| 187 |
kstring_t merge_method_str; |
| 188 |
int argc, drop_header, record_cmd_line, tgts_is_vcf, mark_sites_logic, force, single_overlaps; |
| 189 |
int columns_is_file, has_append_mode, pair_logic; |
| 190 |
dbuf_t *header_lines; |
| 191 |
bcf1_t *current_rec; |
| 192 |
} |
| 193 |
args_t; |
| 194 |
|
| 195 |
char *msprintf(const char *fmt, ...); |
| 196 |
|
| 197 |
int parse_with_payload(const char *line, char **chr_beg, char **chr_end, uint32_t *beg, uint32_t *end, void *payload, void *usr) |
| 198 |
{ |
| 199 |
args_t *args = (args_t*) usr; |
| 200 |
int ret = args->tgt_is_bed ? regidx_parse_bed(line, chr_beg, chr_end, beg, end, NULL, NULL) : regidx_parse_tab(line, chr_beg, chr_end, beg, end, NULL, NULL); |
| 201 |
if ( ret<0 ) return ret; |
| 202 |
*((char **)payload) = strdup(line); |
| 203 |
return 0; |
| 204 |
} |
| 205 |
void free_payload(void *payload) |
| 206 |
{ |
| 207 |
char *str = *((char**)payload); |
| 208 |
free(str); |
| 209 |
} |
| 210 |
|
| 211 |
void remove_id(args_t *args, bcf1_t *line, rm_tag_t *tag) |
| 212 |
{ |
| 213 |
bcf_update_id(args->hdr,line,NULL); |
| 214 |
} |
| 215 |
void remove_filter(args_t *args, bcf1_t *line, rm_tag_t *tag) |
| 216 |
{ |
| 217 |
if ( tag->key && tag->hdr_id<0 ) |
| 218 |
{ |
| 219 |
error("Error: Cannot proceed, not even with the --force option, bad things could happen.\n" |
| 220 |
" Note that \"bcftools annotate -x FILTER\" can be used to remove ALL filters.\n" |
| 221 |
" Even better, use \"bcftools view -h\" and \"bcftools reheader\" to fix the header!\n" |
| 222 |
); |
| 223 |
} |
| 224 |
if ( !tag->key ) bcf_update_filter(args->hdr, line, NULL, args->flt_keep_pass); |
| 225 |
else bcf_remove_filter(args->hdr, line, tag->hdr_id, args->flt_keep_pass); |
| 226 |
} |
| 227 |
void remove_qual(args_t *args, bcf1_t *line, rm_tag_t *tag) |
| 228 |
{ |
| 229 |
bcf_float_set_missing(line->qual); |
| 230 |
} |
| 231 |
void remove_info(args_t *args, bcf1_t *line, rm_tag_t *tag) |
| 232 |
{ |
| 233 |
|
| 234 |
if ( !(line->unpacked & BCF_UN_INFO) ) bcf_unpack(line, BCF_UN_INFO); |
| 235 |
|
| 236 |
int i; |
| 237 |
for (i=0; i<line->n_info; i++) |
| 238 |
{ |
| 239 |
bcf_info_t *inf = &line->d.info[i]; |
| 240 |
if ( !strcmp("END",bcf_hdr_int2id(args->hdr,BCF_DT_ID,inf->key)) ) |
| 241 |
line->rlen = line->n_allele ? strlen(line->d.allele[0]) : 0; |
| 242 |
if ( inf->vptr_free ) |
| 243 |
{ |
| 244 |
free(inf->vptr - inf->vptr_off); |
| 245 |
inf->vptr_free = 0; |
| 246 |
} |
| 247 |
line->d.shared_dirty |= BCF1_DIRTY_INF; |
| 248 |
inf->vptr = NULL; |
| 249 |
inf->vptr_off = inf->vptr_len = 0; |
| 250 |
} |
| 251 |
} |
| 252 |
void remove_info_tag(args_t *args, bcf1_t *line, rm_tag_t *tag) |
| 253 |
{ |
| 254 |
bcf_update_info(args->hdr, line, tag->key, NULL, 0, BCF_HT_INT); |
| 255 |
} |
| 256 |
void remove_format_tag(args_t *args, bcf1_t *line, rm_tag_t *tag) |
| 257 |
{ |
| 258 |
bcf_update_format(args->hdr, line, tag->key, NULL, 0, BCF_HT_INT); |
| 259 |
} |
| 260 |
void remove_format(args_t *args, bcf1_t *line, rm_tag_t *tag) |
| 261 |
{ |
| 262 |
|
| 263 |
if ( !(line->unpacked & BCF_UN_FMT) ) bcf_unpack(line, BCF_UN_FMT); |
| 264 |
|
| 265 |
int i; |
| 266 |
for (i=0; i<line->n_fmt; i++) |
| 267 |
{ |
| 268 |
bcf_fmt_t *fmt = &line->d.fmt[i]; |
| 269 |
const char *key = bcf_hdr_int2id(args->hdr,BCF_DT_ID,fmt->id); |
| 270 |
if ( key[0]=='G' && key[1]=='T' && !key[2] ) continue; |
| 271 |
|
| 272 |
if ( fmt->p_free ) |
| 273 |
{ |
| 274 |
free(fmt->p - fmt->p_off); |
| 275 |
fmt->p_free = 0; |
| 276 |
} |
| 277 |
line->d.indiv_dirty = 1; |
| 278 |
fmt->p = NULL; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
#include "htslib/khash.h" |
| 283 |
KHASH_MAP_INIT_STR(vdict, bcf_idinfo_t) |
| 284 |
typedef khash_t(vdict) vdict_t; |
| 285 |
|
| 286 |
static void remove_hdr_lines(bcf_hdr_t *hdr, int type) |
| 287 |
{ |
| 288 |
int i = 0, nrm = 0; |
| 289 |
while ( i<hdr->nhrec ) |
| 290 |
{ |
| 291 |
if ( hdr->hrec[i]->type!=type ) { i++; continue; } |
| 292 |
bcf_hrec_t *hrec = hdr->hrec[i]; |
| 293 |
if ( type==BCF_HL_FMT || type==BCF_HL_INFO || type==BCF_HL_FMT || type== BCF_HL_CTG ) |
| 294 |
{ |
| 295 |
|
| 296 |
int id = bcf_hrec_find_key(hrec, "ID"); |
| 297 |
if ( id>=0 ) |
| 298 |
{ |
| 299 |
if ( type==BCF_HL_FMT && !strcmp(hrec->vals[id],"GT") ) { i++; continue; } |
| 300 |
vdict_t *d = type==BCF_HL_CTG ? (vdict_t*)hdr->dict[BCF_DT_CTG] : (vdict_t*)hdr->dict[BCF_DT_ID]; |
| 301 |
khint_t k = kh_get(vdict, d, hdr->hrec[i]->vals[id]); |
| 302 |
kh_val(d, k).hrec[type==BCF_HL_CTG?0:type] = NULL; |
| 303 |
kh_val(d, k).info[type] |= 0xf; |
| 304 |
} |
| 305 |
} |
| 306 |
nrm++; |
| 307 |
hdr->nhrec--; |
| 308 |
if ( i < hdr->nhrec ) |
| 309 |
memmove(&hdr->hrec[i],&hdr->hrec[i+1],(hdr->nhrec-i)*sizeof(bcf_hrec_t*)); |
| 310 |
bcf_hrec_destroy(hrec); |
| 311 |
} |
| 312 |
if ( nrm ) { |
| 313 |
if (bcf_hdr_sync(hdr) < 0) |
| 314 |
error_errno("[%s] Failed to update header", __func__); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
static void init_remove_annots(args_t *args) |
| 319 |
{ |
| 320 |
int keep_info = 0, keep_fmt = 0, keep_flt = 0; |
| 321 |
void *keep = khash_str2int_init(); |
| 322 |
kstring_t str = {0,0,0}; |
| 323 |
char *ss = args->remove_annots; |
| 324 |
|
| 325 |
int i, ntags, needs_info = 0; |
| 326 |
if ( args->set_ids ) |
| 327 |
{ |
| 328 |
const char **tags = convert_list_used_tags(args->set_ids,&ntags); |
| 329 |
for (i=0; i<ntags; i++) |
| 330 |
if ( !strncmp("INFO/",tags[i],4) ) needs_info = 1; |
| 331 |
} |
| 332 |
|
| 333 |
while ( *ss ) |
| 334 |
{ |
| 335 |
args->nrm++; |
| 336 |
args->rm = (rm_tag_t*) realloc(args->rm,sizeof(rm_tag_t)*args->nrm); |
| 337 |
rm_tag_t *tag = &args->rm[args->nrm-1]; |
| 338 |
tag->key = NULL; |
| 339 |
|
| 340 |
int type = BCF_HL_GEN; |
| 341 |
if ( !strncasecmp("INFO/",ss,5) ) { type = BCF_HL_INFO; ss += 5; } |
| 342 |
else if ( !strncasecmp("INF/",ss,4) ) { type = BCF_HL_INFO; ss += 4; } |
| 343 |
else if ( !strncasecmp("FORMAT/",ss,7) ) { type = BCF_HL_FMT; ss += 7; } |
| 344 |
else if ( !strncasecmp("FMT/",ss,4) ) { type = BCF_HL_FMT; ss += 4; } |
| 345 |
else if ( !strncasecmp("FILTER/",ss,7) ) { type = BCF_HL_FLT; ss += 7; } |
| 346 |
else if ( !strncasecmp("^INFO/",ss,6) ) { type = BCF_HL_INFO; ss += 6; keep_info = 1; } |
| 347 |
else if ( !strncasecmp("^INF/",ss,5) ) { type = BCF_HL_INFO; ss += 5; keep_info = 1; } |
| 348 |
else if ( !strncasecmp("^FORMAT/",ss,8) ) { type = BCF_HL_FMT; ss += 8; keep_fmt = 1; } |
| 349 |
else if ( !strncasecmp("^FMT/",ss,5) ) { type = BCF_HL_FMT; ss += 5; keep_fmt = 1; } |
| 350 |
else if ( !strncasecmp("^FILTER/",ss,8) ) { type = BCF_HL_FLT; ss += 8; keep_flt = 1; } |
| 351 |
|
| 352 |
char *se = ss; |
| 353 |
while ( *se && *se!=',' ) se++; |
| 354 |
str.l = 0; |
| 355 |
kputsn(ss, se-ss, &str); |
| 356 |
|
| 357 |
if ( type==BCF_HL_FLT ) |
| 358 |
{ |
| 359 |
if ( !keep_flt ) |
| 360 |
{ |
| 361 |
args->flt_keep_pass = 1; |
| 362 |
tag->handler = remove_filter; |
| 363 |
tag->key = strdup(str.s); |
| 364 |
tag->hdr_id = bcf_hdr_id2int(args->hdr, BCF_DT_ID, tag->key); |
| 365 |
if ( !bcf_hdr_idinfo_exists(args->hdr,BCF_HL_FLT,tag->hdr_id) ) |
| 366 |
{ |
| 367 |
if ( args->keep_sites ) |
| 368 |
error("Error: The filter \"%s\" is not defined in the header, cannot use the -k option\n", str.s); |
| 369 |
else |
| 370 |
fprintf(stderr,"Warning: The filter \"%s\" is not defined in the header\n", str.s); |
| 371 |
} |
| 372 |
else if ( !args->keep_sites ) bcf_hdr_remove(args->hdr_out,BCF_HL_FLT,tag->key); |
| 373 |
} |
| 374 |
else |
| 375 |
{ |
| 376 |
int value, ret = khash_str2int_get(keep, str.s, &value); |
| 377 |
if ( ret==-1 ) khash_str2int_set(keep, strdup(str.s),1<<BCF_HL_FLT); |
| 378 |
else khash_str2int_set(keep, str.s, value | 1<<BCF_HL_FLT); |
| 379 |
args->nrm--; |
| 380 |
} |
| 381 |
} |
| 382 |
else if ( type!=BCF_HL_GEN ) |
| 383 |
{ |
| 384 |
int id = bcf_hdr_id2int(args->hdr,BCF_DT_ID,str.s); |
| 385 |
if ( !bcf_hdr_idinfo_exists(args->hdr,type,id) ) |
| 386 |
{ |
| 387 |
if ( args->keep_sites ) |
| 388 |
error("Error: The tag \"%s\" is not defined in the header, cannot use the -k option\n", str.s); |
| 389 |
else |
| 390 |
fprintf(stderr,"Warning: The tag \"%s\" not defined in the header\n", str.s); |
| 391 |
|
| 392 |
tag->key = strdup(str.s); |
| 393 |
if ( type==BCF_HL_INFO ) |
| 394 |
{ |
| 395 |
tag->handler = remove_info_tag; |
| 396 |
if ( needs_info ) error("Error: `--remove INFO/%s` is executed first, cannot combine with `--set-id %s`\n",tag->key,args->set_ids_fmt); |
| 397 |
} |
| 398 |
else if ( type==BCF_HL_FMT ) tag->handler = remove_format_tag; |
| 399 |
} |
| 400 |
else if ( (type==BCF_HL_FMT && keep_fmt) || (type==BCF_HL_INFO && keep_info) ) |
| 401 |
{ |
| 402 |
int value, ret = khash_str2int_get(keep, str.s, &value); |
| 403 |
if ( ret==-1 ) khash_str2int_set(keep, strdup(str.s),1<<type); |
| 404 |
else khash_str2int_set(keep, str.s, value | 1<<type); |
| 405 |
args->nrm--; |
| 406 |
} |
| 407 |
else |
| 408 |
{ |
| 409 |
tag->key = strdup(str.s); |
| 410 |
if ( type==BCF_HL_INFO ) |
| 411 |
{ |
| 412 |
tag->handler = remove_info_tag; |
| 413 |
if ( needs_info ) error("Error: `--remove INFO/%s` is executed first, cannot combine with `--set-id %s`\n",tag->key,args->set_ids_fmt); |
| 414 |
} |
| 415 |
else if ( type==BCF_HL_FMT ) tag->handler = remove_format_tag; |
| 416 |
if ( !args->keep_sites ) bcf_hdr_remove(args->hdr_out,type,tag->key); |
| 417 |
} |
| 418 |
} |
| 419 |
else if ( !strcasecmp("ID",str.s) ) tag->handler = remove_id; |
| 420 |
else if ( !strcasecmp("FILTER",str.s) ) |
| 421 |
{ |
| 422 |
tag->handler = remove_filter; |
| 423 |
if ( !args->keep_sites ) remove_hdr_lines(args->hdr_out,BCF_HL_FLT); |
| 424 |
} |
| 425 |
else if ( !strcasecmp("QUAL",str.s) ) tag->handler = remove_qual; |
| 426 |
else if ( !strcasecmp("INFO",str.s) ) |
| 427 |
{ |
| 428 |
if ( needs_info ) error("Error: `--remove INFO` is executed first, cannot combine with `--set-id %s`\n",args->set_ids_fmt); |
| 429 |
tag->handler = remove_info; |
| 430 |
if ( !args->keep_sites ) remove_hdr_lines(args->hdr_out,BCF_HL_INFO); |
| 431 |
} |
| 432 |
else if ( !strcasecmp("FMT",str.s) || !strcasecmp("FORMAT",str.s) ) |
| 433 |
{ |
| 434 |
tag->handler = remove_format; |
| 435 |
if ( !args->keep_sites ) remove_hdr_lines(args->hdr_out,BCF_HL_FMT); |
| 436 |
} |
| 437 |
else if ( str.l ) |
| 438 |
{ |
| 439 |
int id = bcf_hdr_id2int(args->hdr, BCF_DT_ID, str.s); |
| 440 |
if ( bcf_hdr_idinfo_exists(args->hdr,BCF_HL_INFO,id) ) error("Error: did you mean INFO/%s?\n",str.s); |
| 441 |
if ( bcf_hdr_idinfo_exists(args->hdr,BCF_HL_FMT,id) ) error("Error: did you mean FORMAT/%s?\n",str.s); |
| 442 |
|
| 443 |
if ( !args->keep_sites ) |
| 444 |
{ |
| 445 |
if ( str.s[0]=='#' && str.s[1]=='#' ) |
| 446 |
bcf_hdr_remove(args->hdr_out,BCF_HL_GEN,str.s+2); |
| 447 |
else |
| 448 |
bcf_hdr_remove(args->hdr_out,BCF_HL_STR,str.s); |
| 449 |
} |
| 450 |
args->nrm--; |
| 451 |
} |
| 452 |
|
| 453 |
ss = *se ? se+1 : se; |
| 454 |
} |
| 455 |
free(str.s); |
| 456 |
if ( keep_flt || keep_info || keep_fmt ) |
| 457 |
{ |
| 458 |
int j; |
| 459 |
for (j=0; j<args->hdr->nhrec; j++) |
| 460 |
{ |
| 461 |
bcf_hrec_t *hrec = args->hdr->hrec[j]; |
| 462 |
if ( hrec->type!=BCF_HL_FLT && hrec->type!=BCF_HL_INFO && hrec->type!=BCF_HL_FMT ) continue; |
| 463 |
if ( !keep_flt && hrec->type==BCF_HL_FLT ) continue; |
| 464 |
if ( !keep_info && hrec->type==BCF_HL_INFO ) continue; |
| 465 |
if ( !keep_fmt && hrec->type==BCF_HL_FMT ) continue; |
| 466 |
int k = bcf_hrec_find_key(hrec,"ID"); |
| 467 |
assert( k>=0 ); |
| 468 |
int value, ret = khash_str2int_get(keep,hrec->vals[k],&value); |
| 469 |
if ( ret==0 && value>>hrec->type ) |
| 470 |
{ |
| 471 |
if ( hrec->type==BCF_HL_FLT && !strcmp("PASS",hrec->vals[k]) ) args->flt_keep_pass = 1; |
| 472 |
continue; |
| 473 |
} |
| 474 |
args->nrm++; |
| 475 |
args->rm = (rm_tag_t*) realloc(args->rm,sizeof(rm_tag_t)*args->nrm); |
| 476 |
rm_tag_t *tag = &args->rm[args->nrm-1]; |
| 477 |
if ( hrec->type==BCF_HL_INFO ) tag->handler = remove_info_tag; |
| 478 |
else if ( hrec->type==BCF_HL_FMT ) tag->handler = remove_format_tag; |
| 479 |
else |
| 480 |
{ |
| 481 |
tag->handler = remove_filter; |
| 482 |
tag->hdr_id = bcf_hdr_id2int(args->hdr, BCF_DT_ID, hrec->vals[k]); |
| 483 |
} |
| 484 |
tag->key = strdup(hrec->vals[k]); |
| 485 |
if ( !args->keep_sites ) bcf_hdr_remove(args->hdr_out,hrec->type,tag->key); |
| 486 |
} |
| 487 |
} |
| 488 |
khash_str2int_destroy_free(keep); |
| 489 |
if ( !args->nrm ) error("No matching tag in -x %s\n", args->remove_annots); |
| 490 |
if (bcf_hdr_sync(args->hdr_out) < 0) |
| 491 |
error_errno("[%s] Failed to update header", __func__); |
| 492 |
} |
| 493 |
static void init_header_lines(args_t *args) |
| 494 |
{ |
| 495 |
if ( args->header_fname ) |
| 496 |
{ |
| 497 |
htsFile *file = hts_open(args->header_fname, "rb"); |
| 498 |
if ( !file ) error("Error reading %s\n", args->header_fname); |
| 499 |
kstring_t str = {0,0,0}; |
| 500 |
while ( hts_getline(file, KS_SEP_LINE, &str) > 0 ) |
| 501 |
{ |
| 502 |
if ( bcf_hdr_append(args->hdr_out,str.s) ) error("Could not parse %s: %s\n", args->header_fname, str.s); |
| 503 |
bcf_hdr_append(args->hdr,str.s); |
| 504 |
} |
| 505 |
if ( hts_close(file)!=0 ) error("[%s] Error: close failed .. %s\n", __func__,args->header_fname); |
| 506 |
free(str.s); |
| 507 |
} |
| 508 |
if ( args->header_lines ) |
| 509 |
{ |
| 510 |
int i, n = dbuf_n(args->header_lines); |
| 511 |
for (i=0; i<n; i++) |
| 512 |
{ |
| 513 |
char *line = dbuf_ith(args->header_lines,i); |
| 514 |
if ( bcf_hdr_append(args->hdr_out,line) ) error("Could not parse the header line: %s\n", line); |
| 515 |
bcf_hdr_append(args->hdr,line); |
| 516 |
} |
| 517 |
dbuf_destroy_free(args->header_lines); |
| 518 |
args->header_lines = NULL; |
| 519 |
} |
| 520 |
if (bcf_hdr_sync(args->hdr_out) < 0) |
| 521 |
error_errno("[%s] Failed to update output header", __func__); |
| 522 |
if (bcf_hdr_sync(args->hdr) < 0) |
| 523 |
error_errno("[%s] Failed to update input header", __func__); |
| 524 |
} |
| 525 |
static int vcf_getter_info_str2str(args_t *args, bcf1_t *rec, annot_col_t *col, void **ptr, int *mptr) |
| 526 |
{ |
| 527 |
return bcf_get_info_string(args->tgts_hdr,rec,col->hdr_key_src,ptr,mptr); |
| 528 |
} |
| 529 |
static int vcf_getter_id2str(args_t *args, bcf1_t *rec, annot_col_t *col, void **ptr, int *mptr) |
| 530 |
{ |
| 531 |
char *str = *((char**)ptr); |
| 532 |
int i, len = strlen(rec->d.id); |
| 533 |
if ( len >= *mptr ) str = realloc(str, len+1); |
| 534 |
for (i=0; i<len; i++) |
| 535 |
str[i] = rec->d.id[i]==';' ? ',' : rec->d.id[i]; |
| 536 |
str[len] = 0; |
| 537 |
*((char**)ptr) = str; |
| 538 |
*mptr = len+1; |
| 539 |
return len; |
| 540 |
} |
| 541 |
inline static int vcf_getter_filter2str_core(bcf_hdr_t *hdr, bcf1_t *rec, char **ptr, int *mptr) |
| 542 |
{ |
| 543 |
if ( !(rec->unpacked & BCF_UN_FLT) ) bcf_unpack(rec, BCF_UN_FLT); |
| 544 |
|
| 545 |
kstring_t str; |
| 546 |
str.s = *ptr; |
| 547 |
str.m = *mptr; |
| 548 |
str.l = 0; |
| 549 |
|
| 550 |
int i; |
| 551 |
if ( rec->d.n_flt ) |
| 552 |
{ |
| 553 |
for (i=0; i<rec->d.n_flt; i++) |
| 554 |
{ |
| 555 |
if (i) kputc(',', &str); |
| 556 |
kputs(bcf_hdr_int2id(hdr,BCF_DT_ID,rec->d.flt[i]), &str); |
| 557 |
} |
| 558 |
} |
| 559 |
else kputc('.', &str); |
| 560 |
|
| 561 |
*ptr = str.s; |
| 562 |
*mptr = str.m; |
| 563 |
return str.l; |
| 564 |
} |
| 565 |
static int vcf_getter_filter2str_local(args_t *args, bcf1_t *rec, annot_col_t *col, void **ptr, int *mptr) |
| 566 |
{ |
| 567 |
return vcf_getter_filter2str_core(args->hdr_out, args->current_rec, (char**)ptr, mptr); |
| 568 |
} |
| 569 |
static int vcf_getter_filter2str(args_t *args, bcf1_t *rec, annot_col_t *col, void **ptr, int *mptr) |
| 570 |
{ |
| 571 |
return vcf_getter_filter2str_core(args->tgts_hdr, rec, (char**)ptr, mptr); |
| 572 |
} |
| 573 |
static int setter_filter(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 574 |
{ |
| 575 |
if ( !data ) error("Error: the --merge-logic option cannot be used with FILTER (yet?)\n"); |
| 576 |
|
| 577 |
|
| 578 |
annot_line_t *tab = (annot_line_t*) data; |
| 579 |
if ( tab->cols[col->icol][0]=='.' && !tab->cols[col->icol][1] ) |
| 580 |
{ |
| 581 |
if ( (col->replace & CARRY_OVER_MISSING) && (col->replace & (REPLACE_ALL|REPLACE_NON_MISSING)) ) bcf_update_filter(args->hdr_out,line,NULL,0); |
| 582 |
return 0; |
| 583 |
} |
| 584 |
hts_expand(int,1,args->mtmpi,args->tmpi); |
| 585 |
args->tmpi[0] = bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, tab->cols[col->icol]); |
| 586 |
if ( args->tmpi[0]<0 ) error("The FILTER \"%s\" is not defined in the header, was the -h option provided?\n", tab->cols[col->icol]); |
| 587 |
if ( col->replace & SET_OR_APPEND ) return bcf_add_filter(args->hdr_out,line,args->tmpi[0]); |
| 588 |
if ( !(col->replace & REPLACE_MISSING) ) |
| 589 |
{ |
| 590 |
bcf_update_filter(args->hdr_out,line,NULL,0); |
| 591 |
return bcf_update_filter(args->hdr_out,line,args->tmpi,1); |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
if ( !(line->unpacked & BCF_UN_FLT) ) bcf_unpack(line, BCF_UN_FLT); |
| 596 |
if ( !line->d.n_flt ) |
| 597 |
return bcf_update_filter(args->hdr_out,line,args->tmpi,1); |
| 598 |
|
| 599 |
return 0; |
| 600 |
} |
| 601 |
static int vcf_setter_filter(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 602 |
{ |
| 603 |
int i, ret = 0; |
| 604 |
bcf1_t *rec = (bcf1_t*) data; |
| 605 |
if ( !(rec->unpacked & BCF_UN_FLT) ) bcf_unpack(rec, BCF_UN_FLT); |
| 606 |
if ( !(line->unpacked & BCF_UN_FLT) ) bcf_unpack(line, BCF_UN_FLT); |
| 607 |
if ( !rec->d.n_flt ) |
| 608 |
{ |
| 609 |
if ( (col->replace & CARRY_OVER_MISSING) && (col->replace & (REPLACE_ALL|REPLACE_NON_MISSING)) ) bcf_update_filter(args->hdr_out,line,NULL,0); |
| 610 |
return 0; |
| 611 |
} |
| 612 |
if ( col->replace & (SET_OR_APPEND|REPLACE_MISSING) ) |
| 613 |
{ |
| 614 |
if ( (col->replace & REPLACE_MISSING) && line->d.n_flt ) return 0; |
| 615 |
for (i=0; i<rec->d.n_flt; i++) |
| 616 |
{ |
| 617 |
const char *flt = bcf_hdr_int2id(args->files->readers[1].header, BCF_DT_ID, rec->d.flt[i]); |
| 618 |
if ( bcf_add_filter(args->hdr_out,line,bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, flt)) < 0 ) ret = -1; |
| 619 |
} |
| 620 |
return ret; |
| 621 |
} |
| 622 |
hts_expand(int,rec->d.n_flt,args->mtmpi,args->tmpi); |
| 623 |
for (i=0; i<rec->d.n_flt; i++) |
| 624 |
{ |
| 625 |
const char *flt = bcf_hdr_int2id(args->files->readers[1].header, BCF_DT_ID, rec->d.flt[i]); |
| 626 |
args->tmpi[i] = bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, flt); |
| 627 |
} |
| 628 |
bcf_update_filter(args->hdr_out,line,NULL,0); |
| 629 |
return bcf_update_filter(args->hdr_out,line,args->tmpi,rec->d.n_flt); |
| 630 |
} |
| 631 |
static int setter_pos(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 632 |
{ |
| 633 |
annot_line_t *tab = (annot_line_t*) data; |
| 634 |
if ( tab->cols[col->icol] && tab->cols[col->icol][0]=='.' && !tab->cols[col->icol][1] ) return 0; |
| 635 |
char *tmp; |
| 636 |
int pos = strtol(tab->cols[col->icol], &tmp, 10); |
| 637 |
if ( tmp==tab->cols[col->icol] ) |
| 638 |
error("Could not parse -POS at %s:%"PRId64" .. [%s]\n",bcf_seqname(args->hdr,line),(int64_t)line->pos+1,tab->cols[col->icol]); |
| 639 |
line->pos = pos - 1; |
| 640 |
return 0; |
| 641 |
} |
| 642 |
static int setter_id(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 643 |
{ |
| 644 |
if ( !data ) error("Error: the --merge-logic option cannot be used with ID (yet?)\n"); |
| 645 |
if ( col->replace & MATCH_VALUE ) return 0; |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
annot_line_t *tab = (annot_line_t*) data; |
| 657 |
if ( tab->cols[col->icol] && tab->cols[col->icol][0]=='.' && !tab->cols[col->icol][1] ) return 0; |
| 658 |
if ( col->replace & SET_OR_APPEND ) return bcf_add_id(args->hdr_out,line,tab->cols[col->icol]); |
| 659 |
if ( !(col->replace & REPLACE_MISSING) ) return bcf_update_id(args->hdr_out,line,tab->cols[col->icol]); |
| 660 |
|
| 661 |
|
| 662 |
if ( !line->d.id || (line->d.id[0]=='.' && !line->d.id[1]) ) |
| 663 |
return bcf_update_id(args->hdr_out,line,tab->cols[col->icol]); |
| 664 |
return 0; |
| 665 |
} |
| 666 |
static int vcf_setter_id(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 667 |
{ |
| 668 |
if ( col->replace & MATCH_VALUE ) return 0; |
| 669 |
|
| 670 |
bcf1_t *rec = (bcf1_t*) data; |
| 671 |
|
| 672 |
char *id; |
| 673 |
if ( col->getter ) |
| 674 |
{ |
| 675 |
int nret = col->getter(args,rec,col,&col->ptr,&col->mptr); |
| 676 |
id = (char*) col->ptr; |
| 677 |
if ( nret<=0 || (nret==1 && *id=='.') ) return 0; |
| 678 |
} |
| 679 |
else |
| 680 |
{ |
| 681 |
if ( rec->d.id && rec->d.id[0]=='.' && !rec->d.id[1] ) return 0; |
| 682 |
id = rec->d.id; |
| 683 |
} |
| 684 |
if ( col->replace & SET_OR_APPEND ) return bcf_add_id(args->hdr_out,line,id); |
| 685 |
if ( !(col->replace & REPLACE_MISSING) ) return bcf_update_id(args->hdr_out,line,id); |
| 686 |
|
| 687 |
|
| 688 |
if ( !line->d.id || (line->d.id[0]=='.' && !line->d.id[1]) ) |
| 689 |
return bcf_update_id(args->hdr_out,line,id); |
| 690 |
return 0; |
| 691 |
} |
| 692 |
static int vcf_setter_ref(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 693 |
{ |
| 694 |
bcf1_t *rec = (bcf1_t*) data; |
| 695 |
if ( !strcmp(rec->d.allele[0],line->d.allele[0]) ) return 0; |
| 696 |
const char **als = (const char**) malloc(sizeof(char*)*line->n_allele); |
| 697 |
als[0] = rec->d.allele[0]; |
| 698 |
int i; |
| 699 |
for (i=1; i<line->n_allele; i++) als[i] = line->d.allele[i]; |
| 700 |
int ret = bcf_update_alleles(args->hdr_out, line, als, line->n_allele); |
| 701 |
free(als); |
| 702 |
return ret; |
| 703 |
} |
| 704 |
static int vcf_setter_alt(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 705 |
{ |
| 706 |
bcf1_t *rec = (bcf1_t*) data; |
| 707 |
int i; |
| 708 |
if ( line->n_allele>1 && (col->replace & REPLACE_MISSING) ) return 0; |
| 709 |
if ( rec->n_allele==line->n_allele ) |
| 710 |
{ |
| 711 |
for (i=1; i<rec->n_allele; i++) if ( strcmp(rec->d.allele[i],line->d.allele[i]) ) break; |
| 712 |
if ( i==rec->n_allele ) return 0; |
| 713 |
} |
| 714 |
const char **als = (const char**) malloc(sizeof(char*)*rec->n_allele); |
| 715 |
als[0] = line->d.allele[0]; |
| 716 |
for (i=1; i<rec->n_allele; i++) als[i] = rec->d.allele[i]; |
| 717 |
int ret = bcf_update_alleles(args->hdr_out, line, als, rec->n_allele); |
| 718 |
free(als); |
| 719 |
return ret; |
| 720 |
} |
| 721 |
static int setter_qual(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 722 |
{ |
| 723 |
if ( !data ) error("Error: the --merge-logic option cannot be used with QUAL (yet?)\n"); |
| 724 |
|
| 725 |
annot_line_t *tab = (annot_line_t*) data; |
| 726 |
char *str = tab->cols[col->icol]; |
| 727 |
if ( str[0]=='.' && str[1]==0 ) |
| 728 |
{ |
| 729 |
if ( (col->replace & CARRY_OVER_MISSING) && (col->replace & (REPLACE_ALL|REPLACE_NON_MISSING)) ) bcf_float_set_missing(line->qual); |
| 730 |
return 0; |
| 731 |
} |
| 732 |
if ( (col->replace & REPLACE_MISSING) && !bcf_float_is_missing(line->qual) ) return 0; |
| 733 |
|
| 734 |
line->qual = strtod(str, &str); |
| 735 |
if ( str == tab->cols[col->icol] ) |
| 736 |
error("Could not parse %s at %s:%"PRId64" .. [%s]\n", col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1,tab->cols[col->icol]); |
| 737 |
return 0; |
| 738 |
} |
| 739 |
static int vcf_setter_qual(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 740 |
{ |
| 741 |
bcf1_t *rec = (bcf1_t*) data; |
| 742 |
if ( bcf_float_is_missing(rec->qual) ) |
| 743 |
{ |
| 744 |
if ( (col->replace & CARRY_OVER_MISSING) && (col->replace & (REPLACE_ALL|REPLACE_NON_MISSING)) ) bcf_float_set_missing(line->qual); |
| 745 |
return 0; |
| 746 |
} |
| 747 |
if ( (col->replace & REPLACE_MISSING) && !bcf_float_is_missing(line->qual) ) return 0; |
| 748 |
line->qual = rec->qual; |
| 749 |
return 0; |
| 750 |
} |
| 751 |
static int setter_info_flag(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 752 |
{ |
| 753 |
if ( !data ) error("Error: the --merge-logic option cannot be used with INFO type=Flag (yet?)\n"); |
| 754 |
|
| 755 |
annot_line_t *tab = (annot_line_t*) data; |
| 756 |
char *str = tab->cols[col->icol]; |
| 757 |
if ( str[0]=='.' && str[1]==0 ) |
| 758 |
{ |
| 759 |
if ( (col->replace & CARRY_OVER_MISSING) && (col->replace & (REPLACE_ALL|REPLACE_NON_MISSING)) ) bcf_update_info_flag(args->hdr_out,line,col->hdr_key_dst,NULL,0); |
| 760 |
return 0; |
| 761 |
} |
| 762 |
|
| 763 |
if ( str[0]=='1' && str[1]==0 ) return bcf_update_info_flag(args->hdr_out,line,col->hdr_key_dst,NULL,1); |
| 764 |
if ( str[0]=='0' && str[1]==0 ) return bcf_update_info_flag(args->hdr_out,line,col->hdr_key_dst,NULL,0); |
| 765 |
error("Could not parse %s at %s:%"PRId64" .. [%s]\n", col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1,tab->cols[col->icol]); |
| 766 |
return -1; |
| 767 |
} |
| 768 |
static int vcf_setter_info_flag(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 769 |
{ |
| 770 |
bcf1_t *rec = (bcf1_t*) data; |
| 771 |
int flag = bcf_get_info_flag(args->files->readers[1].header,rec,col->hdr_key_src,NULL,NULL); |
| 772 |
bcf_update_info_flag(args->hdr_out,line,col->hdr_key_dst,NULL,flag); |
| 773 |
return 0; |
| 774 |
} |
| 775 |
static int setter_ARinfo_int32(args_t *args, bcf1_t *line, annot_col_t *col, int nals, char **als, int ntmpi) |
| 776 |
{ |
| 777 |
if ( col->number==BCF_VL_A && ntmpi!=nals-1 && (ntmpi!=1 || args->tmpi[0]!=bcf_int32_missing || args->tmpi[1]!=bcf_int32_vector_end) ) |
| 778 |
error("Incorrect number of values (%d) for the %s tag at %s:%"PRId64"\n", ntmpi,col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 779 |
else if ( col->number==BCF_VL_R && ntmpi!=nals && (ntmpi!=1 || args->tmpi[0]!=bcf_int32_missing || args->tmpi[1]!=bcf_int32_vector_end) ) |
| 780 |
error("Incorrect number of values (%d) for the %s tag at %s:%"PRId64"\n", ntmpi,col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 781 |
|
| 782 |
int ndst = col->number==BCF_VL_A ? line->n_allele - 1 : line->n_allele; |
| 783 |
int *map = vcmp_map_ARvalues(args->vcmp,ndst,nals,als,line->n_allele,line->d.allele); |
| 784 |
if ( !map ) error("REF alleles not compatible at %s:%"PRId64"\n", bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 785 |
|
| 786 |
|
| 787 |
int ntmpi2 = bcf_get_info_float(args->hdr, line, col->hdr_key_dst, &args->tmpi2, &args->mtmpi2); |
| 788 |
if ( ntmpi2 < ndst ) hts_expand(int32_t,ndst,args->mtmpi2,args->tmpi2); |
| 789 |
|
| 790 |
int i; |
| 791 |
for (i=0; i<ndst; i++) |
| 792 |
{ |
| 793 |
if ( map[i]<0 ) |
| 794 |
{ |
| 795 |
if ( ntmpi2 < ndst ) args->tmpi2[i] = bcf_int32_missing; |
| 796 |
continue; |
| 797 |
} |
| 798 |
if ( ntmpi2==ndst && (col->replace & REPLACE_MISSING) |
| 799 |
&& args->tmpi2[i]!=bcf_int32_missing |
| 800 |
&& args->tmpi2[i]!=bcf_int32_vector_end ) continue; |
| 801 |
|
| 802 |
args->tmpi2[i] = args->tmpi[ map[i] ]; |
| 803 |
} |
| 804 |
return bcf_update_info_int32(args->hdr_out,line,col->hdr_key_dst,args->tmpi2,ndst); |
| 805 |
} |
| 806 |
static int setter_info_int(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 807 |
{ |
| 808 |
annot_line_t *tab = (annot_line_t*) data; |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
if ( col->replace & SET_OR_APPEND ) col->merge_method=MM_APPEND; |
| 813 |
|
| 814 |
if ( !tab ) |
| 815 |
{ |
| 816 |
if ( col->merge_method!=MM_SUM && col->merge_method!=MM_AVG && |
| 817 |
col->merge_method!=MM_MIN && col->merge_method!=MM_MAX && |
| 818 |
col->merge_method!=MM_APPEND && |
| 819 |
col->merge_method!=MM_APPEND_MISSING ) |
| 820 |
error("Error: at the moment only the sum,avg,min,max,append,append-missing options are supported with --merge-logic for INFO type=Integer\n"); |
| 821 |
} |
| 822 |
|
| 823 |
int i,ntmpi = 0; |
| 824 |
if ( (col->replace & SET_OR_APPEND) && !col->mm_dbl_nused ) |
| 825 |
{ |
| 826 |
ntmpi = bcf_get_info_int32(args->hdr, line, col->hdr_key_dst, &args->tmpi, &args->mtmpi); |
| 827 |
if ( ntmpi>0 && (args->tmpi[0]!=bcf_int32_missing || (col->replace & CARRY_OVER_MISSING)) ) |
| 828 |
{ |
| 829 |
col->mm_dbl_nused = col->mm_dbl_ndat = ntmpi; |
| 830 |
hts_expand(double,col->mm_dbl_nused,col->mm_dbl_nalloc,col->mm_dbl); |
| 831 |
for (i=0; i<ntmpi; i++) |
| 832 |
col->mm_dbl[i] = args->tmpi[i]; |
| 833 |
col->mm_dbl_ndat = 1; |
| 834 |
} |
| 835 |
ntmpi = 0; |
| 836 |
} |
| 837 |
if ( tab ) |
| 838 |
{ |
| 839 |
char *str = tab->cols[col->icol], *end = str; |
| 840 |
if ( str[0]=='.' && str[1]==0 && col->merge_method!=MM_APPEND_MISSING && !(col->replace & CARRY_OVER_MISSING) ) return 1; |
| 841 |
|
| 842 |
while ( *end ) |
| 843 |
{ |
| 844 |
ntmpi++; |
| 845 |
hts_expand(int32_t,ntmpi,args->mtmpi,args->tmpi); |
| 846 |
if ( str[0]=='.' && (str[1]==0 || str[1]==',') ) |
| 847 |
{ |
| 848 |
if ( col->merge_method==MM_APPEND_MISSING || (col->replace & CARRY_OVER_MISSING) ) |
| 849 |
args->tmpi[ntmpi-1] = bcf_int32_missing; |
| 850 |
else |
| 851 |
ntmpi--; |
| 852 |
if ( str[1]==0 ) end = str+1; |
| 853 |
str += 2; |
| 854 |
} |
| 855 |
else |
| 856 |
{ |
| 857 |
args->tmpi[ntmpi-1] = strtol(str, &end, 10); |
| 858 |
if ( end==str ) |
| 859 |
error("Could not parse %s at %s:%"PRId64" .. [%s]\n", col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1,tab->cols[col->icol]); |
| 860 |
str = end+1; |
| 861 |
} |
| 862 |
} |
| 863 |
if ( col->merge_method!=MM_FIRST ) |
| 864 |
{ |
| 865 |
if ( !col->mm_dbl_nused ) |
| 866 |
{ |
| 867 |
col->mm_dbl_nused = ntmpi; |
| 868 |
hts_expand(double,col->mm_dbl_nused,col->mm_dbl_nalloc,col->mm_dbl); |
| 869 |
for (i=0; i<ntmpi; i++) |
| 870 |
col->mm_dbl[i] = args->tmpi[i]; |
| 871 |
} |
| 872 |
else |
| 873 |
{ |
| 874 |
if ( col->merge_method==MM_APPEND || col->merge_method==MM_APPEND_MISSING ) |
| 875 |
{ |
| 876 |
int nori = col->mm_dbl_nused; |
| 877 |
col->mm_dbl_nused += ntmpi; |
| 878 |
hts_expand(double,col->mm_dbl_nused,col->mm_dbl_nalloc,col->mm_dbl); |
| 879 |
for (i=0; i<ntmpi; i++) |
| 880 |
col->mm_dbl[i+nori] = args->tmpi[i]; |
| 881 |
} |
| 882 |
else |
| 883 |
{ |
| 884 |
if ( ntmpi!=col->mm_dbl_nused ) error("Error: cannot merge fields of unequal length\n"); |
| 885 |
if ( col->merge_method==MM_SUM || col->merge_method==MM_AVG ) |
| 886 |
for (i=0; i<ntmpi; i++) col->mm_dbl[i] += args->tmpi[i]; |
| 887 |
else if ( col->merge_method==MM_MIN ) |
| 888 |
for (i=0; i<ntmpi; i++) { if ( col->mm_dbl[i] > args->tmpi[i] ) col->mm_dbl[i] = args->tmpi[i]; } |
| 889 |
else if ( col->merge_method==MM_MAX ) |
| 890 |
for (i=0; i<ntmpi; i++) { if ( col->mm_dbl[i] < args->tmpi[i] ) col->mm_dbl[i] = args->tmpi[i]; } |
| 891 |
} |
| 892 |
} |
| 893 |
col->mm_dbl_ndat++; |
| 894 |
return 1; |
| 895 |
} |
| 896 |
} |
| 897 |
else if ( col->merge_method==MM_SUM || col->merge_method==MM_MIN || col->merge_method==MM_MAX || col->merge_method==MM_APPEND || col->merge_method==MM_APPEND_MISSING ) |
| 898 |
{ |
| 899 |
ntmpi = col->mm_dbl_nused; |
| 900 |
hts_expand(int32_t,ntmpi,args->mtmpi,args->tmpi); |
| 901 |
for (i=0; i<ntmpi; i++) args->tmpi[i] = col->mm_dbl[i]; |
| 902 |
col->mm_dbl_nused = col->mm_dbl_ndat = 0; |
| 903 |
} |
| 904 |
else if ( col->merge_method==MM_AVG ) |
| 905 |
{ |
| 906 |
ntmpi = col->mm_dbl_nused; |
| 907 |
hts_expand(int32_t,ntmpi,args->mtmpi,args->tmpi); |
| 908 |
for (i=0; i<ntmpi; i++) args->tmpi[i] = col->mm_dbl[i]/col->mm_dbl_ndat; |
| 909 |
col->mm_dbl_nused = col->mm_dbl_ndat = 0; |
| 910 |
} |
| 911 |
|
| 912 |
if ( col->number==BCF_VL_A || col->number==BCF_VL_R ) |
| 913 |
return setter_ARinfo_int32(args,line,col,tab->nals,tab->als,ntmpi); |
| 914 |
|
| 915 |
if ( col->replace & REPLACE_MISSING ) |
| 916 |
{ |
| 917 |
int ret = bcf_get_info_int32(args->hdr, line, col->hdr_key_dst, &args->tmpi2, &args->mtmpi2); |
| 918 |
if ( ret>0 && args->tmpi2[0]!=bcf_int32_missing ) return 0; |
| 919 |
} |
| 920 |
return bcf_update_info_int32(args->hdr_out,line,col->hdr_key_dst,args->tmpi,ntmpi); |
| 921 |
} |
| 922 |
static int vcf_setter_info_int(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 923 |
{ |
| 924 |
bcf1_t *rec = (bcf1_t*) data; |
| 925 |
int ntmpi = bcf_get_info_int32(args->files->readers[1].header,rec,col->hdr_key_src,&args->tmpi,&args->mtmpi); |
| 926 |
if ( ntmpi < 0 ) return 0; |
| 927 |
|
| 928 |
if ( col->number==BCF_VL_A || col->number==BCF_VL_R ) |
| 929 |
return setter_ARinfo_int32(args,line,col,rec->n_allele,rec->d.allele,ntmpi); |
| 930 |
|
| 931 |
if ( col->replace & REPLACE_MISSING ) |
| 932 |
{ |
| 933 |
int ret = bcf_get_info_int32(args->hdr, line, col->hdr_key_dst, &args->tmpi2, &args->mtmpi2); |
| 934 |
if ( ret>0 && args->tmpi2[0]!=bcf_int32_missing ) return 0; |
| 935 |
} |
| 936 |
|
| 937 |
return bcf_update_info_int32(args->hdr_out,line,col->hdr_key_dst,args->tmpi,ntmpi); |
| 938 |
} |
| 939 |
static int setter_ARinfo_real(args_t *args, bcf1_t *line, annot_col_t *col, int nals, char **als, int ntmpf) |
| 940 |
{ |
| 941 |
if ( col->number==BCF_VL_A && ntmpf!=nals-1 && (ntmpf!=1 || !bcf_float_is_missing(args->tmpf[0]) || !bcf_float_is_vector_end(args->tmpf[0])) ) |
| 942 |
error("Incorrect number of values (%d) for the %s tag at %s:%"PRId64"\n", ntmpf,col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 943 |
else if ( col->number==BCF_VL_R && ntmpf!=nals && (ntmpf!=1 || !bcf_float_is_missing(args->tmpf[0]) || !bcf_float_is_vector_end(args->tmpf[0])) ) |
| 944 |
error("Incorrect number of values (%d) for the %s tag at %s:%"PRId64"\n", ntmpf,col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 945 |
|
| 946 |
int ndst = col->number==BCF_VL_A ? line->n_allele - 1 : line->n_allele; |
| 947 |
int *map = vcmp_map_ARvalues(args->vcmp,ndst,nals,als,line->n_allele,line->d.allele); |
| 948 |
if ( !map ) error("REF alleles not compatible at %s:%"PRId64"\n", bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 949 |
|
| 950 |
|
| 951 |
int ntmpf2 = bcf_get_info_float(args->hdr, line, col->hdr_key_dst, &args->tmpf2, &args->mtmpf2); |
| 952 |
if ( ntmpf2 < ndst ) hts_expand(float,ndst,args->mtmpf2,args->tmpf2); |
| 953 |
|
| 954 |
int i; |
| 955 |
for (i=0; i<ndst; i++) |
| 956 |
{ |
| 957 |
if ( map[i]<0 ) |
| 958 |
{ |
| 959 |
if ( ntmpf2 < ndst ) bcf_float_set_missing(args->tmpf2[i]); |
| 960 |
continue; |
| 961 |
} |
| 962 |
if ( ntmpf2==ndst && (col->replace & REPLACE_MISSING) |
| 963 |
&& !bcf_float_is_missing(args->tmpf2[i]) |
| 964 |
&& !bcf_float_is_vector_end(args->tmpf2[i]) ) continue; |
| 965 |
|
| 966 |
args->tmpf2[i] = args->tmpf[ map[i] ]; |
| 967 |
} |
| 968 |
return bcf_update_info_float(args->hdr_out,line,col->hdr_key_dst,args->tmpf2,ndst); |
| 969 |
} |
| 970 |
static int setter_info_real(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 971 |
{ |
| 972 |
annot_line_t *tab = (annot_line_t*) data; |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
if ( col->replace & SET_OR_APPEND ) col->merge_method=MM_APPEND; |
| 977 |
|
| 978 |
if ( !tab ) |
| 979 |
{ |
| 980 |
if ( col->merge_method!=MM_SUM && col->merge_method!=MM_AVG && |
| 981 |
col->merge_method!=MM_MIN && col->merge_method!=MM_MAX && |
| 982 |
col->merge_method!=MM_APPEND && |
| 983 |
col->merge_method!=MM_APPEND_MISSING ) |
| 984 |
error("Error: at the moment only the sum,avg,min,max,append,append-missing options are supported with --merge-logic for INFO type=Float\n"); |
| 985 |
} |
| 986 |
|
| 987 |
int i,ntmpf = 0; |
| 988 |
if ( (col->replace & SET_OR_APPEND) && !col->mm_dbl_nused ) |
| 989 |
{ |
| 990 |
ntmpf = bcf_get_info_float(args->hdr, line, col->hdr_key_dst, &args->tmpf, &args->mtmpf); |
| 991 |
if ( ntmpf>0 && (!bcf_float_is_missing(args->tmpf[0]) || (col->replace & CARRY_OVER_MISSING)) ) |
| 992 |
{ |
| 993 |
col->mm_dbl_nused = ntmpf; |
| 994 |
hts_expand(double,col->mm_dbl_nused,col->mm_dbl_nalloc,col->mm_dbl); |
| 995 |
for (i=0; i<ntmpf; i++) |
| 996 |
if ( bcf_float_is_missing(args->tmpf[i]) ) |
| 997 |
bcf_double_set_missing(col->mm_dbl[i]); |
| 998 |
else |
| 999 |
col->mm_dbl[i] = args->tmpf[i]; |
| 1000 |
col->mm_dbl_ndat = 1; |
| 1001 |
} |
| 1002 |
ntmpf = 0; |
| 1003 |
} |
| 1004 |
if ( tab ) |
| 1005 |
{ |
| 1006 |
char *str = tab->cols[col->icol], *end = str; |
| 1007 |
if ( str[0]=='.' && str[1]==0 && col->merge_method!=MM_APPEND_MISSING && !(col->replace & CARRY_OVER_MISSING) ) return 1; |
| 1008 |
|
| 1009 |
while ( *end ) |
| 1010 |
{ |
| 1011 |
ntmpf++; |
| 1012 |
hts_expand(float,ntmpf,args->mtmpf,args->tmpf); |
| 1013 |
if ( str[0]=='.' && (str[1]==0 || str[1]==',') ) |
| 1014 |
{ |
| 1015 |
if ( col->merge_method==MM_APPEND_MISSING || (col->replace & CARRY_OVER_MISSING) ) |
| 1016 |
bcf_float_set_missing(args->tmpf[ntmpf-1]); |
| 1017 |
else |
| 1018 |
ntmpf--; |
| 1019 |
if ( str[1]==0 ) end = str+1; |
| 1020 |
str += 2; |
| 1021 |
} |
| 1022 |
else |
| 1023 |
{ |
| 1024 |
args->tmpf[ntmpf-1] = strtod(str, &end); |
| 1025 |
if ( end==str ) |
| 1026 |
error("Could not parse %s at %s:%"PRId64" .. [%s]\n", col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1,tab->cols[col->icol]); |
| 1027 |
str = end+1; |
| 1028 |
} |
| 1029 |
} |
| 1030 |
if ( col->merge_method!=MM_FIRST ) |
| 1031 |
{ |
| 1032 |
if ( !col->mm_dbl_nused ) |
| 1033 |
{ |
| 1034 |
col->mm_dbl_nused = ntmpf; |
| 1035 |
hts_expand(double,col->mm_dbl_nused,col->mm_dbl_nalloc,col->mm_dbl); |
| 1036 |
for (i=0; i<ntmpf; i++) |
| 1037 |
{ |
| 1038 |
if ( bcf_float_is_missing(args->tmpf[i]) ) |
| 1039 |
bcf_double_set_missing(col->mm_dbl[i]); |
| 1040 |
else |
| 1041 |
col->mm_dbl[i] = args->tmpf[i]; |
| 1042 |
} |
| 1043 |
} |
| 1044 |
else |
| 1045 |
{ |
| 1046 |
if ( col->merge_method==MM_APPEND || col->merge_method==MM_APPEND_MISSING ) |
| 1047 |
{ |
| 1048 |
int nori = col->mm_dbl_nused; |
| 1049 |
col->mm_dbl_nused += ntmpf; |
| 1050 |
hts_expand(double,col->mm_dbl_nused,col->mm_dbl_nalloc,col->mm_dbl); |
| 1051 |
for (i=0; i<ntmpf; i++) |
| 1052 |
{ |
| 1053 |
if ( bcf_float_is_missing(args->tmpf[i]) ) |
| 1054 |
bcf_double_set_missing(col->mm_dbl[i+nori]); |
| 1055 |
else |
| 1056 |
col->mm_dbl[i+nori] = args->tmpf[i]; |
| 1057 |
} |
| 1058 |
} |
| 1059 |
else |
| 1060 |
{ |
| 1061 |
if ( ntmpf!=col->mm_dbl_nused ) error("Error: cannot merge fields of unequal length\n"); |
| 1062 |
if ( col->merge_method==MM_SUM || col->merge_method==MM_AVG ) |
| 1063 |
for (i=0; i<ntmpf; i++) col->mm_dbl[i] += args->tmpf[i]; |
| 1064 |
else if ( col->merge_method==MM_MIN ) |
| 1065 |
for (i=0; i<ntmpf; i++) { if ( col->mm_dbl[i] > args->tmpf[i] ) col->mm_dbl[i] = args->tmpf[i]; } |
| 1066 |
else if ( col->merge_method==MM_MAX ) |
| 1067 |
for (i=0; i<ntmpf; i++) { if ( col->mm_dbl[i] < args->tmpf[i] ) col->mm_dbl[i] = args->tmpf[i]; } |
| 1068 |
} |
| 1069 |
} |
| 1070 |
col->mm_dbl_ndat++; |
| 1071 |
return 1; |
| 1072 |
} |
| 1073 |
} |
| 1074 |
else if ( col->merge_method==MM_SUM || col->merge_method==MM_MIN || col->merge_method==MM_MAX || col->merge_method==MM_APPEND || col->merge_method==MM_APPEND_MISSING ) |
| 1075 |
{ |
| 1076 |
ntmpf = col->mm_dbl_nused; |
| 1077 |
hts_expand(int32_t,ntmpf,args->mtmpf,args->tmpf); |
| 1078 |
for (i=0; i<ntmpf; i++) |
| 1079 |
{ |
| 1080 |
if ( bcf_double_is_missing(col->mm_dbl[i]) ) |
| 1081 |
bcf_float_set_missing(args->tmpf[i]); |
| 1082 |
else |
| 1083 |
args->tmpf[i] = col->mm_dbl[i]; |
| 1084 |
} |
| 1085 |
col->mm_dbl_nused = col->mm_dbl_ndat = 0; |
| 1086 |
} |
| 1087 |
else if ( col->merge_method==MM_AVG ) |
| 1088 |
{ |
| 1089 |
ntmpf = col->mm_dbl_nused; |
| 1090 |
hts_expand(int32_t,ntmpf,args->mtmpf,args->tmpf); |
| 1091 |
for (i=0; i<ntmpf; i++) args->tmpf[i] = col->mm_dbl[i]/col->mm_dbl_ndat; |
| 1092 |
col->mm_dbl_nused = col->mm_dbl_ndat = 0; |
| 1093 |
} |
| 1094 |
|
| 1095 |
if ( col->number==BCF_VL_A || col->number==BCF_VL_R ) |
| 1096 |
return setter_ARinfo_real(args,line,col,tab->nals,tab->als,ntmpf); |
| 1097 |
|
| 1098 |
if ( col->replace & REPLACE_MISSING ) |
| 1099 |
{ |
| 1100 |
int ret = bcf_get_info_float(args->hdr, line, col->hdr_key_dst, &args->tmpf2, &args->mtmpf2); |
| 1101 |
if ( ret>0 && !bcf_float_is_missing(args->tmpf2[0]) ) return 0; |
| 1102 |
} |
| 1103 |
|
| 1104 |
return bcf_update_info_float(args->hdr_out,line,col->hdr_key_dst,args->tmpf,ntmpf); |
| 1105 |
} |
| 1106 |
static int vcf_setter_info_real(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 1107 |
{ |
| 1108 |
bcf1_t *rec = (bcf1_t*) data; |
| 1109 |
int ntmpf = bcf_get_info_float(args->files->readers[1].header,rec,col->hdr_key_src,&args->tmpf,&args->mtmpf); |
| 1110 |
if ( ntmpf < 0 ) return 0; |
| 1111 |
|
| 1112 |
if ( col->number==BCF_VL_A || col->number==BCF_VL_R ) |
| 1113 |
return setter_ARinfo_real(args,line,col,rec->n_allele,rec->d.allele,ntmpf); |
| 1114 |
|
| 1115 |
if ( col->replace & REPLACE_MISSING ) |
| 1116 |
{ |
| 1117 |
int ret = bcf_get_info_float(args->hdr, line, col->hdr_key_dst, &args->tmpf2, &args->mtmpf2); |
| 1118 |
if ( ret>0 && !bcf_float_is_missing(args->tmpf2[0]) ) return 0; |
| 1119 |
} |
| 1120 |
|
| 1121 |
return bcf_update_info_float(args->hdr_out,line,col->hdr_key_dst,args->tmpf,ntmpf); |
| 1122 |
} |
| 1123 |
int copy_string_field(char *src, int isrc, int src_len, kstring_t *dst, int idst); |
| 1124 |
static int setter_ARinfo_string(args_t *args, bcf1_t *line, annot_col_t *col, int nals, char **als) |
| 1125 |
{ |
| 1126 |
assert( col->merge_method==MM_FIRST ); |
| 1127 |
|
| 1128 |
int nsrc = 1, lsrc = 0; |
| 1129 |
while ( args->tmps[lsrc] ) |
| 1130 |
{ |
| 1131 |
if ( args->tmps[lsrc]==',' ) nsrc++; |
| 1132 |
lsrc++; |
| 1133 |
} |
| 1134 |
if ( col->number==BCF_VL_A && nsrc!=nals-1 && (nsrc!=1 || args->tmps[0]!='.' || args->tmps[1]!=0 ) ) |
| 1135 |
error("Incorrect number of values (%d) for the %s tag at %s:%"PRId64"\n", nsrc,col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 1136 |
else if ( col->number==BCF_VL_R && nsrc!=nals && (nsrc!=1 || args->tmps[0]!='.' || args->tmps[1]!=0 ) ) |
| 1137 |
error("Incorrect number of values (%d) for the %s tag at %s:%"PRId64"\n", nsrc,col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 1138 |
|
| 1139 |
int ndst = col->number==BCF_VL_A ? line->n_allele - 1 : line->n_allele; |
| 1140 |
int *map = vcmp_map_ARvalues(args->vcmp,ndst,nals,als,line->n_allele,line->d.allele); |
| 1141 |
if ( !map ) error("REF alleles not compatible at %s:%"PRId64"\n", bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 1142 |
|
| 1143 |
|
| 1144 |
int i, empty = 0, nstr, mstr = args->tmpks.m; |
| 1145 |
nstr = bcf_get_info_string(args->hdr, line, col->hdr_key_dst, &args->tmpks.s, &mstr); |
| 1146 |
args->tmpks.m = mstr; |
| 1147 |
if ( nstr<0 || (nstr==1 && args->tmpks.s[0]=='.' && args->tmpks.s[1]==0) ) |
| 1148 |
{ |
| 1149 |
empty = 0; |
| 1150 |
args->tmpks.l = 0; |
| 1151 |
kputc('.',&args->tmpks); |
| 1152 |
for (i=1; i<ndst; i++) kputs(",.",&args->tmpks); |
| 1153 |
} |
| 1154 |
else args->tmpks.l = nstr; |
| 1155 |
for (i=0; i<ndst; i++) |
| 1156 |
{ |
| 1157 |
if ( map[i]<0 ) |
| 1158 |
{ |
| 1159 |
if ( empty ) copy_string_field(".",0,1,&args->tmpks,i); |
| 1160 |
continue; |
| 1161 |
} |
| 1162 |
if ( col->replace & REPLACE_MISSING ) |
| 1163 |
{ |
| 1164 |
|
| 1165 |
|
| 1166 |
int n = 0; |
| 1167 |
char *str = args->tmpks.s; |
| 1168 |
while ( *str && n<i ) |
| 1169 |
{ |
| 1170 |
if ( *str==',' ) n++; |
| 1171 |
str++; |
| 1172 |
} |
| 1173 |
if ( str[0]!='.' || (str[1]!=',' && str[1]!=0) ) continue; |
| 1174 |
} |
| 1175 |
int ret = copy_string_field(args->tmps,map[i],lsrc,&args->tmpks,i); |
| 1176 |
if ( ret!=0 ) error("[%s:%d %s] Failed to copy a string field\n", __FILE__,__LINE__,__func__); |
| 1177 |
} |
| 1178 |
return bcf_update_info_string(args->hdr_out,line,col->hdr_key_dst,args->tmpks.s); |
| 1179 |
} |
| 1180 |
void khash_str2int_clear_free(void *_hash) |
| 1181 |
{ |
| 1182 |
khash_t(str2int) *hash = (khash_t(str2int)*)_hash; |
| 1183 |
khint_t k; |
| 1184 |
if (hash == 0) return; |
| 1185 |
for (k = 0; k < kh_end(hash); ++k) |
| 1186 |
if (kh_exist(hash, k)) free((char*)kh_key(hash, k)); |
| 1187 |
kh_clear(str2int, hash); |
| 1188 |
} |
| 1189 |
static const char *escape_string(const char *str, char needle[], char **rmme, size_t *len) |
| 1190 |
{ |
| 1191 |
kstring_t tmp = {0,0,0}; |
| 1192 |
const char *bp = str, *ep = str; |
| 1193 |
while ( *ep ) |
| 1194 |
{ |
| 1195 |
int i = 0; |
| 1196 |
while ( needle[i] && needle[i]!=*ep ) i++; |
| 1197 |
if ( !needle[i] ) { ep++; continue; } |
| 1198 |
kputsn(bp,ep-bp,&tmp); |
| 1199 |
ksprintf(&tmp,"%%%X",*ep); |
| 1200 |
bp = ++ep; |
| 1201 |
} |
| 1202 |
if ( !tmp.l ) |
| 1203 |
{ |
| 1204 |
*len = strlen(str); |
| 1205 |
return str; |
| 1206 |
} |
| 1207 |
kputs(bp,&tmp); |
| 1208 |
*len = tmp.l; |
| 1209 |
*rmme = tmp.s; |
| 1210 |
return tmp.s; |
| 1211 |
} |
| 1212 |
static int setter_info_str(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 1213 |
{ |
| 1214 |
if ( (col->replace & REPLACE_MISSING) && col->number!=BCF_VL_A && col->number!=BCF_VL_R ) |
| 1215 |
{ |
| 1216 |
int ret = bcf_get_info_string(args->hdr, line, col->hdr_key_dst, &args->tmps2, &args->mtmps2); |
| 1217 |
if ( ret>0 && (args->tmps2[0]!='.' || args->tmps2[1]!=0) ) return 0; |
| 1218 |
} |
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
if ( col->replace & SET_OR_APPEND ) col->merge_method=MM_UNIQUE; |
| 1223 |
|
| 1224 |
annot_line_t *tab = (annot_line_t*) data; |
| 1225 |
const char *escaped = NULL; |
| 1226 |
char *rmme = NULL; |
| 1227 |
|
| 1228 |
size_t len = 0; |
| 1229 |
if ( tab ) |
| 1230 |
{ |
| 1231 |
char *str = tab->cols[col->icol]; |
| 1232 |
if ( !str || !*str ) return 0; |
| 1233 |
if ( !str[1] && str[0]=='.' && col->merge_method!=MM_APPEND_MISSING && !(col->replace & CARRY_OVER_MISSING) ) return 1; |
| 1234 |
char needle[] = {';','=',0}; |
| 1235 |
escaped = escape_string(tab->cols[col->icol],needle,&rmme,&len); |
| 1236 |
} |
| 1237 |
|
| 1238 |
if ( col->merge_method!=MM_FIRST ) |
| 1239 |
{ |
| 1240 |
if ( col->number==BCF_VL_A || col->number==BCF_VL_R ) |
| 1241 |
error("Error: the --merge-logic option cannot be used with INFO tags Type=String,Number={A,R,G}\n"); |
| 1242 |
|
| 1243 |
if ( data ) |
| 1244 |
{ |
| 1245 |
assert( col->merge_method==MM_APPEND || col->merge_method==MM_APPEND_MISSING || col->merge_method==MM_UNIQUE ); |
| 1246 |
if ( col->merge_method==MM_UNIQUE ) |
| 1247 |
{ |
| 1248 |
if ( !col->mm_str_hash ) col->mm_str_hash = (khash_t(str2int)*)khash_str2int_init(); |
| 1249 |
if ( khash_str2int_has_key(col->mm_str_hash, escaped) ) |
| 1250 |
{ |
| 1251 |
free(rmme); |
| 1252 |
return 1; |
| 1253 |
} |
| 1254 |
khash_str2int_inc(col->mm_str_hash, strdup(escaped)); |
| 1255 |
} |
| 1256 |
|
| 1257 |
if ( (col->replace & SET_OR_APPEND) && !col->mm_kstr.l ) |
| 1258 |
{ |
| 1259 |
int m = col->mm_kstr.m; |
| 1260 |
int n = bcf_get_info_string(args->hdr, line, col->hdr_key_dst, &col->mm_kstr.s, &m); |
| 1261 |
col->mm_kstr.m = m; |
| 1262 |
if ( n>0 && ((col->replace & CARRY_OVER_MISSING) || col->mm_kstr.s[0]!='.' || col->mm_kstr.s[1]) ) col->mm_kstr.l = n; |
| 1263 |
} |
| 1264 |
|
| 1265 |
if ( col->mm_kstr.l ) kputc(',',&col->mm_kstr); |
| 1266 |
kputs(escaped, &col->mm_kstr); |
| 1267 |
free(rmme); |
| 1268 |
return 1; |
| 1269 |
} |
| 1270 |
if ( col->mm_kstr.l ) |
| 1271 |
{ |
| 1272 |
hts_expand(char,col->mm_kstr.l+1,args->mtmps,args->tmps); |
| 1273 |
memcpy(args->tmps,col->mm_kstr.s,col->mm_kstr.l+1); |
| 1274 |
} |
| 1275 |
else |
| 1276 |
{ |
| 1277 |
free(rmme); |
| 1278 |
return 0; |
| 1279 |
} |
| 1280 |
|
| 1281 |
|
| 1282 |
if ( col->merge_method==MM_UNIQUE ) |
| 1283 |
khash_str2int_clear_free(col->mm_str_hash); |
| 1284 |
col->mm_kstr.l = 0; |
| 1285 |
} |
| 1286 |
else |
| 1287 |
{ |
| 1288 |
assert(tab); |
| 1289 |
hts_expand(char,len+1,args->mtmps,args->tmps); |
| 1290 |
memcpy(args->tmps,escaped,len+1); |
| 1291 |
if ( col->number==BCF_VL_A || col->number==BCF_VL_R ) |
| 1292 |
return setter_ARinfo_string(args,line,col,tab->nals,tab->als); |
| 1293 |
} |
| 1294 |
int ret = bcf_update_info_string(args->hdr_out,line,col->hdr_key_dst,args->tmps); |
| 1295 |
free(rmme); |
| 1296 |
return ret; |
| 1297 |
} |
| 1298 |
static int vcf_setter_info_str(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 1299 |
{ |
| 1300 |
bcf1_t *rec = (bcf1_t*) data; |
| 1301 |
|
| 1302 |
if ( col->getter ) |
| 1303 |
col->getter(args,rec,col,(void**)&args->tmps, &args->mtmps); |
| 1304 |
else |
| 1305 |
{ |
| 1306 |
int ntmps = bcf_get_info_string(args->files->readers[1].header,rec,col->hdr_key_src,&args->tmps,&args->mtmps); |
| 1307 |
if ( ntmps < 0 ) return 0; |
| 1308 |
} |
| 1309 |
|
| 1310 |
if ( col->number==BCF_VL_A || col->number==BCF_VL_R ) |
| 1311 |
return setter_ARinfo_string(args,line,col,rec->n_allele,rec->d.allele); |
| 1312 |
|
| 1313 |
if ( col->replace & REPLACE_MISSING ) |
| 1314 |
{ |
| 1315 |
int ret = bcf_get_info_string(args->hdr, line, col->hdr_key_dst, &args->tmps2, &args->mtmps2); |
| 1316 |
if ( ret>0 && (args->tmps2[0]!='.' || args->tmps2[1]!=0) ) return 0; |
| 1317 |
} |
| 1318 |
|
| 1319 |
return bcf_update_info_string(args->hdr_out,line,col->hdr_key_dst,args->tmps); |
| 1320 |
} |
| 1321 |
static int genotypes_to_string(args_t *args, int nsrc1, int32_t *src, int nsmpl_dst, kstring_t *str) |
| 1322 |
{ |
| 1323 |
int i, isrc, idst; |
| 1324 |
int blen = nsrc1 > 1 ? nsrc1 + 1 : 1; |
| 1325 |
|
| 1326 |
gt_length_too_big: |
| 1327 |
str->l = 0; |
| 1328 |
for (idst=0; idst<nsmpl_dst; idst++) |
| 1329 |
{ |
| 1330 |
isrc = args->sample_map ? args->sample_map[idst] : idst; |
| 1331 |
if ( isrc==-1 ) |
| 1332 |
{ |
| 1333 |
kputc_('.', str); |
| 1334 |
for (i=1; i < blen; i++) kputc_(0, str); |
| 1335 |
continue; |
| 1336 |
} |
| 1337 |
|
| 1338 |
size_t plen = str->l; |
| 1339 |
int32_t *ptr = src + isrc*nsrc1; |
| 1340 |
for (i=0; i<nsrc1 && ptr[i]!=bcf_int32_vector_end; i++) |
| 1341 |
{ |
| 1342 |
if ( i ) kputc("/|"[bcf_gt_is_phased(ptr[i])], str); |
| 1343 |
if ( bcf_gt_is_missing(ptr[i]) ) kputc('.', str); |
| 1344 |
else kputw(bcf_gt_allele(ptr[i]), str); |
| 1345 |
} |
| 1346 |
if ( i==0 ) kputc('.', str); |
| 1347 |
if ( str->l - plen > blen ) |
| 1348 |
{ |
| 1349 |
|
| 1350 |
|
| 1351 |
blen *= 2; |
| 1352 |
goto gt_length_too_big; |
| 1353 |
} |
| 1354 |
plen = str->l - plen; |
| 1355 |
while ( plen < blen ) |
| 1356 |
{ |
| 1357 |
kputc_(0, str); |
| 1358 |
plen++; |
| 1359 |
} |
| 1360 |
} |
| 1361 |
return 0; |
| 1362 |
} |
| 1363 |
static int vcf_setter_format_gt(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 1364 |
{ |
| 1365 |
bcf1_t *rec = (bcf1_t*) data; |
| 1366 |
int nsrc = bcf_get_genotypes(args->files->readers[1].header,rec,&args->tmpi,&args->mtmpi); |
| 1367 |
if ( nsrc==-3 ) return 0; |
| 1368 |
if ( nsrc<=0 ) return 1; |
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
if ( strcmp(col->hdr_key_src,col->hdr_key_dst) ) |
| 1373 |
{ |
| 1374 |
int nsmpl_dst = bcf_hdr_nsamples(args->hdr_out); |
| 1375 |
int nsmpl_src = bcf_hdr_nsamples(args->files->readers[1].header); |
| 1376 |
genotypes_to_string(args,nsrc/nsmpl_src,args->tmpi,nsmpl_dst,&args->tmpks); |
| 1377 |
return bcf_update_format_char(args->hdr_out,line,col->hdr_key_dst,args->tmpks.s,args->tmpks.l); |
| 1378 |
} |
| 1379 |
|
| 1380 |
if ( !args->sample_map ) |
| 1381 |
return bcf_update_genotypes(args->hdr_out,line,args->tmpi,nsrc); |
| 1382 |
|
| 1383 |
int i, j, ndst = bcf_get_genotypes(args->hdr,line,&args->tmpi2,&args->mtmpi2); |
| 1384 |
if ( ndst > 0 ) ndst /= bcf_hdr_nsamples(args->hdr_out); |
| 1385 |
nsrc /= bcf_hdr_nsamples(args->files->readers[1].header); |
| 1386 |
if ( ndst<=0 ) |
| 1387 |
{ |
| 1388 |
if ( col->replace & REPLACE_NON_MISSING ) return 0; |
| 1389 |
hts_expand(int32_t, nsrc*bcf_hdr_nsamples(args->hdr_out), args->mtmpi2, args->tmpi2); |
| 1390 |
for (i=0; i<bcf_hdr_nsamples(args->hdr_out); i++) |
| 1391 |
{ |
| 1392 |
int32_t *dst = args->tmpi2 + nsrc*i; |
| 1393 |
if ( args->sample_map[i]==-1 ) |
| 1394 |
{ |
| 1395 |
dst[0] = bcf_gt_missing; |
| 1396 |
for (j=1; j<nsrc; j++) dst[j] = bcf_int32_vector_end; |
| 1397 |
} |
| 1398 |
else |
| 1399 |
{ |
| 1400 |
int32_t *src = args->tmpi + nsrc*args->sample_map[i]; |
| 1401 |
for (j=0; j<nsrc; j++) dst[j] = src[j]; |
| 1402 |
} |
| 1403 |
} |
| 1404 |
return bcf_update_genotypes(args->hdr_out,line,args->tmpi2,nsrc*bcf_hdr_nsamples(args->hdr_out)); |
| 1405 |
} |
| 1406 |
else if ( ndst >= nsrc ) |
| 1407 |
{ |
| 1408 |
for (i=0; i<bcf_hdr_nsamples(args->hdr_out); i++) |
| 1409 |
{ |
| 1410 |
if ( args->sample_map[i]==-1 ) continue; |
| 1411 |
int32_t *src = args->tmpi + nsrc*args->sample_map[i]; |
| 1412 |
int32_t *dst = args->tmpi2 + ndst*i; |
| 1413 |
if ( (col->replace & REPLACE_NON_MISSING) && bcf_gt_is_missing(dst[0]) ) continue; |
| 1414 |
if ( (col->replace & REPLACE_MISSING) && !bcf_gt_is_missing(dst[0]) ) continue; |
| 1415 |
for (j=0; j<nsrc; j++) dst[j] = src[j]; |
| 1416 |
for (; j<ndst; j++) dst[j] = bcf_int32_vector_end; |
| 1417 |
} |
| 1418 |
return bcf_update_genotypes(args->hdr_out,line,args->tmpi2,ndst*bcf_hdr_nsamples(args->hdr_out)); |
| 1419 |
} |
| 1420 |
else |
| 1421 |
{ |
| 1422 |
hts_expand(int32_t, nsrc*bcf_hdr_nsamples(args->hdr_out), args->mtmpi3, args->tmpi3); |
| 1423 |
for (i=0; i<bcf_hdr_nsamples(args->hdr_out); i++) |
| 1424 |
{ |
| 1425 |
int32_t *ori = args->tmpi2 + ndst*i; |
| 1426 |
int32_t *dst = args->tmpi3 + nsrc*i; |
| 1427 |
int keep_ori = 0; |
| 1428 |
if ( args->sample_map[i]==-1 ) keep_ori = 1; |
| 1429 |
else if ( (col->replace & REPLACE_NON_MISSING) && bcf_gt_is_missing(ori[0]) ) keep_ori = 1; |
| 1430 |
else if ( (col->replace & REPLACE_MISSING) && !bcf_gt_is_missing(ori[0]) ) keep_ori = 1; |
| 1431 |
if ( keep_ori ) |
| 1432 |
{ |
| 1433 |
for (j=0; j<ndst; j++) dst[j] = ori[j]; |
| 1434 |
for (; j<nsrc; j++) dst[j] = bcf_int32_vector_end; |
| 1435 |
} |
| 1436 |
else |
| 1437 |
{ |
| 1438 |
int32_t *src = args->tmpi + nsrc*args->sample_map[i]; |
| 1439 |
for (j=0; j<nsrc; j++) dst[j] = src[j]; |
| 1440 |
} |
| 1441 |
} |
| 1442 |
return bcf_update_genotypes(args->hdr_out,line,args->tmpi3,nsrc*bcf_hdr_nsamples(args->hdr_out)); |
| 1443 |
} |
| 1444 |
} |
| 1445 |
static int count_vals(annot_line_t *tab, int icol_beg, int icol_end) |
| 1446 |
{ |
| 1447 |
int i, nmax = 1; |
| 1448 |
for (i=icol_beg; i<icol_end; i++) |
| 1449 |
{ |
| 1450 |
char *str = tab->cols[i], *end = str; |
| 1451 |
if ( str[0]=='.' && !str[1] ) |
| 1452 |
{ |
| 1453 |
|
| 1454 |
if ( !nmax ) nmax = 1; |
| 1455 |
continue; |
| 1456 |
} |
| 1457 |
int n = 1; |
| 1458 |
while ( *end ) |
| 1459 |
{ |
| 1460 |
if ( *end==',' ) n++; |
| 1461 |
end++; |
| 1462 |
} |
| 1463 |
if ( nmax<n ) nmax = n; |
| 1464 |
} |
| 1465 |
return nmax; |
| 1466 |
} |
| 1467 |
static int core_setter_format_int(args_t *args, bcf1_t *line, annot_col_t *col, int32_t *vals, int nvals) |
| 1468 |
{ |
| 1469 |
if ( !args->sample_map ) |
| 1470 |
return bcf_update_format_int32(args->hdr_out,line,col->hdr_key_dst,vals,nvals*args->nsmpl_annot); |
| 1471 |
|
| 1472 |
int i, j, ndst = bcf_get_format_int32(args->hdr,line,col->hdr_key_dst,&args->tmpi2,&args->mtmpi2); |
| 1473 |
if ( ndst > 0 ) ndst /= bcf_hdr_nsamples(args->hdr_out); |
| 1474 |
if ( ndst<=0 ) |
| 1475 |
{ |
| 1476 |
if ( col->replace & REPLACE_NON_MISSING ) return 0; |
| 1477 |
hts_expand(int32_t, nvals*bcf_hdr_nsamples(args->hdr_out), args->mtmpi2, args->tmpi2); |
| 1478 |
for (i=0; i<bcf_hdr_nsamples(args->hdr_out); i++) |
| 1479 |
{ |
| 1480 |
int32_t *dst = args->tmpi2 + nvals*i; |
| 1481 |
if ( args->sample_map[i]==-1 ) |
| 1482 |
{ |
| 1483 |
dst[0] = bcf_int32_missing; |
| 1484 |
for (j=1; j<nvals; j++) dst[j] = bcf_int32_vector_end; |
| 1485 |
} |
| 1486 |
else |
| 1487 |
{ |
| 1488 |
int32_t *src = vals + nvals*args->sample_map[i]; |
| 1489 |
for (j=0; j<nvals; j++) dst[j] = src[j]; |
| 1490 |
} |
| 1491 |
} |
| 1492 |
return bcf_update_format_int32(args->hdr_out,line,col->hdr_key_dst,args->tmpi2,nvals*bcf_hdr_nsamples(args->hdr_out)); |
| 1493 |
} |
| 1494 |
else if ( ndst >= nvals ) |
| 1495 |
{ |
| 1496 |
for (i=0; i<bcf_hdr_nsamples(args->hdr_out); i++) |
| 1497 |
{ |
| 1498 |
if ( args->sample_map[i]==-1 ) continue; |
| 1499 |
int32_t *src = vals + nvals*args->sample_map[i]; |
| 1500 |
int32_t *dst = args->tmpi2 + ndst*i; |
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
if ( col->replace & REPLACE_NON_MISSING ) { if ( dst[0]==bcf_int32_missing ) continue; } |
| 1510 |
else if ( col->replace & REPLACE_MISSING ) { if ( dst[0]!=bcf_int32_missing ) continue; } |
| 1511 |
else if ( col->replace & REPLACE_ALL ) { if ( src[0]==bcf_int32_missing ) continue; } |
| 1512 |
for (j=0; j<nvals; j++) dst[j] = src[j]; |
| 1513 |
for (; j<ndst; j++) dst[j] = bcf_int32_vector_end; |
| 1514 |
} |
| 1515 |
return bcf_update_format_int32(args->hdr_out,line,col->hdr_key_dst,args->tmpi2,ndst*bcf_hdr_nsamples(args->hdr_out)); |
| 1516 |
} |
| 1517 |
else |
| 1518 |
{ |
| 1519 |
hts_expand(int32_t, nvals*bcf_hdr_nsamples(args->hdr_out), args->mtmpi3, args->tmpi3); |
| 1520 |
for (i=0; i<bcf_hdr_nsamples(args->hdr_out); i++) |
| 1521 |
{ |
| 1522 |
int32_t *ann = vals + nvals*args->sample_map[i]; |
| 1523 |
int32_t *ori = args->tmpi2 + ndst*i; |
| 1524 |
int32_t *dst = args->tmpi3 + nvals*i; |
| 1525 |
int use_new_ann = 1; |
| 1526 |
if ( args->sample_map[i]==-1 ) use_new_ann = 0; |
| 1527 |
else if ( col->replace & REPLACE_NON_MISSING ) { if ( ori[0]==bcf_int32_missing ) use_new_ann = 0; } |
| 1528 |
else if ( col->replace & REPLACE_MISSING ) { if ( ori[0]!=bcf_int32_missing ) use_new_ann = 0; } |
| 1529 |
else if ( col->replace & REPLACE_ALL ) { if ( ann[0]==bcf_int32_missing ) use_new_ann = 0; } |
| 1530 |
if ( !use_new_ann ) |
| 1531 |
{ |
| 1532 |
for (j=0; j<ndst; j++) dst[j] = ori[j]; |
| 1533 |
for (; j<nvals; j++) dst[j] = bcf_int32_vector_end; |
| 1534 |
} |
| 1535 |
else |
| 1536 |
for (j=0; j<nvals; j++) dst[j] = ann[j]; |
| 1537 |
} |
| 1538 |
return bcf_update_format_int32(args->hdr_out,line,col->hdr_key_dst,args->tmpi3,nvals*bcf_hdr_nsamples(args->hdr_out)); |
| 1539 |
} |
| 1540 |
} |
| 1541 |
static int core_setter_format_real(args_t *args, bcf1_t *line, annot_col_t *col, float *vals, int nvals) |
| 1542 |
{ |
| 1543 |
if ( !args->sample_map ) |
| 1544 |
return bcf_update_format_float(args->hdr_out,line,col->hdr_key_dst,vals,nvals*args->nsmpl_annot); |
| 1545 |
|
| 1546 |
int i, j, ndst = bcf_get_format_float(args->hdr,line,col->hdr_key_dst,&args->tmpf2,&args->mtmpf2); |
| 1547 |
if ( ndst > 0 ) ndst /= bcf_hdr_nsamples(args->hdr_out); |
| 1548 |
if ( ndst<=0 ) |
| 1549 |
{ |
| 1550 |
if ( col->replace & REPLACE_NON_MISSING ) return 0; |
| 1551 |
hts_expand(float, nvals*bcf_hdr_nsamples(args->hdr_out), args->mtmpf2, args->tmpf2); |
| 1552 |
for (i=0; i<bcf_hdr_nsamples(args->hdr_out); i++) |
| 1553 |
{ |
| 1554 |
float *dst = args->tmpf2 + nvals*i; |
| 1555 |
if ( args->sample_map[i]==-1 ) |
| 1556 |
{ |
| 1557 |
bcf_float_set_missing(dst[0]); |
| 1558 |
for (j=1; j<nvals; j++) bcf_float_set_vector_end(dst[j]); |
| 1559 |
} |
| 1560 |
else |
| 1561 |
{ |
| 1562 |
float *src = vals + nvals*args->sample_map[i]; |
| 1563 |
for (j=0; j<nvals; j++) dst[j] = src[j]; |
| 1564 |
} |
| 1565 |
} |
| 1566 |
return bcf_update_format_float(args->hdr_out,line,col->hdr_key_dst,args->tmpf2,nvals*bcf_hdr_nsamples(args->hdr_out)); |
| 1567 |
} |
| 1568 |
else if ( ndst >= nvals ) |
| 1569 |
{ |
| 1570 |
for (i=0; i<bcf_hdr_nsamples(args->hdr_out); i++) |
| 1571 |
{ |
| 1572 |
if ( args->sample_map[i]==-1 ) continue; |
| 1573 |
float *src = vals + nvals*args->sample_map[i]; |
| 1574 |
float *dst = args->tmpf2 + ndst*i; |
| 1575 |
if ( col->replace & REPLACE_NON_MISSING ) { if ( bcf_float_is_missing(dst[0]) ) continue; } |
| 1576 |
else if ( col->replace & REPLACE_MISSING ) { if ( !bcf_float_is_missing(dst[0]) ) continue; } |
| 1577 |
else if ( col->replace & REPLACE_ALL ) { if ( bcf_float_is_missing(src[0]) ) continue; } |
| 1578 |
for (j=0; j<nvals; j++) dst[j] = src[j]; |
| 1579 |
for (; j<ndst; j++) bcf_float_set_vector_end(dst[j]); |
| 1580 |
} |
| 1581 |
return bcf_update_format_float(args->hdr_out,line,col->hdr_key_dst,args->tmpf2,ndst*bcf_hdr_nsamples(args->hdr_out)); |
| 1582 |
} |
| 1583 |
else |
| 1584 |
{ |
| 1585 |
hts_expand(float, nvals*bcf_hdr_nsamples(args->hdr_out), args->mtmpf3, args->tmpf3); |
| 1586 |
for (i=0; i<bcf_hdr_nsamples(args->hdr_out); i++) |
| 1587 |
{ |
| 1588 |
float *ann = vals + nvals*args->sample_map[i]; |
| 1589 |
float *ori = args->tmpf2 + ndst*i; |
| 1590 |
float *dst = args->tmpf3 + nvals*i; |
| 1591 |
int use_new_ann = 1; |
| 1592 |
if ( args->sample_map[i]==-1 ) use_new_ann = 0; |
| 1593 |
else if ( col->replace & REPLACE_NON_MISSING ) { if ( bcf_float_is_missing(ori[0]) ) use_new_ann = 0; } |
| 1594 |
else if ( col->replace & REPLACE_MISSING ) { if ( !bcf_float_is_missing(ori[0]) ) use_new_ann = 0; } |
| 1595 |
else if ( col->replace & REPLACE_ALL ) { if ( bcf_float_is_missing(ann[0]) ) use_new_ann = 0; } |
| 1596 |
if ( !use_new_ann ) |
| 1597 |
{ |
| 1598 |
for (j=0; j<ndst; j++) dst[j] = ori[j]; |
| 1599 |
for (; j<nvals; j++) bcf_float_set_vector_end(dst[j]); |
| 1600 |
} |
| 1601 |
else |
| 1602 |
for (j=0; j<nvals; j++) dst[j] = ann[j]; |
| 1603 |
} |
| 1604 |
return bcf_update_format_float(args->hdr_out,line,col->hdr_key_dst,args->tmpf3,nvals*bcf_hdr_nsamples(args->hdr_out)); |
| 1605 |
} |
| 1606 |
} |
| 1607 |
static int core_setter_format_str(args_t *args, bcf1_t *line, annot_col_t *col, char **vals) |
| 1608 |
{ |
| 1609 |
if ( !args->sample_map ) |
| 1610 |
return bcf_update_format_string(args->hdr_out,line,col->hdr_key_dst,(const char**)vals,args->nsmpl_annot); |
| 1611 |
|
| 1612 |
int i; |
| 1613 |
args->tmpp2[0] = args->tmps2; |
| 1614 |
int ret = bcf_get_format_string(args->hdr,line,col->hdr_key_dst,&args->tmpp2,&args->mtmps2); |
| 1615 |
args->tmps2 = args->tmpp2[0]; |
| 1616 |
|
| 1617 |
int nsmpl = bcf_hdr_nsamples(args->hdr_out); |
| 1618 |
if ( ret<=0 ) |
| 1619 |
{ |
| 1620 |
hts_expand(char,bcf_hdr_nsamples(args->hdr_out)*2,args->mtmps2,args->tmps2); |
| 1621 |
char *tmp = args->tmps2; |
| 1622 |
for (i=0; i<nsmpl; i++) |
| 1623 |
{ |
| 1624 |
tmp[0] = '.'; |
| 1625 |
tmp[1] = 0; |
| 1626 |
args->tmpp2[i] = tmp; |
| 1627 |
tmp += 2; |
| 1628 |
} |
| 1629 |
} |
| 1630 |
for (i=0; i<nsmpl; i++) |
| 1631 |
{ |
| 1632 |
if ( args->sample_map[i]==-1 ) continue; |
| 1633 |
char **src = vals + args->sample_map[i]; |
| 1634 |
char **dst = args->tmpp2 + i; |
| 1635 |
|
| 1636 |
if ( col->replace & REPLACE_NON_MISSING ) { if ( (*dst)[0]=='.' && (*dst)[1]==0 ) continue; } |
| 1637 |
else if ( col->replace & REPLACE_MISSING ) { if ( (*dst)[0]!='.' || (*dst)[1]!=0 ) continue; } |
| 1638 |
else if ( col->replace & REPLACE_ALL ) { if ( (*src)[0]=='.' && (*src)[1]==0 ) continue; } |
| 1639 |
*dst = *src; |
| 1640 |
} |
| 1641 |
return bcf_update_format_string(args->hdr_out,line,col->hdr_key_dst,(const char**)args->tmpp2,nsmpl); |
| 1642 |
} |
| 1643 |
static int setter_format_int(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 1644 |
{ |
| 1645 |
if ( !data ) error("Error: the --merge-logic option cannot be used with FORMAT tags (yet?)\n"); |
| 1646 |
|
| 1647 |
annot_line_t *tab = (annot_line_t*) data; |
| 1648 |
if ( col->icol+args->nsmpl_annot > tab->ncols ) |
| 1649 |
error("Incorrect number of values for %s at %s:%"PRId64"\n",col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 1650 |
int nvals = count_vals(tab,col->icol,col->icol+args->nsmpl_annot); |
| 1651 |
hts_expand(int32_t,nvals*args->nsmpl_annot,args->mtmpi,args->tmpi); |
| 1652 |
|
| 1653 |
int icol = col->icol, ismpl; |
| 1654 |
for (ismpl=0; ismpl<args->nsmpl_annot; ismpl++) |
| 1655 |
{ |
| 1656 |
int32_t *ptr = args->tmpi + ismpl*nvals; |
| 1657 |
int ival = 0; |
| 1658 |
|
| 1659 |
char *str = tab->cols[icol]; |
| 1660 |
while ( *str ) |
| 1661 |
{ |
| 1662 |
if ( str[0]=='.' && (!str[1] || str[1]==',') ) |
| 1663 |
{ |
| 1664 |
ptr[ival++] = bcf_int32_missing; |
| 1665 |
str += str[1] ? 2 : 1; |
| 1666 |
continue; |
| 1667 |
} |
| 1668 |
|
| 1669 |
char *end = str; |
| 1670 |
ptr[ival] = strtol(str, &end, 10); |
| 1671 |
if ( end==str ) |
| 1672 |
error("Could not parse %s at %s:%"PRId64" .. [%s]\n", col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1,tab->cols[col->icol]); |
| 1673 |
|
| 1674 |
ival++; |
| 1675 |
str = *end ? end+1 : end; |
| 1676 |
} |
| 1677 |
while ( ival<nvals ) ptr[ival++] = bcf_int32_vector_end; |
| 1678 |
icol++; |
| 1679 |
} |
| 1680 |
return core_setter_format_int(args,line,col,args->tmpi,nvals); |
| 1681 |
} |
| 1682 |
static int setter_format_real(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 1683 |
{ |
| 1684 |
if ( !data ) error("Error: the --merge-logic option cannot be used with FORMAT tags (yet?)\n"); |
| 1685 |
|
| 1686 |
annot_line_t *tab = (annot_line_t*) data; |
| 1687 |
if ( col->icol+args->nsmpl_annot > tab->ncols ) |
| 1688 |
error("Incorrect number of values for %s at %s:%"PRId64"\n",col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 1689 |
int nvals = count_vals(tab,col->icol,col->icol+args->nsmpl_annot); |
| 1690 |
hts_expand(float,nvals*args->nsmpl_annot,args->mtmpf,args->tmpf); |
| 1691 |
|
| 1692 |
int icol = col->icol, ismpl; |
| 1693 |
for (ismpl=0; ismpl<args->nsmpl_annot; ismpl++) |
| 1694 |
{ |
| 1695 |
float *ptr = args->tmpf + ismpl*nvals; |
| 1696 |
int ival = 0; |
| 1697 |
|
| 1698 |
char *str = tab->cols[icol]; |
| 1699 |
while ( *str ) |
| 1700 |
{ |
| 1701 |
if ( str[0]=='.' && (!str[1] || str[1]==',') ) |
| 1702 |
{ |
| 1703 |
bcf_float_set_missing(ptr[ival]); |
| 1704 |
ival++; |
| 1705 |
str += str[1] ? 2 : 1; |
| 1706 |
continue; |
| 1707 |
} |
| 1708 |
|
| 1709 |
char *end = str; |
| 1710 |
ptr[ival] = strtod(str, &end); |
| 1711 |
if ( end==str ) |
| 1712 |
error("Could not parse %s at %s:%"PRId64" .. [%s]\n", col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1,tab->cols[col->icol]); |
| 1713 |
|
| 1714 |
ival++; |
| 1715 |
str = *end ? end+1 : end; |
| 1716 |
} |
| 1717 |
while ( ival<nvals ) { bcf_float_set_vector_end(ptr[ival]); ival++; } |
| 1718 |
icol++; |
| 1719 |
} |
| 1720 |
return core_setter_format_real(args,line,col,args->tmpf,nvals); |
| 1721 |
} |
| 1722 |
static int setter_format_str(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 1723 |
{ |
| 1724 |
if ( !data ) error("Error: the --merge-logic option cannot be used with FORMAT tags (yet?)\n"); |
| 1725 |
|
| 1726 |
annot_line_t *tab = (annot_line_t*) data; |
| 1727 |
if ( col->icol+args->nsmpl_annot > tab->ncols ) |
| 1728 |
error("Incorrect number of values for %s at %s:%"PRId64"\n",col->hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 1729 |
|
| 1730 |
char needle[] = {':',0}; |
| 1731 |
int ismpl; |
| 1732 |
for (ismpl=0; ismpl<args->nsmpl_annot; ismpl++) |
| 1733 |
{ |
| 1734 |
size_t len; |
| 1735 |
char *rmme = NULL; |
| 1736 |
const char *str = escape_string(tab->cols[col->icol + ismpl],needle,&rmme,&len); |
| 1737 |
args->tmpp[ismpl] = rmme ? rmme : strdup(str); |
| 1738 |
} |
| 1739 |
int ret = core_setter_format_str(args,line,col,args->tmpp); |
| 1740 |
for (ismpl=0; ismpl<args->nsmpl_annot; ismpl++) free(args->tmpp[ismpl]); |
| 1741 |
return ret; |
| 1742 |
} |
| 1743 |
static int determine_ploidy(int nals, int *vals, int nvals1, uint8_t *smpl, int nsmpl) |
| 1744 |
{ |
| 1745 |
int i, j, ndip = nals*(nals+1)/2, max_ploidy = 0; |
| 1746 |
for (i=0; i<nsmpl; i++) |
| 1747 |
{ |
| 1748 |
int *ptr = vals + i*nvals1; |
| 1749 |
int has_value = 0; |
| 1750 |
for (j=0; j<nvals1; j++) |
| 1751 |
{ |
| 1752 |
if ( ptr[j]==bcf_int32_vector_end ) break; |
| 1753 |
if ( ptr[j]!=bcf_int32_missing ) has_value = 1; |
| 1754 |
} |
| 1755 |
if ( has_value ) |
| 1756 |
{ |
| 1757 |
if ( j==ndip ) |
| 1758 |
{ |
| 1759 |
smpl[i] = 2; |
| 1760 |
max_ploidy = 2; |
| 1761 |
} |
| 1762 |
else if ( j==nals ) |
| 1763 |
{ |
| 1764 |
smpl[i] = 1; |
| 1765 |
if ( !max_ploidy ) max_ploidy = 1; |
| 1766 |
} |
| 1767 |
else return -j; |
| 1768 |
} |
| 1769 |
else smpl[i] = 0; |
| 1770 |
} |
| 1771 |
return max_ploidy; |
| 1772 |
} |
| 1773 |
static int vcf_setter_format_int(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 1774 |
{ |
| 1775 |
bcf1_t *rec = (bcf1_t*) data; |
| 1776 |
int nsrc = bcf_get_format_int32(args->files->readers[1].header,rec,col->hdr_key_src,&args->tmpi,&args->mtmpi); |
| 1777 |
if ( nsrc==-3 ) return 0; |
| 1778 |
if ( nsrc<=0 ) return 1; |
| 1779 |
int nsmpl_src = bcf_hdr_nsamples(args->files->readers[1].header); |
| 1780 |
int nsrc1 = nsrc / nsmpl_src; |
| 1781 |
if ( col->number!=BCF_VL_G && col->number!=BCF_VL_R && col->number!=BCF_VL_A ) |
| 1782 |
return core_setter_format_int(args,line,col,args->tmpi,nsrc1); |
| 1783 |
|
| 1784 |
|
| 1785 |
int nmap_hap = col->number==BCF_VL_G || col->number==BCF_VL_R ? rec->n_allele : rec->n_allele - 1; |
| 1786 |
int *map_hap = vcmp_map_ARvalues(args->vcmp,nmap_hap,line->n_allele,line->d.allele,rec->n_allele,rec->d.allele); |
| 1787 |
if ( !map_hap ) error("REF alleles not compatible at %s:%"PRId64"\n", bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 1788 |
|
| 1789 |
int i, j; |
| 1790 |
if ( rec->n_allele==line->n_allele ) |
| 1791 |
{ |
| 1792 |
|
| 1793 |
for (i=0; i<rec->n_allele; i++) if ( map_hap[i]!=i ) break; |
| 1794 |
if ( i==rec->n_allele ) |
| 1795 |
return core_setter_format_int(args,line,col,args->tmpi,nsrc1); |
| 1796 |
} |
| 1797 |
|
| 1798 |
int nsmpl_dst = rec->n_sample; |
| 1799 |
int ndst = bcf_get_format_int32(args->hdr,line,col->hdr_key_dst,&args->tmpi2,&args->mtmpi2); |
| 1800 |
int ndst1 = ndst / nsmpl_dst; |
| 1801 |
if ( ndst <= 0 ) |
| 1802 |
{ |
| 1803 |
if ( col->replace & REPLACE_NON_MISSING ) return 0; |
| 1804 |
if ( col->number==BCF_VL_G ) |
| 1805 |
ndst1 = line->n_allele*(line->n_allele+1)/2; |
| 1806 |
else |
| 1807 |
ndst1 = col->number==BCF_VL_A ? line->n_allele - 1 : line->n_allele; |
| 1808 |
hts_expand(int, ndst1*nsmpl_dst, args->mtmpi2, args->tmpi2); |
| 1809 |
for (i=0; i<nsmpl_dst; i++) |
| 1810 |
{ |
| 1811 |
int32_t *dst = args->tmpi2 + i*ndst1; |
| 1812 |
for (j=0; j<ndst1; j++) dst[j] = bcf_int32_missing; |
| 1813 |
} |
| 1814 |
} |
| 1815 |
|
| 1816 |
int nmap_dip = 0, *map_dip = NULL; |
| 1817 |
if ( col->number==BCF_VL_G ) |
| 1818 |
{ |
| 1819 |
map_dip = vcmp_map_dipGvalues(args->vcmp, &nmap_dip); |
| 1820 |
if ( !args->src_smpl_pld ) |
| 1821 |
{ |
| 1822 |
args->src_smpl_pld = (uint8_t*) malloc(nsmpl_src); |
| 1823 |
args->dst_smpl_pld = (uint8_t*) malloc(nsmpl_dst); |
| 1824 |
} |
| 1825 |
int pld_src = determine_ploidy(rec->n_allele, args->tmpi, nsrc1, args->src_smpl_pld, nsmpl_src); |
| 1826 |
if ( pld_src<0 ) |
| 1827 |
error("Unexpected number of %s values (%d) for %d alleles at %s:%"PRId64"\n", col->hdr_key_src,-pld_src, rec->n_allele, bcf_seqname(bcf_sr_get_header(args->files,1),rec),(int64_t) rec->pos+1); |
| 1828 |
int pld_dst = determine_ploidy(line->n_allele, args->tmpi2, ndst1, args->dst_smpl_pld, nsmpl_dst); |
| 1829 |
if ( pld_dst<0 ) |
| 1830 |
error("Unexpected number of %s values (%d) for %d alleles at %s:%"PRId64"\n", col->hdr_key_src,-pld_dst, line->n_allele, bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 1831 |
|
| 1832 |
int ndst1_new = pld_dst==1 ? line->n_allele : line->n_allele*(line->n_allele+1)/2; |
| 1833 |
if ( ndst1_new != ndst1 ) |
| 1834 |
{ |
| 1835 |
if ( ndst1 ) error("todo: %s ndst1!=ndst .. %d %d at %s:%"PRId64"\n",col->hdr_key_src,ndst1_new,ndst1,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 1836 |
ndst1 = ndst1_new; |
| 1837 |
hts_expand(int32_t, ndst1*nsmpl_dst, args->mtmpi2, args->tmpi2); |
| 1838 |
} |
| 1839 |
} |
| 1840 |
else if ( !ndst1 ) |
| 1841 |
{ |
| 1842 |
ndst1 = col->number==BCF_VL_A ? line->n_allele - 1 : line->n_allele; |
| 1843 |
hts_expand(int32_t, ndst1*nsmpl_dst, args->mtmpi2, args->tmpi2); |
| 1844 |
} |
| 1845 |
|
| 1846 |
for (i=0; i<nsmpl_dst; i++) |
| 1847 |
{ |
| 1848 |
int ii = args->sample_map ? args->sample_map[i] : i; |
| 1849 |
int32_t *ptr_src = args->tmpi + i*nsrc1; |
| 1850 |
int32_t *ptr_dst = args->tmpi2 + ii*ndst1; |
| 1851 |
|
| 1852 |
if ( col->number==BCF_VL_G ) |
| 1853 |
{ |
| 1854 |
if ( args->src_smpl_pld[ii] > 0 && args->dst_smpl_pld[i] > 0 && args->src_smpl_pld[ii]!=args->dst_smpl_pld[i] ) |
| 1855 |
error("Sample ploidy differs at %s:%"PRId64"\n", bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 1856 |
if ( !args->dst_smpl_pld[i] ) |
| 1857 |
for (j=0; j<ndst1; j++) ptr_dst[j] = bcf_int32_missing; |
| 1858 |
} |
| 1859 |
if ( col->number!=BCF_VL_G || args->src_smpl_pld[i]==1 ) |
| 1860 |
{ |
| 1861 |
for (j=0; j<nmap_hap; j++) |
| 1862 |
{ |
| 1863 |
int k = map_hap[j]; |
| 1864 |
if ( k>=0 ) ptr_dst[k] = ptr_src[j]; |
| 1865 |
} |
| 1866 |
if ( col->number==BCF_VL_G ) |
| 1867 |
for (j=line->n_allele; j<ndst1; j++) ptr_dst[j++] = bcf_int32_vector_end; |
| 1868 |
} |
| 1869 |
else |
| 1870 |
{ |
| 1871 |
for (j=0; j<nmap_dip; j++) |
| 1872 |
{ |
| 1873 |
int k = map_dip[j]; |
| 1874 |
if ( k>=0 ) ptr_dst[k] = ptr_src[j]; |
| 1875 |
} |
| 1876 |
} |
| 1877 |
} |
| 1878 |
return bcf_update_format_int32(args->hdr_out,line,col->hdr_key_dst,args->tmpi2,nsmpl_dst*ndst1); |
| 1879 |
} |
| 1880 |
static int vcf_setter_format_real(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 1881 |
{ |
| 1882 |
bcf1_t *rec = (bcf1_t*) data; |
| 1883 |
int nsrc = bcf_get_format_float(args->files->readers[1].header,rec,col->hdr_key_src,&args->tmpf,&args->mtmpf); |
| 1884 |
if ( nsrc==-3 ) return 0; |
| 1885 |
if ( nsrc<=0 ) return 1; |
| 1886 |
int nsmpl_src = bcf_hdr_nsamples(args->files->readers[1].header); |
| 1887 |
int nsrc1 = nsrc / nsmpl_src; |
| 1888 |
if ( col->number!=BCF_VL_G && col->number!=BCF_VL_R && col->number!=BCF_VL_A ) |
| 1889 |
return core_setter_format_real(args,line,col,args->tmpf,nsrc1); |
| 1890 |
|
| 1891 |
|
| 1892 |
int nmap_hap = col->number==BCF_VL_G || col->number==BCF_VL_R ? rec->n_allele : rec->n_allele - 1; |
| 1893 |
int *map_hap = vcmp_map_ARvalues(args->vcmp,nmap_hap,line->n_allele,line->d.allele,rec->n_allele,rec->d.allele); |
| 1894 |
if ( !map_hap ) error("REF alleles not compatible at %s:%"PRId64"\n", bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 1895 |
|
| 1896 |
int i, j; |
| 1897 |
if ( rec->n_allele==line->n_allele ) |
| 1898 |
{ |
| 1899 |
|
| 1900 |
for (i=0; i<rec->n_allele; i++) if ( map_hap[i]!=i ) break; |
| 1901 |
if ( i==rec->n_allele ) |
| 1902 |
return core_setter_format_real(args,line,col,args->tmpf,nsrc1); |
| 1903 |
} |
| 1904 |
|
| 1905 |
int nsmpl_dst = rec->n_sample; |
| 1906 |
int ndst = bcf_get_format_float(args->hdr,line,col->hdr_key_dst,&args->tmpf2,&args->mtmpf2); |
| 1907 |
int ndst1 = ndst / nsmpl_dst; |
| 1908 |
if ( ndst <= 0 ) |
| 1909 |
{ |
| 1910 |
if ( col->replace & REPLACE_NON_MISSING ) return 0; |
| 1911 |
if ( col->number==BCF_VL_G ) |
| 1912 |
ndst1 = line->n_allele*(line->n_allele+1)/2; |
| 1913 |
else |
| 1914 |
ndst1 = col->number==BCF_VL_A ? line->n_allele - 1 : line->n_allele; |
| 1915 |
hts_expand(float, ndst1*nsmpl_dst, args->mtmpf2, args->tmpf2); |
| 1916 |
for (i=0; i<nsmpl_dst; i++) |
| 1917 |
{ |
| 1918 |
float *dst = args->tmpf2 + i*ndst1; |
| 1919 |
for (j=0; j<ndst1; j++) bcf_float_set_missing(dst[j]); |
| 1920 |
} |
| 1921 |
} |
| 1922 |
|
| 1923 |
int nmap_dip = 0, *map_dip = NULL; |
| 1924 |
if ( col->number==BCF_VL_G ) |
| 1925 |
{ |
| 1926 |
map_dip = vcmp_map_dipGvalues(args->vcmp, &nmap_dip); |
| 1927 |
if ( !args->src_smpl_pld ) |
| 1928 |
{ |
| 1929 |
args->src_smpl_pld = (uint8_t*) malloc(nsmpl_src); |
| 1930 |
args->dst_smpl_pld = (uint8_t*) malloc(nsmpl_dst); |
| 1931 |
} |
| 1932 |
int pld_src = determine_ploidy(rec->n_allele, args->tmpi, nsrc1, args->src_smpl_pld, nsmpl_src); |
| 1933 |
if ( pld_src<0 ) |
| 1934 |
error("Unexpected number of %s values (%d) for %d alleles at %s:%"PRId64"\n", col->hdr_key_src,-pld_src, rec->n_allele, bcf_seqname(bcf_sr_get_header(args->files,1),rec),(int64_t) rec->pos+1); |
| 1935 |
int pld_dst = determine_ploidy(line->n_allele, args->tmpi2, ndst1, args->dst_smpl_pld, nsmpl_dst); |
| 1936 |
if ( pld_dst<0 ) |
| 1937 |
error("Unexpected number of %s values (%d) for %d alleles at %s:%"PRId64"\n", col->hdr_key_src,-pld_dst, line->n_allele, bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 1938 |
|
| 1939 |
int ndst1_new = pld_dst==1 ? line->n_allele : line->n_allele*(line->n_allele+1)/2; |
| 1940 |
if ( ndst1_new != ndst1 ) |
| 1941 |
{ |
| 1942 |
if ( ndst1 ) error("todo: %s ndst1!=ndst .. %d %d at %s:%"PRId64"\n",col->hdr_key_src,ndst1_new,ndst1,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 1943 |
ndst1 = ndst1_new; |
| 1944 |
hts_expand(float, ndst1*nsmpl_dst, args->mtmpf2, args->tmpf2); |
| 1945 |
} |
| 1946 |
} |
| 1947 |
else if ( !ndst1 ) |
| 1948 |
{ |
| 1949 |
ndst1 = col->number==BCF_VL_A ? line->n_allele - 1 : line->n_allele; |
| 1950 |
hts_expand(float, ndst1*nsmpl_dst, args->mtmpf2, args->tmpf2); |
| 1951 |
} |
| 1952 |
|
| 1953 |
for (i=0; i<nsmpl_dst; i++) |
| 1954 |
{ |
| 1955 |
int ii = args->sample_map ? args->sample_map[i] : i; |
| 1956 |
float *ptr_src = args->tmpf + i*nsrc1; |
| 1957 |
float *ptr_dst = args->tmpf2 + ii*ndst1; |
| 1958 |
|
| 1959 |
if ( col->number==BCF_VL_G ) |
| 1960 |
{ |
| 1961 |
if ( args->src_smpl_pld[ii] > 0 && args->dst_smpl_pld[i] > 0 && args->src_smpl_pld[ii]!=args->dst_smpl_pld[i] ) |
| 1962 |
error("Sample ploidy differs at %s:%"PRId64"\n", bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 1963 |
if ( !args->dst_smpl_pld[i] ) |
| 1964 |
for (j=0; j<ndst1; j++) bcf_float_set_missing(ptr_dst[j]); |
| 1965 |
} |
| 1966 |
if ( col->number!=BCF_VL_G || args->src_smpl_pld[i]==1 ) |
| 1967 |
{ |
| 1968 |
for (j=0; j<nmap_hap; j++) |
| 1969 |
{ |
| 1970 |
int k = map_hap[j]; |
| 1971 |
if ( k>=0 ) |
| 1972 |
{ |
| 1973 |
if ( bcf_float_is_missing(ptr_src[j]) ) bcf_float_set_missing(ptr_dst[k]); |
| 1974 |
else if ( bcf_float_is_vector_end(ptr_src[j]) ) bcf_float_set_vector_end(ptr_dst[k]); |
| 1975 |
else ptr_dst[k] = ptr_src[j]; |
| 1976 |
} |
| 1977 |
} |
| 1978 |
if ( col->number==BCF_VL_G ) |
| 1979 |
for (j=line->n_allele; j<ndst1; j++) bcf_float_set_vector_end(ptr_dst[j]); |
| 1980 |
} |
| 1981 |
else |
| 1982 |
{ |
| 1983 |
for (j=0; j<nmap_dip; j++) |
| 1984 |
{ |
| 1985 |
int k = map_dip[j]; |
| 1986 |
if ( k>=0 ) |
| 1987 |
{ |
| 1988 |
if ( bcf_float_is_missing(ptr_src[j]) ) bcf_float_set_missing(ptr_dst[k]); |
| 1989 |
else if ( bcf_float_is_vector_end(ptr_src[j]) ) bcf_float_set_vector_end(ptr_dst[k]); |
| 1990 |
else ptr_dst[k] = ptr_src[j]; |
| 1991 |
} |
| 1992 |
} |
| 1993 |
} |
| 1994 |
} |
| 1995 |
return bcf_update_format_float(args->hdr_out,line,col->hdr_key_dst,args->tmpf2,nsmpl_dst*ndst1); |
| 1996 |
} |
| 1997 |
|
| 1998 |
static int vcf_setter_format_str(args_t *args, bcf1_t *line, annot_col_t *col, void *data) |
| 1999 |
{ |
| 2000 |
bcf1_t *rec = (bcf1_t*) data; |
| 2001 |
args->tmpp[0] = args->tmps; |
| 2002 |
int ret = bcf_get_format_string(args->files->readers[1].header,rec,col->hdr_key_src,&args->tmpp,&args->mtmps); |
| 2003 |
args->tmps = args->tmpp[0]; |
| 2004 |
if ( ret==-3 ) return 0; |
| 2005 |
if ( ret<=0 ) return 1; |
| 2006 |
if ( strcmp("GT",col->hdr_key_dst) ) |
| 2007 |
return core_setter_format_str(args,line,col,args->tmpp); |
| 2008 |
|
| 2009 |
|
| 2010 |
|
| 2011 |
int nsmpl_src = bcf_hdr_nsamples(args->files->readers[1].header); |
| 2012 |
int nsmpl_dst = bcf_hdr_nsamples(args->hdr_out); |
| 2013 |
int isrc,idst, ndst1 = 0, nsrc1 = ret / nsmpl_src; |
| 2014 |
char *ptr = args->tmps, *ptr_end = ptr + ret; |
| 2015 |
while ( ptr < ptr_end ) |
| 2016 |
{ |
| 2017 |
char *smpl_end = ptr + nsrc1; |
| 2018 |
int n = 1; |
| 2019 |
while ( ptr < smpl_end ) |
| 2020 |
{ |
| 2021 |
if ( *ptr=='/' || *ptr=='|' ) n++; |
| 2022 |
ptr++; |
| 2023 |
} |
| 2024 |
if ( ndst1 < n ) ndst1 = n; |
| 2025 |
} |
| 2026 |
assert( ndst1 ); |
| 2027 |
|
| 2028 |
int ndst = ndst1*nsmpl_dst; |
| 2029 |
hts_expand(int32_t,ndst,args->mtmpi,args->tmpi); |
| 2030 |
hts_expand(char,ret+1,args->mtmps,args->tmps); args->tmps[ret] = 0; |
| 2031 |
for (idst=0; idst<nsmpl_dst; idst++) |
| 2032 |
{ |
| 2033 |
int i = 0, is_phased = 0; |
| 2034 |
int32_t *dst = args->tmpi + idst*ndst1; |
| 2035 |
isrc = args->sample_map ? args->sample_map[idst] : idst; |
| 2036 |
if ( isrc==-1 ) |
| 2037 |
{ |
| 2038 |
dst[0] = bcf_gt_missing; |
| 2039 |
for (i=1; i<ndst1; i++) dst[i] = bcf_int32_vector_end; |
| 2040 |
continue; |
| 2041 |
} |
| 2042 |
char *beg = args->tmps + isrc*nsrc1, *tmp; |
| 2043 |
char *keep_ptr = beg+nsrc1, keep = *keep_ptr; *keep_ptr = 0; |
| 2044 |
while ( *beg ) |
| 2045 |
{ |
| 2046 |
char *end = beg; |
| 2047 |
while ( *end && *end!='/' && *end!='|' ) end++; |
| 2048 |
if ( *beg=='.' && end-beg==1 ) dst[i] = bcf_gt_missing; |
| 2049 |
else |
| 2050 |
{ |
| 2051 |
if ( *end=='|' ) is_phased = 1; |
| 2052 |
dst[i] = strtol(beg, &tmp, 10); |
| 2053 |
if ( tmp!=end ) |
| 2054 |
error("Could not parse the %s field at %s:%"PRId64" in %s\n", col->hdr_key_src,bcf_seqname(args->files->readers[1].header,rec),(int64_t) rec->pos+1,args->targets_fname); |
| 2055 |
if ( dst[i] >= line->n_allele ) |
| 2056 |
error("The source allele index is bigger than the number of destination alleles at %s:%"PRId64"\n", bcf_seqname(args->files->readers[1].header,rec),(int64_t) rec->pos+1); |
| 2057 |
dst[i] = is_phased ? bcf_gt_phased(dst[i]) : bcf_gt_unphased(dst[i]); |
| 2058 |
} |
| 2059 |
beg = *end ? end+1 : end; |
| 2060 |
i++; |
| 2061 |
} |
| 2062 |
*keep_ptr = keep; |
| 2063 |
for (; i<ndst1; i++) dst[i] = bcf_int32_vector_end; |
| 2064 |
} |
| 2065 |
return bcf_update_genotypes(args->hdr_out,line,args->tmpi,ndst); |
| 2066 |
} |
| 2067 |
static int init_sample_map(args_t *args, bcf_hdr_t *src, bcf_hdr_t *dst) |
| 2068 |
{ |
| 2069 |
int i; |
| 2070 |
if ( !args->sample_names ) |
| 2071 |
{ |
| 2072 |
args->nsmpl_annot = bcf_hdr_nsamples(dst); |
| 2073 |
|
| 2074 |
|
| 2075 |
if ( !src ) return 0; |
| 2076 |
|
| 2077 |
int nmatch = 0; |
| 2078 |
for (i=0; i<bcf_hdr_nsamples(src); i++) |
| 2079 |
{ |
| 2080 |
int id = bcf_hdr_id2int(dst, BCF_DT_SAMPLE, src->samples[i]); |
| 2081 |
if ( id!=-1 ) nmatch++; |
| 2082 |
} |
| 2083 |
if ( !nmatch ) return -1; |
| 2084 |
|
| 2085 |
args->nsample_map = bcf_hdr_nsamples(dst); |
| 2086 |
args->sample_map = (int*) malloc(sizeof(int)*args->nsample_map); |
| 2087 |
for (i=0; i<args->nsample_map; i++) |
| 2088 |
{ |
| 2089 |
int id = bcf_hdr_id2int(src, BCF_DT_SAMPLE, dst->samples[i]); |
| 2090 |
args->sample_map[i] = id; |
| 2091 |
} |
| 2092 |
return 1; |
| 2093 |
} |
| 2094 |
|
| 2095 |
args->nsample_map = bcf_hdr_nsamples(dst); |
| 2096 |
args->sample_map = (int*) malloc(sizeof(int)*args->nsample_map); |
| 2097 |
for (i=0; i<args->nsample_map; i++) args->sample_map[i] = -1; |
| 2098 |
|
| 2099 |
int flags = !src ? SMPL_STRICT|SMPL_SINGLE|SMPL_REORDER : SMPL_STRICT|SMPL_SINGLE|SMPL_PAIR2; |
| 2100 |
smpl_ilist_t *ilist = smpl_ilist_init(dst, args->sample_names, args->sample_is_file, flags); |
| 2101 |
if ( !ilist || !ilist->n ) error("Could not parse the samples: %s\n", args->sample_names); |
| 2102 |
args->nsmpl_annot = ilist->n; |
| 2103 |
int need_sample_map = args->nsmpl_annot==bcf_hdr_nsamples(dst) ? 0 : 1; |
| 2104 |
for (i=0; i<args->nsmpl_annot; i++) |
| 2105 |
{ |
| 2106 |
int idst = ilist->idx[i]; |
| 2107 |
const char *src_name = ilist->pair && ilist->pair[i] ? ilist->pair[i] : bcf_hdr_int2id(dst, BCF_DT_SAMPLE, idst); |
| 2108 |
int isrc = i; |
| 2109 |
if ( src ) |
| 2110 |
{ |
| 2111 |
isrc = bcf_hdr_id2int(src, BCF_DT_SAMPLE, src_name); |
| 2112 |
if ( isrc==-1 ) error("Sample \"%s\" not found in the annotation file\n", src_name); |
| 2113 |
} |
| 2114 |
if ( isrc!=idst ) need_sample_map = 1; |
| 2115 |
args->sample_map[idst] = isrc; |
| 2116 |
} |
| 2117 |
smpl_ilist_destroy(ilist); |
| 2118 |
return need_sample_map; |
| 2119 |
} |
| 2120 |
static char *columns_complement(char *columns, void **skip_info, void **skip_fmt) |
| 2121 |
{ |
| 2122 |
kstring_t str = {0,0,0}; |
| 2123 |
char *ss = columns, *se = ss; |
| 2124 |
while ( *ss ) |
| 2125 |
{ |
| 2126 |
if ( *se && *se!=',' ) { se++; continue; } |
| 2127 |
if ( *ss!='^' ) |
| 2128 |
{ |
| 2129 |
if ( str.l ) kputc(',',&str); |
| 2130 |
kputsn(ss, se-ss, &str); |
| 2131 |
if ( !*se ) break; |
| 2132 |
ss = ++se; |
| 2133 |
continue; |
| 2134 |
} |
| 2135 |
|
| 2136 |
if ( !strncasecmp("^INFO/",ss,6) ) |
| 2137 |
{ |
| 2138 |
if ( !*skip_info ) |
| 2139 |
{ |
| 2140 |
*skip_info = khash_str2int_init(); |
| 2141 |
if ( str.l ) kputc(',',&str); |
| 2142 |
kputs("INFO",&str); |
| 2143 |
} |
| 2144 |
char tmp = *se; *se = 0; |
| 2145 |
khash_str2int_inc(*skip_info, strdup(ss+6)); |
| 2146 |
*se = tmp; |
| 2147 |
} |
| 2148 |
else if ( !strncasecmp("^FORMAT/",ss,8) || !strncasecmp("^FMT/",ss,5) ) |
| 2149 |
{ |
| 2150 |
int n = !strncasecmp("^FMT/",ss,5) ? 5 : 8; |
| 2151 |
if ( !*skip_fmt ) |
| 2152 |
{ |
| 2153 |
*skip_fmt = khash_str2int_init(); |
| 2154 |
if ( str.l ) kputc(',',&str); |
| 2155 |
kputs("FORMAT",&str); |
| 2156 |
} |
| 2157 |
char tmp = *se; *se = 0; |
| 2158 |
khash_str2int_inc(*skip_fmt, strdup(ss+n)); |
| 2159 |
*se = tmp; |
| 2160 |
} |
| 2161 |
else |
| 2162 |
{ |
| 2163 |
if ( !*skip_info ) |
| 2164 |
{ |
| 2165 |
*skip_info = khash_str2int_init(); |
| 2166 |
if ( str.l ) kputc(',',&str); |
| 2167 |
kputs("INFO",&str); |
| 2168 |
} |
| 2169 |
char tmp = *se; *se = 0; |
| 2170 |
khash_str2int_inc(*skip_info, strdup(ss+1)); |
| 2171 |
*se = tmp; |
| 2172 |
} |
| 2173 |
|
| 2174 |
if ( !*se ) break; |
| 2175 |
ss = ++se; |
| 2176 |
} |
| 2177 |
free(columns); |
| 2178 |
return str.s; |
| 2179 |
} |
| 2180 |
static void bcf_hrec_format_rename(bcf_hrec_t *hrec, char *tag, kstring_t *str) |
| 2181 |
{ |
| 2182 |
int j, nout = 0; |
| 2183 |
ksprintf(str, "##%s=<", hrec->key); |
| 2184 |
for (j=0; j<hrec->nkeys; j++) |
| 2185 |
{ |
| 2186 |
if ( !strcmp("IDX",hrec->keys[j]) ) continue; |
| 2187 |
if ( nout ) kputc(',',str); |
| 2188 |
if ( !strcmp("ID", hrec->keys[j]) ) |
| 2189 |
ksprintf(str,"%s=%s", hrec->keys[j], tag); |
| 2190 |
else |
| 2191 |
ksprintf(str,"%s=%s", hrec->keys[j], hrec->vals[j]); |
| 2192 |
nout++; |
| 2193 |
} |
| 2194 |
ksprintf(str,">\n"); |
| 2195 |
} |
| 2196 |
static char *set_replace_mode(char *ss, int *replace) |
| 2197 |
{ |
| 2198 |
int mode = 0; |
| 2199 |
while (*ss) |
| 2200 |
{ |
| 2201 |
if ( *ss=='+' ) mode |= REPLACE_MISSING; |
| 2202 |
else if ( *ss=='-' ) mode |= REPLACE_NON_MISSING; |
| 2203 |
else if ( *ss=='=' ) mode |= SET_OR_APPEND; |
| 2204 |
else if ( *ss=='.' ) mode |= CARRY_OVER_MISSING; |
| 2205 |
else break; |
| 2206 |
ss++; |
| 2207 |
} |
| 2208 |
if ( !mode ) mode = REPLACE_ALL; |
| 2209 |
|
| 2210 |
|
| 2211 |
*replace = mode; |
| 2212 |
return ss; |
| 2213 |
} |
| 2214 |
static void rename_annots_push(args_t *args, char *src, char *dst); |
| 2215 |
static void init_columns(args_t *args) |
| 2216 |
{ |
| 2217 |
int need_sample_map = 0; |
| 2218 |
int sample_map_ok = init_sample_map(args, args->tgts_is_vcf?args->files->readers[1].header:NULL, args->hdr); |
| 2219 |
|
| 2220 |
kstring_t tmp = {0,0,0}; |
| 2221 |
if ( args->columns_is_file ) |
| 2222 |
{ |
| 2223 |
int i,n; |
| 2224 |
char **str = hts_readlist(args->columns, args->columns_is_file, &n); |
| 2225 |
if ( !str ) error("Could not parse %s\n", args->columns); |
| 2226 |
for (i=0; i<n; i++) |
| 2227 |
{ |
| 2228 |
char *ptr = str[i]; |
| 2229 |
while ( *ptr && !isspace(*ptr) ) ptr++; |
| 2230 |
if ( *ptr ) |
| 2231 |
{ |
| 2232 |
*ptr = 0; |
| 2233 |
ptr++; |
| 2234 |
while ( *ptr && isspace(*ptr) ) ptr++; |
| 2235 |
if ( *ptr ) |
| 2236 |
{ |
| 2237 |
if ( args->merge_method_str.l ) kputc(',',&args->merge_method_str); |
| 2238 |
kputs(str[i],&args->merge_method_str); |
| 2239 |
kputc(':',&args->merge_method_str); |
| 2240 |
kputs(ptr,&args->merge_method_str); |
| 2241 |
} |
| 2242 |
} |
| 2243 |
if ( tmp.l ) kputc(',',&tmp); |
| 2244 |
kputs(str[i],&tmp); |
| 2245 |
free(str[i]); |
| 2246 |
} |
| 2247 |
free(str); |
| 2248 |
free(args->columns); |
| 2249 |
args->columns = tmp.s; |
| 2250 |
tmp.l = tmp.m = 0; |
| 2251 |
tmp.s = NULL; |
| 2252 |
} |
| 2253 |
|
| 2254 |
void *skip_fmt = NULL, *skip_info = NULL; |
| 2255 |
if ( args->tgts_is_vcf ) |
| 2256 |
args->columns = columns_complement(args->columns, &skip_info, &skip_fmt); |
| 2257 |
|
| 2258 |
kstring_t str = {0,0,0}; |
| 2259 |
char *ss = args->columns, *se = ss; |
| 2260 |
args->ncols = 0; |
| 2261 |
int icol = -1, has_fmt_str = 0; |
| 2262 |
while ( *ss ) |
| 2263 |
{ |
| 2264 |
char *ptr; |
| 2265 |
if ( *se && *se!=',' ) { se++; continue; } |
| 2266 |
int replace; |
| 2267 |
ss = set_replace_mode(ss, &replace); |
| 2268 |
icol++; |
| 2269 |
str.l = 0; |
| 2270 |
kputsn(ss, se-ss, &str); |
| 2271 |
if ( !str.s[0] || !strcasecmp("-",str.s) ) ; |
| 2272 |
else if ( !strcasecmp("CHROM",str.s) ) args->chr_idx = icol; |
| 2273 |
else if ( !strcasecmp("POS",str.s) ) |
| 2274 |
{ |
| 2275 |
if ( replace==REPLACE_NON_MISSING && !args->tgts_is_vcf ) |
| 2276 |
{ |
| 2277 |
args->ncols++; args->cols = (annot_col_t*) realloc(args->cols,sizeof(annot_col_t)*args->ncols); |
| 2278 |
annot_col_t *col = &args->cols[args->ncols-1]; |
| 2279 |
memset(col,0,sizeof(*col)); |
| 2280 |
col->icol = icol; |
| 2281 |
col->replace = replace; |
| 2282 |
col->setter = setter_pos; |
| 2283 |
col->hdr_key_src = strdup(str.s); |
| 2284 |
col->hdr_key_dst = strdup(str.s); |
| 2285 |
args->match_end = icol; |
| 2286 |
} |
| 2287 |
else |
| 2288 |
args->beg_idx = icol; |
| 2289 |
} |
| 2290 |
else if ( !strcasecmp("FROM",str.s) || !strcasecmp("BEG",str.s) ) args->beg_idx = icol; |
| 2291 |
else if ( !strcasecmp("TO",str.s) || !strcasecmp("END",str.s) ) args->end_idx = icol; |
| 2292 |
else if ( !strcasecmp("REF",str.s) ) |
| 2293 |
{ |
| 2294 |
if ( args->tgts_is_vcf ) |
| 2295 |
{ |
| 2296 |
args->ncols++; args->cols = (annot_col_t*) realloc(args->cols,sizeof(annot_col_t)*args->ncols); |
| 2297 |
annot_col_t *col = &args->cols[args->ncols-1]; |
| 2298 |
memset(col,0,sizeof(*col)); |
| 2299 |
col->setter = vcf_setter_ref; |
| 2300 |
col->hdr_key_src = strdup(str.s); |
| 2301 |
col->hdr_key_dst = strdup(str.s); |
| 2302 |
} |
| 2303 |
else args->ref_idx = icol; |
| 2304 |
} |
| 2305 |
else if ( !strcasecmp("ALT",str.s) ) |
| 2306 |
{ |
| 2307 |
if ( args->tgts_is_vcf ) |
| 2308 |
{ |
| 2309 |
args->ncols++; args->cols = (annot_col_t*) realloc(args->cols,sizeof(annot_col_t)*args->ncols); |
| 2310 |
annot_col_t *col = &args->cols[args->ncols-1]; |
| 2311 |
memset(col,0,sizeof(*col)); |
| 2312 |
col->setter = vcf_setter_alt; |
| 2313 |
col->hdr_key_src = strdup(str.s); |
| 2314 |
col->hdr_key_dst = strdup(str.s); |
| 2315 |
col->replace = replace; |
| 2316 |
if ( args->pair_logic==-1 ) bcf_sr_set_opt(args->files,BCF_SR_PAIR_LOGIC,BCF_SR_PAIR_BOTH_REF); |
| 2317 |
} |
| 2318 |
else args->alt_idx = icol; |
| 2319 |
} |
| 2320 |
else if ( !strcasecmp("ID",str.s) || !strcasecmp("~ID",str.s) ) |
| 2321 |
{ |
| 2322 |
if ( replace & REPLACE_NON_MISSING ) error("Apologies, the -ID feature has not been implemented yet.\n"); |
| 2323 |
if ( str.s[0]=='~' ) replace = MATCH_VALUE; |
| 2324 |
if ( args->tgts_is_vcf && (replace & MATCH_VALUE) ) error("todo: -c ~ID with -a VCF?\n"); |
| 2325 |
args->ncols++; args->cols = (annot_col_t*) realloc(args->cols,sizeof(annot_col_t)*args->ncols); |
| 2326 |
annot_col_t *col = &args->cols[args->ncols-1]; |
| 2327 |
memset(col,0,sizeof(*col)); |
| 2328 |
col->icol = icol; |
| 2329 |
col->replace = replace; |
| 2330 |
col->setter = args->tgts_is_vcf ? vcf_setter_id : setter_id; |
| 2331 |
col->hdr_key_src = strdup(str.s); |
| 2332 |
col->hdr_key_dst = strdup(str.s); |
| 2333 |
if ( replace & MATCH_VALUE ) args->match_id = icol; |
| 2334 |
} |
| 2335 |
else if ( !strcasecmp("~INFO/END",str.s) && !args->tgts_is_vcf ) |
| 2336 |
{ |
| 2337 |
replace = MATCH_VALUE; |
| 2338 |
args->ncols++; args->cols = (annot_col_t*) realloc(args->cols,sizeof(annot_col_t)*args->ncols); |
| 2339 |
annot_col_t *col = &args->cols[args->ncols-1]; |
| 2340 |
memset(col,0,sizeof(*col)); |
| 2341 |
col->icol = icol; |
| 2342 |
col->replace = replace; |
| 2343 |
col->setter = NULL; |
| 2344 |
col->hdr_key_src = strdup(str.s); |
| 2345 |
col->hdr_key_dst = strdup(str.s); |
| 2346 |
args->match_end = icol; |
| 2347 |
} |
| 2348 |
else if ( !strcasecmp("~POS",str.s) ) |
| 2349 |
{ |
| 2350 |
error("Error: the use of ~POS has been deprecated, use -POS to transfer the column POS.\n"); |
| 2351 |
} |
| 2352 |
else if ( str.s[0]=='~' ) |
| 2353 |
{ |
| 2354 |
args->ncols++; args->cols = (annot_col_t*) realloc(args->cols,sizeof(annot_col_t)*args->ncols); |
| 2355 |
annot_col_t *col = &args->cols[args->ncols-1]; |
| 2356 |
memset(col,0,sizeof(*col)); |
| 2357 |
col->icol = icol; |
| 2358 |
col->replace = MATCH_VALUE; |
| 2359 |
col->setter = NULL; |
| 2360 |
col->hdr_key_src = strdup(str.s+1); |
| 2361 |
} |
| 2362 |
else if ( !strcasecmp("-POS",str.s) && !args->tgts_is_vcf ) |
| 2363 |
{ |
| 2364 |
if ( args->tgts_is_vcf ) error("Error: cannot use -POS, position can be replaced only from a tab-delimited file\n"); |
| 2365 |
args->ncols++; args->cols = (annot_col_t*) realloc(args->cols,sizeof(annot_col_t)*args->ncols); |
| 2366 |
annot_col_t *col = &args->cols[args->ncols-1]; |
| 2367 |
memset(col,0,sizeof(*col)); |
| 2368 |
col->icol = icol; |
| 2369 |
col->replace = replace; |
| 2370 |
col->setter = setter_pos; |
| 2371 |
col->hdr_key_src = strdup(str.s); |
| 2372 |
col->hdr_key_dst = strdup(str.s); |
| 2373 |
args->match_end = icol; |
| 2374 |
} |
| 2375 |
else if ( !strncasecmp("ID:=",str.s,4) ) |
| 2376 |
{ |
| 2377 |
if ( !args->tgts_is_vcf ) error("The annotation source must be a VCF for \"%s\"\n",str.s); |
| 2378 |
if ( replace & REPLACE_NON_MISSING ) error("Apologies, the -ID feature has not been implemented yet.\n"); |
| 2379 |
args->ncols++; args->cols = (annot_col_t*) realloc(args->cols,sizeof(annot_col_t)*args->ncols); |
| 2380 |
annot_col_t *col = &args->cols[args->ncols-1]; |
| 2381 |
memset(col,0,sizeof(*col)); |
| 2382 |
col->icol = icol; |
| 2383 |
col->replace = replace; |
| 2384 |
col->setter = vcf_setter_id; |
| 2385 |
col->getter = vcf_getter_info_str2str; |
| 2386 |
str.s[2] = 0; |
| 2387 |
col->hdr_key_dst = strdup(str.s); |
| 2388 |
col->hdr_key_src = strncasecmp("INFO/",str.s+4,5) ? strdup(str.s+4) : strdup(str.s+4+5); |
| 2389 |
int hdr_id = bcf_hdr_id2int(args->tgts_hdr, BCF_DT_ID,col->hdr_key_src); |
| 2390 |
if ( !bcf_hdr_idinfo_exists(args->tgts_hdr,BCF_HL_INFO,hdr_id) ) |
| 2391 |
error("The INFO tag \"%s\" is not defined in %s\n", col->hdr_key_src, args->targets_fname); |
| 2392 |
if ( bcf_hdr_id2type(args->tgts_hdr,BCF_HL_INFO,hdr_id)!=BCF_HT_STR ) |
| 2393 |
error("Only Type=String tags can be used to annotate the ID column\n"); |
| 2394 |
} |
| 2395 |
else if ( (ptr=strstr(str.s,":=")) && (!args->targets_fname || !strncasecmp(ptr+2,"./",2)) ) |
| 2396 |
{ |
| 2397 |
*ptr = 0; |
| 2398 |
if ( !strncasecmp(str.s,"INFO/",5) && (!strcasecmp(ptr+2,"FILTER") || !strcasecmp(ptr+2,"./FILTER")) ) |
| 2399 |
{ |
| 2400 |
|
| 2401 |
args->ncols++; args->cols = (annot_col_t*) realloc(args->cols,sizeof(annot_col_t)*args->ncols); |
| 2402 |
annot_col_t *col = &args->cols[args->ncols-1]; |
| 2403 |
memset(col,0,sizeof(*col)); |
| 2404 |
col->icol = icol; |
| 2405 |
col->replace = replace; |
| 2406 |
col->setter = vcf_setter_info_str; |
| 2407 |
col->getter = vcf_getter_filter2str_local; |
| 2408 |
col->hdr_key_src = strdup(ptr+2); |
| 2409 |
col->hdr_key_dst = strdup(str.s+5); |
| 2410 |
tmp.l = 0; |
| 2411 |
ksprintf(&tmp,"##INFO=<ID=%s,Number=1,Type=String,Description=\"Transferred FILTER column\">",col->hdr_key_dst); |
| 2412 |
bcf_hdr_append(args->hdr_out, tmp.s); |
| 2413 |
if (bcf_hdr_sync(args->hdr_out) < 0) error_errno("[%s] Failed to update header", __func__); |
| 2414 |
int hdr_id = bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, col->hdr_key_dst); |
| 2415 |
col->number = bcf_hdr_id2length(args->hdr_out,BCF_HL_INFO,hdr_id); |
| 2416 |
} |
| 2417 |
else |
| 2418 |
rename_annots_push(args,ptr+2,str.s); |
| 2419 |
*ptr = ':'; |
| 2420 |
} |
| 2421 |
else if ( !strcasecmp("FILTER",str.s) ) |
| 2422 |
{ |
| 2423 |
if ( replace & REPLACE_NON_MISSING ) error("Apologies, the -FILTER feature has not been implemented yet.\n"); |
| 2424 |
args->ncols++; args->cols = (annot_col_t*) realloc(args->cols,sizeof(annot_col_t)*args->ncols); |
| 2425 |
annot_col_t *col = &args->cols[args->ncols-1]; |
| 2426 |
memset(col,0,sizeof(*col)); |
| 2427 |
col->icol = icol; |
| 2428 |
col->replace = replace; |
| 2429 |
col->setter = args->tgts_is_vcf ? vcf_setter_filter : setter_filter; |
| 2430 |
col->hdr_key_src = strdup(str.s); |
| 2431 |
col->hdr_key_dst = strdup(str.s); |
| 2432 |
if ( args->tgts_is_vcf ) |
| 2433 |
{ |
| 2434 |
bcf_hdr_t *tgts_hdr = args->files->readers[1].header; |
| 2435 |
int j; |
| 2436 |
for (j=0; j<tgts_hdr->nhrec; j++) |
| 2437 |
{ |
| 2438 |
bcf_hrec_t *hrec = tgts_hdr->hrec[j]; |
| 2439 |
if ( hrec->type!=BCF_HL_FLT ) continue; |
| 2440 |
int k = bcf_hrec_find_key(hrec,"ID"); |
| 2441 |
if ( k<0 ) error("[%s] Failed to parse the header, the ID attribute not found", __func__); |
| 2442 |
tmp.l = 0; |
| 2443 |
bcf_hrec_format(hrec, &tmp); |
| 2444 |
bcf_hdr_append(args->hdr_out, tmp.s); |
| 2445 |
} |
| 2446 |
if (bcf_hdr_sync(args->hdr_out) < 0) |
| 2447 |
error_errno("[%s] Failed to update header", __func__); |
| 2448 |
} |
| 2449 |
} |
| 2450 |
else if ( !strcasecmp("QUAL",str.s) ) |
| 2451 |
{ |
| 2452 |
if ( replace & REPLACE_NON_MISSING ) error("Apologies, the -QUAL feature has not been implemented yet.\n"); |
| 2453 |
if ( replace & SET_OR_APPEND ) error("Apologies, the =QUAL feature has not been implemented yet.\n"); |
| 2454 |
args->ncols++; args->cols = (annot_col_t*) realloc(args->cols,sizeof(annot_col_t)*args->ncols); |
| 2455 |
annot_col_t *col = &args->cols[args->ncols-1]; |
| 2456 |
memset(col,0,sizeof(*col)); |
| 2457 |
col->icol = icol; |
| 2458 |
col->replace = replace; |
| 2459 |
col->setter = args->tgts_is_vcf ? vcf_setter_qual : setter_qual; |
| 2460 |
col->hdr_key_src = strdup(str.s); |
| 2461 |
col->hdr_key_dst = strdup(str.s); |
| 2462 |
} |
| 2463 |
else if ( args->tgts_is_vcf && !strcasecmp("INFO",str.s) ) |
| 2464 |
{ |
| 2465 |
if ( replace & REPLACE_NON_MISSING ) error("Apologies, the -INFO/TAG feature has not been implemented yet.\n"); |
| 2466 |
if ( replace & SET_OR_APPEND ) error("Apologies, the =INFO feature has not been implemented yet.\n"); |
| 2467 |
bcf_hdr_t *tgts_hdr = args->files->readers[1].header; |
| 2468 |
int j; |
| 2469 |
for (j=0; j<tgts_hdr->nhrec; j++) |
| 2470 |
{ |
| 2471 |
bcf_hrec_t *hrec = tgts_hdr->hrec[j]; |
| 2472 |
if ( hrec->type!=BCF_HL_INFO ) continue; |
| 2473 |
int k = bcf_hrec_find_key(hrec,"ID"); |
| 2474 |
assert( k>=0 ); |
| 2475 |
if ( skip_info && khash_str2int_has_key(skip_info,hrec->vals[k]) ) continue; |
| 2476 |
tmp.l = 0; |
| 2477 |
bcf_hrec_format(hrec, &tmp); |
| 2478 |
bcf_hdr_append(args->hdr_out, tmp.s); |
| 2479 |
if (bcf_hdr_sync(args->hdr_out) < 0) |
| 2480 |
error_errno("[%s] Failed to update header", __func__); |
| 2481 |
int hdr_id = bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, hrec->vals[k]); |
| 2482 |
args->ncols++; args->cols = (annot_col_t*) realloc(args->cols,sizeof(annot_col_t)*args->ncols); |
| 2483 |
annot_col_t *col = &args->cols[args->ncols-1]; |
| 2484 |
memset(col,0,sizeof(*col)); |
| 2485 |
col->icol = -1; |
| 2486 |
col->replace = replace; |
| 2487 |
col->hdr_key_src = strdup(hrec->vals[k]); |
| 2488 |
col->hdr_key_dst = strdup(hrec->vals[k]); |
| 2489 |
col->number = bcf_hdr_id2length(args->hdr_out,BCF_HL_INFO,hdr_id); |
| 2490 |
switch ( bcf_hdr_id2type(args->hdr_out,BCF_HL_INFO,hdr_id) ) |
| 2491 |
{ |
| 2492 |
case BCF_HT_FLAG: col->setter = vcf_setter_info_flag; break; |
| 2493 |
case BCF_HT_INT: col->setter = vcf_setter_info_int; break; |
| 2494 |
case BCF_HT_REAL: col->setter = vcf_setter_info_real; break; |
| 2495 |
case BCF_HT_STR: col->setter = vcf_setter_info_str; break; |
| 2496 |
default: error("The type of %s not recognised (%d)\n", str.s,bcf_hdr_id2type(args->hdr_out,BCF_HL_INFO,hdr_id)); |
| 2497 |
} |
| 2498 |
} |
| 2499 |
} |
| 2500 |
else if ( args->tgts_is_vcf && (!strcasecmp("FORMAT",str.s) || !strcasecmp("FMT",str.s)) ) |
| 2501 |
{ |
| 2502 |
bcf_hdr_t *tgts_hdr = args->files->readers[1].header; |
| 2503 |
need_sample_map = 1; |
| 2504 |
int j; |
| 2505 |
for (j=0; j<tgts_hdr->nhrec; j++) |
| 2506 |
{ |
| 2507 |
bcf_hrec_t *hrec = tgts_hdr->hrec[j]; |
| 2508 |
if ( hrec->type!=BCF_HL_FMT) continue; |
| 2509 |
int k = bcf_hrec_find_key(hrec,"ID"); |
| 2510 |
assert( k>=0 ); |
| 2511 |
if ( skip_fmt && khash_str2int_has_key(skip_fmt,hrec->vals[k]) ) continue; |
| 2512 |
tmp.l = 0; |
| 2513 |
bcf_hrec_format(hrec, &tmp); |
| 2514 |
bcf_hdr_append(args->hdr_out, tmp.s); |
| 2515 |
if (bcf_hdr_sync(args->hdr_out) < 0) |
| 2516 |
error_errno("[%s] Failed to update header", __func__); |
| 2517 |
int hdr_id = bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, hrec->vals[k]); |
| 2518 |
args->ncols++; args->cols = (annot_col_t*) realloc(args->cols,sizeof(annot_col_t)*args->ncols); |
| 2519 |
annot_col_t *col = &args->cols[args->ncols-1]; |
| 2520 |
memset(col,0,sizeof(*col)); |
| 2521 |
col->icol = -1; |
| 2522 |
col->replace = replace; |
| 2523 |
col->hdr_key_src = strdup(hrec->vals[k]); |
| 2524 |
col->hdr_key_dst = strdup(hrec->vals[k]); |
| 2525 |
if ( !strcasecmp("GT",col->hdr_key_src) ) |
| 2526 |
{ |
| 2527 |
if ( !args->tgts_is_vcf ) error("The FORMAT/GT field can be currently populated only from a VCF\n"); |
| 2528 |
col->setter = vcf_setter_format_gt; |
| 2529 |
} |
| 2530 |
else |
| 2531 |
switch ( bcf_hdr_id2type(args->hdr_out,BCF_HL_FMT,hdr_id) ) |
| 2532 |
{ |
| 2533 |
case BCF_HT_INT: col->setter = vcf_setter_format_int; break; |
| 2534 |
case BCF_HT_REAL: col->setter = vcf_setter_format_real; break; |
| 2535 |
case BCF_HT_STR: col->setter = vcf_setter_format_str; has_fmt_str = 1; break; |
| 2536 |
default: error("The type of %s not recognised (%d)\n", str.s,bcf_hdr_id2type(args->hdr_out,BCF_HL_FMT,hdr_id)); |
| 2537 |
} |
| 2538 |
hdr_id = bcf_hdr_id2int(tgts_hdr, BCF_DT_ID, hrec->vals[k]); |
| 2539 |
col->number = bcf_hdr_id2length(tgts_hdr,BCF_HL_FMT,hdr_id); |
| 2540 |
} |
| 2541 |
} |
| 2542 |
else if ( !strncasecmp("FORMAT/",str.s, 7) || !strncasecmp("FMT/",str.s,4) ) |
| 2543 |
{ |
| 2544 |
char *key_dst = str.s + (!strncasecmp("FMT/",str.s,4) ? 4 : 7); |
| 2545 |
char *key_src = strstr(key_dst,":="); |
| 2546 |
if ( key_src ) |
| 2547 |
{ |
| 2548 |
*key_src = 0; |
| 2549 |
key_src += 2; |
| 2550 |
if ( !strncasecmp("FORMAT/",key_src,7) ) key_src += 7; |
| 2551 |
else if ( !strncasecmp("FMT/",key_src,4) ) key_src += 4; |
| 2552 |
} |
| 2553 |
else |
| 2554 |
key_src = key_dst; |
| 2555 |
need_sample_map = 1; |
| 2556 |
if ( args->tgts_is_vcf ) |
| 2557 |
{ |
| 2558 |
bcf_hrec_t *hrec = bcf_hdr_get_hrec(args->files->readers[1].header, BCF_HL_FMT, "ID", key_src, NULL); |
| 2559 |
if ( !hrec ) error("No such annotation \"%s\" in %s\n", key_src,args->targets_fname); |
| 2560 |
tmp.l = 0; |
| 2561 |
bcf_hrec_format_rename(hrec, key_dst, &tmp); |
| 2562 |
bcf_hdr_append(args->hdr_out, tmp.s); |
| 2563 |
if (bcf_hdr_sync(args->hdr_out) < 0) |
| 2564 |
error_errno("[%s] Failed to update header", __func__); |
| 2565 |
} |
| 2566 |
int hdr_id = bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, key_dst); |
| 2567 |
if ( !bcf_hdr_idinfo_exists(args->hdr_out,BCF_HL_FMT,hdr_id) ) |
| 2568 |
error("The FORMAT tag \"%s\" is not defined in %s, was the -h option provided?\n", str.s, args->targets_fname); |
| 2569 |
args->ncols++; args->cols = (annot_col_t*) realloc(args->cols,sizeof(annot_col_t)*args->ncols); |
| 2570 |
annot_col_t *col = &args->cols[args->ncols-1]; |
| 2571 |
memset(col,0,sizeof(*col)); |
| 2572 |
if ( !args->tgts_is_vcf ) |
| 2573 |
{ |
| 2574 |
col->icol = icol; |
| 2575 |
icol += args->nsmpl_annot - 1; |
| 2576 |
} |
| 2577 |
else |
| 2578 |
col->icol = -1; |
| 2579 |
col->replace = replace; |
| 2580 |
col->hdr_key_src = strdup(key_src); |
| 2581 |
col->hdr_key_dst = strdup(key_dst); |
| 2582 |
if ( !strcasecmp("GT",key_src) ) |
| 2583 |
{ |
| 2584 |
if ( !args->tgts_is_vcf ) error("The FORMAT/GT field can be currently populated only from a VCF\n"); |
| 2585 |
col->setter = vcf_setter_format_gt; |
| 2586 |
} |
| 2587 |
else |
| 2588 |
switch ( bcf_hdr_id2type(args->hdr_out,BCF_HL_FMT,hdr_id) ) |
| 2589 |
{ |
| 2590 |
case BCF_HT_INT: col->setter = args->tgts_is_vcf ? vcf_setter_format_int : setter_format_int; break; |
| 2591 |
case BCF_HT_REAL: col->setter = args->tgts_is_vcf ? vcf_setter_format_real : setter_format_real; break; |
| 2592 |
case BCF_HT_STR: col->setter = args->tgts_is_vcf ? vcf_setter_format_str : setter_format_str; has_fmt_str = 1; break; |
| 2593 |
default: error("The type of %s not recognised (%d)\n", str.s,bcf_hdr_id2type(args->hdr_out,BCF_HL_FMT,hdr_id)); |
| 2594 |
} |
| 2595 |
if ( args->tgts_is_vcf ) |
| 2596 |
{ |
| 2597 |
bcf_hdr_t *tgts_hdr = args->files->readers[1].header; |
| 2598 |
hdr_id = bcf_hdr_id2int(tgts_hdr, BCF_DT_ID, col->hdr_key_src); |
| 2599 |
col->number = bcf_hdr_id2length(tgts_hdr,BCF_HL_FMT,hdr_id); |
| 2600 |
} |
| 2601 |
} |
| 2602 |
else |
| 2603 |
{ |
| 2604 |
if ( replace & REPLACE_NON_MISSING ) error("Apologies, the -INFO/TAG feature has not been implemented yet.\n"); |
| 2605 |
if ( replace & SET_OR_APPEND ) |
| 2606 |
{ |
| 2607 |
if ( args->tgts_is_vcf ) |
| 2608 |
error("Error: the =INFO/TAG feature is currently supported only with TAB annotation files and has limitations\n" |
| 2609 |
" (the annotation type is modified to \"Number=.\" and allele ordering is disregarded)\n"); |
| 2610 |
fprintf(stderr,"Warning: the =INFO/TAG feature modifies the annotation to \"Number=.\" and disregards allele ordering\n"); |
| 2611 |
} |
| 2612 |
|
| 2613 |
args->ncols++; args->cols = (annot_col_t*) realloc(args->cols,sizeof(annot_col_t)*args->ncols); |
| 2614 |
annot_col_t *col = &args->cols[args->ncols-1]; |
| 2615 |
memset(col,0,sizeof(*col)); |
| 2616 |
col->icol = icol; |
| 2617 |
col->replace = replace; |
| 2618 |
|
| 2619 |
int explicit_src_info = 0; |
| 2620 |
int explicit_dst_info = 0; |
| 2621 |
char *key_dst; |
| 2622 |
if ( !strncasecmp("INFO/",str.s,5) ) |
| 2623 |
{ |
| 2624 |
key_dst = str.s + 5; |
| 2625 |
explicit_dst_info = 1; |
| 2626 |
} |
| 2627 |
else if ( !strcasecmp("~INFO/END",str.s) ) |
| 2628 |
{ |
| 2629 |
key_dst = str.s + 6; |
| 2630 |
explicit_dst_info = 1; |
| 2631 |
} |
| 2632 |
else |
| 2633 |
key_dst = str.s; |
| 2634 |
char *key_src = strstr(key_dst,":="); |
| 2635 |
if ( key_src ) |
| 2636 |
{ |
| 2637 |
*key_src = 0; |
| 2638 |
key_src += 2; |
| 2639 |
if ( !strncasecmp("INFO/",key_src,5) ) |
| 2640 |
{ |
| 2641 |
key_src += 5; |
| 2642 |
explicit_src_info = 1; |
| 2643 |
} |
| 2644 |
else if ( !strncasecmp("FMT/",key_src,4) || !strncasecmp("FORMAT/",key_src,5) ) |
| 2645 |
{ |
| 2646 |
key_src[-2] = ':'; |
| 2647 |
error("Did you mean \"FMT/%s\" rather than \"%s\"?\n",str.s,str.s); |
| 2648 |
} |
| 2649 |
else if ( !strcasecmp("FILTER",key_src) && args->tgts_is_vcf ) |
| 2650 |
{ |
| 2651 |
col->getter = vcf_getter_filter2str; |
| 2652 |
} |
| 2653 |
} |
| 2654 |
else |
| 2655 |
key_src = key_dst; |
| 2656 |
|
| 2657 |
col->hdr_key_src = strdup(key_src); |
| 2658 |
col->hdr_key_dst = strdup(key_dst); |
| 2659 |
|
| 2660 |
int hdr_id = bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, key_dst); |
| 2661 |
if ( !bcf_hdr_idinfo_exists(args->hdr_out,BCF_HL_INFO,hdr_id) ) |
| 2662 |
{ |
| 2663 |
if ( args->tgts_is_vcf ) |
| 2664 |
{ |
| 2665 |
if ( !strcasecmp("ID",key_src) && !explicit_src_info ) |
| 2666 |
{ |
| 2667 |
|
| 2668 |
tmp.l = 0; |
| 2669 |
ksprintf(&tmp,"##INFO=<ID=%s,Number=1,Type=String,Description=\"Transferred ID column\">",key_dst); |
| 2670 |
} |
| 2671 |
else if ( !strcasecmp("FILTER",key_src) && !explicit_src_info ) |
| 2672 |
{ |
| 2673 |
|
| 2674 |
tmp.l = 0; |
| 2675 |
ksprintf(&tmp,"##INFO=<ID=%s,Number=1,Type=String,Description=\"Transferred FILTER column\">",key_dst); |
| 2676 |
} |
| 2677 |
else |
| 2678 |
{ |
| 2679 |
bcf_hrec_t *hrec = bcf_hdr_get_hrec(args->files->readers[1].header, BCF_HL_INFO, "ID", key_src, NULL); |
| 2680 |
if ( !hrec ) |
| 2681 |
{ |
| 2682 |
if ( explicit_dst_info+explicit_src_info==0 && bcf_hdr_get_hrec(args->files->readers[1].header, BCF_HL_FMT, "ID", key_src, NULL) ) |
| 2683 |
error("Did you mean \"FMT/%s\" rather than \"%s\"?\n",str.s,str.s); |
| 2684 |
char *ptr = strchr(key_src,'='); |
| 2685 |
if ( ptr ) |
| 2686 |
{ |
| 2687 |
*ptr = 0; tmp.l = 0; ksprintf(&tmp,"%s:=%s",key_src,ptr+1); *ptr = '='; |
| 2688 |
error("The INFO tag \"%s\" is not defined, is this what you want \"%s\" ?\n",key_src,tmp.s); |
| 2689 |
} |
| 2690 |
error("The INFO tag \"%s\" is not defined in %s, was the -h option provided?\n", key_src,args->files->readers[1].fname); |
| 2691 |
} |
| 2692 |
tmp.l = 0; |
| 2693 |
bcf_hrec_format_rename(hrec, key_dst, &tmp); |
| 2694 |
} |
| 2695 |
bcf_hdr_append(args->hdr_out, tmp.s); |
| 2696 |
if (bcf_hdr_sync(args->hdr_out) < 0) |
| 2697 |
error_errno("[%s] Failed to update header", __func__); |
| 2698 |
hdr_id = bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, key_dst); |
| 2699 |
} |
| 2700 |
else |
| 2701 |
error("The INFO tag \"%s\" is not defined in %s, was the -h option provided?\n", key_dst, args->targets_fname); |
| 2702 |
assert( bcf_hdr_idinfo_exists(args->hdr_out,BCF_HL_INFO,hdr_id) ); |
| 2703 |
} |
| 2704 |
if ( args->tgts_is_vcf ) |
| 2705 |
{ |
| 2706 |
if ( !strcasecmp("ID",key_src) && !explicit_src_info ) col->getter = vcf_getter_id2str; |
| 2707 |
else if ( !strcasecmp("FILTER",key_src) && !explicit_src_info ) col->getter = vcf_getter_filter2str; |
| 2708 |
} |
| 2709 |
col->number = bcf_hdr_id2length(args->hdr_out,BCF_HL_INFO,hdr_id); |
| 2710 |
switch ( bcf_hdr_id2type(args->hdr_out,BCF_HL_INFO,hdr_id) ) |
| 2711 |
{ |
| 2712 |
case BCF_HT_FLAG: col->setter = args->tgts_is_vcf ? vcf_setter_info_flag : setter_info_flag; break; |
| 2713 |
case BCF_HT_INT: col->setter = args->tgts_is_vcf ? vcf_setter_info_int : setter_info_int; break; |
| 2714 |
case BCF_HT_REAL: col->setter = args->tgts_is_vcf ? vcf_setter_info_real : setter_info_real; break; |
| 2715 |
case BCF_HT_STR: col->setter = args->tgts_is_vcf ? vcf_setter_info_str : setter_info_str; break; |
| 2716 |
default: error("The type of %s not recognised (%d)\n", str.s,bcf_hdr_id2type(args->hdr_out,BCF_HL_INFO,hdr_id)); |
| 2717 |
} |
| 2718 |
if ( replace & SET_OR_APPEND ) |
| 2719 |
{ |
| 2720 |
bcf_hrec_t *hrec = bcf_hdr_get_hrec(args->hdr_out, BCF_HL_INFO, "ID", key_dst, NULL); |
| 2721 |
if ( !hrec ) error("Uh, could not find the new tag \"%s\" in the header\n", key_dst); |
| 2722 |
hrec = bcf_hrec_dup(hrec); |
| 2723 |
int j = bcf_hrec_find_key(hrec, "Number"); |
| 2724 |
if ( j<0 ) error("Uh, could not find the entry Number in the header record of %s\n",key_dst); |
| 2725 |
free(hrec->vals[j]); |
| 2726 |
hrec->vals[j] = strdup("."); |
| 2727 |
bcf_hdr_remove(args->hdr_out,BCF_HL_INFO, key_dst); |
| 2728 |
bcf_hdr_add_hrec(args->hdr_out, hrec); |
| 2729 |
} |
| 2730 |
} |
| 2731 |
if ( !*se ) break; |
| 2732 |
ss = ++se; |
| 2733 |
} |
| 2734 |
free(str.s); |
| 2735 |
free(tmp.s); |
| 2736 |
free(args->columns); |
| 2737 |
if ( skip_info ) khash_str2int_destroy_free(skip_info); |
| 2738 |
if ( skip_fmt ) khash_str2int_destroy_free(skip_fmt); |
| 2739 |
if ( has_fmt_str ) |
| 2740 |
{ |
| 2741 |
int n = bcf_hdr_nsamples(args->hdr_out); |
| 2742 |
if ( args->tgts_is_vcf && n<bcf_hdr_nsamples(args->files->readers[1].header) ) n = bcf_hdr_nsamples(args->files->readers[1].header); |
| 2743 |
args->tmpp = (char**)malloc(sizeof(char*)*n); |
| 2744 |
args->tmpp2 = (char**)malloc(sizeof(char*)*n); |
| 2745 |
} |
| 2746 |
if ( !need_sample_map ) |
| 2747 |
{ |
| 2748 |
free(args->sample_map); |
| 2749 |
args->sample_map = NULL; |
| 2750 |
} |
| 2751 |
else if ( sample_map_ok<0 ) |
| 2752 |
error("No matching samples in source and destination file?\n"); |
| 2753 |
} |
| 2754 |
static void init_merge_method(args_t *args) |
| 2755 |
{ |
| 2756 |
int i; |
| 2757 |
for (i=0; i<args->ncols; i++) |
| 2758 |
{ |
| 2759 |
args->cols[i].merge_method = MM_FIRST; |
| 2760 |
args->cols[i].mm_str_hash = NULL; |
| 2761 |
args->cols[i].mm_dbl = NULL; |
| 2762 |
args->cols[i].mm_dbl_nalloc = args->cols[i].mm_dbl_nused = args->cols[i].mm_dbl_ndat = 0; |
| 2763 |
memset(&args->cols[i].mm_kstr, 0, sizeof(args->cols[i].mm_kstr)); |
| 2764 |
} |
| 2765 |
if ( !args->merge_method_str.l ) return; |
| 2766 |
if ( args->tgts_is_vcf ) error("Error: the --merge-logic is intended for use with BED or TAB-delimited files only.\n"); |
| 2767 |
if ( !args->tgt_idx && !args->tgts ) error("Error: BEG,END (or FROM,TO) columns or REF,ALT columns are expected with the --merge-logic option.\n"); |
| 2768 |
char *sb = args->merge_method_str.s; |
| 2769 |
while ( *sb ) |
| 2770 |
{ |
| 2771 |
char *se = sb; |
| 2772 |
while ( *se && *se!=',' ) se++; |
| 2773 |
args->tmpks.l = 0; |
| 2774 |
kputsn(sb, se-sb, &args->tmpks); |
| 2775 |
kputc(0, &args->tmpks); |
| 2776 |
char *mm_type_str = args->tmpks.s + args->tmpks.l; |
| 2777 |
while ( *mm_type_str!=':' && mm_type_str > args->tmpks.s ) mm_type_str--; |
| 2778 |
if ( *mm_type_str!=':' ) |
| 2779 |
error("Error: could not parse the argument to --merge-logic: %s\n", args->merge_method_str.s); |
| 2780 |
*mm_type_str = 0; |
| 2781 |
mm_type_str++; |
| 2782 |
int mm_type = MM_FIRST; |
| 2783 |
if ( !strcasecmp("unique",mm_type_str) ) mm_type = MM_UNIQUE; |
| 2784 |
else if ( !strcasecmp("first",mm_type_str) ) mm_type = MM_FIRST; |
| 2785 |
else if ( !strcasecmp("append",mm_type_str) ) mm_type = MM_APPEND; |
| 2786 |
else if ( !strcasecmp("append-missing",mm_type_str) ) |
| 2787 |
{ |
| 2788 |
mm_type = MM_APPEND_MISSING; |
| 2789 |
if ( args->ref_idx!=-1 ) args->has_append_mode = 1; |
| 2790 |
} |
| 2791 |
else if ( !strcasecmp("sum",mm_type_str) ) mm_type = MM_SUM; |
| 2792 |
else if ( !strcasecmp("avg",mm_type_str) ) mm_type = MM_AVG; |
| 2793 |
else if ( !strcasecmp("min",mm_type_str) ) mm_type = MM_MIN; |
| 2794 |
else if ( !strcasecmp("max",mm_type_str) ) mm_type = MM_MAX; |
| 2795 |
else error("Error: could not parse --merge-logic %s, the logic \"%s\" is not recognised\n", args->merge_method_str.s,mm_type_str); |
| 2796 |
for (i=0; i<args->ncols; i++) |
| 2797 |
{ |
| 2798 |
if ( strcmp(args->cols[i].hdr_key_dst,args->tmpks.s) ) continue; |
| 2799 |
if ( (mm_type==MM_APPEND || mm_type==MM_APPEND_MISSING) && args->cols[i].number!=BCF_VL_VAR ) |
| 2800 |
error("Error: --merge-logic append can be requested only for tags of variable length (Number=.)\n"); |
| 2801 |
args->cols[i].merge_method = mm_type; |
| 2802 |
break; |
| 2803 |
} |
| 2804 |
if ( i==args->ncols ) error("No such tag in the destination file: %s\n", args->tmpks.s); |
| 2805 |
sb = *se ? se + 1 : se; |
| 2806 |
} |
| 2807 |
if ( args->has_append_mode ) |
| 2808 |
{ |
| 2809 |
|
| 2810 |
args->aline_missing = (annot_line_t*)calloc(1,sizeof(*args->aline_missing)); |
| 2811 |
int ncol = 0; |
| 2812 |
for (i=0; i<args->ncols; i++) |
| 2813 |
if ( ncol < args->cols[i].icol + 1 ) ncol = args->cols[i].icol + 1; |
| 2814 |
if ( ncol < args->ref_idx + 1 ) ncol = args->ref_idx + 1; |
| 2815 |
args->aline_missing->mcols = ncol; |
| 2816 |
args->aline_missing->ncols = ncol; |
| 2817 |
args->aline_missing->cols = (char**) malloc(ncol*sizeof(char*)); |
| 2818 |
for (i=0; i<ncol; i++) |
| 2819 |
args->aline_missing->cols[i] = strdup("."); |
| 2820 |
} |
| 2821 |
} |
| 2822 |
|
| 2823 |
static void rename_chrs(args_t *args, char *fname) |
| 2824 |
{ |
| 2825 |
int n, i; |
| 2826 |
char **map = hts_readlist(fname, 1, &n); |
| 2827 |
if ( !map ) error("Could not read: %s\n", fname); |
| 2828 |
for (i=0; i<n; i++) |
| 2829 |
{ |
| 2830 |
char *ss = map[i]; |
| 2831 |
while ( *ss && !isspace(*ss) ) ss++; |
| 2832 |
if ( !*ss ) error("Could not parse: %s\n", fname); |
| 2833 |
*ss = 0; |
| 2834 |
int rid = bcf_hdr_name2id(args->hdr_out, map[i]); |
| 2835 |
bcf_hrec_t *hrec = bcf_hdr_get_hrec(args->hdr_out, BCF_HL_CTG, "ID", map[i], NULL); |
| 2836 |
if ( !hrec ) continue; |
| 2837 |
int j = bcf_hrec_find_key(hrec, "ID"); |
| 2838 |
assert( j>=0 ); |
| 2839 |
free(hrec->vals[j]); |
| 2840 |
ss++; |
| 2841 |
while ( *ss && isspace(*ss) ) ss++; |
| 2842 |
char *se = ss; |
| 2843 |
while ( *se && !isspace(*se) ) se++; |
| 2844 |
*se = 0; |
| 2845 |
hrec->vals[j] = strdup(ss); |
| 2846 |
args->hdr_out->id[BCF_DT_CTG][rid].key = hrec->vals[j]; |
| 2847 |
} |
| 2848 |
for (i=0; i<n; i++) free(map[i]); |
| 2849 |
free(map); |
| 2850 |
} |
| 2851 |
|
| 2852 |
static int rename_annots_core(args_t *args, char *ori_tag, char *new_tag) |
| 2853 |
{ |
| 2854 |
int type; |
| 2855 |
if ( !strncasecmp("info/",ori_tag,5) ) type = BCF_HL_INFO, ori_tag += 5; |
| 2856 |
else if ( !strncasecmp("format/",ori_tag,7) ) type = BCF_HL_FMT, ori_tag += 7; |
| 2857 |
else if ( !strncasecmp("fmt/",ori_tag,4) ) type = BCF_HL_FMT, ori_tag += 4; |
| 2858 |
else if ( !strncasecmp("filter/",ori_tag,7) ) type = BCF_HL_FLT, ori_tag += 7; |
| 2859 |
else return -1; |
| 2860 |
if ( !strncasecmp("info/",new_tag,5) ) |
| 2861 |
{ |
| 2862 |
if ( type != BCF_HL_INFO ) error("Cannot transfer %s to INFO\n", ori_tag); |
| 2863 |
new_tag += 5; |
| 2864 |
} |
| 2865 |
else if ( !strncasecmp("format/",new_tag,7) ) |
| 2866 |
{ |
| 2867 |
if ( type != BCF_HL_FMT ) error("Cannot transfer %s to FORMAT\n", ori_tag); |
| 2868 |
new_tag += 7; |
| 2869 |
} |
| 2870 |
else if ( !strncasecmp("fmt/",new_tag,4) ) |
| 2871 |
{ |
| 2872 |
if ( type != BCF_HL_FMT ) error("Cannot transfer %s to FORMAT\n", ori_tag); |
| 2873 |
new_tag += 4; |
| 2874 |
} |
| 2875 |
else if ( !strncasecmp("filter/",new_tag,7) ) |
| 2876 |
{ |
| 2877 |
if ( type != BCF_HL_FLT ) error("Cannot transfer %s to FILTER\n", ori_tag); |
| 2878 |
new_tag += 7; |
| 2879 |
} |
| 2880 |
int id = bcf_hdr_id2int(args->hdr_out, BCF_DT_ID, ori_tag); |
| 2881 |
if ( id<0 ) return 1; |
| 2882 |
bcf_hrec_t *hrec = bcf_hdr_get_hrec(args->hdr_out, type, "ID", ori_tag, NULL); |
| 2883 |
if ( !hrec ) return 1; |
| 2884 |
int j = bcf_hrec_find_key(hrec, "ID"); |
| 2885 |
assert( j>=0 ); |
| 2886 |
free(hrec->vals[j]); |
| 2887 |
char *ptr = new_tag; |
| 2888 |
while ( *ptr && !isspace(*ptr) ) ptr++; |
| 2889 |
*ptr = 0; |
| 2890 |
hrec->vals[j] = strdup(new_tag); |
| 2891 |
args->hdr_out->id[BCF_DT_ID][id].key = hrec->vals[j]; |
| 2892 |
return 0; |
| 2893 |
} |
| 2894 |
static void rename_annots(args_t *args) |
| 2895 |
{ |
| 2896 |
int i; |
| 2897 |
if ( args->rename_annots ) |
| 2898 |
{ |
| 2899 |
args->rename_annots_map = hts_readlist(args->rename_annots, 1, &args->rename_annots_nmap); |
| 2900 |
if ( !args->rename_annots_map ) error("Could not read: %s\n", args->rename_annots); |
| 2901 |
} |
| 2902 |
for (i=0; i<args->rename_annots_nmap; i++) |
| 2903 |
{ |
| 2904 |
char *ptr = args->rename_annots_map[i]; |
| 2905 |
while ( *ptr && !isspace(*ptr) ) ptr++; |
| 2906 |
if ( !*ptr ) error("Could not parse: %s\n", args->rename_annots_map[i]); |
| 2907 |
char *rmme = ptr; |
| 2908 |
*ptr = 0; |
| 2909 |
ptr++; |
| 2910 |
while ( *ptr && isspace(*ptr) ) ptr++; |
| 2911 |
if ( !*ptr ) { *rmme = ' '; error("Could not parse: %s\n", args->rename_annots_map[i]); } |
| 2912 |
if ( rename_annots_core(args, args->rename_annots_map[i], ptr) < 0 ) |
| 2913 |
error("Cannot rename \"%s\" to \"%s\"\n",args->rename_annots_map[i],ptr); |
| 2914 |
} |
| 2915 |
} |
| 2916 |
static void rename_annots_push(args_t *args, char *src, char *dst) |
| 2917 |
{ |
| 2918 |
args->rename_annots_nmap++; |
| 2919 |
args->rename_annots_map = (char**)realloc(args->rename_annots_map,sizeof(*args->rename_annots_map)*args->rename_annots_nmap); |
| 2920 |
kstring_t str = {0,0,0}; |
| 2921 |
ksprintf(&str,"%s %s",src,dst); |
| 2922 |
args->rename_annots_map[ args->rename_annots_nmap - 1 ] = str.s; |
| 2923 |
} |
| 2924 |
static void init_filters(args_t *args) |
| 2925 |
{ |
| 2926 |
|
| 2927 |
|
| 2928 |
|
| 2929 |
|
| 2930 |
|
| 2931 |
|
| 2932 |
kstring_t str = {0,0,0}; |
| 2933 |
char *src = strdup(args->filter_str); |
| 2934 |
int len = 0; |
| 2935 |
while (1) |
| 2936 |
{ |
| 2937 |
char *beg = strchr(src+len,'{'); |
| 2938 |
if ( !beg ) break; |
| 2939 |
|
| 2940 |
|
| 2941 |
char skip = 0; |
| 2942 |
char *tmp = src; |
| 2943 |
while ( tmp<beg ) |
| 2944 |
{ |
| 2945 |
if ( tmp[0]!='"' && tmp[0]!='\'' ) { tmp++; continue; } |
| 2946 |
|
| 2947 |
|
| 2948 |
int quote = tmp[0]; |
| 2949 |
tmp++; |
| 2950 |
while ( *tmp && tmp[0]!=quote ) tmp++; |
| 2951 |
if ( !*tmp ) error("Could not parse the expression: %s\n",args->filter_str); |
| 2952 |
len = tmp - src + 1; |
| 2953 |
skip = 1; |
| 2954 |
} |
| 2955 |
if ( skip ) continue; |
| 2956 |
|
| 2957 |
char *end = ++beg; |
| 2958 |
while ( *end && *end!='}' ) end++; |
| 2959 |
if ( !*end ) error("Could not parse the expression: %s\n",args->filter_str); |
| 2960 |
*end = 0; |
| 2961 |
|
| 2962 |
|
| 2963 |
int type = -1; |
| 2964 |
tmp = beg; |
| 2965 |
while ( *tmp && *tmp!=':' ) tmp++; |
| 2966 |
if ( *tmp ) |
| 2967 |
{ |
| 2968 |
*tmp = 0; |
| 2969 |
if ( !strcasecmp(beg,"str") ) type = BCF_HT_STR; |
| 2970 |
else if ( !strcasecmp(beg,"int") ) type = BCF_HT_INT; |
| 2971 |
else if ( !strcasecmp(beg,"float") ) type = BCF_HT_REAL; |
| 2972 |
} |
| 2973 |
args->n_ext++; |
| 2974 |
args->ext = (ext_t*)realloc(args->ext,sizeof(*args->ext)*args->n_ext); |
| 2975 |
ext_t *ext = &args->ext[args->n_ext-1]; |
| 2976 |
ext->ht_type = type; |
| 2977 |
ext->name = strdup(beg); |
| 2978 |
if ( beg-1 > src ) kputsn(src,beg-1-src,&str); |
| 2979 |
if ( type==-1 ) kputs("{}",&str); |
| 2980 |
else if ( type==BCF_HT_STR ) kputs("{str}",&str); |
| 2981 |
else if ( type==BCF_HT_INT ) kputs("{int}",&str); |
| 2982 |
else if ( type==BCF_HT_REAL ) kputs("{float}",&str); |
| 2983 |
len = str.l; |
| 2984 |
kputs(end+1,&str); |
| 2985 |
free(src); |
| 2986 |
src = strdup(str.s); |
| 2987 |
str.l = 0; |
| 2988 |
} |
| 2989 |
args->filter = filter_init(args->hdr, src); |
| 2990 |
free(src); |
| 2991 |
free(str.s); |
| 2992 |
|
| 2993 |
int i,j,n_ext; |
| 2994 |
const int *ext_type = filter_ext_types(args->filter, &n_ext); |
| 2995 |
if ( n_ext != args->n_ext ) |
| 2996 |
error("Failed to parse the expression, unexpected number of dynamic variables (%d vs %d): %s\n",n_ext,args->n_ext,args->filter_str); |
| 2997 |
|
| 2998 |
if ( !args->n_ext ) return; |
| 2999 |
|
| 3000 |
if ( !args->tgts ) |
| 3001 |
error("Error: dynamic variables in -i/-e expressions can be currently used only with tab-delimited file, not with VCF (todo)\n"); |
| 3002 |
|
| 3003 |
|
| 3004 |
args->ext_ptr = malloc(sizeof(*args->ext_ptr)*args->n_ext); |
| 3005 |
for (i=0; i<args->n_ext; i++) args->ext[i].ht_type = ext_type[i]; |
| 3006 |
args->filter_ext = args->filter; |
| 3007 |
args->filter = NULL; |
| 3008 |
|
| 3009 |
|
| 3010 |
if ( args->ncols ) |
| 3011 |
{ |
| 3012 |
for (i=0; i<args->n_ext; i++) |
| 3013 |
{ |
| 3014 |
for (j=0; j<args->ncols; j++) |
| 3015 |
{ |
| 3016 |
if ( strcmp(args->ext[i].name,args->cols[j].hdr_key_src) ) continue; |
| 3017 |
args->ext[i].icol = args->cols[j].icol; |
| 3018 |
break; |
| 3019 |
} |
| 3020 |
if ( j==args->ncols ) error("No such column: %s\n",args->ext[i].name); |
| 3021 |
} |
| 3022 |
} |
| 3023 |
} |
| 3024 |
|
| 3025 |
static void init_data(args_t *args) |
| 3026 |
{ |
| 3027 |
args->hdr = args->files->readers[0].header; |
| 3028 |
args->hdr_out = bcf_hdr_dup(args->hdr); |
| 3029 |
|
| 3030 |
if ( args->set_ids_fmt ) |
| 3031 |
{ |
| 3032 |
if ( args->set_ids_fmt[0]=='+' ) { args->set_ids_replace = 0; args->set_ids_fmt++; } |
| 3033 |
args->set_ids = convert_init(args->hdr_out, NULL, 0, args->set_ids_fmt); |
| 3034 |
} |
| 3035 |
if ( args->remove_annots ) init_remove_annots(args); |
| 3036 |
if ( args->header_fname || args->header_lines ) init_header_lines(args); |
| 3037 |
if ( args->targets_fname && args->tgts_is_vcf ) |
| 3038 |
{ |
| 3039 |
|
| 3040 |
if ( !bcf_sr_add_reader(args->files, args->targets_fname) ) |
| 3041 |
error("Failed to open %s: %s\n", args->targets_fname,bcf_sr_strerror(args->files->errnum)); |
| 3042 |
args->tgts_hdr = args->files->readers[1].header; |
| 3043 |
} |
| 3044 |
if ( args->columns ) init_columns(args); |
| 3045 |
if ( args->targets_fname && !args->tgts_is_vcf ) |
| 3046 |
{ |
| 3047 |
if ( !args->columns ) error("The -c option not given\n"); |
| 3048 |
if ( args->chr_idx==-1 ) error("The -c CHROM option not given\n"); |
| 3049 |
if ( args->beg_idx==-1 ) error("The -c POS option not given\n"); |
| 3050 |
if ( args->single_overlaps && args->merge_method_str.l ) error("The options --merge-logic and --single-overlaps cannot be combined\n"); |
| 3051 |
if ( args->end_idx==-1 || (args->single_overlaps && !args->merge_method_str.l) ) |
| 3052 |
{ |
| 3053 |
args->end_idx = -args->beg_idx - 1; |
| 3054 |
args->tgts = bcf_sr_regions_init(args->targets_fname,1,args->chr_idx,args->beg_idx,args->end_idx); |
| 3055 |
if ( !args->tgts ) error("Could not initialize the annotation file: %s\n", args->targets_fname); |
| 3056 |
if ( !args->tgts->tbx ) error("Expected tabix-indexed annotation file: %s\n", args->targets_fname); |
| 3057 |
} |
| 3058 |
else |
| 3059 |
{ |
| 3060 |
if ( args->ref_idx!=-1 ) error("Error: the REF columns will be ignored when BEG,END (or FROM,TO) is present. Replace END (or TO) with \"-\".\n"); |
| 3061 |
int len = strlen(args->targets_fname); |
| 3062 |
if ( len>=7 && !strcasecmp(".bed.gz",args->targets_fname+len-7) ) args->tgt_is_bed = 1; |
| 3063 |
else if ( len>=8 && !strcasecmp(".bed.bgz",args->targets_fname+len-8) ) args->tgt_is_bed = 1; |
| 3064 |
else if ( len>=4 && !strcasecmp(".bed",args->targets_fname+len-4) ) args->tgt_is_bed = 1; |
| 3065 |
args->tgt_idx = regidx_init(args->targets_fname,parse_with_payload,free_payload,sizeof(char*),args); |
| 3066 |
if ( !args->tgt_idx ) error("Failed to parse: %s\n", args->targets_fname); |
| 3067 |
args->tgt_itr = regitr_init(args->tgt_idx); |
| 3068 |
args->nalines++; |
| 3069 |
hts_expand0(annot_line_t,args->nalines,args->malines,args->alines); |
| 3070 |
} |
| 3071 |
if ( args->min_overlap_str ) |
| 3072 |
{ |
| 3073 |
char *tmp = args->min_overlap_str; |
| 3074 |
if ( args->min_overlap_str[0] != ':' ) |
| 3075 |
{ |
| 3076 |
args->min_overlap_ann = strtod(args->min_overlap_str,&tmp); |
| 3077 |
if ( args->min_overlap_ann < 0 || args->min_overlap_ann > 1 || (*tmp && *tmp!=':') ) |
| 3078 |
error("Could not parse \"--min-overlap %s\", expected value(s) between 0-1\n", args->min_overlap_str); |
| 3079 |
} |
| 3080 |
if ( *tmp && *tmp==':' ) |
| 3081 |
{ |
| 3082 |
args->min_overlap_vcf = strtod(tmp+1,&tmp); |
| 3083 |
if ( args->min_overlap_vcf < 0 || args->min_overlap_vcf > 1 || *tmp ) |
| 3084 |
error("Could not parse \"--min-overlap %s\", expected value(s) between 0-1\n", args->min_overlap_str); |
| 3085 |
} |
| 3086 |
} |
| 3087 |
} |
| 3088 |
init_merge_method(args); |
| 3089 |
args->vcmp = vcmp_init(); |
| 3090 |
|
| 3091 |
if ( args->filter_str ) |
| 3092 |
init_filters(args); |
| 3093 |
|
| 3094 |
if ( args->mark_sites ) |
| 3095 |
{ |
| 3096 |
if ( !args->targets_fname ) |
| 3097 |
{ |
| 3098 |
if ( args->mark_sites_logic!=MARK_LISTED ) error("The -a option not given but -%s logic was requested\n",args->mark_sites); |
| 3099 |
fprintf(stderr,"Note: The -a option not given, all sites will be annotated with INFO/%s\n",args->mark_sites); |
| 3100 |
bcf_hdr_printf(args->hdr_out,"##INFO=<ID=%s,Number=0,Type=Flag,Description=\"Sites marked with `bcftools annotate -m %s`\">", |
| 3101 |
args->mark_sites,args->mark_sites); |
| 3102 |
} |
| 3103 |
else |
| 3104 |
bcf_hdr_printf(args->hdr_out,"##INFO=<ID=%s,Number=0,Type=Flag,Description=\"Sites %slisted in %s\">", |
| 3105 |
args->mark_sites,args->mark_sites_logic==MARK_LISTED?"":"not ",args->mark_sites); |
| 3106 |
} |
| 3107 |
|
| 3108 |
if (args->record_cmd_line) bcf_hdr_append_version(args->hdr_out, args->argc, args->argv, "bcftools_annotate"); |
| 3109 |
if ( !args->drop_header ) |
| 3110 |
{ |
| 3111 |
if ( args->rename_chrs ) rename_chrs(args, args->rename_chrs); |
| 3112 |
if ( args->rename_annots || args->rename_annots_map ) rename_annots(args); |
| 3113 |
|
| 3114 |
char wmode[8]; |
| 3115 |
set_wmode(wmode,args->output_type,args->output_fname,args->clevel); |
| 3116 |
args->out_fh = hts_open(args->output_fname ? args->output_fname : "-", wmode); |
| 3117 |
if ( args->out_fh == NULL ) error("[%s] Error: cannot write to \"%s\": %s\n", __func__,args->output_fname, strerror(errno)); |
| 3118 |
if ( args->n_threads ) |
| 3119 |
hts_set_opt(args->out_fh, HTS_OPT_THREAD_POOL, args->files->p); |
| 3120 |
if ( bcf_hdr_write(args->out_fh, args->hdr_out)!=0 ) error("[%s] Error: failed to write the header to %s\n", __func__,args->output_fname); |
| 3121 |
if ( init_index2(args->out_fh,args->hdr,args->output_fname, |
| 3122 |
&args->index_fn, args->write_index) < 0 ) |
| 3123 |
error("Error: failed to initialise index for %s\n",args->output_fname); |
| 3124 |
} |
| 3125 |
} |
| 3126 |
|
| 3127 |
static void destroy_data(args_t *args) |
| 3128 |
{ |
| 3129 |
int i; |
| 3130 |
for (i=0; i<args->n_ext; i++) |
| 3131 |
{ |
| 3132 |
free(args->ext[i].name); |
| 3133 |
if ( args->ext[i].ht_type!=BCF_HT_STR ) continue; |
| 3134 |
} |
| 3135 |
free(args->ext_ptr); |
| 3136 |
free(args->ext); |
| 3137 |
for (i=0; i<args->nrm; i++) free(args->rm[i].key); |
| 3138 |
free(args->rm); |
| 3139 |
if ( args->hdr_out ) bcf_hdr_destroy(args->hdr_out); |
| 3140 |
if (args->vcmp) vcmp_destroy(args->vcmp); |
| 3141 |
for (i=0; i<args->ncols; i++) |
| 3142 |
{ |
| 3143 |
free(args->cols[i].hdr_key_src); |
| 3144 |
free(args->cols[i].hdr_key_dst); |
| 3145 |
free(args->cols[i].mm_kstr.s); |
| 3146 |
if ( args->cols[i].mm_str_hash ) khash_str2int_destroy_free(args->cols[i].mm_str_hash); |
| 3147 |
free(args->cols[i].mm_dbl); |
| 3148 |
free(args->cols[i].ptr); |
| 3149 |
} |
| 3150 |
free(args->cols); |
| 3151 |
if ( args->aline_missing ) |
| 3152 |
{ |
| 3153 |
for (i=0; i<args->aline_missing->ncols; i++) free(args->aline_missing->cols[i]); |
| 3154 |
free(args->aline_missing->cols); |
| 3155 |
free(args->aline_missing); |
| 3156 |
} |
| 3157 |
for (i=0; i<args->malines; i++) |
| 3158 |
{ |
| 3159 |
free(args->alines[i].cols); |
| 3160 |
free(args->alines[i].als); |
| 3161 |
free(args->alines[i].line.s); |
| 3162 |
} |
| 3163 |
free(args->alines); |
| 3164 |
free(args->srt_alines); |
| 3165 |
if ( args->tgt_idx ) |
| 3166 |
{ |
| 3167 |
regidx_destroy(args->tgt_idx); |
| 3168 |
regitr_destroy(args->tgt_itr); |
| 3169 |
} |
| 3170 |
if ( args->rename_annots_map ) |
| 3171 |
{ |
| 3172 |
for (i=0; i<args->rename_annots_nmap; i++) free(args->rename_annots_map[i]); |
| 3173 |
free(args->rename_annots_map); |
| 3174 |
} |
| 3175 |
if ( args->tgts ) bcf_sr_regions_destroy(args->tgts); |
| 3176 |
free(args->tmpks.s); |
| 3177 |
free(args->tmpi); |
| 3178 |
free(args->tmpf); |
| 3179 |
free(args->tmps); |
| 3180 |
free(args->tmpp); |
| 3181 |
free(args->tmpi2); |
| 3182 |
free(args->tmpf2); |
| 3183 |
free(args->tmps2); |
| 3184 |
free(args->tmpp2); |
| 3185 |
free(args->tmpi3); |
| 3186 |
free(args->tmpf3); |
| 3187 |
free(args->src_smpl_pld); |
| 3188 |
free(args->dst_smpl_pld); |
| 3189 |
if ( args->set_ids ) |
| 3190 |
convert_destroy(args->set_ids); |
| 3191 |
if ( args->filter ) filter_destroy(args->filter); |
| 3192 |
if ( args->filter_ext ) filter_destroy(args->filter_ext); |
| 3193 |
if (args->out_fh) |
| 3194 |
{ |
| 3195 |
if ( args->write_index ) |
| 3196 |
{ |
| 3197 |
if ( bcf_idx_save(args->out_fh)<0 ) |
| 3198 |
{ |
| 3199 |
if ( hts_close(args->out_fh)!=0 ) error("Error: close failed .. %s\n", args->output_fname?args->output_fname:"stdout"); |
| 3200 |
error("Error: cannot write to index %s\n", args->index_fn); |
| 3201 |
} |
| 3202 |
free(args->index_fn); |
| 3203 |
} |
| 3204 |
if ( hts_close(args->out_fh)!=0 ) error("Error: close failed .. %s\n", args->output_fname?args->output_fname:"stdout"); |
| 3205 |
} |
| 3206 |
free(args->sample_map); |
| 3207 |
free(args->merge_method_str.s); |
| 3208 |
} |
| 3209 |
|
| 3210 |
static void parse_annot_line(args_t *args, char *str, annot_line_t *tmp) |
| 3211 |
{ |
| 3212 |
tmp->line.l = 0; |
| 3213 |
kputs(str, &tmp->line); |
| 3214 |
char *s = tmp->line.s; |
| 3215 |
tmp->ncols = 1; |
| 3216 |
hts_expand(char*,tmp->ncols,tmp->mcols,tmp->cols); |
| 3217 |
tmp->cols[0] = s; |
| 3218 |
while ( *s ) |
| 3219 |
{ |
| 3220 |
if ( *s=='\t' ) |
| 3221 |
{ |
| 3222 |
tmp->ncols++; |
| 3223 |
hts_expand(char*,tmp->ncols,tmp->mcols,tmp->cols); |
| 3224 |
tmp->cols[tmp->ncols-1] = s+1; |
| 3225 |
*s = 0; |
| 3226 |
} |
| 3227 |
s++; |
| 3228 |
} |
| 3229 |
if ( args->ref_idx != -1 ) |
| 3230 |
{ |
| 3231 |
if ( args->ref_idx >= tmp->ncols ) |
| 3232 |
error("Could not parse the line, expected %d+ columns, found %d:\n\t%s\n",args->ref_idx+1,tmp->ncols,str); |
| 3233 |
if ( args->alt_idx >= tmp->ncols ) |
| 3234 |
error("Could not parse the line, expected %d+ columns, found %d:\n\t%s\n",args->alt_idx+1,tmp->ncols,str); |
| 3235 |
tmp->nals = 2; |
| 3236 |
hts_expand(char*,tmp->nals,tmp->mals,tmp->als); |
| 3237 |
tmp->als[0] = tmp->cols[args->ref_idx]; |
| 3238 |
tmp->als[1] = s = tmp->cols[args->alt_idx]; |
| 3239 |
while ( *s ) |
| 3240 |
{ |
| 3241 |
if ( *s==',' ) |
| 3242 |
{ |
| 3243 |
tmp->nals++; |
| 3244 |
hts_expand(char*,tmp->nals,tmp->mals,tmp->als); |
| 3245 |
tmp->als[tmp->nals-1] = s+1; |
| 3246 |
*s = 0; |
| 3247 |
} |
| 3248 |
s++; |
| 3249 |
} |
| 3250 |
} |
| 3251 |
} |
| 3252 |
static void buffer_annot_lines(args_t *args, bcf1_t *line, int start_pos, int end_pos) |
| 3253 |
{ |
| 3254 |
if ( args->nalines && args->alines[0].rid != line->rid ) args->nalines = 0; |
| 3255 |
|
| 3256 |
int i = 0; |
| 3257 |
while ( i<args->nalines ) |
| 3258 |
{ |
| 3259 |
if ( line->pos > args->alines[i].end ) |
| 3260 |
{ |
| 3261 |
args->nalines--; |
| 3262 |
if ( args->nalines && i<args->nalines ) |
| 3263 |
{ |
| 3264 |
annot_line_t tmp = args->alines[i]; |
| 3265 |
memmove(&args->alines[i],&args->alines[i+1],(args->nalines-i)*sizeof(annot_line_t)); |
| 3266 |
args->alines[args->nalines] = tmp; |
| 3267 |
} |
| 3268 |
} |
| 3269 |
else i++; |
| 3270 |
} |
| 3271 |
if ( !args->filter_ext && args->ref_idx==-1 && args->nalines ) return; |
| 3272 |
|
| 3273 |
while ( !bcf_sr_regions_overlap(args->tgts, bcf_seqname(args->hdr,line), start_pos,end_pos) ) |
| 3274 |
{ |
| 3275 |
if ( args->nalines + 1 == 0xffff ) break; |
| 3276 |
args->nalines++; |
| 3277 |
hts_expand0(annot_line_t,args->nalines,args->malines,args->alines); |
| 3278 |
annot_line_t *tmp = &args->alines[args->nalines-1]; |
| 3279 |
tmp->rid = line->rid; |
| 3280 |
tmp->start = args->tgts->start; |
| 3281 |
tmp->end = args->tgts->end; |
| 3282 |
parse_annot_line(args, args->tgts->line.s, tmp); |
| 3283 |
if ( args->filter_ext || args->ref_idx != -1 ) |
| 3284 |
{ |
| 3285 |
int iseq = args->tgts->iseq; |
| 3286 |
if ( bcf_sr_regions_next(args->tgts)<0 || args->tgts->iseq!=iseq ) break; |
| 3287 |
} |
| 3288 |
else break; |
| 3289 |
} |
| 3290 |
} |
| 3291 |
|
| 3292 |
|
| 3293 |
static int str_match(char *needle, char *haystack) |
| 3294 |
{ |
| 3295 |
int len = strlen(needle); |
| 3296 |
char *ptr = haystack; |
| 3297 |
while ( *ptr && (ptr=strstr(ptr,needle)) ) |
| 3298 |
{ |
| 3299 |
if ( ptr[len]!=0 && ptr[len]!=';' ) ptr++; |
| 3300 |
else if ( ptr==haystack || ptr[-1]==';' ) return 1; |
| 3301 |
ptr++; |
| 3302 |
} |
| 3303 |
return 0; |
| 3304 |
} |
| 3305 |
|
| 3306 |
static int strstr_match(char *a, char *b) |
| 3307 |
{ |
| 3308 |
char *beg = a; |
| 3309 |
while ( *beg ) |
| 3310 |
{ |
| 3311 |
char *end = beg; |
| 3312 |
while ( *end && *end!=';' ) end++; |
| 3313 |
char tmp = *end; |
| 3314 |
if ( *end==';' ) *end = 0; |
| 3315 |
int ret = str_match(beg,b); |
| 3316 |
*end = tmp; |
| 3317 |
if ( ret || !*end ) return ret; |
| 3318 |
beg = end + 1; |
| 3319 |
} |
| 3320 |
return 0; |
| 3321 |
} |
| 3322 |
static int annotate_from_regidx(args_t *args, bcf1_t *line) |
| 3323 |
{ |
| 3324 |
int j; |
| 3325 |
int has_overlap = 0; |
| 3326 |
|
| 3327 |
for (j=0; j<args->ncols; j++) args->cols[j].done = 0; |
| 3328 |
if ( regidx_overlap(args->tgt_idx, bcf_seqname(args->hdr,line),line->pos,line->pos+line->rlen-1, args->tgt_itr) ) |
| 3329 |
{ |
| 3330 |
hts_pos_t vcf_end = line->pos + line->rlen - 1; |
| 3331 |
while ( regitr_overlap(args->tgt_itr) ) |
| 3332 |
{ |
| 3333 |
annot_line_t *tmp = &args->alines[0]; |
| 3334 |
tmp->rid = line->rid; |
| 3335 |
tmp->start = args->tgt_itr->beg; |
| 3336 |
tmp->end = args->tgt_itr->end; |
| 3337 |
|
| 3338 |
|
| 3339 |
int len_ann = tmp->end - tmp->start + 1; |
| 3340 |
int len_vcf = line->rlen; |
| 3341 |
int isec = (tmp->end < vcf_end ? tmp->end : vcf_end) - (tmp->start > line->pos ? tmp->start : line->pos) + 1; |
| 3342 |
assert( isec > 0 ); |
| 3343 |
if ( args->min_overlap_ann && args->min_overlap_ann > (float)isec/len_ann ) continue; |
| 3344 |
if ( args->min_overlap_vcf && args->min_overlap_vcf > (float)isec/len_vcf ) continue; |
| 3345 |
|
| 3346 |
parse_annot_line(args, regitr_payload(args->tgt_itr,char*), tmp); |
| 3347 |
|
| 3348 |
|
| 3349 |
|
| 3350 |
|
| 3351 |
if ( !args->ncols && args->mark_sites ) has_overlap = 1; |
| 3352 |
|
| 3353 |
for (j=0; j<args->ncols; j++) |
| 3354 |
{ |
| 3355 |
if ( args->cols[j].done==1 ) continue; |
| 3356 |
int ret = args->cols[j].setter(args,line,&args->cols[j],tmp); |
| 3357 |
if ( ret < 0 ) |
| 3358 |
error("fixme: Could not set %s at %s:%"PRId64"\n", args->cols[j].hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 3359 |
if ( ret==0 ) |
| 3360 |
args->cols[j].done = 1; |
| 3361 |
has_overlap = 1; |
| 3362 |
} |
| 3363 |
} |
| 3364 |
} |
| 3365 |
for (j=0; j<args->ncols; j++) |
| 3366 |
{ |
| 3367 |
if ( args->cols[j].done==1 || args->cols[j].merge_method == MM_FIRST ) continue; |
| 3368 |
if ( !args->cols[j].setter ) continue; |
| 3369 |
if ( args->cols[j].setter(args,line,&args->cols[j],NULL) < 0 ) |
| 3370 |
error("fixme: Could not set %s at %s:%"PRId64"\n", args->cols[j].hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 3371 |
} |
| 3372 |
return has_overlap; |
| 3373 |
} |
| 3374 |
static int pass_filter_test_ext(args_t *args, bcf1_t *line, annot_line_t *ann) |
| 3375 |
{ |
| 3376 |
char *tmp; |
| 3377 |
int i; |
| 3378 |
for (i=0; i<args->n_ext; i++) |
| 3379 |
{ |
| 3380 |
int j = args->ext[i].icol; |
| 3381 |
if ( args->ext[i].ht_type==BCF_HT_STR ) args->ext_ptr[i] = args->ext[i].s = ann->cols[j]; |
| 3382 |
else if ( args->ext[i].ht_type==BCF_HT_INT ) |
| 3383 |
{ |
| 3384 |
args->ext[i].i = strtol(ann->cols[j],&tmp,10); |
| 3385 |
if ( *tmp ) |
| 3386 |
{ |
| 3387 |
if ( strcmp(".",ann->cols[j]) ) error("Error: could not parse the annotation file, expected an integer, found \"%s\"\n",ann->cols[j]); |
| 3388 |
args->ext_ptr[i] = NULL; |
| 3389 |
} |
| 3390 |
else |
| 3391 |
args->ext_ptr[i] = &args->ext[i].i; |
| 3392 |
} |
| 3393 |
else if ( args->ext[i].ht_type==BCF_HT_REAL ) |
| 3394 |
{ |
| 3395 |
args->ext[i].f = strtod(ann->cols[j],&tmp); |
| 3396 |
if ( *tmp ) |
| 3397 |
{ |
| 3398 |
if ( strcmp(".",ann->cols[j]) ) error("Error: could not parse the annotation file, expected a float, found \"%s\"\n",ann->cols[j]); |
| 3399 |
args->ext_ptr[i] = NULL; |
| 3400 |
} |
| 3401 |
else |
| 3402 |
args->ext_ptr[i] = &args->ext[i].f; |
| 3403 |
} |
| 3404 |
} |
| 3405 |
int pass = filter_test_ext(args->filter_ext,line,NULL,(const void**)args->ext_ptr); |
| 3406 |
if ( args->filter_logic==FLT_EXCLUDE ) pass = pass ? 0 : 1; |
| 3407 |
return pass; |
| 3408 |
} |
| 3409 |
static int annotate_from_tab(args_t *args, bcf1_t *line) |
| 3410 |
{ |
| 3411 |
int i,j; |
| 3412 |
int has_overlap = 0; |
| 3413 |
|
| 3414 |
|
| 3415 |
|
| 3416 |
|
| 3417 |
|
| 3418 |
int end_pos = line->pos + line->rlen - 1; |
| 3419 |
buffer_annot_lines(args, line, line->pos, end_pos); |
| 3420 |
|
| 3421 |
args->nsrt_alines = 0; |
| 3422 |
hts_expand(uint32_t,args->nalines,args->msrt_alines,args->srt_alines); |
| 3423 |
if ( args->nalines >= 0xffff || line->n_allele >= 0xffff ) |
| 3424 |
error("Error: too many alleles or annotation lines in the buffer at %s:%"PRId64" (todo:skip?)\n",bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 3425 |
|
| 3426 |
kstring_t match_end = {0,0,0}; |
| 3427 |
if ( args->match_end>=0 && bcf_get_info_int32(args->hdr,line,"END",&args->tmpi,&args->mtmpi)==1 ) |
| 3428 |
kputw(args->tmpi[0],&match_end); |
| 3429 |
|
| 3430 |
|
| 3431 |
for (i=0; i<args->nalines; i++) |
| 3432 |
{ |
| 3433 |
if ( line->pos > args->alines[i].end || end_pos < args->alines[i].start ) continue; |
| 3434 |
if ( args->ref_idx != -1 ) |
| 3435 |
{ |
| 3436 |
if ( line->pos!=args->alines[i].start || vcmp_set_ref(args->vcmp, line->d.allele[0], args->alines[i].als[0]) < 0 ) continue; |
| 3437 |
for (j=1; j<args->alines[i].nals; j++) |
| 3438 |
{ |
| 3439 |
int ialt; |
| 3440 |
if ( line->n_allele==1 && args->alines[i].als[j][0]=='.' && args->alines[i].als[j][1]==0 ) |
| 3441 |
ialt = 0; |
| 3442 |
else |
| 3443 |
{ |
| 3444 |
ialt = vcmp_find_allele(args->vcmp, line->d.allele+1, line->n_allele - 1, args->alines[i].als[j]); |
| 3445 |
if ( ialt < 0 ) continue; |
| 3446 |
ialt++; |
| 3447 |
} |
| 3448 |
if ( args->match_id>=0 && !strstr_match(line->d.id,args->alines[i].cols[args->match_id]) ) continue; |
| 3449 |
if ( args->match_end>=0 && match_end.l && strcmp(match_end.s,args->alines[i].cols[args->match_end]) ) continue; |
| 3450 |
if ( args->filter_ext && !pass_filter_test_ext(args,line,&args->alines[i]) ) continue; |
| 3451 |
args->srt_alines[args->nsrt_alines++] = (ialt<<16) | i; |
| 3452 |
has_overlap = 1; |
| 3453 |
break; |
| 3454 |
} |
| 3455 |
} |
| 3456 |
else if ( args->filter_ext ) |
| 3457 |
{ |
| 3458 |
if ( pass_filter_test_ext(args,line,&args->alines[i]) ) |
| 3459 |
{ |
| 3460 |
args->srt_alines[args->nsrt_alines++] = (0xffff<<16) | i; |
| 3461 |
has_overlap = 1; |
| 3462 |
} |
| 3463 |
} |
| 3464 |
else |
| 3465 |
{ |
| 3466 |
args->srt_alines[args->nsrt_alines++] = (0xffff<<16) | i; |
| 3467 |
has_overlap = 1; |
| 3468 |
} |
| 3469 |
} |
| 3470 |
|
| 3471 |
free(match_end.s); |
| 3472 |
if ( !has_overlap && args->filter_ext && !args->keep_sites ) return has_overlap; |
| 3473 |
|
| 3474 |
|
| 3475 |
if ( args->has_append_mode ) |
| 3476 |
{ |
| 3477 |
|
| 3478 |
uint32_t tmp; |
| 3479 |
for (i=1; i<args->nsrt_alines; i++) |
| 3480 |
for (j=i; j>0 && args->srt_alines[j] < args->srt_alines[j-1]; j--) |
| 3481 |
tmp = args->srt_alines[j], args->srt_alines[j] = args->srt_alines[j-1], args->srt_alines[j-1] = tmp; |
| 3482 |
} |
| 3483 |
|
| 3484 |
for (j=0; j<args->ncols; j++) args->cols[j].done = 0; |
| 3485 |
int ialt_exp = 1; |
| 3486 |
for (i=0; i<args->nsrt_alines; i++) |
| 3487 |
{ |
| 3488 |
int ialt = args->srt_alines[i] >> 16; |
| 3489 |
int ilin = args->srt_alines[i] & 0xffff; |
| 3490 |
if ( args->has_append_mode ) |
| 3491 |
{ |
| 3492 |
if ( ialt_exp > ialt ) continue; |
| 3493 |
if ( ialt_exp < ialt ) |
| 3494 |
{ |
| 3495 |
|
| 3496 |
while ( ialt_exp++ < ialt ) |
| 3497 |
{ |
| 3498 |
for (j=0; j<args->ncols; j++) |
| 3499 |
{ |
| 3500 |
if ( args->cols[j].merge_method != MM_APPEND_MISSING ) continue; |
| 3501 |
if ( args->cols[j].done==1 ) continue; |
| 3502 |
if ( !args->cols[j].setter ) continue; |
| 3503 |
int ret = args->cols[j].setter(args,line,&args->cols[j],args->aline_missing); |
| 3504 |
if ( ret < 0 ) |
| 3505 |
error("fixme: Could not set missing %s at %s:%"PRId64"\n", args->cols[j].hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 3506 |
if ( ret==0 ) |
| 3507 |
args->cols[j].done = 1; |
| 3508 |
} |
| 3509 |
} |
| 3510 |
} |
| 3511 |
} |
| 3512 |
for (j=0; j<args->ncols; j++) |
| 3513 |
{ |
| 3514 |
if ( args->cols[j].done==1 ) continue; |
| 3515 |
if ( !args->cols[j].setter ) continue; |
| 3516 |
int ret = args->cols[j].setter(args,line,&args->cols[j],&args->alines[ilin]); |
| 3517 |
if ( ret < 0 ) |
| 3518 |
error("fixme: Could not set %s at %s:%"PRId64"\n", args->cols[j].hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 3519 |
if ( ret==0 ) |
| 3520 |
args->cols[j].done = 1; |
| 3521 |
} |
| 3522 |
ialt_exp = ialt + 1; |
| 3523 |
} |
| 3524 |
if ( args->nsrt_alines ) |
| 3525 |
{ |
| 3526 |
|
| 3527 |
|
| 3528 |
if ( args->has_append_mode && ialt_exp < line->n_allele ) |
| 3529 |
{ |
| 3530 |
while ( ialt_exp++ < line->n_allele ) |
| 3531 |
{ |
| 3532 |
for (j=0; j<args->ncols; j++) |
| 3533 |
{ |
| 3534 |
if ( args->cols[j].merge_method != MM_APPEND_MISSING ) continue; |
| 3535 |
if ( args->cols[j].done==1 ) continue; |
| 3536 |
if ( !args->cols[j].setter ) continue; |
| 3537 |
int ret = args->cols[j].setter(args,line,&args->cols[j],args->aline_missing); |
| 3538 |
if ( ret < 0 ) |
| 3539 |
error("fixme: Could not set missing %s at %s:%"PRId64"\n", args->cols[j].hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 3540 |
if ( ret==0 ) |
| 3541 |
args->cols[j].done = 1; |
| 3542 |
} |
| 3543 |
} |
| 3544 |
} |
| 3545 |
|
| 3546 |
for (j=0; j<args->ncols; j++) |
| 3547 |
{ |
| 3548 |
if ( args->cols[j].done==1 || args->cols[j].merge_method == MM_FIRST ) continue; |
| 3549 |
if ( !args->cols[j].setter ) continue; |
| 3550 |
int ret = args->cols[j].setter(args,line,&args->cols[j],NULL); |
| 3551 |
if ( ret < 0 ) |
| 3552 |
error("fixme: Could not set %s at %s:%"PRId64"\n", args->cols[j].hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 3553 |
} |
| 3554 |
} |
| 3555 |
return has_overlap; |
| 3556 |
} |
| 3557 |
static int annotate_from_vcf(args_t *args, bcf1_t *line) |
| 3558 |
{ |
| 3559 |
if ( !bcf_sr_has_line(args->files,1) ) return 0; |
| 3560 |
int j; |
| 3561 |
bcf1_t *aline = bcf_sr_get_line(args->files,1); |
| 3562 |
for (j=0; j<args->ncols; j++) |
| 3563 |
{ |
| 3564 |
if ( !args->cols[j].setter ) continue; |
| 3565 |
if ( args->cols[j].setter(args,line,&args->cols[j],aline) ) |
| 3566 |
error("fixme: Could not set %s at %s:%"PRId64"\n", args->cols[j].hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 3567 |
} |
| 3568 |
return 1; |
| 3569 |
} |
| 3570 |
static int annotate_from_self(args_t *args, bcf1_t *line) |
| 3571 |
{ |
| 3572 |
int j; |
| 3573 |
for (j=0; j<args->ncols; j++) |
| 3574 |
{ |
| 3575 |
if ( !args->cols[j].setter ) continue; |
| 3576 |
if ( args->cols[j].setter(args,line,&args->cols[j],NULL) ) |
| 3577 |
error("fixme: Could not set %s at %s:%"PRId64"\n", args->cols[j].hdr_key_src,bcf_seqname(args->hdr,line),(int64_t) line->pos+1); |
| 3578 |
} |
| 3579 |
return 0; |
| 3580 |
} |
| 3581 |
static int annotate_line(args_t *args, bcf1_t *line) |
| 3582 |
{ |
| 3583 |
args->current_rec = line; |
| 3584 |
|
| 3585 |
int i; |
| 3586 |
for (i=0; i<args->nrm; i++) |
| 3587 |
args->rm[i].handler(args, line, &args->rm[i]); |
| 3588 |
|
| 3589 |
int has_overlap = 0; |
| 3590 |
if ( args->tgt_idx ) |
| 3591 |
has_overlap = annotate_from_regidx(args,line); |
| 3592 |
|
| 3593 |
else if ( args->tgts ) |
| 3594 |
has_overlap = annotate_from_tab(args,line); |
| 3595 |
|
| 3596 |
else if ( args->files->nreaders == 2 ) |
| 3597 |
has_overlap = annotate_from_vcf(args,line); |
| 3598 |
|
| 3599 |
else if ( args->ncols ) |
| 3600 |
has_overlap = annotate_from_self(args,line); |
| 3601 |
|
| 3602 |
if ( args->set_ids ) |
| 3603 |
{ |
| 3604 |
args->tmpks.l = 0; |
| 3605 |
convert_line(args->set_ids, line, &args->tmpks); |
| 3606 |
if ( args->tmpks.l ) |
| 3607 |
{ |
| 3608 |
int replace = 0; |
| 3609 |
if ( args->set_ids_replace ) replace = 1; |
| 3610 |
else if ( !line->d.id || (line->d.id[0]=='.' && !line->d.id[1]) ) replace = 1; |
| 3611 |
if ( replace ) |
| 3612 |
bcf_update_id(args->hdr_out,line,args->tmpks.s); |
| 3613 |
} |
| 3614 |
} |
| 3615 |
|
| 3616 |
if ( args->mark_sites ) |
| 3617 |
{ |
| 3618 |
if ( !args->targets_fname ) has_overlap = 1; |
| 3619 |
|
| 3620 |
|
| 3621 |
if ( args->mark_sites_logic==MARK_LISTED ) |
| 3622 |
bcf_update_info_flag(args->hdr_out,line,args->mark_sites,NULL,has_overlap?1:0); |
| 3623 |
else |
| 3624 |
bcf_update_info_flag(args->hdr_out,line,args->mark_sites,NULL,has_overlap?0:1); |
| 3625 |
} |
| 3626 |
|
| 3627 |
return has_overlap; |
| 3628 |
} |
| 3629 |
|
| 3630 |
static void usage(args_t *args) |
| 3631 |
{ |
| 3632 |
fprintf(stderr, "\n"); |
| 3633 |
fprintf(stderr, "About: Annotate and edit VCF/BCF files.\n"); |
| 3634 |
fprintf(stderr, "Usage: bcftools annotate [options] VCF\n"); |
| 3635 |
fprintf(stderr, "\n"); |
| 3636 |
fprintf(stderr, "Options:\n"); |
| 3637 |
fprintf(stderr, " -a, --annotations FILE VCF file or tabix-indexed FILE with annotations: CHR\\tPOS[\\tVALUE]+\n"); |
| 3638 |
fprintf(stderr, " -c, --columns LIST List of columns in the annotation file, e.g. CHROM,POS,REF,ALT,-,INFO/TAG. See man page for details\n"); |
| 3639 |
fprintf(stderr, " -C, --columns-file FILE Read -c columns from FILE, one name per row, with optional --merge-logic TYPE: NAME[ TYPE]\n"); |
| 3640 |
fprintf(stderr, " -e, --exclude EXPR Exclude sites for which the expression is true (see man page for details)\n"); |
| 3641 |
fprintf(stderr, " --force Continue despite parsing error (at your own risk!)\n"); |
| 3642 |
fprintf(stderr, " -H, --header-line STR Header line which should be appended to the VCF header, can be given multiple times\n"); |
| 3643 |
fprintf(stderr, " -h, --header-lines FILE Lines which should be appended to the VCF header\n"); |
| 3644 |
fprintf(stderr, " -I, --set-id [+]FORMAT Set ID column using a `bcftools query`-like expression, see man page for details\n"); |
| 3645 |
fprintf(stderr, " -i, --include EXPR Select sites for which the expression is true (see man page for details)\n"); |
| 3646 |
fprintf(stderr, " -k, --keep-sites Leave -i/-e sites unchanged instead of discarding them\n"); |
| 3647 |
fprintf(stderr, " -l, --merge-logic TAG:TYPE Merge logic for multiple overlapping regions (see man page for details), EXPERIMENTAL\n"); |
| 3648 |
fprintf(stderr, " -m, --mark-sites [+-]TAG Add INFO/TAG flag to sites which are (\"+\") or are not (\"-\") listed in the -a file\n"); |
| 3649 |
fprintf(stderr, " --min-overlap ANN:VCF Required overlap as a fraction of variant in the -a file (ANN), the VCF (:VCF), or reciprocal (ANN:VCF)\n"); |
| 3650 |
fprintf(stderr, " --no-version Do not append version and command line to the header\n"); |
| 3651 |
fprintf(stderr, " -o, --output FILE Write output to a file [standard output]\n"); |
| 3652 |
fprintf(stderr, " -O, --output-type u|b|v|z[0-9] u/b: un/compressed BCF, v/z: un/compressed VCF, 0-9: compression level [v]\n"); |
| 3653 |
fprintf(stderr, " --pair-logic STR Matching records by <snps|indels|both|all|some|exact>, see man page for details [some]\n"); |
| 3654 |
fprintf(stderr, " -r, --regions REGION Restrict to comma-separated list of regions\n"); |
| 3655 |
fprintf(stderr, " -R, --regions-file FILE Restrict to regions listed in FILE\n"); |
| 3656 |
fprintf(stderr, " --regions-overlap 0|1|2 Include if POS in the region (0), record overlaps (1), variant overlaps (2) [1]\n"); |
| 3657 |
fprintf(stderr, " --rename-annots FILE Rename annotations: TYPE/old\\tnew, where TYPE is one of FILTER,INFO,FORMAT\n"); |
| 3658 |
fprintf(stderr, " --rename-chrs FILE Rename sequences according to the mapping: old\\tnew\n"); |
| 3659 |
fprintf(stderr, " -s, --samples [^]LIST Comma separated list of samples to annotate (or exclude with \"^\" prefix)\n"); |
| 3660 |
fprintf(stderr, " -S, --samples-file [^]FILE File of samples to annotate (or exclude with \"^\" prefix)\n"); |
| 3661 |
fprintf(stderr, " --single-overlaps Keep memory low by avoiding complexities arising from handling multiple overlapping intervals\n"); |
| 3662 |
fprintf(stderr, " -x, --remove LIST List of annotations (e.g. ID,INFO/DP,FORMAT/DP,FILTER) to remove (or keep with \"^\" prefix). See man page for details\n"); |
| 3663 |
fprintf(stderr, " --threads INT Number of extra output compression threads [0]\n"); |
| 3664 |
fprintf(stderr, " -W, --write-index[=FMT] Automatically index the output files [off]\n"); |
| 3665 |
fprintf(stderr, "\n"); |
| 3666 |
fprintf(stderr, "Examples:\n"); |
| 3667 |
fprintf(stderr, " http://samtools.github.io/bcftools/howtos/annotate.html\n"); |
| 3668 |
fprintf(stderr, "\n"); |
| 3669 |
exit(1); |
| 3670 |
} |
| 3671 |
|
| 3672 |
int main_vcfannotate(int argc, char *argv[]) |
| 3673 |
{ |
| 3674 |
int c; |
| 3675 |
args_t *args = (args_t*) calloc(1,sizeof(args_t)); |
| 3676 |
args->argc = argc; args->argv = argv; |
| 3677 |
args->files = bcf_sr_init(); |
| 3678 |
args->output_fname = "-"; |
| 3679 |
args->output_type = FT_VCF; |
| 3680 |
args->n_threads = 0; |
| 3681 |
args->record_cmd_line = 1; |
| 3682 |
args->ref_idx = args->alt_idx = args->chr_idx = args->beg_idx = args->end_idx = -1; |
| 3683 |
args->set_ids_replace = 1; |
| 3684 |
args->match_id = -1; |
| 3685 |
args->match_end = -1; |
| 3686 |
args->clevel = -1; |
| 3687 |
args->pair_logic = -1; |
| 3688 |
int regions_is_file = 0; |
| 3689 |
int regions_overlap = 1; |
| 3690 |
|
| 3691 |
static struct option loptions[] = |
| 3692 |
{ |
| 3693 |
{"keep-sites",no_argument,NULL,'k'}, |
| 3694 |
{"mark-sites",required_argument,NULL,'m'}, |
| 3695 |
{"set-id",required_argument,NULL,'I'}, |
| 3696 |
{"output",required_argument,NULL,'o'}, |
| 3697 |
{"output-type",required_argument,NULL,'O'}, |
| 3698 |
{"threads",required_argument,NULL,9}, |
| 3699 |
{"annotations",required_argument,NULL,'a'}, |
| 3700 |
{"merge-logic",required_argument,NULL,'l'}, |
| 3701 |
{"collapse",required_argument,NULL,2}, |
| 3702 |
{"pair-logic",required_argument,NULL,2}, |
| 3703 |
{"include",required_argument,NULL,'i'}, |
| 3704 |
{"exclude",required_argument,NULL,'e'}, |
| 3705 |
{"regions",required_argument,NULL,'r'}, |
| 3706 |
{"regions-file",required_argument,NULL,'R'}, |
| 3707 |
{"regions-overlap",required_argument,NULL,3}, |
| 3708 |
{"remove",required_argument,NULL,'x'}, |
| 3709 |
{"columns-file",required_argument,NULL,'C'}, |
| 3710 |
{"columns",required_argument,NULL,'c'}, |
| 3711 |
{"rename-annots",required_argument,NULL,11}, |
| 3712 |
{"rename-chrs",required_argument,NULL,1}, |
| 3713 |
{"header-lines",required_argument,NULL,'h'}, |
| 3714 |
{"header-line",required_argument,NULL,'H'}, |
| 3715 |
{"samples",required_argument,NULL,'s'}, |
| 3716 |
{"samples-file",required_argument,NULL,'S'}, |
| 3717 |
{"single-overlaps",no_argument,NULL,10}, |
| 3718 |
{"min-overlap",required_argument,NULL,12}, |
| 3719 |
{"no-version",no_argument,NULL,8}, |
| 3720 |
{"force",no_argument,NULL,'f'}, |
| 3721 |
{"write-index",optional_argument,NULL,'W'}, |
| 3722 |
{NULL,0,NULL,0} |
| 3723 |
}; |
| 3724 |
char *tmp; |
| 3725 |
while ((c = getopt_long(argc, argv, "h:H:?o:O:r:R:a:x:c:C:i:e:S:s:I:m:kl:fW::",loptions,NULL)) >= 0) |
| 3726 |
{ |
| 3727 |
switch (c) { |
| 3728 |
case 'f': args->force = 1; break; |
| 3729 |
case 'k': args->keep_sites = 1; break; |
| 3730 |
case 'm': |
| 3731 |
args->mark_sites_logic = MARK_LISTED; |
| 3732 |
if ( optarg[0]=='+' ) args->mark_sites = optarg+1; |
| 3733 |
else if ( optarg[0]=='-' ) { args->mark_sites = optarg+1; args->mark_sites_logic = MARK_UNLISTED; } |
| 3734 |
else args->mark_sites = optarg; |
| 3735 |
break; |
| 3736 |
case 'l': |
| 3737 |
if ( args->merge_method_str.l ) kputc(',',&args->merge_method_str); |
| 3738 |
kputs(optarg,&args->merge_method_str); |
| 3739 |
break; |
| 3740 |
case 'I': args->set_ids_fmt = optarg; break; |
| 3741 |
case 's': args->sample_names = optarg; break; |
| 3742 |
case 'S': args->sample_names = optarg; args->sample_is_file = 1; break; |
| 3743 |
case 'c': args->columns = strdup(optarg); break; |
| 3744 |
case 'C': args->columns = strdup(optarg); args->columns_is_file = 1; break; |
| 3745 |
case 'o': args->output_fname = optarg; break; |
| 3746 |
case 'O': |
| 3747 |
switch (optarg[0]) { |
| 3748 |
case 'b': args->output_type = FT_BCF_GZ; break; |
| 3749 |
case 'u': args->output_type = FT_BCF; break; |
| 3750 |
case 'z': args->output_type = FT_VCF_GZ; break; |
| 3751 |
case 'v': args->output_type = FT_VCF; break; |
| 3752 |
default: |
| 3753 |
{ |
| 3754 |
args->clevel = strtol(optarg,&tmp,10); |
| 3755 |
if ( *tmp || args->clevel<0 || args->clevel>9 ) error("The output type \"%s\" not recognised\n", optarg); |
| 3756 |
} |
| 3757 |
}; |
| 3758 |
if ( optarg[1] ) |
| 3759 |
{ |
| 3760 |
args->clevel = strtol(optarg+1,&tmp,10); |
| 3761 |
if ( *tmp || args->clevel<0 || args->clevel>9 ) error("Could not parse argument: --compression-level %s\n", optarg+1); |
| 3762 |
} |
| 3763 |
break; |
| 3764 |
case 'e': |
| 3765 |
if ( args->filter_str ) error("Error: only one -i or -e expression can be given, and they cannot be combined\n"); |
| 3766 |
args->filter_str = optarg; args->filter_logic |= FLT_EXCLUDE; break; |
| 3767 |
case 'i': |
| 3768 |
if ( args->filter_str ) error("Error: only one -i or -e expression can be given, and they cannot be combined\n"); |
| 3769 |
args->filter_str = optarg; args->filter_logic |= FLT_INCLUDE; break; |
| 3770 |
case 'x': args->remove_annots = optarg; break; |
| 3771 |
case 'a': args->targets_fname = optarg; break; |
| 3772 |
case 'r': args->regions_list = optarg; break; |
| 3773 |
case 'R': args->regions_list = optarg; regions_is_file = 1; break; |
| 3774 |
case 'h': args->header_fname = optarg; break; |
| 3775 |
case 'H': args->header_lines = dbuf_push(args->header_lines,strdup(optarg)); break; |
| 3776 |
case 1 : args->rename_chrs = optarg; break; |
| 3777 |
case 2 : |
| 3778 |
if ( args->pair_logic==-1 ) args->pair_logic = 0; |
| 3779 |
if ( !strcmp(optarg,"snps") ) args->pair_logic |= BCF_SR_PAIR_SNP_REF; |
| 3780 |
else if ( !strcmp(optarg,"indels") ) args->pair_logic |= BCF_SR_PAIR_INDEL_REF; |
| 3781 |
else if ( !strcmp(optarg,"both") ) args->pair_logic |= BCF_SR_PAIR_BOTH_REF; |
| 3782 |
else if ( !strcmp(optarg,"any") ) args->pair_logic |= BCF_SR_PAIR_ANY; |
| 3783 |
else if ( !strcmp(optarg,"all") ) args->pair_logic |= BCF_SR_PAIR_ANY; |
| 3784 |
else if ( !strcmp(optarg,"some") ) args->pair_logic |= BCF_SR_PAIR_SOME; |
| 3785 |
else if ( !strcmp(optarg,"none") ) args->pair_logic = BCF_SR_PAIR_EXACT; |
| 3786 |
else if ( !strcmp(optarg,"exact") ) args->pair_logic = BCF_SR_PAIR_EXACT; |
| 3787 |
else error("The --pair-logic string \"%s\" not recognised.\n", optarg); |
| 3788 |
break; |
| 3789 |
case 3 : |
| 3790 |
regions_overlap = parse_overlap_option(optarg); |
| 3791 |
if ( regions_overlap < 0 ) error("Could not parse: --regions-overlap %s\n",optarg); |
| 3792 |
break; |
| 3793 |
case 9 : args->n_threads = strtol(optarg, 0, 0); break; |
| 3794 |
case 8 : args->record_cmd_line = 0; break; |
| 3795 |
case 10 : args->single_overlaps = 1; break; |
| 3796 |
case 11 : args->rename_annots = optarg; break; |
| 3797 |
case 12 : args->min_overlap_str = optarg; break; |
| 3798 |
case 'W': |
| 3799 |
if (!(args->write_index = write_index_parse(optarg))) |
| 3800 |
error("Unsupported index format '%s'\n", optarg); |
| 3801 |
break; |
| 3802 |
case '?': usage(args); break; |
| 3803 |
default: error("Unknown argument: %s\n", optarg); |
| 3804 |
} |
| 3805 |
} |
| 3806 |
|
| 3807 |
char *fname = NULL; |
| 3808 |
if ( optind>=argc ) |
| 3809 |
{ |
| 3810 |
if ( !isatty(fileno((FILE *)stdin)) ) fname = "-"; |
| 3811 |
else usage(args); |
| 3812 |
} |
| 3813 |
else fname = argv[optind]; |
| 3814 |
|
| 3815 |
if ( args->regions_list ) |
| 3816 |
{ |
| 3817 |
bcf_sr_set_opt(args->files,BCF_SR_REGIONS_OVERLAP,regions_overlap); |
| 3818 |
if ( bcf_sr_set_regions(args->files, args->regions_list, regions_is_file)<0 ) |
| 3819 |
error("Failed to read the regions: %s\n", args->regions_list); |
| 3820 |
} |
| 3821 |
if ( args->targets_fname ) |
| 3822 |
{ |
| 3823 |
htsFile *fp = hts_open(args->targets_fname,"r"); |
| 3824 |
if ( !fp ) error("Failed to open %s\n", args->targets_fname); |
| 3825 |
htsFormat type = *hts_get_format(fp); |
| 3826 |
hts_close(fp); |
| 3827 |
|
| 3828 |
if ( type.format==vcf || type.format==bcf ) |
| 3829 |
{ |
| 3830 |
args->tgts_is_vcf = 1; |
| 3831 |
args->files->require_index = 1; |
| 3832 |
bcf_sr_set_opt(args->files,BCF_SR_PAIR_LOGIC,args->pair_logic>=0 ? args->pair_logic : BCF_SR_PAIR_SOME); |
| 3833 |
if ( args->min_overlap_str ) error("The --min-overlap option cannot be used when annotating from a VCF\n"); |
| 3834 |
} |
| 3835 |
} |
| 3836 |
if ( args->min_overlap_str && args->single_overlaps ) error("The options --single-overlaps and --min-overlap cannot be combined\n"); |
| 3837 |
if ( bcf_sr_set_threads(args->files, args->n_threads)<0 ) error("Failed to create threads\n"); |
| 3838 |
if ( !bcf_sr_add_reader(args->files, fname) ) error("Failed to read from %s: %s\n", !strcmp("-",fname)?"standard input":fname,bcf_sr_strerror(args->files->errnum)); |
| 3839 |
|
| 3840 |
static int line_errcode_warned = 0; |
| 3841 |
init_data(args); |
| 3842 |
while ( bcf_sr_next_line(args->files) ) |
| 3843 |
{ |
| 3844 |
if ( !bcf_sr_has_line(args->files,0) ) continue; |
| 3845 |
bcf1_t *line = bcf_sr_get_line(args->files,0); |
| 3846 |
if ( line->errcode ) |
| 3847 |
{ |
| 3848 |
if ( !args->force ) |
| 3849 |
error("Encountered an error, cannot proceed. Please check the error output above.\n" |
| 3850 |
"If feeling adventurous, use the --force option. (At your own risk!)\n"); |
| 3851 |
else if ( !line_errcode_warned ) |
| 3852 |
{ |
| 3853 |
fprintf(stderr, |
| 3854 |
"Warning: Encountered an error, proceeding only because --force was given.\n" |
| 3855 |
" Note that this can result in a segfault or a silent corruption of the output file!\n"); |
| 3856 |
line_errcode_warned = 1; |
| 3857 |
line->errcode = 0; |
| 3858 |
} |
| 3859 |
} |
| 3860 |
if ( args->filter ) |
| 3861 |
{ |
| 3862 |
int pass = filter_test(args->filter, line, NULL); |
| 3863 |
if ( args->filter_logic & FLT_EXCLUDE ) pass = pass ? 0 : 1; |
| 3864 |
if ( !pass ) |
| 3865 |
{ |
| 3866 |
if ( args->keep_sites && bcf_write1(args->out_fh, args->hdr_out, line)!=0 ) error("[%s] Error: failed to write to %s\n", __func__,args->output_fname); |
| 3867 |
continue; |
| 3868 |
} |
| 3869 |
} |
| 3870 |
int keep = annotate_line(args, line); |
| 3871 |
if ( args->filter_ext && !args->keep_sites && !keep ) continue; |
| 3872 |
if ( bcf_write1(args->out_fh, args->hdr_out, line)!=0 ) error("[%s] Error: failed to write to %s\n", __func__,args->output_fname); |
| 3873 |
} |
| 3874 |
destroy_data(args); |
| 3875 |
bcf_sr_destroy(args->files); |
| 3876 |
free(args); |
| 3877 |
return 0; |
| 3878 |
} |