#!/usr/bin/perl
eval
(GetOptions(
"version"
,
"help"
,
"username=s"
,
"password=s"
,
"hostname=s"
,
"database=s"
,
"socket=s"
,
"file=s"
,
"unmark"
)) or
die
(
"Please check the above mentioned option(s).\n"
);
if
(
defined
$opt_help
) {
$opt_help
= 1;
&showHelp
();
exit
;
}
if
(
defined
$opt_version
) {
$opt_version
= 1;
&showVersion
();
exit
;
}
my
$database
=
"umls"
;
if
(
defined
$opt_database
) {
$database
=
$opt_database
; }
my
$hostname
=
"localhost"
;
if
(
defined
$opt_hostname
) {
$hostname
=
$opt_hostname
; }
my
$socket
=
"/tmp/mysql.sock"
;
if
(
defined
$opt_socket
) {
$socket
=
$opt_socket
; }
my
$db
=
""
;
print
"Connecting to the UMLS\n"
;
if
(
defined
$opt_username
and
defined
$opt_password
) {
print
" => Connecting with username and password\n\n"
;
$db
= DBI->
connect
(
"DBI:mysql:database=$database;mysql_socket=$socket;host=$hostname"
,
$opt_username
,
$opt_password
, {
RaiseError
=> 1});
}
else
{
print
" => Connecting using the my.cnf file\n\n"
;
my
$dsn
=
"DBI:mysql:umls;mysql_read_default_group=client;"
;
$db
= DBI->
connect
(
$dsn
);
}
if
(!
$db
||
$db
->err()) {
print
STDERR
"Error! Unable to open database: "
.(
$db
->errstr());
exit
;
}
if
(!
$db
||
$db
->err()) {
print
STDERR
"Error Unable to open database: umls\n\n"
;
exit
;
}
$sth
=
$db
->prepare(
"show tables"
);
$sth
->execute();
if
(
$sth
->err()) {
print
STDERR
"Error! Unable run query: "
.(
$sth
->errstr());
exit
;
}
my
%tables
= ();
while
((
$tableName
) =
$sth
->fetchrow()) {
$tables
{
$tableName
} = 1;
}
$sth
->finish();
if
(!
defined
$tables
{
"MRREL"
}) {
print
STDERR
"Table MRREL not found in database."
;
exit
;
}
if
(
defined
$opt_unmark
) {
print
"Unmarking the relations in the file\n"
;
my
$ar
=
$db
->
do
(
"update MRREL set CVF=NULL where CVF=1"
);
my
$drop
=
$db
->
do
(
"drop index CVFINDEX on MRREL"
);
my
$drop
=
$db
->
do
(
"drop index RELINDEX on MRREL"
);
}
elsif
(
defined
$opt_file
) {
print
"Creating Index CVFINDEX in MRREL on CUI1 CUI2 and REL\n"
;
print
"Otherwise this takes forever to run!\n"
;
print
"Create CVFINDEX\n"
;
my
$index
=
$db
->
do
(
"create index CVFINDEX on MRREL (CUI1, CUI2, REL)"
);
print
"Create RELINDEX\n"
;
my
$index
=
$db
->
do
(
"create index RELINDEX on MRREL (REL)"
);
print
"Both indexes are created\n\n"
;
print
"Marking the relations in the file\n"
;
open
(FILE,
$opt_file
) ||
die
"Could not open file: $opt_file\n"
;
print
"Marking relations from the file....\n"
;
my
$ar
=
$db
->
do
(
"update MRREL set CVF=1 where CUI1=CUI2"
);
while
(<FILE>) {
chomp
;
my
(
$cui1
,
$cui2
,
$rel
,
$rela
) =
split
/\|/;
my
$ar1
=
$db
->
do
(
"update MRREL set CVF=1 where CUI1='$cui1' and CUI2='$cui2' and REL='$rel'"
);
if
(
$rel
eq
"PAR"
) {
$ar1
=
$db
->
do
(
"update MRREL set CVF=1 where CUI1='$cui1' and CUI2='$cui2' and REL='RB'"
);
$ar1
=
$db
->
do
(
"update MRREL set CVF=1 where CUI1='$cui2' and CUI2='$cui1' and REL='CHD'"
);
$ar1
=
$db
->
do
(
"update MRREL set CVF=1 where CUI1='$cui2' and CUI2='$cui1' and REL='RN'"
);
}
if
(
$rel
eq
"RB"
) {
$ar1
=
$db
->
do
(
"update MRREL set CVF=1 where CUI1='$cui1' and CUI2='$cui2' and REL='PAR'"
);
$ar1
=
$db
->
do
(
"update MRREL set CVF=1 where CUI1='$cui2' and CUI2='$cui1' and REL='CHD'"
);
$ar1
=
$db
->
do
(
"update MRREL set CVF=1 where CUI1='$cui2' and CUI2='$cui1' and REL='RN'"
);
}
if
(
$rel
eq
"CHD"
) {
$ar1
=
$db
->
do
(
"update MRREL set CVF=1 where CUI1='$cui1' and CUI2='$cui2' and REL='RN'"
);
$ar1
=
$db
->
do
(
"update MRREL set CVF=1 where CUI1='$cui2' and CUI2='$cui1' and REL='PAR'"
);
$ar1
=
$db
->
do
(
"update MRREL set CVF=1 where CUI1='$cui2' and CUI2='$cui1' and REL='RB'"
);
}
if
(
$rel
eq
"RN"
) {
$ar1
=
$db
->
do
(
"update MRREL set CVF=1 where CUI1='$cui1' and CUI2='$cui2' and REL='CHD'"
);
$ar1
=
$db
->
do
(
"update MRREL set CVF=1 where CUI1='$cui2' and CUI2='$cui1' and REL='PAR'"
);
$ar1
=
$db
->
do
(
"update MRREL set CVF=1 where CUI1='$cui2' and CUI2='$cui1' and REL='RB'"
);
}
}
}
else
{
}
print
"Finished.\n"
;
sub
minimalUsageNotes {
print
"Usage: umlsCycles.pl --username STRING --password STRING [OPTIONS]\n\n"
;
&askHelp
();
exit
;
}
sub
showHelp() {
print
"Usage: umlsCycles.pl --username STRING --password STRING [OPTIONS]\n\n"
;
print
"This is a utility that takes as input a list of problematic\n"
;
print
"relations in the MRREL file. The relations cause cycles in \n"
;
print
"structure. This programs marks the problematic relations in\n"
;
print
"the MRREL file by inserting a '1' into the CVF column. It \n"
;
print
"does not physicall remove a relation from the table only tags\n"
;
print
"it. The bad-relations.txt file is found in the default_options\n"
;
print
"directory. This file is used as the default unless one is \n"
;
print
"specified on the command line.\n\n"
;
print
"Options:\n\n"
;
print
"--username STRING Username required to access mysql\n\n"
;
print
"--password STRING Password required to access mysql\n\n"
;
print
"--hostname STRING Hostname for mysql (DEFAULT: localhost)\n\n"
;
print
"--database STRING Database contain UMLS (DEFAULT: umls)\n\n"
;
print
"--unmark Unmark the problematic relations\n\n"
;
print
"--file FILE File containing bad relations\n\n"
;
print
"--version Prints the version number\n\n"
;
print
"--help Prints this help message.\n\n"
;
}
sub
showVersion {
print
'$Id: umlsCycles.pl,v 1.1.1.1 2009/10/14 15:38:57 btmcinnes Exp $'
;
print
"\nCopyright (c) 2007, Ted Pedersen & Bridget McInnes\n"
;
}
sub
askHelp {
print
STDERR
"Type umlsCycles.pl --help for help.\n"
;
}