| 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 (version 2.1) |
| 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'} || 'annspec'; |
| 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 | "-f ".$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 "$resultstring\n"; |
| 127 | # open(TEST, "test.out"); # this sentense is the one I add |
| 128 | # my @lines = <TEST>;# this sentense is the one I add |
| 129 | # my $resultstring = join '', @lines;# this sentense is the one I add |
| 130 | # print STDERR $resultstring; |
| 131 | $self->_parse_AnnSpec_output($resultstring,$command_line); |
| 132 | unlink $tmp_file; |
| 133 | return 1 |
| 134 | } |
| 135 | |
| 136 | sub _parse_AnnSpec_output{ |
| 137 | my ($self,$resultstring,$command_line)=@_; |
| 138 | if ($resultstring eq''){ |
| 139 | # warn "Error running AnnSpec\nNo patterns produced"; |
| 140 | $self->throw ("Error running AnnSpec using command:\n $command_line"); |
| 141 | return; |
| 142 | } |
| 143 | my ($consensus,$matrix)=$self->_parse_raw_matrix($resultstring); |
| 144 | my ($score,$sites)=$self->_parse_sites($resultstring); |
| 145 | |
| 146 | for(my $x = 0; $x < scalar(@$consensus); $x++){ |
| 147 | my $motif =TFBS::PatternGen::AnnSpec::Motif->new |
| 148 | ( |
| 149 | #-length => $length."", |
| 150 | # -bg_probabilities => [split /\s+/, $raw_bp], |
| 151 | -tags => {consensus => $consensus->[$x], |
| 152 | score=>$score->[$x]}, |
| 153 | |
| 154 | -nr_hits => 1, |
| 155 | -sites=>$sites->[$x], |
| 156 | -matrix => $matrix->[$x] |
| 157 | ); |
| 158 | push @{ $self->{'motifs'} }, $motif; |
| 159 | } |
| 160 | return |
| 161 | } |
| 162 | |
| 163 | sub _parse_sites{ |
| 164 | my ($self,$string)=@_; |
| 165 | |
| 166 | my (@hits, @scores); |
| 167 | |
| 168 | foreach my $substring (split /REPORTING/, $string ){ |
| 169 | my @sub_hits; |
| 170 | my ($sites)=$substring=~/STR\s+n.*seq\n(.*)RUN\s+ALIGNMENT.*/s; |
| 171 | my ($average)=$substring=~/RUN INFORMATION_CONTENT\s+(\d*\.*\d*)/; |
| 172 | my ($score)=$substring=~/RUN\s+SCORE\s+(\d*\.*\d*)/; |
| 173 | |
| 174 | if($sites){ |
| 175 | my @sites=split/\n/,$sites; |
| 176 | |
| 177 | foreach my $site (@sites){ |
| 178 | my @site_array=split(/\s+/,$site); |
| 179 | my ($seq_id)=$site_array[6]=~/>(.*)/; |
| 180 | my $strand=1; |
| 181 | $strand=-1 if $site_array[3]=~/\'/;#MEans we have a pattern in the reverse strand |
| 182 | my ($start)=$site_array[3]=~/(\d+)/; |
| 183 | my $site = Bio::SeqFeature::Generic->new ( -start => $start, |
| 184 | -end => $start+(length$site_array[4])-1, |
| 185 | -strand => $strand, |
| 186 | -source => 'AnnSpec', |
| 187 | -score => $site_array[2], |
| 188 | ); |
| 189 | |
| 190 | foreach my $seq(@{$self->{'seq_set'}}){ |
| 191 | if ($seq->id eq $seq_id){ |
| 192 | $site->attach_seq ($seq); |
| 193 | } |
| 194 | } |
| 195 | push (@sub_hits,$site); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | push @scores, $score; |
| 200 | push @hits, \@sub_hits; |
| 201 | } |
| 202 | |
| 203 | } |
| 204 | return \@scores,\@hits; |
| 205 | |
| 206 | } |
| 207 | |
| 208 | sub _parse_raw_matrix{ |
| 209 | my ($self,$string)=@_; |
| 210 | |
| 211 | my (@pfms, @consensus); |
| 212 | foreach my $sub_string (split /REPORTING/, $string){ |
| 213 | my ($ma)=$sub_string=~/RUN\s+WEIGHTS_CONS.*ALR\s+\/.*ALR\s+\#.*(ALR.*\nALR.*\nALR.*\nALR.*\s+\d+\n)ALR\s+=+.*/s; |
| 214 | my ($con)=$sub_string=~/WEIGHTS_CONS\s+(.*)\n/; |
| 215 | |
| 216 | if($ma){ |
| 217 | my @matrix=split("\n",$ma); |
| 218 | my @pfm; |
| 219 | foreach my $row(@matrix){ |
| 220 | # print $row; |
| 221 | my @row=split /\s+/, $row; |
| 222 | push @pfm, [@row[2..scalar@row-1]]; |
| 223 | } |
| 224 | push @pfms, \@pfm; |
| 225 | push @consensus, $con; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | return \@consensus, \@pfms; |
| 230 | |
| 231 | } |
| 232 | |
| 233 | |
| 234 | 1; |