virgil/commit

UPDATE directly fetch the mentions for the brand and synonyms

authorMichiel Hildebrand
Tue Mar 26 13:04:43 2013 +0100
committerMichiel Hildebrand
Tue Mar 26 13:11:01 2013 +0100
commit34cdffa29d09ac47b2007fd67eaf3c462e290e33
tree04c5a7097ffe8b25e1c5424eefde118f4a222e22
parentc1fadfcfa3bd27dec6479d88f18c8cad7280e5cf
Diff style: patch stat
diff --git a/api/drugs.pl b/api/drugs.pl
index ac5677d..5ba3a22 100644
--- a/api/drugs.pl
+++ b/api/drugs.pl
@@ -11,13 +11,14 @@
 :- http_handler(cliopatria(aers/api/drug/mentions), http_drug_mentions, []).
 :- http_handler(cliopatria(aers/api/drug/brands), http_drug_brands, []).
 :- http_handler(cliopatria(aers/api/drug/synonyms), http_drug_synonyms, []).
+:- http_handler(cliopatria(aers/api/drug/brand/mentions), http_drug_brand_mentions, []).
+:- http_handler(cliopatria(aers/api/drug/synonym/mentions), http_drug_synonym_mentions, []).
 
 %%	http_drug_mentions(+Request)
 
 http_drug_mentions(Request) :-
 	http_parameters(Request,
-			['q[]'(Q,
-			       [list(atom)]),
+			[q(Q, []),
 			 method(Method,
 			       [default(word),
 				one_of([exact,word,stem,sounds,corrected])
@@ -27,12 +28,9 @@ http_drug_mentions(Request) :-
 	maplist(mention_json, Mentions, JSON),
 	reply_json(JSON).
 
-mention_json(Count-Name, json([name=Name, reports=Count])).
-
-drug_mentions(Qs, Method, Mentions) :-
+drug_mentions(Q, Method, Mentions) :-
 	findall(Count-M,
-		(   member(Q, Qs),
-		    setof(R, drug_mention(Method, Q, M, R), Rs),
+		(   setof(R, drug_mention(Method, Q, M, R), Rs),
 		    length(Rs, Count)
 		),
 		Mentions0),
@@ -61,6 +59,12 @@ drug_mention(Method, Q, Lit, Report) :-
 	),
 	rdf(Report, aers:drug, DrugUse).
 
+drug_list_mentions([], _, []).
+drug_list_mentions([Q|Qs], Method, [Q-Mentions|Rest]) :-
+	drug_mentions(Q, Method, Mentions),
+	drug_list_mentions(Qs, Method, Rest).
+
+
 
 %%	http_drug_brands(+Request)
 
@@ -77,22 +81,36 @@ http_drug_brands(Request) :-
 	sort(BrandNames0, BrandNames),
 	reply_json(BrandNames).
 
+http_drug_brand_mentions(Request) :-
+	http_parameters(Request,
+			[q(Q,
+			   []),
+			 source(Source,
+				[oneof([drugbank,orangebook]),
+				 default(drugbank)
+				]),
+			 method(Method,
+			       [default(word),
+				one_of([exact,word,stem,sounds,corrected])
+			       ])
+			]),
+	findall(BN, drug_brand_name(Source, Q, BN), BrandNames0),
+	sort(BrandNames0, BrandNames),
+	drug_list_mentions(BrandNames, Method, Brand_Mentions),
+	maplist(drug_mention_json, Brand_Mentions, JSON),
+	reply_json(JSON).
+
+
 drug_brand_name(drugbank, Q, BrandName) :-
 	downcase_atom(Q, DQ),
-	(   rdf(Q, rdf:type, drugbank:'drugbank/drugs')
-	->  Drug = Q
-	;   rdf(Drug, drugbank:'drugbank/genericName', literal(exact(Q),_))
-	),
+	drugbank_by_name(Q, Drug),
 	rdf(Drug, drugbank:'drugbank/brandName', BrandName0),
 	literal_text(BrandName0, BrandName),
 	downcase_atom(BrandName, DB),
 	DQ \== DB.
 drug_brand_name(orangebook, Q, BrandName) :-
 	downcase_atom(Q, DQ),
-	(   rdf(Q, rdf:type, ob:'Ingredient')
-	->  Drug = Q
-	;   rdf(Drug, ob:'ingredientName', literal(exact(Q),_))
-	),
+	orange_book_by_name(Q, Drug),
 	rdf(Dose, ob:hasIngredient, Drug),
 	rdf(Product, ob:hasDose, Dose),
 	rdf(Product, ob:tradeName, BrandName0),
@@ -101,6 +119,18 @@ drug_brand_name(orangebook, Q, BrandName) :-
 	DQ \== DB.
 
 
+drugbank_by_name(Drug, Drug) :-
+	rdf(Drug, rdf:type, drugbank:'drugbank/drugs'),
+	!.
+drugbank_by_name(Q, Drug) :-
+	rdf(Drug, drugbank:'drugbank/genericName', literal(exact(Q),_)).
+
+orange_book_by_name(Drug, Drug) :-
+	rdf(Drug, rdf:type, ob:'Ingredient'),
+	!.
+orange_book_by_name(Q, Drug) :-
+	rdf(Drug, ob:'ingredientName', literal(exact(Q),_)).
+
 %%	http_drug_synonyms(+Request)
 
 http_drug_synonyms(Request) :-
@@ -115,13 +145,39 @@ http_drug_synonyms(Request) :-
 	sort(Synonyms0, Synonyms),
 	reply_json(Synonyms).
 
+http_drug_synonym_mentions(Request) :-
+	http_parameters(Request,
+			[q(Q,
+			   []),
+			 source(Source,
+				[oneof([drugbank,orangebook]),
+				 default(drugbank)
+				]),
+			 method(Method,
+			       [default(word),
+				one_of([exact,word,stem,sounds,corrected])
+			       ])
+			]),
+	findall(BN, drug_synonym(Source, Q, BN), Synonyms0),
+	sort(Synonyms0, Synonyms),
+	drug_list_mentions(Synonyms, Method, Synonym_Mentions),
+	maplist(drug_mention_json, Synonym_Mentions, JSON),
+	reply_json(JSON).
+
 drug_synonym(drugbank, Q, Synonym) :-
-	(   rdf(Q, rdf:type, drugbank:'drugbank/drugs')
-	->  Drug = Q
-	;   rdf(Drug, drugbank:'drugbank/synonym', literal(exact(Q),_))
-	),
+	downcase_atom(Q, DQ),
+	drugbank_by_name(Q, Drug),
 	rdf(Drug, drugbank:'drugbank/synonym', Synonym0),
 	literal_text(Synonym0, Synonym),
-	Q \== Synonym.
+	downcase_atom(Synonym, DS),
+	DQ \== DS.
+
 
+		 /*******************************
+		 *	      JSON terms	*
+		 *******************************/
+
+mention_json(Count-Name, json([name=Name, reports=Count])).
 
+drug_mention_json(Drug-Mentions, json([drug=Drug, mentions=JSON])) :-
+	maplist(mention_json, Mentions, JSON).
diff --git a/api/drugs.pl~ b/api/drugs.pl~
new file mode 100644
index 0000000..5797f96
--- /dev/null
+++ b/api/drugs.pl~
@@ -0,0 +1,183 @@
+:- module(drugs,
+	  []).
+
+:- use_module(library(http/http_dispatch)).
+:- use_module(library(http/http_parameters)).
+:- use_module(library(semweb/rdf_db)).
+:- use_module(library(http/http_json)).
+:- use_module(library(semweb/rdf_litindex)).
+:- use_module(library(semweb/rdf_label)).
+
+:- http_handler(cliopatria(aers/api/drug/mentions), http_drug_mentions, []).
+:- http_handler(cliopatria(aers/api/drug/brands), http_drug_brands, []).
+:- http_handler(cliopatria(aers/api/drug/synonyms), http_drug_synonyms, []).
+:- http_handler(cliopatria(aers/api/drug/brand/mentions), http_drug_brand_mentions, []).
+:- http_handler(cliopatria(aers/api/drug/synonym/mentions), http_drug_synonym_mentions, []).
+
+%%	http_drug_mentions(+Request)
+
+http_drug_mentions(Request) :-
+	http_parameters(Request,
+			[q(Q, []),
+			 method(Method,
+			       [default(word),
+				one_of([exact,word,stem,sounds,corrected])
+			       ])
+			]),
+	drug_mentions(Q, Method, Mentions),
+	maplist(mention_json, Mentions, JSON),
+	reply_json(JSON).
+
+drug_mentions(Q, Method, Mentions) :-
+	findall(Count-M,
+		(   setof(R, drug_mention(Method, Q, M, R), Rs),
+		    length(Rs, Count)
+		),
+		Mentions0),
+	keysort(Mentions0, Mentions1),
+	reverse(Mentions1, Mentions).
+
+drug_mention(exact, Q, Literal, Report) :-
+	rdf(DrugUse, aers:drugname, literal(exact(Q), Literal)),
+	rdf(Report, aers:drug, DrugUse).
+drug_mention(Method, Q, Lit, Report) :-
+	(   Method = word
+	->  Query = case(Q)
+	;   Method = stem
+	->  Query = stem(Q)
+	;   Method = sounds
+	->  Query = sounds(Q)
+	;   Method = corrected
+	->  Query = case(Q)
+	),
+	!,
+	rdf_find_literals(Query, Literals),
+	member(Lit, Literals),
+	(   Method = corrected
+	->  rdf(DrugUse, aers:drugname_corrected, literal(Lit))
+	;   rdf(DrugUse, aers:drugname, literal(Lit))
+	),
+	rdf(Report, aers:drug, DrugUse).
+
+drug_list_mentions([], _, []).
+drug_list_mentions([Q|Qs], Method, [Q-Mentions|Rest]) :-
+	drug_mentions(Q, Method, Mentions),
+	drug_list_mentions(Qs, Method, Rest).
+
+
+
+%%	http_drug_brands(+Request)
+
+http_drug_brands(Request) :-
+	http_parameters(Request,
+			[q(Q, []),
+			 source(Source,
+				[oneof([drugbank,orangebook]),
+				 default(drugbank)
+				])
+
+			]),
+	findall(BN, drug_brand_name(Source, Q, BN), BrandNames0),
+	sort(BrandNames0, BrandNames),
+	reply_json(BrandNames).
+
+http_drug_brand_mentions(Request) :-
+	http_parameters(Request,
+			[q(Q,
+			   []),
+			 source(Source,
+				[oneof([drugbank,orangebook]),
+				 default(drugbank)
+				]),
+			 method(Method,
+			       [default(word),
+				one_of([exact,word,stem,sounds,corrected])
+			       ])
+			]),
+	findall(BN, drug_brand_name(Source, Q, BN), BrandNames0),
+	sort(BrandNames0, BrandNames),
+	drug_list_mentions(BrandNames, Method, Brand_Mentions),
+	maplist(drug_mention_json, Brand_Mentions, JSON),
+	reply_json(JSON).
+
+
+drug_brand_name(drugbank, Q, BrandName) :-
+	downcase_atom(Q, DQ),
+	drugbank_by_name(Q, Drug),
+	rdf(Drug, drugbank:'drugbank/brandName', BrandName0),
+	literal_text(BrandName0, BrandName),
+	downcase_atom(BrandName, DB),
+	DQ \== DB.
+drug_brand_name(orangebook, Q, BrandName) :-
+	downcase_atom(Q, DQ),
+	orange_book_by_name(Q, Drug),
+	rdf(Dose, ob:hasIngredient, Drug),
+	rdf(Product, ob:hasDose, Dose),
+	rdf(Product, ob:tradeName, BrandName0),
+	literal_text(BrandName0, BrandName),
+	downcase_atom(BrandName, DB),
+	DQ \== DB.
+
+
+drugbank_by_name(Drug, Drug) :-
+	rdf(Drug, rdf:type, drugbank:'drugbank/drugs'),
+	!.
+drugbank_by_name(Q, Drug) :-
+	rdf(Drug, drugbank:'drugbank/genericName', literal(exact(Q),_)).
+
+orange_book_by_name(Drug, Drug) :-
+	rdf(Drug, rdf:type, ob:'Ingredient'),
+	!.
+orange_book_by_name(Q, Drug) :-
+	rdf(Drug, ob:'ingredientName', literal(exact(Q),_)).
+
+%%	http_drug_synonyms(+Request)
+
+http_drug_synonyms(Request) :-
+	http_parameters(Request,
+			[q(Q, []),
+			 source(Source,
+				[oneof([drugbank,orangebook]),
+				 default(drugbank)
+				])
+			]),
+	findall(BN, drug_synonym(Source, Q, BN), Synonyms0),
+	sort(Synonyms0, Synonyms),
+	reply_json(Synonyms).
+
+http_drug_brand_mentions(Request) :-
+	http_parameters(Request,
+			[q(Q,
+			   []),
+			 source(Source,
+				[oneof([drugbank,orangebook]),
+				 default(drugbank)
+				]),
+			 method(Method,
+			       [default(word),
+				one_of([exact,word,stem,sounds,corrected])
+			       ])
+			]),
+	findall(BN, drug_synonym(Source, Q, BN), Synonyms0),
+	sort(Synonyms0, Synonyms),
+	drug_list_mentions(Synonyms, Method, Synonym_Mentions),
+	maplist(drug_mention_json, Synonym_Mentions, JSON),
+	reply_json(JSON).
+
+drug_synonym(drugbank, Q, Synonym) :-
+	downcase_atom(Q, DQ),
+	drugbank_by_name(Q, Drug),
+	rdf(Drug, drugbank:'drugbank/synonym', Synonym0),
+	literal_text(Synonym0, Synonym),
+	downcase_atom(Synonym, DS),
+	DQ \== DS.
+
+
+		 /*******************************
+		 *	      JSON terms	*
+		 *******************************/
+
+mention_json(Count-Name, json([name=Name, reports=Count])).
+
+drug_mention_json(Drug-Mentions, json([drug=Drug, mentions=JSON])) :-
+	maplist(mention_json, Mentions, JSON).
diff --git a/api/measurements.pl~ b/api/measurements.pl~
new file mode 100644
index 0000000..07b69cb
--- /dev/null
+++ b/api/measurements.pl~
@@ -0,0 +1,53 @@
+:- module(measurements,
+	  []).
+
+:- use_module(library(http/http_dispatch)).
+:- use_module(library(http/http_parameters)).
+:- use_module(library(http/http_json)).
+:- use_module(library(aers_report)).
+:- use_module(library(count)).
+
+:- http_handler(cliopatria(aers/api/measurements/prr), http_measurements_prr, []).
+
+http_measurements_prr(Request) :-
+	http_parameters(Request,
+			['drugnames[]'(Drugs,
+				   [list(atom)]),
+			 'reactions[]'(Reactions,
+				       [list(atom)
+				       ])
+			]),
+	prr_per_reaction(Reactions, Drugs, PRRs),
+	reply_json(PRRs).
+
+
+prr_per_reaction([], _, []).
+prr_per_reaction([Reaction|Rs], Drugs, [Obj|Os]) :-
+	Obj = json([reaction=Reaction, prr=PRR, prov=Prov]),
+	prr(Drugs, Reaction, PRR, Prov),
+	prr_per_reaction(Rs, Drugs, Os).
+
+prr(Drugs, Reaction, PRR, Prov) :-
+	% all drug cooccurrences
+	setof(R-Report, reaction(Drugs, [], R, Report), DRs),
+	length(DRs, D),
+
+	% drug-reaction cooccurrences
+	proof_count(member(Reaction-_, DRs), DE),
+
+	setof(R, report([], [Reaction], [], R), Es),
+	length(Es, E),
+
+	setof(R, report([], [], [], R), As),
+	length(As, A),
+
+	PRR is (DE/D)/((E-DE)/(A-D)),
+
+	Prov = json([drug=D,
+		     reaction=E,
+		     drugreaction=DE,
+		     all=A
+		    ]).
+	%Prov = (DE/D)/((E-DE)/(A-D)).
+
+
diff --git a/api/reactions.pl~ b/api/reactions.pl~
new file mode 100644
index 0000000..5c13f8a
--- /dev/null
+++ b/api/reactions.pl~
@@ -0,0 +1,73 @@
+:- module(reactions,
+	  []).
+
+:- use_module(library(http/http_dispatch)).
+:- use_module(library(http/http_parameters)).
+:- use_module(library(semweb/rdf_db)).
+:- use_module(library(semweb/rdf_label)).
+:- use_module(library(util)).
+:- use_module(library(http/http_json)).
+:- use_module(library(aers_report)).
+
+:- http_handler(cliopatria(aers/api/reactions), http_reactions, []).
+:- http_handler(cliopatria(aers/api/reaction/count), http_reaction_count, []).
+
+%%	http_reactions(+Request)
+
+http_reactions(Request) :-
+	http_parameters(Request,
+			['drugnames[]'(DrugNames,
+			      [list(atom)
+			      ]),
+			 limit(Limit,
+			       [default(100)]),
+			 offset(Offset,
+				[default(0)])
+			]),
+	findall(Count-Reaction,
+		(   setof(R, reaction(DrugNames, [], Reaction, R), Rs),
+		    length(Rs, Count)
+		),
+		Reactions0),
+	keysort(Reactions0, Reactions1),
+	reverse(Reactions1, Reactions),
+	length(Reactions, Total_Reactions),
+	list_offset(Reactions, Offset, Reactions_Offset),
+	list_limit(Reactions_Offset, Limit, Reactions_Limit, _),
+	maplist(reaction_obj, Reactions_Limit, Reactions_JSON),
+	reply_json(json([totalNumberOfResults=Total_Reactions,
+			 reactions=Reactions_JSON])).
+
+reaction_obj(C-R, json([reaction=R, count=C])).
+
+reaction(Drugs, Filter, Reaction, Report) :-
+	(   Drugs = [_|_]
+	->  report_by_drug(Drugs, Report)
+	;   true
+	),
+	report_by_filter(Filter, Report),
+	rdf(Report, aers:reaction, Reaction_Lit),
+	literal_text(Reaction_Lit, Reaction).
+
+%%	http_reaction_count(+Request)
+
+http_reaction_count(Request) :-
+	http_parameters(Request,
+			[drug(Drugs,
+			      [list(atom)
+			      ]),
+			 reaction(Reactions,
+				  [list(atom)
+				  ])
+			]),
+	(   Reactions = []
+	->  setof(Report-Reaction,
+		  reaction(Drugs, [], Reaction, Report),
+		  Rs),
+	    length(Rs, Count)
+	;   setof(R, report(Drugs, Reactions, [], R), Rs),
+	    length(Rs, Count)
+	),
+	reply_json(json([drugs=Drugs,
+			 reactions=Reactions,
+			 reaction_count=Count])).
diff --git a/lib/aers_report.pl~ b/lib/aers_report.pl~
new file mode 100644
index 0000000..7fb39de
--- /dev/null
+++ b/lib/aers_report.pl~
@@ -0,0 +1,32 @@
+:- module(aers_report,
+	  [report/4,
+	   report_by_drug/2,
+	   report_by_filter/2
+	  ]).
+
+:- use_module(library(semweb/rdf_db)).
+
+report(Drugs, Reactions, Filter, Report) :-
+	(   Drugs = [_|_]
+	->  report_by_drug(Drugs, Report)
+	;   true
+	),
+	(   Reactions = [_|_]
+	->  report_by_reaction(Reactions, Report)
+	;   true
+	),
+	report_by_filter(Filter, Report).
+
+report_by_drug([Drug|_], Report) :-
+	rdf(DrugUse, aers:drugname, literal(exact(Drug),_)),
+	rdf(Report, aers:drug, DrugUse).
+report_by_drug([_|T], Report) :-
+	report_by_drug(T, Report).
+
+report_by_reaction([Reaction|_], Report) :-
+	rdf(Report, aers:reaction, literal(exact(Reaction),_)).
+report_by_reaction([_|T], Report) :-
+	report_by_reaction(T, Report).
+
+report_by_filter([], Report) :-
+	rdf(Report, rdf:type, aers:'Report').
diff --git a/web/html/wizard.html b/web/html/wizard.html
index 5ff0212..ecbf1da 100644
--- a/web/html/wizard.html
+++ b/web/html/wizard.html
@@ -104,17 +104,18 @@
 						</section>
 
 						<section id="brands" class="loader">
-						<h5>Brand names</h5>	
+							<h5>Brand names</h5>	
+							<ul id="brand-list"></ul>
 							<div class="btn-group">
 								<button class="btn" data-toggle="select" data-toggle-name="brand-labels">select all</button>
 								<button class="btn" data-toggle="unselect" data-toggle-name="brand-labels">unselect all</button>
 							</div>
-							<ul id="brand-list"></ul>
 							<div class="labelbox" name="brand-labels"></div>
 						</section>
 				
 						<section id="synonyms" class="loader">
-						<h5>Drug synonyms</h5>
+							<h5>Drug synonyms</h5>
+							<ul id="synonym-list"></ul>
 							 <div class="btn-group">
 								<button class="btn" data-toggle="select" data-toggle-name="synonym-labels">select all</button>
 								<button class="btn" data-toggle="unselect" data-toggle-name="synonym-labels">unselect all</button>
@@ -163,8 +164,8 @@
 	
 	<script>
 		var mentionsURL = '../aers/api/drug/mentions',
-			brandsURL = '../aers/api/drug/brands',
-			synonymsURL = '../aers/api/drug/synonyms',
+			brandsURL = '../aers/api/drug/brand/mentions',
+			synonymsURL = '../aers/api/drug/synonym/mentions',
 			reactionsURL = '../aers/api/reactions';
 			prrURL = '../aers/api/measurements/prr';
 		
@@ -191,22 +192,39 @@
 				return reactions;
 			}
 			
-			$.fn.updateDrugNames = function(drugs) {
+			$.fn.appendMention = function(mention) {
+				$(this).append(
+					'<label class="checkbox"><input type="checkbox" value="'+mention.name+'" checked>'
+					+mention.name+'<span class="badge">'+mention.reports+'</span></label>'
+				);
+			};
+			
+			$.fn.appendMentionGroup = function(group) {
+				node = $(this);
+				node.append('<h6>'+group.drug+'</h6>');
+				$(group.mentions).each(function(i, e) {
+					node.appendMention(e);
+				});
+			};	
+			
+			$.fn.updateDrugNames = function(drug, url) {
 				var labelbox = $(this).find(".labelbox"),
 					loader = $(this).find(".spinner");
 				loader.show();
-				$.ajax(mentionsURL, {
+				$.ajax(url, {
 					dataType:"json",
-					data:{q:drugs},
+					data:{q:drug},
 					error: function() {
 						loader.hide();
 					},
 					success: function(o) {
 						loader.hide();
 						$(o).each(function(i, e) {
-							labelbox.append(
-								'<label class="checkbox"><input type="checkbox" value="'+e.name+'" checked>'
-								+e.name+'<span class="badge">'+e.reports+'</span></label>');
+							if(e.drug) {
+								labelbox.appendMentionGroup(e);
+							} else {
+								labelbox.appendMention(e);
+							}
 						});
 					}
 				});
@@ -289,24 +307,9 @@
 			
 			var drug = getURLParameter("drug");
 			$("#drug").html(drug);
-			$('#mentions').updateDrugNames([drug]);
-			$.ajax(brandsURL, {
-				dataType:"json",
-				data:{q:drug},
-				success: function(o) {
-					$(o).each(function(i,e) {
-						$('brand-list').append('<li><a href="#">'+e+'</a></li>');
-					});
-					$('#brands').updateDrugNames(o);
-				}
-			});
-			$.ajax(synonymsURL, {
-				dataType:"json",
-				data:{q:drug},
-				success: function(o) {
-					$('#synonyms').updateDrugNames(o);
-				}
-			});
+			$('#mentions').updateDrugNames(drug, mentionsURL);
+			$('#brands').updateDrugNames(drug, brandsURL);
+			$('#synonyms').updateDrugNames(drug, synonymsURL);
 			
 			$("#wizard").on('change', function(e,o) {
 				// we only have to update the reactions if we come from the drugs