| 1 | # $Id$ | ||
| 2 | # | ||
| 3 | # BioPerl module for Bio::Tools::Run::EMBOSSApplication | ||
| 4 | # | ||
| 5 | # Copyright Heikki Lehvaslaiho | ||
| 6 | # | ||
| 7 | # You may distribute this module under the same terms as perl itself | ||
| 8 | |||
| 9 | # POD documentation - main docs before the code | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Bio::Tools::Run::EMBOSSApplication - class for EMBOSS Applications | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use Bio::Factory::EMBOSS; | ||
| 18 | # get an EMBOSS application object from the EMBOSS factory | ||
| 19 | $factory = Bio::Factory::EMBOSS->new(); | ||
| 20 | $application = $factory->program('embossversion'); | ||
| 21 | # run the application with an optional hash containing parameters | ||
| 22 | $result = $application->run(); # returns a string or creates a file | ||
| 23 | print $result . "\n"; | ||
| 24 | |||
| 25 | $water = $factory->program('water'); | ||
| 26 | |||
| 27 | # here is an example of running the application | ||
| 28 | # water can compare 1 seq against 1->many sequences | ||
| 29 | # in a database using Smith-Waterman | ||
| 30 | my $seq_to_test; # this would have a seq here | ||
| 31 | my @seqs_to_check; # this would be a list of seqs to compare | ||
| 32 | |||
| 33 | my $wateroutfile = 'out.water'; | ||
| 34 | $water->run({-sequencea => $seq_to_test, | ||
| 35 | -seqall => \@seqs_to_check, | ||
| 36 | -gapopen => '10.0', | ||
| 37 | -gapextend => '0.5', | ||
| 38 | -outfile => $wateroutfile}); | ||
| 39 | # now you might want to get the alignment | ||
| 40 | use Bio::AlignIO; | ||
| 41 | my $alnin = Bio::AlignIO->new(-format => 'emboss', | ||
| 42 | -file => $wateroutfile); | ||
| 43 | |||
| 44 | while ( my $aln = $alnin->next_aln ) { | ||
| 45 | # process the alignment -- these will be Bio::SimpleAlign objects | ||
| 46 | } | ||
| 47 | |||
| 48 | =head1 DESCRIPTION | ||
| 49 | |||
| 50 | The EMBOSSApplication class can represent EMBOSS any program. It is | ||
| 51 | created by a L<Bio::Factory::EMBOSS> object. | ||
| 52 | |||
| 53 | If you want to check command line options before sending them to the | ||
| 54 | program set $prog-E<gt>verbose to positive integer. The ADC | ||
| 55 | description of the available command line options is then parsed in | ||
| 56 | and compared to input. | ||
| 57 | |||
| 58 | See also L<Bio::Factory::EMBOSS> and L<Bio::Tools::Run::EMBOSSacd>. | ||
| 59 | |||
| 60 | =head1 FEEDBACK | ||
| 61 | |||
| 62 | =head2 Mailing Lists | ||
| 63 | |||
| 64 | User feedback is an integral part of the evolution of this and other | ||
| 65 | Bioperl modules. Send your comments and suggestions preferably to the | ||
| 66 | Bioperl mailing lists Your participation is much appreciated. | ||
| 67 | |||
| 68 | bioperl-l@bioperl.org - General discussion | ||
| 69 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 70 | |||
| 71 | =head2 Support | ||
| 72 | |||
| 73 | Please direct usage questions or support issues to the mailing list: | ||
| 74 | |||
| 75 | I<bioperl-l@bioperl.org> | ||
| 76 | |||
| 77 | rather than to the module maintainer directly. Many experienced and | ||
| 78 | reponsive experts will be able look at the problem and quickly | ||
| 79 | address it. Please include a thorough description of the problem | ||
| 80 | with code and data examples if at all possible. | ||
| 81 | |||
| 82 | =head2 Reporting Bugs | ||
| 83 | |||
| 84 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 85 | the bugs and their resolution. Bug reports can be submitted via the | ||
| 86 | web: | ||
| 87 | |||
| 88 | http://redmine.open-bio.org/projects/bioperl/ | ||
| 89 | |||
| 90 | =head1 AUTHOR - Heikki Lehvaslaiho | ||
| 91 | |||
| 92 | Email heikki-at-bioperl-dot-org | ||
| 93 | |||
| 94 | =head2 CONTRIBUTORS | ||
| 95 | |||
| 96 | Email: jason-AT-bioperl_DOT_org | ||
| 97 | |||
| 98 | =head1 APPENDIX | ||
| 99 | |||
| 100 | The rest of the documentation details each of the object | ||
| 101 | methods. Internal methods are usually preceded with a _ | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | # Let the code begin... | ||
| 106 | |||
| 107 | package Bio::Tools::Run::EMBOSSApplication; | ||
| 108 | use vars qw( $SEQIOLOADED $ALIGNIOLOADED ); | ||
| 109 | use strict; | ||
| 110 | use Data::Dumper; | ||
| 111 | use Bio::Tools::Run::EMBOSSacd; | ||
| 112 | use base qw(Bio::Root::Root Bio::Tools::Run::WrapperBase); | ||
| 113 | |||
| 114 | sub new { | ||
| 115 | my($class, $args) = @_; | ||
| 116 | my $self = $class->SUPER::new(); | ||
| 117 | |||
| 118 | $self->name($args->{'name'}); | ||
| 119 | $self->verbose($args->{'verbose'}); | ||
| 120 | |||
| 121 | $self->acd if $self->verbose > 0; | ||
| 122 | |||
| 123 | return $self; | ||
| 124 | } | ||
| 125 | |||
| 126 | =head2 run | ||
| 127 | |||
| 128 | Title : run | ||
| 129 | Usage : $embossapplication->run($attribute_hash) | ||
| 130 | Function: Runs the EMBOSS program. | ||
| 131 | Returns : string or creates files for now; will return objects! | ||
| 132 | Args : hash of input to the program | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | sub run { | ||
| 137 | my ($self, $input) = @_; | ||
| 138 | $self->io->_io_cleanup(); | ||
| 139 | # test input | ||
| 140 | $self->debug( Dumper($input) ); | ||
| 141 | |||
| 142 | # parse ACD information | ||
| 143 | $self->acd if $self->verbose > 0; | ||
| 144 | |||
| 145 | # collect the options into a string | ||
| 146 | my $option_string = ''; | ||
| 147 | foreach my $attr (keys %{$input}) { | ||
| 148 | my $attr_name = substr($attr, 1) if substr($attr, 0, 1) =~ /\W/; | ||
| 149 | |||
| 150 | my $array = 0; | ||
| 151 | |||
| 152 | if( defined $input->{$attr} && ref($input->{$attr}) ) { | ||
| 153 | |||
| 154 | my (@pieces); | ||
| 155 | |||
| 156 | if( $array = (ref($input->{$attr}) =~ /array/i) ) { | ||
| 157 | foreach my $s ( @{$input->{$attr}} ) { | ||
| 158 | @pieces = @{$input->{$attr}}; | ||
| 159 | } | ||
| 160 | } else { | ||
| 161 | @pieces = ($input->{$attr}); | ||
| 162 | } | ||
| 163 | if( ! defined $pieces[0] ) { | ||
| 164 | # we ignore for now | ||
| 165 | $self->warn("specified a parameter $attr with no value"); | ||
| 166 | $input->{$attr} = undef; | ||
| 167 | return; | ||
| 168 | } elsif( $pieces[0]->isa('Bio::PrimarySeqI') ) { | ||
| 169 | unless( $SEQIOLOADED ) { | ||
| 170 | require Bio::SeqIO; | ||
| 171 | $SEQIOLOADED = 1; | ||
| 172 | } | ||
| 173 | my ($tfh,$tempfile) = $self->io->tempfile(-dir => $self->tempdir); | ||
| 174 | my $out = Bio::SeqIO->new(-format => 'fasta', | ||
| 175 | -fh => $tfh); | ||
| 176 | foreach my $seq ( @pieces ) { | ||
| 177 | $out->write_seq($seq); | ||
| 178 | } | ||
| 179 | $out->close(); | ||
| 180 | $input->{$attr} = $tempfile; | ||
| 181 | close($tfh); | ||
| 182 | undef $tfh; | ||
| 183 | } elsif( $pieces[0]->isa('Bio::Align::AlignI') ) { | ||
| 184 | unless( $ALIGNIOLOADED ) { | ||
| 185 | require Bio::AlignIO; | ||
| 186 | $ALIGNIOLOADED = 1; | ||
| 187 | } | ||
| 188 | my ($tfh,$tempfile) = $self->io->tempfile(); | ||
| 189 | my $out = Bio::AlignIO->new(-format => 'msf', | ||
| 190 | -fh => $tfh); | ||
| 191 | foreach my $p ( @pieces ) { | ||
| 192 | $out->write_aln($p); | ||
| 193 | } | ||
| 194 | $input->{$attr} = $tempfile; | ||
| 195 | close($tfh); | ||
| 196 | undef $tfh; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | # check each argument against ACD | ||
| 201 | if ($self->verbose > 0) { | ||
| 202 | |||
| 203 | last unless defined $self->acd; # might not have the parser | ||
| 204 | |||
| 205 | $self->throw("Attribute [$attr] not recognized!\n") | ||
| 206 | unless $self->acd->qualifier($attr); | ||
| 207 | } | ||
| 208 | |||
| 209 | # print out debugging info | ||
| 210 | $self->debug("Input attr: ". $attr_name. " => ". | ||
| 211 | $input->{$attr}. "\n"); | ||
| 212 | $option_string .= " " . $attr; | ||
| 213 | $option_string .= " ". $input->{$attr} | ||
| 214 | if defined $input->{$attr}; | ||
| 215 | } | ||
| 216 | |||
| 217 | # check mandatory attributes against given ones | ||
| 218 | if ($self->verbose > 0) { | ||
| 219 | last unless defined $self->acd; # might not have the parser | ||
| 220 | # $self->acd->mandatory->print; | ||
| 221 | # if ($self->name eq 'water') { | ||
| 222 | # print Dumper($self->acd->mandatory); | ||
| 223 | # } | ||
| 224 | foreach my $attr (keys %{$self->acd->mandatory} ) { | ||
| 225 | last unless defined $self->acd; # might not have the parser | ||
| 226 | unless (defined $input->{$attr}) { | ||
| 227 | print "-" x 38, "\n", "MISSING MANDATORY ATTRIBUTE: $attr\n", | ||
| 228 | "-" x 38, "\n"; | ||
| 229 | $self->acd->print($attr) and | ||
| 230 | $self->throw("Program ". $self->name. | ||
| 231 | " needs attribute [$attr]!\n") | ||
| 232 | } | ||
| 233 | } | ||
| 234 | } | ||
| 235 | my $runstring = join (' ', $self->name, $option_string, '-auto'); | ||
| 236 | $self->debug( "Command line: ", $runstring, "\n"); | ||
| 237 | return `$runstring`; | ||
| 238 | } | ||
| 239 | |||
| 240 | =head2 acd | ||
| 241 | |||
| 242 | Title : acd | ||
| 243 | Usage : $embossprogram->acd | ||
| 244 | Function: finds out all the possible qualifiers for this | ||
| 245 | EMBOSS application. They can be used to debug the | ||
| 246 | options given. | ||
| 247 | Throws : | ||
| 248 | Returns : boolean | ||
| 249 | Args : | ||
| 250 | |||
| 251 | =cut | ||
| 252 | |||
| 253 | sub acd { | ||
| 254 | my ($self) = @_; | ||
| 255 | unless ( $self->{'_acd'} ) { | ||
| 256 | $self->{'_acd'} = | ||
| 257 | Bio::Tools::Run::EMBOSSacd->new($self->name); | ||
| 258 | } | ||
| 259 | return $self->{'_acd'}; | ||
| 260 | } | ||
| 261 | |||
| 262 | =head2 name | ||
| 263 | |||
| 264 | Title : name | ||
| 265 | Usage : $embossprogram->name | ||
| 266 | Function: sets/gets the name of the EMBOSS program | ||
| 267 | Setting is done by the EMBOSSFactory object, | ||
| 268 | you should only get it. | ||
| 269 | Throws : | ||
| 270 | Returns : name string | ||
| 271 | Args : None | ||
| 272 | |||
| 273 | =cut | ||
| 274 | |||
| 275 | sub name { | ||
| 276 | my ($self,$value) = @_; | ||
| 277 | if (defined $value) { | ||
| 278 | $self->{'_name'} = $value; | ||
| 279 | } | ||
| 280 | return $self->{'_name'}; | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | =head2 descr | ||
| 285 | |||
| 286 | Title : descr | ||
| 287 | Usage : $embossprogram->descr | ||
| 288 | Function: sets/gets the descr of the EMBOSS program | ||
| 289 | Setting is done by the EMBOSSFactory object, | ||
| 290 | you should only get it. | ||
| 291 | Throws : | ||
| 292 | Returns : description string | ||
| 293 | Args : None | ||
| 294 | |||
| 295 | =cut | ||
| 296 | |||
| 297 | sub descr { | ||
| 298 | my ($self,$value) = @_; | ||
| 299 | if (defined $value) { | ||
| 300 | $self->{'_descr'} = $value; | ||
| 301 | } | ||
| 302 | return $self->{'_descr'}; | ||
| 303 | } | ||
| 304 | |||
| 305 | |||
| 306 | =head2 group | ||
| 307 | |||
| 308 | Title : group | ||
| 309 | Usage : $embossprogram->group | ||
| 310 | Function: sets/gets the group of the EMBOSS program | ||
| 311 | Setting is done by the EMBOSSFactory object, | ||
| 312 | you should only get it. | ||
| 313 | |||
| 314 | If the application is assigned into a subgroup | ||
| 315 | use l<subgroup> to get it. | ||
| 316 | Throws : | ||
| 317 | Returns : string, group name | ||
| 318 | Args : group string | ||
| 319 | |||
| 320 | =cut | ||
| 321 | |||
| 322 | sub group { | ||
| 323 | my ($self,$value) = @_; | ||
| 324 | if (defined $value) { | ||
| 325 | my ($group, $subgroup) = split ':', $value; | ||
| 326 | $self->{'_group'} = $group; | ||
| 327 | $self->{'_subgroup'} = $subgroup; | ||
| 328 | } | ||
| 329 | return $self->{'_group'}; | ||
| 330 | } | ||
| 331 | |||
| 332 | |||
| 333 | =head2 subgroup | ||
| 334 | |||
| 335 | Title : subgroup | ||
| 336 | Usage : $embossprogram->subgroup | ||
| 337 | Function: sets/gets the subgroup of the EMBOSS program | ||
| 338 | Setting is done by the EMBOSSFactory object, | ||
| 339 | you should only get it. | ||
| 340 | Throws : | ||
| 341 | Returns : string, subgroup name; undef if not defined | ||
| 342 | Args : None | ||
| 343 | |||
| 344 | =cut | ||
| 345 | |||
| 346 | sub subgroup { | ||
| 347 | my ($self) = @_; | ||
| 348 | return $self->{'_subgroup'}; | ||
| 349 | } | ||
| 350 | |||
| 351 | =head2 program_dir | ||
| 352 | |||
| 353 | Title : program_dir | ||
| 354 | Usage : | ||
| 355 | Function: Required by WrapperBase | ||
| 356 | Throws : | ||
| 357 | Returns : Name of directory with EMBOSS programs | ||
| 358 | Args : | ||
| 359 | |||
| 360 | =cut | ||
| 361 | |||
| 362 | sub program_dir { | ||
| 363 | return Bio::Root::IO->catfile($ENV{EMBOSS_ACDROOT}); | ||
| 364 | } | ||
| 365 | |||
| 366 | =head2 program_path | ||
| 367 | |||
| 368 | Title : program_path | ||
| 369 | Usage : | ||
| 370 | Function: Required by WrapperBase | ||
| 371 | Throws : | ||
| 372 | Returns : Full path of program | ||
| 373 | Args : | ||
| 374 | |||
| 375 | =cut | ||
| 376 | |||
| 377 | sub program_path { | ||
| 378 | my $self = shift; | ||
| 379 | my $name = $self->{_name}; | ||
| 380 | my $dir = Bio::Root::IO->catfile($ENV{EMBOSS_ACDROOT}); | ||
| 381 | return "$dir/$name"; | ||
| 382 | } | ||
| 383 | |||
| 384 | =head2 executable | ||
| 385 | |||
| 386 | Title : executable | ||
| 387 | Usage : | ||
| 388 | Function: Required by WrapperBase | ||
| 389 | Throws : | ||
| 390 | Returns : Name of program | ||
| 391 | Args : | ||
| 392 | |||
| 393 | =cut | ||
| 394 | |||
| 395 | sub executable { | ||
| 396 | my $self = shift; | ||
| 397 | $self->{_name}; | ||
| 398 | } | ||
| 399 | |||
| 400 | 1; |