comparison phe/variant_filters/DepthFilter.py @ 0:834a312c0114 draft

Uploaded
author ulfschaefer
date Thu, 10 Dec 2015 09:22:39 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:834a312c0114
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:
39 logging.error("Could not retrieve threshold from %s", args.get(self.parameter))
40 self.threshold = None
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
48 try:
49 record_dp = record.samples[0].data.DP
50 except AttributeError:
51 record_dp = None
52
53 if record_dp is None:
54 # logging.debug("Falling back to INFO DP")
55 record_dp = record.INFO.get("DP")
56
57 if record_dp is None or record_dp < self.threshold:
58 return record_dp or False
59 else:
60 return None
61
62 def short_desc(self):
63 short_desc = self.__doc__ or ''
64
65 if short_desc:
66 short_desc = "%s (DP > %i)" % (short_desc, self.threshold)
67
68 return short_desc