| 1 | # $Id$ | ||
| 2 | # | ||
| 3 | # BioPerl module for Bio::Tools::Run::Phylo::Gumby | ||
| 4 | # | ||
| 5 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 6 | # | ||
| 7 | # Cared for by Sendu Bala <bix@sendu.me.uk> | ||
| 8 | # | ||
| 9 | # Copyright Sendu Bala | ||
| 10 | # | ||
| 11 | # You may distribute this module under the same terms as perl itself | ||
| 12 | |||
| 13 | # POD documentation - main docs before the code | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Bio::Tools::Run::Phylo::Gumby - Wrapper for gumby | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | use Bio::Tools::Run::Phylo::Gumby; | ||
| 22 | |||
| 23 | # Make a Gumby factory | ||
| 24 | $factory = Bio::Tools::Run::Phylo::Gumby->new(); | ||
| 25 | |||
| 26 | # Run gumby with an alignment and tree file | ||
| 27 | my @results = $factory->run($alignfilename, $treefilename); | ||
| 28 | |||
| 29 | # or with alignment object and tree objects | ||
| 30 | @results = $factory->run($bio_simplalign, $bio_tree_tree); | ||
| 31 | |||
| 32 | # or with alignment object and Bio::DB::Taxonomy object | ||
| 33 | @results = $factory->run($bio_simplalign, $bio_db_taxonomy); | ||
| 34 | |||
| 35 | # specify the positions of exons in (at least) one of the alignment sequences | ||
| 36 | # to get better results | ||
| 37 | $factory->econs(1); | ||
| 38 | $factory->annots($gff_filename); | ||
| 39 | @results = $factory->run($alignfilename, $treefilename); | ||
| 40 | |||
| 41 | # or using feature objects | ||
| 42 | $factory->annots(@bio_seqfeature_objects); | ||
| 43 | @results = $factory->run($alignfilename, $treefilename); | ||
| 44 | |||
| 45 | # (mixtures of all the above are possible) | ||
| 46 | |||
| 47 | # look at the results | ||
| 48 | foreach my $feat (@results) { | ||
| 49 | my $seq_id = $feat->seq_id; | ||
| 50 | my $start = $feat->start; | ||
| 51 | my $end = $feat->end; | ||
| 52 | my $score = $feat->score; | ||
| 53 | my ($pvalue) = $feat->get_tag_values('pvalue'); | ||
| 54 | my ($kind) = $feat->get_tag_values('kind'); # 'all', 'exon' or 'nonexon' | ||
| 55 | } | ||
| 56 | |||
| 57 | =head1 DESCRIPTION | ||
| 58 | |||
| 59 | This is a wrapper for running the gumby application by Shyam Prabhakar. You | ||
| 60 | can get details here: http://pga.lbl.gov/gumby/. Gumby is used for phylogenetic | ||
| 61 | footprinting/ shadowing. | ||
| 62 | |||
| 63 | You can try supplying normal gumby command-line arguments to new(), eg. | ||
| 64 | |||
| 65 | $factory->new(-ratio => 2); | ||
| 66 | |||
| 67 | or calling arg-named methods (excluding the initial hyphen, eg. | ||
| 68 | |||
| 69 | $factory->econs(1); | ||
| 70 | |||
| 71 | to set the -econs arg). | ||
| 72 | |||
| 73 | |||
| 74 | You will need to enable this Gumby wrapper to find the gumby program. | ||
| 75 | This can be done in (at least) three ways: | ||
| 76 | |||
| 77 | 1. Make sure the gumby executable is in your path. | ||
| 78 | 2. Define an environmental variable GUMBYDIR which is a | ||
| 79 | directory which contains the gumby application: | ||
| 80 | In bash: | ||
| 81 | |||
| 82 | export GUMBYDIR=/home/username/gumby/ | ||
| 83 | |||
| 84 | In csh/tcsh: | ||
| 85 | |||
| 86 | setenv GUMBYDIR /home/username/gumby | ||
| 87 | |||
| 88 | 3. Include a definition of an environmental variable GUMBYDIR in | ||
| 89 | every script that will use this Gumby wrapper module, e.g.: | ||
| 90 | |||
| 91 | BEGIN { $ENV{GUMBYDIR} = '/home/username/gumby/' } | ||
| 92 | use Bio::Tools::Run::Phylo::Gumby; | ||
| 93 | |||
| 94 | =head1 FEEDBACK | ||
| 95 | |||
| 96 | =head2 Mailing Lists | ||
| 97 | |||
| 98 | User feedback is an integral part of the evolution of this and other | ||
| 99 | Bioperl modules. Send your comments and suggestions preferably to | ||
| 100 | the Bioperl mailing list. Your participation is much appreciated. | ||
| 101 | |||
| 102 | bioperl-l@bioperl.org - General discussion | ||
| 103 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 104 | |||
| 105 | =head2 Support | ||
| 106 | |||
| 107 | Please direct usage questions or support issues to the mailing list: | ||
| 108 | |||
| 109 | I<bioperl-l@bioperl.org> | ||
| 110 | |||
| 111 | rather than to the module maintainer directly. Many experienced and | ||
| 112 | reponsive experts will be able look at the problem and quickly | ||
| 113 | address it. Please include a thorough description of the problem | ||
| 114 | with code and data examples if at all possible. | ||
| 115 | |||
| 116 | =head2 Reporting Bugs | ||
| 117 | |||
| 118 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 119 | of the bugs and their resolution. Bug reports can be submitted via | ||
| 120 | the web: | ||
| 121 | |||
| 122 | http://redmine.open-bio.org/projects/bioperl/ | ||
| 123 | |||
| 124 | =head1 AUTHOR - Sendu Bala | ||
| 125 | |||
| 126 | Email bix@sendu.me.uk | ||
| 127 | |||
| 128 | =head1 APPENDIX | ||
| 129 | |||
| 130 | The rest of the documentation details each of the object methods. | ||
| 131 | Internal methods are usually preceded with a _ | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | package Bio::Tools::Run::Phylo::Gumby; | ||
| 136 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 137 | |||
| 138 | 1 | ▶ | use Cwd; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 139 | 1 | ▶ | use File::Spec; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 140 | 1 | ▶ | use Bio::AlignIO; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 141 | 1 | ▶ | use Bio::TreeIO; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 142 | 1 | ▶ | use Bio::Tools::GFF; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 143 | 1 | ▶ | use Bio::Tools::Phylo::Gumby; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 144 | |||
| 145 | use base qw(Bio::Tools::Run::Phylo::PhyloBase); | ||
| 146 | |||
| 147 | our $PROGRAM_NAME = 'gumby'; | ||
| 148 | our $PROGRAM_DIR = $ENV{'GUMBYDIR'}; | ||
| 149 | |||
| 150 | # methods for the gumby args we support | ||
| 151 | our @PARAMS = qw(annots ratio base plen prob); | ||
| 152 | our @SWITCHES = qw(econs); | ||
| 153 | |||
| 154 | # just to be explicit, args we don't support (yet) or we handle ourselves | ||
| 155 | our @UNSUPPORTED = qw(o minseq blklen); | ||
| 156 | |||
| 157 | =head2 program_name | ||
| 158 | |||
| 159 | Title : program_name | ||
| 160 | Usage : $factory>program_name() | ||
| 161 | Function: holds the program name | ||
| 162 | Returns : string | ||
| 163 | Args : None | ||
| 164 | |||
| 165 | =cut | ||
| 166 | |||
| 167 | sub program_name { | ||
| 168 | return $PROGRAM_NAME; | ||
| 169 | } | ||
| 170 | |||
| 171 | =head2 program_dir | ||
| 172 | |||
| 173 | Title : program_dir | ||
| 174 | Usage : $factory->program_dir(@params) | ||
| 175 | Function: returns the program directory, obtained from ENV variable. | ||
| 176 | Returns : string | ||
| 177 | Args : None | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | sub program_dir { | ||
| 182 | return $PROGRAM_DIR; | ||
| 183 | } | ||
| 184 | |||
| 185 | =head2 new | ||
| 186 | |||
| 187 | Title : new | ||
| 188 | Usage : $factory = Bio::Tools::Run::Phylo::Gumby->new() | ||
| 189 | Function: creates a new Gumby factory | ||
| 190 | Returns : Bio::Tools::Run::Phylo::Gumby | ||
| 191 | Args : Most options understood by gumby can be supplied as key => | ||
| 192 | value pairs. | ||
| 193 | |||
| 194 | These options can NOT be used with this wrapper: | ||
| 195 | o | ||
| 196 | minseq | ||
| 197 | blklen | ||
| 198 | |||
| 199 | =cut | ||
| 200 | |||
| 201 | sub new { | ||
| 202 | my ($class, @args) = @_; | ||
| 203 | my $self = $class->SUPER::new(@args); | ||
| 204 | |||
| 205 | $self->_set_from_args(\@args, -methods => [@PARAMS, @SWITCHES, 'quiet'], | ||
| 206 | -create => 1); | ||
| 207 | |||
| 208 | return $self; | ||
| 209 | } | ||
| 210 | |||
| 211 | =head2 annots | ||
| 212 | |||
| 213 | Title : annots | ||
| 214 | Usage : $factory->annots(@gff_filenames) | ||
| 215 | Function: Specify annotation files for gumby to use | ||
| 216 | Returns : string of absolute filepaths to gff files | ||
| 217 | Args : list of gff filenames (can be relative), where the first column | ||
| 218 | corresponds to a sequence id from the alignment that will be supplied | ||
| 219 | to run() | ||
| 220 | OR | ||
| 221 | list of Bio::SeqFeatureI objects, which have seq_id() values that | ||
| 222 | will correspond to the sequence ids from the alignment that will | ||
| 223 | be supplied to run() (the objects will be grouped by seq_id and | ||
| 224 | output to gff files for use by gumby; filepaths to those tempfiles | ||
| 225 | will be returned). Note that all features must have source, seq_id | ||
| 226 | and primary_tag set or none will be used. | ||
| 227 | |||
| 228 | NB: feature coordinates must be relative to the parts of the | ||
| 229 | sequences in the alignment you will supply, as though numbering | ||
| 230 | started at 1 for each each sequence in the alignment. There is | ||
| 231 | currently no automatic correction for this. | ||
| 232 | |||
| 233 | =cut | ||
| 234 | |||
| 235 | sub annots { | ||
| 236 | my $self = shift; | ||
| 237 | if (@_) { | ||
| 238 | my @files; | ||
| 239 | my %feats; | ||
| 240 | foreach my $thing (@_) { | ||
| 241 | if (ref($thing) && $thing->isa('Bio::SeqFeatureI')) { | ||
| 242 | my $seq_id = $thing->seq_id || $self->throw("Supplied a feature with no seq_id"); | ||
| 243 | push(@{$feats{$seq_id}}, $thing); | ||
| 244 | } | ||
| 245 | elsif (-e $thing) { | ||
| 246 | push(@files, File::Spec->rel2abs($thing)); | ||
| 247 | } | ||
| 248 | else { | ||
| 249 | $self->throw("'$thing' was not a Bio::SeqFeatureI or a file"); | ||
| 250 | } | ||
| 251 | } | ||
| 252 | |||
| 253 | if (keys %feats) { | ||
| 254 | my $temp_dir = $self->tempdir; | ||
| 255 | |||
| 256 | while (my ($seq_id, $feats) = each %feats) { | ||
| 257 | my $temp_file = File::Spec->catfile($temp_dir, $seq_id.'.gff'); | ||
| 258 | $temp_file = File::Spec->rel2abs($temp_file); | ||
| 259 | my $gffout = Bio::Tools::GFF->new(-file => ">$temp_file", -gff_version => 2); | ||
| 260 | $gffout->write_feature(@{$feats}); | ||
| 261 | push(@files, $temp_file); | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | $self->{annots} = \@files; | ||
| 266 | } | ||
| 267 | |||
| 268 | if (defined $self->{annots}) { | ||
| 269 | return join(' ', @{$self->{annots}}); | ||
| 270 | } | ||
| 271 | return; | ||
| 272 | } | ||
| 273 | |||
| 274 | =head2 run | ||
| 275 | |||
| 276 | Title : run | ||
| 277 | Usage : $result = $factory->run($fasta_align_file, $newick_tree_file); | ||
| 278 | -or- | ||
| 279 | $result = $factory->run($align_object, $tree_object); | ||
| 280 | -or- | ||
| 281 | $result = $factory->run($align_object, $db_taxonomy_object); | ||
| 282 | Function: Runs gumby on an alignment. | ||
| 283 | Returns : list of Bio::SeqFeature::Annotated (one per prediction and sequence) | ||
| 284 | Args : The first argument represents an alignment, the second argument | ||
| 285 | a species tree. | ||
| 286 | The alignment can be provided as a multi-fasta format alignment | ||
| 287 | filename, or a Bio::Align::AlignI complient object (eg. a | ||
| 288 | Bio::SimpleAlign). | ||
| 289 | The species tree can be provided as a newick format tree filename | ||
| 290 | or a Bio::Tree::TreeI complient object. Alternatively a | ||
| 291 | Bio::DB::Taxonomy object can be supplied, in which case the species | ||
| 292 | tree will be generated by using the alignment sequence names as | ||
| 293 | species names and looking for those in the supplied database. | ||
| 294 | |||
| 295 | In all cases, the alignment sequence names must correspond to node | ||
| 296 | ids in the species tree. Multi-word species names should have the | ||
| 297 | spaces removed to form the sequence names, eg. Homosapiens. | ||
| 298 | Underscores may also be used for either or both of sequence and node | ||
| 299 | ids ('Homo_sapiens'), but underscores will be removed internally. | ||
| 300 | |||
| 301 | NB: Gumby treats each sequence in the alignment as starting at | ||
| 302 | position 1. This method returns results with the coordinates | ||
| 303 | corrected so they match the coordinates of your input alignment. Eg. | ||
| 304 | if 'Homo_sapiens' sequence had the range 20..60 in your alignment, | ||
| 305 | the first Gumby result might be 1..5 which is corrected to 20..24. | ||
| 306 | |||
| 307 | =cut | ||
| 308 | |||
| 309 | sub run { | ||
| 310 | my ($self, $aln, $tree) = @_; | ||
| 311 | |||
| 312 | ($aln && $tree) || $self->throw("alignment and tree must be supplied"); | ||
| 313 | $aln = $self->_alignment($aln); | ||
| 314 | $tree = $self->_tree($tree); | ||
| 315 | |||
| 316 | $tree->force_binary; | ||
| 317 | |||
| 318 | # adjust seq & node ids to remove spaces and underscores (eg. if tree | ||
| 319 | # generated from taxonomy/ user input bad names) | ||
| 320 | foreach my $thing ($tree->get_leaf_nodes, $aln->each_seq) { | ||
| 321 | my $id = $thing->id; | ||
| 322 | $id =~ s/_aligned//; #*** dubious custom-handling for the allowed case of mlagan adding _aligned to id (according to gumby author) | ||
| 323 | $id =~ s/[ _]//g; | ||
| 324 | $thing->id($id); | ||
| 325 | } | ||
| 326 | my $new_aln = $aln->new; | ||
| 327 | foreach my $seq ($aln->each_seq) { | ||
| 328 | $new_aln->add_seq($seq); | ||
| 329 | } | ||
| 330 | $self->_alignment($new_aln); | ||
| 331 | |||
| 332 | #*** at some stage we want to revert the ids back to original... | ||
| 333 | |||
| 334 | # check node and seq names match | ||
| 335 | $self->_check_names; | ||
| 336 | |||
| 337 | return $self->_run; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub _run { | ||
| 341 | my $self = shift; | ||
| 342 | |||
| 343 | my $exe = $self->executable || return; | ||
| 344 | |||
| 345 | # cd to a temp dir | ||
| 346 | my $temp_dir = $self->tempdir; | ||
| 347 | my $cwd = Cwd->cwd(); | ||
| 348 | chdir($temp_dir) || $self->throw("Couldn't change to temp dir '$temp_dir'"); | ||
| 349 | |||
| 350 | my $tree_file = 'tree_file'; | ||
| 351 | my $aln_file = $self->_write_alignment; | ||
| 352 | |||
| 353 | # generate a gumby-friendly tree file | ||
| 354 | my $tree = $self->_tree; | ||
| 355 | $tree = $tree->simplify_to_leaves_string; | ||
| 356 | open(my $tfhandle, '>', $tree_file) || $self->throw("Could not write to tree file '$tree_file'"); | ||
| 357 | print $tfhandle $tree, "\n"; | ||
| 358 | close($tfhandle); | ||
| 359 | |||
| 360 | my $command = $exe.$self->_setparams($aln_file, $tree_file); | ||
| 361 | $self->debug("gumby command = $command\n"); | ||
| 362 | |||
| 363 | open(PIPE, "$command |") || $self->throw("gumby call ($command) failed to start: $? | $!"); | ||
| 364 | my $error = ''; | ||
| 365 | while (<PIPE>) { | ||
| 366 | print unless $self->quiet; | ||
| 367 | if (/^ERROR: (.+)/ || /^mbgumbel\(\): (.+)/) { | ||
| 368 | $error .= $1; | ||
| 369 | } | ||
| 370 | } | ||
| 371 | close(PIPE) || ($error ? $self->warn("gumby call ($command) failed: $error") : $self->throw("gumby call ($command) crashed: $?")); | ||
| 372 | |||
| 373 | my $aln = $self->_alignment(); | ||
| 374 | my %offsets; | ||
| 375 | foreach my $seq ($aln->each_seq) { | ||
| 376 | $offsets{lc($seq->id)} = $seq->start - 1; | ||
| 377 | } | ||
| 378 | |||
| 379 | my @feats = (); | ||
| 380 | foreach my $file ('out_all.align', 'out_exon.align', 'out_nonexon.align') { | ||
| 381 | -e $file || next; | ||
| 382 | my $parser = Bio::Tools::Phylo::Gumby->new(-file => $file); | ||
| 383 | |||
| 384 | while (my @results = $parser->next_result) { | ||
| 385 | foreach my $result (@results) { | ||
| 386 | my $this_adjust = $offsets{lc($result->seq_id)}; | ||
| 387 | $result->start($result->start + $this_adjust); | ||
| 388 | $result->end($result->end + $this_adjust); | ||
| 389 | } | ||
| 390 | push(@feats, @results); | ||
| 391 | } | ||
| 392 | |||
| 393 | unlink($file); | ||
| 394 | } | ||
| 395 | |||
| 396 | # cd back again | ||
| 397 | chdir($cwd) || $self->throw("Couldn't change back to working directory '$cwd'"); | ||
| 398 | |||
| 399 | return @feats; | ||
| 400 | } | ||
| 401 | |||
| 402 | =head2 _setparams | ||
| 403 | |||
| 404 | Title : _setparams | ||
| 405 | Usage : Internal function, not to be called directly | ||
| 406 | Function: Creates a string of params to be used in the command string | ||
| 407 | Returns : string of params | ||
| 408 | Args : alignment and tree file names | ||
| 409 | |||
| 410 | =cut | ||
| 411 | |||
| 412 | sub _setparams { | ||
| 413 | my ($self, $aln_file, $tree_file) = @_; | ||
| 414 | |||
| 415 | my $param_string = ' '.$tree_file; | ||
| 416 | $param_string .= ' '.$aln_file; | ||
| 417 | $param_string .= $self->SUPER::_setparams(-params => \@PARAMS, | ||
| 418 | -switches => \@SWITCHES, | ||
| 419 | -dash => 1,); | ||
| 420 | $param_string .= ' -o out'; | ||
| 421 | $param_string .= ' 2>&1'; | ||
| 422 | $param_string .= ' 1>/dev/null' if $self->quiet; | ||
| 423 | |||
| 424 | return $param_string; | ||
| 425 | } | ||
| 426 | |||
| 427 | 1; | ||