-
Notifications
You must be signed in to change notification settings - Fork 1
/
omop_ingest.py
125 lines (110 loc) · 4.41 KB
/
omop_ingest.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
import json
import sys
import psycopg2.extras
import pyodbc
import requests
from requests.auth import HTTPBasicAuth
if __name__ == "__main__":
solr_url = "http://localhost:8983/solr/sample"
auth = None
db_host = 'localhost'
db_port = '5432'
db_name = 'omop'
db_user = 'pg'
db_password = 'pg'
db_dialect = 'postgresql'
conn = None
cursor = None
try:
if len(sys.argv) < 2:
print()
print('Please run with the following command line args:')
print('\tpython3 omop_ingest.py <solr_url> <solr_user> <solr_password> <db_host> <db_name> '
'<db_user> <db_password> <db_dialect> <db_port>')
print()
print('e.g.:')
print('\tpython3 omop_ingest.py https://solr.internal.claritynlp.cloud/solr/sample admin test mydb.org '
' mimic admin password postgresql 5432')
print()
sys.exit(0)
if len(sys.argv) > 3:
solr_user = sys.argv[2]
solr_password = sys.argv[3]
if len(solr_user) == 0 and len(solr_password) == 0:
auth = None
else:
auth = HTTPBasicAuth(solr_user, solr_password)
if len(sys.argv) > 9:
db_host = sys.argv[4]
db_name = sys.argv[5]
db_user = sys.argv[6]
db_password = sys.argv[7]
db_dialect = sys.argv[8]
db_port = sys.argv[9]
else:
db_host = 'localhost'
db_port = '5432'
db_name = 'omop'
db_user = 'pg'
db_password = 'pg'
db_dialect = 'postgresql'
if len(sys.argv) > 2:
solr_url = sys.argv[1]
if db_dialect == 'sql_server':
conn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=%s;"
"Database=%s;"
"uid=%s;pwd=%s" % (db_host, db_name, db_user, db_password))
cursor = conn.cursor()
elif db_dialect.startswith('postgres') or db_dialect == 'pg':
conn_string = "host='%s' dbname='%s' user='%s' password='%s' port=%s" % (db_host,
db_name,
db_user,
db_password,
db_port)
# Connecting to the OMOP DB
conn = psycopg2.connect(conn_string)
cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
else:
dialects = ['sql_server', 'postgresql']
dialect_str = ', '.join(dialects)
print('please select a valid dialect, Options are:', dialect_str)
if cursor:
# SOLR upload headers
# url = solr_url + '/update/json'
url = solr_url + '/update?commit=true'
headers = {
'Content-type': 'application/json',
}
# Extracting information - detailed_descriptions
cursor.execute(
"""
select c.concept_name as report_type, n.* from note n
LEFT OUTER JOIN concept c
on n.note_type_concept_id = c.concept_id
""")
result = cursor.fetchall()
result_list = []
for i in result:
d = {"subject": i['person_id'],
"source": "OMOP Database",
"report_type": i['report_type'],
"report_text": i['note_text'],
"report_id": i['note_id'],
"id": i['note_id'],
"report_date": str(i['note_date'])
}
result_list.append(d)
# Pushing data to Solr
data = json.dumps(result_list)
response = requests.post(url, headers=headers, data=data)
# Verifying upload
if response.status_code == 200:
print("Uploaded OMOP notes")
else:
print("Couldn't upload OMOP notes. Reason: " + str(response.reason))
except Exception as ex1:
print(ex1)
finally:
if conn:
conn.close()