comparison phe/variant_filters/GTFilter.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 GT filter parameter.
2
3 Created on 24 Sep 2015
4
5 @author: alex
6 '''
7
8 import argparse
9 import logging
10
11 from phe.variant_filters import PHEFilterBase
12
13
14 class UncallableGTFilter(PHEFilterBase):
15 '''Filter uncallable genotypes'''
16
17 name = "UncallGT"
18 _default_threshold = None
19 parameter = "uncall_gt"
20
21 @classmethod
22 def customize_parser(self, parser):
23 arg_name = self.parameter.replace("_", "-")
24 parser.add_argument("--%s" % arg_name, type=str, default=self._default_threshold,
25 help="Filter sites below given GQ score (default: %s)" % self._default_threshold)
26
27 def __init__(self, args):
28 """Min Depth constructor."""
29 # This needs to happen first, because threshold is initialised here.
30 super(UncallableGTFilter, self).__init__(args)
31
32 # Change the threshold to custom gq value.
33 self.threshold = self._default_threshold
34 if isinstance(args, argparse.Namespace):
35 self.threshold = args.gq_score
36 elif isinstance(args, dict):
37 try:
38 self.threshold = str(args.get(self.parameter))
39 except (TypeError, ValueError):
40 logging.error("Could not retrieve threshold from %s", args.get(self.parameter))
41 logging.error("This parameter requires to be a string!")
42 self.threshold = None
43
44 def __call__(self, record):
45 """Filter a :py:class:`vcf.model._Record`."""
46
47 if len(record.samples) > 1:
48 logging.warn("More than 1 sample detected. Only first is considered.")
49
50 try:
51 record_gt = record.samples[0].data.GT
52 except AttributeError:
53 logging.warn("Could not retrieve GQ score POS %i", record.POS)
54 record_gt = None
55
56 if record_gt is None:
57 return "./."
58 else:
59 return None
60
61 def short_desc(self):
62 short_desc = self.__doc__ or ''
63
64 if short_desc:
65 short_desc = "%s (GT != ./. )" % (short_desc)
66
67 return short_desc
68
69 def is_gap(self):
70 return True
71
72 def is_n(self):
73 return False