1# $Id$
2#
3# BioPerl module for Bio::Tools::Run::Phylo::Hyphy::REL
4#
5# Please direct questions and support issues to <bioperl-l@bioperl.org>
6#
7# Cared for by Albert Vilella <avilella-at-gmail-dot-com>
8#
9# Copyright Albert Vilella
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
17Bio::Tools::Run::Phylo::Hyphy::REL - Wrapper around the Hyphy REL analysis
18
19=head1 SYNOPSIS
20
21  use Bio::Tools::Run::Phylo::Hyphy::REL;
22  use Bio::AlignIO;
23  use Bio::TreeIO;
24
25  my $alignio = Bio::AlignIO->new(-format => 'fasta',
26                                  -file   => 't/data/hyphy1.fasta');
27
28  my $aln = $alignio->next_aln;
29
30  my $treeio = Bio::TreeIO->new(
31      -format => 'newick', -file => 't/data/hyphy1.tree');
32
33  my $rel = Bio::Tools::Run::Phylo::Hyphy::REL->new();
34  $rel->alignment($aln);
35  $rel->tree($tree);
36  my ($rc,$results) = $rel->run();
37
38=head1 DESCRIPTION
39
40This is a wrapper around the REL analysis of HyPhy ([Hy]pothesis
41Testing Using [Phy]logenies) package of Sergei Kosakowsky Pond,
42Spencer V. Muse, Simon D.W. Frost and Art Poon.  See
43http://www.hyphy.org for more information.
44
45This module will generate the correct list of options for interfacing
46with TemplateBatchFiles/Ghostrides/Wrapper.bf.
47
48=head1 FEEDBACK
49
50=head2 Mailing Lists
51
52User feedback is an integral part of the evolution of this and other
53Bioperl modules. Send your comments and suggestions preferably to
54the Bioperl mailing list.  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
61Please direct usage questions or support issues to the mailing list:
62
63I<bioperl-l@bioperl.org>
64
65rather than to the module maintainer directly. Many experienced and
66reponsive experts will be able look at the problem and quickly
67address it. Please include a thorough description of the problem
68with code and data examples if at all possible.
69
70=head2 Reporting Bugs
71
72Report bugs to the Bioperl bug tracking system to help us keep track
73of the bugs and their resolution. Bug reports can be submitted via the
74web:
75
76  http://redmine.open-bio.org/projects/bioperl/
77
78=head1 AUTHOR - Albert Vilella
79
80Email avilella-at-gmail-dot-com
81
82=head1 CONTRIBUTORS
83
84Additional contributors names and emails here
85
86=head1 APPENDIX
87
88The rest of the documentation details each of the object methods.
89Internal methods are usually preceded with a _
90
91=cut
92
93
94# Let the code begin...
95
96
97package Bio::Tools::Run::Phylo::Hyphy::REL;
98use strict;
99use Bio::Root::Root;
100use Bio::AlignIO;
101use Bio::TreeIO;
102use Bio::Tools::Run::Phylo::Hyphy::Base;
103use Bio::Tools::Run::WrapperBase;
104
105use base qw(Bio::Root::Root Bio::Tools::Run::Phylo::Hyphy::Base);
106
107
108=head2 valid_values
109
110 Title   : valid_values
111 Usage   : $factory->valid_values()
112 Function: returns the possible parameters
113 Returns:  an array holding all possible parameters. The default
114values are always the first one listed.  These descriptions are
115essentially lifted from the python wrapper or provided by the author.
116 Args    : None
117
118=cut
119
120sub valid_values {
121    return
122        (
123         {'geneticCode' => [ "Universal","VertebratemtDNA","YeastmtDNA","Mold/ProtozoanmtDNA",
124                             "InvertebratemtDNA","CiliateNuclear","EchinodermmtDNA","EuplotidNuclear",
125                             "Alt.YeastNuclear","AscidianmtDNA","FlatwormmtDNA","BlepharismaNuclear"]},
126         {'tempalnfile' => undef }, # aln file goes here
127         {'temptreefile' => undef }, # tree file goes here
128         {'Model' => [ "Null for Test 1", "Null for Test 2", "Alternative"]},
129         {'outfile' => undef } # site-by-site conditional probabilities go to this file
130        );
131}
132
133=head2 new
134
135 Title   : new
136 Usage   : my $obj = Bio::Tools::Run::Phylo::Hyphy::REL->new();
137 Function: Builds a new Bio::Tools::Run::Phylo::Hyphy::REL object
138 Returns : Bio::Tools::Run::Phylo::Hyphy::REL
139 Args    : -alignment => the Bio::Align::AlignI object
140           -save_tempfiles => boolean to save the generated tempfiles and
141                              NOT cleanup after onesself (default FALSE)
142           -tree => the Bio::Tree::TreeI object
143           -params => a hashref of parameters (all passed to set_parameter)
144           -executable => where the hyphy executable resides
145
146See also: L<Bio::Tree::TreeI>, L<Bio::Align::AlignI>
147
148=cut
149
150sub new {
151   my($class,@args) = @_;
152   my $self = $class->SUPER::new(@args);
153   my ($aln, $tree, $st, $params, $exe,
154   $ubl) = $self->_rearrange([qw(ALIGNMENT TREE SAVE_TEMPFILES PARAMS EXECUTABLE)], @args);
155   defined $aln && $self->alignment($aln);
156   defined $tree && $self->tree($tree);
157   defined $st  && $self->save_tempfiles($st);
158   defined $exe && $self->executable($exe);
159
160   $self->set_default_parameters();
161   if( defined $params ) {
162      if( ref($params) !~ /HASH/i ) {
163         $self->warn("Must provide a valid hash ref for parameter -FLAGS");
164      } else {
165         map { $self->set_parameter($_, $$params{$_}) } keys %$params;
166      }
167   }
168   return $self;
169}
170
171
172=head2 run
173
174 Title   : run
175 Usage   : my ($rc,$results) = $rel->run($aln);
176 Function: run the rel analysis using the default or updated parameters
177           the alignment parameter must have been set
178 Returns : Return code, Hash
179 Args    : L<Bio::Align::AlignI> object,
180         L<Bio::Tree::TreeI> object [optional]
181
182
183=cut
184
185sub run {
186    my $self = shift;
187    my ($rc,$run_results) = $self->SUPER::run();
188    my $results = {};
189    my $outfile = $self->outfile_name();
190    open(OUTFILE, "$outfile") or $self->throw("cannot open $outfile for reading");
191    my $readed_header = 0;
192    my @elems;
193    while (<OUTFILE>) {
194        if ($readed_header) {
195           # REL results are csv
196           my @values = split("\,",$_);
197           for my $i (0 .. (scalar(@values)-1)) {
198               $elems[$i] =~ s/\n//g;
199               push @{$results->{$elems[$i]}}, $values[$i];
200           }
201        } else {
202           @elems = split("\,",$_);
203           $readed_header = 1;
204        }
205    }
206    return ($rc,$results);
207}
208
209=head2 create_wrapper
210
211 Title   : create_wrapper
212 Usage   : $self->create_wrapper
213 Function: It will create the wrapper file that interfaces with the analysis bf file
214 Example :
215 Returns :
216 Args    :
217
218
219=cut
220
221sub create_wrapper {
222   my $self = shift;
223
224   my $batchfile = "YangNielsenBranchSite2005.bf";
225   $self->SUPER::create_wrapper($batchfile);
226}
227
2281;
229