diff --git a/FormulariesMatching/FormulariesMatching/db_interface.py b/FormulariesMatching/FormulariesMatching/db_interface.py index 19c60de..e7c0bf2 100644 --- a/FormulariesMatching/FormulariesMatching/db_interface.py +++ b/FormulariesMatching/FormulariesMatching/db_interface.py @@ -43,6 +43,7 @@ def check_connection(app): def get_trial_summary(db_conn,nct_id): sql_summary =""" +/*get brief and detailed descriptions*/ select s.nct_id, brief_title , @@ -64,22 +65,39 @@ where c.nct_id = %s ; """ sql_keywords=""" +/*get keywords*/ select nct_id ,downcase_name from ctgov.keywords k where k.nct_id = %s ; """ + + sql_indications=''' +select downcase_mesh_term +from ctgov.browse_interventions bi +where bi.nct_id = %s and mesh_type = 'mesh-list' +''' + + with db_conn.cursor() as curse: curse.execute(sql_summary,[nct_id]) summary = curse.fetchall() curse.execute(sql_keywords,[nct_id]) - keywords = curse.fetchall() + keywords = [ x[1] for x in curse.fetchall()] curse.execute(sql_conditions,[nct_id]) - conditions = curse.fetchall() + conditions = [ x[2] for x in curse.fetchall()] + + curse.execute(sql_indications,[nct_id]) + indications = [ x[0] for x in curse.fetchall()] - return {"summary":summary, "keywords":keywords, "conditions":conditions} + return { + "summary":summary, + "keywords":keywords, + "conditions":conditions, + "indications":indications + } def get_trials_unmatched_to_formularies(db_conn): @@ -106,20 +124,40 @@ where nct_id not in ( select nct_id from "Formularies".uspdc_most_recent_matched_status umrms where status is not null ) -; ''' - uspmmg_sql = ''' Null; ''' + and nct_id in (select distinct nct_id from public.formatted_data_mat fd ) +; +''' + uspmmg_sql = '''\ +/* +Get the trials that have not been proceesed. +First: get most recent matched status +Second: only include those who have non-null status +Third: check list of trials against this and remove any of them. +This leaves unmatched trials. +*/ +select distinct(nct_id) +from "DiseaseBurden".trial_to_icd10 tti +where nct_id not in ( + select nct_id from "Formularies".uspmmg_most_recent_matched_status umrms + where status is not null + ) + and nct_id in (select distinct nct_id from public.formatted_data_mat fd ) +; +''' vaform_sql = ''' Null; ''' #query each formulary, adding data to dict formulary_list = dict() - with db_conn.cursor as curse: + with db_conn.cursor() as curse: # uspdc curse.execute(uspdc_sql) formulary_list["uspdc"] = curse.fetchall() # uspmm + curse.execute(uspmmg_sql) + formulary_list["uspmmg"] = curse.fetchall() # vaform return formulary_list diff --git a/FormulariesMatching/FormulariesMatching/matching.py b/FormulariesMatching/FormulariesMatching/matching.py index b5df1ff..e1ea4b3 100644 --- a/FormulariesMatching/FormulariesMatching/matching.py +++ b/FormulariesMatching/FormulariesMatching/matching.py @@ -12,9 +12,11 @@ import FormulariesMatching.uspdc_db as uspdc import FormulariesMatching.uspmmg_db as uspmmg import FormulariesMatching.vaform_db as vaform +import re + FORMULARIES = { "USP DC":uspdc, -# "USP MMG":uspmmg, + "USP MMG":uspmmg, # "VA Formulary":vaform, } @@ -28,10 +30,14 @@ def get_remaining_trials(): db_conn = get_db() #get list of trials - connection_valid = get_trials_unmatched_to_formularies(db_conn) + unmatched_trials = get_trials_unmatched_to_formularies(db_conn) + unmatched_trials_list = list(set([ x[0] for y in unmatched_trials for x in unmatched_trials[y]] )) #return html - return connection_valid +# return {"formularies":unmatched_trials,"nctids":nctid_list} + return render_template('formulary_index.html', + unmatched_trials=unmatched_trials_list, + ) @bp.route("/", methods=['GET',"POST"]) @@ -62,8 +68,36 @@ def match_trial_to_formulary_groups(nct_id): ) elif request.method == "POST": - pass - #build array of data to insert + + #For each Formulary + translation={"('":None,"')":None} + + for key in request.form.keys(): + match key.split("|"): + case ["select_box", formulary]: + #parse data + result = request.form["select_box|{}".format(formulary)] + if result == 'difficult': + FORMULARIES[formulary].insert_match(db_conn,nct_id,None,None,'difficult') + else: + category,uspclass = [re.sub("['\(\)]","",x).strip() for x in result.split("', '")] + + FORMULARIES[formulary].insert_match(db_conn,nct_id,category,uspclass,'accepted') + + + case ["check_box", data]: + formulary_trial,category,uspclass = [re.sub("['\(\)]","",x).strip() for x in data.split("', '")] + formulary,_ = formulary_trial.split(",") + #Insert data + + FORMULARIES[formulary].insert_match(db_conn,nct_id,category,uspclass,'accepted') + + case _: + return 400 + + return redirect(url_for("formularies.get_remaining_trials")) + + else: raise Exception("HTTP method <{}> not implemented".format(request.method)) diff --git a/FormulariesMatching/FormulariesMatching/templates/formulary_index.html b/FormulariesMatching/FormulariesMatching/templates/formulary_index.html index 459d4bb..fe330b5 100644 --- a/FormulariesMatching/FormulariesMatching/templates/formulary_index.html +++ b/FormulariesMatching/FormulariesMatching/templates/formulary_index.html @@ -8,16 +8,14 @@

