Debian Codegraph @

blib/lib/TFBS/PatternGen/AnnSpec.pm

raw
1
2 # TFBS module for TFBS::PatternGen::AnnSpec
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::AnnSpec - a pattern factory that uses the AnnSpec program
14
15 =head1 SYNOPSIS
16
17 my $patterngen =
18 TFBS::PatternGen::AnnSpec->new(-seq_file=>'sequences.fa',
19 -binary => 'ann-spec '
20
21
22 my $pfm = $patterngen->pattern(); # $pfm is now a TFBS::Matrix::PFM object
23
24
25 =head1 DESCRIPTION
26
27 TFBS::PatternGen::AnnSpec builds position frequency matrices
28 using an external program AnnSpec (Workman, C. and Stormo, G.D. (2000) ANN-Spec: A method for discovering transcription factor binding sites with improved specificity. Proc. Pacific Symposium on Biocomputing 2000).
29
30 =head1 FEEDBACK
31
32 Please send bug reports and other comments to the author.
33
34 =head1 AUTHOR - Wynand Alkema
35
36 Wynand Alkema E<lt>[email protected]<gt>
37
38 =cut
39
40 package TFBS::PatternGen::AnnSpec;
41 use vars qw(@ISA);
42 use strict;
43
44
45 # Object preamble - inherits from TFBS::PatternGen;
46
47 use TFBS::PatternGen;
48 use TFBS::PatternGen::AnnSpec::Motif;
49 use File::Temp qw(:POSIX);
50 use Bio::Seq;
51 use Bio::SeqIO;
52
53 @ISA = qw(TFBS::PatternGen);
54
55 =head2 new
56
57 Title : new
58 Usage : my $pattrengen = TFBS::PatternGen::AnnSpec->new(%args);
59 Function: the constructor for the TFBS::PatternGen::AnnSpec object
60 Returns : a TFBS::PatternGen::AnnSpec object
61 Args : This method takes named arguments;
62 you must specify one of the following three
63 -seq_list # a reference to an array of strings
64 # and/or Bio::Seq objects
65 # or
66 -seq_stream # A Bio::SeqIO object
67 # or
68 -seq_file # the name of the fasta file containing
69 # all the sequences
70 Other arguments are:
71 -binary # a fully qualified path to the 'meme' executable
72 # OPTIONAL: default 'ann-spec'
73 -additional_params # a string containing additional
74 # command-line switches for the
75 # ann-spec program
76
77 =cut
78
79 sub new {
80 my ($caller, %args) = @_;
81 my $self = bless {}, ref($caller) || $caller;
82 $self->{'filename'} =$args{'-seq_file'};
83
84 $self->{'additional_params'} =
85 ($args{'-additional_params'}
86 ? (ref($args{'-additional_params'})
87 ? join(' ', @{$args{'-additional_params'}})
88 : $args{'-additional_params'})
89 : "" );
90 $self->{'binary'} = $args{'-binary'} || 'ann-spec';
91 $self->_create_seq_set(%args) or die ('Error creating sequence set');
92 $self->_run_AnnSpec() or $self->throw("Error running AnnSpec.");
93 return $self;
94 }
95
96
97 =head2 pattern
98
99 =head2 all_patterns
100
101 =head2 patternSet
102
103 The three methods listed above are used for the retrieval of patterns,
104 and are common to all TFBS::PatternGen::* classes. Please
105 see L<TFBS::PatternGen> for details.
106
107 =cut
108
109 sub _run_AnnSpec{
110 my ($self)=shift;
111 my $tmp_file = tmpnam();
112 my $outstream = Bio::SeqIO->new(-file=>">$tmp_file", -format=>"fasta");
113 foreach my $seqobj (@{ $self->{'seq_set'} } ) {
114 $outstream->write_seq($seqobj);
115 }
116 $outstream->close();
117 my $command_line =
118 $self->{'binary'}." ".
119 "-p ".$tmp_file." ".
120 # $self->{'motif_length_string'}." ".
121 # $self->{'nr_hits_string'}." ".
122 $self->{'additional_params'}.
123 "";
124 # print STDERR "$command_line\n";
125 my $resultstring = `$command_line`;
126 # print STDERR $resultstring;
127 $self->_parse_AnnSpec_output($resultstring,$command_line);
128 unlink $tmp_file;
129 return 1
130 }
131
132 sub _parse_AnnSpec_output{
133 my ($self,$resultstring,$command_line)=@_;
134 if ($resultstring eq''){
135 # warn "Error running AnnSpec\nNo patterns produced";
136 $self->throw ("Error running AnnSpec using command:\n $command_line");
137 return;
138 }
139 my ($consensus,$matrix)=$self->_parse_raw_matrix($resultstring);
140 my ($score,$sites)=$self->_parse_sites($resultstring);
141 my $motif =TFBS::PatternGen::AnnSpec::Motif->new
142 (
143 #-length => $length."",
144 # -bg_probabilities => [split /\s+/, $raw_bp],
145 -tags => {consensus => $consensus,
146 score=>$score},
147
148 -nr_hits => 1,
149 -sites=>$sites,
150 -matrix => $matrix
151 );
152 push @{ $self->{'motifs'} }, $motif;
153 return
154 }
155
156 sub _parse_sites{
157 my ($self,$string)=@_;
158 # print $raw_motif;
159 my @hits;
160 my ($sites)=$string=~/STR BEST_SITES\n(.*)STR ave\(S\)/s;
161 my ($average)=$string=~/STR ave\(S\)\s+(\d*\.*\d*)/;
162 my ($score)=$string=~/STR ln\(ave\(sum\(exp\(S\)\)\)\)\s+(\d*\.*\d*)/;
163 # print STDERR $score,"\n";
164 my @sites=split/\n/,$sites;
165 shift @sites;
166 # print "@sites\n";
167 foreach my $site (@sites){
168 my @site_array=split(/\s+/,$site);
169 # print "$site_array[3]\n";
170 # print "$site_array[5]\n";
171 my ($seq_id)=$site_array[5]=~/>(.*)/;
172 my $strand=1;
173 $strand=-1 if $site_array[3]=~/\'/;#MEans we have a pattern in the reverse strand
174 my ($start)=$site_array[3]=~/(\d+)/;
175 my $site = Bio::SeqFeature::Generic->new ( -start => $start,
176 -end => $start+(length$site_array[4])-1,
177 -strand => $strand,
178 -source => 'AnnSpec',
179 -score => $site_array[2],
180 );
181 #
182 foreach my $seq(@{$self->{'seq_set'}}){
183 if ($seq->id eq $seq_id){
184 $site->attach_seq ($seq);
185 }
186 }
187 push (@hits,$site);
188 }
189 return $score,\@hits;
190
191 }
192
193 sub _parse_raw_matrix{
194 my ($self,$string)=@_;
195 my ($matrix)=$string=~/ALR ALIGNMENT_MATRIX.*ALR\s+-+(.*)ALR CONSENSUS/s;
196 my ($consensus)=$string=~/ALR CONSENSUS (.*)\n/;
197
198 #print $consensus;
199
200 my @matrix=split("\n",$matrix);
201 shift @matrix;
202 my @pfm;
203 foreach my $row(@matrix){
204 # print $row;
205 my @row=split /\s+/, $row;
206 push @pfm, [@row[2..scalar@row-1]];
207 }
208 return $consensus, \@pfm;
209
210 }
211
212
213 1;