virgil/commit

ADD API for contingency table

authorMichiel Hildebrand
Mon Jul 15 22:38:12 2013 +0200
committerMichiel Hildebrand
Mon Jul 15 22:38:12 2013 +0200
commitb29e036643f59c8a3a4c99af61005da9f2a2bd2c
tree096b473091b5ad7f88536acb9494e09c7265eec6
parent6f914053a1351c78923fa4322307e05692867226
Diff style: patch stat
diff --git a/api/reports.pl b/api/reports.pl
index 85496bf..6cf2920 100644
--- a/api/reports.pl
+++ b/api/reports.pl
@@ -13,7 +13,7 @@
 
 :- http_handler(cliopatria(aers/api/reports), http_reports, []).
 :- http_handler(cliopatria(aers/api/cc), http_cc, []).
-
+:- http_handler(cliopatria(aers/api/table), http_contingency_table, []).
 
 %%	http_reports(+Request)
 
@@ -42,6 +42,8 @@ http_reports(Request) :-
 			 reaction=Reactions,
 			 reports=Reports_Limit])).
 
+%%	http_cc(+Request)
+
 http_cc(Request) :-
 	http_parameters(Request,
 			[drug(Drug,
@@ -133,3 +135,46 @@ cc_json(cc(Report,Drug,Reaction),
 	json([report=Report,drug=Drug,reaction=Reaction])).
 
 
+%%	http_contingency_table(+Request)
+%
+%
+
+http_contingency_table(Request) :-
+	http_parameters(Request,
+			[drug(Drug,
+			      [atom
+			      ]),
+			 reaction(Reaction,
+				  [atom
+				  ]),
+			 drugMatch(DrugMatch0,
+				   [list(atom)
+				   ])
+			]),
+	(   DrugMatch0 = []
+	->  DrugMatch = [word,brand,synonym,corrected]
+	;   DrugMatch = DrugMatch0
+	),
+	drug_match(Drug, DrugMatch, DrugMentions),
+	drug_reaction_cc(DrugMentions, Reaction, CC_A),
+	drug_reaction_cc(DrugMentions, all, CC_AC),
+	drug_reaction_cc([], Reaction, CC_AB),
+	cc_count(ABCD),
+	length(CC_A, A),
+	length(CC_AB, AB),
+	length(CC_AC, AC),
+	B is AB-A,
+	C is AC-A,
+	CD is ABCD-AB,
+	BD is ABCD-AC,
+	D is BD-B,
+	reply_json(json([a=A,
+			 b=B,
+			 c=C,
+			 d=D,
+			 ab=AB,
+			 ac=AC,
+			 bd=BD,
+			 cd=CD,
+			 abcd=ABCD
+			])).