# HG changeset patch # User ulfschaefer # Date 1450714339 18000 # Node ID cd59be4a7fe32a8a3ad2dd2a08dbe33fe07a624e # Parent c2f8e7580133eacbcb5c6242122e6acae0dd5db6 Uploaded diff -r c2f8e7580133 -r cd59be4a7fe3 filter_vcf.py --- a/filter_vcf.py Mon Dec 21 10:50:17 2015 -0500 +++ b/filter_vcf.py Mon Dec 21 11:12:19 2015 -0500 @@ -6,6 +6,8 @@ @author: alex ''' import argparse +import logging +import yaml from phe.variant import VariantSet @@ -15,20 +17,47 @@ args = argparse.ArgumentParser() args.add_argument("--vcf", "-v", required=True, help="VCF file to (re)filter.") - args.add_argument("--filters", "-f", required=True, help="Filter(s) to apply as key:threshold pairs, separated by comma.") + + group = args.add_mutually_exclusive_group() + + group.add_argument("--filters", "-f", help="Filter(s) to apply as key:threshold pairs, separated by comma.") + group.add_argument("--config", "-c", help="Config with filters in YAML format. E.g.filters:-key:value") + args.add_argument("--output", "-o", required=True, help="Location for filtered VCF to be written.") + args.add_argument("--only-good", action="store_true", default=False, help="Write only variants that PASS all filters (default all variants are written).") + + args.add_argument("--debug", action="store_true", default=False, help="Make output more verbose.") + return args.parse_args() +def load_config(config_path): + with open(config_path) as fp: + config = yaml.load(fp) + + return config.get("filters", {}) def main(): args = get_args() + log_level = logging.DEBUG if args.debug else logging.INFO + logging.basicConfig(format="[%(asctime)s] %(levelname)s: %(message)s", + level=log_level) + + if args.config is not None: + args.filters = load_config(args.config) + elif args.filters is None: + logging.error("Either --config or --filters needs to be specified.") + return 1 + var_set = VariantSet(args.vcf, filters=args.filters) - var_set.filter_variants() + if args.filters: + var_set.filter_variants() - var_set.serialise(args.output) + var_set.write_variants(args.output, only_good=args.only_good) + + if __name__ == '__main__': exit(main())