-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcor.pl
More file actions
executable file
·94 lines (72 loc) · 1.8 KB
/
cor.pl
File metadata and controls
executable file
·94 lines (72 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/perl -w
sub printUsage {
print "Usage: ~ [-m p|s|k] <in.csv> <fld1> ... <fldN> <out.csv>\n";
print " -p: pearson\n";
print " -s: spearman\n";
print " -k: kendall\n";
exit(1);
}
use Util;
use Flat;
use Getopt::Std;
my(%options);
getopts("m:", \%options);
my($method) = "pearson";
if(exists $options{"m"}) {
my $m = $options{"m"};
if($m eq "p") {
$method = "pearson";
}
elsif($m eq "s") {
$method = "spearman";
}
elsif($m eq "k") {
$method = "kendall";
}
else {
print "-m has to be p|s|k\n";
printUsage();
}
}
if(scalar(@ARGV) < 3) {
printUsage();
}
my $inFile = shift @ARGV;
my($in) = Flat->new($inFile, 1);
my($out) = pop @ARGV;
my(@fldIndice) = $in->getFieldIndice([@ARGV]);
my(@fldNames) = map { $in->getFieldName($_); } @fldIndice;
my $indiceStr = join(", ", map { "d[,".($_+1)."]" } @fldIndice);
open OUT, "+>$out" or die "Cannot open $out\n";
print OUT join("\t", $method, @fldNames), "\n";
open RS, "+>$out.R" or die "Cannot open $out.R\n";
my $rout = "$out.Rout";
Util::rmIfExists($rout);
print RS<<R0;
d<-read.table("$inFile", header=T, fill=T);
nd<-cbind($indiceStr);
s<-dim(nd)[2];
cor<-matrix(1,nrow=s,ncol=s);
for(i in 1:(s-1)) {
i1<-i+1;
for(j in i1:s) {
cor[i,j]<-cor.test(nd[,i], nd[,j], method="$method")\$estimate;
cor[j,i]<-cor[i,j];
}
}
for(i in 1:s) {
write(cor[i,], sep="\t", file="$rout", ncolumns=length(cor[i,]), append=T);
}
R0
close RS;
Util::run("R --no-restore --no-save < $out.R", 1);
# modify the output file to add row headers
open ROUT, "<$rout" or die "Cannot open $rout\n";
for(my($i) = 0; $i < scalar(@fldNames); $i++) {
my $line = <ROUT>;
print OUT $fldNames[$i], "\t", $line;
}
close ROUT;
close OUT;
# remove intermediate files
#Util::run("rm $rout $out.R", 0);