class ConceptQL::CLI
Public Instance Methods
annotate_statement(statement_file)
click to toggle source
# File lib/conceptql/cli.rb, line 49 def annotate_statement(statement_file) q = ConceptQL::Query.new(db(options), criteria_from_file(statement_file)) pp q.annotate end
dumpit(path)
click to toggle source
# File lib/conceptql/cli.rb, line 117 def dumpit(path) path = Pathname.new(path) path.mkpath unless path.exist? headers_path = path + 'headers' headers_path.mkpath unless headers_path.exist? db.tables.each do |table| puts "Dumping #{table}..." ds = db[table] rows = ds.select_map(ds.columns) CSV.open(path + "#{table}.csv", "wb") do |csv| rows.each { |row| csv << row } end CSV.open(headers_path + "#{table}.csv", "wb") do |csv| csv << ds.columns end end end
fake_graph(file)
click to toggle source
# File lib/conceptql/cli.rb, line 69 def fake_graph(file) require_relative 'fake_annotater' annotated = ConceptQL::FakeAnnotater.new(criteria_from_file(file)).annotate pp annotated ConceptQL::AnnotateGrapher.new.graph_it(annotated, '/tmp/graph.pdf') system('open /tmp/graph.pdf') end
knit(file)
click to toggle source
# File lib/conceptql/cli.rb, line 108 def knit(file) opts = {} if options[:ignore_cache] opts[:cache_options] = { ignore: true } end ConceptQL::Knitter.new(ConceptQL::Database.new(db), file, opts).knit end
metadata()
click to toggle source
# File lib/conceptql/cli.rb, line 78 def metadata File.write('metadata.js', "$metadata = #{ConceptQL.metadata(warn: true).to_json};") File.write('metadata.json', ConceptQL.metadata.to_json) if system("which", "json_pp", out: File::NULL, err: File::NULL) system("cat metadata.json | json_pp", out: "metadata_pp.json") end end
run_statement(statement_file)
click to toggle source
# File lib/conceptql/cli.rb, line 41 def run_statement(statement_file) q = cdb(options).query(criteria_from_file(statement_file)) puts q.sql puts JSON.pretty_generate(q.statement) pp q.query.all end
selection_operators()
click to toggle source
# File lib/conceptql/cli.rb, line 87 def selection_operators require 'csv' CSV.open('selection_operators.tsv', 'w', col_sep: "\t") do |csv| csv << ["name"] ConceptQL.metadata[:operators].values.select { |v| v[:basic_type] == :selection }.map { |v| v[:preferred_name] }.each do |name| csv << [name] end end CSV.open('domains.tsv', 'w', col_sep: "\t") do |csv| csv << ["name"] ConceptQL::Operators::TABLE_COLUMNS.each do |domain, columns| next unless columns.include?(:person_id) next if domain.to_s =~ /era\Z/ csv << [domain] end end end
show_and_tell_file(file)
click to toggle source
# File lib/conceptql/cli.rb, line 64 def show_and_tell_file(file) show_and_tell(criteria_from_file(file), options) end
show_graph(file)
click to toggle source
# File lib/conceptql/cli.rb, line 56 def show_graph(file) graph_it(criteria_from_file(file)) system('open /tmp/graph.pdf') end
Private Instance Methods
cdb(options)
click to toggle source
# File lib/conceptql/cli.rb, line 195 def cdb(options) ConceptQL::Database.new(db(options)) end
criteria_from_file(file)
click to toggle source
# File lib/conceptql/cli.rb, line 182 def criteria_from_file(file) case File.extname(file) when '.json' JSON.parse(File.read(file)) else eval(File.read(file)) end end
fetch_conceptql(conceptql_id)
click to toggle source
# File lib/conceptql/cli.rb, line 151 def fetch_conceptql(conceptql_id) my_db = db(options) my_db.extension(:pg_array, :pg_json) my_db[:concepts].where(concept_id: conceptql_id).select(:statement, :label).first end
filtered(results)
click to toggle source
# File lib/conceptql/cli.rb, line 191 def filtered(results) results.each { |r| r.delete_if { |k,v| v.nil? } } end
graph_it(statement, title = nil)
click to toggle source
# File lib/conceptql/cli.rb, line 176 def graph_it(statement, title = nil) my_db = db(options) q = ConceptQL::Query.new(my_db, statement).annotate ConceptQL::AnnotateGrapher.new.graph_it(q, '/tmp/graph.pdf') end
show_and_tell(statement, options, title = nil)
click to toggle source
# File lib/conceptql/cli.rb, line 157 def show_and_tell(statement, options, title = nil) my_db = db(options) q = ConceptQL::Query.new(my_db, statement) statement = q.annotate puts 'JSON' puts JSON.pretty_generate(statement) graph_it(statement, title) STDIN.gets puts q.sql STDIN.gets results = q.all if options[:full] pp results else pp filtered(results) end puts results.length end
show_and_tell_db(conceptql_id)
click to toggle source
# File lib/conceptql/cli.rb, line 139 def show_and_tell_db(conceptql_id) result = fetch_conceptql(conceptql_id, options) puts "Concept: #{result[:label]}" show_and_tell(result[:statement].to_hash, options, result[:label]) end
show_db_graph(conceptql_id)
click to toggle source
# File lib/conceptql/cli.rb, line 146 def show_db_graph(conceptql_id) result = fetch_conceptql(conceptql_id, options) graph_it(result[:statement], db, result[:label]) end