annotate tools/multivariate_stats/pca.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 import sys, string
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 from rpy import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 import numpy
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 def stop_err(msg):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 sys.stderr.write(msg)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 infile = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 x_cols = sys.argv[2].split(',')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 method = sys.argv[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 outfile = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 outfile2 = sys.argv[5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 if method == 'svd':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 scale = center = "FALSE"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 if sys.argv[6] == 'both':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 scale = center = "TRUE"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 elif sys.argv[6] == 'center':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 center = "TRUE"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 elif sys.argv[6] == 'scale':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 scale = "TRUE"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 fout = open(outfile,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 elems = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 for i, line in enumerate( file ( infile )):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 line = line.rstrip('\r\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 if len( line )>0 and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 elems = line.split( '\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 if i == 30:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 break # Hopefully we'll never get here...
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 if len( elems )<1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 stop_err( "The data in your input dataset is either missing or not formatted properly." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 x_vals = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 for k,col in enumerate(x_cols):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 x_cols[k] = int(col)-1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 x_vals.append([])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 NA = 'NA'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 skipped = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 for ind,line in enumerate( file( infile )):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 if line and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 fields = line.strip().split("\t")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 valid_line = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 for k,col in enumerate(x_cols):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 xval = float(fields[col])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 skipped += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 valid_line = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 if valid_line:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 for k,col in enumerate(x_cols):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 xval = float(fields[col])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 x_vals[k].append(xval)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 skipped += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 x_vals1 = numpy.asarray(x_vals).transpose()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 dat= r.list(array(x_vals1))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 set_default_mode(NO_CONVERSION)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 if method == "cor":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 pc = r.princomp(r.na_exclude(dat), cor = r("TRUE"))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 elif method == "cov":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 pc = r.princomp(r.na_exclude(dat), cor = r("FALSE"))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 elif method=="svd":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 pc = r.prcomp(r.na_exclude(dat), center = r(center), scale = r(scale))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 except RException, rex:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 stop_err("Encountered error while performing PCA on the input data: %s" %(rex))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 set_default_mode(BASIC_CONVERSION)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 summary = r.summary(pc, loadings="TRUE")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 ncomps = len(summary['sdev'])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 if type(summary['sdev']) == type({}):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 comps_unsorted = summary['sdev'].keys()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 comps=[]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 sd = summary['sdev'].values()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 for i in range(ncomps):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 sd[i] = summary['sdev'].values()[comps_unsorted.index('Comp.%s' %(i+1))]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 comps.append('Comp.%s' %(i+1))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 elif type(summary['sdev']) == type([]):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 comps=[]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 for i in range(ncomps):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 comps.append('Comp.%s' %(i+1))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 sd = summary['sdev']
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 print >>fout, "#Component\t%s" %("\t".join(["%s" % el for el in range(1,ncomps+1)]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 print >>fout, "#Std. deviation\t%s" %("\t".join(["%.4g" % el for el in sd]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 total_var = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 vars = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 for s in sd:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 var = s*s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 total_var += var
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 vars.append(var)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 for i,var in enumerate(vars):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 vars[i] = vars[i]/total_var
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 print >>fout, "#Proportion of variance explained\t%s" %("\t".join(["%.4g" % el for el in vars]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 print >>fout, "#Loadings\t%s" %("\t".join(["%s" % el for el in range(1,ncomps+1)]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 xcolnames = ["c%d" %(el+1) for el in x_cols]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 if 'loadings' in summary: #in case of princomp
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 loadings = 'loadings'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 elif 'rotation' in summary: #in case of prcomp
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 loadings = 'rotation'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 for i,val in enumerate(summary[loadings]):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 print >>fout, "%s\t%s" %(xcolnames[i], "\t".join(["%.4g" % el for el in val]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 print >>fout, "#Scores\t%s" %("\t".join(["%s" % el for el in range(1,ncomps+1)]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 if 'scores' in summary: #in case of princomp
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 scores = 'scores'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 elif 'x' in summary: #in case of prcomp
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 scores = 'x'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 for obs,sc in enumerate(summary[scores]):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 print >>fout, "%s\t%s" %(obs+1, "\t".join(["%.4g" % el for el in sc]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 r.pdf( outfile2, 8, 8 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 r.biplot(pc)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 r.dev_off()