| 1 | # | ||
| 2 | # BioPerl module for FAST::Bio::AlignIO::selex | ||
| 3 | |||
| 4 | # based on the FAST::Bio::SeqIO::selex 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 Peter Schattner | ||
| 11 | # | ||
| 12 | # You may distribute this module under the same terms as perl itself | ||
| 13 | # _history | ||
| 14 | # September 5, 2000 | ||
| 15 | # POD documentation - main docs before the code | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | FAST::Bio::AlignIO::selex - selex sequence input/output stream | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | # Do not use this module directly. Use it via the L<FAST::Bio::AlignIO> class. | ||
| 24 | |||
| 25 | use FAST::Bio::AlignIO; | ||
| 26 | use strict; | ||
| 27 | |||
| 28 | my $in = FAST::Bio::AlignIO->new(-format => 'selex', | ||
| 29 | -file => 't/data/testaln.selex'); | ||
| 30 | while( my $aln = $in->next_aln ) { | ||
| 31 | |||
| 32 | } | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | This object can transform L<FAST::Bio::Align::AlignI> objects to and from selex flat | ||
| 37 | file databases. | ||
| 38 | |||
| 39 | =head1 FEEDBACK | ||
| 40 | |||
| 41 | =head2 Support | ||
| 42 | |||
| 43 | Please direct usage questions or support issues to the mailing list: | ||
| 44 | |||
| 45 | I<bioperl-l@bioperl.org> | ||
| 46 | |||
| 47 | rather than to the module maintainer directly. Many experienced and | ||
| 48 | reponsive experts will be able look at the problem and quickly | ||
| 49 | address it. Please include a thorough description of the problem | ||
| 50 | with code and data examples if at all possible. | ||
| 51 | |||
| 52 | =head2 Reporting Bugs | ||
| 53 | |||
| 54 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 55 | the bugs and their resolution. Bug reports can be submitted via the | ||
| 56 | web: | ||
| 57 | |||
| 58 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 59 | |||
| 60 | =head1 AUTHORS - Peter Schattner | ||
| 61 | |||
| 62 | Email: schattner@alum.mit.edu | ||
| 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::AlignIO::selex; | ||
| 78 | use strict; | ||
| 79 | |||
| 80 | use base qw(FAST::Bio::AlignIO); | ||
| 81 | |||
| 82 | =head2 next_aln | ||
| 83 | |||
| 84 | Title : next_aln | ||
| 85 | Usage : $aln = $stream->next_aln() | ||
| 86 | Function: returns the next alignment in the stream. Tries to read *all* selex | ||
| 87 | It reads all non whitespace characters in the alignment | ||
| 88 | area. For selexs with weird gaps (eg ~~~) map them by using | ||
| 89 | $al->map_chars('~','-') | ||
| 90 | Returns : L<FAST::Bio::Align::AlignI> object | ||
| 91 | Args : NONE | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub next_aln { | ||
| 96 | my $self = shift; | ||
| 97 | my $entry; | ||
| 98 | my ($start,$end,%align,$name,$seqname,%hash,@c2name, %accession,%desc); | ||
| 99 | my $aln = FAST::Bio::SimpleAlign->new(-source => 'selex'); | ||
| 100 | |||
| 101 | # in selex format, every non-blank line that does not start | ||
| 102 | # with '#=' is an alignment segment; the '#=' lines are mark up lines. | ||
| 103 | # Of particular interest are the '#=GF <name/st-ed> AC <accession>' | ||
| 104 | # lines, which give accession numbers for each segment | ||
| 105 | while( $entry = $self->_readline) { | ||
| 106 | if( $entry =~ /^\#=GS\s+(\S+)\s+AC\s+(\S+)/ ) { | ||
| 107 | $accession{ $1 } = $2; | ||
| 108 | next; | ||
| 109 | } elsif( $entry =~ /^\#=GS\s+(\S+)\s+DE\s+(.+)\s*$/ ) { | ||
| 110 | $desc{$1} .= $2; | ||
| 111 | } elsif ( $entry =~ /^([^\#]\S+)\s+([A-Za-z\.\-\*]+)\s*/ ) { | ||
| 112 | my ($name,$seq) = ($1,$2); | ||
| 113 | |||
| 114 | if( ! defined $align{$name} ) { | ||
| 115 | push @c2name, $name; | ||
| 116 | } | ||
| 117 | $align{$name} .= $seq; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | # ok... now we can make the sequences | ||
| 121 | |||
| 122 | foreach my $name ( @c2name ) { | ||
| 123 | |||
| 124 | if( $name =~ /(\S+)\/(\d+)-(\d+)/ ) { | ||
| 125 | $seqname = $1; | ||
| 126 | $start = $2; | ||
| 127 | $end = $3; | ||
| 128 | } else { | ||
| 129 | $seqname=$name; | ||
| 130 | $start = 1; | ||
| 131 | $end = length($align{$name}); | ||
| 132 | } | ||
| 133 | my $seq = FAST::Bio::LocatableSeq->new | ||
| 134 | ('-seq' => $align{$name}, | ||
| 135 | '-display_id' => $seqname, | ||
| 136 | '-start' => $start, | ||
| 137 | '-end' => $end, | ||
| 138 | '-description' => $desc{$name}, | ||
| 139 | '-accession_number' => $accession{$name}, | ||
| 140 | '-alphabet' => $self->alphabet, | ||
| 141 | ); | ||
| 142 | |||
| 143 | $aln->add_seq($seq); | ||
| 144 | } | ||
| 145 | |||
| 146 | return $aln if $aln->num_sequences; | ||
| 147 | return; | ||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | =head2 write_aln | ||
| 152 | |||
| 153 | Title : write_aln | ||
| 154 | Usage : $stream->write_aln(@aln) | ||
| 155 | Function: writes the $aln object into the stream in selex format | ||
| 156 | Returns : 1 for success and 0 for error | ||
| 157 | Args : L<FAST::Bio::Align::AlignI> object | ||
| 158 | |||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub write_aln { | ||
| 163 | my ($self,@aln) = @_; | ||
| 164 | my ($namestr,$seq,$add); | ||
| 165 | my ($maxn); | ||
| 166 | foreach my $aln (@aln) { | ||
| 167 | $maxn = $aln->maxdisplayname_length(); | ||
| 168 | foreach $seq ( $aln->each_seq() ) { | ||
| 169 | $namestr = $aln->displayname($seq->get_nse()); | ||
| 170 | $add = $maxn - length($namestr) + 2; | ||
| 171 | $namestr .= " " x $add; | ||
| 172 | $self->_print (sprintf("%s %s\n",$namestr,$seq->seq())) or return; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | $self->flush if $self->_flush_on_write && defined $self->_fh; | ||
| 176 | return 1; | ||
| 177 | } | ||
| 178 | |||
| 179 | 1; |