-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcovar.pl
More file actions
executable file
·66 lines (48 loc) · 1.35 KB
/
covar.pl
File metadata and controls
executable file
·66 lines (48 loc) · 1.35 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
#!/usr/bin/perl -w
if((@ARGV) != 3 || ($ARGV[0] ne 'p' && $ARGV[0] ne 's')) {
print "\nUsage: ~ <p/s> <datamatrix.csv> <output.txt>\n\n";
print "Where the first line in <datamatrix.csv> contains the field names\n";
print " the other lines contains the data\n\n";
print " p -- pearson\n";
print " s -- spearman\n";
exit(1);
}
use math;
use Flat;
my($type) = $ARGV[0];
my($in) = Flat->new($ARGV[1], 1);
my($out) = $ARGV[2];
my %flds2comp;
my @fnames = $in->getFieldNames();
for(my($i) = 0; $i < scalar(@fnames); $i++) {
if($in->fieldIsNumeric($i)) {
$flds2comp{$fnames[$i]} = $i;
}
}
my @fnames2comp = keys %flds2comp;
my @findice2comp = values %flds2comp;
open OUT, "+>$out" || die $!;
print OUT join("\t", "COVAR", @fnames2comp), "\n";
my(@ndata);
$in->reset();
while($row = $in->readNextRow()) {
push @ndata, [map { $row->[$_]; } @findice2comp];
}
my(@ccoef);
if($type eq 'p') { # pearson
@ccoef = math::util::getPearsonCoefMatrix(@ndata);
}
elsif($type eq 's') { # else spearman
@ccoef = math::util::getSpearmanCoefMatrix(@ndata);
}
else {
die "Unknown coef type: $type\n";
}
for(my($i) = 0; $i < scalar(@fnames2comp); $i++) {
print OUT $fnames2comp[$i];
for(my($j) = 0; $j < scalar(@fnames2comp); $j++) {
printf OUT "\t%3.2f", $ccoef[$i]->[$j];
}
print OUT "\n";
}
close OUT;