Mercurial > repos > ulfschaefer > filter_vcf
comparison phe/variant_filters/DepthFilter.py @ 10:c2f8e7580133 draft
Uploaded
author | ulfschaefer |
---|---|
date | Mon, 21 Dec 2015 10:50:17 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:2e3115b4df74 | 10:c2f8e7580133 |
---|---|
1 '''Filter VCF on depth of coverage. | |
2 | |
3 Created on 24 Sep 2015 | |
4 | |
5 @author: alex | |
6 ''' | |
7 import argparse | |
8 import logging | |
9 | |
10 from phe.variant_filters import PHEFilterBase | |
11 | |
12 | |
13 class DepthFilter(PHEFilterBase): | |
14 """Filter sites by depth.""" | |
15 | |
16 name = "MinDepth" | |
17 _default_threshold = 5 | |
18 parameter = "min_depth" | |
19 | |
20 @classmethod | |
21 def customize_parser(self, parser): | |
22 arg_name = self.parameter.replace("_", "-") | |
23 parser.add_argument("--" % arg_name, type=int, default=self._default_threshold, | |
24 help="Filter sites below minimum depth (default: %s)" % self._default_threshold) | |
25 | |
26 def __init__(self, args): | |
27 """Min Depth constructor.""" | |
28 # This needs to happen first, because threshold is initialised here. | |
29 super(DepthFilter, self).__init__(args) | |
30 | |
31 # Change the threshold to custom dp value. | |
32 self.threshold = self._default_threshold | |
33 if isinstance(args, argparse.Namespace): | |
34 self.threshold = args.min_depth | |
35 elif isinstance(args, dict): | |
36 try: | |
37 self.threshold = int(args.get(self.parameter)) | |
38 except (TypeError, ValueError): | |
39 logging.error("Could not retrieve integer threshold from %s", args.get(self.parameter)) | |
40 raise Exception("Could not create depth filter from parameters: %s" % args) | |
41 | |
42 def __call__(self, record): | |
43 """Filter a :py:class:`vcf.model._Record`.""" | |
44 | |
45 if len(record.samples) > 1: | |
46 logging.warn("Currently we only filter VCFs with 1 sample. Only first sample will be used.") | |
47 logging.warn("This parameter requires to be an integer!") | |
48 | |
49 try: | |
50 record_dp = record.samples[0].data.DP | |
51 except AttributeError: | |
52 record_dp = None | |
53 | |
54 if record_dp is None: | |
55 # logging.debug("Falling back to INFO DP") | |
56 record_dp = record.INFO.get("DP") | |
57 | |
58 if record_dp is None or record_dp < self.threshold: | |
59 return record_dp or False | |
60 else: | |
61 return None | |
62 | |
63 def short_desc(self): | |
64 short_desc = self.__doc__ or '' | |
65 | |
66 if short_desc: | |
67 short_desc = "%s (DP > %i)" % (short_desc, self.threshold) | |
68 | |
69 return short_desc |