Source code

Download source archive ↓

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

1#!/usr/bin/env python
2
3# from galaxy import eggs
4
5import sys
6
7import rpy2.rlike.container as rlc
8import rpy2.robjects as robjects
9
10# from rpy import *
11
12
13r = robjects.r
14
15# export PYTHONPATH=~/galaxy/lib/
16# running command python partialR_square.py reg_inp.tab 4 1,2,3 partialR_result.tabular
17
18
19def stop_err(msg):
20    sys.stderr.write(msg)
21    sys.exit()
22
23
24def sscombs(s):
25    if len(s) == 1:
26        return [s]
27    else:
28        ssc = sscombs(s[1:])
29        return [s[0]] + [s[0] + comb for comb in ssc] + ssc
30
31
32infile = sys.argv[1]
33y_col = int(sys.argv[2]) - 1
34x_cols = sys.argv[3].split(",")
35outfile = sys.argv[4]
36
37print("Predictor columns: %s; Response column: %d" % (x_cols, y_col + 1))
38fout = open(outfile, "w")
39
40for i, line in enumerate(file(infile)):  # noqa F821
41    line = line.rstrip("\r\n")
42    if len(line) > 0 and not line.startswith("#"):
43        elems = line.split("\t")
44        break
45    if i == 30:
46        break  # Hopefully we'll never get here...
47
48if len(elems) < 1:
49    stop_err(
50        "The data in your input dataset is either missing or not formatted properly."
51    )
52
53y_vals = []
54x_vals = []
55x_vector = []
56for k, col in enumerate(x_cols):
57    x_cols[k] = int(col) - 1
58    x_vals.append([])
59    """
60    try:
61        float( elems[x_cols[k]] )
62    except Exception:
63        try:
64            msg = "This operation cannot be performed on non-numeric column %d containing value '%s'." %( col, elems[x_cols[k]] )
65        except Exception:
66            msg = "This operation cannot be performed on non-numeric data."
67        stop_err( msg )
68    """
69NA = "NA"
70for ind, line in enumerate(file(infile)):  # noqa F821
71    if line and not line.startswith("#"):
72        try:
73            fields = line.split("\t")
74            try:
75                yval = float(fields[y_col])
76            except Exception:
77                yval = r("NA")
78                # print >>sys.stderr, "ey = %s" %ey
79            y_vals.append(yval)
80            for k, col in enumerate(x_cols):
81                try:
82                    xval = float(fields[col])
83                except Exception:
84                    xval = r("NA")
85                    # print >>sys.stderr, "ex = %s" %ex
86                x_vals[k].append(xval)
87                x_vector.append(xval)
88        except Exception:
89            pass
90
91# x_vals1 = numpy.asarray(x_vals).transpose()
92# dat= r.list(x=array(x_vals1), y=y_vals)
93
94# set_default_mode(NO_CONVERSION)
95# try:
96#    full = r.lm(r("y ~ x"), data= r.na_exclude(dat))    #full model includes all the predictor variables specified by the user
97# except Exception as rex:
98#    stop_err("Error performing linear regression on the input data.\nEither the response column or one of the predictor columns contain no numeric values.")
99# set_default_mode(BASIC_CONVERSION)
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)
108try:
109    full = r.lm(formula, data=r["na.exclude"](dat))
110except Exception:
111    stop_err(
112        "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."
113    )
114
115
116summary = r.summary(full)
117# fullr2 = summary.get('r.squared','NA')
118fullr2 = summary.rx2("r.squared")[0]
119
120if fullr2 == "NA":
121    stop_err("Error in linear regression")
122
123if len(x_vals) < 10:
124    s = ""
125    for ch in range(len(x_vals)):
126        s += str(ch)
127else:
128    stop_err("This tool only works with less than 10 predictors.")
129
130print("#Model\tR-sq\tpartial_R_Terms\tpartial_R_Value", file=fout)
131all_combos = sorted(sscombs(s), key=len)
132all_combos.reverse()
133for j, cols in enumerate(all_combos):
134    # if len(cols) == len(s):    #Same as the full model above
135    #    continue
136    if len(cols) == 1:
137        # x_vals1 = x_vals[int(cols)]
138        x_v = x_vals[int(cols)]
139    else:
140        x_v = []
141        for col in cols:
142            # x_v.append(x_vals[int(col)])
143            x_v.extend(x_vals[int(col)])
144        # x_vals1 = numpy.asarray(x_v).transpose()
145    # dat= r.list(x=array(x_vals1), y=y_vals)
146    # set_default_mode(NO_CONVERSION)
147    # red = r.lm(r("y ~ x"), data= dat)    #Reduced model
148    # set_default_mode(BASIC_CONVERSION)
149    fv = robjects.FloatVector(x_v)
150    m = r["matrix"](fv, ncol=len(cols), byrow=False)
151    # ensure order for generating formula
152    od = rlc.OrdDict([("y", robjects.FloatVector(y_vals)), ("x", m)])
153    dat = robjects.DataFrame(od)
154    # convert dat.names: ["y","x.1","x.2"] to formula string: 'y ~ x.1 + x.2'
155    formula = " + ".join(dat.names).replace("+", "~", 1)
156    try:
157        red = r.lm(formula, data=r["na.exclude"](dat))
158    except Exception:
159        stop_err(
160            "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."
161        )
162
163    summary = r.summary(red)
164    # redr2 = summary.get('r.squared','NA')
165    redr2 = summary.rx2("r.squared")[0]
166
167    try:
168        partial_R = (float(fullr2) - float(redr2)) / (1 - float(redr2))
169    except Exception:
170        partial_R = "NA"
171    col_str = ""
172    for col in cols:
173        col_str = col_str + str(int(x_cols[int(col)]) + 1) + " "
174    col_str.strip()
175    partial_R_col_str = ""
176    for col in s:
177        if col not in cols:
178            partial_R_col_str = partial_R_col_str + str(int(x_cols[int(col)]) + 1) + " "
179    partial_R_col_str.strip()
180    if len(cols) == len(s):  # full model
181        partial_R_col_str = "-"
182        partial_R = "-"
183    try:
184        redr2 = "%.4f" % (float(redr2))
185    except Exception:
186        pass
187    try:
188        partial_R = "%.4f" % (float(partial_R))
189    except Exception:
190        pass
191    print("%s\t%s\t%s\t%s" % (col_str, redr2, partial_R_col_str, partial_R), file=fout)