UMLS-Interface
view release on metacpan or search on metacpan
lib/UMLS/Interface/CuiFinder.pm view on Meta::CPAN
my $sdb = $self->{'sdb'};
if(!$sdb) { $errorhandler->_error($pkg, $function, "Error with sdb.", 3); }
# set an execute the query to show all of the tables
my $sth = $sdb->prepare("show tables");
$sth->execute();
$errorhandler->_checkDbError($pkg, $function, $sth);
my $t = "";
my %tables = ();
while(($t) = $sth->fetchrow()) {
$tables{lc($t)} = 1;
}
$sth->finish();
if(! (exists$tables{lc($table)})) { return 0; }
else { return 1; }
}
# connect the database to the source db that holds
# the path tables for user specified source(s) and
# relation(s)
# input :
# output: $sdb <- reference to the database
sub _connectIndexDB {
my $self = shift;
my $function = "_connectIndexDB";
&_debug($function);
# check self
if(!defined $self || !ref $self) {
$errorhandler->_error($pkg, $function, "", 2);
}
my $sdb = "";
if(defined $self->{'username'}) {
my $username = $self->{'username'};
my $password = $self->{'password'};
my $hostname = $self->{'hostname'};
my $socket = $self->{'socket'};
eval{$sdb = DBI->connect("DBI:mysql:database=$indexDB;mysql_socket=$socket;host=$hostname",
$username, $password,
{RaiseError => 1, PrintError => 1, AutoCommit => 0 });};
if($@) { $errorhandler->_error($pkg, $function, "No database to connect to", 1); }
}
else {
my $dsn = "DBI:mysql:$indexDB;mysql_read_default_group=client;";
eval{$sdb = DBI->connect($dsn);};
if($@) { $errorhandler->_error($pkg, $function, "No database to connect to", 1); }
}
$errorhandler->_checkDbError($pkg, $function, $sdb);
# set database parameters
$sdb->{'mysql_enable_utf8'} = 1;
$sdb->do('SET NAMES utf8');
$sdb->{mysql_auto_reconnect} = 1;
$self->{'sdb'} = $sdb;
return $sdb;
}
# return the database connection to the umlsinterfaceindex
# input :
# output: $sdb <- database handler
sub _getIndexDB {
my $self = shift;
my $function = "_getIndexDB";
&_debug($function);
# check self
if(!defined $self || !ref $self) {
$errorhandler->_error($pkg, $function, "", 2);
}
# get the databawse
my $sdb = $self->{'sdb'};
if(!$sdb) { $errorhandler->_error($pkg, $function, "Error with sdb.", 3); }
# return the database
return $sdb;
}
# return the database connection to the umls database
# input :
# output: $db <- database handler
sub _getDB {
my $self = shift;
my $function = "_getDB";
&_debug($function);
# check self
if(!defined $self || !ref $self) {
$errorhandler->_error($pkg, $function, "", 2);
}
# get the databawse
my $db = $self->{'db'};
if(!$db) { $errorhandler->_error($pkg, $function, "Error with db.", 3); }
# return the database
return $db;
}
# this function creates the umlsinterfaceindex database connection
# input :
# output:
sub _createIndexDB {
my $self = shift;
my $function = "_createIndexDB";
lib/UMLS/Interface/CuiFinder.pm view on Meta::CPAN
}
if($option_t == 0) {
print STDERR "$output\n\n";
}
}
# method to set the umlsinterface index database
# input : $params <- reference to a hash
# output:
sub _setDatabase {
my $self = shift;
my $params = shift;
my $function = "_setDatabase";
&_debug($function);
# check self
if(!defined $self || !ref $self) {
$errorhandler->_error($pkg, $function, "", 2);
}
# check the params
$params = {} if(!defined $params);
# get the database connection parameters
my $database = $params->{'database'};
my $hostname = $params->{'hostname'};
my $socket = $params->{'socket'};
my $port = $params->{'port'};
my $username = $params->{'username'};
my $password = $params->{'password'};
# set up defaults if the options were not passed
if(! defined $database) { $database = "umls"; }
if(! defined $socket) { $socket = "/var/run/mysqld/mysqld.sock"; }
if(! defined $hostname) { $hostname = "localhost"; }
# initialize the database handler
my $db = "";
# create the database object...
if(defined $username and defined $password) {
if($debug) { print STDERR "Connecting with username and password\n"; }
$db = DBI->connect("DBI:mysql:database=$database;mysql_socket=$socket;host=$hostname",$username, $password, {RaiseError => 0});
}
else {
if($debug) { print STDERR "Connecting using the my.cnf file\n"; }
my $dsn = "DBI:mysql:umls;mysql_read_default_group=client;";
$db = DBI->connect($dsn);
}
# check if there is an error
$errorhandler->_checkDbError($pkg, $function, $db);
# check that the db exists
if(!$db) { $errorhandler->_error($pkg, $function, "Error with db.", 3); }
# set database parameters
$db->{'mysql_enable_utf8'} = 1;
$db->do('SET NAMES utf8');
$db->{mysql_auto_reconnect} = 1;
# set the self parameters
$self->{'db'} = $db;
$self->{'username'} = $username;
$self->{'password'} = $password;
$self->{'hostname'} = $hostname;
$self->{'socket'} = $socket;
$self->{'database'} = $database;
# return the database handler
return $db;
}
# returns the parameters set in the configuration file
# input:
# output : $hash <- reference to hash containing parameters in the
# configuration file - if there was not config
# file the hash is empty and defaults are being
# use
sub _getConfigParameters {
my $self = shift;
my $function = "_getConfigParameters";
return \%parameters;
}
# returns all of the cuis given the specified set of sources
# and relations defined in the configuration file
# input : $sab <- string containing a source
# output: $array <- reference to array of cuis
sub _getCuis {
my $self = shift;
my $sab = shift;
my $function = "_getCuis";
#&_debug($function);
# check self
if(!defined $self || !ref $self) {
$errorhandler->_error($pkg, $function, "", 2);
}
# check input variables
if(!$sab) { $errorhandler->_error($pkg, $function, "Error with input variable \$sab.", 4); }
# set up the database
my $db = $self->{'db'};
if(!$db) { $errorhandler->_error($pkg, $function, "Error with db.", 3); }
# NOTE: it is quicker to get all the CUI1s and then all of the CUI2 and then merge
# rather than try to get them all together in a single query.
# get all of the CUI1s
my $allCui1 = $db->selectcol_arrayref("select CUI1 from MRREL where ($relations) and (SAB=\'$sab\') and SUPPRESS='N'\;");
$errorhandler->_checkDbError($pkg, $function, $db);
# get all of the CUI1s
( run in 0.602 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )