annotate rcve.py @ 0:7740956d197b draft default tip

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