Module: Entity::Identified

Defined in:
lib/rbbt/entity/identifiers.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
# File 'lib/rbbt/entity/identifiers.rb', line 10

def self.included(base)
  base.annotation :format
  base.annotation :organism

  class << base
    attr_accessor :identifier_files, :formats, :default_format, :name_format, :description_format
  end

  base.module_eval do
    def identity_type
      self.annotation_types.select{|m| m.include? Entity::Identified }.last
    end

    def identifier_files 
      files = identity_type.identifier_files.dup
      files.collect!{|f| f.annotate f.gsub(/\bNAMESPACE\b/, organism) } if annotations.include? :organism and self.organism
      if files.select{|f| f =~ /\bNAMESPACE\b/ }.any?
        Log.warn "Rejecting some identifier files for lack of 'organism': " << files.select{|f| f =~ /\bNAMESPACE\b/ } * ", "
      end
      files.reject!{|f| f =~ /\bNAMESPACE\b/ } 
      files
    end

    def identifier_index(format = nil, source = nil)
      Persist.memory("Entity index #{identity_type}: #{format} (from #{source || "All"})", :persist => true, :format => format, :source => source) do
        source ||= self.respond_to?(:format)? self.format : nil

        begin
          index = TSV.translation_index(identifier_files, format, source, :persist => true)
          raise "No index from #{ Misc.fingerprint source } to #{ Misc.fingerprint format }: #{Misc.fingerprint identifier_files}" if index.nil?
          index.unnamed = true
          index
        rescue
          raise $! if source.nil?
          source = nil
          retry
        end
      end
    end
  end

  base.property :to => :both do |target_format|

    target_format = case target_format
                    when :name
                      identity_type.name_format 
                    when :default
                      identity_type.default_format 
                    when :ensembl
                      identity_type.formats.select{|f| f =~ /Ensembl/}.first
                    else
                      target_format
                    end

    return self if target_format == format

    if Array === self
      self.annotate(identifier_index(target_format, self.format).values_at(*self))
    else
      self.annotate(identifier_index(target_format, self.format)[self])
    end.tap{|o| o.format = target_format unless o.nil? }
  end

  base.property :name => :both do
    to(:name)
  end

  base.property :default => :both do
    to(:default)
  end

  base.property :ensembl => :both do
    to(:ensembl)
  end

end