upstream-extended/community/tools/rpy_statistics_collection/pca.py
1#!/usr/bin/env python
2
3import sys
4
5# from rpy import *
6import rpy2.robjects as robjects
7from rpy2.robjects.packages import importr
8
9r = robjects.r
10grdevices = importr("grDevices")
11
12
13def stop_err(msg):
14 sys.stderr.write(msg)
15 sys.exit()
16
17
18infile = sys.argv[1]
19x_cols = sys.argv[2].split(",")
20method = sys.argv[3]
21outfile = sys.argv[4]
22outfile2 = sys.argv[5]
23
24if method == "svd":
25 scale = center = "FALSE"
26 if sys.argv[6] == "both":
27 scale = center = "TRUE"
28 elif sys.argv[6] == "center":
29 center = "TRUE"
30 elif sys.argv[6] == "scale":
31 scale = "TRUE"
32
33fout = open(outfile, "w")
34elems = []
35for i, line in enumerate(file(infile)): # noqa F821
36 line = line.rstrip("\r\n")
37 if len(line) > 0 and not line.startswith("#"):
38 elems = line.split("\t")
39 break
40 if i == 30:
41 break # Hopefully we'll never get here...
42
43if len(elems) < 1:
44 stop_err(
45 "The data in your input dataset is either missing or not formatted properly."
46 )
47
48x_vals = []
49
50for k, col in enumerate(x_cols):
51 x_cols[k] = int(col) - 1
52 # x_vals.append([])
53
54NA = "NA"
55skipped = 0
56for ind, line in enumerate(file(infile)): # noqa F821
57 if line and not line.startswith("#"):
58 try:
59 fields = line.strip().split("\t")
60 valid_line = True
61 for k, col in enumerate(x_cols):
62 try:
63 xval = float(fields[col])
64 except Exception:
65 skipped += 1
66 valid_line = False
67 break
68 if valid_line:
69 for k, col in enumerate(x_cols):
70 xval = float(fields[col])
71 # x_vals[k].append(xval)
72 x_vals.append(xval)
73 except Exception:
74 skipped += 1
75
76# x_vals1 = numpy.asarray(x_vals).transpose()
77# dat= r.list(array(x_vals1))
78dat = r["matrix"](robjects.FloatVector(x_vals), ncol=len(x_cols), byrow=True)
79
80# set_default_mode(NO_CONVERSION)
81try:
82 if method == "cor":
83 # pc = r.princomp(r.na_exclude(dat), cor = r("TRUE"))
84 pc = r.princomp(r["na.exclude"](dat), cor=r("TRUE"))
85 elif method == "cov":
86 # pc = r.princomp(r.na_exclude(dat), cor = r("FALSE"))
87 pc = r.princomp(r["na.exclude"](dat), cor=r("FALSE"))
88 elif method == "svd":
89 # pc = r.prcomp(r.na_exclude(dat), center = r(center), scale = r(scale))
90 pc = r.prcomp(r["na.exclude"](dat), center=r(center), scale=r(scale))
91# except Exception as rex:
92except Exception as rex: # need to find rpy2 RException
93 stop_err("Encountered error while performing PCA on the input data: %s" % (rex))
94
95# set_default_mode(BASIC_CONVERSION)
96summary = r.summary(pc, loadings="TRUE")
97# ncomps = len(summary['sdev'])
98ncomps = len(summary.rx2("sdev"))
99
100# if type(summary['sdev']) == type({}):
101# comps_unsorted = summary['sdev'].keys()
102# comps=[]
103# sd = summary['sdev'].values()
104# for i in range(ncomps):
105# sd[i] = summary['sdev'].values()[comps_unsorted.index('Comp.%s' %(i+1))]
106# comps.append('Comp.%s' %(i+1))
107# elif type(summary['sdev']) == type([]):
108# comps=[]
109# for i in range(ncomps):
110# comps.append('Comp.%s' %(i+1))
111# sd = summary['sdev']
112
113comps = []
114for i in range(ncomps):
115 comps.append("Comp.%s" % (i + 1))
116sd = summary.rx2("sdev")
117
118print("#Component\t%s" % (
119 "\t".join(["%s" % el for el in range(1, ncomps + 1)])
120), file=fout)
121# print >>fout, "#Std. deviation\t%s" %("\t".join(["%.4g" % el for el in sd]))
122print("#Std. deviation\t%s" % ("\t".join(["%.4g" % el for el in sd])), file=fout)
123total_var = 0
124vars = []
125for s in sd:
126 var = s * s
127 total_var += var
128 vars.append(var)
129for i, var in enumerate(vars):
130 vars[i] = vars[i] / total_var
131
132print("#Proportion of variance explained\t%s" % (
133 "\t".join(["%.4g" % el for el in vars])
134), file=fout)
135
136print("#Loadings\t%s" % ("\t".join(["%s" % el for el in range(1, ncomps + 1)])), file=fout)
137xcolnames = ["c%d" % (el + 1) for el in x_cols]
138# if 'loadings' in summary: #in case of princomp
139if "loadings" in summary.names: # in case of princomp
140 loadings = "loadings"
141# elif 'rotation' in summary: #in case of prcomp
142elif "rotation" in summary.names: # in case of prcomp
143 loadings = "rotation"
144# for i,val in enumerate(summary[loadings]):
145# print >>fout, "%s\t%s" %(xcolnames[i], "\t".join(["%.4g" % el for el in val]))
146vm = summary.rx2(loadings)
147for i in range(vm.nrow):
148 vals = []
149 for j in range(vm.ncol):
150 vals.append("%.4g" % vm.rx2(i + 1, j + 1)[0])
151 print("%s\t%s" % (xcolnames[i], "\t".join(vals)), file=fout)
152
153print("#Scores\t%s" % ("\t".join(["%s" % el for el in range(1, ncomps + 1)])), file=fout)
154# if 'scores' in summary: #in case of princomp
155if "scores" in summary.names: # in case of princomp
156 scores = "scores"
157# elif 'x' in summary: #in case of prcomp
158elif "x" in summary.names: # in case of prcomp
159 scores = "x"
160# for obs,sc in enumerate(summary[scores]):
161# print >>fout, "%s\t%s" %(obs+1, "\t".join(["%.4g" % el for el in sc]))
162vm = summary.rx2(scores)
163for i in range(vm.nrow):
164 vals = []
165 for j in range(vm.ncol):
166 vals.append("%.4g" % vm.rx2(i + 1, j + 1)[0])
167 print("%s\t%s" % (i + 1, "\t".join(vals)), file=fout)
168r.pdf(outfile2, 8, 8)
169r.biplot(pc)
170# r.dev_off()
171grdevices.dev_off()