annotate phe/variant/__init__.py @ 20:7ac17b6d031e draft

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