summaryrefslogtreecommitdiff
path: root/lib/lazar.rb
blob: e77de9d55be0fb432ba96ade5a1b123badc71a4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
require 'rubygems'
require "bundler/setup"
require "rest-client"
require 'addressable'
require 'yaml'
require 'json'
require 'logger'
require 'mongoid'
require 'rserve'
require "nokogiri"
require "base64"
require 'openbabel'

# Environment setup
ENV["LAZAR_ENV"] ||= "production"
raise "Incorrect lazar environment variable LAZAR_ENV '#{ENV["LAZAR_ENV"]}', please set it to 'production' or 'development'." unless ENV["LAZAR_ENV"].match(/production|development/)

ENV["MONGOID_ENV"] = ENV["LAZAR_ENV"] 
ENV["RACK_ENV"] = ENV["LAZAR_ENV"] # should set sinatra environment
# CH: this interferes with /etc/hosts on my machine
# search for a central mongo database in use
# http://opentox.github.io/installation/2017/03/07/use-central-mongodb-in-docker-environment
# CENTRAL_MONGO_IP = `grep -oP '^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}(?=.*mongodb)' /etc/hosts`.chomp
Mongoid.load_configuration({
  :clients => {
    :default => {
      :database => ENV["LAZAR_ENV"],
      #:hosts => (CENTRAL_MONGO_IP.blank? ? ["localhost:27017"] : ["#{CENTRAL_MONGO_IP}:27017"]),
      :hosts => ["localhost:27017"]
    }
  }
})
Mongoid.raise_not_found_error = false # return nil if no document is found
#$mongo = Mongo::Client.new("mongodb://#{(CENTRAL_MONGO_IP.blank? ? "127.0.0.1" : CENTRAL_MONGO_IP)}:27017/#{ENV['LAZAR_ENV']}")
$mongo = Mongo::Client.new("mongodb://127.0.0.1:27017/#{ENV['LAZAR_ENV']}")
$gridfs = $mongo.database.fs

# Logger setup
STDOUT.sync = true # for redirection, etc see http://stackoverflow.com/questions/8549443/why-doesnt-logger-output-to-stdout-get-redirected-to-files
$logger = Logger.new STDOUT # STDERR did not work on my development machine (CH)
case ENV["LAZAR_ENV"]
when "production"
  $logger.level = Logger::WARN
  Mongo::Logger.level = Logger::WARN 
when "development"
  $logger.level = Logger::DEBUG
  Mongo::Logger.level = Logger::WARN 
end

# R setup
rlib = File.expand_path(File.join(File.dirname(__FILE__),"..","R"))
# should work on POSIX including os x
# http://stackoverflow.com/questions/19619582/number-of-processors-cores-in-command-line
NR_CORES = `getconf _NPROCESSORS_ONLN`.to_i
R = Rserve::Connection.new
R.eval ".libPaths('#{rlib}')"
R.eval "
suppressPackageStartupMessages({
  library(labeling,lib=\"#{rlib}\")
  library(iterators,lib=\"#{rlib}\")
  library(foreach,lib=\"#{rlib}\")
  library(ggplot2,lib=\"#{rlib}\")
  library(grid,lib=\"#{rlib}\")
  library(gridExtra,lib=\"#{rlib}\")
  library(pls,lib=\"#{rlib}\")
  library(caret,lib=\"#{rlib}\")
  library(doMC,lib=\"#{rlib}\")
  library(randomForest,lib=\"#{rlib}\")
  library(plyr,lib=\"#{rlib}\")
  registerDoMC(#{NR_CORES})
})
"

PUBCHEM_URI = "https://pubchem.ncbi.nlm.nih.gov/rest/pug/"
CHEMBL_URI = "https://www.ebi.ac.uk/chembl/api/data/molecule/"

# OpenTox classes and includes
CLASSES = ["Feature","Substance","Dataset","CrossValidation","LeaveOneOutValidation","RepeatedCrossValidation"]# Algorithm and Models are modules

[ # be aware of the require sequence as it affects class/method overwrites
  "overwrite.rb",
  "rest-client-wrapper.rb", 
  "opentox.rb",
  "feature.rb",
  "physchem.rb",
  "substance.rb",
  "compound.rb",
  "nanoparticle.rb",
  "dataset.rb",
  "algorithm.rb",
  "similarity.rb",
  "feature_selection.rb",
  "model.rb",
  "classification.rb",
  "regression.rb",
  "caret.rb",
  "validation-statistics.rb",
  "validation.rb",
  "train-test-validation.rb",
  "leave-one-out-validation.rb",
  "crossvalidation.rb",
  "download.rb",
  "import.rb",
].each{ |f| require_relative f }