Skip to content

Commit

Permalink
Re-Added jobs/epha_interactions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ngiger committed Mar 11, 2015
1 parent 9f99332 commit eca7594
Show file tree
Hide file tree
Showing 12 changed files with 390 additions and 63 deletions.
4 changes: 2 additions & 2 deletions spec/doctors_spec.rb
Expand Up @@ -81,8 +81,8 @@ def enter_search_to_field_by_name(search_text, field_name)
}
# Check map link
@browser.link(:text => /map.search/).click
@browser.url.should match /bahnhofstr/
@browser.url.should match /8753-mollis/
@browser.url.should match /bahnhofstr/i
@browser.url.should match /8753-mollis/i
@browser.back
# go back to search result
@browser.back
Expand Down
2 changes: 1 addition & 1 deletion spec/pharmacies_spec.rb
Expand Up @@ -68,7 +68,7 @@ def enter_search_to_field_by_name(search_text, field_name)
inhalt.should match "Zaunplatz 2"
inhalt.should match "8750 Glarus"
@browser.link(:text => /map.search/).click
@browser.url.should match /8750-glarus\/zaunplatz-2/
@browser.url.should match /8750-glarus\/zaunplatz-2/i
@browser.back
# go back to search result
@browser.back
Expand Down
62 changes: 17 additions & 45 deletions src/model/epha_interaction.rb
Expand Up @@ -26,49 +26,11 @@ module EphaInteractions
def EphaInteractions.get
@@epha_interactions
end
def EphaInteractions.set(interactions)
@@epha_interactions = interactions
end
CSV_FILE = File.expand_path('../../data/csv/interactions_de_utf8.csv', File.dirname(__FILE__))
CSV_ORIGIN_URL = 'https://download.epha.ch/data/matrix/matrix.csv'
def EphaInteractions.read_csv
@csv_file_path = CSV_FILE
latest = @csv_file_path.sub(/\.csv$/, '-latest.csv')
if File.exist?(latest) and ((Time.now-File.mtime(latest))/3600).round < 24 # less than 24 hours old
$stdout.puts "#{Time.now}: EphaInteractionPlugin.update: skip as latest #{latest} #{File.exist?(latest)} is only #{((Time.now-File.mtime(latest))/3600).round} hours old"
else
FileUtils.rm_f(@csv_file_path, :verbose => true)
target = Mechanize.new.get(CSV_ORIGIN_URL)
target.save_as @csv_file_path
$stdout.puts "#{Time.now}: EphaInteractionPlugin.update: #{File.expand_path(@csv_file_path)} ? #{File.exists?(@csv_file_path)}"
FileUtils.cp(@csv_file_path, latest, :preserve => true, :verbose => true) unless defined?(MiniTest)
end
if File.exists?(latest)
@lineno = 0
first_line = nil
File.readlines(latest).each do |line|
@lineno += 1
line = line.force_encoding('utf-8')
next if /ATC1.*Name1.*ATC2.*Name2/.match(line)
begin
elements = CSV.parse_line(line)
rescue CSV::MalformedCSVError
$stdout.puts "CSV::MalformedCSVError in line #{@lineno}: #{line}"
next
end
epha_interaction = EphaInteraction.new
epha_interaction.atc_code_self = elements[0]
epha_interaction.atc_name = elements[1]
epha_interaction.atc_code_other = elements[2]
epha_interaction.name_other = elements[3]
epha_interaction.info = elements[4]
epha_interaction.action = elements[5]
epha_interaction.effect = elements[6]
epha_interaction.measures = elements[7]
epha_interaction.severity = elements[8]
EphaInteractions.get[ [epha_interaction.atc_code_self, epha_interaction.atc_code_other ]] = epha_interaction
end
$stdout.puts "#{Time.now}: Added #{EphaInteractions.get.size} interaction from #{latest}"; $stdout.flush
end
end

def self.calculate_atc_codes(drugs)
atc_codes = []
if drugs and !drugs.empty?
Expand All @@ -79,7 +41,14 @@ def self.calculate_atc_codes(drugs)
atc_codes
end
def EphaInteractions.get_epha_interaction(atc_code_self, atc_code_other)
EphaInteractions.get[ [atc_code_self, atc_code_other] ]
result = nil
@@epha_interactions.each{ | key, value |
if key[0].to_s.eql?(atc_code_self) and key[1].to_s.eql?(atc_code_other)
result = value
break
end
}
result
end

def EphaInteractions.get_interactions(my_atc_code, drugs)
Expand Down Expand Up @@ -114,24 +83,27 @@ def EphaInteractions.get_interactions(my_atc_code, drugs)
end
end
class EphaInteraction
# Based on information contained in http://community.epha.ch/interactions_de_utf8.csv
include ODBA::Persistable
include Persistence
# Based on information contained in http://community.epha.ch/interactions_de_utf8.csv
# ATC1 Name1 ATC2 Name2 Info Mechanismus Effekt Massnahmen Grad
# N06AB06 Sertralin M03BX02 Tizanidin Keine Interaktion Tizanidin wird über CYP1A2 metabolisiert. Sertralin beeinflusst CYP1A2 jedoch nicht. Keine Interaktion. Die Kombination aus Sertralin und Tizanidin hat kein bekanntes Interaktionspotential. A
attr_accessor :atc_code_self, :atc_code_other # these two items are our unique index. They may not be changed
attr_accessor :atc_name, :name_other, :info, :action, :effect, :measures, :severity
EphaInteractions.read_csv if EphaInteractions.get.size == 0

