Created
May 9, 2012 23:22
-
-
Save lennax/2649676 to your computer and use it in GitHub Desktop.
Skeleton for a genomic variant interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Variant(object): | |
"""This is the top level class for genomic variants""" | |
def __init__(self, filename, filetype=None, gvfreader=None, vcfreader=None): | |
""" | |
Uses the default parser Wrappers unless otherwise specified. | |
Currently supported arguments to filetype are "vcf" and "gvf". | |
If filetype is not specified, guessed from file extension. | |
""" | |
self.filename = filename | |
if filetype is not None: | |
self.filetype = filetype | |
else: | |
guess_type = self._guess_filetype() # returns 0 for unknown | |
if filetype: | |
self.filetype = filetype | |
else: | |
raise RuntimeError("Unknown filetype, please specify.") | |
# Use either provided parser instance or instantiate default | |
default_vcfreader = PyVCFWrapper | |
default_gvfreader = BCBioGFFWrapper | |
if self.filetype = "vcf": | |
if vcfreader is not None: | |
self.parser = vcfreader | |
else: | |
self.parser = default_vcfreader() | |
elif self.filetype = "gvf": | |
if gvfreader is not None: | |
self.parser = gvfreader | |
else: | |
self.parser = default_gvfreader() | |
else: | |
raise RuntimeError('Given filetype is not valid.') | |
def parse(self): | |
# Make instance of parser wrapper | |
self.parser.parse(self.filename) | |
def _guess_filetype(self): | |
"""Guess filetype based on extension, returns 0 for unknown""" | |
# TODO use os.path to determine file extension | |
return 0 | |
class Parse(object): | |
"""Abstract interface class, parser wrappers should inherit from this""" | |
def __init__(self): | |
"""Default constructor for abstract interface class""" | |
pass | |
def parse(self): | |
raise NotImplementedError | |
class PyVCFWrapper(Parse): | |
"""Wrapper for PyVCF""" | |
def __init__(self, filename): | |
Parse.__init__(self) | |
self.filename = filename | |
def parse(self): | |
"""Parse the file""" | |
PyVCF.vcf.Reader(self.filename) | |
class BCBioGFFWrapper(Parse): | |
"""Wrapper for BCBio.GFF""" | |
def __init__(self, filename): | |
Parse.__init__(self) | |
self.filename = filename | |
def parse(self): | |
"""Parse the file""" | |
with open(self.filename) as fh: | |
BCBio.GFF.parse(fh) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment