66"""
77
88import sys
9- from rpy import *
9+ import rpy2 .robjects as robjects
10+ r = robjects .r
11+
1012
1113def stop_err (msg ):
1214 sys .stderr .write (msg )
@@ -17,17 +19,25 @@ def main():
1719 assert method in ( "pearson" , "kendall" , "spearman" )
1820
1921 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 ))
2131 except :
2232 stop_err ( "Problem determining columns, perhaps your query does not contain a column of numerical data." )
23-
33+
2434 matrix = []
2535 skipped_lines = 0
2636 first_invalid_line = 0
2737 invalid_value = ''
2838 invalid_column = 0
2939
30- for i , line in enumerate ( file ( sys .argv [1 ] ) ):
40+ for i , line in enumerate ( open ( sys .argv [1 ] ) ):
3141 valid = True
3242 line = line .rstrip ('\n \r ' )
3343
@@ -60,29 +70,32 @@ def main():
6070 first_invalid_line = i + 1
6171
6272 if valid :
63- matrix . append ( row )
73+ matrix += row
6474
6575 if skipped_lines < i :
66- try :
67- out = open ( sys .argv [2 ], "w" )
68- except :
69- stop_err ( "Unable to open output file" )
70-
7176 # Run correlation
7277 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 ) ))
8093
8194 if skipped_lines > 0 :
8295 msg = "..Skipped %d lines starting with line #%d. " % ( skipped_lines , first_invalid_line )
8396 if invalid_value and invalid_column > 0 :
8497 msg += "Value '%s' in column %d is not numeric." % ( invalid_value , invalid_column )
85- print msg
98+ print ( msg )
8699
87100if __name__ == "__main__" :
88101 main ()
0 commit comments