annotate tools/multivariate_stats/kcca.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 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 Run kernel CCA using kcca() from R 'kernlab' package
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 usage: %prog [options]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 -i, --input=i: Input file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 -o, --output1=o: Summary output
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 -x, --x_cols=x: X-Variable columns
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 -y, --y_cols=y: Y-Variable columns
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 -k, --kernel=k: Kernel function
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 -f, --features=f: Number of canonical components to return
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 -s, --sigma=s: sigma
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 -d, --degree=d: degree
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 -l, --scale=l: scale
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 -t, --offset=t: offset
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 -r, --order=r: order
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 usage: %prog input output1 x_cols y_cols kernel features sigma(or_None) degree(or_None) scale(or_None) offset(or_None) order(or_None)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 from galaxy import eggs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 import sys, string
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 from rpy import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 import numpy
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 import pkg_resources; pkg_resources.require( "bx-python" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 from bx.cookbook import doc_optparse
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 def stop_err(msg):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 sys.stderr.write(msg)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 #Parse Command Line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 options, args = doc_optparse.parse( __doc__ )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 #{'options= kernel': 'rbfdot', 'var_cols': '1,2,3,4', 'degree': 'None', 'output2': '/afs/bx.psu.edu/home/gua110/workspace/galaxy_bitbucket/database/files/000/dataset_260.dat', 'output1': '/afs/bx.psu.edu/home/gua110/workspace/galaxy_bitbucket/database/files/000/dataset_259.dat', 'scale': 'None', 'offset': 'None', 'input': '/afs/bx.psu.edu/home/gua110/workspace/galaxy_bitbucket/database/files/000/dataset_256.dat', 'sigma': '1.0', 'order': 'None'}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 infile = options.input
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 x_cols = options.x_cols.split(',')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 y_cols = options.y_cols.split(',')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 kernel = options.kernel
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 outfile = options.output1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 ncomps = int(options.features)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 fout = open(outfile,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 if ncomps < 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 print "You chose to return '0' canonical components. Please try rerunning the tool with number of components = 1 or more."
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 elems = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 for i, line in enumerate( file ( infile )):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 line = line.rstrip('\r\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 if len( line )>0 and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 elems = line.split( '\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 if i == 30:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 break # Hopefully we'll never get here...
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 if len( elems )<1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 stop_err( "The data in your input dataset is either missing or not formatted properly." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 x_vals = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 for k,col in enumerate(x_cols):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 x_cols[k] = int(col)-1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 x_vals.append([])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 y_vals = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 for k,col in enumerate(y_cols):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 y_cols[k] = int(col)-1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 y_vals.append([])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 NA = 'NA'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 skipped = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 for ind,line in enumerate( file( infile )):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 if line and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 fields = line.strip().split("\t")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 valid_line = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 for col in x_cols+y_cols:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 assert float(fields[col])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 skipped += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 valid_line = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 if valid_line:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 for k,col in enumerate(x_cols):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 xval = float(fields[col])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 xval = NaN#
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 x_vals[k].append(xval)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 for k,col in enumerate(y_cols):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 yval = float(fields[col])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 yval = NaN#
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 y_vals[k].append(yval)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 skipped += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 x_vals1 = numpy.asarray(x_vals).transpose()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 y_vals1 = numpy.asarray(y_vals).transpose()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 x_dat= r.list(array(x_vals1))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 y_dat= r.list(array(y_vals1))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 r.suppressWarnings(r.library('kernlab'))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 stop_err('Missing R library kernlab')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 set_default_mode(NO_CONVERSION)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 if kernel=="rbfdot" or kernel=="anovadot":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 pars = r.list(sigma=float(options.sigma))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 elif kernel=="polydot":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 pars = r.list(degree=float(options.degree),scale=float(options.scale),offset=float(options.offset))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 elif kernel=="tanhdot":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 pars = r.list(scale=float(options.scale),offset=float(options.offset))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 elif kernel=="besseldot":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 pars = r.list(degree=float(options.degree),sigma=float(options.sigma),order=float(options.order))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 elif kernel=="anovadot":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 pars = r.list(degree=float(options.degree),sigma=float(options.sigma))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 pars = rlist()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 kcc = r.kcca(x=x_dat, y=y_dat, kernel=kernel, kpar=pars, ncomps=ncomps)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 except RException, rex:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 stop_err("Encountered error while performing kCCA on the input data: %s" %(rex))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 set_default_mode(BASIC_CONVERSION)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 kcor = r.kcor(kcc)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 if ncomps == 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 kcor = [kcor]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 xcoef = r.xcoef(kcc)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 ycoef = r.ycoef(kcc)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 print >>fout, "#Component\t%s" %("\t".join(["%s" % el for el in range(1,ncomps+1)]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 print >>fout, "#Correlation\t%s" %("\t".join(["%.4g" % el for el in kcor]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 print >>fout, "#Estimated X-coefficients\t%s" %("\t".join(["%s" % el for el in range(1,ncomps+1)]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 for obs,val in enumerate(xcoef):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 print >>fout, "%s\t%s" %(obs+1, "\t".join(["%.4g" % el for el in val]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 print >>fout, "#Estimated Y-coefficients\t%s" %("\t".join(["%s" % el for el in range(1,ncomps+1)]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 for obs,val in enumerate(ycoef):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 print >>fout, "%s\t%s" %(obs+1, "\t".join(["%.4g" % el for el in val]))