Module: Sylfy::Service::KEGGREST

Defined in:
lib/sylfy/service/keggrest.rb,
lib/sylfy/service/keggrest/get.rb,
lib/sylfy/service/keggrest/conv.rb,
lib/sylfy/service/keggrest/find.rb,
lib/sylfy/service/keggrest/link.rb,
lib/sylfy/service/keggrest/list.rb

Overview

interface to KEGG API http://www.kegg.jp/kegg/rest/keggapi.html info | list | find | get | conv | link

Constant Summary collapse

BASEURI =
"http://rest.kegg.jp"
DB =
[:pathway, :brite, :module, :disease, :drug, :environ, 
:ko, :genome, :compound, :glycan, :reaction, :rpair, 
:rclass, :enzyme, :genomes, :genes, :ligand, :kegg, :organism]

Class Method Summary collapse

Class Method Details

.conv(targetdb, *sourcedb) ⇒ Hash

interface to KEGG conv service

Parameters:

Returns:

  • (Hash)

    results with sourcedb ID as keys



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sylfy/service/keggrest/conv.rb', line 19

def conv(targetdb, *sourcedb)
  
  begin
    doc = URI.parse("#{Sylfy::Service::KEGGREST::BASEURI}/conv/#{targetdb.to_s}/#{sourcedb.join("+")}").read().strip()
    result = {}
    
    doc.split(/\n/).each do |line|
      dat = line.chomp.split(/\t/)
      result[dat[0]] = [] if !result.has_key?(dat[0])
      result[dat[0]].push(dat[1])
    end
    
    return result
    
  rescue OpenURI::HTTPError
    raise Sylfy::Service::DataNotFound, "Query not found."
  end
  
end

.find(database, query, option = nil) ⇒ Hash

interface to KEGG find service

Parameters:

  • database (Symbol)

    database name can be :pathway, :module, :disease, :drug, :environ, :ko, :genome, org, :compound, :glycan, :reaction, :rpair, :rclass, :enzyme, :genes, :ligand

  • query (String)

    Query text keyword can be join with + sign or in double qoutw

  • option (Symbol) (defaults to: nil)

    can be :formula, :exact_mass, :mol_weight with database = :compound, :drug

Returns:

  • (Hash)

    results with sourcedb ID as keys



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
# File 'lib/sylfy/service/keggrest/find.rb', line 21

def find(database, query, option=nil)
  
  result_tag = {
    :pathway => [:NAME],
    :module => [:NAME],
    :disease => [:NAME],
    :drug => [:NAME],
    :environ => [:NAME],
    :ko => [:NAME, :DEFINITION],
    :genome => [:NAME, :DEFINITION],
    :compound => [:NAME],
    :glycan => [:NAME, :COMPOSITION, :CLASS],
    :reaction => [:NAME, :DEFINITION],
    :rpair => [:NAME],
    :rclass => [:NAME, :DEFINITION],
    :enzyme => [:NAME]
  }
  result_tag.default = [:NAME, :DEFINITION, :ORTHOLOGY]
  result = {}
  begin
    if ['compound', 'drug'].include?(database.to_s) && option != nil &&
      ['formula', 'exact_mass' 'mol_weight'].include?(option.to_s)
      URI.parse("#{Sylfy::Service::KEGGREST::BASEURI}/find/#{database}/#{URI.escape(query)}/#{option}").read.strip.split(/\n/).each do |line|
        dat = line.chomp.split(/\t/)
        result[dat[0]] = { option.to_sym => dat[1]}
      end
    else
      URI.parse("#{Sylfy::Service::KEGGREST::BASEURI}/find/#{database}/#{URI.escape(query)}").read.strip.split(/\n/).each do |line|
        dat = line.chomp.split(/\t/)
        result[dat[0]] = {}
        info = dat[1].split('; ')
        info.each_index {|ind| result[dat[0]][result_tag[database.to_sym][ind]] = info[ind]}
      end
    end
    
    return result
  rescue OpenURI::HTTPError
    raise Unisys::ServiceException, "Query not found."
  end
  
end

.get(dbentries, option = nil) ⇒ Array

interface to KEGG get service

Parameters:

  • dbentries (Symbol, String)

    can be :pathway, :brite, :module, :disease, :drug, :environ, :ko, :genome, :org, :compound, :glycan, :reaction, :rpair, :rclass, :enzyme

  • option (Symbol) (defaults to: nil)

    can be aaseq, :ntseq, :mol, :kcf, :image, :kgml

Returns:

  • (Array)

    An array of Bio::KEGG object or string if option = :mol, :kcf, :kgml or Bio::FastaFormat if option = :aaseq, :ntseq



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
# File 'lib/sylfy/service/keggrest/get.rb', line 19

