-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen_ks_matrix.pl
executable file
·151 lines (111 loc) · 2.98 KB
/
gen_ks_matrix.pl
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use File::Basename;
use Carp;
use Data::Dumper;
my $prog = basename ($0);
my $verbose = 0;
my $minAbundance = 0;
my $base = "";
#my $method = "mean"; #sum
GetOptions (
"base:s"=>\$base,
"min-avg-cov:f"=>\$minAbundance,
"v|verbose"=>\$verbose
);
if (@ARGV != 2)
{
print "generate ks matrix\n";
print "Usage $prog [options] <in.conf> <out.txt>\n";
print " -base [string] : base dir of input data\n";
print " --min-avg-cov [float] : min average coverage ($minAbundance)\n";
print " -v : verbose\n";
exit (1);
}
my ($configFile, $outFile) = @ARGV;
print "loading configuration file from $configFile ...\n" if $verbose;
Carp::croak "contig file $configFile does not exist\n" unless -f $configFile;
my $samples = readConfigFile ($configFile, $base);
print "done.\n" if $verbose;
print "loading data of individual samples ...\n" if $verbose;
my @sampleData;
my $geneId;
my $n = 0;
my $iter = 0;
my $geneInfo;
my @sampleNames = sort {$samples->{$a}->{"id"} <=> $samples->{$b}->{"id"}} keys %$samples;
foreach my $sName (@sampleNames)
{
my $inputFile = $samples->{$sName}->{"f"};
$inputFile = "$base/$inputFile" if $base ne '';
my $sdata = readmRINDataFile ($inputFile);
$geneInfo = $sdata->{"geneInfo"};
if ($n != 0)
{
Carp::croak "data inconsistency detected\n" if @$geneInfo != $n;
}
else
{
$n = @$geneInfo;
}
$sampleData[$iter] = $sdata->{"data"};
$iter++;
}
print "$iter samples, $n genes loaded.\n" if $verbose;
my $fout;
open ($fout, ">$outFile") || Carp::croak "cannot open file $outFile to write\n";
print $fout join ("\t", "name", @sampleNames), "\n";
for (my $i = 0; $i < $n; $i++)
{
my @out;
for (my $s = 0; $s < @sampleNames; $s++)
{
my $sName = $sampleNames[$s];
my $d = $sampleData[$s][$i];
$out[$s] = $d->[0] > $minAbundance ? $d->[1] : '';
}
#print $fout join ("\t", @{$geneInfo->[$i]}, @out), "\n";
print $fout join ("\t", $geneInfo->[$i][0], @out), "\n";
}
close ($fout);
sub readConfigFile
{
my ($configFile, $base) = @_;
my $fin;
open ($fin, "<$configFile") || Carp::croak "cannot open file $configFile to read\n";
my $i = 0;
my %samples;
while (my $line = <$fin>)
{
chomp $line;
next if $line=~/^\s*$/;
next if $line=~/^\#/;
my ($fileName, $sampleName) = split (/\t/, $line);
$samples{$sampleName}->{"id"} = $i++;
$samples{$sampleName}->{"f"} = $fileName;
}
close ($fin);
return \%samples;
}
sub readmRINDataFile
{
my ($inputFile) = @_;
my $fin;
my @data;
my @geneInfo;
open ($fin, "<$inputFile") || Carp::croak "cannot open file $inputFile to read\n";
my $totalCoverage = 0; #reserved
while (my $line = <$fin>)
{
chomp $line;
next if $line =~/^\s*$/;
next if $line =~/^\#/;
my ($name, $exonLen, $abundance, $maxDev, $ks) = split (/\t/, $line);
push @geneInfo, [$name, $exonLen];
push @data, [$abundance, $maxDev, $ks];
$totalCoverage += $abundance * $exonLen;
}
close ($fin);
return {geneInfo=>\@geneInfo, data=>\@data};
}