Mercurial > repos > ulfschaefer > filter_vcf
comparison phe/variant_filters/GQFilter.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 GQ parameter. | |
2 Created on 24 Sep 2015 | |
3 | |
4 @author: alex | |
5 ''' | |
6 | |
7 import argparse | |
8 import logging | |
9 | |
10 from phe.variant_filters import PHEFilterBase | |
11 | |
12 | |
13 class GQFilter(PHEFilterBase): | |
14 '''Filter sites by GQ score.''' | |
15 | |
16 name = "MinGQ" | |
17 _default_threshold = 0 | |
18 parameter = "gq_score" | |
19 | |
20 @classmethod | |
21 def customize_parser(self, parser): | |
22 arg_name = self.parameter.replace("_", "-") | |
23 parser.add_argument("--%s" % arg_name, type=int, default=self._default_threshold, | |
24 help="Filter sites below given GQ score (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(GQFilter, self).__init__(args) | |
30 | |
31 # Change the threshold to custom gq value. | |
32 self.threshold = self._default_threshold | |
33 if isinstance(args, argparse.Namespace): | |
34 self.threshold = args.gq_score | |
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 not record.is_snp: | |
46 return None | |
47 | |
48 if len(record.samples) > 1: | |
49 logging.warn("More than 1 sample detected. Only first is considered.") | |
50 | |
51 try: | |
52 record_gq = record.samples[0].data.GQ | |
53 except AttributeError: | |
54 logging.error("Could not retrieve GQ score POS %i", record.POS) | |
55 record_gq = None | |
56 | |
57 if record_gq is None or record_gq < self.threshold: | |
58 # FIXME: when record_gq is None, i,e, error, what do you do? | |
59 return record_gq 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 (GQ > %s)" % (short_desc, self.threshold) | |
68 | |
69 return short_desc |