| 1 | |
| 2 | # TFBS module for TFBS::PatternGen::Elph |
| 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::Elph - a pattern factory that uses the Elph program |
| 14 | |
| 15 | =head1 SYNOPSIS |
| 16 | |
| 17 | my $patterngen = |
| 18 | TFBS::PatternGen::Elph->new(-seq_file=>'sequences.fa', |
| 19 | -binary => '/Elph/elph' |
| 20 | -motif_length => [8, 9, 10], |
| 21 | -additional_params => '-x -r -e'); |
| 22 | |
| 23 | my $pfm = $patterngen->pattern(); # $pfm is now a TFBS::Matrix::PFM object |
| 24 | |
| 25 | =head1 DESCRIPTION |
| 26 | |
| 27 | TFBS::PatternGen::Gibbs builds position frequency matrices |
| 28 | using an advanced Gibbs sampling algorithm implemented in external |
| 29 | I<Gibbs> program by Chip Lawrence. The algorithm can produce |
| 30 | multiple patterns from a single set of sequences. |
| 31 | |
| 32 | =cut |
| 33 | |
| 34 | |
| 35 | |
| 36 | package TFBS::PatternGen::Elph; |
| 37 | use vars qw(@ISA); |
| 38 | use strict; |
| 39 | |
| 40 | |
| 41 | # Object preamble - inherits from TFBS::PatternGen; |
| 42 | |
| 43 | use TFBS::PatternGen; |
| 44 | use TFBS::PatternGen::Elph::Motif; |
| 45 | use File::Temp qw(:POSIX); |
| 46 | use Bio::Seq; |
| 47 | use Bio::SeqIO; |
| 48 | |
| 49 | @ISA = qw(TFBS::PatternGen); |
| 50 | |
| 51 | =head2 new |
| 52 | |
| 53 | Title : new |
| 54 | Usage : my $db = TFBS::PatternGen::Gibbs->new(%args); |
| 55 | Function: the constructor for the TFBS::PatternGen::Gibbs object |
| 56 | Returns : a TFBS::PatternGen::Gibbs object |
| 57 | Args : This method takes named arguments; |
| 58 | you must specify one of the following three |
| 59 | -seq_list # a reference to an array of strings |
| 60 | # and/or Bio::Seq objects |
| 61 | # or |
| 62 | -seq_stream # A Bio::SeqIO object |
| 63 | # or |
| 64 | -seq_file # the name of the fasta file containing |
| 65 | # all the sequences |
| 66 | Other arguments are: |
| 67 | -binary # a fully qualified path to Gibbs executable |
| 68 | # OPTIONAL: default 'Gibbs' |
| 69 | -nr_hits # a presumed number of pattern occurrences in the |
| 70 | # sequence set: it can be a single integer, e.g. |
| 71 | # -nr_hits => 24 , or a reference to an array of |
| 72 | # integers, e.g -nr_hits => [12, 24, 36] |
| 73 | -motif_length # an expected length of motif in nucleotides: |
| 74 | # it can be a single integer, e.g. |
| 75 | # -motif_length => 8 , or a reference to an |
| 76 | # array ofintegers, e.g -motif_length => [8..12] |
| 77 | -additional_params # a string containing additional |
| 78 | # command-line switches for the |
| 79 | # Gibbs program |
| 80 | |
| 81 | =cut |
| 82 | |
| 83 | sub new { |
| 84 | my ($caller, %args) = @_; |
| 85 | my $self = bless {}, ref($caller) || $caller; |
| 86 | $self->{'motif_length_string'} = |
| 87 | ($args{'-motif_length'} |
| 88 | ? (ref($args{'-motif_length'}) |
| 89 | ? join(',', @{$args{'-motif_length'}}) |
| 90 | : $args{'-motif_length'}) |
| 91 | : 8 ); |
| 92 | $self->{'additional_params'} = |
| 93 | ($args{'-additional_params'} |
| 94 | ? (ref($args{'-additional_params'}) |
| 95 | ? join(' ', @{$args{'-additional_params'}}) |
| 96 | : $args{'-additional_params'}) |
| 97 | : "" ); |
| 98 | $self->{'binary'} = $args{'-binary'} || 'elph'; |
| 99 | |
| 100 | $self->{'motifs'} = []; |
| 101 | $self->_create_seq_set(%args) or die ('Error creating sequence set'); |
| 102 | |
| 103 | $self->_run_elph() or $self->throw("Error running elph."); |
| 104 | return $self; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | sub _run_elph { |
| 109 | my $self = shift; |
| 110 | my $tmp_file = tmpnam(); |
| 111 | my $outstream = Bio::SeqIO->new(-file=>">$tmp_file", -format=>"fasta"); |
| 112 | foreach my $seqobj (@{ $self->{'seq_set'} } ) { |
| 113 | $outstream->write_seq($seqobj); |
| 114 | } |
| 115 | $outstream->close(); |
| 116 | $self->{'additional_params'}=~s/-b//; |
| 117 | #This removes a -b switch. This enables long output containgin info about the sites |
| 118 | |
| 119 | my $command_line = |
| 120 | $self->{'binary'}." ". |
| 121 | $tmp_file." ". |
| 122 | "LEN=".$self->{'motif_length_string'}." ". |
| 123 | $self->{'additional_params'}." 2>/dev/null"; |
| 124 | |
| 125 | my $resultstring = `$command_line`; |
| 126 | $self->_parse_elph_output($resultstring,$command_line); |
| 127 | #print STDERR "$command_line\n"; |
| 128 | #print STDERR $resultstring; |
| 129 | |
| 130 | # unlink $tmp_file; |
| 131 | return 1 |
| 132 | } |
| 133 | |
| 134 | =head2 pattern |
| 135 | |
| 136 | =head2 all_patterns |
| 137 | |
| 138 | =head2 patternSet |
| 139 | |
| 140 | The three methods listed above are used for the retrieval of patterns, |
| 141 | and are common to all TFBS::PatternGen::* classes. Please |
| 142 | see L<TFBS::PatternGen> for details. |
| 143 | |
| 144 | =cut |
| 145 | |
| 146 | sub _parse_elph_output { |
| 147 | my ($self, $resultstring,$command_line) = @_; |
| 148 | #print $resultstring; |
| 149 | if ($resultstring=~/^error/){ |
| 150 | $self->throw ("Error running elp command:\n $command_line"); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | #Motif after optimizing |
| 156 | #MAP for motif: 46.735 InfoPar=0.098 |
| 157 | # |
| 158 | #Motif found: |
| 159 | # |
| 160 | #Background probability model: |
| 161 | # a c g t |
| 162 | # 0.30 0.20 0.19 0.31 |
| 163 | # |
| 164 | #Background counts: |
| 165 | #a: 1456 |
| 166 | #c: 948 |
| 167 | #g: 909 |
| 168 | #t: 1487 |
| 169 | # |
| 170 | # |
| 171 | #Motif probability model: |
| 172 | #Pos: 1 2 3 4 5 6 |
| 173 | #a 1.00 0.00 1.00 0.83 0.00 0.00 |
| 174 | #c 0.00 0.00 0.00 0.00 0.00 0.17 |
| 175 | #g 0.00 1.00 0.00 0.17 1.00 0.83 |
| 176 | #t 0.00 0.00 0.00 0.00 0.00 0.00 |
| 177 | #------------------------------------------ |
| 178 | #Info 1.73 2.42 1.73 1.19 2.42 1.75 |
| 179 | # |
| 180 | #Motif counts: |
| 181 | #a: 6 0 6 5 0 0 |
| 182 | #c: 0 0 0 0 0 1 |
| 183 | #g: 0 6 0 1 6 5 |
| 184 | #t: 0 0 0 0 0 0 |
| 185 | # |
| 186 | # |
| 187 | (my $MAP)=$resultstring=~/MAP for motif: (.*) InfoPar=/; |
| 188 | ($resultstring)=~s/.*Motif counts:\n//s; |
| 189 | |
| 190 | #print STDERR $resultstring; |
| 191 | my @array=split "\n",$resultstring; |
| 192 | my @matrix; |
| 193 | #print $array[0],"\n"; |
| 194 | foreach (0..3){ |
| 195 | my (@line)=split(/\s+/,$array[$_]); |
| 196 | #print "@line\n"; |
| 197 | shift @line; |
| 198 | push @matrix,\@line; |
| 199 | # print "@line\n"; |
| 200 | } |
| 201 | # print @matrix; |
| 202 | #print $resultstring; |
| 203 | my $sites=$self->_site_props($resultstring); |
| 204 | my $motif =TFBS::PatternGen::Elph::Motif->new |
| 205 | ( |
| 206 | -tags => {score=>$MAP},#The score in this case is the E-value given in the output |
| 207 | -sites=>$sites, |
| 208 | -matrix => \@matrix |
| 209 | ); |
| 210 | # Seq.no Pos ***** Motif ***** Prob D Seq.Id |
| 211 | # 1 354 ggatt AGAAGC cgccg 0.1389 -1 GAL1 |
| 212 | # 2 636 caaag AGAAGG ttttt 0.6942 -1 GAL10 |
| 213 | # 3 456 aaggc AGAAGG cagta 0.6942 -1 GAL2 |
| 214 | # 4 444 aaagt AGAGGG ggtaa 0.1388 -1 GAL7 |
| 215 | # 5 324 tagag AGAAGG agcaa 0.6942 -1 GAL80 |
| 216 | # 6 165 gttac AGAAGG gccgc 0.6942 -1 GCY1 |
| 217 | #$resultstring =~ s/.*=== MAP MAXIMIZATION RESULTS ===//s; |
| 218 | #my @raw_motifs = split /\-+\n\s+MOTIF \w\n/s, $resultstring; |
| 219 | #shift @raw_motifs; # discard the first one |
| 220 | #foreach my $raw_motif (@raw_motifs) { |
| 221 | # #print $raw_motif; |
| 222 | # my $motif =$self->_parse_raw_motif($raw_motif) || next; |
| 223 | push @{ $self->{'motifs'} }, $motif; |
| 224 | #} |
| 225 | #return 1; |
| 226 | } |
| 227 | |
| 228 | sub _site_props{ |
| 229 | my ($self,$resultstring)=@_; |
| 230 | my @sites; |
| 231 | # print $resultstring; |
| 232 | #($resultstring)=~s/.*Motif counts:\n//s; |
| 233 | my @array=split(/Seq\.no/,$resultstring); |
| 234 | #print $array[1]; |
| 235 | my @sites_array=split "\n", $array[1]; |
| 236 | foreach my $line(@sites_array){ |
| 237 | # print $line; |
| 238 | next if $line=~/Pos/; |
| 239 | last if $line eq''; |
| 240 | my @site=split(/\s+/,$line); |
| 241 | # print $site[1],"\n"; |
| 242 | my $nr=0; |
| 243 | $nr = 1 if $site[2]==1; |
| 244 | #A special case when the site startsat the first base. |
| 245 | #Then no preceding quence is given and the site array =shorter by 1 |
| 246 | my $motif_seq=$site[4-$nr]; |
| 247 | # print $motif_seq,"\n"; |
| 248 | my $site = Bio::SeqFeature::Generic->new ( -start => $site[2], |
| 249 | -end => $site[2]+(length$motif_seq)-1, |
| 250 | -strand => 1, |
| 251 | #Always 1 with elph |
| 252 | -source => 'Elph', |
| 253 | -score => $site[-3], |
| 254 | ); |
| 255 | foreach my $seq(@{$self->{'seq_set'}}){ |
| 256 | if ($seq->id eq $site[-1]){#last element of the array |
| 257 | $site->attach_seq ($seq); |
| 258 | } |
| 259 | } |
| 260 | push (@sites,$site); |
| 261 | } |
| 262 | |
| 263 | return \@sites; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | 1; |