def initialize
super
end

def init(app)
@pointer.append(@oid)
end

def search_terms
terms = [
@atc_code_self, @atc_name,
@atc_code_other, @name_other,
@info ,@action, @effect,
@info, @action, @effect,
@measures, @severity
]
ODDB.search_terms(terms)
Expand Down
89 changes: 89 additions & 0 deletions src/plugin/epha_interactions.rb
@@ -0,0 +1,89 @@
#!/usr/bin/env ruby
# encoding: utf-8
$: << File.expand_path('../..', File.dirname(__FILE__))
$: << File.expand_path('../../src', File.dirname(__FILE__))

require 'plugin/plugin'
require 'util/oddbconfig'
require 'util/persistence'
require 'drb'
require 'model/epha_interaction'

module ODDB
class EphaInteractionPlugin < Plugin
@@report = []

def debug_msg(msg)
if defined?(MiniTest) then $stdout.puts Time.now.to_s + ': ' + msg; $stdout.flush; return end
if not defined?(@checkLog) or not @checkLog
name = LogFile.filename('oddb/debug/', Time.now)
FileUtils.makedirs(File.dirname(name))
@checkLog = File.open(name, 'a+')
$stdout.puts "Opened #{name}"
end
@checkLog.puts("#{Time.now}: #{msg}")
@checkLog.flush
end

def initialize(app, options = nil)
super
end
def report
@@report.join("\n")
end

def update(agent=Mechanize.new, csv_file_path = ODDB::EphaInteractions::CSV_FILE)
@@report = []
latest = csv_file_path.sub(/\.csv$/, '-latest.csv')
if EphaInteractions.get.size > 0 and File.exist?(latest) and ((Time.now-File.mtime(latest))/3600).round < 24 # less than 24 hours old
msg = "EphaInteractionPlugin.update: skip #{EphaInteractions.get.size } items as latest #{latest} #{File.exist?(latest)} is only #{((Time.now-File.mtime(latest))/3600).round} hours old"
@@report << msg
debug_msg(msg)
else
FileUtils.rm_f(csv_file_path, :verbose => true)
target = agent.get(ODDB::EphaInteractions::CSV_ORIGIN_URL)
if target.is_a?(String)
File.open(csv_file_path, 'w+') { |file| file.write(target) }
else
target.save_as csv_file_path
end
FileUtils.cp(csv_file_path, latest, :preserve => true, :verbose => true)
msg = "EphaInteractionPlugin.update latest #{latest} #{File.exists?(latest)} via #{File.expand_path(csv_file_path)} from #{ODDB::EphaInteractions::CSV_ORIGIN_URL}"
@@report << msg
debug_msg(msg)
@lineno = 0
first_line = nil
@app.delete_all_epha_interactions
File.readlines(latest).each do |line|
@lineno += 1
line = line.force_encoding('utf-8')
next if /ATC1.*Name1.*ATC2.*Name2/.match(line)
begin
elements = CSV.parse_line(line)
rescue CSV::MalformedCSVError
msg << "CSV::MalformedCSVError in line #{@lineno}: #{line}"
next
end
next if elements.size == 0 # Eg. empty line at the end
epha_interaction = @app.create_epha_interaction(elements[0], elements[2])
epha_interaction.atc_code_self = elements[0]
epha_interaction.atc_name = elements[1]
epha_interaction.atc_code_other = elements[2]
epha_interaction.name_other = elements[3]
epha_interaction.info = elements[4]
epha_interaction.action = elements[5]
epha_interaction.effect = elements[6]
epha_interaction.measures = elements[7]
epha_interaction.severity = elements[8]
EphaInteractions.get[ [epha_interaction.atc_code_self, epha_interaction.atc_code_other ]] = epha_interaction
epha_interaction.odba_isolated_store
end
@app.odba_store
msg = "Added #{EphaInteractions.get.size} interactions from #{latest}";
@@report << msg
debug_msg(msg)
end
true
end
end
end
41 changes: 38 additions & 3 deletions src/util/oddbapp.rb
Expand Up @@ -57,6 +57,7 @@ class OddbPrevalence
:registrations, :slates, :users, :narcotics, :accepted_orphans,
:commercial_forms, :rss_updates, :feedbacks, :indices_therapeutici,
:generic_groups, :shorten_paths
attr_accessor :epha_interactions_hash, :epha_interactions
VALID_EAN13 = /^\d{13}/
def initialize
init
Expand All @@ -73,7 +74,10 @@ def init
@currency_rates ||= {}
@divisions ||= {}
@doctors ||= {}
@experiences ||= {}
@epha_interactions ||= []
@epha_interactions_hash ||= {}
ODDB::EphaInteractions.set(@epha_interactions_hash)
@experiences ||= {}
@fachinfos ||= {}
@feedbacks ||= {}
@galenic_forms ||= []
Expand Down Expand Up @@ -387,6 +391,12 @@ def create_experience
experience = ODDB::Experience.new
@experiences.store(experience.oid, experience)
end
def create_epha_interaction(atc_code_self, atc_code_other)
epha_interaction = ODDB::EphaInteraction.new
@epha_interactions_hash ||= {}
@epha_interactions_hash[ [atc_code_self, atc_code_other] ] = epha_interaction
epha_interaction
end
def create_hospital(ean13)
raise "ean13 #{ean13.to_s[0..80]} not valid" unless ean13.to_s.match(VALID_EAN13)
hospital = ODDB::Hospital.new(ean13)
Expand Down Expand Up @@ -494,6 +504,28 @@ def currencies
def analysis_count
@analysis_count ||= analysis_positions.size
end
def delete_all_epha_interactions
if @epha_interactions.is_a?(Array)
$stdout.puts "delete_all_epha_interactions for #{@epha_interactions.class} with #{@epha_interactions.size} items"
@epha_interactions.each do |item|
delete(item.pointer) if item and item.pointer
end
@epha_interactions.odba_isolated_store
@epha_interactions = {}
@epha_interactions.odba_isolated_store
self.odba_store
end
if @epha_interactions_hash.is_a?(Hash)
$stdout.puts "delete_all_epha_interactions for @epha_interactions_hash #{@epha_interactions_hash.class} with #{@epha_interactions_hash.size} items"
@epha_interactions_hash.values.each do |item|
delete(item.pointer) if item and item.pointer
end
@epha_interactions_hash.odba_isolated_store
@epha_interactions_hash = {}
@epha_interactions_hash.odba_isolated_store
self.odba_store
end
end
def delete_all_narcotics
@narcotics.values.each do |narc|
delete(narc.pointer)
Expand Down Expand Up @@ -627,10 +659,13 @@ def experience(oid)
@experiences[oid.to_i]
end
def get_epha_interaction(atc_code_self, atc_code_other)
ODDB::EphaInteractions.get_epha_interaction(atc_code_self, atc_code_other)
EphaInteractions.get_epha_interaction(atc_code_self, atc_code_other)
end
def epha_interaction(oid)
@epha_interactions_hash[oid.to_i]
end
def epha_interaction_count
ODDB::EphaInteractions.get.size
@epha_interactions_hash.size
end
def pharmacy(ean13)
pharmacy_by_gln(ean13)
Expand Down
3 changes: 2 additions & 1 deletion src/util/updater.rb
Expand Up @@ -15,7 +15,7 @@
require 'plugin/dosing'
require 'plugin/drugbank'
require 'plugin/divisibility'
#require 'plugin/epha_interactions'
require 'plugin/epha_interactions'
require 'plugin/hospitals'
require 'plugin/lppv'
require 'plugin/medwin'
Expand Down Expand Up @@ -187,6 +187,7 @@ def recipients
end
def run
logfile_stats
update_epha_interactions

