~ubuntu-branches/ubuntu/hoary/bioperl/hoary

« back to all changes in this revision

Viewing changes to Bio/SeqIO/largefasta.pm

  • Committer: Bazaar Package Importer
  • Author(s): Matt Hope
  • Date: 2004-04-18 14:24:11 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040418142411-gr92uexquw4w8liq
Tags: 1.4-1
* New upstream release
* Examples and working code are installed by default to usr/bin,
  this has been moved to usr/share/doc/bioperl/bin

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# $Id: largefasta.pm,v 1.10 2002/02/18 17:17:25 bosborne Exp $
 
1
# $Id: largefasta.pm,v 1.18 2002/12/27 19:42:32 birney Exp $
2
2
# BioPerl module for Bio::SeqIO::largefasta
3
3
#
4
4
# Cared for by Jason Stajich
56
56
or the web:
57
57
 
58
58
  bioperl-bugs@bio.perl.org
59
 
  http://bio.perl.org/bioperl-bugs/
 
59
  http://bugzilla.bioperl.org/
60
60
 
61
61
=head1 AUTHORS - Jason Stajich
62
62
 
63
 
Email: jason@chg.mc.duke.edu
 
63
Email: jason@bioperl.org
64
64
 
65
65
=head1 APPENDIX
66
66
 
77
77
# Object preamble - inherits from Bio::Root::Object
78
78
 
79
79
use Bio::SeqIO;
80
 
use Bio::Seq::LargePrimarySeq;
81
 
use Bio::Seq::LargeSeq;
 
80
use Bio::Seq::SeqFactory;
82
81
 
83
82
$FASTALINELEN = 60;
84
83
@ISA = qw(Bio::SeqIO);
85
84
 
 
85
sub _initialize {
 
86
  my($self,@args) = @_;
 
87
  $self->SUPER::_initialize(@args);    
 
88
  if( ! defined $self->sequence_factory ) {
 
89
      $self->sequence_factory(new Bio::Seq::SeqFactory
 
90
                              (-verbose => $self->verbose(), 
 
91
                               -type => 'Bio::Seq::LargePrimarySeq'));      
 
92
  }
 
93
}
 
94
 
86
95
=head2 next_seq
87
96
 
88
97
 Title   : next_seq
94
103
=cut
95
104
 
96
105
sub next_seq {
97
 
    return next_primary_seq( $_[0], 1 );
98
 
}
99
 
 
100
 
=head2 next_primary_seq
101
 
 
102
 
 Title   : next_seq
103
 
 Usage   : $seq = $stream->next_seq()
104
 
 Function: returns the next sequence in the stream
105
 
 Returns : Bio::PrimarySeq object
106
 
 Args    : NONE
107
 
 
108
 
=cut
109
 
 
110
 
sub next_primary_seq {
111
 
  my( $self, $as_next_seq ) = @_;
112
 
 
 
106
    my ($self) = @_;
113
107
#  local $/ = "\n";
114
 
  my $largepseq = new Bio::Seq::LargePrimarySeq();
115
 
  my ($id,$fulldesc,$entry);
116
 
  my $count = 0;
117
 
  my $seen = 0;
118
 
  while( defined ($entry = $self->_readline) ) {
119
 
      if( $seen == 1 && $entry =~ /^\s*>/ ) {
120
 
          $self->_pushback($entry);
121
 
          return $largepseq;
122
 
      }
123
 
      if ( $entry eq '>' ) { $seen = 1; next; }      
124
 
      elsif( $entry =~ /\s*>(.+?)$/ ) {
125
 
          $seen = 1;
126
 
          ($id,$fulldesc) = ($1 =~ /^\s*(\S+)\s*(.*)$/)
127
 
              or $self->warn("Can't parse fasta header");
128
 
          $largepseq->display_id($id);
129
 
          $largepseq->primary_id($id);    
130
 
          $largepseq->desc($fulldesc);
131
 
      } else {
132
 
          $entry =~ s/\s+//g;
133
 
          $largepseq->add_sequence_as_string($entry);
134
 
      }
135
 
      (++$count % 1000 == 0 && $self->verbose() > 0) && print "line $count\n";
136
 
  }
137
 
  if( ! $seen ) { return undef; } 
138
 
  if( $as_next_seq ) {
139
 
      return new Bio::Seq::LargeSeq(-primaryseq => $largepseq );      
140
 
  } else {
141
 
      return $largepseq;
142
 
  }
 
108
    my $largeseq = $self->sequence_factory->create();
 
109
    my ($id,$fulldesc,$entry);
 
110
    my $count = 0;
 
111
    my $seen = 0;
 
112
    while( defined ($entry = $self->_readline) ) {
 
113
        if( $seen == 1 && $entry =~ /^\s*>/ ) {
 
114
            $self->_pushback($entry);
 
115
            return $largeseq;
 
116
        }
 
117
#       if ( ($entry eq '>') || eof($self->_fh) ) { $seen = 1; next; }      
 
118
        if ( ($entry eq '>')  ) { $seen = 1; next; }      
 
119
        elsif( $entry =~ /\s*>(.+?)$/ ) {
 
120
            $seen = 1;
 
121
            ($id,$fulldesc) = ($1 =~ /^\s*(\S+)\s*(.*)$/)
 
122
                or $self->warn("Can't parse fasta header");
 
123
            $largeseq->display_id($id);
 
124
            $largeseq->primary_id($id);   
 
125
            $largeseq->desc($fulldesc);
 
126
        } else {
 
127
            $entry =~ s/\s+//g;
 
128
            $largeseq->add_sequence_as_string($entry);
 
129
        }
 
130
        (++$count % 1000 == 0 && $self->verbose() > 0) && print "line $count\n";
 
131
    }
 
132
    if( ! $seen ) { return undef; } 
 
133
    return $largeseq;
143
134
}
144
135
 
145
136
=head2 write_seq
171
162
         $start += $FASTALINELEN;
172
163
     }
173
164
   }
 
165
 
 
166
   $self->flush if $self->_flush_on_write && defined $self->_fh;
174
167
   return 1;
175
168
}
176
169