| 1 |
|
| 2 |
# TFBS module for TFBS::PatternGen::YMF |
| 3 |
# |
| 4 |
# Copyright Wynand Alkema |
| 5 |
# |
| 6 |
# You may distribute this module under the same terms as perl itself |
| 7 |
# |
| 8 |
|
| 9 |
# POD |
| 10 |
|
| 11 |
=head1 NAME |
| 12 |
|
| 13 |
TFBS::PatternGen::MEME - a pattern factory that uses the MEME program |
| 14 |
|
| 15 |
=head1 SYNOPSIS |
| 16 |
|
| 17 |
my $patterngen = |
| 18 |
TFBS::PatternGen::MEME->new(-seq_file=>'sequences.fa', |
| 19 |
-binary => 'meme' |
| 20 |
|
| 21 |
|
| 22 |
my $pfm = $patterngen->pattern(); # $pfm is now a TFBS::Matrix::PFM object |
| 23 |
|
| 24 |
=head1 DESCRIPTION |
| 25 |
|
| 26 |
TFBS::PatternGen::MEME builds position frequency matrices |
| 27 |
using an external program MEME written by Bailey and Elkan. |
| 28 |
For information and source code of MEME see |
| 29 |
|
| 30 |
http://www.sdsc.edu/MEME |
| 31 |
|
| 32 |
|
| 33 |
=head1 FEEDBACK |
| 34 |
|
| 35 |
Please send bug reports and other comments to the author. |
| 36 |
|
| 37 |
=head1 AUTHOR - Wynand Alkema |
| 38 |
|
| 39 |
|
| 40 |
Wynand Alkema E<lt>[email protected]<gt> |
| 41 |
|
| 42 |
=cut |
| 43 |
|
| 44 |
package TFBS::PatternGen::YMF; |
| 45 |
use vars qw(@ISA); |
| 46 |
use strict; |
| 47 |
|
| 48 |
|
| 49 |
# Object preamble - inherits from TFBS::PatternGen; |
| 50 |
|
| 51 |
use TFBS::PatternGen; |
| 52 |
use TFBS::PatternGen::YMF::Motif; |
| 53 |
use File::Temp qw(:POSIX); |
| 54 |
use Bio::Seq; |
| 55 |
use Bio::SeqIO; |
| 56 |
use File::Temp qw/ tempfile tempdir /; |
| 57 |
@ISA = qw(TFBS::PatternGen); |
| 58 |
|
| 59 |
=head2 new |
| 60 |
|
| 61 |
Title : new |
| 62 |
Usage : my $patterngen = TFBS::PatternGen::YMF->new(%args); |
| 63 |
Function: the constructor for the TFBS::PatternGen::MEME object |
| 64 |
Returns : a TFBS::PatternGen::MEME object |
| 65 |
Args : This method takes named arguments; |
| 66 |
you must specify one of the following three |
| 67 |
-seq_list # a reference to an array of strings |
| 68 |
# and/or Bio::Seq objects |
| 69 |
# or |
| 70 |
-seq_stream # A Bio::SeqIO object |
| 71 |
# or |
| 72 |
-seq_file # the name of the fasta file containing |
| 73 |
# all the sequences |
| 74 |
Other arguments are: |
| 75 |
-binary # a fully qualified path to the 'meme' executable |
| 76 |
# OPTIONAL: default 'meme' |
| 77 |
-additional_params # a string containing additional |
| 78 |
# command-line switches for the |
| 79 |
# meme program |
| 80 |
|
| 81 |
=cut |
| 82 |
|
| 83 |
sub new { |
| 84 |
my ($caller, %args) = @_; |
| 85 |
my $self = bless {}, ref($caller) || $caller; |
| 86 |
|
| 87 |
$self->{'width'}=$args{'-length_oligo'}; |
| 88 |
$self->{'path_org'}=$args{'-pathoforganismtables'}; |
| 89 |
$self->{'len_region'}=$args{'-length_region'}; |
| 90 |
$self->{'config_file'}=$args{'-config_file'}||$args{'-stats_path'}."/stats.config"; |
| 91 |
#The latter is the example configfile that comes with the installation of YMF |
| 92 |
|
| 93 |
|
| 94 |
$self->{'abs_stats_path'} = $args{'-abs_stats_path'} ; |
| 95 |
#This is the directory where the executable and the results file is |
| 96 |
#generated by the program are located |
| 97 |
|
| 98 |
$self->_create_seq_set(%args) or die ('Error creating sequence set'); |
| 99 |
|
| 100 |
$self->_run_stats() or $self->throw("Error running stats."); |
| 101 |
return $self; |
| 102 |
} |
| 103 |
|
| 104 |
=head2 pattern |
| 105 |
|
| 106 |
=head2 all_patterns |
| 107 |
|
| 108 |
=head2 patternSet |
| 109 |
|
| 110 |
The three methods listed above are used for the retrieval of patterns, |
| 111 |
and are common to all TFBS::PatternGen::* classes. Please |
| 112 |
see L<TFBS::PatternGen> for details. |
| 113 |
|
| 114 |
=cut |
| 115 |
|
| 116 |
sub _run_stats{ |
| 117 |
my ($self)=shift; |
| 118 |
my $tmp_file = tmpnam(); |
| 119 |
my $dumpfile = tmpnam(); |
| 120 |
my $outstream = Bio::SeqIO->new(-file=>">$tmp_file", -format=>"fasta"); |
| 121 |
foreach my $seqobj (@{ $self->{'seq_set'} } ) { |
| 122 |
$outstream->write_seq($seqobj); |
| 123 |
} |
| 124 |
$outstream->close(); |
| 125 |
my $dir = tempdir(); |
| 126 |
#print $dir; |
| 127 |
#change directory to directory where the program is located |
| 128 |
#system 'cd $dir.w;'; |
| 129 |
# my $command="cd $dir;"; |
| 130 |
# print $command; |
| 131 |
# system $command; |
| 132 |
# `$command`; |
| 133 |
# system 'ls -ltr'; |
| 134 |
|
| 135 |
my $command_line = |
| 136 |
$self->{'abs_stats_path'}."/stats ". |
| 137 |
# "stats ". |
| 138 |
$self->{'config_file'}." ". |
| 139 |
$self->{'len_region'}." ". |
| 140 |
$self->{'width'}." ". |
| 141 |
$self->{'path_org'}." ". |
| 142 |
"-sort ".#sorts on z-score |
| 143 |
$tmp_file |
| 144 |
." >$dumpfile" |
| 145 |
; |
| 146 |
# print STDERR "cd $dir;$command_line\n"; |
| 147 |
my $resultstring = `cd $dir;$command_line`; |
| 148 |
# print STDERR $resultstring; |
| 149 |
|
| 150 |
$self->_parse_stats_output($resultstring,$command_line,$dumpfile,$dir); |
| 151 |
unlink $tmp_file; |
| 152 |
#unlink $dumpfile; |
| 153 |
return 1 |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
# |
| 159 |
sub _parse_stats_output{ |
| 160 |
my ($self,$resultstring,$command_line,$dumpfile,$temp_dir)=@_; |
| 161 |
open DUMP,$dumpfile; |
| 162 |
while(<DUMP>){ |
| 163 |
if ((/(^Error.*)/) or /(.*Aborting.*)/){ |
| 164 |
# warn "Error running AnnSpec\nNo patterns produced"; |
| 165 |
print "YMF Error message: \"$1\"\n"; |
| 166 |
unlink $dumpfile; |
| 167 |
$self->throw ("Error running YMF using command:\n $command_line"); |
| 168 |
return; |
| 169 |
} |
| 170 |
} |
| 171 |
unlink $dumpfile; |
| 172 |
open RES,"$temp_dir/results"; |
| 173 |
my $skip=<RES>; |
| 174 |
while (<RES>){ |
| 175 |
my ($word,$occ,$z_score,$expect,$var)=split; |
| 176 |
#print $word; |
| 177 |
my $motif =TFBS::PatternGen::YMF::Motif->new |
| 178 |
(-word=>$word, |
| 179 |
-tags => {z_score=>$z_score, |
| 180 |
'occurences'=>$occ, |
| 181 |
'expectation value'=>$expect, |
| 182 |
'variance'=>$var} |
| 183 |
); |
| 184 |
push @{ $self->{'motifs'} }, $motif; |
| 185 |
} |
| 186 |
my $command="rm -r $temp_dir"; |
| 187 |
#print $command; |
| 188 |
`$command`;# or die "could not unlink $!"; |
| 189 |
# return |
| 190 |
} |
| 191 |
# |
| 192 |
|
| 193 |
1; |