forked from Clinical-Genomics/NIPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_setup.py
149 lines (126 loc) · 5.27 KB
/
database_setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/user/bin/env python
DESC="""Script to set up the NIPT-database. Fetching info from sample sheets
for sequencing, NIPT analysisi results from ...???
Written by Maya Brandi"""
from argparse import ArgumentParser
import json
import csv
import logging
import os
import sys
import glob
from extentions import app
from database import db, Batch, NCV, Coverage, Sample, User ,BatchStat
class BatchMaker():
def __init__(self, nipt_db):
self.db = nipt_db
self.batch_id = None
self.batch_name = None
self.date = None
self.flowcell = None
self.nipt_results = None
self.sample_sheet = None
def get_run_folder_info(self):
try:
print self.sample_sheet
folder = self.sample_sheet.split('/')[-2]
f_name_info = folder.split('_')
self.batch_id = f_name_info[3]
self.date = f_name_info[0]
self.flowcell = f_name_info[3]
except:
pass
def update_nipt_db(self):
batch = Batch.query.filter_by(batch_id = self.batch_id).first()
if self.sample_sheet and not batch:
self.get_batch_name_from_sample_sheet(self.sample_sheet)
if self.batch_name:
batch = Batch(self.batch_id, self.date, self.flowcell)
batch.batch_name = self.batch_name
self.db.session.add(batch)
self.db.session.commit()
if self.nipt_results and self.batch_name:
reader = csv.DictReader(open(self.nipt_results, 'rb'))
for row in reader:
sample = Sample.query.filter_by(sample_ID = row['SampleID']).first()
if not sample:
sample = Sample(row, batch)
self.db.session.add(sample)
cov = Coverage(row, sample, batch)
self.db.session.add(cov)
ncv = NCV(row, sample, batch)
self.db.session.add(ncv)
batchstat = BatchStat(row, batch)
self.db.session.add(batchstat)
else:
logging.warning("Could not get analysis info from file: %s" % self.nipt_results)
try:
self.db.session.commit()
except:
error = sys.exc_info()
logging.exception('error in update_nipt_db!!!!!!!')
logging.error(error)
pass
def get_batch_name_from_sample_sheet(self, path):
sheet = open(path, 'rb')
for l in sheet:
if 'Investigator Name' in l:
try:
investigator_name = l.split(',')[1].split('_')
self.batch_name = '_'.join(investigator_name[0:2])
except:
logging.exception("Could not get batch name from sample sheet: %s" %path)
pass
def parse_path(self, flowcell_id):
nipt_results = app.config.get('ANALYSIS_PATH') +'*' + flowcell_id + '*/*NIPT_RESULTS.csv'
sample_sheet = app.config.get('RUN_FOLDER_PATH')+'*' + flowcell_id + '*/SampleSheet.csv'
if glob.glob(nipt_results) and glob.glob(sample_sheet):
self.nipt_results = glob.glob(nipt_results)[0]
self.sample_sheet = glob.glob(sample_sheet)[0]
class NiptDBSetup():
def __init__(self, nipt_db):
self.db = nipt_db
def set_users(self, users_file):
if users_file:
users_data = open(users_file)
users = json.load(users_data)
self.db.create_all()
for usr, inf in users.items():
in_db = User.query.filter_by(email = inf['Email']).first()
if not in_db:
in_db = User.query.filter_by(email = inf['Email']).first()
user = User(inf['Email'],inf['Name'])
self.db.session.add(user)
self.db.session.commit()
def main(flowcell_ids, users_file):
db.init_app(app)
logging.basicConfig(filename = 'NIPT_database_log', level=logging.DEBUG)
db.create_all()
NDBS = NiptDBSetup(db)
NDBS.set_users(users_file)
for flowcell_id in flowcell_ids:
BM = BatchMaker(db)
BM.parse_path(flowcell_id)
BM.get_run_folder_info()
if BM.batch_id:
BM.update_nipt_db()
else:
logging.warning("Could not add to database from run: %s" % flowcell_id)
logging.basicConfig(datefmt='%m/%d/%Y %I:%M:%S %p', filename = 'NIPT_log', level=logging.DEBUG)
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)
if __name__ == '__main__':
parser = ArgumentParser(description=DESC)
parser.add_argument('--flowcell_ids', nargs='+', default = [], dest = 'flowcell_ids',
help ='List of run flowcell ids. Format example: AH23VMADXY')
parser.add_argument('--users', default = None, dest = 'users',
help = 'json file with Users')
args = parser.parse_args()
main(args.flowcell_ids, args.users)