maint: restructure to use Dist::Zilla
[bioperl-live.git] / lib / Bio / SeqIO / largefasta.pm
blobc11c089a1a27829b179c7983752176369db90eb5
1 # BioPerl module for Bio::SeqIO::largefasta
3 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 # Cared for by Jason Stajich
7 # Copyright Jason Stajich
9 # You may distribute this module under the same terms as perl itself
10 # _history
12 # POD documentation - main docs before the code
14 =head1 NAME
16 Bio::SeqIO::largefasta - method i/o on very large fasta sequence files
18 =head1 SYNOPSIS
20 Do not use this module directly. Use it via the Bio::SeqIO class.
22 =head1 DESCRIPTION
24 This object can transform Bio::Seq objects to and from fasta flat
25 file databases.
27 This module handles very large sequence files by using the
28 Bio::Seq::LargePrimarySeq module to store all the sequence data in
29 a file. This can be a problem if you have limited disk space on your
30 computer because this will effectively cause 2 copies of the sequence
31 file to reside on disk for the life of the
32 Bio::Seq::LargePrimarySeq object. The default location for this is
33 specified by the L<File::Spec>-E<gt>tmpdir routine which is usually /tmp
34 on UNIX. If a sequence file is larger than the swap space (capacity
35 of the /tmp dir) this could cause problems for the machine. It is
36 possible to set the directory where the temporary file is located by
37 adding the following line to your code BEFORE calling next_seq. See
38 L<Bio::Seq::LargePrimarySeq> for more information.
40 $Bio::Seq::LargePrimarySeq::DEFAULT_TEMP_DIR = 'newdir';
42 =head1 FEEDBACK
44 =head2 Mailing Lists
46 User feedback is an integral part of the evolution of this and other
47 Bioperl modules. Send your comments and suggestions preferably to one
48 of the Bioperl mailing lists. Your participation is much appreciated.
50 bioperl-l@bioperl.org - General discussion
51 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
53 =head2 Support
55 Please direct usage questions or support issues to the mailing list:
57 I<bioperl-l@bioperl.org>
59 rather than to the module maintainer directly. Many experienced and
60 reponsive experts will be able look at the problem and quickly
61 address it. Please include a thorough description of the problem
62 with code and data examples if at all possible.
64 =head2 Reporting Bugs
66 Report bugs to the Bioperl bug tracking system to help us keep track
67 the bugs and their resolution. Bug reports can be submitted via the web:
69 https://github.com/bioperl/bioperl-live/issues
71 =head1 AUTHORS - Jason Stajich
73 Email: jason@bioperl.org
75 =head1 APPENDIX
77 The rest of the documentation details each of the object
78 methods. Internal methods are usually preceded with a _
80 =cut
82 # Let the code begin...
84 package Bio::SeqIO::largefasta;
85 use vars qw($FASTALINELEN);
86 use strict;
88 use Bio::Seq::SeqFactory;
90 $FASTALINELEN = 60;
91 use base qw(Bio::SeqIO);
93 sub _initialize {
94 my($self,@args) = @_;
95 $self->SUPER::_initialize(@args);
96 if( ! defined $self->sequence_factory ) {
97 $self->sequence_factory(Bio::Seq::SeqFactory->new
98 (-verbose => $self->verbose(),
99 -type => 'Bio::Seq::LargePrimarySeq'));
103 =head2 next_seq
105 Title : next_seq
106 Usage : $seq = $stream->next_seq()
107 Function: returns the next sequence in the stream
108 Returns : A Bio::Seq::LargePrimarySeq object
109 Args : NONE
111 =cut
113 sub next_seq {
114 my ($self) = @_;
115 # local $/ = "\n";
116 my $largeseq = $self->sequence_factory->create();
117 my ($id,$fulldesc,$entry);
118 my $count = 0;
119 my $seen = 0;
120 while( defined ($entry = $self->_readline) ) {
121 if( $seen == 1 && $entry =~ /^\s*>/ ) {
122 $self->_pushback($entry);
123 return $largeseq;
125 # if ( ($entry eq '>') || eof($self->_fh) ) { $seen = 1; next; }
126 if ( ($entry eq '>') ) { $seen = 1; next; }
127 elsif( $entry =~ /\s*>(.+?)$/ ) {
128 $seen = 1;
129 ($id,$fulldesc) = ($1 =~ /^\s*(\S+)\s*(.*)$/)
130 or $self->warn("Can't parse fasta header");
131 $largeseq->display_id($id);
132 $largeseq->primary_id($id);
133 $largeseq->desc($fulldesc);
134 } else {
135 $entry =~ s/\s+//g;
136 $largeseq->add_sequence_as_string($entry);
138 (++$count % 1000 == 0 && $self->verbose() > 0) && print "line $count\n";
140 return unless $seen;
141 return $largeseq;
144 =head2 write_seq
146 Title : write_seq
147 Usage : $stream->write_seq(@seq)
148 Function: writes the $seq object into the stream
149 Returns : 1 for success and 0 for error
150 Args : Bio::Seq object
153 =cut
155 sub write_seq {
156 my ($self,@seq) = @_;
157 foreach my $seq (@seq) {
158 my $top = $seq->id();
159 if ($seq->can('desc') and my $desc = $seq->desc()) {
160 $desc =~ s/\n//g;
161 $top .= " $desc";
163 $self->_print (">",$top,"\n");
164 my $end = $seq->length();
165 my $start = 1;
166 while( $start <= $end ) {
167 my $stop = $start + $FASTALINELEN - 1;
168 $stop = $end if( $stop > $end );
169 $self->_print($seq->subseq($start,$stop), "\n");
170 $start += $FASTALINELEN;
174 $self->flush if $self->_flush_on_write && defined $self->_fh;
175 return 1;