module KnowledgeBaseRESTHelpers

Attributes

knowledge_base_dir[RW]
syndications[RW]

Public Class Methods

add_syndication(name, kb) click to toggle source
# File lib/rbbt/rest/knowledge_base/helpers.rb, line 9
def add_syndication(name, kb)
  @syndications ||= {}
  @syndications[name] = kb
end
association_resources() click to toggle source
# File lib/rbbt/rest/knowledge_base/locate.rb, line 3
def self.association_resources
  @association_resources ||= []
end

Public Instance Methods

association_render(pair, database = nil) click to toggle source
# File lib/rbbt/rest/knowledge_base/render.rb, line 7
def association_render(pair, database = nil)
  database ||= pair.database
  template_file = locate_association_template(database)

  locals = {:pair => pair, :database => database}

  render(template_file, locals, nil)
end
association_resources() click to toggle source
# File lib/rbbt/rest/knowledge_base/locate.rb, line 7
def association_resources
  [Rbbt.share.views.find(:lib)] +  KnowledgeBaseRESTHelpers.association_resources
end
association_table(associations = nil, options = {}) { || ... } click to toggle source
# File lib/rbbt/rest/knowledge_base/helpers.rb, line 76
def association_table(associations = nil, options = {}, &block)
  options = Misc.add_defaults options, :row_ids => :consume, :footer => true
  associations = yield if block_given?

  tsv = case associations
        when Array
          associations.tsv
        when TSV
          associations
        else
          TSV.open(tsv)
        end

  tsv = tsv.to_double{|v| v.nil? ? nil : v.split(";;") } unless tsv.fields.nil? or tsv.fields.empty? or tsv.type == :double

  tsv2html tsv, options
end
get_knowledge_base(name=:user, namespace = nil) click to toggle source
# File lib/rbbt/rest/knowledge_base/helpers.rb, line 59
def get_knowledge_base(name=:user, namespace = nil)
  kb = case name.to_s
         when 'step'
           step_path = cookies[:step_path]
           step_path = params[:step_path] if step_path.nil?
           raise "No step_path" if step_path.nil?
           step = Workflow.fast_load_step(step_path)
           step.knowledge_base
         when "user"
           user_kb(user)
         else
           Genomics.knowledge_base
         end

  (namespace and namespace != kb.namespace) ? kb.version(namespace) : kb
end
get_matrix(code) click to toggle source
# File lib/rbbt/rest/knowledge_base/helpers.rb, line 105
def get_matrix(code)
  name, study = code.split("@")
  Study.setup(study).matrix(name)
end
locate_association_template(database) click to toggle source
# File lib/rbbt/rest/knowledge_base/locate.rb, line 27
def locate_association_template(database)

  association_resources.each do |resource|
    path = locate_association_template_from_resource(resource, database)
    return path if path and path.exists?
  end

  association_resources.each do |resource|
    path = locate_association_template_from_resource(resource, "Default")
    return path if path and path.exists?
  end

  raise "Template not found for association database: #{ Misc.fingerprint database }"
end
locate_association_template_from_resource(resource, database = nil) click to toggle source
# File lib/rbbt/rest/knowledge_base/locate.rb, line 11
def locate_association_template_from_resource(resource, database = nil)
  if database == "Default" 
    path = resource.association["Default.haml"]
    if path.exists?
      return path
    else
      return nil
    end
  end

  path = resource.association[database + '.haml']
  return path if path.exists?

  nil
end
prepare_entities_for_json(entities) click to toggle source
# File lib/rbbt/rest/knowledge_base/helpers.rb, line 21
def prepare_entities_for_json(entities)
  case entities
  when AnnotatedArray
    list_hash(entities)
  when Array
    entities.inject([]){|acc,e| acc << prepare_entities_for_json(e); acc }
  when Hash
    hash = {}
    entities.each do |key,values|
      hash[key] = prepare_entities_for_json(values)
    end
    hash
  when String
    entities
  end
end
serialize_entities(obj) click to toggle source
# File lib/rbbt/rest/knowledge_base/helpers.rb, line 94
def serialize_entities(obj)
  case obj
  when Array
    obj.collect{|e| serialize_entities(e)}
  when String
    e = obj
    name = e.respond_to?(:name) ? e.name || e : e
    {:id => e, :name => name, :type => e.base_type, :info => e.info}
  end
end
user_kb(user = nil) click to toggle source
# File lib/rbbt/rest/knowledge_base/helpers.rb, line 38
def user_kb(user = nil)
  user ||= @user
  @@user_kbs ||= {}
  @@user_kbs[user] ||= begin
                         dir = KnowledgeBaseRESTHelpers.knowledge_base_dir.users.common
                         kb = KnowledgeBase.new(dir, Organism.default_code("Hsa"))
                         
                         KnowledgeBaseRESTHelpers.syndications.each do |name, new|
                           Log.low "Syndicating database #{ name } for user #{user}"
                           kb.syndicate name, new
                         end if KnowledgeBaseRESTHelpers.syndications.any?

                         user_studies[user].each do |study|
                           Study.setup(study)
                           kb.syndicate study, study.knowledge_base
                         end if defined? user_studies

                         kb
                       end
end