virgil/commit
ADD cooccurrence API
author | Michiel Hildebrand |
---|---|
Mon Jul 15 21:53:51 2013 +0200 | |
committer | Michiel Hildebrand |
Mon Jul 15 21:53:51 2013 +0200 | |
commit | 6f914053a1351c78923fa4322307e05692867226 |
tree | 3a58e9c3cb7fba33cb653c340a1f8907f3f01915 |
parent | 1191c550bee7a0657b3a45986d86886d19de565b |
Diff style: patch stat
diff --git a/api/drugs.pl b/api/drugs.pl index 29ccab3..616c1b8 100644 --- a/api/drugs.pl +++ b/api/drugs.pl @@ -1,5 +1,7 @@ :- module(drugs, - []). + [drug_mention/4, + drug_brand_name/3, + drug_synonym/3]). :- use_module(library(http/http_dispatch)). :- use_module(library(http/http_parameters)). @@ -31,18 +33,29 @@ http_drug_mentions(Request) :- drug_mentions(Q, Method, Mentions) :- findall(Count-M, - ( setof(R, drug_mention(Method, Q, M, R), Rs), + ( setof(R, ( drug_mention(Method, Q, Lit, Use), + rdf(R, aers:drug, Use), + drug_normalise(Lit, M) + ), + Rs), length(Rs, Count) ), Mentions0), keysort(Mentions0, Mentions1), reverse(Mentions1, Mentions). -drug_mention(exact, Q, Normalised_Lit, Report) :- - rdf(DrugUse, aers:drugname, literal(exact(Q), Lit)), - rdf(Report, aers:drug, DrugUse), - drug_normalise(Lit, Normalised_Lit). -drug_mention(Method, Q, Normalised_Lit, Report) :- +drug_mention(exact, Q, Lit, DrugUse) :- + !, + rdf(DrugUse, aers:drugname, literal(exact(Q), Lit)). +drug_mention(brand, Q, Lit, DrugUse) :- + !, + drug_brand_name(drugbank, Q, Brand), + drug_mention(word, Brand, Lit, DrugUse). +drug_mention(synonym, Q, Lit, DrugUse) :- + !, + drug_synonym(drugbank, Q, Synonym), + drug_mention(word, Synonym, Lit, DrugUse). +drug_mention(Method, Q, Lit, DrugUse) :- ( Method = word -> Query = case(Q) ; Method = stem @@ -59,9 +72,7 @@ drug_mention(Method, Q, Normalised_Lit, Report) :- -> corrected_match(L, Lit, DrugUse) ; rdf(DrugUse, aers:drugname, literal(L)), Lit = L - ), - drug_normalise(Lit, Normalised_Lit), - rdf(Report, aers:drug, DrugUse). + ). corrected_match(Name, Lit, DrugUse) :- rdf(DrugUse, aers:drugname_corrected, literal(Name)), diff --git a/api/reports.pl b/api/reports.pl index 0ba40eb..85496bf 100644 --- a/api/reports.pl +++ b/api/reports.pl @@ -9,8 +9,10 @@ :- use_module(library(semweb/rdf_describe)). :- use_module(library(semweb/rdf_json)). :- use_module(library(aers_report)). +:- use_module(api(drugs)). :- http_handler(cliopatria(aers/api/reports), http_reports, []). +:- http_handler(cliopatria(aers/api/cc), http_cc, []). %% http_reports(+Request) @@ -40,10 +42,94 @@ http_reports(Request) :- reaction=Reactions, reports=Reports_Limit])). +http_cc(Request) :- + http_parameters(Request, + [drug(Drug, + [atom, + optional(true) + ]), + reaction(Reaction, + [atom, + optional(true) + ]), + drugMatch(DrugMatch0, + [list(atom) + ]), + limit(Limit, + [number,default(100)]), + offset(Offset, + [number,default(0)]) + ]), + ( DrugMatch0 = [] + -> DrugMatch = [word,brand,synonym,corrected] + ; DrugMatch = DrugMatch0 + ), + ( var(Drug), + var(Reaction) + -> reply_json(json([error='api'])) + ; ( Drug=all, + Reaction=all + -> cc_count(Total), + CC = [] + ; (nonvar(Drug),\+Drug==all) + -> drug_match(Drug, DrugMatch, DrugMentions), + drug_reaction_cc(DrugMentions, Reaction, CC), + length(CC, Total) + ; nonvar(Reaction) + -> drug_reaction_cc([], Reaction, CC), + length(CC, Total) + ), + list_offset(CC, Offset, CC_Offset), + list_limit(CC_Offset, Limit, CC_Limit, _), + maplist(cc_json, CC_Limit, CC_JSON), + reply_json(json([totalNumberOfResults=Total, + limit=Limit, + offset=Offset, + cc=CC_JSON])) + ). + +drug_match(Drug, Methods, DrugMentions) :- + findall(M, ( member(Method, Methods), + drug_mention(Method, Drug, M, _) + ), + DrugMentions0), + sort(DrugMentions0, DrugMentions). + +drug_reaction_cc([], ReactionMention, []) :- + (var(ReactionMention); ReactionMention==all), + !. +drug_reaction_cc([], ReactionMention, CC) :- + nonvar(ReactionMention), + !, + findall(cc(Report,Drug,Reaction), + ( rdf(Report, aers:reaction, literal(exact(ReactionMention), Reaction)), + rdf(Report, aers:drug, DrugUse), + rdf(DrugUse, aers:drugname, literal(Drug)) + ), + CC0), + sort(CC0, CC). +drug_reaction_cc(DrugMentions, ReactionMention, CC) :- + DrugMentions = [_|_], + !, + ( (var(ReactionMention); ReactionMention==all) + -> ReactionLit = literal(Reaction) + ; ReactionLit = literal(exact(ReactionMention), Reaction) + ), + findall(cc(Report,Drug,Reaction), + ( member(Drug, DrugMentions), + rdf(DrugUse, aers:drugname, literal(Drug)), + rdf(Report, aers:drug, DrugUse), + rdf(Report, aers:reaction, ReactionLit) + ), + CC0), + sort(CC0, CC). + + report_obj(R, JSON) :- resource_CBD(rdf, R, Graph), graph_json(Graph, JSON). - +cc_json(cc(Report,Drug,Reaction), + json([report=Report,drug=Drug,reaction=Reaction])). diff --git a/lib/aers_report.pl b/lib/aers_report.pl index 5e2929f..3dc944c 100644 --- a/lib/aers_report.pl +++ b/lib/aers_report.pl @@ -78,10 +78,11 @@ reaction_count(Reaction, Count) :- virgil_cache(reaction(Reaction), Count), !. reaction_count(Reaction, Count) :- - setof(R-D, (report([], [Reaction], [], R), - report_drug_name(R, D) - ), - Es), + findall(R-D, (report([], [Reaction], [], R), + report_drug_name(R, D) + ), + Es0), + sort(Es0, Es), length(Es, Count), assert(virgil_cache(reaction(Reaction), Count)). @@ -91,7 +92,8 @@ cc_count(Count) :- virgil_cache(cc, Count), !. cc_count(Count) :- - setof(t(R,D,E), report_drug_reaction([], R, D, E), As), + findall(t(R,D,E), report_drug_reaction([], R, D, E), As0), + sort(As0, As), length(As, Count), assert(virgil_cache(cc, Count)).