comparison phe/variant_filters/MQFilter.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 MQ filter.
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 MQFilter(PHEFilterBase):
14 '''Filter sites by Mapping Quality (MQ) score.'''
15
16 name = "MinMQ"
17 _default_threshold = 30
18 parameter = "mq_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 MQ score (default: %s)" % self._default_threshold)
25
26 def __init__(self, args):
27 """Min Mapping Quality constructor."""
28 # This needs to happen first, because threshold is initialised here.
29 super(MQFilter, 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.mq_score
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 threshold from %s", args.get(self.parameter))
40 logging.error("This parameter requires to be an integer!")
41 raise Exception("Could not create MQ filter from parameters: %s" % args)
42
43 def __call__(self, record):
44 """Filter a :py:class:`vcf.model._Record`."""
45
46 good_record = self._check_record(record)
47
48 if good_record is not True:
49 return good_record
50
51 record_mq = record.INFO.get("MQ")
52
53 if record_mq is None or record_mq < self.threshold:
54 # FIXME: when record_mq is None, i,e, error/missing, what do you do?
55 return record_mq or False
56 else:
57 return None
58
59 def short_desc(self):
60 short_desc = self.__doc__ or ''
61
62 if short_desc:
63 short_desc = "%s (MQ > %s)" % (short_desc, self.threshold)
64
65 return short_desc