Unlinked Trials

- - -{% for trial in unlinked_trials %} - -{% endfor %} -
Trials
- - {{ trial [0] }} - -
+

Linked Trials

diff --git a/FormulariesMatching/FormulariesMatching/templates/trial_formularies.html b/FormulariesMatching/FormulariesMatching/templates/trial_formularies.html index a14158b..be5d855 100644 --- a/FormulariesMatching/FormulariesMatching/templates/trial_formularies.html +++ b/FormulariesMatching/FormulariesMatching/templates/trial_formularies.html @@ -10,18 +10,75 @@ {% block content %} -Based on the drugs associated with a trial, -the following are the formulary group suggestions. +
+

Trial Summary

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Short Title{{ summary['summary'][0][1] }}
Complete Title{{ summary['summary'][0][2] }}
Summary{{ summary['summary'][0][3] }}
Summary 2{{ summary['summary'][0][4] }}
Indicated Drugs +
    + {% for drug in summary['indications'] %} +
  • + + {{drug}} + +
  • + {% endfor %} +
+
Conditions + +
keywords{{ summary['keywords'] }}
+
-
+ +

Matching Classes

{% for formulary in potential_matches %}
-

+

{{ formulary }} -

+

-{% for formulary in potential_matches %} @@ -34,9 +91,9 @@ the following are the formulary group suggestions. @@ -47,22 +104,21 @@ the following are the formulary group suggestions.

If you've determined it belongs to another class - {% for option in class_lists[formulary] %} - {% endfor %} -{% endfor %} {% endfor %} diff --git a/FormulariesMatching/FormulariesMatching/uspdc_db.py b/FormulariesMatching/FormulariesMatching/uspdc_db.py index 60b9688..47e9c7b 100644 --- a/FormulariesMatching/FormulariesMatching/uspdc_db.py +++ b/FormulariesMatching/FormulariesMatching/uspdc_db.py @@ -32,7 +32,7 @@ def get_groups_per_nctid(db_conn, nct_id): ''' pass sql = '''\ -select * from "Formularies".trial_to_uspdc_category_class ttucc +select * from "Formularies".uspdc_trial_to_category_class ttucc where nct_id = %(nctid)s ; ''' @@ -43,5 +43,19 @@ where nct_id = %(nctid)s return curse.fetchall() -def store_trial_to_formulary_group_matches(): - pass +def insert_match(db_conn, nct_id, category,uspclass,status): + sql = '''\ +INSERT INTO "Formularies".uspdc_matching +VALUES +( + %(nct_id)s + ,%(category)s + ,%(uspclass)s + ,%(status)s + ,NOW() +) +; +''' + with db_conn.cursor() as curse: + curse.execute(sql, {"nct_id":nct_id, "category":category, "uspclass":uspclass, "status":status} ) + db_conn.commit() diff --git a/FormulariesMatching/FormulariesMatching/uspmmg_db.py b/FormulariesMatching/FormulariesMatching/uspmmg_db.py index fc9e5e3..02edcaa 100644 --- a/FormulariesMatching/FormulariesMatching/uspmmg_db.py +++ b/FormulariesMatching/FormulariesMatching/uspmmg_db.py @@ -13,23 +13,50 @@ def get_all_formulary_groups(db_conn): ''' sql = '''\ select distinct "USP Category", "USP Class" -from "Formularies".usp_dc ud +from "Formularies".usp_mmg ud order by "USP Category", "USP Class" ; ''' - #query with db_conn.cursor() as curse: curse.execute(sql) return curse.fetchall() + -def get_formulary_groups_per_NCTID(db_conn, nct_id): + + +def get_groups_per_nctid(db_conn, nct_id): ''' Get the list of formulary groups associated with the drugs found in a trial identified by NCTID ''' pass + sql = '''\ +select * from "Formularies".uspmmg_trial_to_category_class ttucc +where nct_id = %(nctid)s +; +''' + #query + with db_conn.cursor() as curse: + curse.execute(sql, {"nctid":nct_id}) + return curse.fetchall() -def store_trial_to_formulary_group_matches(): - pass + + +def insert_match(db_conn, nct_id, category,uspclass,status): + sql = '''\ +INSERT INTO "Formularies".uspmmg_matching +VALUES +( + %(nct_id)s + ,%(category)s + ,%(uspclass)s + ,%(status)s + ,NOW() +) +; +''' + with db_conn.cursor() as curse: + curse.execute(sql, {"nct_id":nct_id, "category":category, "uspclass":uspclass, "status":status} ) + db_conn.commit() diff --git a/FormulariesMatching/start.sh b/FormulariesMatching/start.sh index 6868a58..630b60f 100755 --- a/FormulariesMatching/start.sh +++ b/FormulariesMatching/start.sh @@ -1 +1,2 @@ -waitress-serve --port=5000 --call 'Icd10ConditionsMatching:create_app' +waitress-serve --port=5000 --call 'FormulariesMatching:create_app' +#flask --app FormulariesMatching run --debug

{{ row[1] }} {{ row[2] }}