| 1 | # BioPerl module for Bio::Index::Fastq | ||||||||||||
| 2 | # | ||||||||||||
| 3 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||||||||||||
| 4 | # | ||||||||||||
| 5 | # Cared for by Tony Cox <avc@sanger.ac.uk> | ||||||||||||
| 6 | # | ||||||||||||
| 7 | # You may distribute this module under the same terms as perl itself | ||||||||||||
| 8 | |||||||||||||
| 9 | # POD documentation - main docs before the code | ||||||||||||
| 10 | |||||||||||||
| 11 | =head1 NAME | ||||||||||||
| 12 | |||||||||||||
| 13 | Bio::Index::Fastq - Interface for indexing (multiple) fastq files | ||||||||||||
| 14 | |||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||
| 16 | |||||||||||||
| 17 | # Complete code for making an index for several | ||||||||||||
| 18 | # fastq files | ||||||||||||
| 19 | use Bio::Index::Fastq; | ||||||||||||
| 20 | use strict; | ||||||||||||
| 21 | |||||||||||||
| 22 | my $Index_File_Name = shift; | ||||||||||||
| 23 | my $inx = Bio::Index::Fastq->new( | ||||||||||||
| 24 | '-filename' => $Index_File_Name, | ||||||||||||
| 25 | '-write_flag' => 1); | ||||||||||||
| 26 | $inx->make_index(@ARGV); | ||||||||||||
| 27 | |||||||||||||
| 28 | # Print out several sequences present in the index | ||||||||||||
| 29 | # in Fastq format | ||||||||||||
| 30 | use Bio::Index::Fastq; | ||||||||||||
| 31 | use strict; | ||||||||||||
| 32 | |||||||||||||
| 33 | my $Index_File_Name = shift; | ||||||||||||
| 34 | my $inx = Bio::Index::Fastq->new('-filename' => $Index_File_Name); | ||||||||||||
| 35 | my $out = Bio::SeqIO->new('-format' => 'Fastq','-fh' => \*STDOUT); | ||||||||||||
| 36 | |||||||||||||
| 37 | foreach my $id (@ARGV) { | ||||||||||||
| 38 | my $seq = $inx->fetch($id); # Returns Bio::Seq::Quality object | ||||||||||||
| 39 | $out->write_seq($seq); | ||||||||||||
| 40 | } | ||||||||||||
| 41 | |||||||||||||
| 42 | # or, alternatively | ||||||||||||
| 43 | my $id; | ||||||||||||
| 44 | my $seq = $inx->get_Seq_by_id($id); #identical to fetch | ||||||||||||
| 45 | |||||||||||||
| 46 | =head1 DESCRIPTION | ||||||||||||
| 47 | |||||||||||||
| 48 | Inherits functions for managing dbm files from Bio::Index::Abstract.pm, | ||||||||||||
| 49 | and provides the basic funtionality for indexing fastq files, and | ||||||||||||
| 50 | retrieving the sequence from them. Note: for best results 'use strict'. | ||||||||||||
| 51 | |||||||||||||
| 52 | Bio::Index::Fastq supports the Bio::DB::BioSeqI interface, meaning | ||||||||||||
| 53 | it can be used as a Sequence database for other parts of bioperl | ||||||||||||
| 54 | |||||||||||||
| 55 | =head1 FEED_BACK | ||||||||||||
| 56 | |||||||||||||
| 57 | =head2 Mailing Lists | ||||||||||||
| 58 | |||||||||||||
| 59 | User feedback is an integral part of the evolution of this and other | ||||||||||||
| 60 | Bioperl modules. Send your comments and suggestions preferably to one | ||||||||||||
| 61 | of the Bioperl mailing lists. Your participation is much appreciated. | ||||||||||||
| 62 | |||||||||||||
| 63 | bioperl-l@bioperl.org - General discussion | ||||||||||||
| 64 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||||||||||||
| 65 | |||||||||||||
| 66 | =head2 Support | ||||||||||||
| 67 | |||||||||||||
| 68 | Please direct usage questions or support issues to the mailing list: | ||||||||||||
| 69 | |||||||||||||
| 70 | I<bioperl-l@bioperl.org> | ||||||||||||
| 71 | |||||||||||||
| 72 | rather than to the module maintainer directly. Many experienced and | ||||||||||||
| 73 | reponsive experts will be able look at the problem and quickly | ||||||||||||
| 74 | address it. Please include a thorough description of the problem | ||||||||||||
| 75 | with code and data examples if at all possible. | ||||||||||||
| 76 | |||||||||||||
| 77 | =head2 Reporting Bugs | ||||||||||||
| 78 | |||||||||||||
| 79 | Report bugs to the Bioperl bug tracking system to help us keep track | ||||||||||||
| 80 | the bugs and their resolution. Bug reports can be submitted via the | ||||||||||||
| 81 | web: | ||||||||||||
| 82 | |||||||||||||
| 83 | https://github.com/bioperl/bioperl-live/issues | ||||||||||||
| 84 | |||||||||||||
| 85 | =head1 AUTHOR - Tony Cox | ||||||||||||
| 86 | |||||||||||||
| 87 | Email - avc@sanger.ac.uk | ||||||||||||
| 88 | |||||||||||||
| 89 | =head1 APPENDIX | ||||||||||||
| 90 | |||||||||||||
| 91 | The rest of the documentation details each of the object | ||||||||||||
| 92 | methods. Internal methods are usually preceded with a _ | ||||||||||||
| 93 | |||||||||||||
| 94 | =cut | ||||||||||||
| 95 | |||||||||||||
| 96 | |||||||||||||
| 97 | # Let the code begin... | ||||||||||||
| 98 | |||||||||||||
| 99 | |||||||||||||
| 100 | package Bio::Index::Fastq; | ||||||||||||
| 101 | 1 | $Bio::Index::Fastq::VERSION = '1.7.8'; | |||||||||||
| 102 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 103 | |||||||||||||
| 104 | 1 | ▶ | use Bio::Seq; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 105 | |||||||||||||
| 106 | 1 | ▶ | use base qw(Bio::Index::AbstractSeq); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 107 | |||||||||||||
| 108 | # | ||||||||||||
| 109 | # Suggested fix by Michael G Schwern <schwern@pobox.com> to | ||||||||||||
| 110 | # get around a clash with CPAN shell... | ||||||||||||
| 111 | # | ||||||||||||
| 112 | |||||||||||||
| 113 | 0 | ▶ | sub _version { | ||||||||||
|
Subroutine
_version: not called CC=1 SCAR=6.9
| |||||||||||||
| 114 | 0 | return 0.2; | |||||||||||
| 115 | } | ||||||||||||
| 116 | |||||||||||||
| 117 | =head2 _file_format | ||||||||||||
| 118 | |||||||||||||
| 119 | Title : _file_format | ||||||||||||
| 120 | Function: The file format for this package, which is needed | ||||||||||||
| 121 | by the SeqIO system when reading the sequence. | ||||||||||||
| 122 | Returns : 'Fastq' | ||||||||||||
| 123 | |||||||||||||
| 124 | =cut | ||||||||||||
| 125 | |||||||||||||
| 126 | 0 | ▶ | sub _file_format { | ||||||||||
|
Subroutine
_file_format: not called CC=1 SCAR=6.9
| |||||||||||||
| 127 | 0 | return 'Fastq'; | |||||||||||
| 128 | } | ||||||||||||
| 129 | |||||||||||||
| 130 | |||||||||||||
| 131 | |||||||||||||
| 132 | =head2 _index_file | ||||||||||||
| 133 | |||||||||||||
| 134 | Title : _index_file | ||||||||||||
| 135 | Usage : $index->_index_file( $file_name, $i ) | ||||||||||||
| 136 | Function: Specialist function to index FASTQ format files. | ||||||||||||
| 137 | Is provided with a filename and an integer | ||||||||||||
| 138 | by make_index in its SUPER class. | ||||||||||||
| 139 | Example : | ||||||||||||
| 140 | Returns : | ||||||||||||
| 141 | Args : | ||||||||||||
| 142 | |||||||||||||
| 143 | =cut | ||||||||||||
| 144 | |||||||||||||
| 145 | 0 | ▶ | sub _index_file { | ||||||||||
|
Subroutine
_index_file: not called CC=5 SCAR=34.0
| |||||||||||||
| 146 | 0 | my( $self, | |||||||||||
| 147 | $file, # File name | ||||||||||||
| 148 | $i, # Index-number of file being indexed | ||||||||||||
| 149 | ) = @_; | ||||||||||||
| 150 | |||||||||||||
| 151 | 0 | my( $begin, # Offset from start of file of the start | |||||||||||
| 152 | # of the last found record. | ||||||||||||
| 153 | ); | ||||||||||||
| 154 | |||||||||||||
| 155 | 0 | $begin = 0; | |||||||||||
| 156 | |||||||||||||
| 157 | 0 | my $id_parser = $self->id_parser; | |||||||||||
| 158 | 0 | my $c = 0; | |||||||||||
| 159 | 0 | ▶ | open my $FASTQ, '<', $file or $self->throw("Could not read file '$file': $!"); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 160 | |||||||||||||
| 161 | # In Windows, text files have '\r\n' as line separator, but when reading in | ||||||||||||
| 162 | # text mode Perl will only show the '\n'. This means that for a line "ABC\r\n", | ||||||||||||
| 163 | # "length $_" will report 4 although the line is 5 bytes in length. | ||||||||||||
| 164 | # We assume that all lines have the same line separator and only read current line. | ||||||||||||
| 165 | 0 | my $init_pos = tell($FASTQ); | |||||||||||
| 166 | 0 | my $curr_line = <$FASTQ>; | |||||||||||
| 167 | 0 | my $pos_diff = tell($FASTQ) - $init_pos; | |||||||||||
| 168 | 0 | my $correction = $pos_diff - length $curr_line; | |||||||||||
| 169 | 0 | seek $FASTQ, $init_pos, 0; # Rewind position to proceed to read the file | |||||||||||
| 170 | |||||||||||||
| 171 | # Main indexing loop | ||||||||||||
| 172 | 0 | ▶ | while (<$FASTQ>) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 173 | 0 | ▶ | if (/^@/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 174 | 0 | my $begin = tell($FASTQ) - length( $_ ) - $correction; | |||||||||||
| 175 | 0 | foreach my $id (&$id_parser($_)) { | |||||||||||
| 176 | 0 | $self->add_record($id, $i, $begin); | |||||||||||
| 177 | 0 | $c++; | |||||||||||
| 178 | } | ||||||||||||
| 179 | } | ||||||||||||
| 180 | } | ||||||||||||
| 181 | |||||||||||||
| 182 | 0 | close $FASTQ; | |||||||||||
| 183 | 0 | return ($c); | |||||||||||
| 184 | } | ||||||||||||
| 185 | |||||||||||||
| 186 | =head2 id_parser | ||||||||||||
| 187 | |||||||||||||
| 188 | Title : id_parser | ||||||||||||
| 189 | Usage : $index->id_parser( CODE ) | ||||||||||||
| 190 | Function: Stores or returns the code used by record_id to | ||||||||||||
| 191 | parse the ID for record from a string. Useful | ||||||||||||
| 192 | for (for instance) specifying a different | ||||||||||||
| 193 | parser for different flavours of FASTQ file. | ||||||||||||
| 194 | Returns \&default_id_parser (see below) if not | ||||||||||||
| 195 | set. If you supply your own id_parser | ||||||||||||
| 196 | subroutine, then it should expect a fastq | ||||||||||||
| 197 | description line. An entry will be added to | ||||||||||||
| 198 | the index for each string in the list returned. | ||||||||||||
| 199 | Example : $index->id_parser( \&my_id_parser ) | ||||||||||||
| 200 | Returns : ref to CODE if called without arguments | ||||||||||||
| 201 | Args : CODE | ||||||||||||
| 202 | |||||||||||||
| 203 | =cut | ||||||||||||
| 204 | |||||||||||||
| 205 | 0 | ▶ | sub id_parser { | ||||||||||
|
Subroutine
id_parser: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 206 | 0 | my( $self, $code ) = @_; | |||||||||||
| 207 | |||||||||||||
| 208 | 0 | ▶ | if ($code) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 209 | 0 | $self->{'_id_parser'} = $code; | |||||||||||
| 210 | } | ||||||||||||
| 211 | 0 | ▶ | return $self->{'_id_parser'} || \&default_id_parser; | ||||||||||
|
Condition0%0/2
$self->{'_id_parser'} || \&Bio::Index::Fastq::default_id_parser
MC/DC0%0/1
$self->{'_id_parser'} || \&Bio::Index::Fastq::default_id_parser
$self->{'_id_parser'}
Truth table0%0/2
A
A= $self->{'_id_parser'}
| |||||||||||||
| 212 | } | ||||||||||||
| 213 | |||||||||||||
| 214 | |||||||||||||
| 215 | |||||||||||||
| 216 | =head2 default_id_parser | ||||||||||||
| 217 | |||||||||||||
| 218 | Title : default_id_parser | ||||||||||||
| 219 | Usage : $id = default_id_parser( $header ) | ||||||||||||
| 220 | Function: The default Fastq ID parser for Fastq.pm | ||||||||||||
| 221 | Returns $1 from applying the regexp /^>\s*(\S+)/ | ||||||||||||
| 222 | to $header. | ||||||||||||
| 223 | Returns : ID string | ||||||||||||
| 224 | Args : a fastq header line string | ||||||||||||
| 225 | |||||||||||||
| 226 | =cut | ||||||||||||
| 227 | |||||||||||||
| 228 | 0 | ▶ | sub default_id_parser { | ||||||||||
|
Subroutine
default_id_parser: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 229 | 0 | ▶ | if ($_[0] =~ /^@\s*(\S+)/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 230 | 0 | return $1; | |||||||||||
| 231 | } else { | ||||||||||||
| 232 | 0 | return; | |||||||||||
| 233 | } | ||||||||||||
| 234 | } | ||||||||||||
| 235 | |||||||||||||
| 236 | 1 | 1; | |||||||||||