annotate phe/variant/__init__.py @ 22:96f393ad7fc6 draft default tip

Uploaded
author ulfschaefer
date Wed, 23 Dec 2015 04:50:58 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
1 """Classes and methods to work with _variants and such."""
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
2 import abc
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
3 from collections import OrderedDict
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
4 import gzip
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
5 import logging
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
6 import pickle
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
7
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
8 from vcf import filters
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
9 import vcf
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
10 from vcf.parser import _Filter
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
11
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
12 from phe.metadata import PHEMetaData
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
13 from phe.variant_filters import make_filters, PHEFilterBase, str_to_filters
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
14
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
15
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
16 class VCFTemplate(object):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
17 """This is a small hack class for the Template used in generating
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
18 VCF file."""
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
19
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
20 def __init__(self, vcf_reader):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
21 self.infos = vcf_reader.infos
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
22 self.formats = vcf_reader.formats
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
23 self.filters = vcf_reader.filters
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
24 self.alts = vcf_reader.alts
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
25 self.contigs = vcf_reader.contigs
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
26 self.metadata = vcf_reader.metadata
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
27 self._column_headers = vcf_reader._column_headers
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
28 self.samples = vcf_reader.samples
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
29
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
30 class VariantSet(object):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
31 """A convenient representation of set of _variants.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
32 TODO: Implement iterator and generator for the variant set.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
33 """
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
34
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
35 _reader = None
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
36
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
37 def __init__(self, vcf_in, filters=None):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
38 """Constructor of variant set.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
39
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
40 Parameters:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
41 -----------
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
42 vcf_in: str
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
43 Path to the VCF file for loading information.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
44 filters: str or dict, optional
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
45 Dictionary or string of the filter:threshold key value pairs.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
46 """
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
47 self.vcf_in = vcf_in
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
48 self._reader = vcf.Reader(filename=vcf_in)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
49 self.out_template = VCFTemplate(self._reader)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
50
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
51 self.filters = []
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
52 if filters is not None:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
53 if isinstance(filters, str):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
54 self.filters = str_to_filters(filters)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
55 elif isinstance(filters, dict):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
56 self.filters = make_filters(config=filters)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
57 elif isinstance(filters, list):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
58 self.filters = filters
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
59 else:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
60 logging.warn("Could not create filters from %s", filters)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
61 else:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
62 reader = vcf.Reader(filename=self.vcf_in)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
63 filters = {}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
64 for filter_id in reader.filters:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
65 filters.update(PHEFilterBase.decode(filter_id))
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
66
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
67 if filters:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
68 self.filters = make_filters(config=filters)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
69
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
70 self._variants = []
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
71
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
72 def __iter__(self):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
73 '''Iterator over **all** variants'''
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
74 return self.variants(only_good=False)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
75
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
76 def variants(self, only_good=False):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
77 '''Generator over the variant set.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
78 Parameters:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
79 -----------
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
80 only_good: bool, optional
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
81 Iff True good and bad variants are returned,
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
82 otherwise only good are returned (default: False).
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
83 '''
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
84 for var in self._variants:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
85 if not only_good:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
86 yield var
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
87 elif not var.FILTER :
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
88 yield var
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
89
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
90 def filter_variants(self, keep_only_snps=True):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
91 """Create a variant """
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
92
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
93 if self._reader is None:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
94 # Create a reader class from input VCF.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
95 self._reader = vcf.Reader(filename=self.vcf_in)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
96
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
97 # get list of existing filters.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
98 existing_filters = {}
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
99 removed_filters = []
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
100
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
101 for filter_id in self._reader.filters:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
102 conf = PHEFilterBase.decode(filter_id)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
103 tuple(conf.keys())
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
104 existing_filters.update({tuple(conf.keys()):filter_id})
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
105
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
106 # Add each filter we are going to use to the record.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
107 # This is needed for writing out proper #FILTER header in VCF.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
108 for record_filter in self.filters:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
109 # We know that each filter has short description method.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
110 short_doc = record_filter.short_desc()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
111 short_doc = short_doc.split('\n')[0].lstrip()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
112
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
113 filter_name = PHEFilterBase.decode(record_filter.filter_name())
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
114
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
115 # Check if the sample has been filtered for this type of filter
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
116 # in the past. If so remove is, because it is going to be refiltered.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
117 if tuple(filter_name) in existing_filters:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
118 logging.info("Removing existing filter: %s", existing_filters[tuple(filter_name)])
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
119 removed_filters.append(existing_filters[tuple(filter_name)])
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
120 del self._reader.filters[existing_filters[tuple(filter_name)]]
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
121
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
122 self._reader.filters[record_filter.filter_name()] = _Filter(record_filter.filter_name(), short_doc)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
123
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
124 # For each record (POSITION) apply set of filters.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
125 for record in self._reader:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
126
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
127 # If this record failed filters and we removed some,
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
128 # check is they need to be removed from record.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
129 if isinstance(record.FILTER, list) and len(record.FILTER) > 0:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
130 for filter_id in removed_filters:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
131 if filter_id in record.FILTER:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
132 record.FILTER.remove(filter_id)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
133
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
134 for record_filter in self.filters:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
135
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
136 # Call to __call__ method in each filter.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
137 result = record_filter(record)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
138
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
139 # Record is KEPT if filter returns None
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
140 if result is None:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
141 continue
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
142
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
143 # If we got this far, then record is filtered OUT.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
144 record.add_filter(record_filter.filter_name())
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
145
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
146 # After applying all filters, check if FILTER is None.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
147 # If it is, then record PASSED all filters.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
148 if record.FILTER is None or record.FILTER == []:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
149 record.FILTER = []
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
150 # FIXME: Does this work for indels?
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
151 if keep_only_snps and record.is_snp:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
152 self._variants.append(record)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
153 else:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
154 self._variants.append(record)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
155
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
156 self._update_filters(self._reader.filters)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
157
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
158 return [ variant for variant in self._variants if not variant.FILTER]
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
159
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
160 def add_metadata(self, info):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
161 """Add metadata to the variant set.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
162
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
163 Parameters:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
164 -----------
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
165 info: dict
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
166 Dictionary of key value pairs to be inserted into metadata.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
167 """
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
168 for info_key, metadata in info.items():
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
169 self.out_template.metadata[info_key] = metadata
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
170
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
171 def write_variants(self, vcf_out, only_snps=False, only_good=False):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
172 """Write _variants to a VCF file.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
173
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
174 Parameters:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
175 -----------
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
176 vcf_out: str
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
177 Path to the file where VCF data is written.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
178 only_snps: bool, optional
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
179 True is *only* SNP are to be written to the output (default: False).
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
180 only_good: bool, optional
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
181 True if only those records that PASS all filters should be written
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
182 (default: False).
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
183
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
184 Returns:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
185 int:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
186 Number of records written.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
187 """
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
188 written_variants = 0
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
189
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
190 # Check if the output file ends with .gz, then compress data.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
191 open_func = gzip.open if vcf_out.endswith(".gz") else open
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
192
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
193 with open_func(vcf_out, "w") as out_vcf:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
194 writer = vcf.Writer(out_vcf, self.out_template)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
195
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
196 # Output internal _variants (if exist) otherwise, output data from reader.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
197 variants = self._variants if self._variants else self._reader
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
198
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
199 for record in variants:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
200
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
201 if only_snps and not record.is_snp:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
202 continue
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
203
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
204 if only_good and record.FILTER:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
205 continue
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
206
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
207 writer.write_record(record)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
208 written_variants += 1
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
209
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
210 return written_variants
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
211
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
212 def _write_bad_variants(self, vcf_out):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
213 """**PRIVATE:** Write only those records that **haven't** passed."""
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
214 written_variants = 0
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
215 # Check if the output file ends with .gz, then compress data.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
216 open_func = gzip.open if vcf_out.endswith(".gz") else open
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
217 with open_func(vcf_out, "w") as out_vcf:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
218 writer = vcf.Writer(out_vcf, self.out_template)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
219 for record in self._variants:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
220 if record.FILTER != "PASS" and record.FILTER is not None:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
221 writer.write_record(record)
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
222 written_variants += 1
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
223 return written_variants
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
224
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
225 def _update_filters(self, new_filters):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
226 """Update internal filters in the output template."""
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
227 for new_filter, filter_data in new_filters.items():
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
228 self.out_template.filters[new_filter] = filter_data
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
229
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
230
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
231 class VariantCaller(PHEMetaData):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
232 """Abstract class used for access to the implemented variant callers."""
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
233
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
234 __metaclass__ = abc.ABCMeta
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
235
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
236 def __init__(self, cmd_options=None):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
237 """Constructor for variant caller.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
238
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
239 Parameters:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
240 -----------
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
241 cmd_options: str, optional
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
242 Command options to pass to the variant command.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
243 """
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
244 self.cmd_options = cmd_options
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
245
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
246 super(VariantCaller, self).__init__()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
247
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
248 @abc.abstractmethod
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
249 def make_vcf(self, *args, **kwargs):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
250 """Create a VCF from **BAM** file.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
251
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
252 Parameters:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
253 -----------
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
254 ref: str
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
255 Path to the reference file.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
256 bam: str
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
257 Path to the indexed **BAM** file for calling _variants.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
258 vcf_file: str
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
259 path to the VCF file where data will be written to.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
260 make_aux: bool, optional
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
261 True/False create auxilliary files (default: False).
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
262
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
263 Returns:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
264 --------
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
265 bool:
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
266 True if variant calling was successful, False otherwise.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
267 """
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
268 raise NotImplementedError("make_vcf is not implemented yet.")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
269
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
270 @abc.abstractmethod
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
271 def create_aux_files(self, ref):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
272 """Create needed (if any) auxiliary files.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
273 These files are required for proper functioning of the variant caller.
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
274 """
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
275 raise NotImplementedError("create_aux_files is not implemeted.")
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
276
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
277 @abc.abstractmethod
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
278 def get_info(self, plain=False):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
279 """Get information about this variant caller."""
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
280 raise NotImplementedError("Get info has not been implemented yet."
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
281 )
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
282 def get_meta(self):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
283 """Get the metadata about this variant caller."""
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
284 od = self.get_info()
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
285 od["ID"] = "VariantCaller"
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
286 return OrderedDict({"PHEVariantMetaData": [od]})
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
287
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
288 @abc.abstractmethod
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
289 def get_version(self):
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
290 """Get the version of the underlying command used."""
96f393ad7fc6 Uploaded
ulfschaefer
parents:
diff changeset
291 raise NotImplementedError("Get version has not been implemented yet.")