annotate tools/regVariation/linear_regression.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 import sys, string
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 from rpy import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 import numpy
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 def stop_err(msg):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 sys.stderr.write(msg)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 infile = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 y_col = int(sys.argv[2])-1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 x_cols = sys.argv[3].split(',')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 outfile = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 outfile2 = sys.argv[5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 print "Predictor columns: %s; Response column: %d" %(x_cols,y_col+1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 fout = open(outfile,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 elems = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 for i, line in enumerate( file ( infile )):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 line = line.rstrip('\r\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 if len( line )>0 and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 elems = line.split( '\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 if i == 30:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 break # Hopefully we'll never get here...
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 if len( elems )<1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 stop_err( "The data in your input dataset is either missing or not formatted properly." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 y_vals = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 x_vals = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 for k,col in enumerate(x_cols):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 x_cols[k] = int(col)-1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 x_vals.append([])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 NA = 'NA'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 for ind,line in enumerate( file( infile )):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 if line and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 fields = line.split("\t")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 yval = float(fields[y_col])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 yval = r('NA')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 y_vals.append(yval)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 for k,col in enumerate(x_cols):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 xval = float(fields[col])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 xval = r('NA')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 x_vals[k].append(xval)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 x_vals1 = numpy.asarray(x_vals).transpose()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 dat= r.list(x=array(x_vals1), y=y_vals)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 set_default_mode(NO_CONVERSION)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 linear_model = r.lm(r("y ~ x"), data = r.na_exclude(dat))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 except RException, rex:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 stop_err("Error performing linear regression on the input data.\nEither the response column or one of the predictor columns contain only non-numeric or invalid values.")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 set_default_mode(BASIC_CONVERSION)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 coeffs=linear_model.as_py()['coefficients']
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 yintercept= coeffs['(Intercept)']
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 summary = r.summary(linear_model)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 co = summary.get('coefficients', 'NA')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 if len(co) != len(x_vals)+1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 stop_err("Stopped performing linear regression on the input data, since one of the predictor columns contains only non-numeric or invalid values.")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 yintercept = r.round(float(yintercept), digits=10)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 pvaly = r.round(float(co[0][3]), digits=10)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 print >>fout, "Y-intercept\t%s" %(yintercept)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 print >>fout, "p-value (Y-intercept)\t%s" %(pvaly)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 if len(x_vals) == 1: #Simple linear regression case with 1 predictor variable
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 slope = r.round(float(coeffs['x']), digits=10)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 slope = 'NA'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 pval = r.round(float(co[1][3]), digits=10)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 pval = 'NA'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 print >>fout, "Slope (c%d)\t%s" %(x_cols[0]+1,slope)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 print >>fout, "p-value (c%d)\t%s" %(x_cols[0]+1,pval)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 else: #Multiple regression case with >1 predictors
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 ind=1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 while ind < len(coeffs.keys()):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 slope = r.round(float(coeffs['x'+str(ind)]), digits=10)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 slope = 'NA'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 print >>fout, "Slope (c%d)\t%s" %(x_cols[ind-1]+1,slope)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 pval = r.round(float(co[ind][3]), digits=10)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 pval = 'NA'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 print >>fout, "p-value (c%d)\t%s" %(x_cols[ind-1]+1,pval)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 ind+=1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 rsq = summary.get('r.squared','NA')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 adjrsq = summary.get('adj.r.squared','NA')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 fstat = summary.get('fstatistic','NA')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 sigma = summary.get('sigma','NA')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 rsq = r.round(float(rsq), digits=5)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 adjrsq = r.round(float(adjrsq), digits=5)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 fval = r.round(fstat['value'], digits=5)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 fstat['value'] = str(fval)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 sigma = r.round(float(sigma), digits=10)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 print >>fout, "R-squared\t%s" %(rsq)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 print >>fout, "Adjusted R-squared\t%s" %(adjrsq)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 print >>fout, "F-statistic\t%s" %(fstat)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 print >>fout, "Sigma\t%s" %(sigma)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 r.pdf( outfile2, 8, 8 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 if len(x_vals) == 1: #Simple linear regression case with 1 predictor variable
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 sub_title = "Slope = %s; Y-int = %s" %(slope,yintercept)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 r.plot(x=x_vals[0], y=y_vals, xlab="X", ylab="Y", sub=sub_title, main="Scatterplot with regression")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 r.abline(a=yintercept, b=slope, col="red")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 r.pairs(dat, main="Scatterplot Matrix", col="blue")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 r.plot(linear_model)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 r.dev_off()