Module: Ensembl::FTP

Defined in:
lib/rbbt/sources/ensembl_ftp.rb

Constant Summary collapse

SERVER =
"ftp.ensembl.org"
DOMAIN_SERVER =
"ftp.ensemblgenomes.org"

Class Method Summary collapse

Class Method Details

._get_file(organism, table, extension) ⇒ Object



83
84
85
86
# File 'lib/rbbt/sources/ensembl_ftp.rb', line 83

def self._get_file(organism, table, extension)
  url = url_for(organism, table, extension)
  self._get_gz(url)
end

._get_gz(url) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/rbbt/sources/ensembl_ftp.rb', line 75

def self._get_gz(url)
  begin
    CMD.cmd("wget '#{url}' -O  - | gunzip").read
  rescue
    CMD.cmd("wget '#{url}.bz2' -O  - | bunzip2 | gunzip").read
  end
end

.base_url(organism) ⇒ Object



67
68
69
# File 'lib/rbbt/sources/ensembl_ftp.rb', line 67

def self.base_url(organism)
  File.join("ftp://", ftp_url_for(organism) )
end

.ensembl_tsv(organism, table, key_field = nil, fields = nil, options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rbbt/sources/ensembl_ftp.rb', line 99

def self.ensembl_tsv(organism, table, key_field = nil, fields = nil, options = {})
  if key_field and fields
    all_fields = fields_for(organism, table)
    key_pos = all_fields.index key_field
    field_pos = fields.collect{|f| all_fields.index f}

    options[:key_field] = key_pos
    options[:fields]    = field_pos
  end

  tsv = TSV.open(StringIO.new(_get_file(organism, table, "txt")), options)
  tsv.key_field = key_field
  tsv.fields = fields
  tsv
end

.fields_for(organism, table) ⇒ Object



93
94
95
96
97
# File 'lib/rbbt/sources/ensembl_ftp.rb', line 93

def self.fields_for(organism, table)
  sql_file = _get_file(organism, File.basename(base_url(organism)), 'sql')
  chunk = sql_file.match(/^CREATE TABLE .#{table}. \((.*?)^\)/sm)[1]
  chunk.scan(/^\s+`(.*?)`/).flatten
end

.ftp_name_for(organism, subdir = 'mysql') ⇒ Object



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
# File 'lib/rbbt/sources/ensembl_ftp.rb', line 30

def self.ftp_name_for(organism, subdir='mysql')
  if domain = Thread.current["ensembl_domain"]
    return ftp_name_for_domain(domain, organism,subdir)
  end

  code, build = organism.split "/"
  build ||= "current"

  if build.to_s == "current"
    release = 'current'
    name = Organism.scientific_name(organism)
    ftp = Net::FTP.new(Ensembl::FTP::SERVER)
    ftp.passive = true
    ftp.
    dir = File.join('pub', "current_#{subdir}")
    ftp.chdir(dir)
    file = ftp.list(name.downcase.gsub(" ",'_') + "*").reject{|f| f.split("_").length > 3 && ! f.include?("_core_") }.collect{|l| l.split(" ").last}.last
    ftp.close
  else
    release = Ensembl.releases[build]
    name = Organism.scientific_name(organism)
    ftp = Net::FTP.new(Ensembl::FTP::SERVER)
    ftp.passive = true
    ftp.
    dir = File.join('pub', release, subdir)
    ftp.chdir(dir)
    file = ftp.list(name.downcase.gsub(" ",'_') + "*").reject{|f| f.split("_").length > 3 && ! f.include?("_core_") }.collect{|l| l.split(" ").last}.last
    ftp.close
  end
  [release, File.join(Ensembl::FTP::SERVER, dir, file)]
end

.ftp_name_for_domain(domain, organism, subdir = 'mysql') ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rbbt/sources/ensembl_ftp.rb', line 14

def self.ftp_name_for_domain(domain, organism, subdir='mysql')
  code, build = organism.split "/"
  build ||= "current"

  release = build == "current" ? 'current' : Ensembl.releases[build]
  name = Organism.scientific_name(organism)
  ftp = Net::FTP.new(Ensembl::FTP::DOMAIN_SERVER)
  ftp.passive = true
  ftp.
  dir = File.join('pub', domain,  'current', subdir)
  ftp.chdir(dir)
  file = ftp.list(name.downcase.gsub(" ",'_') + "*").reject{|f|  f.split("_").length > 3 && ! f.include?("_core_") }.reject{|f| f =~ /\.gz$/}.collect{|l| l.split(" ").last}.last
  ftp.close
  [release, File.join(Ensembl::FTP::DOMAIN_SERVER, dir, file)]
end

.ftp_url_for(organism) ⇒ Object



62
63
64
65
# File 'lib/rbbt/sources/ensembl_ftp.rb', line 62

def self.ftp_url_for(organism)
  release, ftp_url = ftp_name_for(organism)
  ftp_url
end

.has_table?(organism, table) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
# File 'lib/rbbt/sources/ensembl_ftp.rb', line 88

def self.has_table?(organism, table)
  sql_file = _get_file(organism, File.basename(base_url(organism)), 'sql')
  ! sql_file.match(/^CREATE TABLE .#{table}. \((.*?)^\)/sm).nil?
end

.url_for(organism, table, extension) ⇒ Object



71
72
73
# File 'lib/rbbt/sources/ensembl_ftp.rb', line 71

def self.url_for(organism, table, extension)
  File.join(base_url(organism), table) + ".#{extension}.gz"
end