-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathotu_tables
272 lines (243 loc) · 6.61 KB
/
otu_tables
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env perl
use warnings;
=head1 NAME
otu_tables
=head1 SYNOPSIS
make_otu_tables [--help] [--verbose] [--threshold=INTEGER] [--depth=INTEGER] [--names=<FILE>]--classifier=<sina/rdp> --samples=<FOLDER> --classification=<FILE> --sequences=<FASTA>
--samples: folder containing the -usearch_global output for each sample to be analysed,
and nothing else (important!)
--classification: allrank result from RDP classifier or SINA
--sequences: fasta file with representatives from each OTU
--classifier: which ever was used to generate the classification; default sina-cl
sina-cl: command line sina
sina-ol: online sina
rdp: online RDP
tsv: tsv file with OTU ID on column 1 and classification on the last column
--threshold: minimum precentage confidence level to retain an RDP classification; default 70
--depth: maximum taxonomic depth to retain a SINA classification; default 5
--names: tsv connecting sample file name to sample name. If not provided, file name is used
--help: This info.
=head1 AUTHOR
=cut
use warnings;
use strict;
use Bio::SeqIO;
use Getopt::Long;
use Pod::Usage;
my $help = 0;
my $verbose = 0;
my $samples_folder;
my $class_file;
my $sequences;
my $name_file="";
my $classifier="sina-cl";
my $threshold=70;
my $depth=5;
GetOptions(
"samples=s" => \$samples_folder,
"classification=s" => \$class_file,
"threshold=i" => \$threshold,
"classifier=s" => \$classifier,
"names=s" => \$name_file,
"sequences=s" => \$sequences,
"depth=i" => \$depth,
"help!" => \$help,
"verbose!" => \$verbose,
);
pod2usage(0) if $help;
pod2usage(-msg => "Need a samples folder", -exitval => 1) unless $samples_folder;
opendir(DIR, $samples_folder) or die "Cannot open $samples_folder";
pod2usage(-msg => "Need a classification file", -exitval => 1) unless $class_file;
open CLASS, $class_file or die "Cannot open $class_file";
pod2usage(-msg => "Classifier must be one of the four allowed ones", -exitval => 1) unless (($classifier eq "sina-cl") or ($classifier eq "rdp") or ($classifier eq "tsv") or ($classifier eq "sina-ol"));
pod2usage(-msg => "Need a fasta file", -exitval => 1) unless $sequences;
my $seqfile = Bio::SeqIO->new(-file=>"$sequences", -format => "fasta") or die "Can't open $sequences";
if ($name_file ne ""){
open NAMES, $name_file or die "Cannot open $name_file";
}
sub rdp_parse{
my $line = $_[0];
my @samples = @{$_[1]};
my $samples_ref = $_[2];
my $seq_ref = $_[3];
my $threshold = $_[4];
my @fields = split(';',$line);
my $otu = shift (@fields);
my $print="$otu\t";
shift @fields; shift @fields; shift @fields;
foreach my $sample (@samples){
if (exists ${$samples_ref}{$otu}{$sample}){
$print = $print.${$samples_ref}{$otu}{$sample}."\t";
}
else{
$print = $print."0\t";
}
}
my $count = 0;
my $classified = 'F';
foreach my $taxon (@fields){
unless ($taxon=~/%$/){
$fields[$count+1]=~/(\d+)/;
my $confidence = $1;
if ($confidence >= $threshold){
$taxon=~s/ /_/g;
$print = $print.$taxon.";";
$classified = 'T';
}
}
$count++;
}
$print = $print."Unclassified;" if $classified eq 'F';
print "$print\t".${$seq_ref}{$otu}."\n";
}
sub sina_cl_parse{
my $line = $_[0];
my @samples = @{$_[1]};
my $samples_ref = $_[2];
my $seq_ref = $_[3];
my $depth = $_[4];
chomp $line;
my @fields = split(',',$line);
my $otu = $fields[0];
my $print = "$otu\t";
foreach my $sample (@samples){
if (exists ${$samples_ref}{$otu}{$sample}){
$print = $print.${$samples_ref}{$otu}{$sample}."\t";
}
else{
$print = $print."0\t";
}
}
my $taxonomy=$fields[-5];
my @taxonomy=split(';',$taxonomy);
my $count = 1;
foreach my $taxon (@taxonomy){
if ($count <= $depth and $taxon!~/^"/){
$taxon=~s/ /_/g;
$print = $print.$taxon.";";
$count++;
}
}
print "$print\t".${$seq_ref}{$otu}."\n";
}
sub sina_ol_parse{
my $line = $_[0];
my @samples = @{$_[1]};
my $samples_ref = $_[2];
my $seq_ref = $_[3];
my $depth = $_[4];
chomp $line;
my @fields = split('";"',$line);
my $otu = $fields[2];
my $print = "$otu\t";
foreach my $sample (@samples){
if (exists ${$samples_ref}{$otu}{$sample}){
$print = $print.${$samples_ref}{$otu}{$sample}."\t";
}
else{
$print = $print."0\t";
}
}
my $taxonomy=$fields[-1];
chop $taxonomy;
my @taxonomy=split(';',$taxonomy);
my $count = 1;
foreach my $taxon (@taxonomy){
if ($count <= $depth and $taxon!~/^"/){
$taxon=~s/ /_/g;
$print = $print.$taxon.";";
$count++;
}
}
print "$print\t".${$seq_ref}{$otu}."\n";
}
sub tsv_parse{
my $line = $_[0];
my @samples = @{$_[1]};
my $samples_ref = $_[2];
my $seq_ref = $_[3];
my $depth = $_[4];
chomp $line;
my @fields = split('\t',$line);
my $otu = $fields[0];
my $print = "$otu\t";
foreach my $sample (@samples){
if (exists ${$samples_ref}{$otu}{$sample}){
$print = $print.${$samples_ref}{$otu}{$sample}."\t";
}
else{
$print = $print."0\t";
}
}
my $taxonomy=$fields[-1];
chop $taxonomy;
my @taxonomy=split(';',$taxonomy);
my $count = 1;
foreach my $taxon (@taxonomy){
if ($count <= $depth and $taxon!~/^"/){
$taxon=~s/ /_/g;
$print = $print.$taxon.";";
$count++;
}
}
print "$print\t".${$seq_ref}{$otu}."\n";
}
my %sequences;
while (my $seq = $seqfile->next_seq){
$sequences{$seq->display_id} = uc($seq->seq);
}
my %names;
if ($name_file ne ""){
while (my $line = <NAMES>){
chomp $line;
$line=~/^(\S+)\t(\S+)/;
$names{$1} = $2;
}
}
close NAMES;
my %samples;
my @samples = readdir(DIR);
shift @samples; shift @samples;
foreach my $sample (@samples){
open (SAMPLE, "$samples_folder/$sample") or die "Failed to open $sample";
while (my $line = <SAMPLE>){
$line=~/(\S+)$/;
my $otu=$1;
unless ($otu eq '*'){
$samples{$otu}{$sample}++;
}
}
close SAMPLE;
}
close DIR;
@samples = sort(@samples);
print "OTU\t";
foreach my $sample (@samples){
if (exists($names{$sample})){
print $names{$sample}."\t";
}
else{
print $sample."\t";
}
}
print "Taxonomy\tCentroid\n";
while (my $line = <CLASS>){
chomp $line;
$"="\t"; ## Sets the separator for arrays as tab
if ($classifier eq "rdp"){
if ($line!~/^(Classifier\:|Taxonomical |Query File|Submit Date|Confidence |Symbol |)/ and ($line ne "")){
rdp_parse($line, \@samples, \%samples, \%sequences, $threshold);
}
}
elsif(($classifier eq "sina-cl") and ($line!~/name/) and ($line ne "")){
sina_cl_parse($line, \@samples, \%samples, \%sequences, $depth);
}
elsif(($classifier eq "sina-ol") and ($line!~/job_id/) and ($line ne "")){
sina_ol_parse($line, \@samples, \%samples, \%sequences, $depth);
}
elsif($classifier eq "tsv" and ($line ne "")){
tsv_parse($line, \@samples, \%samples, \%sequences, $depth);
}
}
close CLASS;