6
6
"""
7
7
8
8
import sys
9
- from rpy import *
9
+ import rpy2 .robjects as robjects
10
+ r = robjects .r
11
+
10
12
11
13
def stop_err (msg ):
12
14
sys .stderr .write (msg )
@@ -17,17 +19,25 @@ def main():
17
19
assert method in ( "pearson" , "kendall" , "spearman" )
18
20
19
21
try :
20
- columns = map ( int , sys .argv [3 ].split ( ',' ) )
22
+ column_string = sys .argv [3 ]
23
+ columns = list ()
24
+ for col in column_string .split (',' ):
25
+ if '-' in col :
26
+ s , e = col .split ('-' )
27
+ col = list (range (int (s ), int (e ) + 1 ))
28
+ columns .extend (col )
29
+ else :
30
+ columns .append (int (col ))
21
31
except :
22
32
stop_err ( "Problem determining columns, perhaps your query does not contain a column of numerical data." )
23
-
33
+
24
34
matrix = []
25
35
skipped_lines = 0
26
36
first_invalid_line = 0
27
37
invalid_value = ''
28
38
invalid_column = 0
29
39
30
- for i , line in enumerate ( file ( sys .argv [1 ] ) ):
40
+ for i , line in enumerate ( open ( sys .argv [1 ] ) ):
31
41
valid = True
32
42
line = line .rstrip ('\n \r ' )
33
43
@@ -60,29 +70,32 @@ def main():
60
70
first_invalid_line = i + 1
61
71
62
72
if valid :
63
- matrix . append ( row )
73
+ matrix += row
64
74
65
75
if skipped_lines < i :
66
- try :
67
- out = open ( sys .argv [2 ], "w" )
68
- except :
69
- stop_err ( "Unable to open output file" )
70
-
71
76
# Run correlation
72
77
try :
73
- value = r .cor ( array ( matrix ), use = "pairwise.complete.obs" , method = method )
74
- except Exception , exc :
75
- out .close ()
76
- stop_err ("%s" % str ( exc ))
77
- for row in value :
78
- print >> out , "\t " .join ( map ( str , row ) )
79
- out .close ()
78
+ fv = robjects .FloatVector (matrix )
79
+ m = r ['matrix' ](fv , ncol = len (columns ),byrow = True )
80
+ rslt_mat = r .cor (m , use = "pairwise.complete.obs" , method = method )
81
+ value = []
82
+ for ri in range (1 , rslt_mat .nrow + 1 ):
83
+ row = []
84
+ for ci in range (1 , rslt_mat .ncol + 1 ):
85
+ row .append (rslt_mat .rx (ri ,ci )[0 ])
86
+ value .append (row )
87
+ except Exception as exc :
88
+ stop_err ("%s" % str ( exc ))
89
+
90
+ with open ( sys .argv [2 ], "w" ) as out :
91
+ for row in value :
92
+ out .write ("%s\n " % "\t " .join ( map ( str , row ) ))
80
93
81
94
if skipped_lines > 0 :
82
95
msg = "..Skipped %d lines starting with line #%d. " % ( skipped_lines , first_invalid_line )
83
96
if invalid_value and invalid_column > 0 :
84
97
msg += "Value '%s' in column %d is not numeric." % ( invalid_value , invalid_column )
85
- print msg
98
+ print ( msg )
86
99
87
100
if __name__ == "__main__" :
88
101
main ()
0 commit comments