| 1 |
# TFBS module for TFBS::DB::JASPAR4 |
| 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::DB::JASPAR4 - interface to MySQL relational database of pattern matrices |
| 13 |
|
| 14 |
|
| 15 |
=head1 SYNOPSIS |
| 16 |
|
| 17 |
=over 4 |
| 18 |
|
| 19 |
=item * creating a database object by connecting to the existing JASPAR2-type database |
| 20 |
|
| 21 |
my $db = TFBS::DB::JASPAR4->connect("dbi:mysql:JASPAR4:myhost", |
| 22 |
"myusername", |
| 23 |
"mypassword"); |
| 24 |
|
| 25 |
=item * retrieving a TFBS::Matrix::* object from the database |
| 26 |
|
| 27 |
# retrieving a PFM by ID |
| 28 |
my $pfm = $db->get_Matrix_by_ID('M0079','PFM'); |
| 29 |
|
| 30 |
#retrieving a PWM by name |
| 31 |
my $pwm = $db->get_Matrix_by_name('NF-kappaB', 'PWM'); |
| 32 |
|
| 33 |
=item * retrieving a set of matrices as a TFBS::MatrixSet object according to various criteria |
| 34 |
|
| 35 |
# retrieving a set of PWMs from a list of IDs: |
| 36 |
my @IDlist = ('M0019', 'M0045', 'M0073', 'M0101'); |
| 37 |
my $matrixset = $db->get_MatrixSet(-IDs => \@IDlist, |
| 38 |
-matrixtype => "PWM"); |
| 39 |
|
| 40 |
# retrieving a set of ICMs from a list of names: |
| 41 |
my @namelist = ('p50', 'p53', 'HNF-1'. 'GATA-1', 'GATA-2', 'GATA-3'); |
| 42 |
my $matrixset = $db->get_MatrixSet(-names => \@namelist, |
| 43 |
-matrixtype => "ICM"); |
| 44 |
|
| 45 |
# retrieving a set of all PFMs in the database |
| 46 |
# derived from human genes: |
| 47 |
my $matrixset = $db->get_MatrixSet(-species => ['Homo sapiens'], |
| 48 |
-matrixtype => "PFM"); |
| 49 |
|
| 50 |
=item * creating a new JASPAR4-type database named MYJASPAR4: |
| 51 |
|
| 52 |
my $db = TFBS::DB::JASPAR4->create("dbi:mysql:MYJASPAR4:myhost", |
| 53 |
"myusername", |
| 54 |
"mypassword"); |
| 55 |
|
| 56 |
=item * storing a matrix in the database (currently only PFMs): |
| 57 |
|
| 58 |
#let $pfm is a TFBS::Matrix::PFM object |
| 59 |
$db->store_Matrix($pfm); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
=back |
| 64 |
|
| 65 |
=head1 DESCRIPTION |
| 66 |
|
| 67 |
TFBS::DB::JASPAR4 is a read/write database interface module that |
| 68 |
retrieves and stores TFBS::Matrix::* and TFBS::MatrixSet |
| 69 |
objects in a relational database. The interface is nearly identical |
| 70 |
to the JASPAR2interface, while the underlying data model is different |
| 71 |
|
| 72 |
|
| 73 |
=head1 JASPAR2 DATA MODEL |
| 74 |
|
| 75 |
JASPAR4 is working name for a relational database model used |
| 76 |
for storing transcriptional factor pattern matrices in a MySQL database. |
| 77 |
It was initially designed (JASPAR2) to store matrices for the JASPAR database of |
| 78 |
high quality eukaryotic transcription factor specificity profiles by |
| 79 |
Albin Sandelin and Wyeth W. Wasserman. Besides the profile matrix itself, |
| 80 |
this data model stores profile ID (unique), name, structural class, |
| 81 |
basic taxonomic and bibliographic information |
| 82 |
as well as some additional opseqdbtional tags. |
| 83 |
|
| 84 |
|
| 85 |
Tags that are commonly used in the actual JASPAR database include |
| 86 |
'medline' # PubMed ID |
| 87 |
'species' # Species name |
| 88 |
'superclass' #Species supergroup, eg 'vertebrate', 'plant' etc |
| 89 |
'total_ic' # total information content - redundant, present |
| 90 |
# for historical |
| 91 |
'type' #experimental nethod |
| 92 |
'acc' #accession number for TF protein sequence |
| 93 |
'seqdb' #corresponding database name |
| 94 |
|
| 95 |
but any tag is storable and searchable. |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
----------------------- ADVANCED --------------------------------- |
| 101 |
|
| 102 |
For the developers and the curious, here is the JASPAR4 data model: |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
It is our best intention to hide the details of this data model, which we |
| 107 |
are using on a daily basis in our work, from most TFBS users. |
| 108 |
Most users should only know the methods to store the data and |
| 109 |
which tags are supported. |
| 110 |
|
| 111 |
------------------------------------------------------------------------- |
| 112 |
|
| 113 |
|
| 114 |
=head1 FEEDBACK |
| 115 |
|
| 116 |
Please send bug reports and other comments to the author. |
| 117 |
|
| 118 |
=head1 AUTHOR - Boris Lenhard |
| 119 |
|
| 120 |
Boris Lenhard E<lt>[email protected]<gt> |
| 121 |
|
| 122 |
=head1 APPENDIX |
| 123 |
|
| 124 |
The rest of the documentation details each of the object |
| 125 |
methods. Internal methods are preceded with an underscore. |
| 126 |
|
| 127 |
=cut |
| 128 |
|
| 129 |
|
| 130 |
# The code begins HERE: |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
package TFBS::DB::JASPAR4; |
| 135 |
use vars qw(@ISA $AUTOLOAD); |
| 136 |
|
| 137 |
# we need all three matrices due to the redundancy in JASPAR2 data model |
| 138 |
# which will hopefully be removed in JASPAR3 |
| 139 |
|
| 140 |
use TFBS::Matrix::PWM; |
| 141 |
use TFBS::Matrix::PFM; |
| 142 |
use TFBS::Matrix::ICM; |
| 143 |
|
| 144 |
use TFBS::MatrixSet; |
| 145 |
use Bio::Root::Root; |
| 146 |
use DBI; |
| 147 |
# use TFBS::DB; # eventually |
| 148 |
use strict; |
| 149 |
|
| 150 |
@ISA = qw(TFBS::DB Bio::Root::Root); |
| 151 |
|
| 152 |
######################################################################### |
| 153 |
# CONSTANTS |
| 154 |
######################################################################### |
| 155 |
|
| 156 |
use constant DEFAULT_CONNECTSTRING => "dbi:mysql:JASPAR_DEMO"; # on localhost |
| 157 |
use constant DEFAULT_USER => ""; |
| 158 |
use constant DEFAULT_PASSWORD => ""; |
| 159 |
|
| 160 |
######################################################################### |
| 161 |
# PUBLIC METHODS |
| 162 |
######################################################################### |
| 163 |
|
| 164 |
=head2 new |
| 165 |
|
| 166 |
Title : new |
| 167 |
Usage : DEPRECATED - for backward compatibility only |
| 168 |
Use connect() or create() instead |
| 169 |
|
| 170 |
=cut |
| 171 |
|
| 172 |
sub new { |
| 173 |
_new (@_); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
=head2 connect |
| 178 |
|
| 179 |
Title : connect |
| 180 |
Usage : my $db = |
| 181 |
TFBS::DB::JASPAR4->connect("dbi:mysql:DATABASENAME:HOSTNAME", |
| 182 |
"USERNAME", |
| 183 |
"PASSWORD"); |
| 184 |
Function: connects to the existing JASPAR4-type database and |
| 185 |
returns a database object that interfaces the database |
| 186 |
Returns : a TFBS::DB::JASPAR4 object |
| 187 |
Args : a standard database connection triplet |
| 188 |
("dbi:mysql:DATABASENAME:HOSTNAME", "USERNAME", "PASSWORD") |
| 189 |
In place of DATABASENAME, HOSTNAME, USERNAME and PASSWORD, |
| 190 |
use the actual values. PASSWORD and USERNAME might be |
| •191 |
optional, depending on the user's access permissions for |
| 192 |
the database server. |
| 193 |
|
| 194 |
=cut |
| 195 |
|
| 196 |
sub connect { |
| 197 |
# a more intuitive syntax for the constructor |
| 198 |
my ($caller, @connection_args) = @_; |
| 199 |
$caller->new(-connect => \@connection_args); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
=head2 dbh |
| 204 |
|
| 205 |
Title : dbh |
| 206 |
Usage : my $dbh = $db->dbh(); |
| 207 |
$dbh->do("UPDATE matrix_data SET name='ADD1' WHERE NAME='SREBP2'"); |
| 208 |
Function: returns the DBI database handle of the MySQL database |
| 209 |
interfaced by $db; THIS IS USED FOR WRITING NEW METHODS |
| 210 |
FOR DIRECT RELATIONAL DATABASE MANIPULATION - if you |
| 211 |
have write access AND do not know what you are doing, |
| 212 |
you can severely corrupt the data |
| 213 |
For documentation about database handle methods, see L<DBI> |
| 214 |
Returns : the database (DBI) handle of the MySQL JASPAR2-type |
| 215 |
relational database associated with the TFBS::DB::JASPAR2 |
| 216 |
object |
| 217 |
Args : none |
| 218 |
|
| 219 |
=cut |
| 220 |
|
| 221 |
sub dbh { |
| 222 |
my ($self, $dbh) = @_; |
| 223 |
$self->{'dbh'} = $dbh if $dbh; |
| 224 |
return $self->{'dbh'}; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
=head2 store_Matrix |
| 233 |
|
| 234 |
Title : store_Matrix |
| 235 |
Usage : $db->store_Matrix($matrixobject); |
| 236 |
Function: Stores the contents of a TFBS::Matrix::DB object in the database |
| 237 |
Returns : 0 on success; $@ contents on failure |
| 238 |
(this is too C-like and may change in future versions) |
| 239 |
Args : (PFM_object) |
| 240 |
A TFBS::Matrix::PFM, FBS::Matrix::PWM or FBS::Matrix::ICM object. |
| 241 |
PFM object are recommended to use, as they are eaily converted to |
| 242 |
other formats |
| 243 |
Comment : this is an experimental method that is not 100% bulletproof; |
| 244 |
use at your own risk |
| 245 |
|
| 246 |
=cut |
| 247 |
|
| 248 |
|
| 249 |
sub store_Matrix { |
| 250 |
my ($self, @PFMs) = @_; |
| 251 |
my $err; |
| 252 |
foreach my $pfm (@PFMs) { |
| 253 |
eval { |
| 254 |
$self->_store_matrix_data($pfm); |
| 255 |
$self->_store_matrix_info($pfm); |
| 256 |
$self->_store_matrix_annotation($pfm); |
| 257 |
#$self->_store_matrix_species($pfm); |
| 258 |
}; |
| 259 |
} |
| 260 |
return $@; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
sub create { |
| 265 |
my ($caller, $connectstring, $user, $password) = @_; |
| 266 |
if ($connectstring |
| 267 |
and $connectstring =~ /dbi:mysql:(\w+)(.*)/) |
| 268 |
{ |
| 269 |
# connect to the server; |
| 270 |
my $dbh=DBI->connect("dbi:mysql:mysql".$2, |
| 271 |
$user,$password) |
| 272 |
or die("Error connecting to the database"); |
| 273 |
|
| 274 |
# create database and open it |
| 275 |
$dbh->do("create database $1") |
| 276 |
or die("Error creating database."); |
| 277 |
$dbh->do("use $1"); |
| 278 |
|
| 279 |
# create tables |
| 280 |
_create_tables($dbh); |
| 281 |
|
| 282 |
$dbh->disconnect; |
| 283 |
|
| 284 |
# run "new" with new database |
| 285 |
|
| 286 |
return $caller->new(-connect=>[$connectstring, $user, $password]); |
| 287 |
} |
| 288 |
else { |
| 289 |
die("Missing or malformed connect string for ". |
| 290 |
"TFBS::DB::JASPAR2 connection."); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
=head2 get_Matrix_by_ID |
| 296 |
|
| 297 |
Title : get_Matrix_by_ID |
| 298 |
Usage : my $pfm = $db->get_Matrix_by_ID('M00034', 'PFM'); |
| 299 |
Function: fetches matrix data under the given ID from the |
| 300 |
database and returns a TFBS::Matrix::* object |
| 301 |
Returns : a TFBS::Matrix::* object; the exact type of the |
| 302 |
object depending on what form the matrix is stored |
| 303 |
in the database (PFM is default) |
| 304 |
Args : (Matrix_ID) |
| 305 |
Matrix_ID is a string; |
| 306 |
|
| 307 |
=cut |
| 308 |
|
| 309 |
|
| 310 |
sub get_Matrix_by_ID { |
| 311 |
|
| 312 |
my ($self, $ID, $mt) = @_; |
| 313 |
$mt = (uc($mt) or "PWM"); |
| 314 |
|
| 315 |
unless (defined $ID) { |
| 316 |
$self->throw("No ID passed to get_Matrix_by_ID"); |
| 317 |
} |
| 318 |
my $matrixobj; |
| 319 |
{ |
| 320 |
no strict 'refs'; |
| 321 |
my $ucmt = uc $mt; |
| 322 |
my $matrixstring = $self->_get_matrixstring($ID) || return undef; |
| 323 |
# get type of matrix |
| 324 |
my $sth=$self->dbh->prepare(qq{SELECT type FROM MATRIX_INFO WHERE ID = '$ID'}); |
| 325 |
$sth->execute(); |
| 326 |
my $type=$sth->fetchrow_array(); |
| 327 |
|
| 328 |
|
| 329 |
# get reast of annotation as tags |
| 330 |
$sth=$self->dbh->prepare(qq{SELECT tag, val FROM MATRIX_ANNOTATION WHERE ID = '$ID' }); |
| 331 |
$sth->execute(); |
| 332 |
my %tags; |
| 333 |
while ( my($tag, $val)= $sth->fetchrow_array()){ |
| 334 |
$tags{$tag}=$val; |
| 335 |
} |
| 336 |
my $name= $tags{'name'}; |
| 337 |
my $class= $tags{'class'}; |
| 338 |
delete ($tags{'name'}); |
| 339 |
delete ($tags{'class'}); |
| 340 |
|
| 341 |
eval ("\$matrixobj= TFBS::Matrix::$type->new".' |
| 342 |
( -ID => $ID."", |
| 343 |
-name =>$name, |
| 344 |
-class => $class, |
| 345 |
-tags => \%tags, |
| 346 |
-matrixstring=> $matrixstring # FIXME - temporary |
| 347 |
);'); |
| 348 |
#if ($@) {$self->throw($@); } |
| 349 |
|
| 350 |
|
| 351 |
#print "ref:",ref ($matrixobj); |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
} |
| 356 |
|
| 357 |
# print $matrixobj->ID(); |
| 358 |
# print "here\n";print $matrixobj->prettyprint(); |
| 359 |
return ($matrixobj); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
=head2 get_Matrix_by_name |
| 364 |
|
| 365 |
Title : get_Matrix_by_name |
| 366 |
Usage : my $pfm = $db->get_Matrix_by_name('HNF-1'); |
| 367 |
Function: fetches matrix data under the given name from the |
| 368 |
database and returns a TFBS::Matrix::* object |
| 369 |
Returns : a TFBS::Matrix::* object; the exact type of the object |
| 370 |
depending on what form the matrix object was stored in |
| 371 |
the database (default PFM)) |
| 372 |
Args : (Matrix_name) |
| 373 |
|
| 374 |
Warning : According to the current JASPAR4 data model, name is |
| 375 |
not necessarily a unique identifier. In the case where |
| 376 |
there are several matrices with the same name in the |
| 377 |
database, the function fetches the first one and prints |
| 378 |
a warning on STDERR. You've been warned. |
| 379 |
|
| 380 |
=cut |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
sub get_Matrix_by_name { |
| 385 |
my ($self, $name, $mt) = @_; |
| 386 |
unless(defined $name) { |
| 387 |
$self->throw("No name passed to get_Matrix_by_name."); } |
| 388 |
|
| 389 |
my @IDlist = $self->_get_IDlist_by_query(-name=>[$name]); |
| 390 |
my $ID= ($IDlist[0] |
| 391 |
or $self->warn("No matrix with name $name found.")); |
| 392 |
if ((my $L= scalar @IDlist) > 1) { |
| 393 |
$self->warn("There are $L matrices with name '$name'"); |
| 394 |
} |
| 395 |
return $self->get_Matrix_by_ID($ID); |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
=head2 get_MatrixSet |
| 400 |
|
| 401 |
Title : get_MatrixSet |
| 402 |
Usage : my $matrixset = $db->get_MatrixSet(%args); |
| 403 |
Function: fetches matrix data under for all matrices in the database |
| 404 |
matching criteria defined by the named arguments |
| 405 |
and returns a TFBS::MatrixSet object |
| 406 |
Returns : a TFBS::MatrixSet object |
| 407 |
Args : This method accepts named arguments, corresponding to arbitrary tags. |
| 408 |
Note that this is different from JASPAR2. As any tag is supported for |
| 409 |
database storage, any tag can be used for information retrieval. |
| 410 |
Additionally, arguments as 'name' and 'class' can be used (even though |
| 411 |
they are not tags. |
| 412 |
As with get_Matrix methods, it is important to realize that any matrix |
| 413 |
format can be stored in the database: the TFBS::MatrixSet might therefore |
| 414 |
consist of PFMs, ICMs and PWMS, depending on how matrices are stored, |
| 415 |
|
| 416 |
Examples include |
| 417 |
-ID # a reference to an array of IDs (strings) |
| 418 |
-name # a reference to an array of |
| 419 |
# transcription factor names (string) |
| 420 |
-class # a reference to an array of |
| 421 |
# structural class names (strings) |
| 422 |
-species # a reference to an array of |
| 423 |
# Latin species names (strings) |
| 424 |
-sysgroup # a reference to an array of |
| 425 |
# higher taxonomic categories (strings) |
| 426 |
|
| 427 |
|
| 428 |
-min_ic # float, minimum total information content |
| 429 |
# of the matrix. IMPORTANT:if retrieved matrices are in PWM |
| 430 |
format there is no way to measureinformation content. |
| 431 |
-matrixtype #string describing type of matrix to retrieve. If left out, the format |
| 432 |
will revert to the database format. Note that this option only works |
| 433 |
if the database format is pfm |
| 434 |
|
| 435 |
The arguments that expect list references are used in database |
| 436 |
query formulation: elements within lists are combined with 'OR' |
| 437 |
operators, and the lists of different types with 'AND'. For example, |
| 438 |
|
| 439 |
my $matrixset = $db->(-class => ['TRP_CLUSTER', 'FORKHEAD'], |
| 440 |
-species => ['Homo sapiens', 'Mus musculus'], |
| 441 |
); |
| 442 |
|
| 443 |
gives a set of TFBS::Matrix::PFM objects (given that the matrix models are stored as such) |
| 444 |
whose (structural clas is 'TRP_CLUSTER' OR'FORKHEAD') AND (the species they are derived |
| 445 |
from is 'Homo sapiens'OR 'Mus musculus'). |
| 446 |
|
| 447 |
The -min_ic filter is applied after the query in the sense that the |
| 448 |
matrices profiles with total information content less than specified |
| 449 |
are not included in the set. |
| 450 |
|
| 451 |
=cut |
| 452 |
|
| 453 |
|
| 454 |
sub get_MatrixSet { |
| 455 |
my ($self, %args) = @_; |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
my @IDlist = $self->_get_IDlist_by_query(%args); |
| 460 |
my $type; |
| 461 |
my $matrixset = TFBS::MatrixSet->new(); |
| 462 |
|
| 463 |
foreach (@IDlist) { |
| 464 |
# print "$_\n"; |
| 465 |
} |
| 466 |
|
| 467 |
foreach (@IDlist) { |
| 468 |
#next if (defined $args{'-min_ic'} |
| 469 |
# and $_->_get_total_ic($_) < $args{'-min_ic'}); |
| 470 |
#evaluate total information content: ivolves actually retrieving matrix |
| 471 |
# is actually a problem if matrix is stored PWM: thro an error if so |
| 472 |
|
| 473 |
my $matrix=$self->get_Matrix_by_ID($_); |
| 474 |
#evaluate |
| 475 |
#ugly code: |
| 476 |
if (defined $args{'-min_ic'} ){ |
| 477 |
if ($matrix->isa("TFBS::Matrix::PFM")){ |
| 478 |
next if ( $matrix->to_ICM->total_ic() < $args{'-min_ic'}); |
| 479 |
} |
| 480 |
if ($matrix->isa("TFBS::Matrix::ICM")){ |
| 481 |
next if ($matrix->total_ic() < $args{'-min_ic'}); |
| 482 |
} |
| 483 |
if ($matrix->isa("TFBS::Matrix::PWM")){ |
| 484 |
$self->throw("Cannot evaluate information constent from PWM matrices"); |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
} |
| 491 |
#ugly code: |
| 492 |
|
| 493 |
if ($args{'-matrixtype'} && $matrix->isa("TFBS::Matrix::PFM")){ |
| 494 |
if ( $args{'-matrixtype'} eq ('PWM')) { |
| 495 |
# warn "change"; |
| 496 |
$matrix= $matrix->to_PWM(); |
| 497 |
} |
| 498 |
if ( $args{'-matrixtype'} eq ('ICM')) { |
| 499 |
#warn "change"; |
| 500 |
$matrix= $matrix->to_PWM(); |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
$matrixset->add_Matrix($matrix); |
| 506 |
} |
| 507 |
return $matrixset; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
sub store_MatrixSet { |
| 513 |
$_[0]->throw ("Method store_MtrixSet not yet implemented."); |
| 514 |
} |
| 515 |
|
| 516 |
=head2 delete_Matrix_having_ID |
| 517 |
|
| 518 |
Title : delete_Matrix_having_ID |
| 519 |
Usage : $db->delete_Matrix_with_ID('M00045'); |
| 520 |
Function: Deletes the matrix having the given ID from the database |
| 521 |
Returns : 0 on success; $@ contents on failure |
| 522 |
(this is too C-like and may change in future versions) |
| 523 |
Args : (ID) |
| 524 |
A string |
| 525 |
Comment : Yeah, yeah, 'delete_Matrix_having_ID' is a stupid name |
| 526 |
for a method, but at least it should be obviuos what it does. |
| 527 |
|
| 528 |
=cut |
| 529 |
|
| 530 |
|
| 531 |
sub delete_Matrix_having_ID { |
| 532 |
my ($self, @IDs) = @_; |
| 533 |
eval { |
| 534 |
foreach my $ID (@IDs) { |
| 535 |
my $q_ID = $self->dbh->quote($ID); |
| 536 |
foreach my $table (qw (MATRIX_DATA |
| 537 |
MATRIX_INFO |
| 538 |
MATRIX_ANNOTATION |
| 539 |
) |
| 540 |
) |
| 541 |
{ |
| 542 |
$self->dbh->do("DELETE from $table where ID=$q_ID"); |
| 543 |
} |
| 544 |
} |
| 545 |
}; |
| 546 |
return $@; |
| 547 |
|
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
######################################################################### |
| 553 |
# PRIVATE METHODS |
| 554 |
######################################################################### |
| 555 |
|
| 556 |
|
| 557 |
sub _new { |
| 558 |
my ($caller, %args) = @_; |
| 559 |
my $class = ref $caller || $caller; |
| 560 |
my $self = bless {}, $class; |
| 561 |
|
| 562 |
my ($connectstring, $user, $password); |
| 563 |
|
| 564 |
if ($args{'-connect'} and (ref($args{'-connect'}) eq "ARRAY")) { |
| 565 |
($connectstring, $user, $password) = @{$args{'-connect'}}; |
| 566 |
} |
| 567 |
elsif ($args{'-create'} and (ref($args{'-create'}) eq "ARRAY")) { |
| 568 |
return $caller->create(@{-args{'create'}}); |
| 569 |
} |
| 570 |
else { |
| 571 |
($connectstring, $user, $password) = |
| 572 |
(DEFAULT_CONNECTSTRING, DEFAULT_USER, DEFAULT_PASSWORD); |
| 573 |
} |
| 574 |
|
| 575 |
$self->dbh( DBI->connect($connectstring, $user, $password) ); |
| 576 |
|
| 577 |
return $self; |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
sub _store_matrix_data { |
| 582 |
my ($self, $pfm, $ACTION) = @_; |
| 583 |
my @base = qw(A C G T); |
| 584 |
my $matrix = $pfm->matrix(); |
| 585 |
my $type; |
| 586 |
my $sth = $self->dbh->prepare |
| 587 |
(q! INSERT INTO MATRIX_DATA VALUES(?,?,?,?) !); |
| 588 |
|
| 589 |
for my $i (0..3) { |
| 590 |
for my $j (0..($pfm->length-1)) { |
| 591 |
$sth->execute( $pfm->ID, |
| 592 |
|
| 593 |
$base[$i], |
| 594 |
$j+1, |
| 595 |
$matrix->[$i][$j] |
| 596 |
) |
| 597 |
or $self->throw("Error executing query."); |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
sub _store_matrix_info { |
| 605 |
|
| 606 |
my ($self, $pfm, $ACTION) = @_; |
| 607 |
|
| 608 |
my $type; |
| 609 |
$type= 'PFM' if $pfm->isa("TFBS::Matrix::PFM"); |
| 610 |
$type= 'PWM' if $pfm->isa("TFBS::Matrix::PWM"); |
| 611 |
$type= 'ICM' if $pfm->isa("TFBS::Matrix::ICM"); |
| 612 |
|
| 613 |
|
| 614 |
my $sth = $self->dbh->prepare |
| 615 |
(q! INSERT INTO MATRIX_INFO |
| 616 |
(ID, type) |
| 617 |
VALUES(?,?) !); |
| 618 |
$sth->execute($pfm->ID, |
| 619 |
$type, |
| 620 |
) |
| 621 |
or $self->throw("Error executing query"); |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
sub _store_matrix_annotation { |
| 630 |
my ($self, $pfm, $ACTION) = @_; |
| 631 |
my $sth = $self->dbh->prepare |
| 632 |
(q! INSERT INTO MATRIX_ANNOTATION |
| 633 |
(ID, tag, val) |
| 634 |
VALUES(?,?,?) !); |
| 635 |
|
| 636 |
$sth->execute($pfm->ID, |
| 637 |
'name', |
| 638 |
($pfm->name() or ""), |
| 639 |
); |
| 640 |
$sth->execute($pfm->ID, |
| 641 |
'class', |
| 642 |
($pfm->class() or ""), |
| 643 |
); |
| 644 |
|
| 645 |
# get all tags |
| 646 |
|
| 647 |
my %tags= $pfm->all_tags(); |
| 648 |
foreach my $tag( keys %tags){ |
| 649 |
$sth->execute($pfm->ID, |
| 650 |
$tag, |
| 651 |
($tags{$tag} or ""), |
| 652 |
) |
| 653 |
|
| 654 |
or $self->throw("Error executing query"); |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
#when creating: try to support arbitrary tags |
| 665 |
|
| 666 |
sub _create_tables { |
| 667 |
# utility function |
| 668 |
|
| 669 |
# If you want to change the databse schema, |
| 670 |
# this is the right place to do it |
| 671 |
|
| 672 |
my $dbh = shift; |
| 673 |
|
| 674 |
my @queries = |
| 675 |
( |
| 676 |
q! |
| 677 |
CREATE TABLE MATRIX_DATA( |
| 678 |
ID VARCHAR (16) DEFAULT '' NOT NULL, |
| 679 |
row VARCHAR(1) NOT NULL, |
| 680 |
col TINYINT(3) UNSIGNED NOT NULL, |
| 681 |
val FLOAT, |
| 682 |
PRIMARY KEY (ID, row, col) |
| 683 |
) |
| 684 |
!, |
| 685 |
q! |
| 686 |
CREATE TABLE MATRIX_INFO( |
| 687 |
ID VARCHAR (16) DEFAULT '' NOT NULL PRIMARY KEY , |
| 688 |
type ENUM ('PFM', 'ICM','PWM') DEFAULT 'PFM' NOT NULL |
| 689 |
) |
| 690 |
!, |
| 691 |
|
| 692 |
q! |
| 693 |
CREATE TABLE MATRIX_ANNOTATION( |
| 694 |
ID VARCHAR (16) DEFAULT '' NOT NULL, |
| 695 |
tag VARCHAR(255) DEFAULT '' NOT NULL, |
| 696 |
val TEXT, |
| 697 |
PRIMARY KEY (ID, tag) |
| 698 |
) |
| 699 |
!, |
| 700 |
|
| 701 |
); |
| 702 |
foreach my $query (@queries) { |
| 703 |
$dbh->do($query) |
| 704 |
or die("Error executing the query: $query\n"); |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
sub _get_matrixstring { |
| 711 |
my ($self, $ID) = @_; |
| 712 |
#my %dbname = (PWM => 'pwm', PFM => 'raw', ICM => 'info'); |
| 713 |
#unless (defined $dbname{$mt}) { |
| 714 |
#$self->throw("Unsupported matrix type: ".$mt); |
| 715 |
#} |
| 716 |
my $sth; |
| 717 |
my $qID = $self->dbh->quote($ID); |
| 718 |
my $matrixstring = ""; |
| 719 |
foreach my $base (qw(A C G T)) { |
| 720 |
$sth=$self->dbh->prepare |
| 721 |
("SELECT val FROM MATRIX_DATA |
| 722 |
WHERE ID=$qID AND row='$base' ORDER BY col"); |
| 723 |
$sth->execute; |
| 724 |
$matrixstring .= |
| 725 |
join (" ", (map {$_->[0]} @{$sth->fetchall_arrayref()}))."\n"; |
| 726 |
} |
| 727 |
$sth->finish; |
| 728 |
return undef if $matrixstring eq "\n"x4; |
| 729 |
|
| 730 |
return $matrixstring; |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
sub _get_IDlist_by_query { |
| 738 |
|
| 739 |
# called by get_MatrixSet |
| 740 |
# should be able to search for arbitrary tags...hmmm |
| 741 |
my ($self, %args) = @_; |
| 742 |
my ($TABLES, %arrayref); |
| 743 |
my (%intersected_set); |
| 744 |
|
| 745 |
|
| 746 |
foreach my $key(keys %args){ |
| 747 |
|
| 748 |
unless ( $key eq "-min_ic" or $key eq "-matrixtype"){ |
| 749 |
my $oldkey=$key; |
| 750 |
$key=~s/-//; |
| 751 |
$arrayref{$key}= $args{$oldkey}; |
| 752 |
} |
| 753 |
} |
| 754 |
my @andconditions; |
| 755 |
|
| 756 |
|
| 757 |
$TABLES = 'MATRIX_ANNOTATION '; |
| 758 |
|
| 759 |
#special case: get all matrices |
| 760 |
unless (keys %arrayref){ |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
my $query = "SELECT DISTINCT ID FROM $TABLES "; |
| 765 |
|
| 766 |
my $sth = $self->dbh->prepare($query); |
| 767 |
$sth->execute() or $self->throw("Query failed:\n$query\n"); |
| 768 |
my @ary; |
| 769 |
|
| 770 |
while (my ($id) = $sth->fetchrow_array()) { |
| 771 |
push (@ary, $id); |
| 772 |
|
| 773 |
} |
| 774 |
|
| 775 |
return(@ary); |
| 776 |
|
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
foreach my $key (keys %arrayref) { |
| 784 |
#print "key: $key\n"; |
| 785 |
if ($key eq 'ID'){ |
| 786 |
push @andconditions, |
| 787 |
"(". |
| 788 |
join(" OR ", |
| 789 |
(map {"MATRIX_ANNOTATION.ID=". |
| 790 |
$self->dbh->quote($_) |
| 791 |
} |
| 792 |
@{$arrayref{$key}} |
| 793 |
)). |
| 794 |
")"; |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
} |
| 799 |
else{ |
| 800 |
push @andconditions, |
| 801 |
"(". |
| 802 |
join(" OR ", |
| 803 |
(map {"MATRIX_ANNOTATION.tag=". $self->dbh->quote($key)." AND val=". |
| 804 |
$self->dbh->quote($_) |
| 805 |
} |
| 806 |
@{$arrayref{$key}} |
| 807 |
)). |
| 808 |
")"; |
| 809 |
} |
| 810 |
push (@andconditions, 1) unless(@andconditions); |
| 811 |
my $WHERE = ((scalar @andconditions) == 0) ? "" : " WHERE "; |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
my $query = |
| 816 |
"SELECT DISTINCT ID FROM $TABLES $WHERE". |
| 817 |
join(" AND ", @andconditions); |
| 818 |
|
| 819 |
# warn $query; |
| 820 |
undef @andconditions; |
| 821 |
my $sth = $self->dbh->prepare($query); |
| 822 |
$sth->execute() or $self->throw("Query failed:\n$query\n"); |
| 823 |
|
| 824 |
# collect IDs and return |
| 825 |
|
| 826 |
my %current_query; |
| 827 |
while (my ($id) = $sth->fetchrow_array()) { |
| 828 |
$current_query{$id}=1; |
| 829 |
|
| 830 |
} |
| 831 |
unless (%intersected_set){ |
| 832 |
%intersected_set= %current_query; |
| 833 |
next; |
| 834 |
} |
| 835 |
# do intersect |
| 836 |
foreach my $key (keys %intersected_set){ |
| 837 |
delete $intersected_set{ $key} unless $current_query{$key}; |
| 838 |
|
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
my @ary; |
| 843 |
foreach my $key (keys %intersected_set){ |
| 844 |
push (@ary, $key); |
| 845 |
# warn "$key\n"; |
| 846 |
} |
| 847 |
return (@ary); |
| 848 |
|
| 849 |
|
| 850 |
} |
| 851 |
|
| 852 |
sub DESTROY { |
| 853 |
$_[0]->dbh->disconnect() if $_[0]->dbh; |
| 854 |
} |
| 855 |
|
| 856 |
|
| 857 |
|