Debian Codegraph @

TFBS/DB/JASPAR2.pm

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