Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Karmazin committed Jan 25, 2023
1 parent 1d09603 commit 7f777bd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__/
19 changes: 10 additions & 9 deletions coronary_ref_homo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ class CoronaryRefHomo:

rsid_map:dict[str, dict] = {}

def init(self, reporter, sql_insert:str) -> None:
self.parent = reporter
self.sql_insert = sql_insert

def setup(self, parent, result_cursor:sqlite3.Cursor, data_cursor:sqlite3.Cursor, sql_insert:str) -> None:
self.parent = parent
self.sql_insert = sql_insert
self.result_cursor: sqlite3.Cursor = result_cursor
self.data_cursor: sqlite3.Cursor = data_cursor

def setup(self) -> None:
sql:str = "SELECT rsID, Risk_allele FROM coronary_disease WHERE Ref_allele = Risk_allele"
self.parent.coronary_cursor.execute(sql)
rows:list = self.parent.coronary_cursor.fetchall()
self.data_cursor.execute(sql)
rows:list = self.data_cursor.fetchall()
for rsid, risk_allele in rows:
self.rsid_map[rsid] = {'exist':True, 'risk':risk_allele}

Expand All @@ -42,9 +43,9 @@ def end(self) -> None:
query:str = "SELECT Risk_allele, Gene, Genotype, Conclusion, Weight, PMID, Population, GWAS_study_design, P_value " \
f"FROM coronary_disease WHERE rsID = '{rsid}' AND Genotype = '{risk}';"

self.parent.coronary_cursor.execute(query)
row:list = self.parent.coronary_cursor.fetchone()
self.data_cursor.execute(query)
row:list = self.data_cursor.fetchone()
if row:
task:tuple = (rsid, row[1], row[0], row[0]+"/"+row[0], row[3], row[4], row[5], row[6], row[7], row[8],
self.parent.get_color(row[4], 0.6))
self.parent.longevity_cursor.execute(self.sql_insert, task)
self.result_cursor.execute(self.sql_insert, task)
41 changes: 20 additions & 21 deletions just_coronary.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@ def check(self):
return True

def setup (self):
self.ref_homo.init(self, self.sql_insert)
modules_path:str = str(Path(__file__).parent)
sql_file:str = modules_path + "/data/coronary.sqlite"
if Path(sql_file).exists():
self.coronary_conn:sqlite3.Connection = sqlite3.connect(sql_file)
self.coronary_cursor:sqlite3.Cursor = self.coronary_conn.cursor()
self.data_conn:sqlite3.Connection = sqlite3.connect(sql_file)
self.data_cursor:sqlite3.Cursor = self.data_conn.cursor()

self.result_path:Path = Path(self.output_dir, self.run_name + "_longevity.sqlite")
self.longevity_conn:sqlite3.Connection = sqlite3.connect(self.result_path)
self.longevity_cursor:sqlite3.Cursor = self.longevity_conn.cursor()
self.result_conn:sqlite3.Connection = sqlite3.connect(self.result_path)
self.result_cursor:sqlite3.Cursor = self.result_conn.cursor()
sql_create:str = """ CREATE TABLE IF NOT EXISTS coronary (
id integer NOT NULL PRIMARY KEY,
rsid text,
Expand All @@ -51,22 +50,22 @@ def setup (self):
pvalue text,
weightcolor text
)"""
self.longevity_cursor.execute(sql_create)
self.longevity_conn.commit()
self.longevity_cursor.execute("DELETE FROM coronary;")
self.ref_homo.setup()
self.result_cursor.execute(sql_create)
self.result_conn.commit()
self.result_cursor.execute("DELETE FROM coronary;")
self.ref_homo.setup(self, self.result_cursor, self.data_cursor, self.sql_insert)


def cleanup (self):
if self.longevity_cursor is not None:
self.longevity_cursor.close()
if self.longevity_conn is not None:
self.longevity_conn.commit()
self.longevity_conn.close()
if self.coronary_cursor is not None:
self.coronary_cursor.close()
if self.coronary_conn is not None:
self.coronary_conn.close()
if self.result_cursor is not None:
self.result_cursor.close()
if self.result_conn is not None:
self.result_conn.commit()
self.result_conn.close()
if self.data_cursor is not None:
self.data_cursor.close()
if self.data_conn is not None:
self.data_conn.close()
return


Expand Down Expand Up @@ -109,8 +108,8 @@ def annotate (self, input_data):
query:str = "SELECT Risk_allele, Gene, Genotype, Conclusion, Weight, PMID, Population, GWAS_study_design, P_value " \
f"FROM coronary_disease WHERE rsID = '{rsid}';"

self.coronary_cursor.execute(query)
rows:list[Any] = self.coronary_cursor.fetchall()
self.data_cursor.execute(query)
rows:list[Any] = self.data_cursor.fetchall()

if len(rows) == 0:
return
Expand All @@ -128,7 +127,7 @@ def annotate (self, input_data):
if gen_set == row_gen:
task:tuple = (rsid, row[1], allele, genome, row[3], float(row[4]), row[5], row[6], row[7],
row[8], self.get_color(row[4], 0.6))
self.longevity_cursor.execute(self.sql_insert, task)
self.result_cursor.execute(self.sql_insert, task)

return {"col1":""}

Expand Down

0 comments on commit 7f777bd

Please sign in to comment.