| 1 | # TFBS module for TFBS::MatrixSet |
| 2 | # |
| 3 | # Copyright Boris Lenhard |
| 4 | # |
| 5 | # You may distribute this module under the same terms as perl itself |
| 6 | # |
| 7 | |
| 8 | # POD |
| 9 | |
| 10 | =head1 NAME |
| 11 | |
| 12 | TFBS::Matrix::Set - an agregate class representing a set of matrix patterns, containing methods for manipulating the set as a whole |
| 13 | |
| 14 | =head1 SYNOPSIS |
| 15 | |
| 16 | # creation of a TFBS::MatrixSet object |
| 17 | # let @list_of_matrix_objects be a list of TFBS::Matrix::* objects |
| 18 | |
| 19 | ################################### |
| 20 | # Create a TFBS::MatrixSet object: |
| 21 | |
| 22 | my $matrixset = TFBS::MatrixSet->new(); # creates an empty set |
| 23 | $matrixset->add_Matrix(@list_of_matrix_objects); #add matrix objects to set |
| 24 | $matrixset->add_Matrix($matrixobj); # adds a single matrix object to set |
| 25 | |
| 26 | # or, same as above: |
| 27 | |
| 28 | my $matrixset = TFBS::MatrixSet->new(@list_of_matrix_objects, $matrixobj); |
| 29 | |
| 30 | ################################### |
| 31 | # |
| 32 | |
| 33 | |
| 34 | =head1 DESCRIPTION |
| 35 | |
| 36 | TFBS::MatrixSet is an aggregate class storing a set of TFBS::Matrix::* subclass objects, and providing methods form manipulating those sets as a whole. TFBS::MatrixSet objects are created <I>de novo<I> or returned by some database (TFBS::DB::*) retrieval methods. |
| 37 | |
| 38 | =head1 FEEDBACK |
| 39 | |
| 40 | Please send bug reports and other comments to the author. |
| 41 | |
| 42 | =head1 AUTHOR - Boris Lenhard |
| 43 | |
| 44 | Boris Lenhard E<lt>[email protected]<gt> |
| 45 | |
| 46 | =head1 APPENDIX |
| 47 | |
| 48 | The rest of the documentation details each of the object |
| 49 | methods. Internal methods are preceded with an underscore. |
| 50 | |
| 51 | =cut |
| 52 | |
| 53 | |
| 54 | |
| 55 | # The code begins HERE: |
| 56 | |
| 57 | package TFBS::MatrixSet; |
| 58 | use vars '@ISA'; |
| 59 | |
| 60 | use PDL; |
| 61 | use Bio::Seq; |
| 62 | use Bio::SeqIO; |
| 63 | use Bio::Root::Root; |
| 64 | use File::Temp qw/:POSIX/; |
| 65 | |
| 66 | use TFBS::Matrix; |
| 67 | use TFBS::_Iterator::_MatrixSetIterator; |
| 68 | use TFBS::SiteSet; |
| 69 | |
| 70 | use strict; |
| 71 | |
| 72 | @ISA = qw(Bio::Root::Root); |
| 73 | |
| 74 | |
| 75 | =head2 new |
| 76 | |
| 77 | =cut |
| 78 | |
| 79 | |
| 80 | sub new { |
| 81 | my ($caller, @matrices) = shift; |
| 82 | my $self = bless {matrix_list =>[]}, ref($caller) || $caller; |
| 83 | $self->add_matrix(@matrices) if @matrices; |
| 84 | return $self; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | =head2 add_matrix |
| 91 | |
| 92 | Title : add_matrix |
| 93 | Usage : $matrixset->add_matrix(@list_of_matrix_objects); |
| 94 | Function: Adds matrix objects to matrixset |
| 95 | Returns : object reference (usually ignored) |
| 96 | Args : one or more TFBS::Matrix::* objects |
| 97 | |
| 98 | =cut |
| 99 | |
| 100 | sub add_matrix { |
| 101 | my ($self, @matrices) = @_; |
| 102 | foreach my $matrix (@matrices) { |
| 103 | $self->throw("Argument to add_matrix_set not a TFBS::Matrix object") |
| 104 | unless $matrix->isa("TFBS::Matrix"); |
| 105 | } |
| 106 | push @{$self->{matrix_list}}, @matrices; |
| 107 | return $self; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | |
| 112 | =head2 add_matrix_set |
| 113 | |
| 114 | Title : add_matrix |
| 115 | Usage : $matrixset->add_matrix(@list_of_matrixset_objects); |
| 116 | Function: Adds to the matrixset matrix objects contained in one or |
| 117 | more other matrixsets |
| 118 | Returns : object reference (usually ignored) |
| 119 | Args : one or more TFBS::MatrixSet objects |
| 120 | |
| 121 | =cut |
| 122 | |
| 123 | |
| 124 | sub add_matrix_set { |
| 125 | my ($self, @sets) = @_; |
| 126 | foreach my $matrixset (@sets) { |
| 127 | $self->throw("Argument to add_matrix_set not a TFBS::Matrixset object") |
| 128 | unless $matrixset->isa("TFBS::MatrixSet"); |
| 129 | push @{$self->{matrix_list}}, @{$matrixset->{matrix_list}}; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | sub reset { |
| 134 | my ($self) = @_; |
| 135 | $self->warn("reset: Deprecated method use Iterator instead."); |
| 136 | @{$self->{_iterator_list}} = @{$self->{matrix_list}}; |
| 137 | } |
| 138 | |
| 139 | sub sort_by_name { |
| 140 | my ($self) = @_; |
| 141 | $self->warn("sort_by_name: Deprecated method use Iterator instead."); |
| 142 | @{$self->{matrix_list}} = sort { uc($a->{name}) cmp uc ($b->{name}) } |
| 143 | @{$self->{matrix_list}}; |
| 144 | $self->reset(); |
| 145 | } |
| 146 | |
| 147 | sub next { |
| 148 | my ($self) = @_; |
| 149 | $self->warn("next: Deprecated method use Iterator instead."); |
| 150 | if (my $next_matrix = shift (@{$self->{_iterator_list}})) { |
| 151 | return $next_matrix; |
| 152 | } |
| 153 | else { |
| 154 | $self->reset; |
| 155 | return undef; |
| 156 | } |
| 157 | } |
| 158 | =head2 search_seq |
| 159 | |
| 160 | Title : search_seq |
| 161 | Usage : my $siteset = $matrixset->search_seq(%args) |
| 162 | Function: scans a nucleotide sequence with all patterns represented |
| 163 | stored in $matrixset; |
| 164 | |
| 165 | It works only if all matrix objects in $matrixset understand |
| 166 | search_seq method (currently only TFBS::Matrix::PWM objects do) |
| 167 | Returns : a TFBS::SiteSet object |
| 168 | Args : # you must specify either one of the following three: |
| 169 | |
| 170 | -file, # the name od a fasta file (single sequence) |
| 171 | #or |
| 172 | -seqobj # a Bio::Seq object |
| 173 | # (more accurately, a Bio::PrimarySeqobject or a |
| 174 | # subclass thereof) |
| 175 | #or |
| 176 | -seqstring # a string containing the sequence |
| 177 | |
| 178 | -threshold, # minimum score for the hit, either absolute |
| 179 | # (e.g. 11.2) or relative (e.g. "75%") |
| 180 | # OPTIONAL: default "80%" |
| 181 | |
| 182 | =cut |
| 183 | |
| 184 | |
| 185 | |
| 186 | sub search_seq { |
| 187 | my ($self, %args) = @_; |
| 188 | $self->_search(%args); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | =head2 search_aln |
| 193 | |
| 194 | Title : search_aln |
| 195 | Usage : my $site_pair_set = $matrixset->search_aln(%args) |
| 196 | Function: Scans a pairwise alignment of nucleotide sequences |
| 197 | with the pattern represented by the PWM: it reports only |
| 198 | those hits that are present in equivalent positions of both |
| 199 | sequences and exceed a specified threshold score in both, AND |
| 200 | are found in regions of the alignment above the specified |
| 201 | conservation cutoff value. |
| 202 | It works only if all matrix object in $matrixset understand |
| 203 | search_aln method (currently only TFBS::Matrix::PWM objects do) |
| 204 | |
| 205 | Returns : a TFBS::SitePairSet object |
| 206 | Args : # you must specify either one of the following three: |
| 207 | |
| 208 | -file, # the name of the alignment file in Clustal |
| 209 | format |
| 210 | #or |
| 211 | -alignobj # a Bio::SimpleAlign object |
| 212 | # (more accurately, a Bio::PrimarySeqobject or a |
| 213 | # subclass thereof) |
| 214 | #or |
| 215 | -alignstring # a multi-line string containing the alignment |
| 216 | # in clustal format |
| 217 | ############# |
| 218 | |
| 219 | -threshold, # minimum score for the hit, either absolute |
| 220 | # (e.g. 11.2) or relative (e.g. "75%") |
| 221 | # OPTIONAL: default "80%" |
| 222 | |
| 223 | -window, # size of the sliding window (inn nucleotides) |
| 224 | # for calculating local conservation in the |
| 225 | # alignment |
| 226 | # OPTIONAL: default 50 |
| 227 | |
| 228 | -cutoff # conservation cutoff (%) for including the |
| 229 | # region in the results of the pattern search |
| 230 | # OPTIONAL: default "70%" |
| 231 | |
| 232 | -subpart # subpart of the alignment to search, given as e.g. |
| 233 | # -subpart => { relative_to => 1, |
| 234 | # start => 140, |
| 235 | # end => 180 } |
| 236 | # where start and end are coordinates in the |
| 237 | # sequence indicated by relative_to (1 for the |
| 238 | # 1st sequence in the alignment, 2 for the 2nd) |
| 239 | # OPTIONAL: by default searches entire alignment |
| 240 | |
| 241 | -conservation |
| 242 | # conservation profile, a TFBS::ConservationProfile |
| 243 | # OPTIONAL: by default the conservation profile is |
| 244 | # computed internally on the fly (less efficient) |
| 245 | |
| 246 | =cut |
| 247 | |
| 248 | sub search_aln { |
| 249 | my ($self, %args) = @_; |
| 250 | my $mxit = $self->Iterator(); |
| 251 | my $sitepairset = TFBS::SitePairSet->new; |
| 252 | my $aln = TFBS::Matrix::_Alignment->new(%args); |
| 253 | while (my $mx = $mxit->next) { |
| 254 | my $singleset = $mx->search_aln(%args, |
| 255 | -alignment_setup => $aln); |
| 256 | $sitepairset->add_site_pair_set($singleset); |
| 257 | } |
| 258 | return $sitepairset; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | |
| 263 | =head2 size |
| 264 | |
| 265 | Title : size |
| 266 | Usage : my $number_of_matrices = $matrixset->size; |
| 267 | Function: gets the number of matrix objects in the $matrixset |
| 268 | (i.e. the size of the set) |
| 269 | Returns : a number |
| 270 | Args : none |
| 271 | |
| 272 | =cut |
| 273 | |
| 274 | |
| 275 | sub size { |
| 276 | scalar @{ $_[0]->{matrix_list} }; |
| 277 | } |
| 278 | |
| 279 | |
| 280 | =head2 Iterator |
| 281 | |
| 282 | Title : Iterator |
| 283 | Usage : my $matrixset_iterator = |
| 284 | $matrixset->Iterator(-sort_by =>'total_ic'); |
| 285 | while (my $matrix_object = $matrix_iterator->next) { |
| 286 | # do whatever you want with individual matrix objects |
| 287 | } |
| 288 | Function: Returns an iterator object that can be used to go through |
| 289 | all members of the set |
| 290 | Returns : an iterator object (currently undocumentened in TFBS - |
| 291 | but understands the 'next' method) |
| 292 | Args : -sort_by # optional - currently it accepts |
| 293 | # 'ID' (alphabetically) |
| 294 | # 'name' (alphabetically) |
| 295 | # 'class' (alphabetically) |
| 296 | # 'total_ic' (numerically, decreasing order) |
| 297 | |
| 298 | -reverse # optional - reverses the default sorting order if true |
| 299 | |
| 300 | =cut |
| 301 | |
| 302 | |
| 303 | sub Iterator { |
| 304 | |
| 305 | my ($self, %args) = @_; |
| 306 | return TFBS::_Iterator::_MatrixSetIterator->new($self->{matrix_list}, |
| 307 | $args{'-sort_by'}, |
| 308 | $args{'-reverse'} |
| 309 | ); |
| 310 | } |
| 311 | |
| 312 | |
| 313 | sub _search { |
| 314 | |
| 315 | my ($self, %args) = @_; |
| 316 | |
| 317 | # DIRTY - stick tmp file name to seq object |
| 318 | |
| 319 | my $seqobj = $self->_to_seqobj(%args); |
| 320 | ($seqobj->{_fastaFH}, $seqobj->{_fastafile}) = tmpnam(); |
| 321 | # we need $fastafile below |
| 322 | |
| 323 | my $outstream = Bio::SeqIO->new(-file=>">".$seqobj->{_fastafile}, -format=>"Fasta"); |
| 324 | my $subseqobj; |
| 325 | if(my $subpart = $args{-subpart}) { |
| 326 | my $subseq_start = $subpart->{-start}; |
| 327 | my $subseq_end = $subpart->{-end}; |
| 328 | unless($subseq_start and $subseq_end) { |
| 329 | $self->throw("Option -subpart missing suboption -relative_to, -start or -end"); |
| 330 | } |
| 331 | $subseqobj = Bio::Seq->new(-seq => $seqobj->subseq($subseq_start, $subseq_end), |
| 332 | -id => $seqobj->id); |
| 333 | } |
| 334 | $outstream->write_seq($subseqobj or $seqobj); |
| 335 | $outstream->close; |
| 336 | |
| 337 | # iterate through pwms |
| 338 | my @PWMs; |
| 339 | my $mxit = $self->Iterator(); |
| 340 | |
| 341 | while (my $pwm = $mxit->next() ) { |
| 342 | push @PWMs,$pwm; |
| 343 | } |
| 344 | |
| 345 | # do the analysis |
| 346 | |
| 347 | my $hitlist = TFBS::SiteSet->new(); |
| 348 | |
| 349 | foreach my $pwm (@PWMs) { |
| 350 | my $threshold = ($args{-threshold} or $pwm->{minscore}); |
| 351 | $hitlist->add_siteset($pwm->search_seq(-seqobj=>$seqobj, |
| 352 | -threshold =>$threshold, |
| 353 | -subpart=>$args{-subpart})); |
| 354 | } |
| 355 | delete $seqobj->{_fastaFH}; |
| 356 | unlink $seqobj->{_fastafile}; |
| 357 | delete $seqobj->{_fastafile}; |
| 358 | return $hitlist; |
| 359 | } |
| 360 | |
| 361 | |
| 362 | |
| 363 | sub _csearch { |
| 364 | |
| 365 | my ($self, %args) = @_; |
| 366 | my $PWM_SEARCH = '/home/httpd/cgi-bin/CONSITE/bin/pwm_searchPFF'; |
| 367 | |
| 368 | # DIRTY - stick tmp file name to seq object |
| 369 | |
| 370 | my $seqobj = $self->_to_seqobj(%args); |
| 371 | ($seqobj->{_fastaFH}, $seqobj->{_fastafile}) = tmpnam(); |
| 372 | # we need $fastafile below |
| 373 | |
| 374 | my $seqFH = Bio::SeqIO->newFh(-fh=>$seqobj->{_fastaFH}, -format=>"Fasta"); |
| 375 | print $seqFH $seqobj; |
| 376 | |
| 377 | |
| 378 | # iterate through pwms |
| 379 | my @PWMs; |
| 380 | $self->reset(); |
| 381 | |
| 382 | while (my $pwm = $self->next() ) { |
| 383 | push @PWMs,$pwm; |
| 384 | } |
| 385 | |
| 386 | # do the analysis |
| 387 | |
| 388 | my $hitlist = TFBS::SiteSet->new(); |
| 389 | |
| 390 | foreach my $pwm (@PWMs) { |
| 391 | my $threshold = ($args{-threshold} or $pwm->{minscore}); |
| 392 | $hitlist->add_siteset($pwm->search_seq(-seqobj=>$seqobj, |
| 393 | -threshold =>$threshold )); |
| 394 | } |
| 395 | delete $seqobj->{_fastaFH}; |
| 396 | delete $seqobj->{_fastafile}; |
| 397 | return $hitlist; |
| 398 | |
| 399 | } |
| 400 | |
| 401 | |
| 402 | |
| 403 | sub _bsearch { |
| 404 | my ($self,%args) = @_; #the rest of @_ goes to _to_seqob; |
| 405 | my @PWMs; |
| 406 | |
| 407 | # prepare the sequence |
| 408 | |
| 409 | my $seqobj = $self->_to_seqobj(%args); |
| 410 | $seqobj->{_pdl_matrix} = _seq_to_pdlmatrix($seqobj); |
| 411 | |
| 412 | # prepare the PWMs |
| 413 | |
| 414 | $self->reset(); |
| 415 | |
| 416 | while (my $pwm = $self->next() ) { |
| 417 | push @PWMs,$pwm; |
| 418 | } |
| 419 | |
| 420 | # do the analysis |
| 421 | |
| 422 | my $hitlist = TFBS::SiteSet->new(); |
| 423 | |
| 424 | foreach my $pwm (@PWMs) { |
| 425 | my $threshold = ($args{-threshold} or $pwm->{minscore}); |
| 426 | $hitlist->add_siteset($pwm->bsearch(-seqobj=>$seqobj, |
| 427 | -threshold =>$threshold )); |
| 428 | } |
| 429 | delete $seqobj->{_pdl_matrix}; |
| 430 | return $hitlist; |
| 431 | } |
| 432 | |
| 433 | sub _seq_to_pdlmatrix { |
| 434 | |
| 435 | # not OO - help function for search |
| 436 | |
| 437 | my $seqobj = shift; |
| 438 | my $seqstring = uc($seqobj->seq()); |
| 439 | |
| 440 | my @perlarray; |
| 441 | foreach (qw(A C G T)) { |
| 442 | my $seqtobits = $seqstring; |
| 443 | eval "\$seqtobits =~ tr/$_/1/"; # curr. letter $_ to 1 |
| 444 | eval "\$seqtobits =~ tr/1/0/c"; # non-1s to 0 |
| 445 | push @perlarray, [split("", $seqtobits)]; |
| 446 | } |
| 447 | return byte (\@perlarray); |
| 448 | } |
| 449 | |
| 450 | |
| 451 | |
| 452 | sub _to_seqobj { |
| 453 | my ($self, %args) = @_; |
| 454 | |
| 455 | my $seq; |
| 456 | if ($args{-file}) { # not a Bio::Seq |
| 457 | return Bio::SeqIO->new(-file => $args{-file}, |
| 458 | -format => 'fasta', |
| 459 | -moltype => 'dna')->next_seq(); |
| 460 | } |
| 461 | elsif ($args{-seqstring} |
| 462 | or $args{-seq}) |
| 463 | { # I guess it's a string then |
| 464 | return Bio::Seq->new(-seq => ($args{-seqstring} or $args{-seq}), |
| 465 | -id => ($args{-seq_id} or "undefined"), |
| 466 | -moltype => 'dna'); |
| 467 | } |
| 468 | elsif ($args{'-seqobj'} and ref($args{'-seqobj'}) =~ /Bio\:\:Seq/) { |
| 469 | # do nothing (maybe check later) |
| 470 | return $args{'-seqobj'}; |
| 471 | } |
| 472 | #elsif (ref($format) =~ /Bio\:\:Seq/ and !defined $seq) { |
| 473 | # if only one parameter passed and it's a Bio::Seq |
| 474 | #return $format; |
| 475 | #} |
| 476 | else { |
| 477 | $self->throw ("Wrong parameters passed to search method: ".%args); |
| 478 | } |
| 479 | |
| 480 | |
| 481 | # CONTINUE HERE TOMORROW |
| 482 | |
| 483 | } |
| 484 | |
| 485 | sub add_Matrix { |
| 486 | my ($self, @matrixlist) = @_; |
| 487 | foreach (@matrixlist) { |
| 488 | ref($_) =~ /TFBS::Matrix::/ |
| 489 | or $self->throw("Attempted to add an element ". |
| 490 | "that is not a TFBS::Matrix object."); |
| 491 | push @{$self->{matrix_list}}, $_; |
| 492 | push @{$self->{_iterator_list}}, $_; |
| 493 | |
| 494 | } |
| 495 | return 1; |
| 496 | } |
| 497 | |
| 498 | |
| 499 | |
| 500 | 1; |
| 501 |