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