| 1 | # $Id: Match.pm,v 1.3 2007/05/25 10:14:55 sendu Exp $ | ||
| 2 | # | ||
| 3 | # BioPerl module for Bio::Tools::Run::Match | ||
| 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::Match - Wrapper for Transfac's match(TM) | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | use Bio::Tools::Run::Match; | ||
| 22 | |||
| 23 | # Make a Match factory | ||
| 24 | $factory = Bio::Tools::Run::Match->new(-mxlib => '/path/to/matrix.dat'); | ||
| 25 | |||
| 26 | # Run Match on an sequence object | ||
| 27 | my @results = $factory->run($bio_seq); | ||
| 28 | |||
| 29 | # look at the results | ||
| 30 | foreach my $feat (@results) { | ||
| 31 | my $seq_id = $feat->seq_id; | ||
| 32 | my $start = $feat->start; | ||
| 33 | my $end = $feat->end; | ||
| 34 | my $score = $feat->score; | ||
| 35 | my ($pvalue) = $feat->get_tag_values('pvalue'); | ||
| 36 | } | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | This is a wrapper for running the match(TM) program supplied with Transfac Pro | ||
| 41 | distributions. | ||
| 42 | |||
| 43 | You can try supplying normal match command-line arguments to new(), eg. | ||
| 44 | new(-b => 1) or calling arg-named methods (excluding the initial | ||
| 45 | hyphens, eg. $factory->b(1) to set the -b option to true). | ||
| 46 | |||
| 47 | Histogram output isn't supported. -p is supported by using -mxprf, see the | ||
| 48 | docs of new() for details. | ||
| 49 | |||
| 50 | You will need to enable this match wrapper to find the match executable. | ||
| 51 | This can be done in (at least) three ways: | ||
| 52 | |||
| 53 | 1. Make sure match is in your path. | ||
| 54 | 2. Define an environmental variable MATCHDIR which is a | ||
| 55 | directory which contains the match executable: | ||
| 56 | In bash: | ||
| 57 | |||
| 58 | export MATCHDIR=/home/username/match/ | ||
| 59 | |||
| 60 | In csh/tcsh: | ||
| 61 | |||
| 62 | setenv MATCHDIR /home/username/match | ||
| 63 | |||
| 64 | 3. Include a definition of an environmental variable MATCHDIR in | ||
| 65 | every script that will use this match wrapper module, e.g.: | ||
| 66 | |||
| 67 | BEGIN { $ENV{MATCHDIR} = '/home/username/match/' } | ||
| 68 | use Bio::Tools::Run::Match; | ||
| 69 | |||
| 70 | =head1 FEEDBACK | ||
| 71 | |||
| 72 | =head2 Mailing Lists | ||
| 73 | |||
| 74 | User feedback is an integral part of the evolution of this and other | ||
| 75 | Bioperl modules. Send your comments and suggestions preferably to | ||
| 76 | the Bioperl mailing list. Your participation is much appreciated. | ||
| 77 | |||
| 78 | bioperl-l@bioperl.org - General discussion | ||
| 79 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 80 | |||
| 81 | =head2 Support | ||
| 82 | |||
| 83 | Please direct usage questions or support issues to the mailing list: | ||
| 84 | |||
| 85 | I<bioperl-l@bioperl.org> | ||
| 86 | |||
| 87 | rather than to the module maintainer directly. Many experienced and | ||
| 88 | reponsive experts will be able look at the problem and quickly | ||
| 89 | address it. Please include a thorough description of the problem | ||
| 90 | with code and data examples if at all possible. | ||
| 91 | |||
| 92 | =head2 Reporting Bugs | ||
| 93 | |||
| 94 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 95 | of the bugs and their resolution. Bug reports can be submitted via | ||
| 96 | the web: | ||
| 97 | |||
| 98 | http://redmine.open-bio.org/projects/bioperl/ | ||
| 99 | |||
| 100 | =head1 AUTHOR - Sendu Bala | ||
| 101 | |||
| 102 | Email bix@sendu.me.uk | ||
| 103 | |||
| 104 | =head1 APPENDIX | ||
| 105 | |||
| 106 | The rest of the documentation details each of the object methods. | ||
| 107 | Internal methods are usually preceded with a _ | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | package Bio::Tools::Run::Match; | ||
| 112 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 113 | |||
| 114 | 1 | ▶ | use Cwd; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 115 | 1 | ▶ | use File::Spec; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 116 | 1 | ▶ | use Bio::SeqIO; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 117 | 1 | ▶ | use Bio::FeatureIO; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 118 | use Bio::Annotation::SimpleValue; | ||
| 119 | use Bio::Tools::Match; | ||
| 120 | |||
| 121 | use base qw(Bio::Tools::Run::WrapperBase); | ||
| 122 | |||
| 123 | our $PROGRAM_NAME = 'match'; | ||
| 124 | our $PROGRAM_DIR = $ENV{'MATCHDIR'}; | ||
| 125 | |||
| 126 | # methods for the match args we support | ||
| 127 | our @PARAMS = qw(mxlib mxprf imcut); # these aren't actually match args, but | ||
| 128 | # are methods we use internally | ||
| 129 | our @SWITCHES = qw(b u); | ||
| 130 | |||
| 131 | # just to be explicit, args we don't support (yet) or we handle ourselves | ||
| 132 | our @UNSUPPORTED = qw(H HH pp ppg pn png pr jkn i p); | ||
| 133 | |||
| 134 | |||
| 135 | =head2 program_name | ||
| 136 | |||
| 137 | Title : program_name | ||
| 138 | Usage : $factory>program_name() | ||
| 139 | Function: holds the program name | ||
| 140 | Returns : string | ||
| 141 | Args : None | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub program_name { | ||
| 146 | return $PROGRAM_NAME; | ||
| 147 | } | ||
| 148 | |||
| 149 | =head2 program_dir | ||
| 150 | |||
| 151 | Title : program_dir | ||
| 152 | Usage : $factory->program_dir(@params) | ||
| 153 | Function: returns the program directory, obtained from ENV variable. | ||
| 154 | Returns : string | ||
| 155 | Args : None | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | sub program_dir { | ||
| 160 | return $PROGRAM_DIR; | ||
| 161 | } | ||
| 162 | |||
| 163 | =head2 new | ||
| 164 | |||
| 165 | Title : new | ||
| 166 | Usage : $factory = Bio::Tools::Run::Match->new() | ||
| 167 | Function: creates a new MCS factory | ||
| 168 | Returns : Bio::Tools::Run::MCS | ||
| 169 | Args : The following args can either be supplied here or set by calling | ||
| 170 | arg-named methods (eg. $factory->imcut(2) ). | ||
| 171 | |||
| 172 | -mxlib => path to the matrix.dat file containing Transfac matricies | ||
| 173 | -mxprf => path to a profile file | [core_thresh, [matrix_thresh]] | ||
| 174 | (defaults to a standard one based on the mxlib provided if | ||
| 175 | file not supplied, using core_thresh and matrix_thresh | ||
| 176 | values if those are supplied instead) | ||
| 177 | -imcut => floating point number, the importance cutoff | ||
| 178 | -b | -u => boolean, mutually exclusive | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | sub new { | ||
| 183 | my ($class, @args) = @_; | ||
| 184 | my $self = $class->SUPER::new(@args); | ||
| 185 | |||
| 186 | $self->_set_from_args(\@args, -methods => [@PARAMS, @SWITCHES, 'quiet'], | ||
| 187 | -create => 1); | ||
| 188 | |||
| 189 | return $self; | ||
| 190 | } | ||
| 191 | |||
| 192 | =head2 run | ||
| 193 | |||
| 194 | Title : run | ||
| 195 | Usage : $result = $factory->run($bio_seqi_object); | ||
| 196 | Function: Runs match on a sequence. | ||
| 197 | Returns : list of Bio::SeqFeatureI feature objects | ||
| 198 | Args : Bio::SeqI compliant object | ||
| 199 | |||
| 200 | NB: mxlib has to have been set prior to calling run(), either as an | ||
| 201 | argument to new() or by calling mxlib(). | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub run { | ||
| 206 | my ($self, $seq) = @_; | ||
| 207 | $self->mxlib || $self->throw("mxlib has to have been set first"); | ||
| 208 | |||
| 209 | return $self->_run($seq); | ||
| 210 | } | ||
| 211 | |||
| 212 | sub _run { | ||
| 213 | my ($self, $seq) = @_; | ||
| 214 | |||
| 215 | my $exe = $self->executable || return; | ||
| 216 | |||
| 217 | my $mxlib = File::Spec->rel2abs($self->mxlib()); | ||
| 218 | my $mxprf_file = $self->mxprf(); | ||
| 219 | if ($mxprf_file && -e $mxprf_file) { | ||
| 220 | $mxprf_file = File::Spec->rel2abs($mxprf_file); | ||
| 221 | } | ||
| 222 | |||
| 223 | # cd to a temp dir | ||
| 224 | my $temp_dir = $self->tempdir; | ||
| 225 | my $cwd = Cwd->cwd(); | ||
| 226 | chdir($temp_dir) || $self->throw("Couldn't change to temp dir '$temp_dir'"); | ||
| 227 | |||
| 228 | # make the profile file if necessary | ||
| 229 | if (! $mxprf_file || ! -e $mxprf_file) { | ||
| 230 | my @thresh; | ||
| 231 | if ($mxprf_file && ref($mxprf_file) eq 'ARRAY') { | ||
| 232 | @thresh = @{$mxprf_file}; | ||
| 233 | } | ||
| 234 | |||
| 235 | $mxprf_file = 'mxprf'; | ||
| 236 | system("$exe $mxlib ignored ignored $mxprf_file -p @thresh") && $self->throw("Something went wrong whist creating profile: $! | $?"); | ||
| 237 | } | ||
| 238 | |||
| 239 | # output the sequence to a fasta file | ||
| 240 | my $seq_file = 'sequence.fa'; | ||
| 241 | my $so = Bio::SeqIO->new(-file => ">$seq_file", -format => 'fasta'); | ||
| 242 | $so->write_seq($seq); | ||
| 243 | $so->close(); | ||
| 244 | |||
| 245 | # run match | ||
| 246 | my $result_file = 'out'; | ||
| 247 | my $param_str = $self->_setparams(); | ||
| 248 | my $cmd_line = "$exe $mxlib $seq_file $result_file $mxprf_file".$param_str; | ||
| 249 | |||
| 250 | system($cmd_line) && $self->throw("Something went wrong whist running '$cmd_line': $! | $?"); | ||
| 251 | |||
| 252 | # parse the results | ||
| 253 | my $parser = Bio::Tools::Match->new(-file => $result_file); | ||
| 254 | |||
| 255 | # correct the coords | ||
| 256 | my @feats; | ||
| 257 | while (my $feat = $parser->next_result) { | ||
| 258 | push(@feats, $feat); | ||
| 259 | } | ||
| 260 | |||
| 261 | # cd back again | ||
| 262 | chdir($cwd) || $self->throw("Couldn't change back to working directory '$cwd'"); | ||
| 263 | |||
| 264 | return @feats; | ||
| 265 | } | ||
| 266 | |||
| 267 | =head2 _setparams | ||
| 268 | |||
| 269 | Title : _setparams | ||
| 270 | Usage : Internal function, not to be called directly | ||
| 271 | Function: Creates a string of params to be used in the command string | ||
| 272 | Returns : string of params | ||
| 273 | Args : none | ||
| 274 | |||
| 275 | =cut | ||
| 276 | |||
| 277 | sub _setparams { | ||
| 278 | my $self = shift; | ||
| 279 | |||
| 280 | my $param_string = $self->SUPER::_setparams(-switches => \@SWITCHES, | ||
| 281 | -dash => 1); | ||
| 282 | |||
| 283 | $param_string .= ' 1>/dev/null' if $self->quiet; | ||
| 284 | |||
| 285 | return $param_string; | ||
| 286 | } | ||
| 287 | |||
| 288 | 1; | ||