Skip to content

Commit

Permalink
allows config.txt to specify path to umls_tables
Browse files Browse the repository at this point in the history
  • Loading branch information
wboag committed Nov 15, 2015
1 parent f696562 commit cbea6e6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 31 deletions.
20 changes: 16 additions & 4 deletions cliner/features_dir/umls_dir/create_sqliteDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,46 @@
import sys
import os

features_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if features_dir not in sys.path:
sys.path.append(features_dir)


# find where umls tables are located
from read_config import enabled_modules
enabled = enabled_modules()
umls_tables = enabled['UMLS']



def create_db():

print "\ncreating umls.db"
#connect to the .db file we are creating.
db_path = os.path.join(os.environ['CLINER_DIR'],'umls_tables/umls.db')
db_path = os.path.join(umls_tables, 'umls.db')
conn = sqlite3.connect( db_path )
conn.text_factory = str

print "opening files"
#load data in files.
try:
mrsty_path = os.path.join(os.environ['CLINER_DIR'],'umls_tables/MRSTY.RRF')
mrsty_path = os.path.join(umls_tables, 'MRSTY.RRF')
MRSTY_TABLE_FILE = open( mrsty_path, "r" )
except IOError:
print "\nNo file to use for creating MRSTY.RRF table\n"
conn.close()
sys.exit()

try:
mrcon_path = os.path.join(os.environ['CLINER_DIR'],'umls_tables/MRCONSO.RRF')
mrcon_path = os.path.join(umls_tables, 'MRCONSO.RRF')
MRCON_TABLE_FILE = open( mrcon_path , "r" )
except IOError:
print "\nNo file to use for creating MRCONSO.RRF table\n"
conn.close()
sys.exit()

try:
mrrel_path = os.path.join(os.environ['CLINER_DIR'],'umls_tables/MRREL.RRF')
mrrel_path = os.path.join(umls_tables, 'MRREL.RRF')
MRREL_TABLE_FILE = open( mrrel_path , "r" )
except IOError:
print "\nNo file to use for creating MRREL.RRF table\n"
Expand Down
20 changes: 13 additions & 7 deletions cliner/features_dir/umls_dir/create_trie.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
import sys
import os

features_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if features_dir not in sys.path:
sys.path.append(features_dir)

# find where umls tables are located
from read_config import enabled_modules
enabled = enabled_modules()
umls_tables = enabled['UMLS']



def create_trie():

"""
Expand All @@ -12,16 +23,11 @@ def create_trie():
@return A trie object
"""

# Is trie already built & pickled?
prefix = os.environ['CLINER_DIR']
filename = os.path.join( prefix, 'umls_tables/umls-concept.trie' )
filename = os.path.join(umls_tables, 'umls-concept.trie')
try:

t = marisa_trie.Trie().load(filename)

return t

except IOError:
pass

Expand All @@ -31,7 +37,7 @@ def create_trie():
#load data in files.
print "opening file"
try:
mrcon_path = os.path.join(os.environ['CLINER_DIR'],'umls_tables/MRCONSO.RRF')
mrcon_path = os.path.join(umls_tables, 'MRCONSO.RRF')
MRCON_TABLE = open( mrcon_path , "r" )
except IOError:
print "\nNo file to use for creating MRCON table\n"
Expand Down
22 changes: 16 additions & 6 deletions cliner/features_dir/umls_dir/interface_umls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,22 @@
import sqlite3
import create_sqliteDB
import os
import sys

import create_trie


features_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if features_dir not in sys.path:
sys.path.append(features_dir)


# find where umls tables are located
from read_config import enabled_modules
enabled = enabled_modules()
umls_tables = enabled['UMLS']




############################################
Expand All @@ -23,12 +35,10 @@
#connect to UMLS database
def SQLConnect():
#try to connect to the sqlite database.
db_path = os.path.join( os.environ['CLINER_DIR'], "umls_tables/umls.db")
if( os.path.isfile( db_path ) ):
print "\ndb exists"
else:
# Database does not exit. Make one.
print "\ndb doesn't exist"
# if database does not exit. Make one.
db_path = os.path.join(umls_tables, "umls.db")
if not os.path.isfile(db_path):
print "\n\tdb doesn't exist (creating one now)\n"
create_sqliteDB.create_db()

db = sqlite3.connect( db_path )
Expand Down
46 changes: 32 additions & 14 deletions cliner/features_dir/umls_dir/umls_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,47 @@
import sys
import os

sys.path.append((os.environ["CLINER_DIR"] + "/cliner/features_dir"))
import atexit


features_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if features_dir not in sys.path:
sys.path.append(features_dir)

# find where umls tables are located
from read_config import enabled_modules
enabled = enabled_modules()
umls_tables = enabled['UMLS']


sys.path.append((os.environ["CLINER_DIR"] + "/cliner/features_dir"))
from utilities import load_pickled_obj


class UmlsCache:

# static class variables
filename = None
cache = None

def __init__(self):
try:
prefix = os.environ['CLINER_DIR']
self.filename = os.path.join( prefix, 'umls_tables/umls_cache' )

self.cache = load_pickled_obj(self.filename)
UmlsCache.filename = os.path.join(umls_tables, 'umls_cache')
UmlsCache.cache = load_pickled_obj(UmlsCache.filename)

except IOError:
self.cache = {}
UmlsCache.cache = {}

def has_key( self , string ):
return self.cache.has_key( string )
def has_key(self , string):
return UmlsCache.cache.has_key( string )

def add_map( self , string, mapping ):
self.cache[string] = mapping
def add_map(self , string, mapping):
UmlsCache.cache[string] = mapping

def get_map( self , string ):
return self.cache[string]
def get_map(self , string):
return UmlsCache.cache[string]

def __del__(self):
pickle.dump( self.cache, open( self.filename, "wb" ) )
@staticmethod
@atexit.register
def destructor():
pickle.dump(UmlsCache.cache, open(UmlsCache.filename,"wb"))

0 comments on commit cbea6e6

Please sign in to comment.