Debian Codegraph @

blib/lib/TFBS/PatternGen/MEME.pm

raw
1
2 # TFBS module for TFBS::PatternGen::MEME
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::MEME;
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::SimplePFM;
53 use TFBS::PatternGen::MEME::Motif;
54 use File::Temp qw(:POSIX);
55 use Bio::Seq;
56 use Bio::SeqIO;
57
58 @ISA = qw(TFBS::PatternGen);
59
60 =head2 new
61
62 Title : new
63 Usage : my $pattrengen = TFBS::PatternGen::MEME->new(%args);
64 Function: the constructor for the TFBS::PatternGen::MEME object
65 Returns : a TFBS::PatternGen::MEME object
66 Args : This method takes named arguments;
67 you must specify one of the following three
68 -seq_list # a reference to an array of strings
69 # and/or Bio::Seq objects
70 # or
71 -seq_stream # A Bio::SeqIO object
72 # or
73 -seq_file # the name of the fasta file containing
74 # all the sequences
75 Other arguments are:
76 -binary # a fully qualified path to the 'meme' executable
77 # OPTIONAL: default 'meme'
78 -additional_params # a string containing additional
79 # command-line switches for the
80 # meme program
81
82 =cut
83
84 sub new {
85 my ($caller, %args) = @_;
86 my $self = bless {}, ref($caller) || $caller;
87 $self->{'filename'} =$args{'-seq_file'};
88
89 $self->{'additional_params'} =
90 ($args{'-additional_params'}
91 ? (ref($args{'-additional_params'})
92 ? join(' ', @{$args{'-additional_params'}})
93 : $args{'-additional_params'})
94 : "" );
95 $self->{'binary'} = $args{'-binary'} || 'meme';
96
97 $self->_create_seq_set(%args) or die ('Error creating sequence set');
98
99 $self->_run_meme() or $self->throw("Error running MEME.");
100 return $self;
101 }
102
103 =head2 pattern
104
105 =head2 all_patterns
106
107 =head2 patternSet
108
109 The three methods listed above are used for the retrieval of patterns,
110 and are common to all TFBS::PatternGen::* classes. Please
111 see L<TFBS::PatternGen> for details.
112
113 =cut
114
115 sub _run_meme{
116 my ($self)=shift;
117 my $tmp_file = tmpnam();
118 my $outstream = Bio::SeqIO->new(-file=>">$tmp_file", -format=>"fasta");
119 foreach my $seqobj (@{ $self->{'seq_set'} } ) {
120 $outstream->write_seq($seqobj);
121 }
122 $outstream->close();
123 my $command_line =
124 $self->{'binary'}." ".
125 $tmp_file." ".
126 "-text ".
127 "-dna ".
128 $self->{'additional_params'}
129 ." 2>/dev/null"
130 ;
131 # print STDERR "$command_line\n";
132 my $resultstring = `$command_line`;
133 # print STDERR $resultstring;
134 $self->_parse_meme_output($resultstring,$command_line);
135 unlink $tmp_file;
136 return 1
137 }
138
139 sub _parse_meme_output{
140 my ($self,$resultstring,$command_line)=@_;
141 if ($resultstring=~/^error/){
142 # warn "Error running AnnSpec\nNo patterns produced";
143 $self->throw ("Error running MEME command:\n $command_line");
144 return;
145 }
146 my @motifs=split(/\*\nMOTIF/,$resultstring);
147 shift @motifs;#discard the first one
148 #print STDERR scalar @motifs,"\n";
149 foreach my $raw_motif(@motifs){
150 my ($matrix,$sites,$score)=$self->_parse_raw_matrix($raw_motif);
151 # print STDERR $matrix;
152 my $motif =TFBS::PatternGen::MEME::Motif->new
153 (
154 -tags => {score=>$score},#The score in this case is the E-value given in the output
155 -sites=>$sites,
156 -matrix => $matrix
157 );
158 push @{ $self->{'motifs'} }, $motif;
159 }
160 return
161 }
162 #
163 #
164 sub _parse_raw_matrix{
165 my ($self,$string)=@_;
166 my @sites;
167 my @matrix;
168 $string=~s/(Motif \d+ block diagrams.*)//s;
169 # print STDERR $string;
170 my ($width,$e_value)=$string=~/width =\s+(\d+)\s+sites.*E-value =(.*)\n/;
171 # print STDERR $e_value,"\n";
172 $string=~s/.*Motif \d+ sites sorted by position p-value//s;
173 #print STDERR $string;
174 my @array=split("\n",$string);
175 foreach my $line(@array){
176 my $nr=0;
177 my $strand=1;#if revcomp is not selected the strand is always 1
178 next if $line=~/^-/;
179 next if $line=~/P-value\s+Site/;
180 my (@properties)=split(/\s+/,$line);
181 next if @properties<1;
182 # print STDERR "@properties\n";
183 #First determine whether -revcomp switch is used and thus strand info is given
184 if ($properties[1] eq "+" or $properties[1] eq "-"){
185 $strand=$properties[1];
186 $nr=1;
187 }
188 my $site = Bio::SeqFeature::Generic->new ( -start =>$properties[1+$nr],
189 -end =>$properties[1+$nr]+$width-1,
190 -strand=>$strand,
191 -source=>'MEME',
192 -score=>$properties[2+$nr]
193 );
194 foreach my $seq(@{$self->{'seq_set'}}){
195 if ($seq->id eq $properties[0]){
196 $site->attach_seq ($seq);
197 }
198 }
199 push @sites,$site;
200 }
201 foreach my $site(@sites){
202 push @matrix,$site->seq->seq;
203 }
204 my $patterngen=TFBS::PatternGen::SimplePFM->new(-seq_list=>\@matrix);
205 my $matrix=$patterngen->pattern->rawprint;
206 # print STDERR $matrix;
207 return ($matrix,\@sites,$e_value);
208 }
209
210
211 1;