14
|
1 #!/usr/bin/python
|
|
2
|
|
3 import re
|
|
4 import sys
|
|
5 import optparse
|
|
6 import csv
|
|
7 from rpy import *
|
|
8
|
|
9 def main():
|
|
10 parser = optparse.OptionParser()
|
|
11 parser.add_option('-s', '--snp_vcf', dest = 'snp_vcf', action = 'store', type = 'string', default = None, help = "VCF of SNPs")
|
|
12 parser.add_option('-c', '--hist_color', dest = 'hist_color', action = 'store', type = 'string', default = "darkgray", help = "Color for 1Mb histograms")
|
|
13 parser.add_option('-y', '--ylim', dest = 'ylim', action = 'store', type = 'int', default= 100, help = "Upper limit of Y axis")
|
|
14 parser.add_option('-z', '--standardize', dest = 'standardize', default= 'false', help = "Standardize X-axis")
|
|
15 parser.add_option('-e', '--ems', dest = 'ems', default= 'false', help = "Whether EMS variants should be filtered for")
|
|
16 parser.add_option('-o', '--output', dest = 'plot_output', action = 'store', type = 'string', default = 'EMS_Variant_Density_Plot.pdf', help = "Output file name of plot")
|
|
17 (options, args) = parser.parse_args()
|
|
18
|
|
19
|
|
20 i, ii, iii, iv, v, x = parse_snp_vcf(snp_vcf = options.snp_vcf, ems=options.ems)
|
|
21
|
|
22 create_histograms(plot_output = options.plot_output, hist_color=options.hist_color, ylim=options.ylim, ems=options.ems, standardize=options.standardize, i = i, ii = ii, iii = iii, iv = iv, v = v, x = x)
|
|
23
|
|
24 def create_histograms(plot_output = None, hist_color=None, ylim=None, ems=None, standardize=None , i = None, ii = None, iii = None, iv = None, v = None, x = None):
|
|
25 breaks = { 'I' : 16 , 'II' : 16, 'III' : 14, 'IV' : 18, 'V' : 21, 'X' : 18 }
|
|
26
|
|
27 try:
|
|
28 r.pdf(plot_output, 8, 8)
|
|
29 if len(i) > 0:
|
|
30 plot_data(position_list = i, chr = "I", breaks = breaks["I"], hist_color=hist_color, ylim=ylim, ems=ems, standardize=standardize)
|
|
31 if len(ii) > 0:
|
|
32 plot_data(position_list = ii, chr = "II", breaks = breaks["II"], hist_color=hist_color, ylim=ylim, ems=ems, standardize=standardize)
|
|
33 if len(iii) > 0:
|
|
34 plot_data(position_list = iii, chr = "III", breaks = breaks["III"], hist_color=hist_color, ylim=ylim, ems=ems, standardize=standardize)
|
|
35 if len(iv) > 0:
|
|
36 plot_data(position_list = iv, chr = "IV", breaks = breaks["IV"], hist_color=hist_color, ylim=ylim, ems=ems, standardize=standardize)
|
|
37 if len(v) > 0:
|
|
38 plot_data(position_list = v, chr = "V", breaks = breaks["V"], hist_color=hist_color, ylim=ylim, ems=ems, standardize=standardize)
|
|
39 if len(x) > 0:
|
|
40 plot_data(position_list = x, chr = "X", breaks = breaks["X"], hist_color=hist_color, ylim=ylim, ems=ems, standardize=standardize)
|
|
41 r.dev_off()
|
|
42 except Exception as inst:
|
|
43 print inst
|
|
44 print "There was an error creating the plot pdf... Please try again"
|
|
45
|
|
46 def parse_snp_vcf(snp_vcf = None, ems=None):
|
|
47 i_file = open(snp_vcf, 'rU')
|
|
48 reader = csv.reader(i_file, delimiter = '\t', quoting = csv.QUOTE_NONE)
|
|
49 skip_headers(reader = reader, i_file = i_file)
|
|
50
|
|
51 i_position_list = []
|
|
52 ii_position_list = []
|
|
53 iii_position_list = []
|
|
54 iv_position_list = []
|
|
55 v_position_list = []
|
|
56 x_position_list = []
|
|
57
|
|
58 for row in reader:
|
|
59 chromosome = row[0].upper()
|
|
60 chromosome = re.sub("CHROMOSOME_", "", chromosome, flags = re.IGNORECASE)
|
|
61 chromosome = re.sub("chr", "", chromosome, flags = re.IGNORECASE)
|
|
62
|
|
63
|
|
64 position = row[1]
|
|
65 ref_allele = row[3]
|
|
66 alt_allele = row[4]
|
|
67
|
|
68 if (ems=='true'):
|
|
69 if (ref_allele =="G" or ref_allele =="C") and (alt_allele =="A" or alt_allele =="T"):
|
|
70 if chromosome == "I":
|
|
71 i_position_list.append(position)
|
|
72 elif chromosome == "II":
|
|
73 ii_position_list.append(position)
|
|
74 elif chromosome == "III":
|
|
75 iii_position_list.append(position)
|
|
76 elif chromosome == "IV":
|
|
77 iv_position_list.append(position)
|
|
78 elif chromosome == "V":
|
|
79 v_position_list.append(position)
|
|
80 elif chromosome == "X":
|
|
81 x_position_list.append(position)
|
|
82 elif (ems=='false'):
|
|
83 if chromosome == "I":
|
|
84 i_position_list.append(position)
|
|
85 elif chromosome == "II":
|
|
86 ii_position_list.append(position)
|
|
87 elif chromosome == "III":
|
|
88 iii_position_list.append(position)
|
|
89 elif chromosome == "IV":
|
|
90 iv_position_list.append(position)
|
|
91 elif chromosome == "V":
|
|
92 v_position_list.append(position)
|
|
93 elif chromosome == "X":
|
|
94 x_position_list.append(position)
|
|
95
|
|
96 return i_position_list, ii_position_list, iii_position_list, iv_position_list, v_position_list, x_position_list
|
|
97
|
|
98 def skip_headers(reader = None, i_file = None):
|
|
99 # count headers
|
|
100 comment = 0
|
|
101 while reader.next()[0].startswith('#'):
|
|
102 comment = comment + 1
|
|
103
|
|
104 # skip headers
|
|
105 i_file.seek(0)
|
|
106 for i in range(0, comment):
|
|
107 reader.next()
|
|
108
|
|
109 def plot_data(position_list = None, chr = None, breaks = None, hist_color=None, ylim = None, ems=None, standardize=None):
|
|
110 positions = ",".join(map(str, map(lambda x: float(x) / 1000000, position_list)))
|
|
111 positions = "c(" + positions + ")"
|
|
112
|
|
113 if (standardize=='true'):
|
|
114 r("hist(" + positions + ", xlim=c(0,21), ylim=c(0, %d "%ylim +"),col='"+ hist_color + "', breaks = seq(0, as.integer( ' " + str(breaks) + " '), by=1), main = 'LG " + chr + "', ylab = 'Frequency Of SNPs', xlab = 'Location (Mb)')")
|
|
115 r("hist(" + positions + ", xlim=c(0,21), add=TRUE, ylim=c(0, %d "%ylim +"), col=rgb(1, 0, 0, 1), breaks = seq(0, as.integer( ' " + str(breaks) + " '), by=.5), main = 'Chr " + chr + "', ylab = 'Number Of SNPs', xlab = 'Location (Mb)')")
|
|
116 r("axis(1, at=seq(0, 21, by=1), labels=FALSE, tcl=-0.5)")
|
|
117 r("axis(1, at=seq(0, 21, by=0.5), labels=FALSE, tcl=-0.25)")
|
|
118 elif (standardize=='false'):
|
|
119 r("hist(" + positions + ", xlim=c(0,as.integer( ' " + str(breaks) + " ')), ylim=c(0, %d "%ylim +"),col='"+ hist_color + "', breaks = seq(0, as.integer( ' " + str(breaks) + " '), by=1), main = 'LG " + chr + "', ylab = 'Frequency Of SNPs', xlab = 'Location (Mb)')")
|
|
120 r("hist(" + positions + ", xlim=c(0,as.integer( ' " + str(breaks) + " ')), add=TRUE, ylim=c(0, %d "%ylim +"), col=rgb(1, 0, 0, 1), breaks = seq(0, as.integer( ' " + str(breaks) + " '), by=.5), main = 'Chr " + chr + "', ylab = 'Number Of SNPs', xlab = 'Location (Mb)')")
|
|
121 r("axis(1, at=seq(0, as.integer( ' " + str(breaks) + " '), by=1), labels=FALSE, tcl=-0.5)")
|
|
122 r("axis(1, at=seq(0, as.integer( ' " + str(breaks) + " '), by=0.5), labels=FALSE, tcl=-0.25)")
|
|
123
|
|
124
|
|
125
|
|
126 if __name__ == "__main__":
|
|
127 main()
|