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
|
# 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
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"})", :format => format, :source => source) do
source ||= self.respond_to?(:format)? self.format : nil
index = TSV.translation_index(identifier_files, format, source, :persist => true)
raise "No index from #{ source } to #{ format }: #{Misc.fingerprint identifier_files}" if index.nil?
index.unnamed = true
index
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(:name)
end
base.property :ensembl => :both do
to(:ensembl)
end
end
|