« lib/Bio/AssemblyImprovement/Quiver/Main.pm
↑ Summary
lib/Bio/AssemblyImprovement/Abacas/DelimiterRole.pm »
| 1 | package Bio::AssemblyImprovement::Util::FastqTools; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Take a fastq file and calculate some useful statistics and values | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Statistics::Lite qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Cwd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Cwd 'abs_path'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use File::Basename; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | #use GD::Graph::histogram; | ||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Bio::SeqIO; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | with 'Bio::AssemblyImprovement::Util::GetReadLengthsRole'; | |||||||||||||||||||||||||||
| 15 | 4 | with 'Bio::AssemblyImprovement::Util::ZipFileRole'; | |||||||||||||||||||||||||||
| 16 | 4 | with 'Bio::AssemblyImprovement::Util::UnzipFileIfNeededRole'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 4 | has 'input_filename' => ( is => 'ro', isa => 'Str' , required => 1 ); | |||||||||||||||||||||||||||
| 19 | 4 | has 'single_cell' => ( is => 'ro', isa => 'Bool', default => 0); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # sub draw_histogram_of_read_lengths { | ||||||||||||||||||||||||||||
| 23 | # my ($self) = @_; | ||||||||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||||||||
| 25 | # my $fastq_file = $self->_gunzip_file_if_needed( $self->input_filename ); | ||||||||||||||||||||||||||||
| 26 | # | ||||||||||||||||||||||||||||
| 27 | # my $arrayref = $self->_get_read_lengths($fastq_file); | ||||||||||||||||||||||||||||
| 28 | # | ||||||||||||||||||||||||||||
| 29 | # # Set graph details | ||||||||||||||||||||||||||||
| 30 | # my $graph = new GD::Graph::histogram(400,600); | ||||||||||||||||||||||||||||
| 31 | # $graph->set( | ||||||||||||||||||||||||||||
| 32 | # x_label => 'Read length', | ||||||||||||||||||||||||||||
| 33 | # y_label => 'Number of reads', | ||||||||||||||||||||||||||||
| 34 | # title => 'Histogram of read lengths for '.$self->input_filename, | ||||||||||||||||||||||||||||
| 35 | # x_labels_vertical => 1, | ||||||||||||||||||||||||||||
| 36 | # bar_spacing => 0, | ||||||||||||||||||||||||||||
| 37 | # shadow_depth => 1, | ||||||||||||||||||||||||||||
| 38 | # shadowclr => 'dred', | ||||||||||||||||||||||||||||
| 39 | # transparent => 0, | ||||||||||||||||||||||||||||
| 40 | # ) | ||||||||||||||||||||||||||||
| 41 | # or warn $graph->error; | ||||||||||||||||||||||||||||
| 42 | # #Draw the graph | ||||||||||||||||||||||||||||
| 43 | # my $gd = $graph->plot($arrayref) or die $graph->error; | ||||||||||||||||||||||||||||
| 44 | # #Store the graph | ||||||||||||||||||||||||||||
| 45 | # open(IMG, '>histogram.png') or die "Could not open a file called histogram.png"; | ||||||||||||||||||||||||||||
| 46 | # binmode IMG; | ||||||||||||||||||||||||||||
| 47 | # print IMG $gd->png; | ||||||||||||||||||||||||||||
| 48 | # | ||||||||||||||||||||||||||||
| 49 | # $self->_cleaup_after_ourselves($fastq_file); | ||||||||||||||||||||||||||||
| 50 | # | ||||||||||||||||||||||||||||
| 51 | # | ||||||||||||||||||||||||||||
| 52 | # } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | ▶ | sub calculate_kmer_sizes { | ||||||||||||||||||||||||||
|
Subroutine
calculate_kmer_sizes: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 57 | 2 | my %kmer_size; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | ▶ | if ($self->single_cell) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 1 | my $read_length = $self->first_read_length(); | |||||||||||||||||||||||||||
| 61 | 1 | $kmer_size{min} = int($read_length * 0.5); | |||||||||||||||||||||||||||
| 62 | 1 | $kmer_size{max} = int($read_length * 0.95); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||
| 65 | 1 | my $fastq_file = $self->_gunzip_file_if_needed( $self->input_filename ); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | my $arrayref = $self->_get_read_lengths($fastq_file); | |||||||||||||||||||||||||||
| 68 | 1 | my $median = median(@$arrayref); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Set a minimum median so that the min kmer length stays at a reasonable value | ||||||||||||||||||||||||||||
| 71 | 1 | ▶ | if($median < 30){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | $median = 30; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | $kmer_size{min} = int($median*0.66); #66% of median read length will be minimum kmer length | |||||||||||||||||||||||||||
| 76 | 1 | $kmer_size{max} = int($median*0.90); #90% of median read length will be maximum kmer length | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | $self->_cleaup_after_ourselves($fastq_file); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 2 | ▶ | if($kmer_size{min} % 2 == 0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | $kmer_size{min}--; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 2 | ▶ | if($kmer_size{max} % 2 == 0){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 1 | $kmer_size{max}--; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 2 | return %kmer_size; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | ▶ | sub calculate_coverage { | ||||||||||||||||||||||||||
|
Subroutine
calculate_coverage: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | my ($self, $expected_genome_size) = @_; | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | unless ($expected_genome_size) { return undef }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 1 | my $fastq_file = $self->_gunzip_file_if_needed( $self->input_filename ); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 1 | my $arrayref = $self->_get_read_lengths($fastq_file); | |||||||||||||||||||||||||||
| 101 | 1 | my $total_length_of_reads = 0; | |||||||||||||||||||||||||||
| 102 | 1 | $total_length_of_reads += $_ for @$arrayref; | |||||||||||||||||||||||||||
| 103 | 1 | my $coverage = $total_length_of_reads/$expected_genome_size; | |||||||||||||||||||||||||||
| 104 | 1 | $coverage = sprintf ("%.0f", $coverage); # Rounding it up | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | $self->_cleaup_after_ourselves($fastq_file); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | return $coverage; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # If the reads are ordered as /1, /2, /1, /2...this subroutine will split them | ||||||||||||||||||||||||||||
| 113 | # into two separate files | ||||||||||||||||||||||||||||
| 114 | 1 | ▶ | sub split_fastq { | ||||||||||||||||||||||||||
|
Subroutine
split_fastq: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 1 | my ($self, $outfile_forward, $outfile_reverse) = @_; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 1 | my $fastq_file = $self->_gunzip_file_if_needed( $self->input_filename ); | |||||||||||||||||||||||||||
| 119 | 1 | my $fastq_obj = Bio::SeqIO->new( -file => $fastq_file , -format => 'Fastq' ); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 1 | ▶ | $outfile_forward ||= 'forward.fastq'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$outfile_forward ||= "forward.fastq"
MC/DC0%0/1
$outfile_forward ||= "forward.fastq"
$outfile_forward
Truth table50%1/2
A
A= $outfile_forward
| |||||||||||||||||||||||||||||
| 122 | 1 | ▶ | $outfile_reverse ||= 'reverse.fastq'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$outfile_reverse ||= "reverse.fastq"
MC/DC0%0/1
$outfile_reverse ||= "reverse.fastq"
$outfile_reverse
Truth table50%1/2
A
A= $outfile_reverse
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 1 | my $forward_fastq = Bio::SeqIO->new( -file => ">$outfile_forward" , -format => 'Fastq' ); | |||||||||||||||||||||||||||
| 125 | 1 | my $reverse_fastq = Bio::SeqIO->new( -file => ">$outfile_reverse" , -format => 'Fastq' ); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | ▶ | while(my $seq = $fastq_obj->next_seq()){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | # Example ID: @IL9_4021:8:1:8:1892#7/1 | ||||||||||||||||||||||||||||
| 129 | 6 | ▶ | if($seq->id() =~ m/\/1$/ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 3 | $forward_fastq->write_seq($seq); | |||||||||||||||||||||||||||
| 131 | }else{ | ||||||||||||||||||||||||||||
| 132 | 3 | $reverse_fastq->write_seq($seq); | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1 | $self->_cleaup_after_ourselves($fastq_file); | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1 | return $outfile_forward; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 3 | ▶ | sub _cleaup_after_ourselves { | ||||||||||||||||||||||||||
|
Subroutine
_cleaup_after_ourselves: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 3 | my ($self, $fastq_file) = @_; | |||||||||||||||||||||||||||
| 146 | 3 | ▶ | if($fastq_file ne abs_path($self->input_filename)){ #Which means we unzipped it | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 2 | unlink($fastq_file); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # Returns the length of the first read in the file | ||||||||||||||||||||||||||||
| 155 | 3 | ▶ | sub first_read_length { | ||||||||||||||||||||||||||
|
Subroutine
first_read_length: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 156 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 157 | 3 | my $fastq_obj; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 3 | my $fastq_file = $self->_gunzip_file_if_needed( $self->input_filename ); | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 3 | ▶ | if ( $fastq_file =~ /\.fq$/ || $fastq_file =~ /\.fastq$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$fastq_file =~ /\.fq$/ or $fastq_file =~ /\.fastq$/
MC/DC0%0/2
$fastq_file =~ /\.fq$/ or $fastq_file =~ /\.fastq$/
$fastq_file =~ /\.fq$/
$fastq_file =~ /\.fastq$/
Truth table33%1/3
A or B
A= $fastq_file =~ /\.fq$/B= $fastq_file =~ /\.fastq$/
| |||||||||||||||||||||||||||||
| 162 | 3 | $fastq_obj = Bio::SeqIO->new( -file => $fastq_file , -format => 'Fastq'); | |||||||||||||||||||||||||||
| 163 | }else { | ||||||||||||||||||||||||||||
| 164 | 0 | die "File '$self->input_filename' not recognised as fastq. Needs to end in .fastq[.gz] or .fq[.gz]"; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 3 | my $seq = $fastq_obj->next_seq(); | |||||||||||||||||||||||||||
| 168 | 3 | return length($seq->seq()); | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 4 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 172 | 4 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 173 | 4 | 1; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | __END__ | ||||||||||||||||||||||||||||