0
|
1 #!/usr/bin/env python
|
2
|
2 # New in this version:
|
6
|
3 # - Add in proper header line if not present
|
0
|
4
|
|
5 import os,sys,numpy
|
|
6 from rpy2.robjects import Formula
|
|
7 from rpy2.robjects.packages import importr
|
|
8 from rpy2 import robjects
|
|
9
|
|
10 def fail(message):
|
|
11 sys.stderr.write(message+'\n')
|
|
12 sys.exit(1)
|
|
13
|
6
|
14 COLUMN_LABELS = ['SAMPLE', 'CHR', 'POS', 'A', 'C', 'G', 'T', 'CVRG', 'ALLELES', 'MAJOR', 'MINOR', 'MINOR.FREQ.PERC.'] #, 'STRAND.BIAS']
|
|
15
|
0
|
16 args = sys.argv[1:]
|
|
17 if len(args) >= 1:
|
|
18 infile = args[0]
|
|
19 else:
|
|
20 fail('Error: No input filename provided (as argument 1).')
|
|
21 if len(args) >= 2:
|
|
22 outfile = args[1]
|
|
23 else:
|
|
24 fail('Error: No output filename provided (as argument 2).')
|
|
25 if len(args) >= 3:
|
|
26 report = args[2]
|
4
|
27 else:
|
|
28 report = ''
|
0
|
29
|
2
|
30 # Check input file
|
6
|
31 add_header = False
|
0
|
32 if not os.path.exists(infile):
|
|
33 fail('Error: Input file '+infile+' could not be found.')
|
2
|
34 with open(infile, 'r') as lines:
|
|
35 line = lines.readline()
|
|
36 if not line:
|
|
37 fail('Error: Input file seems to be empty')
|
|
38 line = line.strip().lstrip('#') # rm whitespace, comment chars
|
|
39 labels = line.split("\t")
|
|
40 if 'SAMPLE' not in labels or labels[11] != 'MINOR.FREQ.PERC.':
|
6
|
41 sys.stderr.write("Error: Input file does not seem to have a proper header "
|
|
42 +"line.\nAdding an artificial header..")
|
|
43 add_header = True
|
|
44
|
0
|
45
|
|
46
|
|
47 utils = importr('utils')
|
|
48 graphics = importr('graphics')
|
|
49 base = importr('base')
|
|
50 stats = importr('stats')
|
|
51 rprint = robjects.globalenv.get("print")
|
|
52 grdevices = importr('grDevices')
|
|
53 grdevices.png(file=outfile, width=1024, height=768)
|
|
54
|
|
55 # Read file into a data frame
|
6
|
56 if add_header:
|
|
57 # add header line manually if not present
|
|
58 DATA = utils.read_delim(infile, header=False)
|
|
59 labels = robjects.r.names(DATA)
|
|
60 for i in range(len(labels)):
|
|
61 try:
|
|
62 labels[i] = COLUMN_LABELS[i]
|
|
63 except IndexError, e:
|
|
64 fail("Error in input file: Too many columns (does not match hardcoded "
|
|
65 +"column labels).")
|
|
66 else:
|
|
67 DATA = utils.read_delim(infile)
|
|
68 # Remove comment from header, if present
|
|
69 labels = robjects.r.names(DATA)
|
|
70 if labels[0][0:2] == 'X.':
|
|
71 labels[0] = labels[0][2:]
|
0
|
72 # Multiply minor allele frequencies by 100 to get percentage
|
2
|
73 # .rx2() looks up a column by its label and returns it as a vector
|
|
74 # .ro turns the returned object into one that can be operated on per-element
|
|
75 minor_freq = DATA.rx2('MINOR.FREQ.PERC.').ro * 100
|
|
76 samples = DATA.rx2('SAMPLE')
|
0
|
77 # Formula() creates a Python object representing the R object returned by x ~ y
|
2
|
78 formula = Formula('minor_freq ~ samples')
|
0
|
79 # The "environment" in .getenvironment() is the entire R workspace in which the
|
|
80 # Formula object exists. The R workspace meaning all the defined variables.
|
|
81 # Here, the .getenvironment() method is being used to set some variables in the
|
|
82 # R workspace
|
2
|
83 formula.getenvironment()['minor_freq'] = minor_freq
|
|
84 formula.getenvironment()['samples'] = samples
|
0
|
85
|
|
86 # create boxplot - fill kwargs1 with the options for the boxplot function
|
6
|
87 kwargs1 = {'ylab':"Minor allele frequency (%)", 'col':"gray", 'xaxt':"n", 'outpch':"*",'main':"Distribution of minor allele frequencies", 'cex.lab':"1.5"}
|
0
|
88 p = graphics.boxplot(formula, **kwargs1)
|
|
89
|
2
|
90 table = base.table(DATA.rx2('SAMPLE'))
|
|
91 graphics.text(0.5, 1, 'N:', font=2)
|
|
92 for i in range(1, base.length(table)[0]+1, 1):
|
|
93 graphics.text(i, 1, table[i-1], font=2)
|
0
|
94
|
2
|
95 graphlabels = base.names(table)
|
0
|
96 kwargs3 = {'pos':"0", 'las':"2", 'cex.axis':"1"}
|
2
|
97 graphics.axis(1, at=range(1, len(graphlabels)+1, 1), labels=graphlabels, **kwargs3)
|
0
|
98 grdevices.dev_off()
|
|
99
|
4
|
100 if not report:
|
0
|
101 sys.exit(0)
|
|
102
|
2
|
103
|
0
|
104 ####################################
|
|
105 # GENERATE REPORT
|
|
106 # report should be something like:
|
|
107 # SAMPLE NoHET MEDIAN MAD TEST
|
|
108 # s1 7 10% n p/w/f
|
|
109 # n <= 5 pass
|
|
110 # 6 <= n <=10 warn
|
|
111 # n >= 11 fail
|
|
112 # MAD <= 2.0 fail
|
|
113 # MAD > 2.0 pass
|
|
114 ###################################
|
|
115
|
|
116 SAMPLES=[]
|
4
|
117 for i in range(len(table)):
|
|
118 SAMPLES.append(base.names(table)[i])
|
0
|
119
|
|
120 def boxstats(data,sample):
|
|
121 VALUES = [100*float(x.strip().split('\t')[11]) for x in list(open(data)) if x.strip().split('\t')[0]==sample]
|
|
122 NoHET = len(VALUES)
|
|
123 MEDIAN = numpy.median(VALUES)
|
|
124 MAD = numpy.median([abs(i - MEDIAN) for i in VALUES]) # Median absolute distance (robust spread statistic)
|
|
125 return [NoHET,MEDIAN, MAD]
|
|
126
|
|
127 boxreport = open(report, "w+")
|
|
128 boxreport.write("SAMPLE\tTOTAL.SITES\tMEDIAN.FREQ.\tMAD.FREQ\tEVAL\n")
|
|
129 for sample in SAMPLES:
|
|
130 ENTRY = [sample] + boxstats(infile,sample)
|
|
131 if ENTRY[1] <= 5:
|
|
132 ENTRY.append('pass')
|
|
133 elif 6 <= ENTRY[1] <=10:
|
|
134 ENTRY.append('warn')
|
|
135 elif ENTRY[1] >= 11:
|
|
136 ENTRY.append('fail')
|
|
137 if ENTRY[3] <=2.0:
|
|
138 ENTRY.append('fail')
|
|
139 elif ENTRY[3] >2.0:
|
|
140 ENTRY.append('pass')
|
|
141 if len(set(ENTRY[4:6])) == 2:
|
|
142 ENTRY.append('warn')
|
|
143 else:
|
|
144 ENTRY.append(list(set(ENTRY[4:6]))[0])
|
|
145 boxreport.write ('%s\t%d\t%.1f\t%.1f\t%s\n' % tuple([ENTRY[i] for i in [0,1,2,3,6]]))
|
|
146
|
|
147 boxreport.close()
|
|
148
|
|
149
|
|
150
|
|
151
|
|
152
|
|
153
|
|
154 #####################################
|
|
155 #STUFF
|
|
156 #
|
|
157 #kwargs = {'ylab':"Minor allele frequency (sites >= 2%)",'col':"blue", 'axes':"FALSE", 'outpch':"*", 'ylim':"c(-0.03,0.5)", 'main':"Minor allele frequencies run020 at 2%"}
|
|
158 #boxplot(freq~id,data=run020[run020$freq>=0.02,], col="blue", axes=FALSE, outpch="*", ylab="Minor allele frequency (sites >= 2%)", ylim=c(-0.03,0.5), main="Minor allele frequencies run020 at 2%")
|
|
159 #biglabels2=sort(as.vector(unique(run020$id)))
|
|
160 #biglabels=apply(as.matrix(biglabels2), 1,function(x) substr(x,1,5))
|
|
161 #axis(1, at=c(1:length(biglabels)),labels=biglabels, pos=-0.02, las=2, cex.axis=0.9)
|
|
162 #axis(2, at=c(seq(0,50,5)/100), pos=0,cex.axis=0.9)
|
|
163 #nbig=as.vector(table(run020[run020$freq>=0.02,1]))
|
|
164 #for (i in 1:length(nbig)){text(i,-0.01,nbig[i],cex=0.9, font=2)}
|
|
165
|