| 1 | # BioPerl module for FAST::Bio::SeqIO::fasta | ||
| 2 | # | ||
| 3 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 4 | # | ||
| 5 | # Cared for by Ewan Birney <birney@ebi.ac.uk> | ||
| 6 | # and Lincoln Stein <lstein@cshl.org> | ||
| 7 | # | ||
| 8 | # Copyright Ewan Birney & Lincoln Stein | ||
| 9 | # | ||
| 10 | # You may distribute this module under the same terms as perl itself | ||
| 11 | # _history | ||
| 12 | # October 18, 1999 Largely rewritten by Lincoln Stein | ||
| 13 | |||
| 14 | # POD documentation - main docs before the code | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | FAST::Bio::SeqIO::fasta - fasta sequence input/output stream | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | Do not use this module directly. Use it via the FAST::Bio::SeqIO class. | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | This object can transform FAST::Bio::Seq objects to and from fasta flat | ||
| 27 | file databases. | ||
| 28 | |||
| 29 | =head1 FEEDBACK | ||
| 30 | |||
| 31 | =head2 Mailing Lists | ||
| 32 | |||
| 33 | User feedback is an integral part of the evolution of this and other | ||
| 34 | Bioperl modules. Send your comments and suggestions preferably to one | ||
| 35 | of the Bioperl mailing lists. Your participation is much appreciated. | ||
| 36 | |||
| 37 | bioperl-l@bioperl.org - General discussion | ||
| 38 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 39 | |||
| 40 | =head2 Support | ||
| 41 | |||
| 42 | Please direct usage questions or support issues to the mailing list: | ||
| 43 | |||
| 44 | I<bioperl-l@bioperl.org> | ||
| 45 | |||
| 46 | rather than to the module maintainer directly. Many experienced and | ||
| 47 | reponsive experts will be able look at the problem and quickly | ||
| 48 | address it. Please include a thorough description of the problem | ||
| 49 | with code and data examples if at all possible. | ||
| 50 | |||
| 51 | =head2 Reporting Bugs | ||
| 52 | |||
| 53 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 54 | the bugs and their resolution. Bug reports can be submitted via the | ||
| 55 | web: | ||
| 56 | |||
| 57 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 58 | |||
| 59 | =head1 AUTHORS - Ewan Birney & Lincoln Stein | ||
| 60 | |||
| 61 | Email: birney@ebi.ac.uk | ||
| 62 | lstein@cshl.org | ||
| 63 | |||
| 64 | =head1 CONTRIBUTORS | ||
| 65 | |||
| 66 | Jason Stajich, jason-at-bioperl.org | ||
| 67 | |||
| 68 | =head1 APPENDIX | ||
| 69 | |||
| 70 | The rest of the documentation details each of the object | ||
| 71 | methods. Internal methods are usually preceded with a _ | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | # Let the code begin... | ||
| 76 | |||
| 77 | package FAST::Bio::SeqIO::fasta; | ||
| 78 | use strict; | ||
| 79 | use warnings; | ||
| 80 | |||
| 81 | use FAST::Bio::Seq::SeqFastaSpeedFactory; | ||
| 82 | |||
| 83 | use parent qw(FAST::Bio::SeqIO); | ||
| 84 | |||
| 85 | sub _initialize { | ||
| 86 | my ($self, @args) = @_; | ||
| 87 | $self->SUPER::_initialize(@args); | ||
| 88 | |||
| 89 | ## Initialize fasta specific parameters | ||
| 90 | ## There are some problems with _rearrange. If there's no value for one of | ||
| 91 | ## the parameters, it will return an empty value (not undef). This means we | ||
| 92 | ## can't just merge two hashes since the empty values would override the | ||
| 93 | ## defaults anyway. | ||
| 94 | my (%defs) = ( | ||
| 95 | "width" => 60, | ||
| 96 | "block" => "", # default is same as width | ||
| 97 | "preferred_id_type" => "display", | ||
| 98 | ); | ||
| 99 | foreach my $param (keys %defs) { | ||
| 100 | $self->$param( $self->_rearrange([$param], @args) || | ||
| 101 | $defs{$param}); | ||
| 102 | } | ||
| 103 | |||
| 104 | unless ( defined $self->sequence_factory ) { | ||
| 105 | $self->sequence_factory(FAST::Bio::Seq::SeqFastaSpeedFactory->new()); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 next_seq | ||
| 110 | |||
| 111 | Title : next_seq | ||
| 112 | Usage : $seq = $stream->next_seq() | ||
| 113 | Function: returns the next sequence in the stream | ||
| 114 | Returns : FAST::Bio::Seq object, or nothing if no more available | ||
| 115 | Args : NONE | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | sub next_seq { | ||
| 120 | my( $self ) = @_; | ||
| 121 | my $seq; | ||
| 122 | my $alphabet; | ||
| 123 | local $/ = "\n>"; | ||
| 124 | return unless my $entry = $self->_readline; | ||
| 125 | |||
| 126 | # Replacing chomp for s///, since chomp is not working in some cases | ||
| 127 | $entry =~ s/\n$//; | ||
| 128 | $entry =~ s/\r$//; | ||
| 129 | if ($entry =~ m/\A\s*\Z/s) { # very first one | ||
| 130 | return unless $entry = $self->_readline; | ||
| 131 | chomp($entry); | ||
| 132 | } | ||
| 133 | |||
| 134 | # this just checks the initial input; beyond that, due to setting $/ above, | ||
| 135 | # the > is part of the record separator and is removed | ||
| 136 | #DHA THE FOLLOWING COMMENTED BY DHA FOR BETTER PROCESSING OF DATA FROM STDIN | ||
| 137 | #DHA $self->throw("The sequence does not appear to be FASTA format ". | ||
| 138 | #DHA "(lacks a descriptor line '>')") if $. == 1 && $entry !~ /^>/; | ||
| 139 | |||
| 140 | $entry =~ s/^>//; | ||
| 141 | |||
| 142 | my ($top,$sequence) = split(/\n/,$entry,2); | ||
| 143 | defined $sequence && $sequence =~ s/>//g; | ||
| 144 | #my ($top,$sequence) = $entry =~ /^>?(.+?)\n+([^>]*)/s | ||
| 145 | # or $self->throw("Can't parse fasta entry"); | ||
| 146 | |||
| 147 | my ($id,$fulldesc); | ||
| 148 | if( $top =~ /^\s*(\S+)\s*(.*)/ ) { | ||
| 149 | ($id,$fulldesc) = ($1,$2); | ||
| 150 | } | ||
| 151 | |||
| 152 | if (defined $id && $id eq '') {$id=$fulldesc;} # FIX incase no space | ||
| 153 | # between > and name \AE | ||
| 154 | defined $sequence && $sequence =~ tr/ \t\n\r//d; # Remove whitespace | ||
| 155 | |||
| 156 | # for empty sequences we need to know the mol.type | ||
| 157 | $alphabet = $self->alphabet(); | ||
| 158 | if(defined $sequence && length($sequence) == 0) { | ||
| 159 | if(! defined($alphabet)) { | ||
| 160 | # let's default to dna | ||
| 161 | $alphabet = "dna"; | ||
| 162 | } | ||
| 163 | }# else { | ||
| 164 | # we don't need it really, so disable | ||
| 165 | # we want to keep this if SeqIO alphabet was set by user | ||
| 166 | # not sure if this could break something | ||
| 167 | #$alphabet = undef; | ||
| 168 | #} | ||
| 169 | |||
| 170 | $seq = $self->sequence_factory->create( | ||
| 171 | -seq => $sequence, | ||
| 172 | -id => $id, | ||
| 173 | # Ewan's note - I don't think this healthy | ||
| 174 | # but obviously to taste. | ||
| 175 | #-primary_id => $id, | ||
| 176 | -desc => $fulldesc, | ||
| 177 | -alphabet => $alphabet, | ||
| 178 | -direct => 1, | ||
| 179 | ); | ||
| 180 | |||
| 181 | # if there wasn't one before, set the guessed type | ||
| 182 | #unless ( defined $alphabet ) { | ||
| 183 | # don't assume that all our seqs are the same as the first one found | ||
| 184 | #$self->alphabet($seq->alphabet()); | ||
| 185 | #} | ||
| 186 | return $seq; | ||
| 187 | |||
| 188 | } | ||
| 189 | |||
| 190 | =head2 write_seq | ||
| 191 | |||
| 192 | Title : write_seq | ||
| 193 | Usage : $stream->write_seq(@seq) | ||
| 194 | Function: Writes the $seq object into the stream | ||
| 195 | Returns : 1 for success and 0 for error | ||
| 196 | Args : Array of 1 or more FAST::Bio::PrimarySeqI objects | ||
| 197 | |||
| 198 | =cut | ||
| 199 | |||
| 200 | sub write_seq { | ||
| 201 | my ($self,@seq) = @_; | ||
| 202 | my $width = $self->width; | ||
| 203 | my $block = $self->block; | ||
| 204 | |||
| 205 | ## take a reference for single string (the sequence) and add the whitespace | ||
| 206 | local *format_str = sub { | ||
| 207 | my $str = $_[0]; | ||
| 208 | my @lines = unpack ("(A$width)*", $$str); | ||
| 209 | if ($block >= $width) { | ||
| 210 | $$str = join ("\n", @lines)."\n"; | ||
| 211 | } else { | ||
| 212 | $$str = ""; | ||
| 213 | $$str .= join (" ", unpack ("(A$block)*", $_)) . "\n" foreach (@lines); | ||
| 214 | } | ||
| 215 | }; | ||
| 216 | |||
| 217 | foreach my $seq (@seq) { | ||
| 218 | $self->throw("Did not provide a valid FAST::Bio::PrimarySeqI object") | ||
| 219 | unless defined $seq && ref($seq) && $seq->isa('FAST::Bio::PrimarySeqI'); | ||
| 220 | |||
| 221 | # Allow for different ids | ||
| 222 | my $top; | ||
| 223 | my $id_type = $self->preferred_id_type; | ||
| 224 | if( $id_type =~ /^acc/i ) { | ||
| 225 | $top = $seq->accession_number(); | ||
| 226 | if( $id_type =~ /vers/i ) { | ||
| 227 | $top .= "." . $seq->version(); | ||
| 228 | } | ||
| 229 | } elsif($id_type =~ /^displ/i ) { | ||
| 230 | $self->warn("No whitespace allowed in FASTA ID [". $seq->display_id. "]") | ||
| 231 | if defined $seq->display_id && $seq->display_id =~ /\s/; | ||
| 232 | $top = $seq->display_id(); | ||
| 233 | $top = '' unless defined $top; | ||
| 234 | $self->warn("No whitespace allowed in FASTA ID [". $top. "]") | ||
| 235 | if defined $top && $top =~ /\s/; | ||
| 236 | } elsif($id_type =~ /^pri/i ) { | ||
| 237 | $top = $seq->primary_id(); | ||
| 238 | } | ||
| 239 | |||
| 240 | if ($seq->can('desc') and my $desc = $seq->desc()) { | ||
| 241 | $desc =~ s/\n//g; | ||
| 242 | $top .= " $desc"; | ||
| 243 | } | ||
| 244 | |||
| 245 | if( $seq->isa('FAST::Bio::Seq::LargeSeqI') ) { | ||
| 246 | $self->_print(">$top\n"); | ||
| 247 | # for large seqs, don't call seq(), it defeats the | ||
| 248 | # purpose of the largeseq functionality. instead get | ||
| 249 | # chunks of the seq, $width at a time | ||
| 250 | my $buff_max = 2000; | ||
| 251 | my $buff_size = int($buff_max/$width)*$width; #< buffer is even multiple of widths | ||
| 252 | my $seq_length = $seq->length; | ||
| 253 | my $num_chunks = int($seq_length/$buff_size+1); | ||
| 254 | for( my $c = 0; $c < $num_chunks; $c++ ) { | ||
| 255 | my $buff_end = $buff_size*($c+1); | ||
| 256 | $buff_end = $seq_length if $buff_end > $seq_length; | ||
| 257 | my $buff = $seq->subseq($buff_size*$c+1,$buff_end); | ||
| 258 | if($buff) { | ||
| 259 | format_str (\$buff); | ||
| 260 | $self->_print($buff); | ||
| 261 | } else { | ||
| 262 | $self->_print("\n"); | ||
| 263 | } | ||
| 264 | } | ||
| 265 | } else { | ||
| 266 | my $str = $seq->seq; | ||
| 267 | if(defined $str && length($str) > 0) { | ||
| 268 | format_str (\$str); | ||
| 269 | } else { | ||
| 270 | $str = "\n"; | ||
| 271 | } | ||
| 272 | $self->_print (">",$top,"\n",$str) or return; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | $self->flush if $self->_flush_on_write && defined $self->_fh; | ||
| 277 | return 1; | ||
| 278 | } | ||
| 279 | |||
| 280 | =head2 width | ||
| 281 | |||
| 282 | Title : width | ||
| 283 | Usage : $obj->width($newval) | ||
| 284 | Function: Get/Set the line width for FASTA output (not counting whitespace). | ||
| 285 | Returns : value of width | ||
| 286 | Args : newvalue (optional) | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | sub width { | ||
| 291 | my ($self,$value) = @_; | ||
| 292 | if (defined $value) { | ||
| 293 | $self->{'width'} = $value; | ||
| 294 | } | ||
| 295 | return $self->{'width'}; | ||
| 296 | } | ||
| 297 | |||
| 298 | =head2 block | ||
| 299 | |||
| 300 | Title : block | ||
| 301 | Usage : $obj->block($newval) | ||
| 302 | Function: Get/Set the length of each block for FASTA output. Sequence blocks | ||
| 303 | will be split with a space. Configuring block, to a value of 10 for | ||
| 304 | example, allows to easily indentify a position in a sequence by eye. | ||
| 305 | Default : same value used for width. | ||
| 306 | Returns : value of block | ||
| 307 | Args : newvalue (optional) | ||
| 308 | |||
| 309 | =cut | ||
| 310 | |||
| 311 | sub block { | ||
| 312 | my ($self,$value) = @_; | ||
| 313 | if (defined $value) { | ||
| 314 | $self->{'block'} = $value; | ||
| 315 | } | ||
| 316 | return $self->{'block'} || $self->width; | ||
| 317 | } | ||
| 318 | |||
| 319 | =head2 preferred_id_type | ||
| 320 | |||
| 321 | Title : preferred_id_type | ||
| 322 | Usage : $obj->preferred_id_type('accession') | ||
| 323 | Function: Get/Set the preferred type of identifier to use in the ">ID" position | ||
| 324 | for FASTA output. | ||
| 325 | Returns : string, one of values defined in @FAST::Bio::SeqIO::fasta::SEQ_ID_TYPES. | ||
| 326 | Default : display | ||
| 327 | Args : string when setting. This must be one of values defined in | ||
| 328 | @FAST::Bio::SeqIO::fasta::SEQ_ID_TYPES. Allowable values: | ||
| 329 | accession, accession.version, display, primary | ||
| 330 | Throws : fatal exception if the supplied id type is not in @SEQ_ID_TYPES. | ||
| 331 | |||
| 332 | =cut | ||
| 333 | |||
| 334 | our @SEQ_ID_TYPES = qw(accession accession.version display primary); | ||
| 335 | |||
| 336 | sub preferred_id_type { | ||
| 337 | my ($self,$type) = @_; | ||
| 338 | if (defined $type) { | ||
| 339 | if( ! grep lc($type) eq $_, @SEQ_ID_TYPES) { | ||
| 340 | $self->throw(-class=>'FAST::Bio::Root::BadParameter', | ||
| 341 | -text=>"Invalid ID type \"$type\". Must be one of: @SEQ_ID_TYPES"); | ||
| 342 | } | ||
| 343 | $self->{'_seq_id_type'} = lc($type); | ||
| 344 | } | ||
| 345 | $self->{'_seq_id_type'}; | ||
| 346 | } | ||
| 347 | |||
| 348 | 1; |