-
Notifications
You must be signed in to change notification settings - Fork 4
/
MetaPrism_table.pl
179 lines (167 loc) · 6.9 KB
/
MetaPrism_table.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Author: Jiwoong Kim ([email protected])
use strict;
use warnings;
local $SIG{__WARN__} = sub { die "ERROR in $0: ", $_[0] };
use Cwd 'abs_path';
use Getopt::Long qw(:config no_ignore_case);
use Statistics::R;
use Bio::DB::Taxonomy;
(my $codePath = abs_path($0)) =~ s/\/[^\/]*$//;
my $dataPath = "$codePath/MetaPrism_data";
system("mkdir -p $dataPath");
GetOptions(
'h' => \(my $help = ''),
'A=s' => \(my $abundanceColumn = 'meanDepth/genome'),
'R=s' => \(my $taxonRank = 'genus'),
'F=s' => \(my $featureType = 'gene_taxon'),
's' => \(my $scale = ''),
);
my @featureTypeList = ('gene_taxon', 'gene', 'gene_average', 'taxon', 'taxon_average');
my $featureTypes = join(', ', map {"\"$_\""} @featureTypeList);
if($help || scalar(@ARGV) == 0) {
die <<EOF;
Usage: perl MetaPrism_table.pl [options] [sample=]abundance.txt [...] > table.txt
Options: -h display this help message
-A STR abundance column [$abundanceColumn]
-R STR taxon rank [$taxonRank]
-F STR feature type, $featureTypes [$featureType]
-s scale
EOF
}
die "ERROR in $0: The feature type is not available.\n" if(scalar(grep {$featureType eq $_} @featureTypeList) == 0);
my (@sampleFileList) = @ARGV;
if(not -r "$dataPath/nodes.dmp" or not -r "$dataPath/names.dmp") {
my $URL = "ftp://ftp.ncbi.nlm.nih.gov/pub/taxonomy/taxdump.tar.gz";
my $file = "$dataPath/taxdump.tar.gz";
system("wget --no-verbose -O $file $URL") if(not -r $file);
die "ERROR in $0: '$file' has zero size.\n" if(-z $file);
system("cd $dataPath; tar -zxf taxdump.tar.gz nodes.dmp");
system("cd $dataPath; tar -zxf taxdump.tar.gz names.dmp");
system("rm -f $dataPath/$_") foreach('nodes', 'parents', 'names2id', 'id2names');
}
my $db = Bio::DB::Taxonomy->new(-source => 'flatfile', -directory => $dataPath, -nodesfile => "$dataPath/nodes.dmp", -namesfile => "$dataPath/names.dmp");
my @sampleList = ();
my %sampleFeatureAbundanceHash = ();
my %sampleFeatureCountHash = ();
my %geneDefinitionHash = ();
@sampleFileList = map {[$_->[0], $_->[-1]]} map {[split(/=/, $_, 2)]} @sampleFileList;
foreach(@sampleFileList) {
my ($sample, $file) = @$_;
if(-s $file) {
push(@sampleList, $sample);
open(my $reader, $file);
chomp(my $line = <$reader>);
my @columnList = split(/\t/, $line, -1);
while(my $line = <$reader>) {
chomp($line);
my %tokenHash = ();
@tokenHash{@columnList} = split(/\t/, $line, -1);
my $feature = '';
if($featureType eq 'gene_taxon') {
if((my $taxonName = getTaxonName($tokenHash{'taxon'})) ne '') {
$feature = join("\t", $tokenHash{'gene'}, $taxonName);
}
} elsif($featureType eq 'gene' || $featureType eq 'gene_average') {
$feature = $tokenHash{'gene'};
} elsif($featureType eq 'taxon' || $featureType eq 'taxon_average') {
if((my $taxonName = getTaxonName($tokenHash{'taxon'})) ne '') {
$feature = $taxonName;
}
}
if($feature ne '') {
$sampleFeatureAbundanceHash{$sample}->{$feature} += $tokenHash{$abundanceColumn};
$sampleFeatureCountHash{$sample}->{$feature} += 1;
}
$geneDefinitionHash{$tokenHash{'gene'}} = $tokenHash{'definition'} if(defined($tokenHash{'definition'}) && $tokenHash{'definition'} ne '');
}
close($reader);
}
}
if($featureType eq 'gene_average' || $featureType eq 'taxon_average') {
foreach my $sample (@sampleList) {
$sampleFeatureAbundanceHash{$sample}->{$_} /= $sampleFeatureCountHash{$sample}->{$_} foreach(keys %{$sampleFeatureAbundanceHash{$sample}});
}
}
my %featureHash = ();
foreach my $sample (@sampleList) {
$featureHash{$_} = 1 foreach(keys %{$sampleFeatureAbundanceHash{$sample}});
}
if(my @featureList = sort keys %featureHash) {
if($featureType eq 'gene_taxon') {
if(%geneDefinitionHash) {
my @featureColumnList = ('gene', 'taxon', 'definition');
print join("\t", @featureColumnList, @sampleList), "\n";
foreach my $feature (@featureList) {
my %tokenHash = ();
@tokenHash{'gene', 'taxon'} = split(/\t/, $feature);
$tokenHash{'definition'} = $geneDefinitionHash{$tokenHash{'gene'}};
my @abundanceList = map {defined($_) ? $_ : 0} map {$_->{$feature}} @sampleFeatureAbundanceHash{@sampleList};
@abundanceList = scale(@abundanceList) if($scale);
print join("\t", (map {defined($_) ? $_ : ''} @tokenHash{@featureColumnList}), @abundanceList), "\n";
}
} else {
my @featureColumnList = ('gene', 'taxon');
print join("\t", @featureColumnList, @sampleList), "\n";
foreach my $feature (@featureList) {
my %tokenHash = ();
@tokenHash{'gene', 'taxon'} = split(/\t/, $feature);
my @abundanceList = map {defined($_) ? $_ : 0} map {$_->{$feature}} @sampleFeatureAbundanceHash{@sampleList};
@abundanceList = scale(@abundanceList) if($scale);
print join("\t", (map {defined($_) ? $_ : ''} @tokenHash{@featureColumnList}), @abundanceList), "\n";
}
}
} elsif($featureType eq 'gene' || $featureType eq 'gene_average') {
if(%geneDefinitionHash) {
my @featureColumnList = ('gene', 'definition');
print join("\t", @featureColumnList, @sampleList), "\n";
foreach my $feature (@featureList) {
my %tokenHash = ();
$tokenHash{'gene'} = $feature;
$tokenHash{'definition'} = $geneDefinitionHash{$tokenHash{'gene'}};
my @abundanceList = map {defined($_) ? $_ : 0} map {$_->{$feature}} @sampleFeatureAbundanceHash{@sampleList};
@abundanceList = scale(@abundanceList) if($scale);
print join("\t", (map {defined($_) ? $_ : ''} @tokenHash{@featureColumnList}), @abundanceList), "\n";
}
} else {
my @featureColumnList = ('gene');
print join("\t", @featureColumnList, @sampleList), "\n";
foreach my $feature (@featureList) {
my %tokenHash = ();
$tokenHash{'gene'} = $feature;
my @abundanceList = map {defined($_) ? $_ : 0} map {$_->{$feature}} @sampleFeatureAbundanceHash{@sampleList};
@abundanceList = scale(@abundanceList) if($scale);
print join("\t", (map {defined($_) ? $_ : ''} @tokenHash{@featureColumnList}), @abundanceList), "\n";
}
}
} elsif($featureType eq 'taxon' || $featureType eq 'taxon_average') {
my @featureColumnList = ('taxon');
print join("\t", @featureColumnList, @sampleList), "\n";
foreach my $feature (@featureList) {
my %tokenHash = ();
$tokenHash{'taxon'} = $feature;
my @abundanceList = map {defined($_) ? $_ : 0} map {$_->{$feature}} @sampleFeatureAbundanceHash{@sampleList};
@abundanceList = scale(@abundanceList) if($scale);
print join("\t", (map {defined($_) ? $_ : ''} @tokenHash{@featureColumnList}), @abundanceList), "\n";
}
}
}
sub scale {
my @valueList = @_;
my $mean = 0;
$mean += $_ foreach(@valueList);
$mean /= scalar(@valueList);
my $sd = 0;
$sd += ($_ - $mean) ** 2 foreach(@valueList);
$sd /= scalar(@valueList) - 1;
$sd = $sd ** 0.5;
return map {($_ - $mean) / $sd} @valueList;
}
sub getTaxonName {
my ($taxonId) = @_;
if(defined(my $taxon = $db->get_taxon(-taxonid => $taxonId))) {
do {
return $taxon->scientific_name if($taxon->rank eq $taxonRank);
} while(defined($taxon = $taxon->ancestor));
}
return '';
}