Source code

Download source archive ↓

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

1#!/usr/bin/env python
2
3import re
4import sys
5import tempfile
6
7# from rpy import *
8import rpy2.robjects as robjects
9from rpy2.robjects.vectors import DataFrame
10
11r = robjects.r
12
13assert sys.version_info[:2] >= (2, 4)
14
15
16def stop_err(msg):
17    sys.stderr.write(msg)
18    sys.exit()
19
20
21def S3_METHODS(all="key"):
22    Group_Math = [
23        "abs",
24        "sign",
25        "sqrt",
26        "floor",
27        "ceiling",
28        "trunc",
29        "round",
30        "signif",
31        "exp",
32        "log",
33        "cos",
34        "sin",
35        "tan",
36        "acos",
37        "asin",
38        "atan",
39        "cosh",
40        "sinh",
41        "tanh",
42        "acosh",
43        "asinh",
44        "atanh",
45        "lgamma",
46        "gamma",
47        "gammaCody",
48        "digamma",
49        "trigamma",
50        "cumsum",
51        "cumprod",
52        "cummax",
53        "cummin",
54        "c",
55    ]
56    Group_Ops = [
57        "+",
58        "-",
59        "*",
60        "/",
61        "^",
62        "%%",
63        "%/%",
64        "&",
65        "|",
66        "!",
67        "==",
68        "!=",
69        "<",
70        "<=",
71        ">=",
72        ">",
73        "(",
74        ")",
75        "~",
76        ",",
77    ]
78    if all == "key":
79        return {"Math": Group_Math, "Ops": Group_Ops}
80
81
82def main():
83    try:
84        datafile = sys.argv[1]
85        outfile_name = sys.argv[2]
86        expression = sys.argv[3]
87    except Exception:
88        stop_err("Usage: python gsummary.py input_file ouput_file expression")
89
90    math_allowed = S3_METHODS()["Math"]
91    ops_allowed = S3_METHODS()["Ops"]
92
93    # Check for invalid expressions
94    for word in re.compile("[a-zA-Z]+").findall(expression):
95        if word and word not in math_allowed:
96            stop_err(
97                "Invalid expression '%s': term '%s' is not recognized or allowed"
98                % (expression, word)
99            )
100    symbols = set()
101    for symbol in re.compile("[^a-z0-9\s]+").findall(expression):  # noqa W605
102        if symbol and symbol not in ops_allowed:
103            stop_err(
104                "Invalid expression '%s': operator '%s' is not recognized or allowed"
105                % (expression, symbol)
106            )
107        else:
108            symbols.add(symbol)
109    if len(symbols) == 1 and "," in symbols:
110        # User may have entered a comma-separated list r_data_frame columns
111        stop_err(
112            "Invalid columns '%s': this tool requires a single column or expression"
113            % expression
114        )
115
116    # Find all column references in the expression
117    cols = []
118    for col in re.compile("c[0-9]+").findall(expression):
119        try:
120            cols.append(int(col[1:]) - 1)
121        except Exception:
122            pass
123
124    tmp_file = tempfile.NamedTemporaryFile("w+b")
125    # Write the R header row to the temporary file
126    hdr_str = "\t".join("c%s" % str(col + 1) for col in cols)
127    tmp_file.write("%s\n" % hdr_str)
128    skipped_lines = 0
129    first_invalid_line = 0
130    i = 0
131    for i, line in enumerate(file(datafile)):  # noqa F821
132        line = line.rstrip("\r\n")
133        if line and not line.startswith("#"):
134            valid = True
135            fields = line.split("\t")
136            # Write the R data row to the temporary file
137            for col in cols:
138                try:
139                    float(fields[col])
140                except Exception:
141                    skipped_lines += 1
142                    if not first_invalid_line:
143                        first_invalid_line = i + 1
144                    valid = False
145                    break
146            if valid:
147                data_str = "\t".join(fields[col] for col in cols)
148                tmp_file.write("%s\n" % data_str)
149    tmp_file.flush()
150
151    if skipped_lines == i + 1:
152        stop_err(
153            "Invalid column or column data values invalid for computation.  See tool tips and syntax for data requirements."
154        )
155    else:
156        # summary function and return labels
157        summary_func = r(
158            "function( x ) { c( sum=sum( as.numeric( x ), na.rm=T ), mean=mean( as.numeric( x ), na.rm=T ), stdev=sd( as.numeric( x ), na.rm=T ), quantile( as.numeric( x ), na.rm=TRUE ) ) }"
159        )
160        headings = ["sum", "mean", "stdev", "0%", "25%", "50%", "75%", "100%"]
161        headings_str = "\t".join(headings)
162
163        # r.set_default_mode( NO_CONVERSION )
164        # r_data_frame = r.read_table( tmp_file.name, header=True, sep="\t" )
165        r_data_frame = DataFrame.from_csvfile(tmp_file.name, header=True, sep="\t")
166
167        outfile = open(outfile_name, "w")
168
169        for col in re.compile("c[0-9]+").findall(expression):
170            r.assign(col, r["$"](r_data_frame, col))
171        try:
172            summary = summary_func(r(expression))
173        except Exception as s:
174            outfile.close()
175            stop_err("Computation resulted in the following error: %s" % str(s))
176        # summary = summary.as_py( BASIC_CONVERSION )
177        outfile.write("#%s\n" % headings_str)
178        print(summary)
179        print(summary.r_repr())
180        outfile.write(
181            "%s\n" % "\t".join(["%g" % (summary.rx2(k)[0]) for k in headings])
182        )
183        outfile.close()
184
185        if skipped_lines:
186            print(
187                "Skipped %d invalid lines beginning with line #%d.  See tool tips for data requirements."
188                % (
189                    skipped_lines,
190                    first_invalid_line,
191                )
192            )
193
194
195if __name__ == "__main__":
196    main()