|
| 1 | +#!/soft/packages/perl/5.12.1/bin/perl |
| 2 | +use warnings; |
| 3 | +use strict; |
| 4 | + |
| 5 | +use Getopt::Long; |
| 6 | + |
| 7 | +my ($file, $dir, $file_format); |
| 8 | + |
| 9 | +GetOptions( 'file=s' => \$file, |
| 10 | + 'dir=s' => \$dir, |
| 11 | + 'file_format=s' => \$file_format ); |
| 12 | + |
| 13 | +### report keys: |
| 14 | +# bp_count, sequence_count, length_max, id_length_max, length_min, id_length_min, file_size, |
| 15 | +# average_length, standard_deviation_length, average_gc_content, standard_deviation_gc_content, |
| 16 | +# average_gc_ratio, standard_deviation_gc_ratio, ambig_char_count, ambig_sequence_count, average_ambig_chars |
| 17 | + |
| 18 | +my $filetype = ""; |
| 19 | +if ($file_format eq 'fastq') { |
| 20 | + $filetype = " -t fastq" |
| 21 | +} |
| 22 | + |
| 23 | +my @stats = `seq_length_stats.py -i '$dir/$file'$filetype -s`; |
| 24 | +chomp @stats; |
| 25 | + |
| 26 | +my $report = {}; |
| 27 | +foreach my $stat (@stats) { |
| 28 | + my ($key, $value) = split(/\t/, $stat); |
| 29 | + $report->{$key} = $value; |
| 30 | +} |
| 31 | +my $header = `head -1 '$dir/$file'`; |
| 32 | +my $options = '-s '.$report->{sequence_count}.' -a '.$report->{average_length}.' -d '.$report->{standard_deviation_length}.' -m '.$report->{length_max}; |
| 33 | +my $method = `tech_guess -f '$header' $options`; |
| 34 | + |
| 35 | +my $success = 1; |
| 36 | +my $message = ""; |
| 37 | +if ( $stats[0] =~ /^ERROR/i ) { |
| 38 | + $success = 0; |
| 39 | + my @parts = split(/\t/, $stats[0]); |
| 40 | + if ( @parts == 3 ) { |
| 41 | + $message = $parts[1] . ": " . $parts[2]; |
| 42 | + } else { |
| 43 | + $message = join(" ", @stats); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +# count unique ids |
| 48 | +my $unique_ids = 0; |
| 49 | +if ($file_format eq 'fasta') { |
| 50 | + $unique_ids = `grep '>' $dir/$file | cut -f1 -d' ' | sort -T $dir/.tmp -u | wc -l`; |
| 51 | + chomp $unique_ids; |
| 52 | +} |
| 53 | +elsif ($file_format eq 'fastq') { |
| 54 | + $unique_ids = `awk '0 == (NR + 3) % 4' $dir/$file | cut -f1 -d' ' | sort -T $dir/.tmp -u | wc -l`; |
| 55 | + chomp $unique_ids; |
| 56 | +} |
| 57 | + |
| 58 | +push(@stats, "unique_id_count\t$unique_ids"); |
| 59 | + |
| 60 | +# write results |
| 61 | +open(FH, ">>$dir/$file.stats_info") or die "could not open stats file for $dir/$file.stats_info: $!"; |
| 62 | +if ($success) { |
| 63 | + if ($report->{sequence_count} eq "0") { |
| 64 | + print FH "Error\tFile contains no sequences\n"; |
| 65 | + return 0; |
| 66 | + } |
| 67 | + foreach my $line (@stats) { |
| 68 | + print FH $line."\n"; |
| 69 | + } |
| 70 | + print FH "sequencing_method_guess\t$method"; |
| 71 | +} else { |
| 72 | + print FH "Error\t$message\n"; |
| 73 | +} |
| 74 | +close FH; |
0 commit comments