def get(dbentries, option = nil)
  
  if option != nil && ['aaseq', 'ntseq', 'mol', 'kcf', 'kgml'].include?(option.to_s)
    text = "#{dbentries}/#{option}"
  else
    text = "#{dbentries}"
  end
  
  begin
    doc = URI.parse("#{Sylfy::Service::KEGGREST::BASEURI}/get/#{text}").read
    
  rescue OpenURI::HTTPError
    raise Sylfy::Service::DataNotFound, "Query not found."
  end
  
  if ['aaseq', 'ntseq'].include?(option.to_s)
    return Bio::FastaFormat.new(doc)
  elsif option.to_s == 'mol'
    return doc
  elsif option.to_s == 'kcf'
    return doc
  elsif option.to_s == 'kgml'
    return Bio::KEGG::KGML.new(doc.strip)
  else
    result = []
  
    doc.strip.split('///').each do |entry|
      case entry
      when /^ENTRY\s+[A-Z0-9]+\s+Glycan/
        result.push(Bio::KEGG::GLYCAN.new(entry))
      when /^ENTRY\s+[A-Z0-9]+\s+Compound/
        result.push(Bio::KEGG::COMPOUND.new(entry))
      when /^ENTRY\s+[A-Z0-9]+\s+CDS/
        result.push(Bio::KEGG::GENES.new(entry))
      when /^ENTRY\s+[A-Z0-9]+\s+Drug/
        result.push(Bio::KEGG::DRUG.new(entry))
      when /^ENTRY\s+[A-Z0-9]+\s+Reaction/
        result.push(Bio::KEGG::REACTION.new(entry))
      when /^ENTRY\s+[A-Z0-9]+\s+RPair/
        result.push(entry)
      when /^ENTRY\s+[A-Z0-9]+\s+Genome/
        result.push(Bio::KEGG::GENOME.new(entry))
      when /^ENTRY\s+[A-Z0-9]+\s+Enzyme/
        result.push(Bio::KEGG::ENZYME.new(entry))
      when /^ENTRY\s+[A-Z0-9]+\s+Module/
        result.push(Bio::KEGG::MODULE.new(entry))
      when /^ENTRY\s+[A-Z0-9]+\s+Pathway/
        result.push(Bio::KEGG::PATH.new(entry))
      else
        result.push(entry)
      end
    end
  end
  
  return result
  
end

interface to KEGG link service

Parameters:

  • targetdb (Symbol)

    can be pathway, :brite, :module, :disease, :drug, :environ, :ko, :genome, org, :compound, :glycan, :reaction, :rpair, :rclass, :enzyme

  • sourcedb (Symbol)

    :pathway, :brite, :module, :disease, :drug, :environ, :ko, :genome, org, :compound, :glycan, :reaction, :rpair, :rclass, :enzyme, :genes

Returns:

  • (Hash)

    results with ID and description



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sylfy/service/keggrest/link.rb', line 19

def link(targetdb, *sourcedb)
  
  begin
    doc = URI.parse("#{Sylfy::Service::KEGGREST::BASEURI}/link/#{targetdb.to_s}/#{sourcedb.join("+")}").read().strip()
    result = {}
    
    doc.split(/\n/).each do |line|
      dat = line.chomp.split(/\t/)
      result[dat[0]] = result.has_key?(dat[0]) ? result[dat[0]].push(dat[1]) : [dat[1]]
    end
  
    return result
  rescue OpenURI::HTTPError
    raise Sylfy::Service::DataNotFound, "Query not found."
  end
  
end

.list(database, org = nil) ⇒ Hash

interface to KEGG list service

Parameters:

  • database (Symbol)

    :pathway, :module, :disease, :drug, :environ, :ko, :genome, org, :compound, :glycan, :reaction, :rpair, :rclass, :enzyme, :genes, :ligand

  • org (Symbol) (defaults to: nil)

    organism code can be :hsa, :eco, etc http://www.kegg.jp/kegg/rest/keggapi.html

Returns:

  • (Hash)

    results with ID and description



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sylfy/service/keggrest/list.rb', line 19

def list(database, org = nil)
  
  begin
    doc = URI.parse("#{Sylfy::Service::KEGGREST::BASEURI}/list/#{database.to_s}#{org ? "/#{org}" : ""}").read.strip
    result = {}
    doc.split(/\n/).each do |line|
      dat = line.chomp.split(/\t/, 2)
      result[dat[0]] = dat[1]
    end
    
    return result
  rescue OpenURI::HTTPError
    raise Sylfy::Service::DataNotFound, "Data not found."
  end
  
end

.ncbi2ko(*sourcedb) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sylfy/service/keggrest/conv.rb', line 39

def ncbi2ko(*sourcedb)
  begin
    ids = sourcedb.flatten.map { |x| x =~ /^\d+$/ ? "ncbi-geneid:" + x : x}
    hash1 = conv(:genes, ids)
    list1 = []
    hash1.each_value do |v|
      list1 += v
    end
    hash2 = link(:ko, list1)
    results = {}
    hash1.each_pair do |k, vs|
      results[k] ||= []
      vs.each {|v| results[k] = results[k] | hash2[v] if hash2.has_key?(v)}
    end
    
    results.delete_if {|key, value| value.empty? }
    
    return results
  rescue Sylfy::Service::DataNotFound
    return {}
  end
end

.ncbi2ko_org(org) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sylfy/service/keggrest/conv.rb', line 62

def ncbi2ko_org(org)
  
  results = {}
  
  begin
    hash1 = conv(org.to_s, 'ncbi-geneid')
    hash2 = link(:ko, org.to_s)
    hash1.each_pair do |k, vs|
      results[k] ||= []
      vs.each {|v| results[k] = results[k] | hash2[v] if hash2.has_key?(v)}
    end
    results.delete_if {|key, value| value.empty? }
    return results
  rescue
    return results
  end
  
  
end