annotate cca.py @ 0:471fc9dfcc4e draft default tip

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