| 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 <unistd.h> |
| 27 |
#include <getopt.h> |
| 28 |
#include <string.h> |
| 29 |
#include <assert.h> |
| 30 |
#include <errno.h> |
| 31 |
#include <math.h> |
| 32 |
#include <stdint.h> |
| 33 |
#include <inttypes.h> |
| 34 |
#include <htslib/vcf.h> |
| 35 |
#include <htslib/synced_bcf_reader.h> |
| 36 |
#include <htslib/kseq.h> |
| 37 |
#include <htslib/bgzf.h> |
| 38 |
#include <htslib/tbx.h> |
| 39 |
#include <htslib/thread_pool.h> |
| 40 |
#include <htslib/hts_endian.h> |
| 41 |
#include <sys/time.h> |
| 42 |
#include "bcftools.h" |
| 43 |
|
| 44 |
typedef struct _args_t |
| 45 |
{ |
| 46 |
bcf_srs_t *files; |
| 47 |
htsFile *out_fh; |
| 48 |
int output_type, n_threads, record_cmd_line, clevel; |
| 49 |
bcf_hdr_t *out_hdr; |
| 50 |
int *seen_seq; |
| 51 |
char *index_fn; |
| 52 |
int write_index; |
| 53 |
|
| 54 |
|
| 55 |
int *start_pos, start_tid, ifname; |
| 56 |
int *swap_phase, nswap, *nmatch, *nmism; |
| 57 |
bcf1_t **buf; |
| 58 |
uint8_t *buf_mask; |
| 59 |
int nbuf, mbuf, prev_chr, min_PQ, prev_pos_check; |
| 60 |
int32_t *GTa, *GTb, mGTa, mGTb, *phase_qual, *phase_set; |
| 61 |
|
| 62 |
char **argv, *output_fname, *file_list, **fnames, *remove_dups, *regions_list; |
| 63 |
int argc, nfnames, allow_overlaps, phased_concat, regions_is_file, regions_overlap; |
| 64 |
int compact_PS, phase_set_changed, naive_concat, naive_concat_trust_headers; |
| 65 |
int verbose, explicit_output_type, ligate_force, ligate_warn; |
| 66 |
int sites_only; |
| 67 |
htsThreadPool *tpool; |
| 68 |
} |
| 69 |
args_t; |
| 70 |
|
| 71 |
static bcf_hdr_t *drop_hdr_genotypes(args_t *args, bcf_hdr_t *hdr) |
| 72 |
{ |
| 73 |
if ( !args->sites_only ) return hdr; |
| 74 |
bcf_hdr_t *rmme = hdr; |
| 75 |
hdr = bcf_hdr_subset(rmme, 0, 0, 0); |
| 76 |
bcf_hdr_remove(hdr, BCF_HL_FMT, NULL); |
| 77 |
bcf_hdr_destroy(rmme); |
| 78 |
return hdr; |
| 79 |
} |
| 80 |
|
| 81 |
static void init_data(args_t *args) |
| 82 |
{ |
| 83 |
bcf1_t *line = NULL; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
if ( args->phased_concat ) |
| 89 |
{ |
| 90 |
args->start_pos = (int*) malloc(sizeof(int)*args->nfnames); |
| 91 |
line = bcf_init(); |
| 92 |
} |
| 93 |
|
| 94 |
if ( args->verbose ) fprintf(stderr,"Checking the headers and starting positions of %d files\n", args->nfnames); |
| 95 |
kstring_t str = {0,0,0}; |
| 96 |
int i, prev_chrid = -1; |
| 97 |
for (i=0; i<args->nfnames; i++) |
| 98 |
{ |
| 99 |
htsFile *fp = hts_open(args->fnames[i], "r"); if ( !fp ) error("Failed to open: %s\n", args->fnames[i]); |
| 100 |
bcf_hdr_t *hdr = bcf_hdr_read(fp); if ( !hdr ) error("Failed to parse header: %s\n", args->fnames[i]); |
| 101 |
hdr = drop_hdr_genotypes(args, hdr); |
| 102 |
|
| 103 |
args->out_hdr = bcf_hdr_merge(args->out_hdr,hdr); |
| 104 |
if ( bcf_hdr_nsamples(hdr) != bcf_hdr_nsamples(args->out_hdr) ) |
| 105 |
error("Different number of samples in %s. Perhaps \"bcftools merge\" is what you are looking for?\n", args->fnames[i]); |
| 106 |
|
| 107 |
int j; |
| 108 |
for (j=0; j<bcf_hdr_nsamples(hdr); j++) |
| 109 |
if ( strcmp(args->out_hdr->samples[j],hdr->samples[j]) ) |
| 110 |
error("Different sample names in %s. Perhaps \"bcftools merge\" is what you are looking for?\n", args->fnames[i]); |
| 111 |
|
| 112 |
if ( args->phased_concat ) |
| 113 |
{ |
| 114 |
int ret = bcf_read(fp, hdr, line); |
| 115 |
if ( ret!=0 ) args->start_pos[i] = -2; |
| 116 |
else |
| 117 |
{ |
| 118 |
int chrid = bcf_hdr_id2int(args->out_hdr,BCF_DT_CTG,bcf_seqname(hdr,line)); |
| 119 |
args->start_pos[i] = chrid==prev_chrid ? line->pos : -1; |
| 120 |
prev_chrid = chrid; |
| 121 |
} |
| 122 |
} |
| 123 |
bcf_hdr_destroy(hdr); |
| 124 |
if ( hts_close(fp)!=0 ) error("[%s] Error: close failed .. %s\n", __func__,args->fnames[i]); |
| 125 |
} |
| 126 |
free(str.s); |
| 127 |
if ( line ) bcf_destroy(line); |
| 128 |
|
| 129 |
args->seen_seq = (int*) calloc(args->out_hdr->n[BCF_DT_CTG],sizeof(int)); |
| 130 |
|
| 131 |
if ( args->phased_concat ) |
| 132 |
{ |
| 133 |
bcf_hdr_append(args->out_hdr,"##FORMAT=<ID=PQ,Number=1,Type=Integer,Description=\"Phasing Quality (bigger is better)\">"); |
| 134 |
bcf_hdr_append(args->out_hdr,"##FORMAT=<ID=PS,Number=1,Type=Integer,Description=\"Phase Set\">"); |
| 135 |
} |
| 136 |
if (args->record_cmd_line) bcf_hdr_append_version(args->out_hdr, args->argc, args->argv, "bcftools_concat"); |
| 137 |
char wmode[8]; |
| 138 |
set_wmode(wmode,args->output_type,args->output_fname,args->clevel); |
| 139 |
args->out_fh = hts_open(args->output_fname ? args->output_fname : "-", wmode); |
| 140 |
if ( args->out_fh == NULL ) error("Can't write to \"%s\": %s\n", args->output_fname, strerror(errno)); |
| 141 |
if ( args->allow_overlaps || args->phased_concat ) |
| 142 |
{ |
| 143 |
args->files = bcf_sr_init(); |
| 144 |
args->files->require_index = 1; |
| 145 |
} |
| 146 |
if ( args->n_threads ) |
| 147 |
{ |
| 148 |
if ( args->files ) |
| 149 |
{ |
| 150 |
if ( bcf_sr_set_threads(args->files, args->n_threads)<0 ) error("Failed to create threads\n"); |
| 151 |
args->tpool = args->files->p; |
| 152 |
} |
| 153 |
else |
| 154 |
{ |
| 155 |
args->tpool = (htsThreadPool*) calloc(1, sizeof(htsThreadPool)); |
| 156 |
if ( !args->tpool ) error("Failed to allocate memory\n"); |
| 157 |
if ( !(args->tpool->pool = hts_tpool_init(args->n_threads)) ) error("Failed to initialize %d threads\n",args->n_threads); |
| 158 |
} |
| 159 |
hts_set_opt(args->out_fh, HTS_OPT_THREAD_POOL, args->tpool); |
| 160 |
} |
| 161 |
if ( bcf_hdr_write(args->out_fh, args->out_hdr)!=0 ) error("[%s] Error: cannot write the header to %s\n", __func__,args->output_fname); |
| 162 |
if ( init_index2(args->out_fh,args->out_hdr,args->output_fname, |
| 163 |
&args->index_fn, args->write_index)<0 ) |
| 164 |
error("Error: failed to initialise index for %s\n",args->output_fname); |
| 165 |
|
| 166 |
if ( args->allow_overlaps ) |
| 167 |
{ |
| 168 |
if ( args->regions_list ) |
| 169 |
{ |
| 170 |
bcf_sr_set_opt(args->files,BCF_SR_REGIONS_OVERLAP,args->regions_overlap); |
| 171 |
if ( bcf_sr_set_regions(args->files, args->regions_list, args->regions_is_file)<0 ) |
| 172 |
error("Failed to read the regions: %s\n", args->regions_list); |
| 173 |
} |
| 174 |
if ( args->remove_dups ) |
| 175 |
{ |
| 176 |
if ( !strcmp(args->remove_dups,"snps") ) args->files->collapse |= COLLAPSE_SNPS; |
| 177 |
else if ( !strcmp(args->remove_dups,"indels") ) args->files->collapse |= COLLAPSE_INDELS; |
| 178 |
else if ( !strcmp(args->remove_dups,"both") ) args->files->collapse |= COLLAPSE_SNPS | COLLAPSE_INDELS; |
| 179 |
else if ( !strcmp(args->remove_dups,"any") ) args->files->collapse |= COLLAPSE_ANY; |
| 180 |
else if ( !strcmp(args->remove_dups,"all") ) args->files->collapse |= COLLAPSE_ANY; |
| 181 |
else if ( !strcmp(args->remove_dups,"none") ) args->files->collapse = COLLAPSE_NONE; |
| 182 |
else if ( !strcmp(args->remove_dups,"exact") ) args->files->collapse = COLLAPSE_NONE; |
| 183 |
else error("The -D string \"%s\" not recognised.\n", args->remove_dups); |
| 184 |
} |
| 185 |
for (i=0; i<args->nfnames; i++) |
| 186 |
if ( !bcf_sr_add_reader(args->files,args->fnames[i]) ) error("Failed to open %s: %s\n", args->fnames[i],bcf_sr_strerror(args->files->errnum)); |
| 187 |
} |
| 188 |
else if ( args->phased_concat ) |
| 189 |
{ |
| 190 |
|
| 191 |
int nok = 0; |
| 192 |
while (1) |
| 193 |
{ |
| 194 |
while ( nok<args->nfnames && args->start_pos[nok]!=-2 ) nok++; |
| 195 |
if ( nok==args->nfnames ) break; |
| 196 |
|
| 197 |
i = nok; |
| 198 |
while ( i<args->nfnames && args->start_pos[i]==-2 ) i++; |
| 199 |
if ( i==args->nfnames ) break; |
| 200 |
|
| 201 |
int tmp = args->start_pos[nok]; args->start_pos[nok] = args->start_pos[i]; args->start_pos[i] = tmp; |
| 202 |
char *str = args->fnames[nok]; args->fnames[nok] = args->fnames[i]; args->fnames[i] = str; |
| 203 |
} |
| 204 |
for (i=nok; i<args->nfnames; i++) free(args->fnames[i]); |
| 205 |
args->nfnames = nok; |
| 206 |
|
| 207 |
for (i=1; i<args->nfnames; i++) |
| 208 |
if ( args->start_pos[i-1]!=-1 && args->start_pos[i]!=-1 && args->start_pos[i]<args->start_pos[i-1] ) |
| 209 |
error("The files not in ascending order: %d in %s, %d in %s\n", args->start_pos[i-1]+1,args->fnames[i-1],args->start_pos[i]+1,args->fnames[i]); |
| 210 |
|
| 211 |
args->prev_chr = -1; |
| 212 |
args->swap_phase = (int*) calloc(bcf_hdr_nsamples(args->out_hdr),sizeof(int)); |
| 213 |
args->nmatch = (int*) calloc(bcf_hdr_nsamples(args->out_hdr),sizeof(int)); |
| 214 |
args->nmism = (int*) calloc(bcf_hdr_nsamples(args->out_hdr),sizeof(int)); |
| 215 |
args->phase_qual = (int32_t*) malloc(bcf_hdr_nsamples(args->out_hdr)*sizeof(int32_t)); |
| 216 |
args->phase_set = (int32_t*) malloc(bcf_hdr_nsamples(args->out_hdr)*sizeof(int32_t)); |
| 217 |
args->ifname = 0; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
static void destroy_data(args_t *args) |
| 222 |
{ |
| 223 |
int i; |
| 224 |
if ( args->out_fh ) |
| 225 |
{ |
| 226 |
if ( args->write_index ) |
| 227 |
{ |
| 228 |
if ( bcf_idx_save(args->out_fh)<0 ) |
| 229 |
{ |
| 230 |
if ( hts_close(args->out_fh)!=0 ) error("Error: close failed .. %s\n", args->output_fname?args->output_fname:"stdout"); |
| 231 |
error("Error: cannot write to index %s\n", args->index_fn); |
| 232 |
} |
| 233 |
free(args->index_fn); |
| 234 |
} |
| 235 |
if ( hts_close(args->out_fh)!=0 ) error("Error: close failed .. %s\n",args->output_fname?args->output_fname:"stdout"); |
| 236 |
} |
| 237 |
if ( args->tpool && !args->files ) |
| 238 |
{ |
| 239 |
hts_tpool_destroy(args->tpool->pool); |
| 240 |
free(args->tpool); |
| 241 |
} |
| 242 |
if ( args->files ) bcf_sr_destroy(args->files); |
| 243 |
if ( args->out_hdr ) bcf_hdr_destroy(args->out_hdr); |
| 244 |
free(args->seen_seq); |
| 245 |
free(args->start_pos); |
| 246 |
free(args->swap_phase); |
| 247 |
for (i=0; i<args->mbuf; i++) bcf_destroy(args->buf[i]); |
| 248 |
free(args->buf); |
| 249 |
free(args->buf_mask); |
| 250 |
free(args->GTa); |
| 251 |
free(args->GTb); |
| 252 |
free(args->nmatch); |
| 253 |
free(args->nmism); |
| 254 |
free(args->phase_qual); |
| 255 |
free(args->phase_set); |
| 256 |
for (i=0; i<args->nfnames; i++) free(args->fnames[i]); |
| 257 |
free(args->fnames); |
| 258 |
} |
| 259 |
|
| 260 |
int vcf_write_line(htsFile *fp, kstring_t *line); |
| 261 |
|
| 262 |
#define SWAP(type_t, a, b) { type_t t = a; a = b; b = t; } |
| 263 |
static void phase_update(args_t *args, bcf_hdr_t *hdr, bcf1_t *rec) |
| 264 |
{ |
| 265 |
int i, nGTs = bcf_get_genotypes(hdr, rec, &args->GTa, &args->mGTa); |
| 266 |
if ( nGTs <= 0 ) return; |
| 267 |
for (i=0; i<bcf_hdr_nsamples(hdr); i++) |
| 268 |
{ |
| 269 |
if ( !args->swap_phase[i] ) continue; |
| 270 |
int *gt = &args->GTa[i*2]; |
| 271 |
if ( bcf_gt_is_missing(gt[0]) || gt[1]==bcf_int32_vector_end ) continue; |
| 272 |
if ( !bcf_gt_is_phased(gt[1]) ) continue; |
| 273 |
SWAP(int, gt[0], gt[1]); |
| 274 |
gt[1] |= 1; |
| 275 |
} |
| 276 |
bcf_update_genotypes(hdr,rec,args->GTa,nGTs); |
| 277 |
} |
| 278 |
|
| 279 |
static void phased_flush(args_t *args) |
| 280 |
{ |
| 281 |
if ( !args->nbuf ) return; |
| 282 |
|
| 283 |
bcf_hdr_t *ahdr = args->files->readers[0].header; |
| 284 |
bcf_hdr_t *bhdr = args->files->readers[1].header; |
| 285 |
|
| 286 |
int i, j, nsmpl = bcf_hdr_nsamples(args->out_hdr); |
| 287 |
static int gt_absent_warned = 0; |
| 288 |
for (i=0; i<args->nbuf; i+=2) |
| 289 |
{ |
| 290 |
if ( args->buf_mask[i/2]!=3 ) continue; |
| 291 |
|
| 292 |
bcf1_t *arec = args->buf[i]; |
| 293 |
bcf1_t *brec = args->buf[i+1]; |
| 294 |
|
| 295 |
int nGTs = bcf_get_genotypes(ahdr, arec, &args->GTa, &args->mGTa); |
| 296 |
if ( nGTs < 0 ) |
| 297 |
{ |
| 298 |
if ( !gt_absent_warned ) |
| 299 |
{ |
| 300 |
fprintf(stderr,"GT is not present at %s:%"PRId64". (This warning is printed only once.)\n", bcf_seqname(ahdr,arec), (int64_t) arec->pos+1); |
| 301 |
gt_absent_warned = 1; |
| 302 |
} |
| 303 |
continue; |
| 304 |
} |
| 305 |
if ( nGTs != 2*nsmpl ) continue; |
| 306 |
nGTs = bcf_get_genotypes(bhdr, brec, &args->GTb, &args->mGTb); |
| 307 |
if ( nGTs < 0 ) |
| 308 |
{ |
| 309 |
if ( !gt_absent_warned ) |
| 310 |
{ |
| 311 |
fprintf(stderr,"GT is not present at %s:%"PRId64". (This warning is printed only once.)\n", bcf_seqname(bhdr,brec), (int64_t) brec->pos+1); |
| 312 |
gt_absent_warned = 1; |
| 313 |
} |
| 314 |
continue; |
| 315 |
} |
| 316 |
if ( nGTs != 2*nsmpl ) continue; |
| 317 |
|
| 318 |
for (j=0; j<nsmpl; j++) |
| 319 |
{ |
| 320 |
int *gta = &args->GTa[j*2]; |
| 321 |
int *gtb = &args->GTb[j*2]; |
| 322 |
if ( gta[1]==bcf_int32_vector_end || gtb[1]==bcf_int32_vector_end ) continue; |
| 323 |
if ( bcf_gt_is_missing(gta[0]) || bcf_gt_is_missing(gta[1]) || bcf_gt_is_missing(gtb[0]) || bcf_gt_is_missing(gtb[1]) ) continue; |
| 324 |
if ( !bcf_gt_is_phased(gta[1]) || !bcf_gt_is_phased(gtb[1]) ) continue; |
| 325 |
if ( bcf_gt_allele(gta[0])==bcf_gt_allele(gta[1]) || bcf_gt_allele(gtb[0])==bcf_gt_allele(gtb[1]) ) continue; |
| 326 |
if ( bcf_gt_allele(gta[0])==bcf_gt_allele(gtb[0]) && bcf_gt_allele(gta[1])==bcf_gt_allele(gtb[1]) ) |
| 327 |
{ |
| 328 |
if ( args->swap_phase[j] ) args->nmism[j]++; else args->nmatch[j]++; |
| 329 |
} |
| 330 |
if ( bcf_gt_allele(gta[0])==bcf_gt_allele(gtb[1]) && bcf_gt_allele(gta[1])==bcf_gt_allele(gtb[0]) ) |
| 331 |
{ |
| 332 |
if ( args->swap_phase[j] ) args->nmatch[j]++; else args->nmism[j]++; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
for (i=0; i<args->nbuf/2; i+=2) |
| 337 |
{ |
| 338 |
bcf1_t *rec; |
| 339 |
bcf_hdr_t *hdr; |
| 340 |
int mask = args->buf_mask[i/2]; |
| 341 |
if ( mask & 1 ) { rec = args->buf[i]; hdr = args->files->readers[0].header; } |
| 342 |
else { rec = args->buf[i+1]; hdr = args->files->readers[1].header; } |
| 343 |
bcf_translate(args->out_hdr, hdr, rec); |
| 344 |
if ( args->nswap && (mask&1) ) |
| 345 |
phase_update(args, args->out_hdr, rec); |
| 346 |
if ( !args->compact_PS || args->phase_set_changed ) |
| 347 |
{ |
| 348 |
bcf_update_format_int32(args->out_hdr,rec,"PS",args->phase_set,nsmpl); |
| 349 |
args->phase_set_changed = 0; |
| 350 |
} |
| 351 |
if ( bcf_write(args->out_fh, args->out_hdr, rec)!=0 ) error("[%s] Error: cannot write to %s\n", __func__,args->output_fname); |
| 352 |
|
| 353 |
if ( rec->pos < args->prev_pos_check ) error("FIXME, disorder: %s:%"PRId64" vs %d [1]\n", bcf_seqname(hdr,rec),(int64_t)rec->pos+1,args->prev_pos_check+1); |
| 354 |
args->prev_pos_check = rec->pos; |
| 355 |
} |
| 356 |
args->nswap = 0; |
| 357 |
for (j=0; j<nsmpl; j++) |
| 358 |
{ |
| 359 |
if ( args->nmatch[j] >= args->nmism[j] ) |
| 360 |
args->swap_phase[j] = 0; |
| 361 |
else |
| 362 |
{ |
| 363 |
args->swap_phase[j] = 1; |
| 364 |
args->nswap++; |
| 365 |
} |
| 366 |
if ( args->nmatch[j] && args->nmism[j] ) |
| 367 |
{ |
| 368 |
|
| 369 |
double f = (double)args->nmatch[j]/(args->nmatch[j]+args->nmism[j]); |
| 370 |
args->phase_qual[j] = 99*(0.7 + f*log(f) + (1-f)*log(1-f))/0.7; |
| 371 |
} |
| 372 |
else |
| 373 |
args->phase_qual[j] = 99; |
| 374 |
args->nmatch[j] = 0; |
| 375 |
args->nmism[j] = 0; |
| 376 |
} |
| 377 |
int PQ_printed = 0; |
| 378 |
for (; i<args->nbuf; i+=2) |
| 379 |
{ |
| 380 |
bcf1_t *rec; |
| 381 |
bcf_hdr_t *hdr; |
| 382 |
int mask = args->buf_mask[i/2]; |
| 383 |
if ( mask & 2 ) { rec = args->buf[i+1]; hdr = args->files->readers[1].header; } |
| 384 |
else { rec = args->buf[i]; hdr = args->files->readers[0].header; } |
| 385 |
bcf_translate(args->out_hdr, hdr, rec); |
| 386 |
if ( !PQ_printed && mask==3 ) |
| 387 |
{ |
| 388 |
bcf_update_format_int32(args->out_hdr,rec,"PQ",args->phase_qual,nsmpl); |
| 389 |
PQ_printed = 1; |
| 390 |
for (j=0; j<nsmpl; j++) |
| 391 |
if ( args->phase_qual[j] < args->min_PQ ) |
| 392 |
{ |
| 393 |
args->phase_set[j] = rec->pos+1; |
| 394 |
args->phase_set_changed = 1; |
| 395 |
} |
| 396 |
else if ( args->compact_PS ) args->phase_set[j] = bcf_int32_missing; |
| 397 |
} |
| 398 |
if ( args->nswap ) |
| 399 |
phase_update(args, args->out_hdr, rec); |
| 400 |
if ( !args->compact_PS || args->phase_set_changed ) |
| 401 |
{ |
| 402 |
bcf_update_format_int32(args->out_hdr,rec,"PS",args->phase_set,nsmpl); |
| 403 |
args->phase_set_changed = 0; |
| 404 |
} |
| 405 |
if ( bcf_write(args->out_fh, args->out_hdr, rec)!=0 ) error("[%s] Error: cannot write to %s\n", __func__,args->output_fname); |
| 406 |
|
| 407 |
if ( rec->pos < args->prev_pos_check ) error("FIXME, disorder: %s:%"PRId64" vs %d [2]\n", bcf_seqname(hdr,rec),(int64_t)rec->pos+1,args->prev_pos_check+1); |
| 408 |
args->prev_pos_check = rec->pos; |
| 409 |
} |
| 410 |
args->nbuf = 0; |
| 411 |
} |
| 412 |
|
| 413 |
static void phased_push(args_t *args, bcf1_t *arec, bcf1_t *brec, int is_overlap) |
| 414 |
{ |
| 415 |
bcf_hdr_t *ahdr = arec ? bcf_sr_get_header(args->files,0) : NULL; |
| 416 |
bcf_hdr_t *bhdr = brec ? bcf_sr_get_header(args->files,1) : NULL; |
| 417 |
|
| 418 |
if ( arec && arec->errcode ) |
| 419 |
error("Parse error at %s:%"PRId64", cannot proceed: %s\n", bcf_seqname(ahdr,arec),(int64_t) arec->pos+1, args->files->readers[0].fname); |
| 420 |
if ( brec && brec->errcode ) |
| 421 |
error("Parse error at %s:%"PRId64", cannot proceed: %s\n", bcf_seqname(bhdr,brec),(int64_t) brec->pos+1, args->files->readers[1].fname); |
| 422 |
|
| 423 |
int i, nsmpl = bcf_hdr_nsamples(args->out_hdr); |
| 424 |
int chr_id = arec ? bcf_hdr_name2id(args->out_hdr,bcf_seqname(ahdr,arec)) : bcf_hdr_name2id(args->out_hdr,bcf_seqname(bhdr,brec)); |
| 425 |
if ( args->prev_chr<0 || args->prev_chr!=chr_id ) |
| 426 |
{ |
| 427 |
if ( args->prev_chr>=0 ) phased_flush(args); |
| 428 |
|
| 429 |
for (i=0; i<nsmpl; i++) |
| 430 |
args->phase_set[i] = arec ? arec->pos+1 : brec->pos+1; |
| 431 |
args->phase_set_changed = 1; |
| 432 |
|
| 433 |
if ( args->seen_seq[chr_id] ) error("The chromosome block %s is not contiguous\n", arec ? bcf_seqname(ahdr,arec) : bcf_seqname(bhdr,brec)); |
| 434 |
args->seen_seq[chr_id] = 1; |
| 435 |
args->prev_chr = chr_id; |
| 436 |
args->prev_pos_check = -1; |
| 437 |
} |
| 438 |
|
| 439 |
if ( !is_overlap ) |
| 440 |
{ |
| 441 |
assert(arec); |
| 442 |
|
| 443 |
bcf_translate(args->out_hdr, ahdr, arec); |
| 444 |
if ( args->nswap ) |
| 445 |
phase_update(args, args->out_hdr, arec); |
| 446 |
if ( !args->compact_PS || args->phase_set_changed ) |
| 447 |
{ |
| 448 |
bcf_update_format_int32(args->out_hdr,arec,"PS",args->phase_set,nsmpl); |
| 449 |
args->phase_set_changed = 0; |
| 450 |
} |
| 451 |
if ( bcf_write(args->out_fh, args->out_hdr, arec)!=0 ) error("[%s] Error: cannot write to %s\n", __func__,args->output_fname); |
| 452 |
|
| 453 |
if ( arec->pos < args->prev_pos_check ) |
| 454 |
error("FIXME, disorder: %s:%"PRId64" in %s vs %d written [3]\n", bcf_seqname(ahdr,arec), (int64_t) arec->pos+1,args->files->readers[0].fname, args->prev_pos_check+1); |
| 455 |
args->prev_pos_check = arec->pos; |
| 456 |
return; |
| 457 |
} |
| 458 |
|
| 459 |
int m = args->mbuf; |
| 460 |
args->nbuf += 2; |
| 461 |
hts_expand(bcf1_t*,args->nbuf,args->mbuf,args->buf); |
| 462 |
if ( m < args->mbuf ) args->buf_mask = (uint8_t*)realloc(args->buf_mask,sizeof(*args->buf_mask)*args->mbuf); |
| 463 |
for (i=m; i<args->mbuf; i++) |
| 464 |
args->buf[i] = bcf_init1(); |
| 465 |
|
| 466 |
if ( arec ) SWAP(bcf1_t*, args->files->readers[0].buffer[0], args->buf[args->nbuf-2]); |
| 467 |
if ( brec ) SWAP(bcf1_t*, args->files->readers[1].buffer[0], args->buf[args->nbuf-1]); |
| 468 |
args->buf_mask[args->nbuf/2-1] = (arec?1:0) | (brec?2:0); |
| 469 |
} |
| 470 |
|
| 471 |
static int _get_active_index(bcf_srs_t *sr) |
| 472 |
{ |
| 473 |
int i; |
| 474 |
for (i=0; i<sr->nreaders; i++) |
| 475 |
if ( bcf_sr_has_line(sr,i) ) return i; |
| 476 |
return -1; |
| 477 |
} |
| 478 |
|
| 479 |
static void concat(args_t *args) |
| 480 |
{ |
| 481 |
static int site_drop_warned = 0; |
| 482 |
int i; |
| 483 |
if ( args->phased_concat ) |
| 484 |
{ |
| 485 |
|
| 486 |
while ( args->ifname < args->nfnames ) |
| 487 |
{ |
| 488 |
int new_file = 0; |
| 489 |
while ( args->files->nreaders < 2 && args->ifname < args->nfnames ) |
| 490 |
{ |
| 491 |
if ( !bcf_sr_add_reader(args->files,args->fnames[args->ifname]) ) error("Failed to open %s: %s\n", args->fnames[args->ifname],bcf_sr_strerror(args->files->errnum)); |
| 492 |
new_file = 1; |
| 493 |
|
| 494 |
args->ifname++; |
| 495 |
if ( args->start_pos[args->ifname-1]==-1 ) break; |
| 496 |
if ( args->ifname < args->nfnames && args->start_pos[args->ifname]==-1 ) break; |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
int seek_pos = -1; |
| 501 |
int seek_chr = -1; |
| 502 |
if ( bcf_sr_has_line(args->files,0) ) |
| 503 |
{ |
| 504 |
bcf1_t *line = bcf_sr_get_line(args->files,0); |
| 505 |
bcf_sr_seek(args->files, bcf_seqname(args->files->readers[0].header,line), line->pos); |
| 506 |
seek_pos = line->pos; |
| 507 |
seek_chr = bcf_hdr_name2id(args->out_hdr, bcf_seqname(args->files->readers[0].header,line)); |
| 508 |
} |
| 509 |
else if ( new_file ) |
| 510 |
bcf_sr_seek(args->files,NULL,0); |
| 511 |
|
| 512 |
int nret, ir; |
| 513 |
while ( (nret = bcf_sr_next_line(args->files)) ) |
| 514 |
{ |
| 515 |
int is_overlap = args->files->nreaders==1 ? 0 : 1; |
| 516 |
if ( !bcf_sr_has_line(args->files,0) ) |
| 517 |
{ |
| 518 |
|
| 519 |
if ( bcf_sr_region_done(args->files,0) ) |
| 520 |
{ |
| 521 |
phased_flush(args); |
| 522 |
bcf_sr_remove_reader(args->files, 0); |
| 523 |
is_overlap = 0; |
| 524 |
} |
| 525 |
else if ( args->ligate_warn ) |
| 526 |
{ |
| 527 |
if ( !site_drop_warned ) |
| 528 |
{ |
| 529 |
ir = _get_active_index(args->files); |
| 530 |
fprintf(stderr, |
| 531 |
"Warning: Dropping the site %s:%"PRId64". The --ligate option is intended for VCFs with perfect\n" |
| 532 |
" overlap, sites in overlapping regions present in one but missing in other are dropped.\n" |
| 533 |
" This warning is printed only once.\n", |
| 534 |
bcf_seqname(bcf_sr_get_header(args->files,ir),bcf_sr_get_line(args->files,ir)), (int64_t) bcf_sr_get_line(args->files,ir)->pos+1); |
| 535 |
site_drop_warned = 1; |
| 536 |
} |
| 537 |
continue; |
| 538 |
} |
| 539 |
else if ( !args->ligate_force ) |
| 540 |
{ |
| 541 |
ir = _get_active_index(args->files); |
| 542 |
error("Error: The --ligate option is intended for VCFs with perfect overlap, the site %s:%"PRId64" breaks the assumption\n", |
| 543 |
bcf_seqname(bcf_sr_get_header(args->files,ir),bcf_sr_get_line(args->files,ir)), (int64_t) bcf_sr_get_line(args->files,ir)->pos+1); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
ir = _get_active_index(args->files); |
| 549 |
bcf1_t *line = bcf_sr_get_line(args->files,ir); |
| 550 |
|
| 551 |
|
| 552 |
if ( seek_chr>=0 && seek_pos>line->pos && seek_chr==bcf_hdr_name2id(args->out_hdr, bcf_seqname(args->files->readers[ir].header,line)) ) continue; |
| 553 |
seek_pos = seek_chr = -1; |
| 554 |
|
| 555 |
|
| 556 |
int must_seek = 0; |
| 557 |
while ( args->ifname < args->nfnames && args->start_pos[args->ifname]!=-1 && line->pos >= args->start_pos[args->ifname] ) |
| 558 |
{ |
| 559 |
must_seek = 1; |
| 560 |
if ( !bcf_sr_add_reader(args->files,args->fnames[args->ifname]) ) error("Failed to open %s: %s\n", args->fnames[args->ifname],bcf_sr_strerror(args->files->errnum)); |
| 561 |
args->ifname++; |
| 562 |
} |
| 563 |
if ( must_seek ) |
| 564 |
{ |
| 565 |
bcf_sr_seek(args->files, bcf_seqname(args->files->readers[ir].header,line), line->pos); |
| 566 |
seek_pos = line->pos; |
| 567 |
seek_chr = bcf_hdr_name2id(args->out_hdr, bcf_seqname(args->files->readers[ir].header,line)); |
| 568 |
continue; |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
if ( args->files->nreaders>1 && !bcf_sr_has_line(args->files,1) && !bcf_sr_region_done(args->files,1) && !args->ligate_force ) |
| 573 |
{ |
| 574 |
if ( args->ligate_warn && !site_drop_warned ) |
| 575 |
{ |
| 576 |
ir = _get_active_index(args->files); |
| 577 |
fprintf(stderr, |
| 578 |
"Warning: Dropping the site %s:%"PRId64". The --ligate option is intended for VCFs with perfect\n" |
| 579 |
" overlap, sites in overlapping regions present in one but missing in other are dropped.\n" |
| 580 |
" This warning is printed only once.\n", |
| 581 |
bcf_seqname(bcf_sr_get_header(args->files,ir),line), (int64_t) line->pos+1); |
| 582 |
site_drop_warned = 1; |
| 583 |
} |
| 584 |
else if ( !args->ligate_warn ) |
| 585 |
{ |
| 586 |
ir = _get_active_index(args->files); |
| 587 |
error("Error: The --ligate option is intended for VCFs with perfect overlap, the site %s:%"PRId64" breaks the assumption\n", |
| 588 |
bcf_seqname(bcf_sr_get_header(args->files,ir),bcf_sr_get_line(args->files,ir)), (int64_t) bcf_sr_get_line(args->files,ir)->pos+1); |
| 589 |
} |
| 590 |
continue; |
| 591 |
} |
| 592 |
|
| 593 |
bcf1_t *line0 = bcf_sr_get_line(args->files,0); |
| 594 |
bcf1_t *line1 = args->files->nreaders > 1 ? bcf_sr_get_line(args->files,1) : NULL; |
| 595 |
phased_push(args, line0, line1, is_overlap); |
| 596 |
} |
| 597 |
|
| 598 |
if ( args->files->nreaders ) |
| 599 |
{ |
| 600 |
phased_flush(args); |
| 601 |
while ( args->files->nreaders ) |
| 602 |
bcf_sr_remove_reader(args->files, 0); |
| 603 |
} |
| 604 |
} |
| 605 |
} |
| 606 |
else if ( args->files ) |
| 607 |
{ |
| 608 |
while ( bcf_sr_next_line(args->files) ) |
| 609 |
{ |
| 610 |
for (i=0; i<args->files->nreaders; i++) |
| 611 |
{ |
| 612 |
bcf1_t *line = bcf_sr_get_line(args->files,i); |
| 613 |
if ( !line ) continue; |
| 614 |
if ( args->sites_only ) bcf_subset(args->out_hdr, line, 0, 0); |
| 615 |
bcf_translate(args->out_hdr, args->files->readers[i].header, line); |
| 616 |
if ( bcf_write1(args->out_fh, args->out_hdr, line)!=0 ) error("[%s] Error: cannot write to %s\n", __func__,args->output_fname); |
| 617 |
if ( args->remove_dups ) break; |
| 618 |
} |
| 619 |
} |
| 620 |
} |
| 621 |
else |
| 622 |
{ |
| 623 |
struct timeval t0, t1; |
| 624 |
kstring_t tmp = {0,0,0}; |
| 625 |
int prev_chr_id = -1, prev_pos; |
| 626 |
bcf1_t *line = bcf_init(); |
| 627 |
for (i=0; i<args->nfnames; i++) |
| 628 |
{ |
| 629 |
if ( args->verbose ) |
| 630 |
{ |
| 631 |
fprintf(stderr,"Concatenating %s", args->fnames[i]); |
| 632 |
gettimeofday(&t0, NULL); |
| 633 |
} |
| 634 |
htsFile *fp = hts_open(args->fnames[i], "r"); if ( !fp ) error("\nFailed to open: %s\n", args->fnames[i]); |
| 635 |
if ( args->n_threads ) hts_set_opt(fp, HTS_OPT_THREAD_POOL, args->tpool); |
| 636 |
bcf_hdr_t *hdr = bcf_hdr_read(fp); if ( !hdr ) error("\nFailed to parse header: %s\n", args->fnames[i]); |
| 637 |
if ( args->sites_only ) |
| 638 |
{ |
| 639 |
bcf_hdr_t *hdr_ori = hdr; |
| 640 |
hdr = bcf_hdr_subset(hdr_ori, 0, 0, 0); |
| 641 |
bcf_hdr_remove(hdr, BCF_HL_FMT, NULL); |
| 642 |
bcf_hdr_destroy(hdr_ori); |
| 643 |
} |
| 644 |
if ( !fp->is_bin && args->output_type&FT_VCF ) |
| 645 |
{ |
| 646 |
line->max_unpack = BCF_UN_STR; |
| 647 |
|
| 648 |
while ( hts_getline(fp, KS_SEP_LINE, &fp->line) >=0 ) |
| 649 |
{ |
| 650 |
char *str = fp->line.s; |
| 651 |
|
| 652 |
|
| 653 |
if ( args->sites_only ) |
| 654 |
{ |
| 655 |
int ntab = 0; |
| 656 |
while ( *str ) |
| 657 |
{ |
| 658 |
if ( *str == '\t' && ++ntab==8 ) |
| 659 |
{ |
| 660 |
*str = 0; |
| 661 |
break; |
| 662 |
} |
| 663 |
str++; |
| 664 |
} |
| 665 |
str = fp->line.s; |
| 666 |
} |
| 667 |
while ( *str && *str!='\t' ) str++; |
| 668 |
tmp.l = 0; |
| 669 |
kputsn(fp->line.s,str-fp->line.s,&tmp); |
| 670 |
int chr_id = bcf_hdr_name2id(args->out_hdr, tmp.s); |
| 671 |
if ( chr_id<0 ) error("\nThe sequence \"%s\" not defined in the header: %s\n(Quick workaround: index the file.)\n", tmp.s, args->fnames[i]); |
| 672 |
if ( prev_chr_id!=chr_id ) |
| 673 |
{ |
| 674 |
prev_pos = -1; |
| 675 |
if ( args->seen_seq[chr_id] ) |
| 676 |
error("\nThe chromosome block %s is not contiguous, consider running with -a.\n", tmp.s); |
| 677 |
} |
| 678 |
char *end; |
| 679 |
int pos = strtol(str+1,&end,10) - 1; |
| 680 |
if ( end==str+1 ) error("Could not parse line: %s\n", fp->line.s); |
| 681 |
if ( prev_pos > pos ) |
| 682 |
error("\nThe chromosome block %s is not sorted, consider running with -a.\n", tmp.s); |
| 683 |
args->seen_seq[chr_id] = 1; |
| 684 |
prev_chr_id = chr_id; |
| 685 |
|
| 686 |
if ( vcf_write_line(args->out_fh, &fp->line)!=0 ) error("\nFailed to write %"PRIu64" bytes\n", (uint64_t)fp->line.l); |
| 687 |
} |
| 688 |
} |
| 689 |
else |
| 690 |
{ |
| 691 |
|
| 692 |
line->max_unpack = 0; |
| 693 |
while ( bcf_read(fp, hdr, line)==0 ) |
| 694 |
{ |
| 695 |
if ( args->sites_only ) bcf_subset(args->out_hdr, line, 0, 0); |
| 696 |
bcf_translate(args->out_hdr, hdr, line); |
| 697 |
|
| 698 |
if ( prev_chr_id!=line->rid ) |
| 699 |
{ |
| 700 |
prev_pos = -1; |
| 701 |
if ( args->seen_seq[line->rid] ) |
| 702 |
error("\nThe chromosome block %s is not contiguous, consider running with -a.\n", bcf_seqname(args->out_hdr, line)); |
| 703 |
} |
| 704 |
if ( prev_pos > line->pos ) |
| 705 |
error("\nThe chromosome block %s is not sorted, consider running with -a.\n", bcf_seqname(args->out_hdr, line)); |
| 706 |
args->seen_seq[line->rid] = 1; |
| 707 |
prev_chr_id = line->rid; |
| 708 |
|
| 709 |
if ( bcf_write(args->out_fh, args->out_hdr, line)!=0 ) error("\nFailed to write\n"); |
| 710 |
} |
| 711 |
} |
| 712 |
bcf_hdr_destroy(hdr); |
| 713 |
hts_close(fp); |
| 714 |
if ( args->verbose ) |
| 715 |
{ |
| 716 |
gettimeofday(&t1, NULL); |
| 717 |
double delta = (t1.tv_sec - t0.tv_sec) * 1e6 + (t1.tv_usec - t0.tv_usec); |
| 718 |
fprintf(stderr,"\t%f seconds\n",delta/1e6); |
| 719 |
} |
| 720 |
} |
| 721 |
bcf_destroy(line); |
| 722 |
free(tmp.s); |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
int print_vcf_gz_header(BGZF *fp, BGZF *bgzf_out, int print_header, kstring_t *tmp) |
| 727 |
{ |
| 728 |
char *buffer = (char*) fp->uncompressed_block; |
| 729 |
|
| 730 |
|
| 731 |
if ( buffer[0]!='#' ) error("Could not parse the header, expected '#', found '%c'\n", buffer[0]); |
| 732 |
|
| 733 |
int nskip = 1; |
| 734 |
while (1) |
| 735 |
{ |
| 736 |
if ( buffer[nskip]=='\n' ) |
| 737 |
{ |
| 738 |
nskip++; |
| 739 |
if ( nskip>=fp->block_length ) |
| 740 |
{ |
| 741 |
kputsn(buffer,nskip,tmp); |
| 742 |
if ( bgzf_read_block(fp) != 0 ) return -1; |
| 743 |
if ( !fp->block_length ) break; |
| 744 |
nskip = 0; |
| 745 |
} |
| 746 |
|
| 747 |
if ( buffer[nskip]!='#' ) |
| 748 |
{ |
| 749 |
kputsn(buffer,nskip,tmp); |
| 750 |
break; |
| 751 |
} |
| 752 |
} |
| 753 |
nskip++; |
| 754 |
if ( nskip>=fp->block_length ) |
| 755 |
{ |
| 756 |
kputsn(buffer,fp->block_length,tmp); |
| 757 |
if ( bgzf_read_block(fp) != 0 ) return -1; |
| 758 |
if ( !fp->block_length ) break; |
| 759 |
nskip = 0; |
| 760 |
} |
| 761 |
} |
| 762 |
if ( print_header ) |
| 763 |
{ |
| 764 |
if ( bgzf_write(bgzf_out,tmp->s,tmp->l) != tmp->l ) error("Failed to write %"PRIu64" bytes\n", (uint64_t)tmp->l); |
| 765 |
tmp->l = 0; |
| 766 |
} |
| 767 |
return nskip; |
| 768 |
} |
| 769 |
|
| 770 |
static inline int unpackInt16(const uint8_t *buffer) |
| 771 |
{ |
| 772 |
return buffer[0] | buffer[1] << 8; |
| 773 |
} |
| 774 |
static int check_header(const uint8_t *header) |
| 775 |
{ |
| 776 |
if ( header[0] != 31 || header[1] != 139 || header[2] != 8 ) return -2; |
| 777 |
return ((header[3] & 4) != 0 |
| 778 |
&& unpackInt16((uint8_t*)&header[10]) == 6 |
| 779 |
&& header[12] == 'B' && header[13] == 'C' |
| 780 |
&& unpackInt16((uint8_t*)&header[14]) == 2) ? 0 : -1; |
| 781 |
} |
| 782 |
static void _check_hrecs(const bcf_hdr_t *hdr0, const bcf_hdr_t *hdr, char *fname0, char *fname) |
| 783 |
{ |
| 784 |
int j; |
| 785 |
for (j=0; j<hdr0->nhrec; j++) |
| 786 |
{ |
| 787 |
bcf_hrec_t *hrec0 = hdr0->hrec[j]; |
| 788 |
if ( hrec0->type!=BCF_HL_FLT && hrec0->type!=BCF_HL_INFO && hrec0->type!=BCF_HL_FMT && hrec0->type!=BCF_HL_CTG ) continue; |
| 789 |
int itag = bcf_hrec_find_key(hrec0, "ID"); |
| 790 |
bcf_hrec_t *hrec = bcf_hdr_get_hrec(hdr, hrec0->type, "ID", hrec0->vals[itag], NULL); |
| 791 |
|
| 792 |
char *type = NULL; |
| 793 |
if ( hrec0->type==BCF_HL_FLT ) type = "FILTER"; |
| 794 |
if ( hrec0->type==BCF_HL_INFO ) type = "INFO"; |
| 795 |
if ( hrec0->type==BCF_HL_FMT ) type = "FORMAT"; |
| 796 |
if ( hrec0->type==BCF_HL_CTG ) type = "contig"; |
| 797 |
|
| 798 |
if ( !hrec ) |
| 799 |
error("Cannot use --naive, incompatible headers, the tag %s/%s not present in %s\n",type,hrec0->vals[itag],fname); |
| 800 |
|
| 801 |
int idx0 = bcf_hrec_find_key(hrec0, "IDX"); |
| 802 |
int idx = bcf_hrec_find_key(hrec, "IDX"); |
| 803 |
if ( idx0<0 || idx<0 ) |
| 804 |
error("fixme: unexpected IDX<0 for %s/%s in %s or %s\n",type,hrec0->vals[itag],fname0,fname); |
| 805 |
if ( strcmp(hrec0->vals[idx0],hrec->vals[idx]) ) |
| 806 |
error("Cannot use --naive, use --naive-force instead: different order the tag %s/%s in %s vs %s\n",type,hrec0->vals[itag],fname0,fname); |
| 807 |
} |
| 808 |
} |
| 809 |
static void naive_concat_check_headers(args_t *args) |
| 810 |
{ |
| 811 |
fprintf(stderr,"Checking the headers of %d files.\n",args->nfnames); |
| 812 |
bcf_hdr_t *hdr0 = NULL; |
| 813 |
int i,j; |
| 814 |
for (i=0; i<args->nfnames; i++) |
| 815 |
{ |
| 816 |
htsFile *fp = hts_open(args->fnames[i], "r"); if ( !fp ) error("Failed to open: %s\n", args->fnames[i]); |
| 817 |
bcf_hdr_t *hdr = bcf_hdr_read(fp); if ( !hdr ) error("Failed to parse header: %s\n", args->fnames[i]); |
| 818 |
htsFormat type = *hts_get_format(fp); |
| 819 |
hts_close(fp); |
| 820 |
|
| 821 |
if ( i==0 ) |
| 822 |
{ |
| 823 |
hdr0 = hdr; |
| 824 |
continue; |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
if ( bcf_hdr_nsamples(hdr0)!=bcf_hdr_nsamples(hdr) ) |
| 829 |
error("Cannot concatenate, different number of samples: %d vs %d in %s vs %s\n",bcf_hdr_nsamples(hdr0),bcf_hdr_nsamples(hdr),args->fnames[0],args->fnames[i]); |
| 830 |
for (j=0; j<bcf_hdr_nsamples(hdr0); j++) |
| 831 |
if ( strcmp(hdr0->samples[j],hdr->samples[j]) ) |
| 832 |
error("Cannot concatenate, different samples in %s vs %s\n",args->fnames[0],args->fnames[i]); |
| 833 |
|
| 834 |
|
| 835 |
if ( type.compression!=bgzf ) |
| 836 |
error("The --naive option works only for compressed BCFs or VCFs, sorry :-/\n"); |
| 837 |
|
| 838 |
_check_hrecs(hdr0,hdr,args->fnames[0],args->fnames[i]); |
| 839 |
_check_hrecs(hdr,hdr0,args->fnames[i],args->fnames[0]); |
| 840 |
|
| 841 |
bcf_hdr_destroy(hdr); |
| 842 |
} |
| 843 |
if ( hdr0 ) bcf_hdr_destroy(hdr0); |
| 844 |
fprintf(stderr,"Done, the headers are compatible.\n"); |
| 845 |
} |
| 846 |
static void naive_concat(args_t *args) |
| 847 |
{ |
| 848 |
if ( !args->naive_concat_trust_headers ) |
| 849 |
naive_concat_check_headers(args); |
| 850 |
|
| 851 |
|
| 852 |
BGZF *bgzf_out = bgzf_open(args->output_fname,"w");; |
| 853 |
|
| 854 |
htsFormat output_type; |
| 855 |
output_type.format = (args->output_type & FT_VCF) ? vcf : bcf; |
| 856 |
output_type.compression = (args->output_type & FT_GZ) ? bgzf : no_compression; |
| 857 |
|
| 858 |
struct timeval t0, t1; |
| 859 |
const size_t page_size = BGZF_MAX_BLOCK_SIZE; |
| 860 |
uint8_t *buf = (uint8_t*) malloc(page_size); |
| 861 |
kstring_t tmp = {0,0,0}; |
| 862 |
int i, file_types = 0; |
| 863 |
for (i=0; i<args->nfnames; i++) |
| 864 |
{ |
| 865 |
if ( args->verbose ) |
| 866 |
{ |
| 867 |
fprintf(stderr,"Concatenating %s", args->fnames[i]); |
| 868 |
gettimeofday(&t0, NULL); |
| 869 |
} |
| 870 |
htsFile *hts_fp = hts_open(args->fnames[i],"r"); |
| 871 |
if ( !hts_fp ) error("\nFailed to open: %s\n", args->fnames[i]); |
| 872 |
htsFormat type = *hts_get_format(hts_fp); |
| 873 |
|
| 874 |
if ( type.compression!=bgzf ) |
| 875 |
error("\nThe --naive option works only for compressed BCFs or VCFs\n"); |
| 876 |
file_types |= type.format==vcf ? 1 : 2; |
| 877 |
if ( file_types==3 ) |
| 878 |
error("\nThe --naive option works only for compressed files of the same type, all BCFs or all VCFs\n"); |
| 879 |
if ( args->explicit_output_type ) |
| 880 |
{ |
| 881 |
if ( output_type.format!=type.format ) |
| 882 |
error("\nThe --naive option works only for the output of the same type, all BCFs or all VCFs\n"); |
| 883 |
if ( output_type.compression!=type.compression ) |
| 884 |
error("\nThe --naive option works only for the output of the same compression type\n"); |
| 885 |
} |
| 886 |
|
| 887 |
BGZF *fp = hts_get_bgzfp(hts_fp); |
| 888 |
if ( !fp || bgzf_read_block(fp) != 0 || !fp->block_length ) |
| 889 |
error("\nFailed to read %s: %s\n", args->fnames[i], strerror(errno)); |
| 890 |
|
| 891 |
int nskip; |
| 892 |
if ( type.format==bcf ) |
| 893 |
{ |
| 894 |
const size_t magic_len = 5 + 4; |
| 895 |
uint8_t magic[magic_len]; |
| 896 |
if ( bgzf_read(fp, magic, magic_len) != magic_len ) error("\nFailed to read the BCF header in %s\n", args->fnames[i]); |
| 897 |
|
| 898 |
if (strncmp((char*)magic, "BCF\2\2", 5) != 0) error("\nInvalid BCF magic string in %s\n", args->fnames[i]); |
| 899 |
|
| 900 |
tmp.l = le_to_u32(magic + 5); |
| 901 |
hts_expand(char,tmp.l,tmp.m,tmp.s); |
| 902 |
if ( bgzf_read(fp, tmp.s, tmp.l) != tmp.l ) error("\nFailed to read the BCF header in %s\n", args->fnames[i]); |
| 903 |
|
| 904 |
|
| 905 |
if ( i==0 ) |
| 906 |
{ |
| 907 |
if ( bgzf_write(bgzf_out, magic, magic_len) != magic_len ) error("\nFailed to write %zu bytes to %s\n", magic_len,args->output_fname); |
| 908 |
if ( bgzf_write(bgzf_out, tmp.s, tmp.l) != tmp.l) error("\nFailed to write %"PRId64" bytes to %s\n", (uint64_t)tmp.l,args->output_fname); |
| 909 |
} |
| 910 |
nskip = fp->block_offset; |
| 911 |
} |
| 912 |
else |
| 913 |
{ |
| 914 |
nskip = print_vcf_gz_header(fp, bgzf_out, i==0?1:0, &tmp); |
| 915 |
if ( nskip==-1 ) error("\nError reading %s\n", args->fnames[i]); |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
if ( fp->block_length - nskip > 0 ) |
| 920 |
{ |
| 921 |
if ( bgzf_write(bgzf_out, (char *)fp->uncompressed_block+nskip, fp->block_length-nskip)<0 ) error("\nError: %d\n",fp->errcode); |
| 922 |
} |
| 923 |
if ( bgzf_flush(bgzf_out)<0 ) error("\nError: %d\n",bgzf_out->errcode); |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
ssize_t nread, nblock, nwr; |
| 929 |
const int nheader = 18, neof = 28; |
| 930 |
const uint8_t *eof = (uint8_t*) "\037\213\010\4\0\0\0\0\0\377\6\0\102\103\2\0\033\0\3\0\0\0\0\0\0\0\0\0"; |
| 931 |
while (1) |
| 932 |
{ |
| 933 |
nread = bgzf_raw_read(fp, buf, nheader); |
| 934 |
if ( !nread ) break; |
| 935 |
if ( nread != nheader || check_header(buf)!=0 ) error("\nCould not parse the header of a bgzf block: %s\n",args->fnames[i]); |
| 936 |
nblock = unpackInt16(buf+16) + 1; |
| 937 |
assert( nblock <= page_size && nblock >= nheader ); |
| 938 |
nread += bgzf_raw_read(fp, buf+nheader, nblock - nheader); |
| 939 |
if ( nread!=nblock ) error("\nCould not read %"PRId64" bytes: %s\n",(uint64_t)nblock,args->fnames[i]); |
| 940 |
if ( nread==neof && !memcmp(buf,eof,neof) ) continue; |
| 941 |
nwr = bgzf_raw_write(bgzf_out, buf, nread); |
| 942 |
if ( nwr != nread ) error("\nWrite failed, wrote %"PRId64" instead of %d bytes.\n", (uint64_t)nwr,(int)nread); |
| 943 |
} |
| 944 |
if (hts_close(hts_fp)) error("\nClose failed: %s\n",args->fnames[i]); |
| 945 |
if ( args->verbose ) |
| 946 |
{ |
| 947 |
gettimeofday(&t1, NULL); |
| 948 |
double delta = (t1.tv_sec - t0.tv_sec) * 1e6 + (t1.tv_usec - t0.tv_usec); |
| 949 |
fprintf(stderr,"\t%f seconds\n",delta/1e6); |
| 950 |
} |
| 951 |
} |
| 952 |
free(buf); |
| 953 |
free(tmp.s); |
| 954 |
if (bgzf_close(bgzf_out) < 0) error("Error: %d\n",bgzf_out->errcode); |
| 955 |
} |
| 956 |
|
| 957 |
static void usage(args_t *args) |
| 958 |
{ |
| 959 |
fprintf(stderr, "\n"); |
| 960 |
fprintf(stderr, "About: Concatenate or combine VCF/BCF files. All source files must have the same sample\n"); |
| 961 |
fprintf(stderr, " columns appearing in the same order. The program can be used, for example, to\n"); |
| 962 |
fprintf(stderr, " concatenate chromosome VCFs into one VCF, or combine a SNP VCF and an indel\n"); |
| 963 |
fprintf(stderr, " VCF into one. The input files must be sorted by chr and position. The files\n"); |
| 964 |
fprintf(stderr, " must be given in the correct order to produce sorted VCF on output unless\n"); |
| 965 |
fprintf(stderr, " the -a, --allow-overlaps option is specified. With the --naive option, the files\n"); |
| 966 |
fprintf(stderr, " are concatenated without being recompressed, which is very fast.\n"); |
| 967 |
fprintf(stderr, "Usage: bcftools concat [options] <A.vcf.gz> [<B.vcf.gz> [...]]\n"); |
| 968 |
fprintf(stderr, "\n"); |
| 969 |
fprintf(stderr, "Options:\n"); |
| 970 |
fprintf(stderr, " -a, --allow-overlaps First coordinate of the next file can precede last record of the current file.\n"); |
| 971 |
fprintf(stderr, " -c, --compact-PS Do not output PS tag at each site, only at the start of a new phase set block.\n"); |
| 972 |
fprintf(stderr, " -d, --rm-dups STRING Output duplicate records present in multiple files only once: <snps|indels|both|all|exact>\n"); |
| 973 |
fprintf(stderr, " -D, --remove-duplicates Alias for -d exact\n"); |
| 974 |
fprintf(stderr, " -f, --file-list FILE Read the list of files from a file.\n"); |
| 975 |
fprintf(stderr, " -G, --drop-genotypes Drop individual genotype information.\n"); |
| 976 |
fprintf(stderr, " -l, --ligate Ligate phased VCFs by matching phase at overlapping haplotypes\n"); |
| 977 |
fprintf(stderr, " --ligate-force Ligate even non-overlapping chunks, keep all sites\n"); |
| 978 |
fprintf(stderr, " --ligate-warn Drop sites in imperfect overlaps\n"); |
| 979 |
fprintf(stderr, " --no-version Do not append version and command line to the header\n"); |
| 980 |
fprintf(stderr, " -n, --naive Concatenate files without recompression, a header check compatibility is performed\n"); |
| 981 |
fprintf(stderr, " --naive-force Same as --naive, but header compatibility is not checked. Dangerous, use with caution.\n"); |
| 982 |
fprintf(stderr, " -o, --output FILE Write output to a file [standard output]\n"); |
| 983 |
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"); |
| 984 |
fprintf(stderr, " -q, --min-PQ INT Break phase set if phasing quality is lower than <int> [30]\n"); |
| 985 |
fprintf(stderr, " -r, --regions REGION Restrict to comma-separated list of regions\n"); |
| 986 |
fprintf(stderr, " -R, --regions-file FILE Restrict to regions listed in a file\n"); |
| 987 |
fprintf(stderr, " --regions-overlap 0|1|2 Include if POS in the region (0), record overlaps (1), variant overlaps (2) [1]\n"); |
| 988 |
fprintf(stderr, " --threads INT Use multithreading with <int> worker threads [0]\n"); |
| 989 |
fprintf(stderr, " -v, --verbose 0|1 Set verbosity level [1]\n"); |
| 990 |
fprintf(stderr, " -W, --write-index[=FMT] Automatically index the output files [off]\n"); |
| 991 |
fprintf(stderr, "\n"); |
| 992 |
exit(1); |
| 993 |
} |
| 994 |
|
| 995 |
int main_vcfconcat(int argc, char *argv[]) |
| 996 |
{ |
| 997 |
int c; |
| 998 |
args_t *args = (args_t*) calloc(1,sizeof(args_t)); |
| 999 |
args->argc = argc; args->argv = argv; |
| 1000 |
args->output_fname = "-"; |
| 1001 |
args->output_type = FT_VCF; |
| 1002 |
args->n_threads = 0; |
| 1003 |
args->record_cmd_line = 1; |
| 1004 |
args->min_PQ = 30; |
| 1005 |
args->verbose = 1; |
| 1006 |
args->clevel = -1; |
| 1007 |
|
| 1008 |
static struct option loptions[] = |
| 1009 |
{ |
| 1010 |
{"verbose",required_argument,NULL,'v'}, |
| 1011 |
{"naive",no_argument,NULL,'n'}, |
| 1012 |
{"naive-force",no_argument,NULL,7}, |
| 1013 |
{"compact-PS",no_argument,NULL,'c'}, |
| 1014 |
{"regions",required_argument,NULL,'r'}, |
| 1015 |
{"regions-file",required_argument,NULL,'R'}, |
| 1016 |
{"regions-overlap",required_argument,NULL,12}, |
| 1017 |
{"remove-duplicates",no_argument,NULL,'D'}, |
| 1018 |
{"rm-dups",required_argument,NULL,'d'}, |
| 1019 |
{"allow-overlaps",no_argument,NULL,'a'}, |
| 1020 |
{"ligate",no_argument,NULL,'l'}, |
| 1021 |
{"ligate-force",no_argument,NULL,10}, |
| 1022 |
{"ligate-warn",no_argument,NULL,11}, |
| 1023 |
{"output",required_argument,NULL,'o'}, |
| 1024 |
{"output-type",required_argument,NULL,'O'}, |
| 1025 |
{"threads",required_argument,NULL,9}, |
| 1026 |
{"file-list",required_argument,NULL,'f'}, |
| 1027 |
{"min-PQ",required_argument,NULL,'q'}, |
| 1028 |
{"no-version",no_argument,NULL,8}, |
| 1029 |
{"write-index",optional_argument,NULL,'W'}, |
| 1030 |
{"drop-genotypes",no_argument,NULL,'G'}, |
| 1031 |
{NULL,0,NULL,0} |
| 1032 |
}; |
| 1033 |
char *tmp; |
| 1034 |
while ((c = getopt_long(argc, argv, "h:?o:O:f:alq:Dd:Gr:R:cnv:W::",loptions,NULL)) >= 0) |
| 1035 |
{ |
| 1036 |
switch (c) { |
| 1037 |
case 'c': args->compact_PS = 1; break; |
| 1038 |
case 'r': args->regions_list = optarg; break; |
| 1039 |
case 'R': args->regions_list = optarg; args->regions_is_file = 1; break; |
| 1040 |
case 'd': args->remove_dups = optarg; break; |
| 1041 |
case 'D': args->remove_dups = "exact"; break; |
| 1042 |
case 'q': |
| 1043 |
args->min_PQ = strtol(optarg,&tmp,10); |
| 1044 |
if ( *tmp ) error("Could not parse argument: --min-PQ %s\n", optarg); |
| 1045 |
break; |
| 1046 |
case 'n': args->naive_concat = 1; break; |
| 1047 |
case 'a': args->allow_overlaps = 1; break; |
| 1048 |
case 'l': args->phased_concat = 1; break; |
| 1049 |
case 'f': args->file_list = optarg; break; |
| 1050 |
case 'G': args->sites_only = 1; break; |
| 1051 |
case 'o': args->output_fname = optarg; break; |
| 1052 |
case 'O': |
| 1053 |
args->explicit_output_type = 1; |
| 1054 |
switch (optarg[0]) { |
| 1055 |
case 'b': args->output_type = FT_BCF_GZ; break; |
| 1056 |
case 'u': args->output_type = FT_BCF; break; |
| 1057 |
case 'z': args->output_type = FT_VCF_GZ; break; |
| 1058 |
case 'v': args->output_type = FT_VCF; break; |
| 1059 |
default: |
| 1060 |
{ |
| 1061 |
args->clevel = strtol(optarg,&tmp,10); |
| 1062 |
if ( *tmp || args->clevel<0 || args->clevel>9 ) error("The output type \"%s\" not recognised\n", optarg); |
| 1063 |
} |
| 1064 |
}; |
| 1065 |
if ( optarg[1] ) |
| 1066 |
{ |
| 1067 |
args->clevel = strtol(optarg+1,&tmp,10); |
| 1068 |
if ( *tmp || args->clevel<0 || args->clevel>9 ) error("Could not parse argument: --compression-level %s\n", optarg+1); |
| 1069 |
} |
| 1070 |
break; |
| 1071 |
case 10 : args->ligate_force = 1; break; |
| 1072 |
case 11 : args->ligate_warn = 1; break; |
| 1073 |
case 9 : args->n_threads = strtol(optarg, 0, 0); break; |
| 1074 |
case 8 : args->record_cmd_line = 0; break; |
| 1075 |
case 7 : args->naive_concat = 1; args->naive_concat_trust_headers = 1; break; |
| 1076 |
case 12 : |
| 1077 |
args->regions_overlap = parse_overlap_option(optarg); |
| 1078 |
if ( args->regions_overlap < 0 ) error("Could not parse: --regions-overlap %s\n",optarg); |
| 1079 |
break; |
| 1080 |
case 'v': |
| 1081 |
args->verbose = strtol(optarg, &tmp, 0); |
| 1082 |
if ( *tmp || args->verbose<0 || args->verbose>1 ) error("Error: currently only --verbose 0 or --verbose 1 is supported\n"); |
| 1083 |
break; |
| 1084 |
case 'W': |
| 1085 |
if (!(args->write_index = write_index_parse(optarg))) |
| 1086 |
error("Unsupported index format '%s'\n", optarg); |
| 1087 |
break; |
| 1088 |
case 'h': |
| 1089 |
case '?': usage(args); break; |
| 1090 |
default: error("Unknown argument: %s\n", optarg); |
| 1091 |
} |
| 1092 |
} |
| 1093 |
while ( optind<argc ) |
| 1094 |
{ |
| 1095 |
args->nfnames++; |
| 1096 |
args->fnames = (char **)realloc(args->fnames,sizeof(char*)*args->nfnames); |
| 1097 |
args->fnames[args->nfnames-1] = strdup(argv[optind]); |
| 1098 |
optind++; |
| 1099 |
} |
| 1100 |
if ( args->ligate_force && args->ligate_warn ) error("The options cannot be combined: --ligate-force and --ligate-warn\n"); |
| 1101 |
if ( args->allow_overlaps && args->phased_concat ) error("The options -a and -l should not be combined. Please run with -l only.\n"); |
| 1102 |
if ( args->sites_only && args->phased_concat ) error("The options --drop-genotypes and --ligate cannot be combined\n"); |
| 1103 |
if ( args->compact_PS && !args->phased_concat ) error("The -c option is intended only with -l\n"); |
| 1104 |
if ( args->file_list ) |
| 1105 |
{ |
| 1106 |
if ( args->nfnames ) error("Cannot combine -l with file names on command line.\n"); |
| 1107 |
args->fnames = hts_readlines(args->file_list, &args->nfnames); |
| 1108 |
if ( !args->fnames ) error("Could not read the file: %s\n", args->file_list); |
| 1109 |
} |
| 1110 |
if ( !args->nfnames ) usage(args); |
| 1111 |
if ( args->remove_dups && !args->allow_overlaps ) error("The -D option is supported only with -a\n"); |
| 1112 |
if ( args->regions_list && !args->allow_overlaps ) error("The -r/-R option is supported only with -a\n"); |
| 1113 |
if ( args->naive_concat ) |
| 1114 |
{ |
| 1115 |
if ( args->write_index ) error("Error: cannot --write-index in the %s mode\n",args->naive_concat_trust_headers?"--naive-force":"--naive"); |
| 1116 |
if ( args->allow_overlaps ) error("The option --naive cannot be combined with --allow-overlaps\n"); |
| 1117 |
if ( args->phased_concat ) error("The option --naive cannot be combined with --ligate\n"); |
| 1118 |
if ( args->sites_only ) error("The option --naive cannot be combined with --drop-genotypes\n"); |
| 1119 |
naive_concat(args); |
| 1120 |
destroy_data(args); |
| 1121 |
free(args); |
| 1122 |
return 0; |
| 1123 |
} |
| 1124 |
init_data(args); |
| 1125 |
concat(args); |
| 1126 |
destroy_data(args); |
| 1127 |
free(args); |
| 1128 |
return 0; |
| 1129 |
} |