| 1 | # $Id: Meme.pm,v 1.25 2006/07/04 22:23:31 mauricio Exp $ | ||
| 2 | # BioPerl module for Meme | ||
| 3 | # | ||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 5 | # | ||
| 6 | # Cared for by Sendu Bala <bix@sendu.me.uk> | ||
| 7 | # | ||
| 8 | # Copyright Sendu Bala | ||
| 9 | # | ||
| 10 | # You may distribute this module under the same terms as perl itself | ||
| 11 | |||
| 12 | # POD documentation - main docs before the code | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Bio::Tools::Run::Meme - Wrapper for Meme Program | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | use Bio::Tools::Run::Meme; | ||
| 21 | |||
| 22 | my $factory = Bio::Tools::Run::Meme->new(-dna => 1, -mod => 'zoops'); | ||
| 23 | |||
| 24 | # return a Bio::AlignIO given Bio::PrimarySeqI objects | ||
| 25 | my $alignio = $factory->run($seq1, $seq2, $seq3...); | ||
| 26 | |||
| 27 | # add a Bio::Map::Prediction to the appropriate maps given Bio::Map::GeneMap | ||
| 28 | # objects (predict on the full map sequences supplied) or Bio::Map::Gene | ||
| 29 | # objects (predict on the full map sequences of the maps the supplied Genes | ||
| 30 | # are on) or Bio::Map::PositionWithSequence objects | ||
| 31 | my $prediction = $factory->run($biomap1, $biomap2, $biomap3...); | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | This is a wrapper for running meme, a transcription factor binding site | ||
| 36 | prediction program. It can be found here: | ||
| 37 | http://meme.sdsc.edu/meme4/meme-download.html | ||
| 38 | |||
| 39 | You can try supplying normal meme command-line arguments to new(), eg. | ||
| 40 | new(-mod => 'oops') or calling arg-named methods (excluding the initial | ||
| 41 | hyphen(s), eg. $factory->mod('oops') to set the -mod option to 'oops'). | ||
| 42 | |||
| 43 | |||
| 44 | You will need to enable this MEME wrapper to find the meme program. During | ||
| 45 | standard installation of meme you will have set up an environment variable | ||
| 46 | called MEME_BIN which is used for this purpose. | ||
| 47 | |||
| 48 | =head1 FEEDBACK | ||
| 49 | |||
| 50 | =head2 Mailing Lists | ||
| 51 | |||
| 52 | User feedback is an integral part of the evolution of this and other | ||
| 53 | Bioperl modules. Send your comments and suggestions preferably to one | ||
| 54 | of the Bioperl mailing lists. Your participation is much appreciated. | ||
| 55 | |||
| 56 | bioperl-l@bioperl.org - General discussion | ||
| 57 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 58 | |||
| 59 | =head2 Support | ||
| 60 | |||
| 61 | Please direct usage questions or support issues to the mailing list: | ||
| 62 | |||
| 63 | I<bioperl-l@bioperl.org> | ||
| 64 | |||
| 65 | rather than to the module maintainer directly. Many experienced and | ||
| 66 | reponsive experts will be able look at the problem and quickly | ||
| 67 | address it. Please include a thorough description of the problem | ||
| 68 | with code and data examples if at all possible. | ||
| 69 | |||
| 70 | =head2 Reporting Bugs | ||
| 71 | |||
| 72 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 73 | the bugs and their resolution. Bug reports can be submitted via the | ||
| 74 | web: | ||
| 75 | |||
| 76 | http://redmine.open-bio.org/projects/bioperl/ | ||
| 77 | |||
| 78 | =head1 AUTHOR - Sendu Bala | ||
| 79 | |||
| 80 | Email bix@sendu.me.uk | ||
| 81 | |||
| 82 | =head1 APPENDIX | ||
| 83 | |||
| 84 | The rest of the documentation details each of the object | ||
| 85 | methods. Internal methods are usually preceded with a "_". | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | package Bio::Tools::Run::Meme; | ||
| 90 | |||
| 91 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 92 | 1 | ▶ | use Bio::SeqIO; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 93 | 1 | ▶ | use Bio::AlignIO; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 94 | 1 | ▶ | use Bio::Map::Prediction; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 95 | use Bio::Map::Position; | ||
| 96 | |||
| 97 | use base qw(Bio::Tools::Run::WrapperBase); | ||
| 98 | |||
| 99 | our $PROGRAM_NAME = 'meme'; | ||
| 100 | our $PROGRAM_DIR = $ENV{'MEME_BIN'}; | ||
| 101 | |||
| 102 | # methods for the meme args we support | ||
| 103 | our @PARAMS = qw(mod nmotifs evt nsites minsites maxsites wnsites w minw maxw | ||
| 104 | wg ws bfile maxiter distance prior b plib spfuzz spmap cons | ||
| 105 | maxsize p time sf); | ||
| 106 | our @SWITCHES = qw(dna protein nomatrim noendgaps revcomp pal); | ||
| 107 | |||
| 108 | # just to be explicit, args we don't support (yet) or we handle ourselves | ||
| 109 | our @UNSUPPORTED = qw(h text nostatus); | ||
| 110 | |||
| 111 | |||
| 112 | =head2 new | ||
| 113 | |||
| 114 | Title : new | ||
| 115 | Usage : $rm->new($seq) | ||
| 116 | Function: creates a new wrapper | ||
| 117 | Returns: Bio::Tools::Run::Meme | ||
| 118 | Args : Most options understood by meme can be supplied as key => | ||
| 119 | value pairs, with a boolean value for switches. -quiet can also be | ||
| 120 | set to silence meme completely. | ||
| 121 | |||
| 122 | These options can NOT be used with this wrapper (they are handled | ||
| 123 | internally or don't make sense in this context): | ||
| 124 | -h -text -nostatus | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | sub new { | ||
| 129 | my ($class, @args) = @_; | ||
| 130 | my $self = $class->SUPER::new(@args); | ||
| 131 | |||
| 132 | $self->_set_from_args(\@args, -methods => [@PARAMS, @SWITCHES, 'quiet'], | ||
| 133 | -create => 1); | ||
| 134 | |||
| 135 | return $self; | ||
| 136 | } | ||
| 137 | |||
| 138 | =head2 program_name | ||
| 139 | |||
| 140 | Title : program_name | ||
| 141 | Usage : $factory>program_name() | ||
| 142 | Function: holds the program name | ||
| 143 | Returns: string | ||
| 144 | Args : None | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub program_name { | ||
| 149 | return $PROGRAM_NAME; | ||
| 150 | } | ||
| 151 | |||
| 152 | =head2 program_dir | ||
| 153 | |||
| 154 | Title : program_dir | ||
| 155 | Usage : $factory->program_dir(@params) | ||
| 156 | Function: returns the program directory, obtained from ENV variable. | ||
| 157 | Returns: string | ||
| 158 | Args : | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub program_dir { | ||
| 163 | return $PROGRAM_DIR; | ||
| 164 | } | ||
| 165 | |||
| 166 | =head2 version | ||
| 167 | |||
| 168 | Title : version | ||
| 169 | Usage : n/a | ||
| 170 | Function: Determine the version number of the program, which is | ||
| 171 | non-discoverable for Meme | ||
| 172 | Returns : undef | ||
| 173 | Args : none | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | sub version { | ||
| 178 | return; | ||
| 179 | } | ||
| 180 | |||
| 181 | =head2 run | ||
| 182 | |||
| 183 | Title : run | ||
| 184 | Usage : $rm->run($seq1, $seq2, $seq3...); | ||
| 185 | Function: Run Meme on the sequences/Bio::Map::* set as the argument | ||
| 186 | Returns : Bio::AlignIO if sequence objects supplied, OR | ||
| 187 | Bio::Map::Prediction if Bio::Map::* objects supplied | ||
| 188 | undef if no executable found | ||
| 189 | Args : list of Bio::PrimarySeqI compliant objects, OR | ||
| 190 | list of Bio::Map::GeneMap objects, OR | ||
| 191 | list of Bio::Map::Gene objects, OR | ||
| 192 | list of Bio::Map::PositionWithSequence objects | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | sub run { | ||
| 197 | my ($self, @things) = @_; | ||
| 198 | |||
| 199 | my $infile = $self->_setinput(@things); | ||
| 200 | |||
| 201 | return $self->_run($infile); | ||
| 202 | } | ||
| 203 | |||
| 204 | =head2 _run | ||
| 205 | |||
| 206 | Title : _run | ||
| 207 | Usage : $rm->_run ($filename,$param_string) | ||
| 208 | Function: internal function that runs meme | ||
| 209 | Returns : as per run(), undef if no executable found | ||
| 210 | Args : the filename to the input sequence file | ||
| 211 | |||
| 212 | =cut | ||
| 213 | |||
| 214 | sub _run { | ||
| 215 | my ($self, $infile) = @_; | ||
| 216 | |||
| 217 | my $exe = $self->executable || return; | ||
| 218 | |||
| 219 | my $outfile = $infile.".out"; | ||
| 220 | |||
| 221 | my $command = $exe.$self->_setparams($infile, $outfile); | ||
| 222 | $self->debug("meme command = $command\n"); | ||
| 223 | |||
| 224 | open(my $pipe, "$command |") || $self->throw("meme call ($command) failed to start: $? | $!"); | ||
| 225 | my $error = ''; | ||
| 226 | while (<$pipe>) { | ||
| 227 | print unless $self->quiet; | ||
| 228 | $error .= $_; | ||
| 229 | } | ||
| 230 | close($pipe) || ($error ? $self->throw("meme call ($command) failed: $error") : $self->throw("meme call ($command) crashed: $?")); | ||
| 231 | |||
| 232 | #my $status = system($cmd_str); | ||
| 233 | #$self->throw("Meme call ($cmd_str) crashed: $?\n") unless $status == 0; | ||
| 234 | |||
| 235 | my $aio = Bio::AlignIO->new(-format => 'meme', -file => $outfile); | ||
| 236 | unless ($self->{map_mode}) { | ||
| 237 | # return directly the AlignIO | ||
| 238 | return $aio; | ||
| 239 | } | ||
| 240 | else { | ||
| 241 | # use the AlignIO meme parser to generate a Bio::Map::Prediction and | ||
| 242 | # return that | ||
| 243 | my $pred = Bio::Map::Prediction->new(-source => "meme"); | ||
| 244 | while (my $aln = $aio->next_aln) { | ||
| 245 | foreach my $seq ($aln->each_seq) { | ||
| 246 | my $id = $seq->id; | ||
| 247 | unless ($id) { | ||
| 248 | $self->warn("Got a sequence in the alignment with no id, but I need one to determine the map"); | ||
| 249 | next; | ||
| 250 | } | ||
| 251 | my ($uid) = $id =~ /^([^\[]+)/; | ||
| 252 | my $map = Bio::Map::GeneMap->get(-uid => $uid); | ||
| 253 | |||
| 254 | my ($start, $end) = ($seq->start, $seq->end); | ||
| 255 | if ($seq->strand == -1) { | ||
| 256 | my $length; | ||
| 257 | my ($pos_s, $pos_e) = $id =~ /\[(\d+)\.\.(\d+)\]$/; | ||
| 258 | if (defined($pos_s) && defined($pos_e)) { | ||
| 259 | $length = $pos_e - $pos_s + 1; | ||
| 260 | } | ||
| 261 | else { | ||
| 262 | $length = length($map->seq); | ||
| 263 | } | ||
| 264 | my $motif_length = $end - $start + 1; | ||
| 265 | $end = $length - $start + 1; | ||
| 266 | $start = $end - $motif_length + 1; | ||
| 267 | } | ||
| 268 | |||
| 269 | Bio::Map::Position->new(-element => $pred, | ||
| 270 | -start => $start, | ||
| 271 | -end => $end, | ||
| 272 | -map => $map); | ||
| 273 | } | ||
| 274 | } | ||
| 275 | delete $self->{map_mode}; | ||
| 276 | return $pred; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | =head2 _setparams() | ||
| 281 | |||
| 282 | Title : _setparams | ||
| 283 | Usage : Internal function, not to be called directly | ||
| 284 | Function: Create parameter inputs for meme program | ||
| 285 | Returns : parameter string to be passed to meme | ||
| 286 | Args : none | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | sub _setparams { | ||
| 291 | my ($self, $infile, $outfile) = @_; | ||
| 292 | |||
| 293 | my $param_string = ' '.$infile; | ||
| 294 | |||
| 295 | # -text and -nostatus must be set | ||
| 296 | $param_string .= ' -text -nostatus'; | ||
| 297 | |||
| 298 | $param_string .= $self->SUPER::_setparams(-params => \@PARAMS, | ||
| 299 | -switches => \@SWITCHES, | ||
| 300 | -dash => 1); | ||
| 301 | |||
| 302 | $param_string .= " > $outfile"; | ||
| 303 | $param_string .= ' 2> /dev/null' if $self->quiet || $self->verbose < 0; | ||
| 304 | |||
| 305 | return $param_string; | ||
| 306 | } | ||
| 307 | |||
| 308 | =head2 _setinput() | ||
| 309 | |||
| 310 | Title : _setinput | ||
| 311 | Usage : Internal function, not to be called directly | ||
| 312 | Function: writes input sequence to file and return the file name | ||
| 313 | Returns : string (file name) | ||
| 314 | Args : as per run() | ||
| 315 | |||
| 316 | =cut | ||
| 317 | |||
| 318 | sub _setinput { | ||
| 319 | my ($self, @inputs) = @_; | ||
| 320 | $self->throw("At least two sequence or map objects must be supplied") unless @inputs >= 2; | ||
| 321 | ref($inputs[0]) || $self->throw("Inputs must be object references"); | ||
| 322 | |||
| 323 | my ($fh, $outfile) = $self->io->tempfile(-dir => $self->tempdir); | ||
| 324 | my $out = Bio::SeqIO->new(-fh => $fh, '-format' => 'fasta'); | ||
| 325 | |||
| 326 | my %done; | ||
| 327 | foreach my $input (@inputs) { | ||
| 328 | if ($input->isa('Bio::Map::MappableI')) { | ||
| 329 | # we want to work on all its maps, since mappables themselves don't | ||
| 330 | # have sequences | ||
| 331 | push(@inputs, $input->known_maps); | ||
| 332 | next; | ||
| 333 | } | ||
| 334 | |||
| 335 | $input->can('seq') || $self->throw("Supplied an input [$input] with no seq() method!"); | ||
| 336 | |||
| 337 | if ($input->isa('Bio::Map::EntityI')) { | ||
| 338 | $self->{map_mode} = 1; | ||
| 339 | |||
| 340 | if ($input->isa('Bio::Map::MapI')) { | ||
| 341 | # change the id of the seq so we'll know what input object it | ||
| 342 | # came from later | ||
| 343 | my $id = $input->unique_id; | ||
| 344 | next if $done{$id}; | ||
| 345 | $input->id($id); | ||
| 346 | $done{$id} = 1; | ||
| 347 | #*** should this be automatic in GeneMap? Anyway, we don't want | ||
| 348 | # to alter users genemap id here permanently... | ||
| 349 | } | ||
| 350 | else { | ||
| 351 | my $id = $input->id; | ||
| 352 | unless ($id) { | ||
| 353 | $input->id($input->map->unique_id.'['.$input->toString.']'); | ||
| 354 | } | ||
| 355 | } | ||
| 356 | } | ||
| 357 | |||
| 358 | $out->write_seq($input); | ||
| 359 | } | ||
| 360 | close($fh); | ||
| 361 | |||
| 362 | return $outfile; | ||
| 363 | } | ||
| 364 | |||
| 365 | =head1 Bio::Tools::Run::Wrapper methods | ||
| 366 | |||
| 367 | =cut | ||
| 368 | |||
| 369 | =head2 no_param_checks | ||
| 370 | |||
| 371 | Title : no_param_checks | ||
| 372 | Usage : $obj->no_param_checks($newval) | ||
| 373 | Function: Boolean flag as to whether or not we should | ||
| 374 | trust the sanity checks for parameter values | ||
| 375 | Returns : value of no_param_checks | ||
| 376 | Args : newvalue (optional) | ||
| 377 | |||
| 378 | |||
| 379 | =cut | ||
| 380 | |||
| 381 | =head2 save_tempfiles | ||
| 382 | |||
| 383 | Title : save_tempfiles | ||
| 384 | Usage : $obj->save_tempfiles($newval) | ||
| 385 | Function: | ||
| 386 | Returns : value of save_tempfiles | ||
| 387 | Args : newvalue (optional) | ||
| 388 | |||
| 389 | |||
| 390 | =cut | ||
| 391 | |||
| 392 | =head2 outfile_name | ||
| 393 | |||
| 394 | Title : outfile_name | ||
| 395 | Usage : my $outfile = $codeml->outfile_name(); | ||
| 396 | Function: Get/Set the name of the output file for this run | ||
| 397 | (if you wanted to do something special) | ||
| 398 | Returns : string | ||
| 399 | Args : [optional] string to set value to | ||
| 400 | |||
| 401 | |||
| 402 | =cut | ||
| 403 | |||
| 404 | =head2 tempdir | ||
| 405 | |||
| 406 | Title : tempdir | ||
| 407 | Usage : my $tmpdir = $self->tempdir(); | ||
| 408 | Function: Retrieve a temporary directory name (which is created) | ||
| 409 | Returns : string which is the name of the temporary directory | ||
| 410 | Args : none | ||
| 411 | |||
| 412 | =cut | ||
| 413 | |||
| 414 | =head2 cleanup | ||
| 415 | |||
| 416 | Title : cleanup | ||
| 417 | Usage : $codeml->cleanup(); | ||
| 418 | Function: Will cleanup the tempdir directory | ||
| 419 | Returns : none | ||
| 420 | Args : none | ||
| 421 | |||
| 422 | =cut | ||
| 423 | |||
| 424 | =head2 io | ||
| 425 | |||
| 426 | Title : io | ||
| 427 | Usage : $obj->io($newval) | ||
| 428 | Function: Gets a L<Bio::Root::IO> object | ||
| 429 | Returns : L<Bio::Root::IO> | ||
| 430 | Args : none | ||
| 431 | |||
| 432 | =cut | ||
| 433 | |||
| 434 | 1; | ||