-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvcf_venn.py
executable file
·108 lines (79 loc) · 3.1 KB
/
vcf_venn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
import os
import sys
import logging
from collections import namedtuple
import argparse
#sys.path.insert(0, '/Users/wilma/GIS/lofreq/lofreq2-gis.git/src/lofreq_python/lofreq_star/')
# PyVCF by default
import vcf
from matplotlib import pyplot as plt
#import matplotlib
#matplotlib.use('Agg')
#import matplotlib.pyplot as plt
# https://pypi.python.org/pypi/matplotlib-venn
# http://fouryears.eu/2012/10/13/venn-diagrams-in-python/
from matplotlib_venn import venn3, venn3_circles
#global logger
# http://docs.python.org/library/logging.html
LOG = logging.getLogger("")
logging.basicConfig(level=logging.WARN,
format='%(levelname)s [%(asctime)s]: %(message)s')
VcfContainer = namedtuple('Vcf', ['name', 'file', 'text', 'vars', 'var_set'])
def cmdline_parser():
"""Returns an argparse instance
"""
# http://docs.python.org/dev/howto/argparse.html
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--verbose",
action="store_true",
help="Be verbose")
parser.add_argument("--debug",
action="store_true",
help="Enable debugging")
parser.add_argument("-o", "--plot",
required=True,
help="Output plot filename")
parser.add_argument("-v", "--vcf",
required=True,
action='append',
help="Two to three vcf files in the form vcf:name:[txt]")
return parser
def var_key(var):
# pyvcf encodes .alt not as string, ie.
return "%s-%s-%s-%s" % (
var.CHROM, var.POS, var.REF, ''.join(["%s" % k for k in var.ALT]))
def main():
"""main fcuntion
"""
parser = cmdline_parser()
args = parser.parse_args()
if args.verbose:
LOG.setLevel(logging.INFO)
if args.debug:
LOG.setLevel(logging.DEBUG)
if os.path.exists(args.plot):
LOG.fatal("Cowardly refusing to overwrite already existing file %s" % args.plot)
sys.exit(1)
variants = []
for arg in args.vcf:
s = arg.rstrip().split(':')
assert len(s)==3, ("Excpected argument format vcf:name:[txt] not found")
(vcffile, name, text) = s
print "DEBUG vcffile=%s name=%s text=%s" % (vcffile, name, text)
LOG.info("Reading %s" % vcffile)
# PyVCF can read gzip
vcfreader = vcf.VCFReader(filename=vcffile)
vars = [v for v in vcfreader if v.FILTER in [".", "PASS"] or len(v.FILTER)==0]
LOG.info("Got %d vars" % len(vars))
var_set = set(dict([(var_key(v), v) for v in vars]))
variants.append(VcfContainer(name, vcffile, text, vars, var_set))
subsets = [v.var_set for v in variants]
labels = [v.name + "\n" + v.text for v in variants]
v = venn3(subsets=subsets, set_labels=labels)
c = venn3_circles(subsets=subsets, linestyle='solid')
#plt.show()
plt.savefig(args.plot)
if __name__ == "__main__":
main()
LOG.info("Successful program exit")