| 1 |
#include "bcftools.pysam.h" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include <getopt.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 <htslib/vcf.h> |
| 36 |
#include <htslib/synced_bcf_reader.h> |
| 37 |
#include <htslib/vcfutils.h> |
| 38 |
#include <htslib/hts_os.h> |
| 39 |
#include <htslib/hts_defs.h> |
| 40 |
#include "bcftools.h" |
| 41 |
#include "filter.h" |
| 42 |
|
| 43 |
#define OP_PLUS 1 |
| 44 |
#define OP_MINUS 2 |
| 45 |
#define OP_EQUAL 3 |
| 46 |
#define OP_VENN 4 |
| 47 |
#define OP_COMPLEMENT 5 |
| 48 |
#define OP_EXACT 6 |
| 49 |
|
| 50 |
|
| 51 |
#define FLT_INCLUDE 1 |
| 52 |
#define FLT_EXCLUDE 2 |
| 53 |
|
| 54 |
typedef struct |
| 55 |
{ |
| 56 |
int isec_op, isec_n, *write, iwrite, nwrite, output_type, n_threads, clevel; |
| 57 |
int nflt, *flt_logic; |
| 58 |
filter_t **flt; |
| 59 |
char **flt_expr; |
| 60 |
bcf_srs_t *files; |
| 61 |
FILE *fh_log, *fh_sites; |
| 62 |
htsFile **fh_out; |
| 63 |
char **argv, *prefix, *output_fname, **fnames, *write_files, *targets_list, *regions_list; |
| 64 |
char *isec_exact, *file_list; |
| 65 |
int argc, record_cmd_line; |
| 66 |
char *index_fn; |
| 67 |
int write_index; |
| 68 |
} |
| 69 |
args_t; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
void HTS_FORMAT(HTS_PRINTF_FMT, 1, 2) |
| 76 |
mkdir_p(const char *fmt, ...) |
| 77 |
{ |
| 78 |
va_list ap; |
| 79 |
va_start(ap, fmt); |
| 80 |
int n = vsnprintf(NULL, 0, fmt, ap) + 2; |
| 81 |
va_end(ap); |
| 82 |
|
| 83 |
char *tmp = (char*)malloc(n); |
| 84 |
if (!tmp) error("Couldn't allocate space for path: %s\n", strerror(errno)); |
| 85 |
va_start(ap, fmt); |
| 86 |
vsnprintf(tmp, n, fmt, ap); |
| 87 |
va_end(ap); |
| 88 |
|
| 89 |
char *p = tmp+1; |
| 90 |
while (*p) |
| 91 |
{ |
| 92 |
while (*p && *p!='/') p++; |
| 93 |
if ( !*p ) break; |
| 94 |
char ctmp = *p; |
| 95 |
*p = 0; |
| 96 |
int ret = mkdir(tmp,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); |
| 97 |
if ( ret!=0 && errno!=EEXIST ) error("Error creating directory %s: %s\n", tmp,strerror(errno)); |
| 98 |
*p = ctmp; |
| 99 |
while ( *p && *p=='/' ) p++; |
| 100 |
} |
| 101 |
free(tmp); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
FILE * HTS_FORMAT(HTS_PRINTF_FMT, 3, 4) |
| 113 |
open_file(char **fname, const char *mode, const char *fmt, ...) |
| 114 |
{ |
| 115 |
va_list ap; |
| 116 |
va_start(ap, fmt); |
| 117 |
int n = vsnprintf(NULL, 0, fmt, ap) + 2; |
| 118 |
va_end(ap); |
| 119 |
|
| 120 |
char *str = (char*)malloc(n); |
| 121 |
va_start(ap, fmt); |
| 122 |
vsnprintf(str, n, fmt, ap); |
| 123 |
va_end(ap); |
| 124 |
|
| 125 |
mkdir_p("%s", str); |
| 126 |
if ( !mode ) |
| 127 |
{ |
| 128 |
if ( !fname ) error("Uh: expected fname or mode\n"); |
| 129 |
*fname = str; |
| 130 |
return NULL; |
| 131 |
} |
| 132 |
|
| 133 |
FILE *fp = fopen(str,mode); |
| 134 |
if ( fname ) *fname = str; |
| 135 |
else free(str); |
| 136 |
return fp; |
| 137 |
} |
| 138 |
|
| 139 |
void isec_vcf(args_t *args) |
| 140 |
{ |
| 141 |
bcf_srs_t *files = args->files; |
| 142 |
kstring_t str = {0,0,0}; |
| 143 |
htsFile *out_fh = NULL; |
| 144 |
|
| 145 |
|
| 146 |
int out_std = 0; |
| 147 |
if ( args->nwrite==1 && !args->prefix ) out_std = 1; |
| 148 |
if ( args->targets_list && files->nreaders==1 ) out_std = 1; |
| 149 |
if ( out_std ) |
| 150 |
{ |
| 151 |
char wmode[8]; |
| 152 |
set_wmode(wmode,args->output_type,args->output_fname,args->clevel); |
| 153 |
out_fh = hts_open(args->output_fname ? args->output_fname : "-", wmode); |
| 154 |
if ( out_fh == NULL ) error("Can't write to %s: %s\n", args->output_fname? args->output_fname : "standard output", strerror(errno)); |
| 155 |
if ( args->n_threads ) hts_set_threads(out_fh, args->n_threads); |
| 156 |
if (args->record_cmd_line) bcf_hdr_append_version(files->readers[args->iwrite].header,args->argc,args->argv,"bcftools_isec"); |
| 157 |
if ( bcf_hdr_write(out_fh, files->readers[args->iwrite].header)!=0 ) error("[%s] Error: cannot write to %s\n", __func__,args->output_fname?args->output_fname:"standard output"); |
| 158 |
if ( init_index2(out_fh,files->readers[args->iwrite].header, |
| 159 |
args->output_fname,&args->index_fn, |
| 160 |
args->write_index)<0 ) |
| 161 |
error("Error: failed to initialise index for %s\n", |
| 162 |
args->output_fname?args->output_fname:"standard output"); |
| 163 |
} |
| 164 |
if ( !args->nwrite && !out_std && !args->prefix ) |
| 165 |
fprintf(bcftools_stderr,"Note: -w option not given, printing list of sites...\n"); |
| 166 |
|
| 167 |
int n; |
| 168 |
while ( (n=bcf_sr_next_line(files)) ) |
| 169 |
{ |
| 170 |
bcf_sr_t *reader = NULL; |
| 171 |
bcf1_t *line = NULL; |
| 172 |
int i, ret = 0; |
| 173 |
for (i=0; i<files->nreaders; i++) |
| 174 |
{ |
| 175 |
if ( !bcf_sr_has_line(files,i) ) continue; |
| 176 |
|
| 177 |
if ( args->nflt && args->flt[i] ) |
| 178 |
{ |
| 179 |
bcf1_t *rec = bcf_sr_get_line(files, i); |
| 180 |
int pass = filter_test(args->flt[i], rec, NULL); |
| 181 |
if ( args->flt_logic[i] & FLT_EXCLUDE ) pass = pass ? 0 : 1; |
| 182 |
if ( !pass ) |
| 183 |
{ |
| 184 |
files->has_line[i] = 0; |
| 185 |
n--; |
| 186 |
continue; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
if ( !line ) |
| 191 |
{ |
| 192 |
line = files->readers[i].buffer[0]; |
| 193 |
reader = &files->readers[i]; |
| 194 |
} |
| 195 |
ret |= 1<<i; |
| 196 |
} |
| 197 |
if ( !line ) continue; |
| 198 |
|
| 199 |
switch (args->isec_op) |
| 200 |
{ |
| 201 |
case OP_COMPLEMENT: if ( n!=1 || !bcf_sr_has_line(files,0) ) continue; break; |
| 202 |
case OP_EQUAL: if ( n != args->isec_n ) continue; break; |
| 203 |
case OP_PLUS: if ( n < args->isec_n ) continue; break; |
| 204 |
case OP_MINUS: if ( n > args->isec_n ) continue; break; |
| 205 |
case OP_EXACT: |
| 206 |
for (i=0; i<files->nreaders; i++) |
| 207 |
if ( files->has_line[i] != args->isec_exact[i] ) break; |
| 208 |
if ( i<files->nreaders ) continue; |
| 209 |
break; |
| 210 |
} |
| 211 |
|
| 212 |
if ( out_std ) |
| 213 |
{ |
| 214 |
if ( bcf_sr_has_line(files,args->iwrite) && bcf_write1(out_fh, files->readers[args->iwrite].header, files->readers[args->iwrite].buffer[0])!=0 ) |
| 215 |
error("[%s] Error: cannot write to %s\n", __func__, args->output_fname ? args->output_fname : "standard output"); |
| 216 |
continue; |
| 217 |
} |
| 218 |
else if ( args->fh_sites ) |
| 219 |
{ |
| 220 |
str.l = 0; |
| 221 |
kputs(reader->header->id[BCF_DT_CTG][line->rid].key, &str); kputc('\t', &str); |
| 222 |
kputw(line->pos+1, &str); kputc('\t', &str); |
| 223 |
if (line->n_allele > 0) kputs(line->d.allele[0], &str); |
| 224 |
else kputc('.', &str); |
| 225 |
kputc('\t', &str); |
| 226 |
if (line->n_allele > 1) kputs(line->d.allele[1], &str); |
| 227 |
else kputc('.', &str); |
| 228 |
for (i=2; i<line->n_allele; i++) |
| 229 |
{ |
| 230 |
kputc(',', &str); |
| 231 |
kputs(line->d.allele[i], &str); |
| 232 |
} |
| 233 |
kputc('\t', &str); |
| 234 |
for (i=0; i<files->nreaders; i++) |
| 235 |
kputc(bcf_sr_has_line(files,i)?'1':'0', &str); |
| 236 |
kputc('\n', &str); |
| 237 |
if ( fwrite(str.s,sizeof(char),str.l,args->fh_sites)!=str.l ) |
| 238 |
error("[%s] Error: failed to write %d bytes to %s\n", __func__,(int)str.l,args->output_fname ? args->output_fname : "standard output"); |
| 239 |
} |
| 240 |
|
| 241 |
if ( args->prefix ) |
| 242 |
{ |
| 243 |
if ( args->isec_op==OP_VENN && ret==3 ) |
| 244 |
{ |
| 245 |
if ( !args->nwrite || args->write[0] ) |
| 246 |
{ |
| 247 |
if ( bcf_write1(args->fh_out[2], bcf_sr_get_header(files,0), bcf_sr_get_line(files,0))!=0 ) |
| 248 |
error("[%s] Error: cannot write\n", __func__); |
| 249 |
} |
| 250 |
if ( !args->nwrite || args->write[1] ) |
| 251 |
{ |
| 252 |
if ( bcf_write1(args->fh_out[3], bcf_sr_get_header(files,1), bcf_sr_get_line(files,1))!=0 ) |
| 253 |
error("[%s] Error: cannot write\n", __func__); |
| 254 |
} |
| 255 |
} |
| 256 |
else |
| 257 |
{ |
| 258 |
for (i=0; i<files->nreaders; i++) |
| 259 |
{ |
| 260 |
if ( !bcf_sr_has_line(files,i) ) continue; |
| 261 |
if ( args->write && !args->write[i] ) continue; |
| 262 |
if ( bcf_write1(args->fh_out[i], files->readers[i].header, files->readers[i].buffer[0])!=0 ) error("[%s] Error: cannot write\n", __func__); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
if ( str.s ) free(str.s); |
| 268 |
if ( out_fh ) |
| 269 |
{ |
| 270 |
if ( args->write_index ) |
| 271 |
{ |
| 272 |
if ( bcf_idx_save(out_fh)<0 ) |
| 273 |
{ |
| 274 |
if ( hts_close(out_fh)!=0 ) error("Error: close failed .. %s\n", args->output_fname?args->output_fname:"bcftools_stdout"); |
| 275 |
error("Error: cannot write to index %s\n", args->index_fn); |
| 276 |
} |
| 277 |
free(args->index_fn); |
| 278 |
} |
| 279 |
if ( hts_close(out_fh)!=0 ) error("[%s] Error: close failed .. %s\n", __func__,args->output_fname? args->output_fname : "-"); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
static void add_filter(args_t *args, char *expr, int logic) |
| 284 |
{ |
| 285 |
args->nflt++; |
| 286 |
args->flt_expr = (char**) realloc(args->flt_expr,sizeof(char*)*args->nflt); |
| 287 |
args->flt_logic = (int*) realloc(args->flt_logic,sizeof(int)*args->nflt); |
| 288 |
args->flt = (filter_t**) realloc(args->flt,sizeof(filter_t*)*args->nflt); |
| 289 |
if ( expr[0]=='-' && expr[1]==0 ) |
| 290 |
{ |
| 291 |
args->flt_expr[args->nflt-1] = NULL; |
| 292 |
args->flt[args->nflt-1] = NULL; |
| 293 |
} |
| 294 |
else |
| 295 |
args->flt_expr[args->nflt-1] = expr; |
| 296 |
args->flt_logic[args->nflt-1] = logic; |
| 297 |
} |
| 298 |
|
| 299 |
static void destroy_data(args_t *args); |
| 300 |
static void init_data(args_t *args) |
| 301 |
{ |
| 302 |
int i; |
| 303 |
if ( args->nflt ) |
| 304 |
{ |
| 305 |
if ( args->nflt > 1 && args->nflt!=args->files->nreaders ) |
| 306 |
error("Error: expected either one -i/-e option or as many as there are input files\n"); |
| 307 |
if ( args->nflt < args->files->nreaders ) |
| 308 |
{ |
| 309 |
if ( !args->flt_expr[0] ) error("Error: useless use of -i/-e\n"); |
| 310 |
args->nflt = args->files->nreaders; |
| 311 |
args->flt_expr = (char**) realloc(args->flt_expr,sizeof(char*)*args->nflt); |
| 312 |
args->flt_logic = (int*) realloc(args->flt_logic,sizeof(int)*args->nflt); |
| 313 |
args->flt = (filter_t**) realloc(args->flt,sizeof(filter_t*)*args->nflt); |
| 314 |
for (i=1; i<args->nflt; i++) |
| 315 |
{ |
| 316 |
args->flt_expr[i] = args->flt_expr[0]; |
| 317 |
args->flt_logic[i] = args->flt_logic[0]; |
| 318 |
args->flt[i] = filter_init(args->files->readers[i].header,args->flt_expr[i]); |
| 319 |
} |
| 320 |
args->flt[0] = filter_init(args->files->readers[0].header,args->flt_expr[0]); |
| 321 |
} |
| 322 |
else |
| 323 |
{ |
| 324 |
for (i=0; i<args->files->nreaders; i++) |
| 325 |
{ |
| 326 |
if ( !args->flt_expr[i] ) continue; |
| 327 |
args->flt[i] = filter_init(args->files->readers[i].header,args->flt_expr[i]); |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
if ( args->isec_op==OP_EXACT ) |
| 333 |
{ |
| 334 |
if ( strlen(args->isec_exact)!=args->files->nreaders ) |
| 335 |
error("The number of files does not match the bitmask: %d vs %s\n", args->files->nreaders,args->isec_exact); |
| 336 |
for (i=0; i<args->files->nreaders; i++) |
| 337 |
if ( args->isec_exact[i]!='0' && args->isec_exact[i]!='1' ) error("Unexpected bitmask: %s\n",args->isec_exact); |
| 338 |
for (i=0; i<args->files->nreaders; i++) |
| 339 |
args->isec_exact[i] -= '0'; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
char *p = args->write_files; |
| 344 |
while (p && *p) |
| 345 |
{ |
| 346 |
if ( !args->write ) args->write = (int*) calloc(args->files->nreaders,sizeof(int)); |
| 347 |
if ( sscanf(p,"%d",&i)!=1 ) error("Could not parse --write %s\n", args->write_files); |
| 348 |
if ( i<=0 || i>args->files->nreaders ) error("The index is out of range: %d (-w %s)\n", i, args->write_files); |
| 349 |
args->write[i-1] = 1; |
| 350 |
args->iwrite = i-1; |
| 351 |
args->nwrite++; |
| 352 |
while (*p && *p!=',') p++; |
| 353 |
if ( *p==',' ) p++; |
| 354 |
} |
| 355 |
if ( args->nwrite>1 && !args->prefix ) error("Expected -p when multiple output files given: --write %s\n", args->write_files); |
| 356 |
if ( args->isec_op==OP_COMPLEMENT && args->nwrite ) |
| 357 |
{ |
| 358 |
if ( args->nwrite>1 ) error("Multiple files to -w make no sense with -C\n"); |
| 359 |
if ( !args->write[0] ) error("Only -w1 makes sense with -C\n"); |
| 360 |
} |
| 361 |
|
| 362 |
if ( args->prefix ) |
| 363 |
{ |
| 364 |
|
| 365 |
args->fh_log = open_file(NULL,"w","%s/README.txt", args->prefix); |
| 366 |
if ( !args->fh_log ) error("%s/README.txt: %s\n", args->prefix, strerror(errno)); |
| 367 |
|
| 368 |
fprintf(args->fh_log,"This file was produced by vcfisec.\n"); |
| 369 |
fprintf(args->fh_log,"The command line was:\tbcftools %s ", args->argv[0]); |
| 370 |
int i; |
| 371 |
for (i=1; i<args->argc; i++) fprintf(args->fh_log," %s",args->argv[i]); |
| 372 |
fprintf(args->fh_log,"\n\nUsing the following file names:\n"); |
| 373 |
|
| 374 |
const char *suffix = "vcf"; |
| 375 |
if ( args->output_type & FT_BCF ) suffix = "bcf"; |
| 376 |
else if ( args->output_type & FT_GZ ) suffix = "vcf.gz"; |
| 377 |
|
| 378 |
|
| 379 |
if ( args->isec_op==OP_VENN ) |
| 380 |
{ |
| 381 |
args->fh_out = (htsFile**) malloc(sizeof(htsFile*)*4); |
| 382 |
args->fnames = (char**) calloc(4,sizeof(char*)); |
| 383 |
|
| 384 |
#define OPEN_FILE(i,j) { \ |
| 385 |
open_file(&args->fnames[i], NULL, "%s/%04d.%s", args->prefix, i, suffix); \ |
| 386 |
char wmode[8]; \ |
| 387 |
set_wmode(wmode,args->output_type,args->fnames[i],args->clevel); \ |
| 388 |
args->fh_out[i] = hts_open(args->fnames[i], wmode); \ |
| 389 |
if ( !args->fh_out[i] ) error("Could not open %s\n", args->fnames[i]); \ |
| 390 |
if ( args->n_threads ) hts_set_threads(args->fh_out[i], args->n_threads); \ |
| 391 |
if (args->record_cmd_line) bcf_hdr_append_version(args->files->readers[j].header,args->argc,args->argv,"bcftools_isec"); \ |
| 392 |
if ( bcf_hdr_write(args->fh_out[i], args->files->readers[j].header)!=0 ) error("[%s] Error: cannot write to %s\n", __func__,args->fnames[i]); \ |
| 393 |
} |
| 394 |
if ( !args->nwrite || args->write[0] ) |
| 395 |
{ |
| 396 |
OPEN_FILE(0,0); |
| 397 |
fprintf(args->fh_log,"%s\tfor records private to\t%s\n", args->fnames[0], args->files->readers[0].fname); |
| 398 |
} |
| 399 |
if ( !args->nwrite || args->write[1] ) |
| 400 |
{ |
| 401 |
OPEN_FILE(1,1); |
| 402 |
fprintf(args->fh_log,"%s\tfor records private to\t%s\n", args->fnames[1], args->files->readers[1].fname); |
| 403 |
} |
| 404 |
if ( !args->nwrite || args->write[0] ) |
| 405 |
{ |
| 406 |
OPEN_FILE(2,0); |
| 407 |
fprintf(args->fh_log,"%s\tfor records from %s shared by both\t%s %s\n", args->fnames[2], args->files->readers[0].fname, args->files->readers[0].fname, args->files->readers[1].fname); |
| 408 |
} |
| 409 |
if ( !args->nwrite || args->write[1] ) |
| 410 |
{ |
| 411 |
OPEN_FILE(3,1); |
| 412 |
fprintf(args->fh_log,"%s\tfor records from %s shared by both\t%s %s\n", args->fnames[3], args->files->readers[1].fname, args->files->readers[0].fname, args->files->readers[1].fname); |
| 413 |
} |
| 414 |
} |
| 415 |
else |
| 416 |
{ |
| 417 |
|
| 418 |
args->fh_out = (htsFile**) calloc(args->files->nreaders, sizeof(htsFile*)); |
| 419 |
args->fnames = (char**) calloc(args->files->nreaders, sizeof(char*)); |
| 420 |
|
| 421 |
for (i=0; i<args->files->nreaders; i++) |
| 422 |
{ |
| 423 |
if ( args->write && !args->write[i] ) continue; |
| 424 |
if ( args->isec_op==OP_COMPLEMENT && i>0 ) break; |
| 425 |
OPEN_FILE(i,i); |
| 426 |
fprintf(args->fh_log,"%s\tfor stripped\t%s\n", args->fnames[i], args->files->readers[i].fname); |
| 427 |
} |
| 428 |
#undef OPEN_FILE |
| 429 |
} |
| 430 |
args->fh_sites = open_file(NULL, "w", "%s/sites.txt", args->prefix); |
| 431 |
if ( !args->fh_sites ) error("%s/sites.txt: %s\n", args->prefix, strerror(errno)); |
| 432 |
} |
| 433 |
else { |
| 434 |
if (args->output_fname) { |
| 435 |
args->fh_sites = fopen(args->output_fname, "w"); |
| 436 |
if ( args->fh_sites == NULL ) error("Can't write to \"%s\": %s\n", args->output_fname, strerror(errno)); |
| 437 |
} |
| 438 |
else |
| 439 |
args->fh_sites = bcftools_stdout; |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
static void destroy_data(args_t *args) |
| 444 |
{ |
| 445 |
int i; |
| 446 |
if ( args->nflt ) |
| 447 |
{ |
| 448 |
for (i=0; i<args->nflt; i++) |
| 449 |
{ |
| 450 |
if ( !args->flt[i] ) continue; |
| 451 |
filter_destroy(args->flt[i]); |
| 452 |
} |
| 453 |
free(args->flt_expr); |
| 454 |
free(args->flt); |
| 455 |
free(args->flt_logic); |
| 456 |
} |
| 457 |
if ( args->prefix ) |
| 458 |
{ |
| 459 |
fclose(args->fh_log); |
| 460 |
int n = args->isec_op==OP_VENN ? 4 : args->files->nreaders; |
| 461 |
for (i=0; i<n; i++) |
| 462 |
{ |
| 463 |
if ( !args->fnames[i] ) continue; |
| 464 |
if ( hts_close(args->fh_out[i])!=0 ) error("[%s] Error: close failed .. %s\n", __func__,args->fnames[i]); |
| 465 |
int is_tbi = !args->write_index |
| 466 |
|| (args->write_index&127) == HTS_FMT_TBI; |
| 467 |
if ( args->output_type==FT_VCF_GZ && is_tbi ) |
| 468 |
{ |
| 469 |
tbx_conf_t conf = tbx_conf_vcf; |
| 470 |
tbx_index_build(args->fnames[i], -1, &conf); |
| 471 |
} |
| 472 |
else if ( args->output_type==FT_BCF_GZ || !is_tbi ) |
| 473 |
{ |
| 474 |
if ( bcf_index_build(args->fnames[i],14) ) error("Could not index %s\n", args->fnames[i]); |
| 475 |
} |
| 476 |
free(args->fnames[i]); |
| 477 |
} |
| 478 |
free(args->fh_out); |
| 479 |
free(args->fnames); |
| 480 |
if ( args->fh_sites ) fclose(args->fh_sites); |
| 481 |
if ( args->write ) free(args->write); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
static void usage(void) |
| 486 |
{ |
| 487 |
fprintf(bcftools_stderr, "\n"); |
| 488 |
fprintf(bcftools_stderr, "About: Create intersections, unions and complements of VCF files.\n"); |
| 489 |
fprintf(bcftools_stderr, "Usage: bcftools isec [options] <A.vcf.gz> <B.vcf.gz> [...]\n"); |
| 490 |
fprintf(bcftools_stderr, "\n"); |
| 491 |
fprintf(bcftools_stderr, "Options:\n"); |
| 492 |
fprintf(bcftools_stderr, " -c, --collapse STRING Treat as identical records with <snps|indels|both|all|some|none>, see man page for details [none]\n"); |
| 493 |
fprintf(bcftools_stderr, " -C, --complement Output positions present only in the first file but missing in the others\n"); |
| 494 |
fprintf(bcftools_stderr, " -e, --exclude EXPR Exclude sites for which the expression is true\n"); |
| 495 |
fprintf(bcftools_stderr, " -f, --apply-filters LIST Require at least one of the listed FILTER strings (e.g. \"PASS,.\")\n"); |
| 496 |
fprintf(bcftools_stderr, " -i, --include EXPR Include only sites for which the expression is true\n"); |
| 497 |
fprintf(bcftools_stderr, " -l, --file-list FILE Read the input file names from the file\n"); |
| 498 |
fprintf(bcftools_stderr, " --no-version Do not append version and command line to the header\n"); |
| 499 |
fprintf(bcftools_stderr, " -n, --nfiles [+-=~]INT Output positions present in this many (=), this many or more (+), this many or fewer (-), the exact (~) files\n"); |
| 500 |
fprintf(bcftools_stderr, " -o, --output FILE Write output to a file [standard output]\n"); |
| 501 |
fprintf(bcftools_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"); |
| 502 |
fprintf(bcftools_stderr, " -p, --prefix DIR If given, subset each of the input files accordingly, see also -w\n"); |
| 503 |
fprintf(bcftools_stderr, " -r, --regions REGION Restrict to comma-separated list of regions\n"); |
| 504 |
fprintf(bcftools_stderr, " -R, --regions-file FILE Restrict to regions listed in a file\n"); |
| 505 |
fprintf(bcftools_stderr, " --regions-overlap 0|1|2 Include if POS in the region (0), record overlaps (1), variant overlaps (2) [1]\n"); |
| 506 |
fprintf(bcftools_stderr, " -t, --targets REGION Similar to -r but streams rather than index-jumps\n"); |
| 507 |
fprintf(bcftools_stderr, " -T, --targets-file FILE Similar to -R but streams rather than index-jumps\n"); |
| 508 |
fprintf(bcftools_stderr, " --targets-overlap 0|1|2 Include if POS in the region (0), record overlaps (1), variant overlaps (2) [0]\n"); |
| 509 |
fprintf(bcftools_stderr, " --threads INT Use multithreading with <int> worker threads [0]\n"); |
| 510 |
fprintf(bcftools_stderr, " -w, --write LIST List of files to write with -p given as 1-based indexes. By default, all files are written\n"); |
| 511 |
fprintf(bcftools_stderr, " -W, --write-index[=FMT] Automatically index the output files [off]\n"); |
| 512 |
fprintf(bcftools_stderr, "\n"); |
| 513 |
fprintf(bcftools_stderr, "Examples:\n"); |
| 514 |
fprintf(bcftools_stderr, " # Create intersection and complements of two sets saving the output in dir/*\n"); |
| 515 |
fprintf(bcftools_stderr, " bcftools isec A.vcf.gz B.vcf.gz -p dir\n"); |
| 516 |
fprintf(bcftools_stderr, "\n"); |
| 517 |
fprintf(bcftools_stderr, " # Filter sites in A and B (but not in C) and create intersection\n"); |
| 518 |
fprintf(bcftools_stderr, " bcftools isec -e'MAF<0.01' -i'dbSNP=1' -e - A.vcf.gz B.vcf.gz C.vcf.gz -p dir\n"); |
| 519 |
fprintf(bcftools_stderr, "\n"); |
| 520 |
fprintf(bcftools_stderr, " # Extract and write records from A shared by both A and B using exact allele match\n"); |
| 521 |
fprintf(bcftools_stderr, " bcftools isec A.vcf.gz B.vcf.gz -p dir -n =2 -w 1\n"); |
| 522 |
fprintf(bcftools_stderr, "\n"); |
| 523 |
fprintf(bcftools_stderr, " # Extract and write records from C found in A and C but not in B\n"); |
| 524 |
fprintf(bcftools_stderr, " bcftools isec A.vcf.gz B.vcf.gz C.vcf.gz -p dir -n~101 -w 3\n"); |
| 525 |
fprintf(bcftools_stderr, "\n"); |
| 526 |
fprintf(bcftools_stderr, " # Extract records private to A or B comparing by position only\n"); |
| 527 |
fprintf(bcftools_stderr, " bcftools isec A.vcf.gz B.vcf.gz -p dir -n -1 -c all\n"); |
| 528 |
fprintf(bcftools_stderr, "\n"); |
| 529 |
bcftools_exit(1); |
| 530 |
} |
| 531 |
|
| 532 |
int main_vcfisec(int argc, char *argv[]) |
| 533 |
{ |
| 534 |
int c; |
| 535 |
args_t *args = (args_t*) calloc(1,sizeof(args_t)); |
| 536 |
args->files = bcf_sr_init(); |
| 537 |
args->argc = argc; args->argv = argv; |
| 538 |
args->output_fname = NULL; |
| 539 |
args->output_type = FT_VCF; |
| 540 |
args->n_threads = 0; |
| 541 |
args->record_cmd_line = 1; |
| 542 |
args->clevel = -1; |
| 543 |
int targets_is_file = 0, regions_is_file = 0; |
| 544 |
int regions_overlap = 1; |
| 545 |
int targets_overlap = 0; |
| 546 |
|
| 547 |
static struct option loptions[] = |
| 548 |
{ |
| 549 |
{"help",no_argument,NULL,'h'}, |
| 550 |
{"exclude",required_argument,NULL,'e'}, |
| 551 |
{"include",required_argument,NULL,'i'}, |
| 552 |
{"collapse",required_argument,NULL,'c'}, |
| 553 |
{"complement",no_argument,NULL,'C'}, |
| 554 |
{"apply-filters",required_argument,NULL,'f'}, |
| 555 |
{"file-list",required_argument,NULL,'l'}, |
| 556 |
{"nfiles",required_argument,NULL,'n'}, |
| 557 |
{"prefix",required_argument,NULL,'p'}, |
| 558 |
{"write",required_argument,NULL,'w'}, |
| 559 |
{"targets",required_argument,NULL,'t'}, |
| 560 |
{"targets-file",required_argument,NULL,'T'}, |
| 561 |
{"targets-overlap",required_argument,NULL,4}, |
| 562 |
{"regions",required_argument,NULL,'r'}, |
| 563 |
{"regions-file",required_argument,NULL,'R'}, |
| 564 |
{"regions-overlap",required_argument,NULL,3}, |
| 565 |
{"output",required_argument,NULL,'o'}, |
| 566 |
{"output-type",required_argument,NULL,'O'}, |
| 567 |
{"threads",required_argument,NULL,9}, |
| 568 |
{"no-version",no_argument,NULL,8}, |
| 569 |
{"write-index",optional_argument,NULL,'W'}, |
| 570 |
{NULL,0,NULL,0} |
| 571 |
}; |
| 572 |
char *tmp; |
| 573 |
while ((c = getopt_long(argc, argv, "hc:r:R:p:n:w:t:T:Cf:o:O:i:e:l:W::",loptions,NULL)) >= 0) { |
| 574 |
switch (c) { |
| 575 |
case 'o': args->output_fname = optarg; break; |
| 576 |
case 'O': |
| 577 |
switch (optarg[0]) { |
| 578 |
case 'b': args->output_type = FT_BCF_GZ; break; |
| 579 |
case 'u': args->output_type = FT_BCF; break; |
| 580 |
case 'z': args->output_type = FT_VCF_GZ; break; |
| 581 |
case 'v': args->output_type = FT_VCF; break; |
| 582 |
default: |
| 583 |
{ |
| 584 |
args->clevel = strtol(optarg,&tmp,10); |
| 585 |
if ( *tmp || args->clevel<0 || args->clevel>9 ) error("The output type \"%s\" not recognised\n", optarg); |
| 586 |
} |
| 587 |
} |
| 588 |
if ( optarg[1] ) |
| 589 |
{ |
| 590 |
args->clevel = strtol(optarg+1,&tmp,10); |
| 591 |
if ( *tmp || args->clevel<0 || args->clevel>9 ) error("Could not parse argument: --compression-level %s\n", optarg+1); |
| 592 |
} |
| 593 |
break; |
| 594 |
case 'c': |
| 595 |
if ( !strcmp(optarg,"snps") ) args->files->collapse |= COLLAPSE_SNPS; |
| 596 |
else if ( !strcmp(optarg,"indels") ) args->files->collapse |= COLLAPSE_INDELS; |
| 597 |
else if ( !strcmp(optarg,"both") ) args->files->collapse |= COLLAPSE_SNPS | COLLAPSE_INDELS; |
| 598 |
else if ( !strcmp(optarg,"any") ) args->files->collapse |= COLLAPSE_ANY; |
| 599 |
else if ( !strcmp(optarg,"all") ) args->files->collapse |= COLLAPSE_ANY; |
| 600 |
else if ( !strcmp(optarg,"some") ) args->files->collapse |= COLLAPSE_SOME; |
| 601 |
else if ( !strcmp(optarg,"none") ) args->files->collapse = COLLAPSE_NONE; |
| 602 |
else error("The --collapse string \"%s\" not recognised.\n", optarg); |
| 603 |
break; |
| 604 |
case 'f': args->files->apply_filters = optarg; break; |
| 605 |
case 'C': |
| 606 |
if ( args->isec_op!=0 && args->isec_op!=OP_COMPLEMENT ) error("Error: either -C or -n should be given, not both.\n"); |
| 607 |
args->isec_op = OP_COMPLEMENT; break; |
| 608 |
case 'l': args->file_list = optarg; break; |
| 609 |
case 'r': args->regions_list = optarg; break; |
| 610 |
case 'R': args->regions_list = optarg; regions_is_file = 1; break; |
| 611 |
case 't': args->targets_list = optarg; break; |
| 612 |
case 'T': args->targets_list = optarg; targets_is_file = 1; break; |
| 613 |
case 'p': args->prefix = optarg; break; |
| 614 |
case 'w': |
| 615 |
if ( args->write_files ) error("The option -w accepts a list of indices and can be given only once\n"); |
| 616 |
args->write_files = optarg; |
| 617 |
break; |
| 618 |
case 'i': add_filter(args, optarg, FLT_INCLUDE); break; |
| 619 |
case 'e': add_filter(args, optarg, FLT_EXCLUDE); break; |
| 620 |
case 'n': |
| 621 |
{ |
| 622 |
if ( args->isec_op!=0 && args->isec_op==OP_COMPLEMENT ) error("Error: either -C or -n should be given, not both.\n"); |
| 623 |
if ( args->isec_op!=0 ) error("Error: -n should be given only once.\n"); |
| 624 |
char *p = optarg; |
| 625 |
if ( *p=='-' ) { args->isec_op = OP_MINUS; p++; } |
| 626 |
else if ( *p=='+' ) { args->isec_op = OP_PLUS; p++; } |
| 627 |
else if ( *p=='=' ) { args->isec_op = OP_EQUAL; p++; } |
| 628 |
else if ( *p=='~' ) { args->isec_op = OP_EXACT; p++; } |
| 629 |
else if ( isdigit(*p) ) args->isec_op = OP_EQUAL; |
| 630 |
else error("Could not parse --nfiles %s\n", optarg); |
| 631 |
if ( args->isec_op == OP_EXACT ) args->isec_exact = p; |
| 632 |
else if ( sscanf(p,"%d",&args->isec_n)!=1 ) error("Could not parse --nfiles %s\n", optarg); |
| 633 |
} |
| 634 |
break; |
| 635 |
case 3 : |
| 636 |
regions_overlap = parse_overlap_option(optarg); |
| 637 |
if ( regions_overlap < 0 ) error("Could not parse: --regions-overlap %s\n",optarg); |
| 638 |
break; |
| 639 |
case 4 : |
| 640 |
targets_overlap = parse_overlap_option(optarg); |
| 641 |
if ( targets_overlap < 0 ) error("Could not parse: --targets-overlap %s\n",optarg); |
| 642 |
break; |
| 643 |
case 9 : args->n_threads = strtol(optarg, 0, 0); break; |
| 644 |
case 8 : args->record_cmd_line = 0; break; |
| 645 |
case 'W': |
| 646 |
if (!(args->write_index = write_index_parse(optarg))) |
| 647 |
error("Unsupported index format '%s'\n", optarg); |
| 648 |
break; |
| 649 |
case 'h': |
| 650 |
case '?': usage(); break; |
| 651 |
default: error("Unknown argument: %s\n", optarg); |
| 652 |
} |
| 653 |
} |
| 654 |
if ( argc-optind<1 && !args->file_list ) usage(); |
| 655 |
|
| 656 |
int nfiles = 0,i; |
| 657 |
char **files = NULL; |
| 658 |
if ( args->file_list ) |
| 659 |
{ |
| 660 |
files = hts_readlines(args->file_list, &nfiles); |
| 661 |
if ( !files ) error("Failed to read from %s\n", args->file_list); |
| 662 |
} |
| 663 |
if ( optind<argc ) |
| 664 |
{ |
| 665 |
int n = argc - optind; |
| 666 |
files = (char**)realloc(files,sizeof(*files)*(n+nfiles)); |
| 667 |
for (i=nfiles; i>0; i--) files[n+i-1] = files[n+i-2]; |
| 668 |
for (i=0; i<n; i++) files[i] = strdup(argv[optind+i]); |
| 669 |
nfiles += n; |
| 670 |
} |
| 671 |
|
| 672 |
if ( args->targets_list ) |
| 673 |
{ |
| 674 |
bcf_sr_set_opt(args->files,BCF_SR_TARGETS_OVERLAP,targets_overlap); |
| 675 |
if ( bcf_sr_set_targets(args->files, args->targets_list, targets_is_file,0)<0 ) |
| 676 |
error("Failed to read the targets: %s\n", args->targets_list); |
| 677 |
} |
| 678 |
if ( args->regions_list ) |
| 679 |
{ |
| 680 |
bcf_sr_set_opt(args->files,BCF_SR_REGIONS_OVERLAP,regions_overlap); |
| 681 |
if ( bcf_sr_set_regions(args->files, args->regions_list, regions_is_file)<0 ) |
| 682 |
error("Failed to read the regions: %s\n", args->regions_list); |
| 683 |
} |
| 684 |
if ( nfiles==2 && !args->isec_op ) |
| 685 |
{ |
| 686 |
args->isec_op = OP_VENN; |
| 687 |
if ( !args->prefix ) error("Expected the -p option\n"); |
| 688 |
} |
| 689 |
if ( !args->isec_op ) |
| 690 |
{ |
| 691 |
args->isec_op = OP_PLUS; |
| 692 |
args->isec_n = 1; |
| 693 |
} |
| 694 |
args->files->require_index = 1; |
| 695 |
for (i=0; i<nfiles; i++) |
| 696 |
{ |
| 697 |
if ( !bcf_sr_add_reader(args->files, files[i]) ) error("Failed to open %s: %s\n", files[i],bcf_sr_strerror(args->files->errnum)); |
| 698 |
free(files[i]); |
| 699 |
} |
| 700 |
free(files); |
| 701 |
|
| 702 |
init_data(args); |
| 703 |
isec_vcf(args); |
| 704 |
destroy_data(args); |
| 705 |
bcf_sr_destroy(args->files); |
| 706 |
free(args); |
| 707 |
return 0; |
| 708 |
} |
| 709 |
|