annotate cca.py @ 0:9bc0c48a027f draft default tip

Imported from capsule None
author devteam
date Mon, 19 May 2014 12:34:48 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/env python
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
2
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
3 import sys, string
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
4 from rpy import *
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
5 import numpy
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
6
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
7 def stop_err(msg):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
8 sys.stderr.write(msg)
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
9 sys.exit()
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
10
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
11 infile = sys.argv[1]
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
12 x_cols = sys.argv[2].split(',')
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
13 y_cols = sys.argv[3].split(',')
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
14
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
15 x_scale = x_center = "FALSE"
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
16 if sys.argv[4] == 'both':
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
17 x_scale = x_center = "TRUE"
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
18 elif sys.argv[4] == 'center':
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
19 x_center = "TRUE"
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
20 elif sys.argv[4] == 'scale':
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
21 x_scale = "TRUE"
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
22
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
23 y_scale = y_center = "FALSE"
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
24 if sys.argv[5] == 'both':
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
25 y_scale = y_center = "TRUE"
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
26 elif sys.argv[5] == 'center':
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
27 y_center = "TRUE"
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
28 elif sys.argv[5] == 'scale':
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
29 y_scale = "TRUE"
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
30
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
31 std_scores = "FALSE"
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
32 if sys.argv[6] == "yes":
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
33 std_scores = "TRUE"
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
34
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
35 outfile = sys.argv[7]
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
36 outfile2 = sys.argv[8]
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
37
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
38 fout = open(outfile,'w')
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
39 elems = []
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
40 for i, line in enumerate( file ( infile )):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
41 line = line.rstrip('\r\n')
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
42 if len( line )>0 and not line.startswith( '#' ):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
43 elems = line.split( '\t' )
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
44 break
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
45 if i == 30:
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
46 break # Hopefully we'll never get here...
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
47
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
48 if len( elems )<1:
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
49 stop_err( "The data in your input dataset is either missing or not formatted properly." )
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
50
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
51 x_vals = []
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
52
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
53 for k,col in enumerate(x_cols):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
54 x_cols[k] = int(col)-1
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
55 x_vals.append([])
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
56
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
57 y_vals = []
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
58
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
59 for k,col in enumerate(y_cols):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
60 y_cols[k] = int(col)-1
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
61 y_vals.append([])
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
62
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
63 skipped = 0
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
64 for ind,line in enumerate( file( infile )):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
65 if line and not line.startswith( '#' ):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
66 try:
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
67 fields = line.strip().split("\t")
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
68 valid_line = True
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
69 for col in x_cols+y_cols:
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
70 try:
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
71 assert float(fields[col])
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
72 except:
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
73 skipped += 1
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
74 valid_line = False
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
75 break
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
76 if valid_line:
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
77 for k,col in enumerate(x_cols):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
78 try:
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
79 xval = float(fields[col])
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
80 except:
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
81 xval = NaN#
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
82 x_vals[k].append(xval)
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
83 for k,col in enumerate(y_cols):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
84 try:
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
85 yval = float(fields[col])
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
86 except:
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
87 yval = NaN#
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
88 y_vals[k].append(yval)
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
89 except:
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
90 skipped += 1
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
91
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
92 x_vals1 = numpy.asarray(x_vals).transpose()
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
93 y_vals1 = numpy.asarray(y_vals).transpose()
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
94
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
95 x_dat= r.list(array(x_vals1))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
96 y_dat= r.list(array(y_vals1))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
97
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
98 try:
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
99 r.suppressWarnings(r.library("yacca"))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
100 except:
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
101 stop_err("Missing R library yacca.")
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
102
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
103 set_default_mode(NO_CONVERSION)
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
104 try:
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
105 xcolnames = ["c%d" %(el+1) for el in x_cols]
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
106 ycolnames = ["c%d" %(el+1) for el in y_cols]
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
107 cc = r.cca(x=x_dat, y=y_dat, xlab=xcolnames, ylab=ycolnames, xcenter=r(x_center), ycenter=r(y_center), xscale=r(x_scale), yscale=r(y_scale), standardize_scores=r(std_scores))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
108 ftest = r.F_test_cca(cc)
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
109 except RException, rex:
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
110 stop_err("Encountered error while performing CCA on the input data: %s" %(rex))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
111
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
112 set_default_mode(BASIC_CONVERSION)
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
113 summary = r.summary(cc)
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
114
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
115 ncomps = len(summary['corr'])
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
116 comps = summary['corr'].keys()
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
117 corr = summary['corr'].values()
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
118 xlab = summary['xlab']
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
119 ylab = summary['ylab']
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
120
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
121 for i in range(ncomps):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
122 corr[comps.index('CV %s' %(i+1))] = summary['corr'].values()[i]
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
123
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
124 ftest=ftest.as_py()
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
125 print >>fout, "#Component\t%s" %("\t".join(["%s" % el for el in range(1,ncomps+1)]))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
126 print >>fout, "#Correlation\t%s" %("\t".join(["%.4g" % el for el in corr]))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
127 print >>fout, "#F-statistic\t%s" %("\t".join(["%.4g" % el for el in ftest['statistic']]))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
128 print >>fout, "#p-value\t%s" %("\t".join(["%.4g" % el for el in ftest['p.value']]))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
129
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
130 print >>fout, "#X-Coefficients\t%s" %("\t".join(["%s" % el for el in range(1,ncomps+1)]))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
131 for i,val in enumerate(summary['xcoef']):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
132 print >>fout, "%s\t%s" %(xlab[i], "\t".join(["%.4g" % el for el in val]))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
133
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
134 print >>fout, "#Y-Coefficients\t%s" %("\t".join(["%s" % el for el in range(1,ncomps+1)]))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
135 for i,val in enumerate(summary['ycoef']):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
136 print >>fout, "%s\t%s" %(ylab[i], "\t".join(["%.4g" % el for el in val]))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
137
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
138 print >>fout, "#X-Loadings\t%s" %("\t".join(["%s" % el for el in range(1,ncomps+1)]))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
139 for i,val in enumerate(summary['xstructcorr']):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
140 print >>fout, "%s\t%s" %(xlab[i], "\t".join(["%.4g" % el for el in val]))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
141
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
142 print >>fout, "#Y-Loadings\t%s" %("\t".join(["%s" % el for el in range(1,ncomps+1)]))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
143 for i,val in enumerate(summary['ystructcorr']):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
144 print >>fout, "%s\t%s" %(ylab[i], "\t".join(["%.4g" % el for el in val]))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
145
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
146 print >>fout, "#X-CrossLoadings\t%s" %("\t".join(["%s" % el for el in range(1,ncomps+1)]))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
147 for i,val in enumerate(summary['xcrosscorr']):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
148 print >>fout, "%s\t%s" %(xlab[i], "\t".join(["%.4g" % el for el in val]))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
149
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
150 print >>fout, "#Y-CrossLoadings\t%s" %("\t".join(["%s" % el for el in range(1,ncomps+1)]))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
151 for i,val in enumerate(summary['ycrosscorr']):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
152 print >>fout, "%s\t%s" %(ylab[i], "\t".join(["%.4g" % el for el in val]))
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
153
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
154 r.pdf( outfile2, 8, 8 )
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
155 #r.plot(cc)
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
156 for i in range(ncomps):
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
157 r.helio_plot(cc, cv = i+1, main = r.paste("Explained Variance for CV",i+1), type = "variance")
9bc0c48a027f Imported from capsule None
devteam
parents:
diff changeset
158 r.dev_off()