Skip to content
Snippets Groups Projects
Commit c4f9445b authored by nfontrod's avatar nfontrod
Browse files

src/db_utils/populate_database.py - src/db_utils/config.py: modification of...

src/db_utils/populate_database.py - src/db_utils/config.py: modification of check_table_name, it now checks if the table name given exists in the database, not if it presents in Config.tables (so, this last variable was removed)
parent 1dda100c
No related branches found
No related tags found
No related merge requests found
......@@ -13,8 +13,11 @@ class Config:
"""
A class containing every parameters used in the submodule db_utils
"""
db_file = Path(__file__).parents[2] / "results" / 'chia_pet_database.db'
tables = ["cin_gene", "cin_exon", "cin_frequency", "cin_interaction"]
bed_exon = Path(__file__).parents[2] / 'data' / 'bed' / 'exon.bed'
bed_gene = Path(__file__).parents[2] / 'data' / 'bed' / 'gene.bed'
data = Path(__file__).parents[2] / 'data'
results = Path(__file__).parents[2] / "results"
db_file = results / 'chia_pet_database.db'
bed_exon = data / 'bed' / 'exon.bed'
bed_gene = data / 'bed' / 'gene.bed'
ase_event_file = data / 'splicing_lore_data' / 'ase_event.txt'
splicing_projects = data / 'splicing_lore_data' / \
'splicing_lore_projects.txt'
......@@ -113,16 +113,31 @@ def insert_data(table: str, content: List[Tuple], cnx: sqlite3.Connection
cursor.close()
def check_table_name(table : str):
def get_table_names(cnx: sqlite3.Connection) -> List[str]:
"""
Get the list of available table names.
:param cnx: The connection to ChIA-PET database.
:return: The list of availbale tables
"""
c = cnx.cursor()
c.execute("""SELECT name FROM sqlite_master WHERE type = 'table';""")
res = c.fetchall()
res = [r[0] for r in res]
return res
def check_table_name(cnx: sqlite3.Connection, table: str):
"""
Check if we can use the table name `table`.
:param cnx: Connection to ChIA-PET database
:param table: The name of the table to fille
:return: The same name with the prefix gin if it wasn't here.
"""
if "cin" not in table:
table = f"cin_{table.lower()}"
if table not in Config.tables:
tables = get_table_names(cnx)
if table not in tables:
msg = f"The name {table} is not available." \
f" If the table exist in the database, " \
f"change the config file to add the table name " \
......@@ -175,8 +190,8 @@ def populate_df(table: str, df: pd.DataFrame, clean: str):
tab.
:param clean: y to remove the data in the table, n else.
"""
table = check_table_name(table)
cnx = sqlite3.connect(Config.db_file)
table = check_table_name(cnx, table)
check_content_clean_and_insert(table, df, cnx, clean)
......@@ -195,8 +210,8 @@ def populate(table: str, file: str, clean: str, logging_level: str =
"""
logging_def(Config.db_file.parent, __file__, logging_level)
mfile = Path(file)
table = check_table_name(table)
cnx = sqlite3.connect(Config.db_file)
table = check_table_name(cnx, table)
check_content_clean_and_insert(table, mfile, cnx, clean)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment