annotate rcve.py @ 0:d8c414b9d774 draft default tip

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