-
Notifications
You must be signed in to change notification settings - Fork 1
/
analysis.pl
executable file
·331 lines (217 loc) · 9.13 KB
/
analysis.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
#!/usr/bin/perl
use strict;
use warnings;
# --------------------------------------------------------------------------- #
# File: analysis.pl
# Date created: 16 March 2021
# Date last modified: 24 March 2021
# Author: Eliot Stanton ([email protected])
# Description: Pull genome deliverables from sequencing data pipeline.
# --------------------------------------------------------------------------- #
# Variables imported by this script:
my $var_accession = $ARGV[0];
my $dir_in = $ARGV[1];
my $dir_out = $ARGV[2];
# Define filepaths used by this script:
# TODO: Fix using fastq for original FASTQ file
$dir_out = "$dir_out\/$var_accession";
my $file_out = "$dir_out\/$var_accession\_stats.txt";
my $file_quast = "$dir_out\/quast/report.txt";
my $file_kraken = "$dir_out\/$var_accession\_kraken2_report.txt";
my $file_FASTQ1 = "$dir_out/$var_accession\_filtered_R1.fastq";
my $file_FASTQ2 = "$dir_in/$var_accession\_R1.fastq";
# Data structures used by script:
my @array_out;
# Define temporary array to hold data and store in @array_out:
my @array_temp = ( "Accession", "Original reads", "Processed reads", "Dropped (%)",
"Assembled genome length", "GC (%)", "Contigs", "Genus",
"Kraken2 genus alignment", "Species", "Kraken2 species alignment",
"Avg genome length", "Genome ratio", "Coverage depth",
"Date analysed" );
push @array_out, \@array_temp;
# Define variables used by script:
my $var_num_reads = 0;
my $var_ori_reads = 1;
my $var_dropped = 0;
my $var_GC = 0;
my $var_contigs = 0;
my $var_length = 0;
my $var_genus = "unknown";
my $var_species = "unknown";
my $var_align_genus = 0;
my $var_align_species = 0;
my $var_avg_length = 0;
my $var_ratio = 0;
my $var_coverage = 0;
my $var_date = `date +%F`;
chomp $var_date;
# --------------------------------------------------------------------------- #
# Import $file_kraken into @array_kraken:
my @array_kraken = @{ file_to_array ( $file_kraken ) };
# Import $file_quast into @array_quast:
my @array_quast = @{ file_to_array ( $file_quast ) };
# Identify putative species identification from Kraken2 report:
( $var_genus, $var_align_genus, $var_species, $var_align_species ) = Kraken2 ( \@array_kraken );
# --------------------------------------------------------------------------- #
# Determine number of original paired reads in $file_FASTQ2:
$var_ori_reads = `echo \$\(zcat $file_FASTQ2 | wc \-l) \/4 \| bc`;
chomp $var_ori_reads;
# Define variable for URL used to download average genome length:
my $var_html = "https\:\/\/www\.ncbi\.nlm\.nih\.gov\/genome\/\?term\=$var_genus\+$var_species";
# Download taxon webpage from NCBI website for average genome length:
system "wget --output-document $var_accession.html -q $var_html";
# Convert website to array:
my @array_html = @{ file_to_array ( "$var_accession.html" ) };
# Scrape average length from @array_html:
$var_avg_length = Scrape (\@array_html);
# Convert length from Mbp to bp:
$var_avg_length *= 1000000;
# Cleanup and remove the HTML file:
system ( "rm $var_accession.html" );
# --------------------------------------------------------------------------- #
# Iterate through @array_quast and find sum total assembly length, contig
# number, GC content, read number, and read depth:
for ( my $j = 0; $j < scalar @array_quast; $j++ ) {
my @array_temp = split (/\s/, $array_quast[$j]);
if ( $array_quast[$j] =~ /Total length \(\>\= 0 bp\) /) {
$var_length = $array_temp[10];
}
if ( $array_quast[$j] =~ /\# contigs / ) {
$var_contigs = $array_temp[20];
}
if ( $array_quast[$j] =~ /GC \(%\)/ ) {
$var_GC = $array_temp[23];
}
if ( $array_quast[$j] =~ /\# left/ ) {
$var_num_reads = $array_temp[23];
}
if ( $array_quast[$j] =~ /Avg\. coverage depth/ ) {
$var_coverage = $array_temp[11];
}
}
# Calculate dropped read percentage:
$var_dropped = ( 1 - $var_num_reads/$var_ori_reads ) * 100;
# Round dropped read percentage to two decimals:
$var_dropped = sprintf( "%.2f", $var_dropped );
# Determine genome ratio:
$var_ratio = $var_length/($var_avg_length) unless $var_avg_length == 0;
# Round genome ratio to two decimals:
$var_ratio = sprintf( "%.2f", $var_ratio );
# Store data in a temporary array and push to @array_out:
my @array_temp2 = ($var_accession, $var_ori_reads, $var_num_reads,
$var_dropped, $var_length, $var_GC, $var_contigs,
$var_genus, $var_align_genus, $var_species,
$var_align_species, $var_avg_length,
$var_ratio, $var_coverage, $var_date);
push @array_out, \@array_temp2;
print "Sequence and assembly analysis:\n";
print "\tAccession:\t\t\t$var_accession\n";
print "\tOriginal reads:\t\t\t$var_ori_reads\n";
print "\tProcessed reads:\t\t$var_num_reads\n";
print "\tDropped reads:\t\t\t$var_dropped %\n";
print "\tAssembly length:\t\t$var_length bp\n";
print "\tGC:\t\t\t\t$var_GC %\n";
print "\tContigs:\t\t\t$var_contigs\n";
print "\tGenus:\t\t\t\t$var_genus\n";
print "\tKraken2 genus alignment:\t$var_align_genus %\n";
print "\tSpecies:\t\t\t$var_species\n";
print "\tKraken2 species alignment:\t$var_align_species %\n";
print "\tAvg genome length:\t\t$var_avg_length bp\n";
print "\tGenome ratio:\t\t\t$var_ratio\n";
print "\tCoverage:\t\t\t$var_coverage x\n";
print "\tDate analyzed:\t\t\t$var_date\n";
# --------------------------------------------------------------------------- #
# Write data in @array_out to $file_out:
open ( my $file_write, '>', $file_out ) or die $!;
for ( my $i = 0; $i < scalar @array_out; $i++ ) {
my $var_string = join ( "\t", @{ $array_out[$i]} );
print $file_write "$var_string\n";
# print "$var_string\n";
}
# Close $file_write:
close ( $file_write );
# --------------------------------------------------------------------------- #
# Subroutine for reading a file into an array as strings:
sub file_to_array {
# Arguments:
my ( $file_in ) = @_;
# Data structures:
my @array_out;
# Open $file_in:
open ( my $file_read, '<', $file_in ) or die "Unable to open $file_in!";
# Store each line of $file_in as a string in @array_out:
while ( <$file_read> ){
chomp $_;
push @array_out, $_;
}
# Close $file_in:
close ( $file_read );
# Return refernece to @array_out and end subroutine:
return \@array_out;
}
# --------------------------------------------------------------------------- #
# Subroutine for pulling data from Kraken2 report:
sub Kraken2 {
# Arguments:
my ( $array_in ) = @_;
# Data structures:
my @array_in = @$array_in;
# Variables:
my $var_genus = "unknown";
my $var_genus_align = 0;
my $var_species = "unknown";
my $var_species_align = 0;
# Iterate through @array_in and process:
for ( my $i = 0; $i < scalar @array_in; $i++ ) {
# Split string into a temporary array for extracting variables:
my @array_temp = split (" ", $array_in[$i]);
$array_in[$i] = \@array_temp;
}
@array_in = sort { $a -> [0] <=> $b -> [0] } @array_in;
# Iterate through @array_in and process:
for ( my $i = 0; $i < scalar @array_in; $i++ ) {
my $var_percent = $array_in[$i][0];
my $var_rank = $array_in[$i][3];
next if $var_percent < 30;
# print "$i: @{$array_in[$i]}\n";
# If ranking is at genus level record:
if ( $var_rank eq "G" || $var_rank eq "G1" ) {
my $var_scalar = scalar @{$array_in[$i]};
$var_genus = $array_in[$i][$var_scalar-1];
$var_genus_align = $array_in[$i][0];
}
# If ranking is at the species level record:
elsif ( $var_rank eq "S" ) {
my $var_scalar = scalar @{$array_in[$i]};
$var_species_align = $array_in[$i][0];
$var_species = $array_in[$i][$var_scalar-1];
}
}
return ( $var_genus, $var_genus_align, $var_species, $var_species_align );
}
# --------------------------------------------------------------------------- #
# Subroutine for scrapping average genome length from NCBI HTML file:
sub Scrape {
# Parameters:
my ( $array_html ) = @_;
# Data structures:
my @array_html = @$array_html;
# Variables:
my $var_avg_length;
# Scrape average length from @array_html:
for ( my $j = 0; $j < scalar @array_html; $j++ ) {
if ( $array_html[$j] =~ /median total length/ ) {
my @array_temp = split ( /[\>,\<,\:]+/, $array_html[$j] );
for ( my $k = 0; $k < scalar @array_temp; $k++ ) {
if ( $array_temp[$k] =~ /median total length/) {
$var_avg_length = $array_temp[$k+1];
$var_avg_length =~ s/^\s+//;
last;
}
}
}
}
# Return $var_avg_length and end subroutine:
return $var_avg_length;
}
# --------------------------------------------------------------------------- #