| 1 | |
| 2 | # TFBS module for TFBS::PatternGen::Gibbs |
| 3 | # |
| 4 | # Copyright Boris Lenhard |
| 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::Gibbs - a pattern factory that uses Chip Lawrences Gibbs program |
| 14 | |
| 15 | =head1 SYNOPSIS |
| 16 | |
| 17 | my $patterngen = |
| 18 | TFBS::PatternGen::Gibbs->new(-seq_file=>'sequences.fa', |
| 19 | -binary => '/Programs/Gibbs-1.0/bin/Gibbs' |
| 20 | -nr_hits => 24, |
| 21 | -motif_length => [8, 9, 10], |
| 22 | -additional_params => '-x -r -e'); |
| 23 | |
| 24 | my $pfm = $patterngen->pattern(); # $pfm is now a TFBS::Matrix::PFM object |
| 25 | |
| 26 | =head1 DESCRIPTION |
| 27 | |
| 28 | TFBS::PatternGen::Gibbs builds position frequency matrices |
| 29 | using an advanced Gibbs sampling algorithm implemented in external |
| 30 | I<Gibbs> program by Chip Lawrence. The algorithm can produce |
| 31 | multiple patterns from a single set of sequences. |
| 32 | |
| 33 | =cut |
| 34 | |
| 35 | |
| 36 | |
| 37 | package TFBS::PatternGen::Gibbs; |
| 38 | use vars qw(@ISA); |
| 39 | use strict; |
| 40 | |
| 41 | |
| 42 | # Object preamble - inherits from TFBS::PatternGen; |
| 43 | |
| 44 | use TFBS::PatternGen; |
| 45 | use TFBS::PatternGen::Gibbs::Motif; |
| 46 | use File::Temp qw(:POSIX); |
| 47 | use Bio::Seq; |
| 48 | use Bio::SeqIO; |
| 49 | |
| 50 | @ISA = qw(TFBS::PatternGen); |
| 51 | |
| 52 | =head2 new |
| 53 | |
| 54 | Title : new |
| 55 | Usage : my $db = TFBS::PatternGen::Gibbs->new(%args); |
| 56 | Function: the constructor for the TFBS::PatternGen::Gibbs object |
| 57 | Returns : a TFBS::PatternGen::Gibbs object |
| 58 | Args : This method takes named arguments; |
| 59 | you must specify one of the following three |
| 60 | -seq_list # a reference to an array of strings |
| 61 | # and/or Bio::Seq objects |
| 62 | # or |
| 63 | -seq_stream # A Bio::SeqIO object |
| 64 | # or |
| 65 | -seq_file # the name of the fasta file containing |
| 66 | # all the sequences |
| 67 | Other arguments are: |
| 68 | -binary # a fully qualified path to Gibbs executable |
| 69 | # OPTIONAL: default 'Gibbs' |
| 70 | -nr_hits # a presumed number of pattern occurrences in the |
| 71 | # sequence set: it can be a single integer, e.g. |
| 72 | # -nr_hits => 24 , or a reference to an array of |
| 73 | # integers, e.g -nr_hits => [12, 24, 36] |
| 74 | -motif_length # an expected length of motif in nucleotides: |
| 75 | # it can be a single integer, e.g. |
| 76 | # -motif_length => 8 , or a reference to an |
| 77 | # array ofintegers, e.g -motif_length => [8..12] |
| 78 | -additional_params # a string containing additional |
| 79 | # command-line switches for the |
| 80 | # Gibbs program |
| 81 | |
| 82 | =cut |
| 83 | |
| 84 | sub new { |
| 85 | my ($caller, %args) = @_; |
| 86 | my $self = bless {}, ref($caller) || $caller; |
| 87 | $self->{'motif_length_string'} = |
| 88 | ($args{'-motif_length'} |
| 89 | ? (ref($args{'-motif_length'}) |
| 90 | ? join(',', @{$args{'-motif_length'}}) |
| 91 | : $args{'-motif_length'}) |
| 92 | : 8 ); |
| 93 | $self->{'nr_hits_string'} = |
| 94 | ($args{'-nr_hits'} |
| 95 | ? (ref($args{'-nr_hits'}) |
| 96 | ? join(',', @{$args{'-nr_hits'}}) |
| 97 | : $args{'-nr_hits'}) |
| 98 | : "" ); |
| 99 | $self->{'additional_params'} = |
| 100 | ($args{'-additional_params'} |
| 101 | ? (ref($args{'-additional_params'}) |
| 102 | ? join(' ', @{$args{'-additional_params'}}) |
| 103 | : $args{'-additional_params'}) |
| 104 | : "" ); |
| 105 | $self->{'binary'} = $args{'-binary'} || 'Gibbs'; |
| 106 | $self->{'motifs'} = []; |
| 107 | $self->_create_seq_set(%args) or die ('Error creating sequence set'); |
| 108 | #print $self->{'seq_set'}->[0]->seq; |
| 109 | #$self->_seq_props; |
| 110 | $self->_run_Gibbs() or $self->throw("Error running Gibbs."); |
| 111 | return $self; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | sub _run_Gibbs { |
| 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 | " -PBernoulli ". |
| 126 | $tmp_file." ". |
| 127 | $self->{'motif_length_string'}." ". |
| 128 | $self->{'nr_hits_string'}." ". |
| 129 | $self->{'additional_params'}." -n"; |
| 130 | |
| 131 | my $resultstring = `$command_line`; |
| 132 | $self->_parse_Gibbs_output($resultstring); |
| 133 | #print STDERR "$command_line\n"; |
| 134 | #print STDERR $resultstring; |
| 135 | |
| 136 | unlink $tmp_file; |
| 137 | return 1 |
| 138 | } |
| 139 | |
| 140 | =head2 pattern |
| 141 | |
| 142 | =head2 all_patterns |
| 143 | |
| 144 | =head2 patternSet |
| 145 | |
| 146 | The three methods listed above are used for the retrieval of patterns, |
| 147 | and are common to all TFBS::PatternGen::* classes. Please |
| 148 | see L<TFBS::PatternGen> for details. |
| 149 | |
| 150 | =cut |
| 151 | |
| 152 | sub _parse_Gibbs_output { |
| 153 | my ($self, $resultstring) = @_; |
| 154 | #print $resultstring; |
| 155 | #print"===========END_RESULTSTRING===============================\n"; |
| 156 | $resultstring =~ s/.*=== MAP MAXIMIZATION RESULTS ===//s; |
| 157 | my @raw_motifs = split /\-+\n\s+MOTIF \w\n/s, $resultstring; |
| 158 | shift @raw_motifs; # discard the first one |
| 159 | foreach my $raw_motif (@raw_motifs) { |
| 160 | #print $raw_motif; |
| 161 | my $motif =$self->_parse_raw_motif($raw_motif) || next; |
| 162 | push @{ $self->{'motifs'} }, $motif; |
| 163 | } |
| 164 | return 1; |
| 165 | } |
| 166 | |
| 167 | sub _site_props{ |
| 168 | my ($self,$raw_motif)=@_; |
| 169 | my @sites; |
| 170 | # print $raw_motif; |
| 171 | $raw_motif=~s/.*Num Motifs:\s+\d+\n//s; |
| 172 | # print $raw_motif; |
| 173 | #print "#####################################################\n"; |
| 174 | $raw_motif=~s/\n\s+\*+.*//s; |
| 175 | #print $raw_motif,"\n"; |
| 176 | my @site_lines=(split("\n", $raw_motif)); |
| 177 | foreach my $site(@site_lines){ |
| 178 | my $start_seq; |
| 179 | my $end_seq; |
| 180 | my ($seq_nr,$pattern_nr,$start,$seq,$end,$score,$strand,$desc)=$site=~/\s+(\d+),\s+(\d+)\s+(\d+)\s+([\w,\s]+)\s+(\d+)\s+(\d\.\d+)\s+([F,R])(.*)/; |
| 181 | # print $seq_nr,$pattern_nr,$start,$seq; |
| 182 | if ($strand eq "F"){ |
| 183 | $strand=1; |
| 184 | $start_seq=$start; |
| 185 | $end_seq=$end; |
| 186 | } |
| 187 | else{ |
| 188 | $strand=-1; |
| 189 | $start_seq=$end; |
| 190 | $end_seq=$start; |
| 191 | } |
| 192 | #print $site; |
| 193 | my $site = Bio::SeqFeature::Generic->new ( -start => $start_seq, |
| 194 | -end => $end_seq, |
| 195 | -strand => $strand, |
| 196 | -source => 'Gibbs sampler', |
| 197 | -score => $score, |
| 198 | ); |
| 199 | |
| 200 | $site->attach_seq ($self->{'seq_set'}->[$seq_nr-1]); |
| 201 | push (@sites,$site); |
| 202 | } |
| 203 | foreach my $site(@sites){ |
| 204 | #print $site->start."\n"; |
| 205 | } |
| 206 | |
| 207 | return \@sites; |
| 208 | } |
| 209 | |
| 210 | sub _parse_raw_motif { |
| 211 | # a utility function |
| 212 | my ($self,$raw_motif) = @_; |
| 213 | # print $raw_motif; |
| 214 | |
| 215 | my ($raw_matrix, $raw_bp, $length, $nr_hits, $MAP_score) = |
| 216 | $raw_motif =~ /Motif model \(residue frequency x 100\)\n(.+)Motif probability model\n.+Background probability model\n\s+(.+?)\n.+\D(\d+) columns\nNum Motifs\: (\d+).+Difference of Logs of Maps = ([\-\.\d]+)\n/s; |
| 217 | |
| 218 | #print $raw_matrix; |
| 219 | return undef unless $raw_matrix; |
| 220 | my $sites = $self->_site_props($raw_motif); |
| 221 | # print STDERR |
| 222 | # join ":", ($raw_matrix, $raw_bp, $length, $nr_hits); print "\n"; |
| 223 | my $matrix = _parse_raw_matrix($raw_matrix); |
| 224 | #print $matrix; |
| 225 | return |
| 226 | TFBS::PatternGen::Gibbs::Motif->new |
| 227 | #This object does not contain a new method. Instead the new method is than searched in the first ISA package. Remember that the object is still a TFBS::PatternGen::Gibbs::Motif. |
| 228 | #The only ISA in the package is TFBS::PatternGen::Motif.pm. This package indeed contains the new method |
| 229 | (-length => $length."", |
| 230 | -bg_probabilities => [split /\s+/, $raw_bp], |
| 231 | -MAP_score => $MAP_score, |
| 232 | -tags => {MAP_score => $MAP_score}, |
| 233 | -nr_hits => $nr_hits, |
| 234 | -sites=>$sites, |
| 235 | -matrix => $matrix ); |
| 236 | } |
| 237 | |
| 238 | sub _parse_raw_matrix { |
| 239 | # a utility function |
| 240 | my $raw_matrix = shift; |
| 241 | my @lines = split "\n", $raw_matrix; |
| 242 | my (@A, @C, @G, @T); |
| 243 | foreach my $line (@lines) { |
| 244 | my $value_string; |
| 245 | next unless ($value_string) = $line =~ /\s+\d+\s+\|\s+(.+)/; |
| 246 | $value_string =~ s/\./0/g; |
| 247 | my ($a, $t, $c, $g) = split /\s+/, $value_string; |
| 248 | push @A, $a; |
| 249 | push @C, $c; |
| 250 | push @G, $g; |
| 251 | push @T, $t; |
| 252 | } |
| 253 | # print STDERR join(" ",@A, "\n", @C, "\n", @G, "\n", @T, "\n"); |
| 254 | return [\@A, \@C, \@G, \@T]; |
| 255 | |
| 256 | } |
| 257 | |
| 258 | |
| 259 | 1; |