Mercurial > repos > ulfschaefer > filter_vcf
comparison phe/variant_filters/GTFilter.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 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: | |
40 logging.error("Could not retrieve threshold from %s", args.get(self.parameter)) | |
41 self.threshold = None | |
42 | |
43 def __call__(self, record): | |
44 """Filter a :py:class:`vcf.model._Record`.""" | |
45 | |
46 if len(record.samples) > 1: | |
47 logging.warn("More than 1 sample detected. Only first is considered.") | |
48 | |
49 try: | |
50 record_gt = record.samples[0].data.GT | |
51 except AttributeError: | |
52 logging.error("Could not retrieve GQ score POS %i", record.POS) | |
53 record_gt = None | |
54 | |
55 if record_gt is None: | |
56 return "./." | |
57 else: | |
58 return None | |
59 | |
60 def short_desc(self): | |
61 short_desc = self.__doc__ or '' | |
62 | |
63 if short_desc: | |
64 short_desc = "%s (GT != ./. )" % (short_desc) | |
65 | |
66 return short_desc | |
67 | |
68 def is_gap(self): | |
69 return True | |
70 | |
71 def is_n(self): | |
72 return False |