| 1 |
# TFBS module for TFBS::DB::JASPAR5 |
| 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::JASPAR5 - interface to MySQL relational database of pattern matrices. Currently status: experimental. |
| 13 |
|
| 14 |
|
| 15 |
=head1 SYNOPSIS |
| 16 |
|
| 17 |
=over 4 |
| 18 |
|
| 19 |
=item * creating a database object by connecting to the existing JASPAR5-type database |
| 20 |
|
| 21 |
my $db = TFBS::DB::JASPAR5->connect("dbi:mysql:JASPAR5: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 |
@namelist = ('p50', 'p53', 'HNF-1'. 'GATA-1', 'GATA-2', 'GATA-3'); |
| 42 |
my $matrixset = $db->get_MatrixSet(-names => \@namelist, |
| 43 |
-matrixtype => "ICM"); |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
=item * creating a new JASPAR5-type database named MYJASPAR5: |
| 48 |
|
| 49 |
my $db = TFBS::DB::JASPAR4->create("dbi:mysql:MYJASPAR5:myhost", |
| 50 |
"myusername", |
| 51 |
"mypassword"); |
| 52 |
|
| 53 |
=item * storing a matrix in the database (currently only PFMs): |
| 54 |
|
| 55 |
#let $pfm is a TFBS::Matrix::PFM object |
| 56 |
$db->store_Matrix($pfm); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
=back |
| 61 |
|
| 62 |
=head1 DESCRIPTION |
| 63 |
|
| 64 |
TFBS::DB::JASPAR5 is a read/write database interface module that |
| 65 |
retrieves and stores TFBS::Matrix::* and TFBS::MatrixSet |
| 66 |
objects in a relational database. The interface is nearly identical |
| 67 |
to the JASPAR2 and JASPAR4 interface, while the underlying data model is different |
| 68 |
|
| 69 |
|
| 70 |
=head1 JASPAR5 DATA MODEL |
| 71 |
|
| 72 |
JASPAR5 is working name for a relational database model used |
| 73 |
for storing transcriptional factor pattern matrices in a MySQL database. |
| 74 |
It was initially designed (JASPAR2) to store matrices for the JASPAR database of |
| 75 |
high quality eukaryotic transcription factor specificity profiles by |
| 76 |
Albin Sandelin and Wyeth W. Wasserman. Besides the profile matrix itself, |
| 77 |
this data model stores profile ID (unique), name, structural class, |
| 78 |
basic taxonomic and bibliographic information |
| 79 |
as well as some additional, and custom, tags. |
| 80 |
|
| 81 |
Here goes a moore thorough description on tables and IDs |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
----------------------- ADVANCED --------------------------------- |
| 87 |
|
| 88 |
For the developers and the curious, here is the JASPAR5 data model: |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
MISSING TEXT HEER ON HOW IT WORKS |
| 93 |
|
| 94 |
|
| 95 |
It is our best intention to hide the details of this data model, which we |
| 96 |
are using on a daily basis in our work, from most TFBS users. |
| 97 |
Most users should only know the methods to store the data and |
| 98 |
which tags are supported. |
| 99 |
|
| 100 |
------------------------------------------------------------------------- |
| 101 |
|
| 102 |
|
| 103 |
=head1 FEEDBACK |
| 104 |
|
| 105 |
Please send bug reports and other comments to the author. |
| 106 |
|
| 107 |
=head1 AUTHOR - Boris Lenhard |
| 108 |
|
| 109 |
Boris Lenhard E<lt>[email protected]<gt> |
| 110 |
|
| 111 |
=head1 APPENDIX |
| 112 |
|
| 113 |
The rest of the documentation details each of the object |
| 114 |
methods. Internal methods are preceded with an underscore. |
| 115 |
|
| 116 |
=cut |
| 117 |
|
| 118 |
|
| 119 |
# The code begins HERE: |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
package TFBS::DB::JASPAR5; |
| 124 |
use vars qw(@ISA $AUTOLOAD); |
| 125 |
|
| 126 |
# we need all three matrices due to the redundancy in JASPAR2 data model |
| 127 |
# which will hopefully be removed in JASPAR3 |
| 128 |
|
| 129 |
use TFBS::Matrix::PWM; |
| 130 |
use TFBS::Matrix::PFM; |
| 131 |
use TFBS::Matrix::ICM; |
| 132 |
|
| 133 |
use TFBS::MatrixSet; |
| 134 |
use Bio::Root::Root; |
| 135 |
use DBI; |
| 136 |
# use TFBS::DB; # eventually |
| 137 |
use strict; |
| 138 |
|
| 139 |
@ISA = qw(TFBS::DB Bio::Root::Root); |
| 140 |
|
| 141 |
######################################################################### |
| 142 |
# CONSTANTS |
| 143 |
######################################################################### |
| 144 |
|
| 145 |
use constant DEFAULT_CONNECTSTRING => "dbi:mysql:JASPAR_DEMO"; # on localhost |
| 146 |
use constant DEFAULT_USER => ""; |
| 147 |
use constant DEFAULT_PASSWORD => ""; |
| 148 |
|
| 149 |
######################################################################### |
| 150 |
# PUBLIC METHODS |
| 151 |
######################################################################### |
| 152 |
|
| 153 |
=head2 new |
| 154 |
|
| 155 |
Title : new |
| 156 |
Usage : DEPRECATED - for backward compatibility only |
| 157 |
Use connect() or create() instead |
| 158 |
|
| 159 |
=cut |
| 160 |
|
| 161 |
sub new { |
| 162 |
_new (@_); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
=head2 connect |
| 167 |
|
| 168 |
Title : connect |
| 169 |
Usage : my $db = |
| 170 |
TFBS::DB::JASPAR5->connect("dbi:mysql:DATABASENAME:HOSTNAME", |
| 171 |
"USERNAME", |
| 172 |
"PASSWORD"); |
| 173 |
Function: connects to the existing JASPAR5-type database and |
| 174 |
returns a database object that interfaces the database |
| 175 |
Returns : a TFBS::DB::JASPAR5 object |
| 176 |
Args : a standard database connection triplet |
| 177 |
("dbi:mysql:DATABASENAME:HOSTNAME", "USERNAME", "PASSWORD") |
| 178 |
In place of DATABASENAME, HOSTNAME, USERNAME and PASSWORD, |
| 179 |
use the actual values. PASSWORD and USERNAME might be |
| •180 |
optional, depending on the user's access permissions for |
| 181 |
the database server. |
| 182 |
|
| 183 |
=cut |
| 184 |
|
| 185 |
sub connect { #DONE |
| 186 |
# a more intuitive syntax for the constructor |
| 187 |
my ($caller, @connection_args) = @_; |
| 188 |
$caller->new(-connect => \@connection_args); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
=head2 dbh |
| 193 |
|
| 194 |
Title : dbh |
| 195 |
Usage : my $dbh = $db->dbh(); |
| 196 |
$dbh->do("UPDATE matrix_data SET name='ADD1' WHERE NAME='SREBP2'"); |
| 197 |
Function: returns the DBI database handle of the MySQL database |
| 198 |
interfaced by $db; THIS IS USED FOR WRITING NEW METHODS |
| 199 |
FOR DIRECT RELATIONAL DATABASE MANIPULATION - if you |
| 200 |
have write access AND do not know what you are doing, |
| 201 |
you can severely corrupt the data |
| 202 |
For documentation about database handle methods, see L<DBI> |
| 203 |
Returns : the database (DBI) handle of the MySQL JASPAR2-type |
| 204 |
relational database associated with the TFBS::DB::JASPAR2 |
| 205 |
object |
| 206 |
Args : none |
| 207 |
|
| 208 |
=cut |
| 209 |
|
| 210 |
sub dbh { #DONE |
| 211 |
my ($self, $dbh) = @_; |
| 212 |
$self->{'dbh'} = $dbh if $dbh; |
| 213 |
return $self->{'dbh'}; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
=head2 store_Matrix |
| 222 |
|
| 223 |
Title : store_Matrix |
| 224 |
Usage : $db->store_Matrix($matrixobject); |
| 225 |
Function: Stores the contents of a TFBS::Matrix::DB object in the database |
| 226 |
Returns : 0 on success; $@ contents on failure |
| 227 |
(this is too C-like and may change in future versions) |
| 228 |
Args : (PFM_object) |
| 229 |
A TFBS::Matrix::PFM, FBS::Matrix::PWM or FBS::Matrix::ICM object. |
| 230 |
PFM object are recommended to use, as they are eaily converted to |
| 231 |
other formats |
| 232 |
# might have to give version and collection here |
| 233 |
Comment : this is an experimental method that is not 100% bulletproof; |
| 234 |
use at your own risk |
| 235 |
|
| 236 |
=cut |
| 237 |
|
| 238 |
|
| 239 |
sub store_Matrix { #PROBABLY DONE |
| 240 |
# collection, version are taken from the corresponding tags. Warn if they are not there |
| 241 |
; |
| 242 |
my ($self, @PFMs) = @_; |
| 243 |
my $err; |
| 244 |
foreach my $pfm (@PFMs) { |
| 245 |
eval { |
| 246 |
|
| 247 |
my $int_id= $self->_store_matrix($pfm); # needs to have collection and version |
| 248 |
$self->_store_matrix_data($pfm, $int_id); |
| 249 |
$self->_store_matrix_annotation($pfm, $int_id); |
| 250 |
$self->_store_matrix_species($pfm, $int_id); |
| 251 |
$self->_store_matrix_acc($pfm, $int_id); |
| 252 |
|
| 253 |
}; |
| 254 |
} |
| 255 |
return $@; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
sub create { #done |
| 260 |
my ($caller, $connectstring, $user, $password) = @_; |
| 261 |
if ($connectstring |
| 262 |
and $connectstring =~ /dbi:mysql:(\w+)(.*)/) |
| 263 |
{ |
| 264 |
# connect to the server; |
| 265 |
my $dbh=DBI->connect("dbi:mysql:mysql".$2, |
| 266 |
$user,$password) |
| 267 |
or die("Error connecting to the database"); |
| 268 |
|
| 269 |
# create database and open it |
| 270 |
$dbh->do("create database $1") |
| 271 |
or die("Error creating database."); |
| 272 |
$dbh->do("use $1"); |
| 273 |
|
| 274 |
# create tables |
| 275 |
_create_tables($dbh); |
| 276 |
|
| 277 |
$dbh->disconnect; |
| 278 |
|
| 279 |
# run "new" with new database |
| 280 |
|
| 281 |
return $caller->new(-connect=>[$connectstring, $user, $password]); |
| 282 |
} |
| 283 |
else { |
| 284 |
die("Missing or malformed connect string for ". |
| 285 |
"TFBS::DB::JASPAR2 connection."); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
=head2 get_Matrix_by_ID |
| 291 |
|
| 292 |
Title : get_Matrix_by_ID |
| 293 |
Usage : my $pfm = $db->get_Matrix_by_ID('M00034', 'PFM'); |
| 294 |
Function: fetches matrix data under the given ID from the |
| 295 |
database and returns a TFBS::Matrix::* object |
| 296 |
Returns : a TFBS::Matrix::* object; the exact type of the |
| 297 |
object depending on what form the matrix is stored |
| 298 |
in the database (PFM is default) |
| 299 |
Args : (Matrix_ID) |
| 300 |
Matrix_ID id is a string which refers to the stable |
| 301 |
JASPAR ID (usually something like "MA0001") with |
| 302 |
or without version numbers. "MA0001" will give the |
| 303 |
latest version on MA0001, while "MA0001.2" will give |
| 304 |
the second version, if existing. Warnings will be |
| 305 |
given for non-existing matrices. |
| 306 |
|
| 307 |
=cut |
| 308 |
|
| 309 |
|
| 310 |
sub get_Matrix_by_ID { #DONE. MAYBE :) |
| 311 |
|
| 312 |
my ($self, $q, $mt) = @_; # q is a stable ID with possible version number |
| 313 |
$mt = (uc($mt) or "PFM"); |
| 314 |
unless (defined $q) { |
| 315 |
$self->throw("No ID passed to get_Matrix_by_ID"); |
| 316 |
} |
| 317 |
|
| 318 |
my $ucmt = uc $mt; |
| 319 |
# separate stable ID and version number |
| 320 |
my ($base_ID, $version)= split (/\./, $q); |
| 321 |
$version=$self->_get_latest_version($base_ID) unless $version; # latest version per default |
| 322 |
|
| 323 |
# get internal ID - also a check for validity |
| 324 |
my $int_id= $self->_get_internal_id($base_ID, $version); |
| 325 |
|
| 326 |
|
| 327 |
# get matrix using internal ID |
| 328 |
|
| 329 |
my $m= $self->_get_Matrix_by_int_id($int_id, $ucmt); |
| 330 |
warn ref ($m); |
| 331 |
|
| 332 |
return ($m); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
=head2 get_Matrix_by_name |
| 337 |
|
| 338 |
Title : get_Matrix_by_name |
| 339 |
Usage : my $pfm = $db->get_Matrix_by_name('HNF-1'); |
| 340 |
Function: fetches matrix data under the given name from the |
| 341 |
database and returns a TFBS::Matrix::* object |
| 342 |
Returns : a TFBS::Matrix::* object; the exact type of the object |
| 343 |
depending on what form the matrix object was stored in |
| 344 |
the database (default PFM)) |
| 345 |
Args : (Matrix_name) |
| 346 |
|
| 347 |
Warning : According to the current JASPAR5 data model, name is |
| 348 |
not necessarily a unique identifier. Also, names change |
| 349 |
over time. |
| 350 |
In the case where |
| 351 |
there are several matrices with the same name in the |
| 352 |
database, the function fetches the first one and prints |
| 353 |
a warning on STDERR. You've been warned. |
| 354 |
Some matrices have multiple versions. The function will |
| 355 |
return the latest version. For specific versions, use |
| 356 |
get_Matrix_by_ID($ID.$version) |
| 357 |
|
| 358 |
=cut |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
sub get_Matrix_by_name { #DONE |
| 363 |
my ($self, $name, $mt) = @_; |
| 364 |
unless(defined $name) { |
| 365 |
$self->throw("No name passed to get_Matrix_by_name."); } |
| 366 |
# sanity check: are there many different stable IDs with same name? |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
my $sth=$self->dbh->prepare(qq!SELECT distinct BASE_ID FROM MATRIX |
| 371 |
WHERE NAME="$name"!); |
| 372 |
$sth->execute(); |
| 373 |
my (@stable_ids)=$sth->fetchrow_array(); |
| 374 |
my $L =scalar @stable_ids; |
| 375 |
$self->warn("There are $L distinct stable IDs with name '$name'") if scalar $L>1; |
| 376 |
|
| 377 |
return $self->get_Matrix_by_ID($stable_ids[0], $mt); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
=head2 get_MatrixSet |
| 382 |
|
| 383 |
Title : get_MatrixSet |
| 384 |
Usage : my $matrixset = $db->get_MatrixSet(%args); |
| 385 |
Function: fetches matrix data under for all matrices in the database |
| 386 |
matching criteria defined by the named arguments |
| 387 |
and returns a TFBS::MatrixSet object |
| 388 |
Returns : a TFBS::MatrixSet object |
| 389 |
Args : This method accepts named arguments, corresponding to arbitrary tags, and also some utility functions |
| 390 |
Note that this is different from JASPAR2 and to some extent JASPAR4. As any tag is supported for |
| 391 |
database storage, any tag can be used for information retrieval. |
| 392 |
Additionally, arguments as 'name','class','collection' can be used (even though |
| 393 |
they are not tags. |
| 394 |
Per default, only the last version of the matrix is given. The only way to get older matrices out of this |
| 395 |
to use an array of IDs with actual versions like MA0001.1, or set the argyment -all_versions=>1, in which case you get all versions for each stable ID |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
Examples include: |
| 401 |
Fundamental matrix features |
| 402 |
-all # gives absolutely all matrix entry, regardless of versin and collection. Only useful for backup situations and sanity checks. Takes precedence over everything else |
| 403 |
|
| 404 |
-ID # a reference to an array of stable IDs (strings), with or without version, as above. tyically something like "MA0001.2" . Takes precedence over everything salve -all |
| 405 |
-name # a reference to an array of |
| 406 |
# transcription factor names (string). Will only take latest version. NOT a preferred way to access since names change over time |
| 407 |
-collection # a string corresponding to a JASPAR collection. Per default CORE |
| 408 |
-all_versions # gives all matrix versions that fit with rest of criteria, including obsolete ones.Is off per default. |
| 409 |
# Typical usage is in combiation with a stable IDs withou versions to get all versinos of a particular matrix |
| 410 |
Typical tag queries: |
| 411 |
These can be either a string or a reference to an array of strings. If it is an arrau it will be interpreted as as an "or"s statement |
| 412 |
-class # a reference to an array of |
| 413 |
# structural class names (strings) |
| 414 |
-species # a reference to an array of |
| 415 |
# NCBI Taxonomy IDs (integers) |
| 416 |
-taxgroup # a reference to an array of |
| 417 |
# higher taxonomic categories (string) |
| 418 |
|
| 419 |
Computed features of the matrices |
| 420 |
-min_ic # float, minimum total information content |
| 421 |
# of the matrix. |
| 422 |
-matrixtype #string describing type of matrix to retrieve. If left out, the format |
| 423 |
will revert to the database format, which is PFM. |
| 424 |
|
| 425 |
The arguments that expect list references are used in database |
| 426 |
query formulation: elements within lists are combined with 'OR' |
| 427 |
operators, and the lists of different types with 'AND'. For example, |
| 428 |
|
| 429 |
my $matrixset = $db->(-class => ['TRP_CLUSTER', 'FORKHEAD'], |
| 430 |
-species => ['Homo sapiens', 'Mus musculus'], |
| 431 |
); |
| 432 |
|
| 433 |
gives a set of TFBS::Matrix::PFM objects (given that the matrix models are stored as such) |
| 434 |
whose (structural clas is 'TRP_CLUSTER' OR'FORKHEAD') AND (the species they are derived |
| 435 |
from is 'Homo sapiens'OR 'Mus musculus'). |
| 436 |
|
| 437 |
As above, unless IDs with version numbers are used, only one matrix per stable ID wil be returned: the matrix with the highest version number |
| 438 |
|
| 439 |
The -min_ic filter is applied after the query in the sense that the |
| 440 |
matrices profiles with total information content less than specified |
| 441 |
are not included in the set. |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
=cut |
| 446 |
|
| 447 |
|
| 448 |
sub get_MatrixSet { # IC conetent and matrix stuff is not there yet, rest should work |
| 449 |
my ($self, %args) = @_; |
| 450 |
$args{'-collection'}='CORE' unless $args{'-collection'}; |
| 451 |
|
| 452 |
|
| 453 |
$args{'-all_versions'}=0 unless $args{'-all_versions'}; |
| 454 |
|
| 455 |
|
| 456 |
my @IDlist = @{$self->_get_IDlist_by_query(%args)}; # the IDlist here are INTERNAL ids |
| 457 |
|
| 458 |
|
| 459 |
my $type; |
| 460 |
my $matrixset = TFBS::MatrixSet->new(); |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
foreach my $int_id(@IDlist) { |
| 465 |
|
| 466 |
|
| 467 |
my $matrix=$self->_get_Matrix_by_int_id($int_id); |
| 468 |
|
| 469 |
|
| 470 |
if (defined $args{'-min_ic'} ){ |
| 471 |
# we assume the matrix IS a PFM, o something in normal space at least |
| 472 |
# unless it explicitly says otherwise in tag=matrixtype |
| 473 |
# if so warn and do not use IC content |
| 474 |
# this is not foolproof in any way |
| 475 |
if ( $matrix->{tags}{matrixtype} eq "ICM"){ |
| 476 |
next if ( $matrix->total_ic() < $args{'-min_ic'}); |
| 477 |
} |
| 478 |
elsif ($matrix->isa("TFBS::Matrix::PFM")){ |
| 479 |
next if ( $matrix->to_ICM->total_ic() < $args{'-min_ic'}); |
| 480 |
} |
| 481 |
|
| 482 |
else{ |
| 483 |
warn "Warning: you are assessning information content on matrices that are not in PFM or ICM format.Skipping this criteria"; |
| 484 |
next; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
} |
| 489 |
|
| 490 |
# length |
| 491 |
if (defined $args{'-length'} ){ |
| 492 |
next if ( $matrix->length() < $args{'-length'}); |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
# number of sites within |
| 497 |
# since column sums MIGHT be slightly different we take the integer of the mean of the columns |
| 498 |
# or really int( sum of matrix/#columns) |
| 499 |
if (defined $args{'-sites'} ){ |
| 500 |
my $sum=0; |
| 501 |
foreach ( 1..$matrix->length){ |
| 502 |
$sum+=$matrix->column_sum(); |
| 503 |
} |
| 504 |
$sum=int($sum /$matrix->length); |
| 505 |
warn $matrix->ID, " $sum is $sum"; |
| 506 |
next if ( $sum < $args{'-sites'}); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
#ugly code: think about this a bit. |
| 513 |
|
| 514 |
if ($args{'-matrixtype'} && $matrix->isa("TFBS::Matrix::PFM")){ |
| 515 |
if ( $args{'-matrixtype'} eq ('PWM')) { |
| 516 |
|
| 517 |
$matrix= $matrix->to_PWM(); |
| 518 |
} |
| 519 |
if ( $args{'-matrixtype'} eq ('ICM')) { |
| 520 |
|
| 521 |
$matrix= $matrix->to_PWM(); |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
$matrixset->add_Matrix($matrix); |
| 527 |
} |
| 528 |
return $matrixset; |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
sub store_MatrixSet { #DONE a wrapper around store_Matrix (which also can take an array of matrices, so utility only |
| 534 |
my ($self, $matrixset) = @_; |
| 535 |
my $it=$matrixset->Iterator(); |
| 536 |
while (my $matrix_object = $it->next) { |
| 537 |
# do whatever you want with individual matrix objects |
| 538 |
$self->store_Matrix($matrix_object) |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
} |
| 543 |
|
| 544 |
=head2 delete_Matrix_having_ID |
| 545 |
|
| 546 |
Title : delete_Matrix_having_ID |
| 547 |
Usage : $db->delete_Matrix_with_ID('M00045.1'); |
| 548 |
Function: Deletes the matrix having the given ID from the database |
| 549 |
Returns : 0 on success; $@ contents on failure |
| 550 |
(this is too C-ike and may change in future versions) |
| 551 |
Args : (ID) |
| 552 |
A string. Has to be a matrix ID with version suffix in JASPAR5. |
| 553 |
Comment : Yeah, yeah, 'delete_Matrix_having_ID' is a stupid name |
| 554 |
for a method, but at least it should be obviuos what it does. |
| 555 |
|
| 556 |
=cut |
| 557 |
|
| 558 |
|
| 559 |
sub delete_Matrix_having_ID { |
| 560 |
my ($self, @IDs) = @_; |
| 561 |
# this has to be versioned IDs |
| 562 |
foreach my $ID (@IDs){ |
| 563 |
my ($base_id, $version)= split (/\./, $ID); |
| 564 |
unless ($version) { |
| 565 |
warn "You have supplied a non-versioned matrix ID to delete. Skipping $ID "; |
| 566 |
return 0; |
| 567 |
} |
| 568 |
# get relevant internal ID |
| 569 |
my($int_id)= $self->_get_internal_id($base_id, $version); |
| 570 |
|
| 571 |
eval { |
| 572 |
my $q_ID = $self->dbh->quote($int_id); |
| 573 |
foreach my $table (qw (MATRIX_DATA |
| 574 |
MATRIX |
| 575 |
MATRIX_SPECIES |
| 576 |
MATRIX_PROTEIN |
| 577 |
MATRIX_ANNOTATION |
| 578 |
) |
| 579 |
) |
| 580 |
{ |
| 581 |
$self->dbh->do("DELETE from $table where ID=$q_ID"); |
| 582 |
} |
| 583 |
}; |
| 584 |
} |
| 585 |
return $@; |
| 586 |
|
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
######################################################################### |
| 592 |
# PRIVATE METHODS |
| 593 |
######################################################################### |
| 594 |
|
| 595 |
|
| 596 |
sub _new { #PROBABLY OK |
| 597 |
my ($caller, %args) = @_; |
| 598 |
my $class = ref $caller || $caller; |
| 599 |
my $self = bless {}, $class; |
| 600 |
|
| 601 |
my ($connectstring, $user, $password); |
| 602 |
|
| 603 |
if ($args{'-connect'} and (ref($args{'-connect'}) eq "ARRAY")) { |
| 604 |
($connectstring, $user, $password) = @{$args{'-connect'}}; |
| 605 |
} |
| 606 |
elsif ($args{'-create'} and (ref($args{'-create'}) eq "ARRAY")) { |
| 607 |
return $caller->create(@{-args{'create'}}); |
| 608 |
} |
| 609 |
else { |
| 610 |
($connectstring, $user, $password) = |
| 611 |
(DEFAULT_CONNECTSTRING, DEFAULT_USER, DEFAULT_PASSWORD); |
| 612 |
} |
| 613 |
|
| 614 |
$self->dbh( DBI->connect($connectstring, $user, $password) ); |
| 615 |
|
| 616 |
return $self; |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
sub _store_matrix_data {# DONE |
| 621 |
my ($self, $pfm, $int_id,$ACTION) = @_; |
| 622 |
my @base = qw(A C G T); |
| 623 |
my $matrix = $pfm->matrix(); |
| 624 |
my $type; |
| 625 |
my $sth = $self->dbh->prepare |
| 626 |
(q! INSERT INTO MATRIX_DATA VALUES(?,?,?,?) !); |
| 627 |
|
| 628 |
for my $i (0..3) { |
| 629 |
for my $j (0..($pfm->length-1)) { |
| 630 |
$sth->execute( $int_id, |
| 631 |
|
| 632 |
$base[$i], |
| 633 |
$j+1, |
| 634 |
$matrix->[$i][$j] |
| 635 |
) |
| 636 |
or $self->throw("Error executing query."); |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
sub _store_matrix { #DONE |
| 644 |
|
| 645 |
my ($self, $pfm, $ACTION) = @_; |
| 646 |
# creation of the matrix will also give an internal unique ID (incremental int) |
| 647 |
# which will be returned to use for the other tables |
| 648 |
|
| 649 |
# Get collection and versio from the matrix tags |
| 650 |
my $version= $pfm->{'tags'}{'version'}; |
| 651 |
# will warn but not die if version is missing: will assume 1 |
| 652 |
unless ($version) { |
| 653 |
warn "WARNING: Lacking version number for ". $pfm->ID. ". Setting version=1"; |
| 654 |
$version=1; |
| 655 |
|
| 656 |
} |
| 657 |
|
| 658 |
my $collection= $pfm->{'tags'}{'collection'}; |
| 659 |
unless ($collection) { |
| 660 |
warn "WARNING: Lacking collection name for ". $pfm->ID. ". Setting collection to an empty string. You probably do not want this"; |
| 661 |
$collection=''; |
| 662 |
} |
| 663 |
# sanity check: do we alsready have this cobination of base ID and version? If we do, die |
| 664 |
my $base_id= $pfm->ID ; |
| 665 |
my $sth = $self->dbh->prepare |
| 666 |
(qq! select count(*) from MATRIX where VERSION=$version and BASE_ID= "$base_id"and collection="$collection" !); |
| 667 |
$sth->execute; |
| 668 |
my ($sanity_count)= $sth->fetchrow_array; |
| 669 |
|
| 670 |
if ($sanity_count >0){ |
| 671 |
warn "WARNING: Database input inconsistency: You have already have $sanity_count $base_id matrices of version $version in collection $collection. Terminating program"; |
| 672 |
die; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
# insert data |
| 680 |
|
| 681 |
|
| 682 |
$sth = $self->dbh->prepare |
| 683 |
(q! INSERT INTO MATRIX |
| 684 |
VALUES(?,?,?,?,?) !); |
| 685 |
|
| 686 |
# update next sth with actual version and collection: DO |
| 687 |
$sth->execute(0, $collection,$pfm->ID,$version,$pfm->name) |
| 688 |
or $self->throw("Error executing query"); |
| 689 |
|
| 690 |
# get the actual (new) iternal ID |
| 691 |
|
| 692 |
my $int_id = $self->dbh->{ q{mysql_insertid}}; |
| 693 |
|
| 694 |
return $int_id; |
| 695 |
|
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
sub _store_matrix_annotation { # DONE |
| 704 |
#this is for tag-value items that are not one-to-many (so, not species and not acc) |
| 705 |
|
| 706 |
my ($self, $pfm, $int_id,$ACTION) = @_; |
| 707 |
my $sth = $self->dbh->prepare |
| 708 |
(q! INSERT INTO MATRIX_ANNOTATION |
| 709 |
(ID, tag, val) |
| 710 |
VALUES(?,?,?) !); |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
# get all tags |
| 716 |
# but skip out collection or version as we already have those in the MATRIX table |
| 717 |
|
| 718 |
#special handling for class which mighht have a true slot |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
my %tags= $pfm->all_tags(); |
| 723 |
if (defined ($pfm->{class})){ |
| 724 |
$tags{class}=$pfm->{class} ; |
| 725 |
} |
| 726 |
|
| 727 |
foreach my $tag( keys %tags){ |
| 728 |
next if $tag eq "collection"; |
| 729 |
next if $tag eq "version"; |
| 730 |
next if $tag eq "species"; |
| 731 |
next if $tag eq "acc"; |
| 732 |
# next if $tag eq "class"; |
| 733 |
$sth->execute($int_id, |
| 734 |
$tag, |
| 735 |
($tags{$tag} or ""), |
| 736 |
) |
| 737 |
|
| 738 |
or $self->throw("Error executing query"); |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
sub _store_matrix_species { # DONE |
| 744 |
#these are for species IDs - can be several |
| 745 |
# these are taken from the tag "species" |
| 746 |
# if that tag is a reference to an array we walk over the array |
| 747 |
# if it is a comma-separated string we split the string |
| 748 |
|
| 749 |
my ($self, $pfm, $int_id,$ACTION) = @_; |
| 750 |
my $sth = $self->dbh->prepare |
| 751 |
(q! INSERT INTO MATRIX_SPECIES |
| 752 |
VALUES(?,?) !); |
| 753 |
|
| 754 |
#sanity check: are there any species? Its ok not to have it. |
| 755 |
return() unless $pfm->{'tags'}{'species'}; |
| 756 |
#is the species a string or an arrayref? |
| 757 |
if ( ref ($pfm->{'tags'}{'species'}) eq 'ARRAY'){ |
| 758 |
# walkthru array |
| 759 |
foreach my $species ( @{$pfm->{'tags'}{'species'}}){ |
| 760 |
$sth->execute($int_id,$species); |
| 761 |
} |
| 762 |
} |
| 763 |
else{ |
| 764 |
# split and walk thru |
| 765 |
foreach my $species ( split(/\,/, $pfm->{'tags'}{'species'})){ |
| 766 |
$species=~s/^\s//g; |
| 767 |
$sth->execute($int_id,$species); |
| 768 |
} |
| 769 |
|
| 770 |
} |
| 771 |
|
| 772 |
} |
| 773 |
|
| 774 |
sub _store_matrix_acc { # DONE |
| 775 |
#these are for protein accession numbers - can be several |
| 776 |
# these are taken from the tag "acc" |
| 777 |
# if that tag is a reference to an array we walk over the array |
| 778 |
# if it is a comma-separated string we split the string |
| 779 |
|
| 780 |
my ($self, $pfm, $int_id,$ACTION) = @_; |
| 781 |
my $sth = $self->dbh->prepare |
| 782 |
(q! INSERT INTO MATRIX_PROTEIN |
| 783 |
VALUES(?,?) !); |
| 784 |
|
| 785 |
#sanity check: are there any accession numbers? Its ok not to have it. |
| 786 |
return() unless $pfm->{'tags'}{'acc'}; |
| 787 |
|
| 788 |
#is the species a string or an arrayref? |
| 789 |
if ( ref ($pfm->{'tags'}{'acc'}) eq 'ARRAY'){ |
| 790 |
# walkthru array |
| 791 |
foreach my $acc ( @{$pfm->{'tags'}{'acc'}}){ |
| 792 |
$acc=~s/\s//g; |
| 793 |
$sth->execute($int_id,$acc); |
| 794 |
} |
| 795 |
} |
| 796 |
else{ |
| 797 |
# split and walk thru |
| 798 |
foreach my $acc ( split(/\,/, $pfm->{'tags'}{'acc'})){ |
| 799 |
$acc=~s/\s//g; |
| 800 |
$sth->execute($int_id,$acc); |
| 801 |
} |
| 802 |
|
| 803 |
} |
| 804 |
|
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
#when creating: try to support arbitrary tags |
| 810 |
|
| 811 |
sub _create_tables { # DONE |
| 812 |
# utility function |
| 813 |
|
| 814 |
# If you want to change the databse schema, |
| 815 |
# this is the right place to do it |
| 816 |
|
| 817 |
my $dbh = shift; |
| 818 |
|
| 819 |
my @queries = |
| 820 |
( |
| 821 |
q! |
| 822 |
CREATE TABLE MATRIX( |
| 823 |
ID INT NOT NULL AUTO_INCREMENT, |
| 824 |
COLLECTION VARCHAR (16) DEFAULT '', |
| 825 |
BASE_ID VARCHAR (16)DEFAULT '' NOT NULL , |
| 826 |
VERSION TINYINT DEFAULT 1 NOT NULL , |
| 827 |
NAME VARCHAR (255) DEFAULT '' NOT NULL, |
| 828 |
PRIMARY KEY (ID)) |
| 829 |
!, |
| 830 |
q! |
| 831 |
|
| 832 |
CREATE TABLE MATRIX_DATA( |
| 833 |
ID INT NOT NULL, |
| 834 |
row VARCHAR(1) NOT NULL, |
| 835 |
col TINYINT(3) UNSIGNED NOT NULL, |
| 836 |
val float(10,3), |
| 837 |
PRIMARY KEY (ID, row, col) |
| 838 |
) |
| 839 |
!, |
| 840 |
q! |
| 841 |
CREATE TABLE MATRIX_ANNOTATION( |
| 842 |
ID INT NOT NULL, |
| 843 |
TAG VARCHAR(255)DEFAULT '' NOT NULL, |
| 844 |
VAL varchar(255) DEFAULT '', |
| 845 |
PRIMARY KEY (ID, TAG) |
| 846 |
) |
| 847 |
!, |
| 848 |
q! |
| 849 |
CREATE TABLE MATRIX_SPECIES( |
| 850 |
ID INT NOT NULL, |
| 851 |
TAX_ID VARCHAR(255)DEFAULT '' NOT NULL |
| 852 |
) |
| 853 |
!, |
| 854 |
q! |
| 855 |
CREATE TABLE MATRIX_PROTEIN( |
| 856 |
ID INT NOT NULL, |
| 857 |
ACC VARCHAR(255)DEFAULT '' NOT NULL |
| 858 |
) |
| 859 |
! |
| 860 |
); |
| 861 |
foreach my $query (@queries) { |
| 862 |
$dbh->do($query) |
| 863 |
or die("Error executing the query: $query\n"); |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
sub _get_matrixstring { #DONE |
| 870 |
my ($self, $ID) = @_; |
| 871 |
#my %dbname = (PWM => 'pwm', PFM => 'raw', ICM => 'info'); |
| 872 |
#unless (defined $dbname{$mt}) { |
| 873 |
#$self->throw("Unsupported matrix type: ".$mt); |
| 874 |
#} |
| 875 |
my $sth; |
| 876 |
my $qID = $self->dbh->quote($ID); |
| 877 |
my $matrixstring = ""; |
| 878 |
foreach my $base (qw(A C G T)) { |
| 879 |
$sth=$self->dbh->prepare |
| 880 |
("SELECT val FROM MATRIX_DATA |
| 881 |
WHERE ID=$qID AND row='$base' ORDER BY col"); |
| 882 |
$sth->execute; |
| 883 |
$matrixstring .= |
| 884 |
join (" ", (map {$_->[0]} @{$sth->fetchall_arrayref()}))."\n"; |
| 885 |
} |
| 886 |
$sth->finish; |
| 887 |
return undef if $matrixstring eq "\n"x4; |
| 888 |
|
| 889 |
return $matrixstring; |
| 890 |
} |
| 891 |
|
| 892 |
sub _get_latest_version { #DONE |
| 893 |
my ($self, $base_ID) = @_; |
| 894 |
# SELECT VERSION FROM MATRIX WHERE BASE_ID=? ORDER BY VERSION DESC LIMIT 1 |
| 895 |
my $sth=$self->dbh->prepare |
| 896 |
(qq!SELECT VERSION FROM MATRIX |
| 897 |
WHERE BASE_ID="$base_ID" |
| 898 |
ORDER BY VERSION DESC LIMIT 1!); |
| 899 |
$sth->execute; |
| 900 |
my ($latest)=$sth->fetchrow_array(); |
| 901 |
return($latest); |
| 902 |
|
| 903 |
} |
| 904 |
sub _get_internal_id { #DONE |
| 905 |
# picks out the internal id for a a stable id+ version. Also checks if this cobo exists or not |
| 906 |
my ($self, $base_ID, $version) = @_; |
| 907 |
# SELECT ID FROM MATRIX WHERE BASE_ID=? and VERSION=? |
| 908 |
my $sth=$self->dbh->prepare |
| 909 |
(qq!SELECT ID FROM MATRIX |
| 910 |
WHERE BASE_ID="$base_ID" AND VERSION="$version"!); |
| 911 |
$sth->execute; |
| 912 |
my ($int_id)=$sth->fetchrow_array(); |
| 913 |
return($int_id); |
| 914 |
|
| 915 |
|
| 916 |
} |
| 917 |
|
| 918 |
sub _get_Matrix_by_int_id |
| 919 |
{ #done |
| 920 |
my ($self, $int_id, $mt)= @_; |
| 921 |
my $matrixobj; |
| 922 |
$mt='PFM' unless $mt; |
| 923 |
# get the matrix as a string |
| 924 |
my $matrixstring = $self->_get_matrixstring($int_id) || return undef; |
| 925 |
|
| 926 |
#get remaining data in the matrix table: name, collection |
| 927 |
my $sth=$self->dbh->prepare(qq!SELECT BASE_ID,VERSION, COLLECTION,NAME FROM MATRIX WHERE ID="$int_id"!); |
| 928 |
$sth->execute(); |
| 929 |
my ($base_ID, $version,$collection,$name)=$sth->fetchrow_array(); |
| 930 |
|
| 931 |
# get species |
| 932 |
$sth=$self->dbh->prepare(qq!SELECT TAX_ID FROM MATRIX_SPECIES WHERE ID="$int_id"!); |
| 933 |
$sth->execute(); |
| 934 |
my @tax_ids; |
| 935 |
while (my ($specie)=$sth->fetchrow_array()) |
| 936 |
{ |
| 937 |
push(@tax_ids, $specie); |
| 938 |
} |
| 939 |
# get acc |
| 940 |
$sth=$self->dbh->prepare(qq!SELECT ACC FROM MATRIX_PROTEIN WHERE ID="$int_id"!); |
| 941 |
$sth->execute(); |
| 942 |
my @accs; |
| 943 |
while (my ($a)=$sth->fetchrow_array()) |
| 944 |
{ |
| 945 |
push(@accs, $a); |
| 946 |
} |
| 947 |
# get remaining annotation as tags, form ANNOTATION table |
| 948 |
my %tags; |
| 949 |
$sth=$self->dbh->prepare(qq{SELECT TAG, VAL FROM MATRIX_ANNOTATION WHERE ID = "$int_id" }); |
| 950 |
$sth->execute(); |
| 951 |
while ( my($tag, $val)= $sth->fetchrow_array()) |
| 952 |
{ |
| 953 |
$tags{$tag}=$val; |
| 954 |
} |
| 955 |
$tags{'collection'}= $collection; |
| 956 |
$tags{'species'}=\@tax_ids; # as array reference instead of strigifying |
| 957 |
$tags{'acc'}=\@accs; # same |
| 958 |
# |
| 959 |
my $class= $tags{'class'}; |
| 960 |
delete ($tags{'class'}); |
| 961 |
# |
| 962 |
eval("\$matrixobj= TFBS::Matrix::PFM->new".' |
| 963 |
( -ID => "$base_ID.$version", |
| 964 |
-name =>$name, |
| 965 |
-class => $class, |
| 966 |
-tags => \%tags, |
| 967 |
-matrixstring=> $matrixstring # FIXME - temporary |
| 968 |
);' |
| 969 |
); |
| 970 |
if ($@) |
| 971 |
{ |
| 972 |
$self->throw($@); |
| 973 |
} |
| 974 |
# warn $int_id, "\t", ref($matrixobj); |
| 975 |
return ($matrixobj->to_PWM) if $mt eq "PWM"; |
| 976 |
return ($matrixobj->to_ICM) if $mt eq "ICM"; |
| 977 |
return ($matrixobj); # default PFM |
| 978 |
} |
| 979 |
|
| 980 |
sub _get_IDlist_by_query |
| 981 |
{ #needs cleanup. NOT for the faint-hearted. |
| 982 |
my ($self, %args)=@_; |
| 983 |
# called by get_MatrixSet |
| 984 |
# warn $args{"-collection"}; |
| 985 |
|
| 986 |
$args{'-collection'}='CORE' unless $args{'-collection'}; |
| 987 |
|
| 988 |
# returns a set of internal IDs with whicj to get the actual matrices |
| 989 |
# current idea: |
| 990 |
# 1: first catch non-tag things like collection, name and version, species |
| 991 |
# makw one query for these if they are named and check the IDs for "latest" unless requested not to. |
| 992 |
# these are AND statements |
| 993 |
# 2:then do the rest on tag level: |
| 994 |
# to be able to do this with actual and tattemnet innthe tag table, we do an inner join query, which is kept separate just for convenice |
| 995 |
# we then intersect 1 and 2 |
| 996 |
# 3: then do matrix-based features such as ic, with, number of sites etc, for the surviving matrices. This shold happen in the get_matrixset part |
| 997 |
|
| 998 |
my @int_ids_to_return; |
| 999 |
|
| 1000 |
# should redo so that matrix_annotation queries are separate, with an intersect in the end |
| 1001 |
#special case 1: get ALL matrices. Higher priority than all |
| 1002 |
|
| 1003 |
if ($args{'-all'}) |
| 1004 |
{ |
| 1005 |
my $sth=$self->dbh->prepare (qq!SELECT ID FROM MATRIX!); |
| 1006 |
$sth->execute(); |
| 1007 |
my @a; |
| 1008 |
while ( my ($i)=$sth->fetchrow_array()) |
| 1009 |
{ |
| 1010 |
push (@a, $i); |
| 1011 |
} |
| 1012 |
return \@a; |
| 1013 |
} |
| 1014 |
|
| 1015 |
# ids: special case2 which is has higher priority than any other except the above (ignore all others |
| 1016 |
|
| 1017 |
if ($args{'-ID'}) |
| 1018 |
{ |
| 1019 |
# these might be either stable IDs or stableid.version. |
| 1020 |
# if just stable ID and if all_versions==1, take all versions, otherwise the latest |
| 1021 |
if ( $args{-all_versions}) |
| 1022 |
{ |
| 1023 |
my $sth=$self->dbh->prepare (qq!SELECT ID FROM MATRIX WHERE BASE_ID=?!); |
| 1024 |
foreach my $stID(@{$args{'-ID'}}) |
| 1025 |
{ |
| 1026 |
my ($stable_ID, $version)= split (/\./, $stID); # ignore vesion here, this is a stupidity filter |
| 1027 |
$sth->execute($stable_ID); |
| 1028 |
while( my ($int_id)=$sth->fetchrow_array()) |
| 1029 |
{ |
| 1030 |
push (@int_ids_to_return, $int_id); |
| 1031 |
} |
| 1032 |
} |
| 1033 |
} |
| 1034 |
else |
| 1035 |
{ # only the lastest version, or the requested version |
| 1036 |
foreach my $stID(@{$args{'-ID'}}) |
| 1037 |
{ |
| 1038 |
#warn $stID; |
| 1039 |
my ($stable_ID, $version)= split (/\./, $stID); |
| 1040 |
$version=$self->_get_latest_version($stable_ID) unless $version; |
| 1041 |
my $int_id= $self->_get_internal_id($stable_ID, $version); |
| 1042 |
push (@int_ids_to_return, $int_id) if $int_id; |
| 1043 |
} |
| 1044 |
} |
| 1045 |
return \@int_ids_to_return |
| 1046 |
} |
| 1047 |
|
| 1048 |
my @tables=("MATRIX M"); |
| 1049 |
my @and; |
| 1050 |
|
| 1051 |
# in matrix table: collection, |
| 1052 |
if ( $args{-collection}) |
| 1053 |
{ |
| 1054 |
my $q=' (COLLECTION='; |
| 1055 |
if (ref $args{-collection} eq "ARRAY") |
| 1056 |
{ # so, possibly several |
| 1057 |
my @a; |
| 1058 |
foreach (@{$args{-collection}}) |
| 1059 |
{ |
| 1060 |
push (@a, "\"$_\""); |
| 1061 |
} |
| 1062 |
$q.= join ( " or COLLECTION=", @a); |
| 1063 |
} |
| 1064 |
else |
| 1065 |
{# just one - typical usage |
| 1066 |
$q.="\"$args{-collection}\""; |
| 1067 |
} |
| 1068 |
$q.=" ) "; |
| 1069 |
push (@and, $q); |
| 1070 |
} |
| 1071 |
|
| 1072 |
# in matrix table: names. Is something that is basically only used from the web interface |
| 1073 |
# typically used by the get_matrix_by_name function instead |
| 1074 |
|
| 1075 |
if ($args{-name}) |
| 1076 |
{ |
| 1077 |
my $q=' (NAME='; |
| 1078 |
if (ref $args{-name} eq "ARRAY") |
| 1079 |
{ # so, possibly several |
| 1080 |
my @a; |
| 1081 |
foreach (@{$args{-name}}) |
| 1082 |
{ |
| 1083 |
push (@a, "\"$_\""); |
| 1084 |
} |
| 1085 |
$q.= join ( " or NAME=", @a); |
| 1086 |
} |
| 1087 |
else |
| 1088 |
{# just one - typical usage |
| 1089 |
$q.="\"$args{-name}\""; |
| 1090 |
} |
| 1091 |
$q.=" ) "; |
| 1092 |
push (@and, $q); |
| 1093 |
} |
| 1094 |
|
| 1095 |
# in species table: tax.id: possibly many species with OR in between |
| 1096 |
if ( $args{-species}) |
| 1097 |
{ |
| 1098 |
push (@tables , "MATRIX_SPECIES S"); |
| 1099 |
my $q=" M.ID=S.ID and (TAX_ID= ";; |
| 1100 |
if (ref $args{-species} eq "ARRAY") |
| 1101 |
{ # so, possibly several |
| 1102 |
my @a; |
| 1103 |
foreach (@{$args{-species}}) |
| 1104 |
{ |
| 1105 |
push (@a, "\"$_\""); |
| 1106 |
} |
| 1107 |
$q.= join ( " or TAX_ID=", @a); |
| 1108 |
} |
| 1109 |
else |
| 1110 |
{# just one - typical usage |
| 1111 |
$q.="=\"$args{-species}\""; |
| 1112 |
} |
| 1113 |
$q.=") "; |
| 1114 |
push (@and, $q); |
| 1115 |
} |
| 1116 |
|
| 1117 |
# TAG_BASED |
| 1118 |
# an internal join query:should be able to handle up to 26 tags-value combos with ANDS in between |
| 1119 |
# Very ugly code ahead: |
| 1120 |
|
| 1121 |
my (@inner_tables, @internal_ands1,@internal_ands2 ); |
| 1122 |
my $int_counter=0; # for keeping track of names; |
| 1123 |
my @alpha = ("a" .. "z"); |
| 1124 |
|
| 1125 |
my %arrayref; |
| 1126 |
foreach my $key(keys %args) |
| 1127 |
{ |
| 1128 |
next if $key eq "-min_ic"; |
| 1129 |
next if $key eq "-matrixtype"; |
| 1130 |
next if $key eq "-species"; |
| 1131 |
next if $key eq "-collection"; |
| 1132 |
next if $key eq "-all_versions"; |
| 1133 |
next if $key eq "-all"; |
| 1134 |
next if $key eq "-ID"; |
| 1135 |
next if $key eq "-length"; |
| 1136 |
next if $key eq "-name"; |
| 1137 |
my $oldkey=$key; |
| 1138 |
$key=~s/-//; |
| 1139 |
$arrayref{$key}= $args{$oldkey}; |
| 1140 |
} |
| 1141 |
if (%arrayref) |
| 1142 |
{ |
| 1143 |
# get an internal name for the table |
| 1144 |
push (@internal_ands2 , " M.ID=a.ID " ); |
| 1145 |
my @a; |
| 1146 |
foreach my $key (keys %arrayref) |
| 1147 |
{ |
| 1148 |
my $tname= $alpha[$int_counter]; |
| 1149 |
push (@inner_tables , "MATRIX_ANNOTATION $tname"); |
| 1150 |
push (@internal_ands1 , $alpha[$int_counter].".ID=". $alpha[$int_counter-1].".ID") unless $int_counter==0; |
| 1151 |
$int_counter++; |
| 1152 |
# is the thing aupplied an array reference in inteslf: make an "or" query from that |
| 1153 |
if ( ref $arrayref{$key} eq "ARRAY") |
| 1154 |
{ |
| 1155 |
my @b; |
| 1156 |
foreach( @{$arrayref{$key}}) |
| 1157 |
{ |
| 1158 |
push (@b, $self->dbh->quote($_)); |
| 1159 |
} |
| 1160 |
my $orstring= join (" or $tname.VAL=" , @b); |
| 1161 |
push (@a, "($tname.TAG=\"$key\" AND ($tname.VAL=$orstring))"); |
| 1162 |
} |
| 1163 |
#or not |
| 1164 |
else |
| 1165 |
{ |
| 1166 |
push (@a, "($tname.TAG=\"$key\" AND $tname.VAL=\"$arrayref{$key}\")"); |
| 1167 |
} |
| 1168 |
} |
| 1169 |
my $s= " ( ". join (" AND ", @a). ")"; |
| 1170 |
push (@internal_ands2 , $s); |
| 1171 |
} |
| 1172 |
|
| 1173 |
my $qq= "SELECT distinct(M.ID) from ". join (",", (@tables,@inner_tables)) . " where" . join ( " AND ", (@and,@internal_ands1, @internal_ands2 )); |
| 1174 |
|
| 1175 |
# warn $qq; |
| 1176 |
|
| 1177 |
#do actual mammoth query,and check for latest matrix |
| 1178 |
my $sth=$self->dbh->prepare ($qq); |
| 1179 |
$sth->execute(); |
| 1180 |
my @r; |
| 1181 |
|
| 1182 |
while (my($int_id)= $sth->fetchrow_array) |
| 1183 |
{ |
| 1184 |
if ($args{-all_versions}) |
| 1185 |
{ |
| 1186 |
push (@r,$int_id); |
| 1187 |
} |
| 1188 |
else |
| 1189 |
{ |
| 1190 |
# is latest? |
| 1191 |
push(@r,$int_id) if ( $self->_is_latest_version($int_id) ==1); |
| 1192 |
} |
| 1193 |
} |
| 1194 |
warn "Warning: Zero matrices returned with current critera" unless scalar @r; |
| 1195 |
return \@r; |
| 1196 |
} |
| 1197 |
|
| 1198 |
sub _is_latest_version{ |
| 1199 |
# is a particular internal ID representingthe latest matrix (collapse on base ids) |
| 1200 |
my ($self, $int_id)=@_; |
| 1201 |
my $sth=$self->dbh->prepare( qq! select count(*) from MATRIX |
| 1202 |
where BASE_ID= (SELECT BASE_ID from MATRIX where ID=$int_id) |
| 1203 |
AND VERSION>(SELECT VERSION from MATRIX where ID=$int_id) !); |
| 1204 |
$sth->execute(); |
| 1205 |
my ($count)= $sth->fetchrow_array(); |
| 1206 |
return(1) if $count ==0;# no matrices with higher version ID and same base id |
| 1207 |
return(0); |
| 1208 |
} |
| 1209 |
|
| 1210 |
sub DESTROY |
| 1211 |
{ |
| 1212 |
#OK |
| 1213 |
$_[0]->dbh->disconnect() if $_[0]->dbh; |
| 1214 |
} |
| 1215 |
|
| 1216 |
|
| 1217 |
|