forked from ding-lab/VirusScan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment_report_virus_gi.pl
371 lines (314 loc) · 12 KB
/
assignment_report_virus_gi.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/perl
use strict;
use Switch;
use Bio::SearchIO;
my $usage = '
This script will read corresponding files in the given director and
generate a report. It will report in each library, for each category,
how many total sequence were assigned to this category, how many were
assigned by BLASTN, how many were assigned by TBLASTX, the range of
percent identity. It will also generate four fasta format files which
contain viral reads from blastn, tblastx, all viral reads and reads
that can not be assigned to any category.
perl script <sample dir> <input repeatmasked good seq(full path)><ref genome>
<sample dir> = full path to the directory holding files for the given
library
e.g. .../S21_Rota_other
<ref genome> = 1. Human
2. Mouse
3. Worm (C. elegans, C. briggsae)
4. Mouse lemur (Microcebus_murinus)
5. sand fly (Lutzomyia longipalpis)
';
die $usage unless scalar @ARGV == 3;
my ( $dir, $input_good_seq_fasta_file, $ref_genome_choice ) = @ARGV;
# get all the viral read sequences
my %viral_reads_blastn = ();
my %viral_reads_blastx = ();
my %best_e_blastn = (); # viral_read_ID => best_e value for this read in blastn
my %best_e_blastx = (); # viral_read_ID => best_e value for this read in blastx
my @blast_files_blastn = (); # all blastn.out files
my @blast_files_blastx = (); # all blastx.out files
my @unassigned_reads = ();
####################################
my @ambiguous_reads = (); #cai added 12/2010
####################################
# read in original sequences
my @temp = split("\/", $dir);
my $lib_name = pop @temp;
# print "lib is $lib_name\n";
#my $fasta_file = $dir."/".$lib_name.".fa.cdhit_out.masked.goodSeq";
my $fasta_file = $input_good_seq_fasta_file; #cai changed, added segmasker
my %seq = &read_FASTA_data($fasta_file);
my $out1 = $dir."/".$lib_name.".gi.AssignmentReport";
open (OUT1, ">$out1") or die "can not open file $out1!\n";
my $OUT2 = $dir."/".$lib_name.".gi.ViralReads_all.fa";
open (OUT2, ">$OUT2") or die "can not open file $OUT2!\n";
my $OUT3 = $dir."/".$lib_name.".gi.unassigned.fa";
open (OUT3, ">$OUT3") or die "can not open file $OUT3!\n";
##################################cai added 12/2010
my $out4 = $dir."/".$lib_name.".gi.AmbiguousReads_all.fa";
open (OUT4, ">$out4") or die "can not open file $out4!\n";
##################################
# category => num of sequence assigned to this category by blastn
my %blastn = (
"Bacteria" => 0,
"Fungi" => 0,
"Homo" => 0,
"Mus" => 0,
"Phage" => 0,
"Viruses" => 0,
"other" => 0,
"unassigned" => 0,
##################################cai added 12/2010
"Ambiguous" => 0,
##################################cai added
);
# category => num of sequence assigned to this category by blastn of Reference genome
my %blastn_RefG = ();
foreach my $key (keys %blastn) {
$blastn_RefG{$key} = 0;
}
# category => num of sequence assigned to this category by tblastx of viral genome
my %blastx = ();
foreach my $key (keys %blastn) {
$blastx{$key} = 0;
}
# viral_lineage => number of reads assigned to this lineage in the library
my %num_reads = ();
my %blast_readinfo =(); # readID => information about this read
my %lineage_blastn = (); # lineage => [read ID]
my %lineage_gi = ();
my %lineage_blastx = (); # lineage => [read ID]
opendir(DH, $dir) or die "Can not open dir $dir!\n";
foreach my $name (readdir DH) {
# name is either file name or directory for splited files
my $full_path = $dir."/".$name;
# full_path= dir/goodSeq_RefGblast
if ($name =~ /goodSeq_RefGblast$/) { # Reference genome blast result
# enter subdirectory where blastn results resides
opendir (RefGDIR, $full_path) or die "can not open dir $full_path!\n";
foreach my $blast_file (readdir RefGDIR) {
if ($blast_file =~ /RefGblast\.parsed$/) {
my $parsed = $full_path."/".$blast_file;
open (IN, $parsed) or die "can not open file $parsed!\n";
while (<IN>) {
if ($_ =~ /#/) { # skip comment line
next;
}
chomp;
my ($read_ID, $length, $category, $lineage, $hit_name, $e_value) = split("\t", $_);
# print "readID = $read_ID, length = $length, category = $category, lineage = $lineage, hit name = $hit_name, e = $e_value\n";
$blastn_RefG{$ref_genome_choice}++;
}
close IN;
}
}
closedir RefGDIR;
} # finish .RefGblast.parsed
# full_path= dir/RefGfiltered_BLASTN
if ($name =~ /RefGfiltered_BLASTN$/) {
# enter subdirectory where blastx results resides
opendir (BNDIR, $full_path) or die "can not open dir $full_path!\n";
foreach my $blast_file (readdir BNDIR) {
if ($blast_file =~ /blastn\.parsed$/) {
# print "blastn parsed file $blast_file\n";
my $blast_out = $blast_file;
$blast_out =~ s/\.blastn\.parsed/\.blastn\.out/;
$blast_out = $full_path."/".$blast_out;
my $blast_s = $blast_file;
$blast_s =~ s/\.blastn\.parsed/\.blastn\.summary/;
$blast_s = $full_path."/".$blast_s;
push @blast_files_blastn, $blast_s;
my $parsed = $full_path."/".$blast_file;
#print $parsed,"\n";
##################################cai changed 12/2010
&collect_information($parsed, \%blastn, \%viral_reads_blastn, \%best_e_blastn, \%lineage_blastn, \%lineage_gi, \%num_reads, \@unassigned_reads, \@ambiguous_reads);
##################################
}
}
closedir BNDIR;
} # finish .blastn.parsed
# full_path= dir/RefGfiltered_BLASTN
#if ($name =~ /BLASTX_NR$/i) {
# enter subdirectory where blastx results resides
# opendir (BXDIR, $full_path) or die "can not open dir $full_path!\n";
# foreach my $blast_file (readdir BXDIR) {
# if ($blast_file =~ /blastx\.parsed$/) {
# print "blastn parsed file $blast_file\n";
# my $blast_out = $blast_file;
# $blast_out =~ s/\.blastx\.parsed/\.blastx\.out/;
# $blast_out = $full_path."/".$blast_out;
# push @blast_files_blastx, $blast_out;
# my $parsed = $full_path."/".$blast_file;
##################################cai changed 12/2010
# &collect_information($parsed, \%blastx, \%viral_reads_blastx, \%best_e_blastx, \%lineage_blastx, \%num_reads, \@unassigned_reads, \@ambiguous_reads);
##################################
# }
# }
# closedir BXDIR;
#} # finish .blastx.parsed
}
close DH;
# get detailed information about each viral read
&get_viral_read_info(\@blast_files_blastn, \%blast_readinfo);
#&get_viral_read_info( \@blast_files_blastx, "blastx", \%viral_reads_blastx, \%best_e_blastx, \%blast_readinfo);
# print out report for this library
print OUT1 $dir, "\n";
printf OUT1 "%12s\t%7s\t%7s\t%7s\t%7s\t%7s\t%7s\t%7s\n", "category", "total", "BN_RefG", "BN", "BX_NR";
foreach my $key (sort {$a cmp $b } keys %blastx) {
printf OUT1 "%12s\t%7d\t%7d\t%7d\t%7d\n", $key, $blastn_RefG{$key}+$blastn{$key}+$blastx{$key},$blastn_RefG{$key}, $blastn{$key}, $blastx{$key};
}
print OUT1 "\n###########################################################\n\n";
foreach my $gi (sort {$num_reads{$a} <=> $num_reads{$b}} keys %num_reads) {
print OUT1 $gi, "\t", $lineage_gi{$gi}, "\ttotal number of reads: ", $num_reads{$gi}, "\n\n";
print OUT1 "QueryName\tQuerylength\t HitName \tHitLen\t HitDesc \tAlnLen\t%ID\tHitStart\tHitEnd\te\n";
if (defined $lineage_blastn{$gi}) {
if (scalar @{$lineage_blastn{$gi}}) {
print OUT1 "reads from blastn:\n";
foreach my $read (sort {$a cmp $b} @{$lineage_blastn{$gi}}) {
print OUT1 $blast_readinfo{$read};
}
}
}
#if (defined $lineage_blastx{$lineage}) {
# if (scalar @{$lineage_blastx{$lineage}}) {
# print OUT1 "reads from blastx:\n";
# foreach my $read (sort {$a cmp $b} @{$lineage_blastx{$lineage}}) {
# print OUT1 $blast_readinfo{$read};
# }
# }
#}
print OUT1 "\n##################################################\n\n";
}
# get all the viral reads and put into output file:
foreach my $gi (keys %num_reads) {
foreach my $read (@{$lineage_blastn{$gi}}) {
print OUT2 ">$read\n";
print OUT2 $seq{$read}, "\n";
}
}
#foreach my $read (@{$lineage_blastx{$lineage}}) {
# print OUT2 ">$read\n";
# print OUT2 $seq{$read}, "\n";
#}
#}
# get all unassigned reads
#foreach my $read (@unassigned_reads) {
# print OUT3 ">$read\n";
# print OUT3 $seq{$read}, "\n";
#}
######################cai added
#foreach my $read (@ambiguous_reads) {
# print OUT4 ">$read\n";
# print OUT4 $seq{$read}, "\n";
#}
#####################
print OUT1 "# Finished Assignment Report\n";
exit;
#####################################################################################
# collecte information from given directory
sub collect_information {
##################################cai changed 12/2010
my ($infile, $category_hash_ref, $viral_reads_hash_ref, $best_e_hash_ref, $lineage_hash_ref, $lineage_hash_gi, $num_reads_hash_ref, $unassigned_reads_arr_ref, $ambiguous_reads_arr_ref) = @_;
##################################
open (IN, $infile) or die "can not open file $infile!\n";
while (<IN>) {
if ($_ =~ /#/) { # skip comment line
next;
}
chomp;
my ($read_ID, $length, $category, $lineage, $hit_name, $e_value) = split("\t", $_);
# print "readID = $read_ID, length = $length, category = $category, lineage = $lineage, hit name = $hit_name, e = $e_value\n";
my $gid=0;
if($hit_name=~/gi\|(\d+)\|/) { $gid=$1; $lineage_hash_gi->{$gid}=$lineage; }
switch ($category ) {
case "Bacteria" { $category_hash_ref->{"Bacteria"}++ }
case "Fungi" { $category_hash_ref->{"Fungi"}++ }
case "Homo" { $category_hash_ref->{"Homo"}++ }
case "Mus" { $category_hash_ref->{"Mus"}++ }
case "Phage" {$category_hash_ref->{"Phage"}++ }
case "Viruses" { $category_hash_ref->{"Viruses"}++ }
case "other" {$category_hash_ref->{"other"}++ }
case "unassigned" {$category_hash_ref->{"unassigned"}++}
case "Ambiguous" {$category_hash_ref->{"Ambiguous"}++ } #cai added
}
if (($category eq "Viruses") && $gid!=0) {
$viral_reads_hash_ref->{$read_ID} = 1;
$best_e_hash_ref->{$read_ID} = $e_value;
if (!(defined $lineage_hash_ref->{$gid})) {
$lineage_hash_ref->{$gid} = [$read_ID];
}
else {
push @{$lineage_hash_ref->{$gid}}, $read_ID;
}
if (defined $num_reads_hash_ref->{$gid}) {
$num_reads_hash_ref->{$gid}++;
}
else {
$num_reads_hash_ref->{$gid} = 1;
}
##################################cai added 12/2010
}elsif ($category eq "Ambiguous"){
push @{$ambiguous_reads_arr_ref}, $read_ID;
##################################
}elsif ($category eq "unassigned") {
push @{$unassigned_reads_arr_ref}, $read_ID;
}
}
close IN;
}
############################################################################
sub read_FASTA_data () {
my $fastaFile = shift @_;
#keep old read seperator and set new read seperator to ">"
my $oldseperator = $/;
$/ = ">";
my %fastaSeq;
open (FAfile, $fastaFile) or die "Can't Open FASTA file: $fastaFile";
while (my $line = <FAfile>){
# Discard blank lines
if ($line =~ /^\s*$/) {
next;
}
# discard comment lines
elsif ($line =~ /^\s*#/) {
next;
}
# discard the first line which only has ">", keep the rest
elsif ($line ne ">") {
chomp $line;
my @rows = ();
@rows = split (/\n/, $line);
my $temp = shift @rows;
my @temp = split(/\s+/, $temp);
my $name = shift @temp;
my $Seq = join("", @rows);
$Seq =~ s/\s//g; #remove white space
$fastaSeq{$name} = $Seq;
}
}
# check
# foreach my $key (keys %fastaSeq){
# print "Here is the key for fasta seq: $key \t $fastaSeq{$key}\n";
# }
#reset the read seperator
$/ = $oldseperator;
close FAfile;
return %fastaSeq;
}
#############################################################################
# get detailed information about each viral read
sub get_viral_read_info {
my ($report_file_ref,$blast_readinfo_hash_ref) = @_;
my $report; # blast report object
foreach my $file (@{$report_file_ref}) {
foreach my $line (`cat $file`)
{
if($line=~/Finished summary/) { next; }
else {
my @ss=split("\t",$line);
$blast_readinfo_hash_ref->{$ss[0]} = $line;}
}
}
}