annotate EMS_VariantDensityMapping.py @ 3:2e236e914815

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