# recall, hpc
update_swissmedic_feeds
Expand Down
4 changes: 2 additions & 2 deletions src/view/interactions/interaction_chooser.rb
Expand Up @@ -119,7 +119,7 @@ def init
ean13 = @session.user_input(:search_query)
path = @session.request_path
@drugs = @session.choosen_drugs
@interactions = ODDB::EphaInteractions.get_interactions(model.atc_class.code, @drugs)
@interactions = EphaInteractions.get_interactions(model.atc_class.code, @drugs)
if @model.is_a? ODDB::Package
nextRow = 0
unless @hide_interaction_headers
Expand Down Expand Up @@ -175,7 +175,7 @@ def text_info(model, session=@session)
span.set_attribute('class', 'print bold italic')
list.value << span
end
ODDB::EphaInteractions.get_interactions(model.atc_class.code, @session.choosen_drugs).each {
EphaInteractions.get_interactions(model.atc_class.code, @session.choosen_drugs).each {
|interaction|
headerDiv = HtmlGrid::Div.new(model, @session, self)
headerDiv.value = []
Expand Down
2 changes: 2 additions & 0 deletions test/data/csv/epha_interactions_de_utf8-example.csv
@@ -0,0 +1,2 @@
"ATC1","Name1","ATC2","Name2","Info","Mechanismus","Effekt","Massnahmen","Grad"
"C09CA01","Losartan","C07AB02","Metoprolol","Verstärkte Blutdrucksenkung","Additive Blutdrucksenkung","Eine pharmakodynamische Verstärkung der antihypertensiven Wirkung der Substanzen ist zu erwarten. AT2-Antagonisten, Betablocker und auch Thiaziddiuretika werden häufig zusammen eingesetzt","Therapeutisch häufig genutzte Kombination. Bei erwarteter additiver Blutdrucksenkung empfehlen sich Blutdruckkontrollen, insbesondere zu Beginn der Kombination. ","B"
3 changes: 0 additions & 3 deletions test/data/csv/epha_interactions_de_utf8-latest-latest.csv

This file was deleted.

0 comments on commit eca7594

Please sign in to comment.