| 1 | # | ||
| 2 | # BioPerl module for FAST::Bio::AlignIO::largemultifasta | ||
| 3 | |||
| 4 | # based on the FAST::Bio::SeqIO::largefasta module | ||
| 5 | # by Ewan Birney <birney@ebi.ac.uk> | ||
| 6 | # and Lincoln Stein <lstein@cshl.org> | ||
| 7 | # | ||
| 8 | # and the SimpleAlign.pm module of Ewan Birney | ||
| 9 | # | ||
| 10 | # Copyright Albert Vilella | ||
| 11 | # | ||
| 12 | # You may distribute this module under the same terms as perl itself | ||
| 13 | # _history | ||
| 14 | # January 20, 2004 | ||
| 15 | # POD documentation - main docs before the code | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | FAST::Bio::AlignIO::largemultifasta - Largemultifasta MSA Sequence | ||
| 20 | input/output stream | ||
| 21 | |||
| 22 | =head1 SYNOPSIS | ||
| 23 | |||
| 24 | Do not use this module directly. Use it via the L<FAST::Bio::AlignIO> class. | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | This object can transform L<FAST::Bio::SimpleAlign> objects to and from | ||
| 29 | largemultifasta flat file databases. This is for the fasta sequence | ||
| 30 | format NOT FastA analysis program. To process the pairwise alignments | ||
| 31 | from a FastA (FastX, FastN, FastP, tFastA, etc) use the FAST::Bio::SearchIO | ||
| 32 | module. | ||
| 33 | |||
| 34 | Reimplementation of FAST::Bio::AlignIO::fasta modules so that creates | ||
| 35 | temporary files instead of keeping the whole sequences in memory. | ||
| 36 | |||
| 37 | =head1 FEEDBACK | ||
| 38 | |||
| 39 | =head2 Support | ||
| 40 | |||
| 41 | Please direct usage questions or support issues to the mailing list: | ||
| 42 | |||
| 43 | I<bioperl-l@bioperl.org> | ||
| 44 | |||
| 45 | rather than to the module maintainer directly. Many experienced and | ||
| 46 | reponsive experts will be able look at the problem and quickly | ||
| 47 | address it. Please include a thorough description of the problem | ||
| 48 | with code and data examples if at all possible. | ||
| 49 | |||
| 50 | =head2 Reporting Bugs | ||
| 51 | |||
| 52 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 53 | the bugs and their resolution. Bug reports can be submitted via the | ||
| 54 | web: | ||
| 55 | |||
| 56 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 57 | |||
| 58 | =head1 AUTHORS - Albert Vilella, Heikki Lehvaslaiho | ||
| 59 | |||
| 60 | Email: avilella-at-gmail-dot-com, heikki-at-bioperl-dot-org | ||
| 61 | |||
| 62 | |||
| 63 | =head1 APPENDIX | ||
| 64 | |||
| 65 | The rest of the documentation details each of the object | ||
| 66 | methods. Internal methods are usually preceded with a _ | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | # Let the code begin... | ||
| 71 | |||
| 72 | package FAST::Bio::AlignIO::largemultifasta; | ||
| 73 | use strict; | ||
| 74 | |||
| 75 | use FAST::Bio::Seq::LargeLocatableSeq; | ||
| 76 | use FAST::Bio::Seq::SeqFactory; | ||
| 77 | |||
| 78 | use base qw(FAST::Bio::AlignIO FAST::Bio::SeqIO FAST::Bio::SimpleAlign); | ||
| 79 | |||
| 80 | |||
| 81 | sub _initialize { | ||
| 82 | my($self,@args) = @_; | ||
| 83 | $self->SUPER::_initialize(@args); | ||
| 84 | if( ! defined $self->sequence_factory ) { | ||
| 85 | $self->sequence_factory(FAST::Bio::Seq::SeqFactory->new | ||
| 86 | (-verbose => $self->verbose(), | ||
| 87 | -type => 'FAST::Bio::Seq::LargeLocatableSeq')); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | =head2 next_seq | ||
| 92 | |||
| 93 | Title : next_seq | ||
| 94 | Usage : $seq = $stream->next_seq() | ||
| 95 | Function: returns the next sequence in the stream while taking care | ||
| 96 | of the length | ||
| 97 | Returns : FAST::Bio::Seq object | ||
| 98 | Args : NONE | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | sub next_seq { | ||
| 103 | my ($self) = @_; | ||
| 104 | my $largeseq = $self->sequence_factory->create(-alphabet=>$self->alphabet); | ||
| 105 | my ($id,$fulldesc,$entry); | ||
| 106 | my $count = 0; | ||
| 107 | my $seen = 0; | ||
| 108 | while( defined ($entry = $self->_readline) ) { | ||
| 109 | if( $seen == 1 && $entry =~ /^\s*>/ ) { | ||
| 110 | $self->_pushback($entry); | ||
| 111 | return $largeseq; | ||
| 112 | } | ||
| 113 | if ( ($entry eq '>') ) { $seen = 1; next; } | ||
| 114 | elsif( $entry =~ /\s*>(.+?)$/ ) { | ||
| 115 | $seen = 1; | ||
| 116 | ($id,$fulldesc) = ($1 =~ /^\s*(\S+)\s*(.*)$/) | ||
| 117 | or $self->warn("Can't parse fasta header"); | ||
| 118 | $largeseq->display_id($id); | ||
| 119 | $largeseq->primary_id($id); | ||
| 120 | $largeseq->desc($fulldesc); | ||
| 121 | } else { | ||
| 122 | $entry =~ s/\s+//g; | ||
| 123 | $largeseq->add_sequence_as_string($entry); | ||
| 124 | } | ||
| 125 | (++$count % 1000 == 0 && $self->verbose() > 0) && print "line $count\n"; | ||
| 126 | } | ||
| 127 | if( ! $seen ) { return; } | ||
| 128 | return $largeseq; | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | =head2 next_aln | ||
| 133 | |||
| 134 | Title : next_aln | ||
| 135 | Usage : $aln = $stream->next_aln() | ||
| 136 | Function: returns the next alignment in the stream. | ||
| 137 | Returns : L<FAST::Bio::Align::AlignI> object - returns 0 on end of file | ||
| 138 | or on error | ||
| 139 | Args : NONE | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | sub next_aln { | ||
| 144 | my $self = shift; | ||
| 145 | my $largeseq; | ||
| 146 | my $aln = FAST::Bio::SimpleAlign->new(); | ||
| 147 | while (defined ($largeseq = $self->next_seq) ) { | ||
| 148 | $aln->add_seq($largeseq); | ||
| 149 | $self->debug("sequence readed\n"); | ||
| 150 | } | ||
| 151 | |||
| 152 | my $alnlen = $aln->length; | ||
| 153 | foreach my $largeseq ( $aln->each_seq ) { | ||
| 154 | if( $largeseq->length < $alnlen ) { | ||
| 155 | my ($diff) = ($alnlen - $largeseq->length); | ||
| 156 | $largeseq->seq("-" x $diff); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | return $aln if $aln->num_sequences; | ||
| 161 | return; | ||
| 162 | |||
| 163 | } | ||
| 164 | |||
| 165 | =head2 write_aln | ||
| 166 | |||
| 167 | Title : write_aln | ||
| 168 | Usage : $stream->write_aln(@aln) | ||
| 169 | Function: writes the $aln object into the stream in largemultifasta format | ||
| 170 | Returns : 1 for success and 0 for error | ||
| 171 | Args : L<FAST::Bio::Align::AlignI> object | ||
| 172 | |||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | sub write_aln { | ||
| 177 | my ($self,@aln) = @_; | ||
| 178 | my ($seq,$desc,$rseq,$name,$count,$length,$seqsub); | ||
| 179 | |||
| 180 | foreach my $aln (@aln) { | ||
| 181 | if( ! $aln || ! $aln->isa('FAST::Bio::Align::AlignI') ) { | ||
| 182 | $self->warn("Must provide a FAST::Bio::Align::AlignI object when calling write_aln"); | ||
| 183 | next; | ||
| 184 | } | ||
| 185 | foreach $rseq ( $aln->each_seq() ) { | ||
| 186 | $name = $aln->displayname($rseq->get_nse()); | ||
| 187 | $seq = $rseq->seq(); | ||
| 188 | $desc = $rseq->description || ''; | ||
| 189 | $self->_print (">$name $desc\n") or return ; | ||
| 190 | $count =0; | ||
| 191 | $length = length($seq); | ||
| 192 | while( ($count * 60 ) < $length ) { | ||
| 193 | $seqsub = substr($seq,$count*60,60); | ||
| 194 | $self->_print ("$seqsub\n") or return ; | ||
| 195 | $count++; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | } | ||
| 199 | $self->flush if $self->_flush_on_write && defined $self->_fh; | ||
| 200 | return 1; | ||
| 201 | } | ||
| 202 | |||
| 203 | 1; |