| 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 |
Modified by Eivind Valen [email protected] |
| 46 |
|
| 47 |
=head1 APPENDIX |
| 48 |
|
| 49 |
The rest of the documentation details each of the object |
| 50 |
methods. Internal methods are preceded with an underscore. |
| 51 |
|
| 52 |
=cut |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
# The code begins HERE: |
| 57 |
|
| 58 |
package TFBS::MatrixSet; |
| 59 |
use vars '@ISA'; |
| 60 |
|
| 61 |
use PDL; |
| 62 |
use Bio::Seq; |
| 63 |
use Bio::SeqIO; |
| 64 |
use Bio::Root::Root; |
| 65 |
use Bio::TreeIO; |
| 66 |
use File::Temp qw/:POSIX/; |
| 67 |
|
| 68 |
use TFBS::Matrix; |
| 69 |
use TFBS::_Iterator::_MatrixSetIterator; |
| 70 |
use TFBS::SiteSet; |
| 71 |
|
| 72 |
use strict; |
| 73 |
|
| 74 |
use constant TRUE => 1; |
| 75 |
use constant FALSE => 0; |
| 76 |
|
| 77 |
@ISA = qw(Bio::Root::Root); |
| 78 |
|
| 79 |
|
| 80 |
# Hash of accepted options and their arguments for the program |
| 81 |
# STAMP. Reference to empty list means the option take no arguments |
| 82 |
# This test for legal arguments is maybe superfluous and can |
| 83 |
# potentially be removed. |
| 84 |
my %stamp_opt = ( |
| 85 |
-tf => [], |
| 86 |
-sd => [], |
| 87 |
-cc => [ "PCC", "ALLR", "ALLR_LL", "CS", "KL", "SSD" ], |
| 88 |
-align => [ "NW", "SW", "SWA", "SWU" ], |
| 89 |
-go => [], |
| 90 |
-ge => [], |
| 91 |
-out => [], |
| 92 |
-overlapalign => [], |
| 93 |
-nooverlapalign => [], |
| 94 |
-extendedoverlap => [], |
| 95 |
-printpairwise => [], |
| 96 |
-tree => [ "UPGMA", "SOTA" ], |
| 97 |
-ch => [], |
| 98 |
-ma => [ "PPA", "IR" ], |
| 99 |
-match => [], |
| 100 |
-matchtop => [], |
| 101 |
-prot => [], |
| 102 |
-genrand => [], |
| 103 |
-genscores => [], |
| 104 |
-stampdir => [], |
| 105 |
-tempdir => [], |
| 106 |
-noclean => [] |
| 107 |
); |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
=head2 new |
| 113 |
|
| 114 |
=cut |
| 115 |
|
| 116 |
|
| 117 |
sub new { |
| 118 |
my ($caller, @matrices) = shift; |
| 119 |
my $self = bless {matrix_list =>[]}, ref($caller) || $caller; |
| 120 |
$self->add_matrix(@matrices) if @matrices; |
| 121 |
return $self; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
=head2 new2 |
| 126 |
|
| 127 |
=cut |
| 128 |
|
| 129 |
|
| 130 |
sub new2 { |
| 131 |
my $class = shift; |
| 132 |
my %args = @_; |
| 133 |
my $self = bless {}, ref($class) || $class; |
| 134 |
|
| 135 |
if (defined $args{'-matrices'}) { |
| 136 |
$self->add_matrix( @{$args{'-matrices'}} ) if @{$args{'-matrices'}}; |
| 137 |
} |
| 138 |
|
| 139 |
if (defined $args{'-matrixfile'}) { |
| 140 |
my @matrices; |
| 141 |
|
| 142 |
open (FILE, $args{-matrixfile}) |
| 143 |
or $self->throw("Could not open $args{-matrixfile}"); |
| 144 |
|
| 145 |
while (<FILE>) { |
| 146 |
/^\s*$/ && next; |
| 147 |
if (/^>/) { |
| 148 |
|
| 149 |
} |
| 150 |
} |
| 151 |
close(FILE); |
| 152 |
|
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
return $self; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
=head2 add_matrix |
| 161 |
|
| 162 |
Title : add_matrix |
| 163 |
Usage : $matrixset->add_matrix(@list_of_matrix_objects); |
| 164 |
Function: Adds matrix objects to matrixset |
| 165 |
Returns : object reference (usually ignored) |
| 166 |
Args : one or more TFBS::Matrix::* objects |
| 167 |
|
| 168 |
=cut |
| 169 |
|
| 170 |
sub add_matrix { |
| 171 |
my ($self, @matrices) = @_; |
| 172 |
foreach my $matrix (@matrices) { |
| 173 |
$self->throw("Argument to add_matrix_set not a TFBS::Matrix object") |
| 174 |
unless $matrix->isa("TFBS::Matrix"); |
| 175 |
} |
| 176 |
push @{$self->{matrix_list}}, @matrices; |
| 177 |
return $self; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
sub add_Matrix { |
| 182 |
my $self = shift; |
| 183 |
return $self->add_matrix(@_); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
=head2 add_matrix_set |
| 188 |
|
| 189 |
Title : add_matrix |
| 190 |
Usage : $matrixset->add_matrix(@list_of_matrixset_objects); |
| 191 |
Function: Adds to the matrixset matrix objects contained in one or |
| 192 |
more other matrixsets |
| 193 |
Returns : object reference (usually ignored) |
| 194 |
Args : one or more TFBS::MatrixSet objects |
| 195 |
|
| 196 |
=cut |
| 197 |
|
| 198 |
|
| 199 |
sub add_matrix_set { |
| 200 |
my ($self, @sets) = @_; |
| 201 |
foreach my $matrixset (@sets) { |
| 202 |
$self->throw("Argument to add_matrix_set not a TFBS::Matrixset object") |
| 203 |
unless $matrixset->isa("TFBS::MatrixSet"); |
| 204 |
push @{$self->{matrix_list}}, @{$matrixset->{matrix_list}}; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
sub reset { |
| 209 |
my ($self) = @_; |
| 210 |
$self->warn("reset: Deprecated method use Iterator instead."); |
| 211 |
@{$self->{_iterator_list}} = @{$self->{matrix_list}}; |
| 212 |
} |
| 213 |
|
| 214 |
sub sort_by_name { |
| 215 |
my ($self) = @_; |
| 216 |
$self->warn("sort_by_name: Deprecated method use Iterator instead."); |
| 217 |
@{$self->{matrix_list}} = sort { uc($a->{name}) cmp uc ($b->{name}) } |
| 218 |
@{$self->{matrix_list}}; |
| 219 |
$self->reset(); |
| 220 |
} |
| 221 |
|
| 222 |
sub next { |
| 223 |
my ($self) = @_; |
| 224 |
$self->warn("next: Deprecated method use Iterator instead."); |
| 225 |
if (my $next_matrix = shift (@{$self->{_iterator_list}})) { |
| 226 |
return $next_matrix; |
| 227 |
} |
| 228 |
else { |
| 229 |
$self->reset; |
| 230 |
return undef; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
=head2 search_seq |
| 236 |
|
| 237 |
Title : search_seq |
| 238 |
Usage : my $siteset = $matrixset->search_seq(%args) |
| 239 |
Function: scans a nucleotide sequence with all patterns represented |
| 240 |
stored in $matrixset; |
| 241 |
|
| 242 |
It works only if all matrix objects in $matrixset understand |
| 243 |
search_seq method (currently only TFBS::Matrix::PWM objects do) |
| 244 |
Returns : a TFBS::SiteSet object |
| 245 |
Args : # you must specify either one of the following three: |
| 246 |
|
| 247 |
-file, # the name od a fasta file (single sequence) |
| 248 |
#or |
| 249 |
-seqobj # a Bio::Seq object |
| 250 |
# (more accurately, a Bio::PrimarySeqobject or a |
| 251 |
# subclass thereof) |
| 252 |
#or |
| 253 |
-seqstring # a string containing the sequence |
| 254 |
|
| 255 |
-threshold, # minimum score for the hit, either absolute |
| 256 |
# (e.g. 11.2) or relative (e.g. "75%") |
| 257 |
# OPTIONAL: default "80%" |
| 258 |
|
| 259 |
=cut |
| 260 |
|
| 261 |
|
| 262 |
sub search_seq { |
| 263 |
my ($self, %args) = @_; |
| 264 |
$self->_search(%args); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
=head2 search_aln |
| 269 |
|
| 270 |
Title : search_aln |
| 271 |
Usage : my $site_pair_set = $matrixset->search_aln(%args) |
| 272 |
Function: Scans a pairwise alignment of nucleotide sequences |
| 273 |
with the pattern represented by the PWM: it reports only |
| 274 |
those hits that are present in equivalent positions of both |
| 275 |
sequences and exceed a specified threshold score in both, AND |
| 276 |
are found in regions of the alignment above the specified |
| 277 |
conservation cutoff value. |
| 278 |
It works only if all matrix object in $matrixset understand |
| 279 |
search_aln method (currently only TFBS::Matrix::PWM objects do) |
| 280 |
|
| 281 |
Returns : a TFBS::SitePairSet object |
| 282 |
Args : # you must specify either one of the following three: |
| 283 |
|
| 284 |
-file, # the name of the alignment file in Clustal |
| 285 |
format |
| 286 |
#or |
| 287 |
-alignobj # a Bio::SimpleAlign object |
| 288 |
# (more accurately, a Bio::PrimarySeqobject or a |
| 289 |
# subclass thereof) |
| 290 |
#or |
| 291 |
-alignstring # a multi-line string containing the alignment |
| 292 |
# in clustal format |
| 293 |
############# |
| 294 |
|
| 295 |
-threshold, # minimum score for the hit, either absolute |
| 296 |
# (e.g. 11.2) or relative (e.g. "75%") |
| 297 |
# OPTIONAL: default "80%" |
| 298 |
|
| 299 |
-window, # size of the sliding window (inn nucleotides) |
| 300 |
# for calculating local conservation in the |
| 301 |
# alignment |
| 302 |
# OPTIONAL: default 50 |
| 303 |
|
| 304 |
-cutoff # conservation cutoff (%) for including the |
| 305 |
# region in the results of the pattern search |
| 306 |
# OPTIONAL: default "70%" |
| 307 |
|
| 308 |
-subpart # subpart of the alignment to search, given as e.g. |
| 309 |
# -subpart => { relative_to => 1, |
| 310 |
# start => 140, |
| 311 |
# end => 180 } |
| 312 |
# where start and end are coordinates in the |
| 313 |
# sequence indicated by relative_to (1 for the |
| 314 |
# 1st sequence in the alignment, 2 for the 2nd) |
| 315 |
# OPTIONAL: by default searches entire alignment |
| 316 |
|
| 317 |
-conservation |
| 318 |
# conservation profile, a TFBS::ConservationProfile |
| 319 |
# OPTIONAL: by default the conservation profile is |
| 320 |
# computed internally on the fly (less efficient) |
| 321 |
|
| 322 |
=cut |
| 323 |
|
| 324 |
sub search_aln { |
| 325 |
my ($self, %args) = @_; |
| 326 |
my $mxit = $self->Iterator(); |
| 327 |
my $sitepairset = TFBS::SitePairSet->new; |
| 328 |
my $aln = TFBS::Matrix::_Alignment->new(%args); |
| 329 |
while (my $mx = $mxit->next) { |
| 330 |
my $singleset = $mx->search_aln(%args, |
| 331 |
-alignment_setup => $aln); |
| 332 |
$sitepairset->add_site_pair_set($singleset); |
| 333 |
} |
| 334 |
return $sitepairset; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
=head2 size |
| 340 |
|
| 341 |
Title : size |
| 342 |
Usage : my $number_of_matrices = $matrixset->size; |
| 343 |
Function: gets the number of matrix objects in the $matrixset |
| 344 |
(i.e. the size of the set) |
| 345 |
Returns : a number |
| 346 |
Args : none |
| 347 |
|
| 348 |
=cut |
| 349 |
|
| 350 |
|
| 351 |
sub size { |
| 352 |
scalar @{ $_[0]->{matrix_list} }; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
=head2 Iterator |
| 357 |
|
| 358 |
Title : Iterator |
| 359 |
Usage : my $matrixset_iterator = |
| 360 |
$matrixset->Iterator(-sort_by =>'total_ic'); |
| 361 |
while (my $matrix_object = $matrix_iterator->next) { |
| 362 |
# do whatever you want with individual matrix objects |
| 363 |
} |
| 364 |
Function: Returns an iterator object that can be used to go through |
| 365 |
all members of the set |
| 366 |
Returns : an iterator object (currently undocumentened in TFBS - |
| 367 |
but understands the 'next' method) |
| 368 |
Args : -sort_by # optional - currently it accepts |
| 369 |
# 'ID' (alphabetically) |
| 370 |
# 'name' (alphabetically) |
| 371 |
# 'class' (alphabetically) |
| 372 |
# 'total_ic' (numerically, decreasing order) |
| 373 |
|
| 374 |
-reverse # optional - reverses the default sorting order if true |
| 375 |
|
| 376 |
=cut |
| 377 |
|
| 378 |
sub Iterator { |
| 379 |
|
| 380 |
my ($self, %args) = @_; |
| 381 |
return TFBS::_Iterator::_MatrixSetIterator->new($self->{matrix_list}, |
| 382 |
$args{'-sort_by'}, |
| 383 |
$args{'-reverse'} |
| 384 |
); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
=head2 randomize_columns |
| 390 |
|
| 391 |
Title : randomize_columns |
| 392 |
Usage : $matrixset->randomize_columns(); |
| 393 |
Function: Randomizes the columns between all the matrices in the set (in place). |
| 394 |
Returns : nothing |
| 395 |
Args : none |
| 396 |
|
| 397 |
=cut |
| 398 |
|
| 399 |
|
| 400 |
sub randomize_columns { |
| 401 |
my $self = shift; |
| 402 |
my (@lengths, @concat); |
| 403 |
my ($length, $i) = (-1, 0); |
| 404 |
|
| 405 |
# Concatenate to one big matrix |
| 406 |
for my $matrix (@{$self->{matrix_list}}) { |
| 407 |
$length += $matrix->length(); |
| 408 |
push @lengths, $matrix->length(); |
| 409 |
push @{$concat[$_]}, @{${$matrix->matrix()}[$_]} for (0..3); |
| 410 |
} |
| 411 |
|
| 412 |
# Schwartzian transform to get random permutation |
| 413 |
map { ( undef, $concat[0][$i], $concat[1][$i], $concat[2][$i], $concat[3][$i] ) = @$_; $i++; } |
| 414 |
sort { $a->[0] <=> $b->[0] } |
| 415 |
map { [ rand(), $concat[0][$_], $concat[1][$_], $concat[2][$_], $concat[3][$_] ] } ( 0 .. $length ); |
| 416 |
|
| 417 |
# Split it up again |
| 418 |
my $start = 0; |
| 419 |
for my $matrix (@{$self->{matrix_list}}) { |
| 420 |
my $length = shift(@lengths); |
| 421 |
my $end = $start + $length - 1; |
| 422 |
|
| 423 |
$matrix->matrix( [ |
| 424 |
[ @{$concat[0]}[$start..$end] ], |
| 425 |
[ @{$concat[1]}[$start..$end] ], |
| 426 |
[ @{$concat[2]}[$start..$end] ], |
| 427 |
[ @{$concat[3]}[$start..$end] ] |
| 428 |
] |
| 429 |
); |
| 430 |
$start += $length; |
| 431 |
} |
| 432 |
|
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
sub _search { |
| 437 |
|
| 438 |
my ($self, %args) = @_; |
| 439 |
|
| 440 |
# DIRTY - stick tmp file name to seq object |
| 441 |
|
| 442 |
my $seqobj = $self->_to_seqobj(%args); |
| 443 |
($seqobj->{_fastaFH}, $seqobj->{_fastafile}) = tmpnam(); |
| 444 |
# we need $fastafile below |
| 445 |
|
| 446 |
my $outstream = Bio::SeqIO->new(-file=>">".$seqobj->{_fastafile}, -format=>"Fasta"); |
| 447 |
my $subseqobj; |
| 448 |
if(my $subpart = $args{-subpart}) { |
| 449 |
my $subseq_start = $subpart->{-start}; |
| 450 |
my $subseq_end = $subpart->{-end}; |
| 451 |
unless($subseq_start and $subseq_end) { |
| 452 |
$self->throw("Option -subpart missing suboption -relative_to, -start or -end"); |
| 453 |
} |
| 454 |
$subseqobj = Bio::Seq->new(-seq => $seqobj->subseq($subseq_start, $subseq_end), |
| 455 |
-id => $seqobj->id); |
| 456 |
} |
| 457 |
$outstream->write_seq($subseqobj or $seqobj); |
| 458 |
$outstream->close; |
| 459 |
|
| 460 |
# iterate through pwms |
| 461 |
my @PWMs; |
| 462 |
my $mxit = $self->Iterator(); |
| 463 |
|
| 464 |
while (my $pwm = $mxit->next() ) { |
| 465 |
push @PWMs,$pwm; |
| 466 |
} |
| 467 |
|
| 468 |
# do the analysis |
| 469 |
|
| 470 |
my $hitlist = TFBS::SiteSet->new(); |
| 471 |
|
| 472 |
foreach my $pwm (@PWMs) { |
| 473 |
my $threshold = ($args{-threshold} or $pwm->{minscore}); |
| 474 |
$hitlist->add_siteset($pwm->search_seq(-seqobj=>$seqobj, |
| 475 |
-threshold =>$threshold, |
| 476 |
-subpart=>$args{-subpart})); |
| 477 |
} |
| 478 |
delete $seqobj->{_fastaFH}; |
| 479 |
unlink $seqobj->{_fastafile}; |
| 480 |
delete $seqobj->{_fastafile}; |
| 481 |
return $hitlist; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
sub _csearch { |
| 487 |
|
| 488 |
my ($self, %args) = @_; |
| 489 |
my $PWM_SEARCH = '/home/httpd/cgi-bin/CONSITE/bin/pwm_searchPFF'; |
| 490 |
|
| 491 |
# DIRTY - stick tmp file name to seq object |
| 492 |
|
| 493 |
my $seqobj = $self->_to_seqobj(%args); |
| 494 |
($seqobj->{_fastaFH}, $seqobj->{_fastafile}) = tmpnam(); |
| 495 |
# we need $fastafile below |
| 496 |
|
| 497 |
my $seqFH = Bio::SeqIO->newFh(-fh=>$seqobj->{_fastaFH}, -format=>"Fasta"); |
| 498 |
print $seqFH $seqobj; |
| 499 |
|
| 500 |
|
| 501 |
# iterate through pwms |
| 502 |
my @PWMs; |
| 503 |
$self->reset(); |
| 504 |
|
| 505 |
while (my $pwm = $self->next() ) { |
| 506 |
push @PWMs,$pwm; |
| 507 |
} |
| 508 |
|
| 509 |
# do the analysis |
| 510 |
|
| 511 |
my $hitlist = TFBS::SiteSet->new(); |
| 512 |
|
| 513 |
foreach my $pwm (@PWMs) { |
| 514 |
my $threshold = ($args{-threshold} or $pwm->{minscore}); |
| 515 |
$hitlist->add_siteset($pwm->search_seq(-seqobj=>$seqobj, |
| 516 |
-threshold =>$threshold )); |
| 517 |
} |
| 518 |
delete $seqobj->{_fastaFH}; |
| 519 |
delete $seqobj->{_fastafile}; |
| 520 |
return $hitlist; |
| 521 |
|
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
sub _bsearch { |
| 527 |
my ($self,%args) = @_; #the rest of @_ goes to _to_seqob; |
| 528 |
my @PWMs; |
| 529 |
|
| 530 |
# prepare the sequence |
| 531 |
|
| 532 |
my $seqobj = $self->_to_seqobj(%args); |
| 533 |
$seqobj->{_pdl_matrix} = _seq_to_pdlmatrix($seqobj); |
| 534 |
|
| 535 |
# prepare the PWMs |
| 536 |
|
| 537 |
$self->reset(); |
| 538 |
|
| 539 |
while (my $pwm = $self->next() ) { |
| 540 |
push @PWMs,$pwm; |
| 541 |
} |
| 542 |
|
| 543 |
# do the analysis |
| 544 |
|
| 545 |
my $hitlist = TFBS::SiteSet->new(); |
| 546 |
|
| 547 |
foreach my $pwm (@PWMs) { |
| 548 |
my $threshold = ($args{-threshold} or $pwm->{minscore}); |
| 549 |
$hitlist->add_siteset($pwm->bsearch(-seqobj=>$seqobj, |
| 550 |
-threshold =>$threshold )); |
| 551 |
} |
| 552 |
delete $seqobj->{_pdl_matrix}; |
| 553 |
return $hitlist; |
| 554 |
} |
| 555 |
|
| 556 |
sub _seq_to_pdlmatrix { |
| 557 |
|
| 558 |
# not OO - help function for search |
| 559 |
|
| 560 |
my $seqobj = shift; |
| 561 |
my $seqstring = uc($seqobj->seq()); |
| 562 |
|
| 563 |
my @perlarray; |
| 564 |
foreach (qw(A C G T)) { |
| 565 |
my $seqtobits = $seqstring; |
| 566 |
eval "\$seqtobits =~ tr/$_/1/"; # curr. letter $_ to 1 |
| 567 |
eval "\$seqtobits =~ tr/1/0/c"; # non-1s to 0 |
| 568 |
push @perlarray, [split("", $seqtobits)]; |
| 569 |
} |
| 570 |
return byte (\@perlarray); |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
sub _to_seqobj { |
| 576 |
my ($self, %args) = @_; |
| 577 |
|
| 578 |
my $seq; |
| 579 |
if ($args{-file}) { # not a Bio::Seq |
| 580 |
return Bio::SeqIO->new(-file => $args{-file}, |
| 581 |
-format => 'fasta', |
| 582 |
-moltype => 'dna')->next_seq(); |
| 583 |
} |
| 584 |
elsif ($args{-seqstring} |
| 585 |
or $args{-seq}) |
| 586 |
{ # I guess it's a string then |
| 587 |
return Bio::Seq->new(-seq => ($args{-seqstring} or $args{-seq}), |
| 588 |
-id => ($args{-seq_id} or "undefined"), |
| 589 |
-moltype => 'dna'); |
| 590 |
} |
| 591 |
elsif ($args{'-seqobj'} and ref($args{'-seqobj'}) =~ /Bio\:\:Seq/) { |
| 592 |
# do nothing (maybe check later) |
| 593 |
return $args{'-seqobj'}; |
| 594 |
} |
| 595 |
#elsif (ref($format) =~ /Bio\:\:Seq/ and !defined $seq) { |
| 596 |
# if only one parameter passed and it's a Bio::Seq |
| 597 |
#return $format; |
| 598 |
#} |
| 599 |
else { |
| 600 |
$self->throw ("Wrong parameters passed to search method: ".%args); |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
# CONTINUE HERE TOMORROW |
| 605 |
|
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
=head2 remove_Matrix_by_ID |
| 612 |
|
| 613 |
Title : remove_Matrix_by_ID |
| 614 |
Usage : $matrixset->remove_Matrix_by_ID($id); |
| 615 |
Function: Removes a matrix from the set |
| 616 |
Returns : Nothing |
| 617 |
Args : None |
| 618 |
|
| 619 |
=cut |
| 620 |
|
| 621 |
sub remove_Matrix_by_ID { |
| 622 |
my ($self, $id) = @_; |
| 623 |
|
| 624 |
my @list = grep { $_->ID() ne $id } @{$self->{matrix_list}}; |
| 625 |
$self->{matrix_list} = \@list; |
| 626 |
} |
| 627 |
|
| 628 |
my $error; |
| 629 |
|
| 630 |
|
| 631 |
sub _check_opt { |
| 632 |
my ($self, $opt, $arg, $list) = @_; |
| 633 |
|
| 634 |
# Invalid argument |
| 635 |
if (not defined($list)) { |
| 636 |
$error = "Invalid argument: $opt\n"; |
| 637 |
return FALSE; |
| 638 |
} |
| 639 |
|
| 640 |
# Valid flag or switch. |
| 641 |
return TRUE if (not scalar(@$list)); |
| 642 |
|
| 643 |
|
| 644 |
# Valid switch, check the argument |
| 645 |
for (@$list) { |
| 646 |
return TRUE if ($arg eq $_) ; |
| 647 |
} |
| 648 |
|
| 649 |
# Valid switch, invalid argument |
| 650 |
$error = "$arg is invalid argument to $opt"; |
| 651 |
return FALSE; |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
sub _find_optimal { |
| 656 |
my ($self, $output) = @_; |
| 657 |
my ($optimal, $score_best, $in) = (undef, undef, 0); |
| 658 |
|
| 659 |
for (@$output) { |
| 660 |
if (/NumClust/) { |
| 661 |
$in = 1; |
| 662 |
next; |
| 663 |
} |
| 664 |
|
| 665 |
last if (/Tree Built/); |
| 666 |
|
| 667 |
if ($in) { |
| 668 |
my (undef, $clusters, $score) = split(/\t/); |
| 669 |
|
| 670 |
if ((not defined($score_best)) || $score < $score_best) { |
| 671 |
$score_best = $score; |
| 672 |
$optimal = $clusters; |
| 673 |
} |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
return $optimal; |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
sub _run_STAMP { |
| 682 |
my ($self, %args) = @_; |
| 683 |
my $fh; |
| 684 |
|
| 685 |
for (keys(%args)) { |
| 686 |
die $error unless ($self->_check_opt($_, $args{$_}, $stamp_opt{$_})); |
| 687 |
} |
| 688 |
|
| 689 |
# Write matrices to temporary file |
| 690 |
if (not exists($args{-tf})) { |
| 691 |
$fh = new File::Temp( TEMPLATE => 'STAMP-XXXXX', |
| 692 |
DIR => $args{-tempdir} || '/tmp', |
| 693 |
SUFFIX => '.set'); |
| 694 |
|
| 695 |
print $fh $_->STAMPprint() for (@{$self->{matrix_list}}); |
| 696 |
$args{-tf} = $fh->filename(); |
| 697 |
} |
| 698 |
# Set some default options |
| 699 |
$args{-tree} ||= "UPGMA"; |
| 700 |
$args{-ma} ||= "IR"; |
| 701 |
$args{-cc} ||= "PCC"; |
| 702 |
$args{-align} ||= "SWU"; |
| 703 |
|
| 704 |
|
| 705 |
# Make sure we find all files |
| 706 |
my $path; |
| 707 |
if ($args{-stampdir}) { |
| 708 |
$path = $args{-stampdir}; |
| 709 |
die "Could not find STAMP at $path\n" if (not -e "$path/STAMP"); |
| 710 |
} else { |
| 711 |
$path = (grep {-e "$_/STAMP"} split(/:+/, $ENV{PATH}))[0]; |
| 712 |
$path || die "Could not find STAMP in path\n"; |
| 713 |
} |
| 714 |
|
| 715 |
$args{-sd} ||= $path."/ScoreDists/JaspRand_".$args{-cc}."_".$args{-align}.".scores"; |
| 716 |
die "No score distribution file found or not readable at '$args{-sd}'.\n Use -sd.\n" unless (-r $args{-sd}); |
| 717 |
|
| 718 |
# Execute STAMP |
| 719 |
my $args = ""; |
| 720 |
$args .= "$_ $args{$_} " for (keys(%args)); |
| 721 |
my @output = `$path/STAMP -ch $args -out $fh`; |
| 722 |
|
| 723 |
# Get tree |
| 724 |
my $treeio = new Bio::TreeIO(-format => 'newick', -file => $fh->filename().".tree"); |
| 725 |
my $tree = $treeio->next_tree; |
| 726 |
|
| 727 |
# Get FBP |
| 728 |
my $fbp = TFBS::Matrix::PFM->new(-matrixfile => $fh->filename()."FBP.txt"); |
| 729 |
$fbp->{'filename'} = $fh->filename()."FBP.txt"; |
| 730 |
|
| 731 |
print STDERR "::: $fbp->{'filename'} \n"; |
| 732 |
|
| 733 |
if (not $args{-noclean}) { |
| 734 |
my $deleted = unlink($fh->filename()."FBP.txt", $fh->filename().".tree"); |
| 735 |
warn("Couldn't remove temporary files") if ($deleted != 2); |
| 736 |
} |
| 737 |
|
| 738 |
return ($fh, \@output, $tree, $fbp); |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
sub _build_cluster { |
| 744 |
my ($self, $cluster, $node) = @_; |
| 745 |
|
| 746 |
if ($node->is_Leaf()) { |
| 747 |
for (@{$self->{matrix_list}}) { |
| 748 |
if ($_->ID() eq $node->id()) { |
| 749 |
$cluster->add_matrix($_); |
| 750 |
return; |
| 751 |
} |
| 752 |
} |
| 753 |
} else { |
| 754 |
$self->_build_cluster($cluster, $_) for ($node->each_Descendent()); |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
=head2 cluster |
| 762 |
|
| 763 |
Title : cluster |
| 764 |
Usage : $matrixset->cluster(%args) |
| 765 |
Function: Clusters the matrices in the set |
| •766 |
Returns : The root node of the hierarchical clustering tree. |
| 767 |
An integer specifying the optimal number of clusters. |
| 768 |
An array of TFBS::MatrixSets, one for each cluster. |
| 769 |
Args : Many: |
| 770 |
-stampdir Directory where stamp is located. Not necessary if it is in the PATH. |
| 771 |
-tempdir Directory to put temporary files. Defaults to "/tmp" |
| 772 |
-noclean 0 to clean up temporary files, 1 otherwise |
| 773 |
-tree Method for constructing tree (UPGMA/SOTA). Def:UPGMA |
| 774 |
|
| 775 |
=cut |
| 776 |
|
| 777 |
sub cluster { |
| 778 |
my ($self, %args) = @_; |
| 779 |
|
| 780 |
if ($self->size() <= 1) { |
| 781 |
warn("Can't cluster MatrixSet of size less than 2"); |
| 782 |
return; |
| 783 |
} |
| 784 |
|
| 785 |
my ($fh, $output, $tree, $fbp) = $self->_run_STAMP(%args); |
| 786 |
|
| 787 |
# Find optimal cluster number |
| 788 |
my $optimal = $args{-optimal} || $self->_find_optimal($output); |
| 789 |
my $root = $tree->get_root_node(); |
| 790 |
|
| 791 |
my @nodes = ($root); |
| 792 |
my @leaves; |
| 793 |
|
| 794 |
# Descend the tree until the optimal cluster number is reached |
| 795 |
while (scalar(@nodes) && (scalar(@nodes) + scalar(@leaves)) < $optimal) { |
| 796 |
my $node = pop @nodes; |
| 797 |
|
| 798 |
if ($node->is_Leaf()) { |
| 799 |
push @leaves, $node; |
| 800 |
} else { |
| 801 |
@nodes = sort {$a->height() <=> $b->height()} (@nodes, $node->each_Descendent()); |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
# Build the clusters |
| 806 |
my @clusters; |
| 807 |
for (@leaves, @nodes) { |
| 808 |
my $cluster = $self->new(); |
| 809 |
$self->_build_cluster($cluster, $_); |
| 810 |
push @clusters, $cluster; |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
return ($tree, $optimal, \@clusters); |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
=head2 fbp |
| 820 |
|
| 821 |
Title : fbp |
| 822 |
Usage : $matrixset->fbp(%args); |
| 823 |
Function: Creates a familial binding profile (FBP) for the set |
| 824 |
Returns : A familial binding profile represented as a TFBS::Matrix::PFM |
| 825 |
Args : Many |
| 826 |
-stampdir Directory where stamp is located. Not necessary if it is in the PATH. |
| 827 |
-tempdir Directory to put temporary files. Defaults to "/tmp" |
| 828 |
-noclean 0 to clean up temporary files, 1 otherwise |
| 829 |
-align Alignment method |
| 830 |
=cut |
| 831 |
|
| 832 |
sub fbp { |
| 833 |
my ($self, %args) = @_; |
| 834 |
if ($self->size() == 0) { |
| 835 |
warn("Can't create FBP for MatrixSet of size 0"); |
| 836 |
return; |
| 837 |
} elsif ($self->size() == 1) { |
| 838 |
return @{$self->{'matrix_list'}}[0]; |
| 839 |
} |
| 840 |
|
| 841 |
my ($fh, $output, $tree, $fbp) = $self->_run_STAMP(%args); |
| 842 |
|
| 843 |
return $fbp; |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
1; |
| 850 |
|
| 851 |
|
| 852 |
|