Skip to content
Merged

#2627 #2628

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 31 additions & 34 deletions code/ARAX/ARAXQuery/Infer/scripts/ExplianableDTD_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
pathlist = os.path.realpath(__file__).split(os.path.sep)
RTXindex = pathlist.index("RTX")
sys.path.append(os.path.sep.join([*pathlist[:(RTXindex + 1)], 'code']))
from RTXConfiguration import RTXConfiguration
from RTXConfiguration import RTXConfiguration #noqa: E402
RTXConfig = RTXConfiguration()
from ARAX_database_manager import ARAXDatabaseManager
from ARAX_database_manager import ARAXDatabaseManager #noqa: E402


DEBUG = True
Expand Down Expand Up @@ -70,15 +70,15 @@ def __init__(self,

if build:
if path_to_score_results is None:
self.logger.error(f"Please set a path to 'path_to_score_results'")
self.logger.error("Please set a path to 'path_to_score_results'")
raise
elif not os.path.exists(path_to_score_results) or not len(os.listdir(path_to_score_results)) > 0:
self.logger.error(f"The given path '{path_to_score_results}' doesn't exist or is an empty folder")
raise
else:
self.path_to_score_results = path_to_score_results
if path_to_path_results is None:
self.logger.error(f"Please set a path to 'path_to_path_results'")
self.logger.error("Please set a path to 'path_to_path_results'")
raise
elif not os.path.exists(path_to_path_results) or not len(os.listdir(path_to_path_results)) > 0:
self.logger.error(f"The given path '{path_to_path_results}' doesn't exist or is an empty folder")
Expand Down Expand Up @@ -127,10 +127,7 @@ def connect(self):
# get it remotely
DBmanager = ARAXDatabaseManager()
if DBmanager.check_versions():
self.response.debug(
f"Downloading databases because mismatch in local versions and remote versions was found... (will take a few minutes)")
self.response = DBmanager.update_databases(response=self.response)
#os.system(f"scp {RTXConfig.db_username}@{RTXConfig.db_host}:{RTXConfig.explainable_dtd_db_path} {database}")
raise FileNotFoundError("ARAX database manager indicates that there are missing databases")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @saramsey for the changes. I am ok with this change if we have other scripts that can automatically check and update database version based on the config_dbs.json in advance.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering whether the ARAX database manager can implement any pre-checking and update database version before we submit a request to ARAX system. If so, I think this change is good.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, ARAX database manager checks databases at application start-up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for reviewing. I will merge.

self.connection = sqlite3.connect(database)
print("INFO: Connecting to database", flush=True)
self.is_connected = True
Expand All @@ -145,7 +142,7 @@ def disconnect(self):
try:
self.connection.commit()
self.connection.close()
except:
except sqlite3.ProgrammingError:
self.logger.info("Database appears to be already disconnected")
self.success_con = False
else:
Expand All @@ -157,11 +154,11 @@ def create_tables(self):

if self.success_con is True:
self.logger.info(f"Creating database {self.database_name}")
self.connection.execute(f"DROP TABLE IF EXISTS PREDICTION_SCORE_TABLE")
self.connection.execute(f"CREATE TABLE PREDICTION_SCORE_TABLE( drug_id VARCHAR(255), drug_name VARCHAR(255), disease_id VARCHAR(255), disease_name VARCHAR(255), tn_score FLOAT, tp_score FLOAT, unknown_score FLOAT)")
self.connection.execute(f"DROP TABLE IF EXISTS PATH_RESULT_TABLE")
self.connection.execute(f"CREATE TABLE PATH_RESULT_TABLE( drug_id VARCHAR(255), drug_name VARCHAR(255), disease_id VARCHAR(255), disease_name VARCHAR(255), path VARCHAR(255), path_score FLOAT )")
self.logger.info(f"Creating tables is completed")
self.connection.execute("DROP TABLE IF EXISTS PREDICTION_SCORE_TABLE")
self.connection.execute("CREATE TABLE PREDICTION_SCORE_TABLE( drug_id VARCHAR(255), drug_name VARCHAR(255), disease_id VARCHAR(255), disease_name VARCHAR(255), tn_score FLOAT, tp_score FLOAT, unknown_score FLOAT)")
self.connection.execute("DROP TABLE IF EXISTS PATH_RESULT_TABLE")
self.connection.execute("CREATE TABLE PATH_RESULT_TABLE( drug_id VARCHAR(255), drug_name VARCHAR(255), disease_id VARCHAR(255), disease_name VARCHAR(255), path VARCHAR(255), path_score FLOAT )")
self.logger.info("Creating tables is completed")

## Populate the tables
def populate_table(self):
Expand All @@ -174,11 +171,11 @@ def populate_table(self):
with open(f"{self.path_to_score_results}/{file_name}", 'r') as file_in:
content_list = file_in.readlines()
col_name = content_list.pop(0)
insert_command1 = f"INSERT INTO PREDICTION_SCORE_TABLE("
insert_command2 = f" values ("
insert_command1 = "INSERT INTO PREDICTION_SCORE_TABLE("
insert_command2 = " values ("
for col in col_name.strip().split("\t"):
insert_command1 = insert_command1 + f"{col},"
insert_command2 = insert_command2 + f"?,"
insert_command2 = insert_command2 + "?,"

insert_command = insert_command1 + ")" + insert_command2 + ")"
insert_command = insert_command.replace(',)', ')')
Expand All @@ -201,11 +198,11 @@ def populate_table(self):
with open(f"{self.path_to_path_results}/{file_name}", 'r') as file_in:
content_list = file_in.readlines()
col_name = content_list.pop(0)
insert_command1 = f"INSERT INTO PATH_RESULT_TABLE("
insert_command2 = f" values ("
insert_command1 = "INSERT INTO PATH_RESULT_TABLE("
insert_command2 = " values ("
for col in col_name.strip().split("\t"):
insert_command1 = insert_command1 + f"{col},"
insert_command2 = insert_command2 + f"?,"
insert_command2 = insert_command2 + "?,"

insert_command = insert_command1 + ")" + insert_command2 + ")"
insert_command = insert_command.replace(',)', ')')
Expand All @@ -221,24 +218,24 @@ def populate_table(self):
self.connection.commit()
self.connection.commit()

self.logger.info(f"Populating tables is completed")
self.logger.info("Populating tables is completed")

def create_indexes(self):

if self.success_con is True:
self.logger.info(f"Creating INDEXes on PREDICTION_SCORE_TABLE",)
self.connection.execute(f"CREATE INDEX idx_PREDICTION_SCORE_TABLE_drug_id ON PREDICTION_SCORE_TABLE(drug_id)")
self.connection.execute(f"CREATE INDEX idx_PREDICTION_SCORE_TABLE_drug_name ON PREDICTION_SCORE_TABLE(drug_name)")
self.connection.execute(f"CREATE INDEX idx_PREDICTION_SCORE_TABLE_disease_id ON PREDICTION_SCORE_TABLE(disease_id)")
self.connection.execute(f"CREATE INDEX idx_PREDICTION_SCORE_TABLE_disease_name ON PREDICTION_SCORE_TABLE(disease_name)")

self.logger.info(f"Creating INDEXes on PREDICTION_SCORE_TABLE",)
self.connection.execute(f"CREATE INDEX idx_PATH_RESULT_TABLE_drug_id ON PATH_RESULT_TABLE(drug_id)")
self.connection.execute(f"CREATE INDEX idx_PATH_RESULT_TABLE_drug_name ON PATH_RESULT_TABLE(drug_name)")
self.connection.execute(f"CREATE INDEX idx_PATH_RESULT_TABLE_disease_id ON PATH_RESULT_TABLE(disease_id)")
self.connection.execute(f"CREATE INDEX idx_PATH_RESULT_TABLE_disease_name ON PATH_RESULT_TABLE(disease_name)")

self.logger.info(f"INFO: Creating INDEXes is completed")
self.logger.info("Creating INDEXes on PREDICTION_SCORE_TABLE",)
self.connection.execute("CREATE INDEX idx_PREDICTION_SCORE_TABLE_drug_id ON PREDICTION_SCORE_TABLE(drug_id)")
self.connection.execute("CREATE INDEX idx_PREDICTION_SCORE_TABLE_drug_name ON PREDICTION_SCORE_TABLE(drug_name)")
self.connection.execute("CREATE INDEX idx_PREDICTION_SCORE_TABLE_disease_id ON PREDICTION_SCORE_TABLE(disease_id)")
self.connection.execute("CREATE INDEX idx_PREDICTION_SCORE_TABLE_disease_name ON PREDICTION_SCORE_TABLE(disease_name)")

self.logger.info("Creating INDEXes on PREDICTION_SCORE_TABLE",)
self.connection.execute("CREATE INDEX idx_PATH_RESULT_TABLE_drug_id ON PATH_RESULT_TABLE(drug_id)")
self.connection.execute("CREATE INDEX idx_PATH_RESULT_TABLE_drug_name ON PATH_RESULT_TABLE(drug_name)")
self.connection.execute("CREATE INDEX idx_PATH_RESULT_TABLE_disease_id ON PATH_RESULT_TABLE(disease_id)")
self.connection.execute("CREATE INDEX idx_PATH_RESULT_TABLE_disease_name ON PATH_RESULT_TABLE(disease_name)")

self.logger.info("INFO: Creating INDEXes is completed")

def get_score_table(self, drug_curie_ids=None, disease_curie_ids=None):
"""get the score table for given drug and/or disease curie ids
Expand Down