PageRenderTime 56ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Bio/Index/Fastq.pm

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