Source code

Download source archive ↓

upstream-extended/community/tools/rpy_statistics_collection/logistic_regression_vif.py

1#!/usr/bin/env python
2
3# from galaxy import eggs
4import sys
5
6import rpy2.rinterface as ri
7import rpy2.rlike.container as rlc
8# from rpy import *
9import rpy2.robjects as robjects
10
11r = robjects.r
12
13
14def stop_err(msg):
15    sys.stderr.write(msg)
16    sys.exit()
17
18
19infile = sys.argv[1]
20y_col = int(sys.argv[2]) - 1
21x_cols = sys.argv[3].split(",")
22outfile = sys.argv[4]
23
24
25print("Predictor columns: %s; Response column: %d" % (x_cols, y_col + 1))
26fout = open(outfile, "w")
27elems = []
28for i, line in enumerate(file(infile)):  # noqa F821
29    line = line.rstrip("\r\n")
30    if len(line) > 0 and not line.startswith("#"):
31        elems = line.split("\t")
32        break
33    if i == 30:
34        break  # Hopefully we'll never get here...
35
36if len(elems) < 1:
37    stop_err(
38        "The data in your input dataset is either missing or not formatted properly."
39    )
40
41y_vals = []
42x_vals = []
43x_vector = []
44for k, col in enumerate(x_cols):
45    x_cols[k] = int(col) - 1
46    x_vals.append([])
47
48NA = "NA"
49for ind, line in enumerate(file(infile)):  # noqa F821
50    if line and not line.startswith("#"):
51        try:
52            fields = line.split("\t")
53            try:
54                yval = float(fields[y_col])
55            except Exception:
56                yval = r("NA")
57            y_vals.append(yval)
58            for k, col in enumerate(x_cols):
59                try:
60                    xval = float(fields[col])
61                except Exception:
62                    xval = r("NA")
63                x_vals[k].append(xval)
64                x_vector.append(xval)
65        except Exception as e:
66            print(e)
67
68# x_vals1 = numpy.asarray(x_vals).transpose()
69
70check1 = 0
71check0 = 0
72for i in y_vals:
73    if i == 1:
74        check1 = 1
75    if i == 0:
76        check0 = 1
77if check1 == 0 or check0 == 0:
78    sys.exit("Warning: logistic regression must have at least two classes")
79
80for i in y_vals:
81    if i not in [1, 0, r("NA")]:
82        print(str(i), file=fout)
83        sys.exit(
84            "Warning: the current version of this tool can run only with two classes and need to be labeled as 0 and 1."
85        )
86
87
88# dat= r.list(x=array(x_vals1), y=y_vals)
89novif = 0
90# set_default_mode(NO_CONVERSION)
91# try:
92#    linear_model = r.glm(r("y ~ x"), data = r.na_exclude(dat),family="binomial")
93#    #r('library(car)')
94#    #r.assign('dat',dat)
95#    #r.assign('ncols',len(x_cols))
96#    #r.vif(r('glm(dat$y ~ ., data = na.exclude(data.frame(as.matrix(dat$x,ncol=ncols))->datx),family="binomial")')).as_py()
97#
98# except Exception as rex:
99#    stop_err("Error performing logistic regression on the input data.\nEither the response column or one of the predictor columns contain only non-numeric or invalid values.")
100
101fv = robjects.FloatVector(x_vector)
102m = r["matrix"](fv, ncol=len(x_cols), byrow=True)
103# ensure order for generating formula
104od = rlc.OrdDict([("y", robjects.FloatVector(y_vals)), ("x", m)])
105dat = robjects.DataFrame(od)
106# convert dat.names: ["y","x.1","x.2"] to formula string: 'y ~ x.1 + x.2'
107formula = " + ".join(dat.names).replace("+", "~", 1)
108print(formula)
109try:
110    linear_model = r.glm(formula, data=r["na.exclude"](dat), family="binomial")
111except Exception:
112    stop_err(
113        "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."
114    )
115
116if len(x_cols) > 1:
117    try:
118        r("library(car)")
119        r.assign("dat", dat)
120        r.assign("ncols", len(x_cols))
121        # vif=r.vif(r('glm(dat$y ~ ., data = na.exclude(data.frame(as.matrix(dat$x,ncol=ncols))->datx),family="binomial")'))
122        od2 = rlc.OrdDict([("datx", m)])
123        glm_data_frame = robjects.DataFrame(od2)
124        glm_result = r.glm(
125            "dat$y ~ .", data=r["na.exclude"](glm_data_frame), family="binomial"
126        )
127        print("Have glm")
128        vif = r.vif(glm_result)
129    except Exception as rex:
130        print(rex)
131else:
132    novif = 1
133
134# set_default_mode(BASIC_CONVERSION)
135
136# coeffs=linear_model.as_py()['coefficients']
137coeffs = linear_model.rx2("coefficients")
138# null_deviance=linear_model.as_py()['null.deviance']
139null_deviance = linear_model.rx2("null.deviance")[0]
140# residual_deviance=linear_model.as_py()['deviance']
141residual_deviance = linear_model.rx2("deviance")[0]
142# yintercept= coeffs['(Intercept)']
143yintercept = coeffs.rx2("(Intercept)")[0]
144
145summary = r.summary(linear_model)
146# co = summary.get('coefficients', 'NA')
147co = summary.rx2("coefficients")
148print(co)
149"""
150if len(co) != len(x_vals)+1:
151    stop_err("Stopped performing logistic regression on the input data, since one of the predictor columns contains only non-numeric or invalid values.")
152"""
153
154try:
155    yintercept = r.round(float(yintercept), digits=10)[0]
156    # pvaly = r.round(float(co[0][3]), digits=10)
157    pvaly = r.round(float(co.rx(1, 4)[0]), digits=10)[0]
158except Exception as e:
159    print(str(e))
160print("response column\tc%d" % (y_col + 1), file=fout)
161tempP = []
162for i in x_cols:
163    tempP.append("c" + str(i + 1))
164tempP = ",".join(tempP)
165print("predictor column(s)\t%s" % (tempP), file=fout)
166print("Y-intercept\t%s" % (yintercept), file=fout)
167print("p-value (Y-intercept)\t%s" % (pvaly), file=fout)
168
169print(coeffs)
170if len(x_vals) == 1:  # Simple linear  regression case with 1 predictor variable
171    try:
172        # slope = r.round(float(coeffs['x']), digits=10)
173        raw_slope = coeffs.rx2("x")[0]
174        slope = r.round(float(raw_slope), digits=10)[0]
175    except Exception:
176        slope = "NA"
177    try:
178        # pval = r.round(float(co[1][3]), digits=10)
179        pval = r.round(float(co.rx2(2, 4)[0]), digits=10)[0]
180    except Exception:
181        pval = "NA"
182    print("Slope (c%d)\t%s" % (x_cols[0] + 1, slope), file=fout)
183    print("p-value (c%d)\t%s" % (x_cols[0] + 1, pval), file=fout)
184else:  # Multiple regression case with >1 predictors
185    ind = 1
186    # while ind < len(coeffs.keys()):
187    print(len(coeffs.names))
188    while ind < len(coeffs.names):
189        try:
190            # slope = r.round(float(coeffs['x'+str(ind)]), digits=10)
191            raw_slope = coeffs.rx2("x." + str(ind))[0]
192            slope = r.round(float(raw_slope), digits=10)[0]
193        except Exception:
194            slope = "NA"
195        print("Slope (c%d)\t%s" % (x_cols[ind - 1] + 1, slope), file=fout)
196
197        try:
198            # pval = r.round(float(co[ind][3]), digits=10)
199            pval = r.round(float(co.rx2(ind + 1, 4)[0]), digits=10)[0]
200        except Exception:
201            pval = "NA"
202        print("p-value (c%d)\t%s" % (x_cols[ind - 1] + 1, pval), file=fout)
203        ind += 1
204
205# rsq = summary.get('r.squared','NA')
206rsq = summary.rx2("r.squared")
207if rsq == ri.RNULLType():
208    rsq = "NA"
209else:
210    rsq = rsq[0]
211
212
213try:
214    # rsq= r.round(float((null_deviance-residual_deviance)/null_deviance), digits=5)
215    rsq = r.round(float((null_deviance - residual_deviance) / null_deviance), digits=5)[
216        0
217    ]
218    # null_deviance= r.round(float(null_deviance), digits=5)
219    null_deviance = r.round(float(null_deviance), digits=5)[0]
220    # residual_deviance= r.round(float(residual_deviance), digits=5)
221    residual_deviance = r.round(float(residual_deviance), digits=5)[0]
222
223except Exception:
224    pass
225
226print("Null deviance\t%s" % (null_deviance), file=fout)
227
228print("Residual deviance\t%s" % (residual_deviance), file=fout)
229print("pseudo R-squared\t%s" % (rsq), file=fout)
230print("\n", file=fout)
231print("vif", file=fout)
232
233if novif == 0:
234    # py_vif=vif.as_py()
235    count = 0
236    for i in sorted(vif.names):
237        print("c" + str(x_cols[count] + 1), str(vif.rx2(i)[0]), file=fout)
238        count += 1
239elif novif == 1:
240    print("vif can calculate only when model have more than 1 predictor", file=fout)