2 # BioPerl module for Bio::AlignIO::selex
4 # based on the Bio::SeqIO::selex module
5 # by Ewan Birney <birney@ebi.ac.uk>
6 # and Lincoln Stein <lstein@cshl.org>
8 # and the SimpleAlign.pm module of Ewan Birney
10 # Copyright Peter Schattner
12 # You may distribute this module under the same terms as perl itself
15 # POD documentation - main docs before the code
19 Bio::AlignIO::selex - selex sequence input/output stream
23 # Do not use this module directly. Use it via the L<Bio::AlignIO> class.
28 my $in = Bio::AlignIO->new(-format => 'selex',
29 -file => 't/data/testaln.selex');
30 while( my $aln = $in->next_aln ) {
36 This object can transform L<Bio::Align::AlignI> objects to and from selex flat
43 Please direct usage questions or support issues to the mailing list:
45 I<bioperl-l@bioperl.org>
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.
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
58 https://github.com/bioperl/bioperl-live/issues
60 =head1 AUTHORS - Peter Schattner
62 Email: schattner@alum.mit.edu
66 Jason Stajich, jason-at-bioperl.org
70 The rest of the documentation details each of the object
71 methods. Internal methods are usually preceded with a _
75 # Let the code begin...
77 package Bio
::AlignIO
::selex
;
81 use base
qw(Bio::AlignIO);
86 Usage : $aln = $stream->next_aln()
87 Function: returns the next alignment in the stream. Tries to read *all* selex
88 It reads all non whitespace characters in the alignment
89 area. For selexs with weird gaps (eg ~~~) map them by using
90 $al->map_chars('~','-')
91 Returns : L<Bio::Align::AlignI> object
99 my ($start,$end,%align,$name,$seqname,%hash,@c2name, %accession,%desc);
100 my $aln = Bio
::SimpleAlign
->new(-source
=> 'selex');
102 # in selex format, every non-blank line that does not start
103 # with '#=' is an alignment segment; the '#=' lines are mark up lines.
104 # Of particular interest are the '#=GF <name/st-ed> AC <accession>'
105 # lines, which give accession numbers for each segment
106 while( $entry = $self->_readline) {
107 if( $entry =~ /^\#=GS\s+(\S+)\s+AC\s+(\S+)/ ) {
108 $accession{ $1 } = $2;
110 } elsif( $entry =~ /^\#=GS\s+(\S+)\s+DE\s+(.+)\s*$/ ) {
112 } elsif ( $entry =~ /^([^\#]\S+)\s+([A-Za-z\.\-\*]+)\s*/ ) {
113 my ($name,$seq) = ($1,$2);
115 if( ! defined $align{$name} ) {
118 $align{$name} .= $seq;
121 # ok... now we can make the sequences
123 foreach my $name ( @c2name ) {
125 if( $name =~ /(\S+)\/(\d
+)-(\d
+)/ ) {
132 $end = length($align{$name});
134 my $seq = Bio
::LocatableSeq
->new
135 ('-seq' => $align{$name},
136 '-display_id' => $seqname,
139 '-description' => $desc{$name},
140 '-accession_number' => $accession{$name},
141 '-alphabet' => $self->alphabet,
147 return $aln if $aln->num_sequences;
155 Usage : $stream->write_aln(@aln)
156 Function: writes the $aln object into the stream in selex format
157 Returns : 1 for success and 0 for error
158 Args : L<Bio::Align::AlignI> object
164 my ($self,@aln) = @_;
165 my ($namestr,$seq,$add);
167 foreach my $aln (@aln) {
168 $maxn = $aln->maxdisplayname_length();
169 foreach $seq ( $aln->each_seq() ) {
170 $namestr = $aln->displayname($seq->get_nse());
171 $add = $maxn - length($namestr) + 2;
172 $namestr .= " " x
$add;
173 $self->_print (sprintf("%s %s\n",$namestr,$seq->seq())) or return;
176 $self->flush if $self->_flush_on_write && defined $self->_fh;