annotate phe/variant/variant_factory.py @ 14:f72039c5faa4 draft

Uploaded
author ulfschaefer
date Wed, 16 Dec 2015 07:29:05 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
1 '''Classes and functions for working with variant callers.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
2
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
3 Created on 22 Sep 2015
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
4
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
5 @author: alex
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
6 '''
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
7 import glob
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
8 import inspect
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
9 import logging
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
10 import os
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
11 import sys
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
12
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
13 from phe.variant import VariantCaller
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
14
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
15 def dynamic_caller_loader():
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
16 """Fancy way of dynamically importing existing variant callers.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
17
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
18 Returns
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
19 -------
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
20 dict:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
21 Available variant callers dictionary. Keys are parameters that
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
22 can be used to call variants.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
23 """
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
24
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
25 # We assume the caller are in the same directory as THIS file.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
26 variants_dir = os.path.dirname(__file__)
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
27 variants_dir = os.path.abspath(variants_dir)
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
28
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
29 # This is populated when the module is first imported.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
30 avail_callers = {}
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
31
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
32 # Add this directory to the syspath.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
33 sys.path.insert(0, variants_dir)
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
34
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
35 # Find all "py" files.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
36 for caller_mod in glob.glob(os.path.join(variants_dir, "*.py")):
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
37
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
38 # Derive name of the module where caller is.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
39 caller_mod_file = os.path.basename(caller_mod)
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
40
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
41 # Ignore __init__ file, only base class is there.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
42 if caller_mod_file.startswith("__init__"):
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
43 continue
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
44
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
45 # Import the module with a caller.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
46 mod = __import__(caller_mod_file.replace(".pyc", "").replace(".py", ""))
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
47
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
48 # Find all the classes contained in this module.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
49 classes = inspect.getmembers(mod, inspect.isclass)
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
50 for cls_name, cls in classes:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
51 # For each class, if it is a sublass of VariantCaller, add it.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
52 if cls_name != "VariantCaller" and issubclass(cls, VariantCaller):
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
53 # The name is inherited and defined within each caller.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
54 avail_callers[cls.name] = cls
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
55
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
56 sys.path.remove(variants_dir)
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
57
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
58 return avail_callers
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
59
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
60 _avail_variant_callers = dynamic_caller_loader()
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
61
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
62 def available_callers():
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
63 """Return list of available variant callers."""
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
64 return _avail_variant_callers.keys()
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
65
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
66 def factory(variant=None, custom_options=None):
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
67 """Make an instance of a variant class.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
68
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
69 Parameters:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
70 -----------
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
71 variant: str, optional
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
72 Name of the variant class to instantiate.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
73 custom_options: str, optional
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
74 Custom options to be passed directly to the implementing class.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
75
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
76 Returns:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
77 --------
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
78 :py:class:`phe.variant.VariantCaller`:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
79 Instance of the :py:class:`phe.variant.VariantCaller` for given
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
80 variant name, or None if one couldn't be found.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
81 """
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
82 if variant is not None and isinstance(variant, str):
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
83
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
84 variant = variant.lower()
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
85 if variant in _avail_variant_callers:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
86 return _avail_variant_callers[variant](cmd_options=custom_options)
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
87 else:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
88 logging.error("No implementation for %s mapper.")
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
89 return None
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
90
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
91 logging.warn("Unknown parameters. Mapper could not be initialised.")
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
92 return None