| 1 | # | ||
| 2 | # BioPerl module for FAST::Bio::AlignIO::meme | ||
| 3 | # Based on the FAST::Bio::SeqIO modules | ||
| 4 | # by Ewan Birney <birney@ebi.ac.uk> | ||
| 5 | # and Lincoln Stein <lstein@cshl.org> | ||
| 6 | # and the SimpleAlign.pm module of Ewan Birney | ||
| 7 | # | ||
| 8 | # Copyright Benjamin Berman | ||
| 9 | # | ||
| 10 | # You may distribute this module under the same terms as perl itself | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | FAST::Bio::AlignIO::meme - meme sequence input/output stream | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | Do not use this module directly. Use it via the FAST::Bio::AlignIO class. | ||
| 19 | |||
| 20 | use FAST::Bio::AlignIO; | ||
| 21 | # read in an alignment from meme | ||
| 22 | my $in = FAST::Bio::AlignIO->new(-format => 'meme', | ||
| 23 | -file => 'meme.out'); | ||
| 24 | while( my $aln = $in->next_aln ) { | ||
| 25 | # do something with the alignment | ||
| 26 | } | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | This object transforms the "sites sorted by position p-value" sections | ||
| 31 | of a meme (text) output file into a series of FAST::Bio::SimpleAlign | ||
| 32 | objects. Each SimpleAlign object contains FAST::Bio::LocatableSeq | ||
| 33 | objects which represent the individual aligned sites as defined by | ||
| 34 | the central portion of the "site" field in the meme file. The start | ||
| 35 | and end coordinates are derived from the "Start" field. See | ||
| 36 | L<FAST::Bio::SimpleAlign> and L<FAST::Bio::LocatableSeq> for more information. | ||
| 37 | |||
| 38 | This module can only parse MEME version 3.0 and greater. Previous | ||
| 39 | versions have output formats that are more difficult to parse | ||
| 40 | correctly. If the meme output file is not version 3.0 or greater, | ||
| 41 | we signal an error. | ||
| 42 | |||
| 43 | =head1 FEEDBACK | ||
| 44 | |||
| 45 | =head2 Support | ||
| 46 | |||
| 47 | Please direct usage questions or support issues to the mailing list: | ||
| 48 | |||
| 49 | I<bioperl-l@bioperl.org> | ||
| 50 | |||
| 51 | rather than to the module maintainer directly. Many experienced and | ||
| 52 | reponsive experts will be able look at the problem and quickly | ||
| 53 | address it. Please include a thorough description of the problem | ||
| 54 | with code and data examples if at all possible. | ||
| 55 | |||
| 56 | =head2 Reporting Bugs | ||
| 57 | |||
| 58 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 59 | the bugs and their resolution. Bug reports can be submitted via the | ||
| 60 | web: | ||
| 61 | |||
| 62 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 63 | |||
| 64 | =head1 AUTHORS - Benjamin Berman | ||
| 65 | |||
| 66 | Bbased on the FAST::Bio::SeqIO modules by Ewan Birney and others | ||
| 67 | Email: benb@fruitfly.berkeley.edu | ||
| 68 | |||
| 69 | =head1 APPENDIX | ||
| 70 | |||
| 71 | The rest of the documentation details each of the object | ||
| 72 | methods. Internal methods are usually preceded with an | ||
| 73 | underscore. | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | # Let the code begin... | ||
| 78 | |||
| 79 | package FAST::Bio::AlignIO::meme; | ||
| 80 | use strict; | ||
| 81 | use FAST::Bio::LocatableSeq; | ||
| 82 | |||
| 83 | use base qw(FAST::Bio::AlignIO); | ||
| 84 | |||
| 85 | # Constants | ||
| 86 | my $MEME_VERS_ERR = | ||
| 87 | "MEME output file must be generated by version 3.0 or higher"; | ||
| 88 | my $MEME_NO_HEADER_ERR = | ||
| 89 | "MEME output file contains no header line (ex: MEME version 3.0)"; | ||
| 90 | my $HTML_VERS_ERR = | ||
| 91 | "MEME output file must be generated with the -text option"; | ||
| 92 | |||
| 93 | =head2 next_aln | ||
| 94 | |||
| 95 | Title : next_aln | ||
| 96 | Usage : $aln = $stream->next_aln() | ||
| 97 | Function: returns the next alignment in the stream | ||
| 98 | Returns : FAST::Bio::SimpleAlign object with the score() set to the evalue of the | ||
| 99 | motif. | ||
| 100 | Args : NONE | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub next_aln { | ||
| 105 | my ($self) = @_; | ||
| 106 | my $aln = FAST::Bio::SimpleAlign->new(-source => 'meme'); | ||
| 107 | my $line; | ||
| 108 | my $good_align_sec = 0; | ||
| 109 | my $in_align_sec = 0; | ||
| 110 | my $evalue; | ||
| 111 | while (!$good_align_sec && defined($line = $self->_readline())) { | ||
| 112 | if (!$in_align_sec) { | ||
| 113 | # Check for the meme header | ||
| 114 | if ($line =~ /^\s*MEME\s+version\s+(\S+)/ ){ | ||
| 115 | $self->{'meme_vers'} = $1; | ||
| 116 | my ($vers) = $self->{'meme_vers'} =~ /^(\d)/; | ||
| 117 | $self->throw($MEME_VERS_ERR) unless ($vers >= 3); | ||
| 118 | $self->{'seen_header'} = 1; | ||
| 119 | } | ||
| 120 | # Check if they've output the HTML version | ||
| 121 | if ($line =~ /\<TITLE\>/i){ | ||
| 122 | $self->throw($HTML_VERS_ERR); | ||
| 123 | } | ||
| 124 | # Grab the evalue | ||
| 125 | if ($line =~ /MOTIF\s+\d+\s+width.+E-value = (\S+)/) { | ||
| 126 | $self->throw($MEME_NO_HEADER_ERR) unless ($self->{'seen_header'}); | ||
| 127 | $evalue = $1; | ||
| 128 | } | ||
| 129 | # Check if we're going into an alignment section | ||
| 130 | if ($line =~ /sites sorted by position/) { | ||
| 131 | $self->throw($MEME_NO_HEADER_ERR) unless ($self->{'seen_header'}); | ||
| 132 | $in_align_sec = 1; | ||
| 133 | } | ||
| 134 | } elsif ($line =~ /^(\S+)\s+([+-]?)\s+(\d+)\s+ | ||
| 135 | (\S+)\s+([.ACTGNX\-]*)\s+([ACTGNX\-]+)\s+ | ||
| 136 | ([.ACTGNX\-]*)/xi ) { | ||
| 137 | # Got a sequence line | ||
| 138 | my $seq_name = $1; | ||
| 139 | my $strand = ($2 eq '-') ? -1 : 1; | ||
| 140 | my $start_pos = $3; | ||
| 141 | my $central = uc($6); | ||
| 142 | |||
| 143 | # my $p_val = $4; | ||
| 144 | # my $left_flank = uc($5); | ||
| 145 | # my $right_flank = uc($7); | ||
| 146 | |||
| 147 | # Info about the flanking sequence | ||
| 148 | # my $start_len = ($strand > 0) ? length($left_flank) : | ||
| 149 | # length($right_flank); | ||
| 150 | # my $end_len = ($strand > 0) ? length($right_flank) : | ||
| 151 | # length($left_flank); | ||
| 152 | |||
| 153 | # Make the sequence. Meme gives the start coordinate at the left | ||
| 154 | # hand side of the motif relative to the INPUT sequence. | ||
| 155 | my $end_pos = $start_pos + length($central) - 1; | ||
| 156 | my $seq = FAST::Bio::LocatableSeq->new | ||
| 157 | ('-seq' => $central, | ||
| 158 | '-display_id' => $seq_name, | ||
| 159 | '-start' => $start_pos, | ||
| 160 | '-end' => $end_pos, | ||
| 161 | '-strand' => $strand, | ||
| 162 | '-alphabet' => $self->alphabet, | ||
| 163 | ); | ||
| 164 | # Add the sequence motif to the alignment | ||
| 165 | $aln->add_seq($seq); | ||
| 166 | } elsif (($line =~ /^\-/) || ($line =~ /Sequence name/)){ | ||
| 167 | # These are acceptable things to be in the site section | ||
| 168 | } elsif ($line =~ /^\s*$/){ | ||
| 169 | # This ends the site section | ||
| 170 | $in_align_sec = 0; | ||
| 171 | $good_align_sec = 1; | ||
| 172 | } else{ | ||
| 173 | $self->warn("Unrecognized format:\n$line"); | ||
| 174 | return 0; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | # Signal an error if we didn't find a header section | ||
| 178 | $self->throw($MEME_NO_HEADER_ERR) unless ($self->{'seen_header'}); | ||
| 179 | |||
| 180 | if ($good_align_sec) { | ||
| 181 | $aln->score($evalue); | ||
| 182 | return $aln; | ||
| 183 | } | ||
| 184 | |||
| 185 | return; | ||
| 186 | } | ||
| 187 | |||
| 188 | =head2 write_aln | ||
| 189 | |||
| 190 | Title : write_aln | ||
| 191 | Usage : $stream->write_aln(@aln) | ||
| 192 | Function: Not implemented | ||
| 193 | Returns : 1 for success and 0 for error | ||
| 194 | Args : FAST::Bio::SimpleAlign object | ||
| 195 | |||
| 196 | =cut | ||
| 197 | |||
| 198 | sub write_aln { | ||
| 199 | my ($self,@aln) = @_; | ||
| 200 | $self->throw_not_implemented(); | ||
| 201 | } | ||
| 202 | |||
| 203 | # ---------------------------------------- | ||
| 204 | # - Private methods | ||
| 205 | # ---------------------------------------- | ||
| 206 | |||
| 207 | sub _initialize { | ||
| 208 | my($self,@args) = @_; | ||
| 209 | |||
| 210 | # Call into our base version | ||
| 211 | $self->SUPER::_initialize(@args); | ||
| 212 | |||
| 213 | # Then initialize our data variables | ||
| 214 | $self->{'seen_header'} = 0; | ||
| 215 | } | ||
| 216 | |||
| 217 | 1; |