Module: Organism

Extended by:
Resource
Defined in:
lib/rbbt/sources/organism.rb

Defined Under Namespace

Classes: OrganismNotProcessedError

Class Method Summary collapse

Class Method Details

.allowed_biomart_archivesObject



13
14
15
16
17
# File 'lib/rbbt/sources/organism.rb', line 13

def self.allowed_biomart_archives
  Rbbt.etc.allowed_biomart_archives.exists? ? 
    Rbbt.etc.allowed_biomart_archives.list :
    nil
end

.attach_translations(org, tsv, target = nil, fields = nil, options = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rbbt/sources/organism.rb', line 89

def self.attach_translations(org, tsv, target = nil, fields = nil, options = {})
  Log.high "Attaching Translations for #{ org.inspect }, target #{target.inspect}, fields #{fields.inspect}"
  options = Misc.add_defaults options, :persist => true, :case_insensitive => false

  options.merge! :key_field    => target unless target.nil?
  options.merge! :fields => fields unless fields.nil?

  index = identifiers(org).tsv options

  tsv.attach index, :fields => [:key], :persist_input => true
end

.entrez_taxid_organism(taxid) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/rbbt/sources/organism.rb', line 156

def self.entrez_taxid_organism(taxid)
  all_organisms = Organism.installable_organisms

  all_organisms.each do |organism|
    return organism if Organism.entrez_taxids(organism).read.split("\n").include? taxid.to_s
  end

  raise "No organism identified for taxid #{taxid}. Supported organism are: #{all_organisms * ", "}"
end

.gene_list_bases(genes, organism = nil) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rbbt/sources/organism.rb', line 166

def self.gene_list_bases(genes, organism = nil)
  if genes.respond_to? :orgnanism
    organism = genes.organism if organism.nil?
    genes = genes.clean_annotations
  end

  organism ||= "Hsa"

  @@gene_start_end ||= {}
  gene_start_end = @@gene_start_end[organism] ||= Organism.gene_positions(organism).tsv(:persist => true, :key_field => "Ensembl Gene ID", :fields => ["Gene Start", "Gene End"], :type => :list, :cast => :to_i, :unmamed => true)

  ranges = genes.collect{|gene| start, eend = gene_start_end[gene]; (start..eend) }
  Misc.total_length(ranges)
end

.gene_list_exon_bases(genes, organism = nil) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/rbbt/sources/organism.rb', line 181

def self.gene_list_exon_bases(genes, organism = nil)
  if genes.respond_to? :orgnanism
    organism = genes.organism if organism.nil?
    genes = genes.clean_annotations
  end

  organism ||= "Hsa"

  @@gene_exons_tsv ||= {}
  gene_exons = @@gene_exons_tsv[organism] ||= Organism.exons(organism).tsv(:persist => true, :key_field => "Ensembl Gene ID", :fields => ["Ensembl Exon ID"], :type => :flat, :merge => true, :unnamed => true)

  @@exon_range_tsv ||= {}
  exon_ranges = @@exon_range_tsv[organism] ||= Organism.exons(organism).tsv(:persist => true, :fields => ["Exon Chr Start", "Exon Chr End"], :type => :list, :cast => :to_i, :unnamed => true)

  exons = gene_exons.values_at(*genes).compact.flatten.uniq

  exon_ranges = exons.collect{|exon|
    Log.low "Exon #{ exon } does not have range" and next if not exon_ranges.include? exon
    pos = exon_ranges[exon]
    (pos.first..pos.last)
  }.compact
  
  Misc.total_length(exon_ranges)
end

.guess_id(org, values) ⇒ Object



127
128
129
130
# File 'lib/rbbt/sources/organism.rb', line 127

def self.guess_id(org, values)
  field_matches = TSV.field_match_counts(Organism.identifiers(org).find, values)
  field_matches.sort_by{|field, count| count.to_i}.last
end

.hg_build(organism) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rbbt/sources/organism.rb', line 27

def self.hg_build(organism)
  require 'rbbt/sources/ensembl_ftp'

  raise "Only organism 'Hsa' (Homo sapiens) supported" unless organism =~ /^Hsa/

  return 'hg19' unless organism =~ /\//
  date = organism.split("/")[1] 

  release = Ensembl.releases[date]

  release.sub(/.*-/,'').to_i > 54 ? 'hg19' : 'hg18'
end

.installable_organismsObject



9
10
11
# File 'lib/rbbt/sources/organism.rb', line 9

def self.installable_organisms
  Rbbt.share.install.Organism.find.glob('???').collect{|f| File.basename(f)}
end

.known_ids(name) ⇒ Object



152
153
154
# File 'lib/rbbt/sources/organism.rb', line 152

def self.known_ids(name)
  TSV::Parser.new(Organism.identifiers(name).open).all_fields
end

.liftOver(positions, source, target) ⇒ Object



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/sources/organism.rb', line 40

def self.liftOver(positions, source, target)

  source_hg = hg_build(source)
  target_hg = hg_build(target)

  case
  when (source_hg == 'hg19' and target_hg == 'hg18')
    map_url = "http://hgdownload.cse.ucsc.edu/goldenPath/hg19/liftOver/hg19ToHg18.over.chain.gz" 
  when (source_hg == 'hg18' and target_hg == 'hg19')
    map_url = "http://hgdownload.cse.ucsc.edu/goldenPath/hg18/liftOver/hg18ToHg19.over.chain.gz" 
  else
    return positions
  end

  positions_bed = positions.collect{|position| 
    chr, pos = position.split(":").values_at(0,1)
    ["chr" << chr, pos.to_i-1, pos, position] * "\t"
  } * "\n" + "\n"

  new_positions = {}

  TmpFile.with_file(positions_bed) do |source_bed|
    TmpFile.with_file() do |unmapped_file|
      TmpFile.with_file() do |map_file|


        Open.write(map_file, Open.read(map_url))
        new_mutations = TmpFile.with_file() do |target_bed|
          FileUtils.chmod(755, Rbbt.software.opt.bin.liftOver.produce.find)
          CMD.cmd("#{Rbbt.software.opt.bin.liftOver.find} '#{source_bed}' '#{map_file}' '#{target_bed}' '#{unmapped_file}'").read
          Open.read(target_bed) do |line|
            chr, position_alt, position, name = line.chomp.split("\t")
            chr.sub! /chr/, ''

            old_chr, old_position, *rest = name.split(":")
            new_positions[name] = ([chr, position].concat rest) * ":"
          end
        end
      end
    end
  end

  positions.collect do |position|
    new_positions[position]
  end
end

.normalize(org, list, target = nil, fields = nil, options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rbbt/sources/organism.rb', line 101

def self.normalize(org, list, target = nil, fields = nil, options = {})
  return [] if list.nil? or list.empty?
  options = Misc.add_defaults options, :persist => true, :case_insensitive => true, :double => false
  double = Misc.process_options options, :double


  options.merge! :target => target unless target.nil?
  options.merge! :fields => fields unless fields.nil?

  index = identifiers(org).index options

  if Array === list
    if double
      index.values_at *list
    else
      index.values_at(*list).collect{|e| Misc.first e}
    end
  else
    if double
      index[list]
    else
      index[list].first
    end
  end
end

.organism(name) ⇒ Object



140
141
142
143
144
# File 'lib/rbbt/sources/organism.rb', line 140

def self.organism(name)
  organisms.select{|organism|
    organism == name or Organism.scientific_name(organism) =~ /#{ name }/i
  }.first
end

.organism_code(name) ⇒ Object



146
147
148
149
150
# File 'lib/rbbt/sources/organism.rb', line 146

def self.organism_code(name)
  organisms.select{|organism|
    organism == name or Organism.scientific_name(organism) =~ /#{ name }/i
  }.first
end

.organismsObject



132
133
134
# File 'lib/rbbt/sources/organism.rb', line 132

def self.organisms
  Dir.glob(File.join(Organism.root.find, '*')).collect{|f| File.basename(f)}
end

.scientific_name(organism) ⇒ Object



136
137
138
# File 'lib/rbbt/sources/organism.rb', line 136

def self.scientific_name(organism)
  Organism[organism]["scientific_name"].produce.read.strip
end