annotate phe/variant/GATKVariantCaller.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 '''
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
2 Created on 22 Sep 2015
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
3
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
4 @author: alex
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
5 '''
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
6 from collections import OrderedDict
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
7 import logging
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
8 import os
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
9 import subprocess
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
10
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
11 from phe.variant import VariantCaller
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
12
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
13
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
14 class GATKVariantCaller(VariantCaller):
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
15 """Implemetation of the Broad institute's variant caller."""
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
16
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
17 name = "gatk"
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
18 """Plain text name of the variant caller."""
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
19
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
20 _default_options = "--sample_ploidy 2 --genotype_likelihoods_model BOTH -rf BadCigar -out_mode EMIT_ALL_SITES -nt 1"
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
21 """Default options for the variant caller."""
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
22
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
23 def __init__(self, cmd_options=None):
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
24 """Constructor"""
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
25 if cmd_options is None:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
26 cmd_options = self._default_options
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
27
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
28 super(GATKVariantCaller, self).__init__(cmd_options=cmd_options)
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
29
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
30 self.last_command = None
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
31
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
32 def get_info(self, plain=False):
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
33 d = {"name": "gatk", "version": self.get_version(), "command": self.last_command}
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
34
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
35 if plain:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
36 result = "GATK(%(version)s): %(command)s" % d
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
37 else:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
38 result = OrderedDict(d)
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
39
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
40 return result
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
41
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
42 def get_version(self):
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
43
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
44 p = subprocess.Popen(["java", "-jar", os.environ["GATK_JAR"], "-version"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
45 (output, _) = p.communicate()
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
46
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
47 # last character is EOL.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
48 version = output.split("\n")[-2]
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
49
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
50 return version
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
51
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
52 def make_vcf(self, *args, **kwargs):
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
53 ref = kwargs.get("ref")
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
54 bam = kwargs.get("bam")
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
55
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
56 if kwargs.get("vcf_file") is None:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
57 kwargs["vcf_file"] = "variants.vcf"
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
58
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
59 opts = {"ref": os.path.abspath(ref),
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
60 "bam": os.path.abspath(bam),
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
61 "gatk_jar": os.environ["GATK_JAR"],
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
62 "all_variants_file": os.path.abspath(kwargs.get("vcf_file")),
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
63 "extra_cmd_options": self.cmd_options}
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
64
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
65 # if not self.create_aux_files(ref):
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
66 # logging.warn("Auxiliary files were not created.")
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
67 # return False
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
68
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
69 # Call variants
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
70 # FIXME: Sample ploidy = 2?
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
71 os.environ["GATK_JAR"]
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
72 cmd = "java -XX:+UseSerialGC -jar %(gatk_jar)s -T UnifiedGenotyper -R %(ref)s -I %(bam)s -o %(all_variants_file)s %(extra_cmd_options)s" % opts
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
73 success = os.system(cmd)
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
74
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
75 if success != 0:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
76 logging.warn("Calling variants returned non-zero exit status.")
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
77 return False
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
78
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
79 self.last_command = cmd
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
80
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
81 return True
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
82
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
83 def create_aux_files(self, ref):
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
84 """Create auxiliary files needed for this variant.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
85
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
86 Tools needed: samtools and picard tools. Picard tools is a Java
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
87 library that can be defined using environment variable: PICARD_JAR
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
88 specifying path to picard.jar or PICARD_TOOLS_PATH specifying path
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
89 to the directory where separate jars are (older version before jars
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
90 were merged into a single picard.jar).
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
91 Parameters:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
92 -----------
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
93 ref: str
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
94 Path to the reference file.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
95
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
96 Returns:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
97 --------
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
98 bool:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
99 True if auxiliary files were created, False otherwise.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
100 """
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
101
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
102 ref_name, _ = os.path.splitext(ref)
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
103
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
104 success = os.system("samtools faidx %s" % ref)
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
105
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
106 if success != 0:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
107 logging.warn("Fasta index could not be created.")
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
108 return False
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
109
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
110 d = {"ref": ref, "ref_name": ref_name}
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
111
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
112 if os.environ.get("PICARD_TOOLS_PATH"):
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
113 d["picard_tools_path"] = os.path.join(os.environ["PICARD_TOOLS_PATH"], "CreateSequenceDictionary.jar")
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
114 elif os.environ.get("PICARD_JAR"):
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
115 # This is used in newer version of PICARD tool where multiple
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
116 # jars were merged into a single jar file.
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
117 d["picard_tools_path"] = "%s %s" % (os.environ["PICARD_JAR"], "CreateSequenceDictionary")
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
118 else:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
119 logging.error("Picard tools are not present in the path.")
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
120 return False
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
121
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
122 success = os.system("java -jar %(picard_tools_path)s R=%(ref)s O=%(ref_name)s.dict" % d)
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
123
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
124 if success != 0:
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
125 logging.warn("Dictionary for the %s reference could not be created", ref)
f72039c5faa4 Uploaded
ulfschaefer
parents:
diff changeset
126 return False