-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dylan Hoogduin
authored and
Dylan Hoogduin
committed
Apr 17, 2020
1 parent
81ed102
commit 86a6877
Showing
3 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import sqlalchemy | ||
|
||
|
||
class MaterialViews(): | ||
|
||
def __init__(self, db): | ||
self.db = db | ||
self.run_mv_transcript() | ||
|
||
def run_mv_transcript(self): | ||
with open('mviews/transcript_mv.sql') as file: | ||
query = file.read() | ||
self.prepare(query) | ||
result = self.db.execute("SELECT id FROM tissue") | ||
tissues = [item for item in result] | ||
for tissue in tissues: | ||
tissue_id = tissue[0] | ||
query = "SELECT gene, tissue, avg(count) from transcript " | ||
query += "WHERE tissue = {tissue} ".format(tissue=tissue_id) | ||
query += "GROUP BY gene " | ||
query += "ORDER BY avg(count) DESC " | ||
query += "LIMIT 100 " | ||
result = self.db.execute(query) | ||
for item in result: | ||
query = "INSERT INTO transcript_mv (gene, tissue, count_avg) " | ||
query += "VALUES ({x[0]}, {x[1]}, {x[2]})".format(x=list(item)) | ||
self.db.execute(query) | ||
self.db.close() | ||
|
||
def prepare(self, file): | ||
sql_command = '' | ||
for line in file: | ||
if not line.startswith('--') and line.strip('\n'): | ||
sql_command += line.strip('\n') | ||
if sql_command.endswith(';'): | ||
try: | ||
self.db.execute(sqlalchemy.text(sql_command)) | ||
finally: | ||
sql_command = '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
DROP TABLE IF EXISTS transcript_mv; | ||
CREATE TABLE transcript_mv ( | ||
gene INT NOT NULL | ||
, tissue INT NOT NULL | ||
, count_avg DECIMAL(10,2) NOT NULL | ||
, UNIQUE INDEX product (gene, tissue) | ||
); |