###
#### Variant update procedures
####
#
RDF_DIR = "/data/store/rdf"
GRAPH_NS = "http://togogenome.org"

####
#### Triple store
####
#
#ISQL = "/data/store/virtuoso7.1hs/bin/isql 20861 dba dba"
#ISQL = "/data/store/virtuoso7.1tmp/bin/isql 20891 dba dba"
ISQL = "/data/store/virtuoso7.2.4.2/bin/isql 20721 dba dba" #for 7.2.4.2

USAGE = <<"USAGE"
71variant.sh stop
71variant.sh clear
71variant.sh start


rake -f RakefileVariant medgen:load 20170322
rake -f RakefileVariant exac:load 20170322
rake -f RakefileVariant clinvar:load 20170322

USAGE

def set_name
  if ARGV.size > 1
    name = ARGV.last
    task name.to_sym  # do nothing, just to avoid "Don't know how to build task #{name}"
  else
    date = Time.now.strftime("%Y%m%d")
  end
  return name || date
end

def create_subdir(path, name)
  sh "mkdir -p #{path}/#{name}"
  return "#{path}/#{name}"
end

def link_current(path, name)
  sh "cd #{path}; ln -snf #{name} current"
end

def isql_create(graph, name)
  sleep 1
  time = Time.now.strftime("%Y%m%d-%H%M%S")
  path = "isql/#{time}-#{graph}-#{name}.isql"
  return path
end

def isql_write(file, line)
  file.puts "ECHOLN \"#{line}\";"
  file.puts line
end


def load_ttl(path, graph, name)
  isql = isql_create(graph.sub('/',''), name)
  File.open(isql, "w") do |file|
    isql_write(file, "log_enable(3, 1);")
    isql_write(file, "DB.DBA.TTLP_MT(file_to_string_output('#{path}'), '', '#{GRAPH_NS}/#{graph}', 81);")
    isql_write(file, "checkpoint;")
  end
  sh "#{ISQL} #{isql}"
end

def load_rdf(path, graph, name)
  isql = isql_create(graph, name)
  File.open(isql, "w") do |file|
    isql_write(file, "log_enable(3, 1);")
    isql_write(file, "DB.DBA.RDF_LOAD_RDFXML_MT(file_to_string_output('#{path}'), '', '#{GRAPH_NS}/#{graph}');")
    isql_write(file, "checkpoint;")
  end
  sh "#{ISQL} #{isql}"
end

def load_dir(path, pattern, graph, name)
  isql = isql_create(graph.sub('/',''), name)
  File.open(isql, "w") do |file|
    isql_write(file, "log_enable(3, 1);")
    isql_write(file, "ld_dir_all('#{path}', '#{pattern}', '#{GRAPH_NS}/#{graph}');")
    isql_write(file, "rdf_loader_run();")
    isql_write(file, "checkpoint;")
  end
  sh "#{ISQL} #{isql}"
end

def update_graph(graph, name)
  sparql = "sparql 
    PREFIX dct: <http://purl.org/dc/terms/>
    DELETE FROM <#{GRAPH_NS}/graph> {
      <#{GRAPH_NS}/#{graph}> ?p ?o .
    }
    WHERE {
      GRAPH <#{GRAPH_NS}/graph> {
        <#{GRAPH_NS}/#{graph}> ?p ?o .
      }
    }
    INSERT DATA INTO <#{GRAPH_NS}/graph> {
      <#{GRAPH_NS}/graph/#{graph}> dct:isVersionOf <#{GRAPH_NS}/#{graph}/#{name}> .
    }
  ;"
  isql = isql_create(graph, name)
  File.open(isql, "w") do |file|
    isql_write(file, sparql)
  end
  sh "#{ISQL} #{isql}"
end


namespace :medgen do
  desc "Load MedGen to Variant ep"
  task :load do
    name = set_name
    load_dir("#{RDF_DIR}/variant/medgen/current", '*.ttl', 'medgen', name)
    update_graph('medgen', name)
  end
end

namespace :exac do
  desc "Load ExAC to Variant ep"
  task :load do
    name = set_name
    load_dir("#{RDF_DIR}/variant/exac/current", '*.ttl', 'exac', name)
    update_graph('exac', name)
  end
end

namespace :clinvar do
  desc "Load ClinVar to Variant ep"
  task :load do
    name = set_name
    load_dir("#{RDF_DIR}/variant/clinvar/current", '*.ttl', 'clinvar', name)
    update_graph('clinvar', name)
  end
end
