-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_functions.py
More file actions
152 lines (142 loc) · 6.31 KB
/
Copy pathdatabase_functions.py
File metadata and controls
152 lines (142 loc) · 6.31 KB
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
148
149
150
151
152
from mysql.connector import Error
from typing import Dict, Any, List
def insert_trial_data(trial: Dict[str, Any], connection, cursor) -> str:
"""Insert trial and all related objects into database"""
try:
# Insert trial base data
trial_sql = """
INSERT INTO trials (
nct_id, trial_type, brief_title, official_title,
conditions, conditions_mesh_terms, keywords, study_type,
intervention_mesh_terms, eligibility_summary,
trial_source, annotation_status, ai_summary, annotation_time_seconds
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
cursor.execute(trial_sql, (
trial['nct_id'],
trial['type'],
trial['brief_title'],
trial['official_title'],
','.join(trial['conditions']),
','.join(trial['condition_mesh_terms']),
','.join(trial['keywords']),
trial['study_type'],
','.join(trial['intervention_mesh_terms']),
trial.get('eligibility_summary'),
trial.get('trial_source'),
trial.get('annotation_status'),
trial.get('ai_summary'),
trial.get('annotation_time_seconds')
))
trial_id = cursor.lastrowid
# Insert arm groups
for arm in trial['arm_groups']:
arm_sql = """
INSERT INTO arm_groups (
trial_id_arm, arm_group_label, arm_group_type, arm_group_description, intervention_names
) VALUES (%s, %s, %s, %s, %s)
"""
cursor.execute(arm_sql, (
trial_id,
arm['label'],
arm['type'],
arm['description'],
','.join(arm['intervention_names'])
))
# Insert interventions
for intervention in trial['interventions']:
intervention_sql = """
INSERT INTO interventions (
trial_id_int, intervention_type_int, intervention_name, intervention_description, arm_group_labels, other_names
) VALUES (%s, %s, %s, %s, %s, %s)
"""
cursor.execute(intervention_sql, (
trial_id,
intervention['type'],
intervention['name'],
intervention['description'],
','.join(intervention['arm_group_labels']),
','.join(intervention['other_names'])
))
# Insert diseases
if 'diseases' in trial and trial['diseases']:
for disease in trial['diseases']:
disease_sql = """
INSERT INTO diseases (
trial_id_dis, disease_type, disease_name, tissue_type,
disease_mesh_term, oncotree_name, oncotree_main_type
) VALUES (%s, %s, %s, %s, %s, %s, %s)
"""
cursor.execute(disease_sql, (
trial_id,
disease['type'],
disease['disease_name'],
disease['tissue_type'],
disease.get('disease_mesh_term'),
disease.get('Oncotree_name'),
disease.get('Oncotree_mainType')
))
# Insert drugs and their targets
if 'drugs' in trial and trial['drugs']:
for drug in trial['drugs']:
drug_sql = """
INSERT INTO drugs (
trial_id_dru, drug_name, intervention_type, drug_description,
drug_type, nci_thesaurus_concept_id, nci_thesaurus_preferred_term,
nci_thesaurus_definition, drug_mesh_term, drug_category,
drug_class, drug_delivery_route, drug_mechanism_of_action
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
cursor.execute(drug_sql, (
trial_id,
drug['drug_name'],
drug['intervention_type'],
drug['drug_description'],
drug['type'],
drug.get('NCI_thesaurus_concept_id'),
drug.get('NCI_thesaurus_preferred_term'),
drug.get('NCI_thesaurus_definition'),
drug.get('drug_mesh_term'),
drug.get('drug_category'),
drug.get('drug_class'),
drug.get('drug_delivery_route'),
drug.get('drug_mechanism_of_action')
))
drug_id = cursor.lastrowid
# Insert targets
if 'targets' in drug and drug['targets']:
for target in drug['targets']:
target_sql = """
INSERT INTO targets (
drug_id_tar, gene, gene_symbol, hgnc_id, antigen_ligand,
binding_type, cytotoxicity, cytotoxicity_type, cytotoxicity_mechanism,
enzyme_product, epitope, variant, isoform,
hla_specific, gated, other_modifier
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
cursor.execute(target_sql, (
drug_id,
target['gene'],
target['gene_symbol'],
target['HGNC_id'],
target['antigen_ligand'],
target['binding_type'],
target.get('cytotoxicity'),
target.get('cytotoxicity_type'),
target.get('cytotoxicity_mechanism'),
target['enzyme_product'],
target['epitope'],
target['variant'],
target['isoform'],
target['HLA_specific'],
target['gated'],
target.get('other_modifier')
))
connection.commit()
print(f"Successfully inserted trial {trial['nct_id']}")
except Error as e:
print(f"Error inserting trial data: {e}")
nct_error = trial['nct_id']
connection.rollback()
return nct_error
return ""