annotate tools/regVariation/rcve.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
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 import sys, string
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 from rpy import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 import numpy
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 def stop_err(msg):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 sys.stderr.write(msg)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 def sscombs(s):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 if len(s) == 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 return [s]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 ssc = sscombs(s[1:])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 return [s[0]] + [s[0]+comb for comb in ssc] + ssc
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 infile = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 y_col = int(sys.argv[2])-1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 x_cols = sys.argv[3].split(',')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 outfile = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 print "Predictor columns: %s; Response column: %d" %(x_cols,y_col+1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 fout = open(outfile,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 for i, line in enumerate( file ( infile )):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 line = line.rstrip('\r\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 if len( line )>0 and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 elems = line.split( '\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 if i == 30:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 break # Hopefully we'll never get here...
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 if len( elems )<1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 stop_err( "The data in your input dataset is either missing or not formatted properly." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 y_vals = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 x_vals = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 for k,col in enumerate(x_cols):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 x_cols[k] = int(col)-1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 x_vals.append([])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 float( elems[x_cols[k]] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 msg = "This operation cannot be performed on non-numeric column %d containing value '%s'." %( col, elems[x_cols[k]] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 msg = "This operation cannot be performed on non-numeric data."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 stop_err( msg )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 NA = 'NA'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 for ind,line in enumerate( file( infile )):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 if line and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 fields = line.split("\t")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 yval = float(fields[y_col])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 except Exception, ey:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 yval = r('NA')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 #print >>sys.stderr, "ey = %s" %ey
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 y_vals.append(yval)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 for k,col in enumerate(x_cols):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 xval = float(fields[col])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 except Exception, ex:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 xval = r('NA')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 #print >>sys.stderr, "ex = %s" %ex
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 x_vals[k].append(xval)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 x_vals1 = numpy.asarray(x_vals).transpose()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 dat= r.list(x=array(x_vals1), y=y_vals)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 set_default_mode(NO_CONVERSION)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 full = r.lm(r("y ~ x"), data= r.na_exclude(dat)) #full model includes all the predictor variables specified by the user
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 except RException, rex:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 stop_err("Error performing linear regression on the input data.\nEither the response column or one of the predictor columns contain no numeric values.")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 set_default_mode(BASIC_CONVERSION)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 summary = r.summary(full)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 fullr2 = summary.get('r.squared','NA')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 if fullr2 == 'NA':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 stop_error("Error in linear regression")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 if len(x_vals) < 10:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 s = ""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 for ch in range(len(x_vals)):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 s += str(ch)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 stop_err("This tool only works with less than 10 predictors.")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 print >>fout, "#Model\tR-sq\tRCVE_Terms\tRCVE_Value"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 all_combos = sorted(sscombs(s), key=len)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 all_combos.reverse()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 for j,cols in enumerate(all_combos):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 #if len(cols) == len(s): #Same as the full model above
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 # continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 if len(cols) == 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 x_vals1 = x_vals[int(cols)]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 x_v = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 for col in cols:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 x_v.append(x_vals[int(col)])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 x_vals1 = numpy.asarray(x_v).transpose()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 dat= r.list(x=array(x_vals1), y=y_vals)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 set_default_mode(NO_CONVERSION)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 red = r.lm(r("y ~ x"), data= dat) #Reduced model
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 set_default_mode(BASIC_CONVERSION)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 summary = r.summary(red)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 redr2 = summary.get('r.squared','NA')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 rcve = (float(fullr2)-float(redr2))/float(fullr2)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 rcve = 'NA'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 col_str = ""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 for col in cols:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 col_str = col_str + str(int(x_cols[int(col)]) + 1) + " "
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 col_str.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 rcve_col_str = ""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 for col in s:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 if col not in cols:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 rcve_col_str = rcve_col_str + str(int(x_cols[int(col)]) + 1) + " "
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 rcve_col_str.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 if len(cols) == len(s): #full model
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 rcve_col_str = "-"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 rcve = "-"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 redr2 = "%.4f" %(float(redr2))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 rcve = "%.4f" %(float(rcve))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 print >>fout, "%s\t%s\t%s\t%s" %(col_str,redr2,rcve_col_str,rcve)