class Bio::KEGG::KGML

KGML (KEGG XML) parser

See www.genome.jp/kegg/xml/ for more details on KGML.

Note for older version users

Incompatible attribute names with KGML tags

<entry>
:map -> :pathway
names()
<subtype>
edge()

Examples

file = File.read("kgml/hsa/hsa00010.xml")
kgml = Bio::KEGG::KGML.new(file)

# <pathway> attributes
puts kgml.name
puts kgml.org
puts kgml.number
puts kgml.title
puts kgml.image
puts kgml.link

kgml.entries.each do |entry|
  # <entry> attributes
  puts entry.id
  puts entry.name
  puts entry.type
  puts entry.link
  puts entry.reaction
  # <graphics> attributes
  entry.graphics.each do |graphics|
    puts graphics.name
    puts graphics.type
    puts graphics.x
    puts graphics.y
    puts graphics.width
    puts graphics.height
    puts graphics.fgcolor
    puts graphics.bgcolor
  end
  # <component> attributes
  puts entry.components
  # methood
  puts entry.names
end

kgml.relations.each do |relation|
  # <relation> attributes
  puts relation.entry1
  puts relation.entry2
  puts relation.type
  # <subtype> attributes
  puts relation.name
  puts relation.value
end

kgml.reactions.each do |reaction|
  # <reaction> attributes
  puts reaction.name
  puts reaction.type
  # <substrate> attributes
  reaction.substrates.each do |substrate|
    puts substrate.id
    puts substrate.name
    # <alt> attributes
    altnames = reaction.alt[entry_id]
    altnames.each do |name|
      puts name
    end
  end
  # <product> attributes
  reaction.products.each do |product|
    puts product.id
    puts product.name
    # <alt> attributes
    altnames = reaction.alt[entry_id]
    altnames.each do |name|
      puts name
    end
  end
end

References

Attributes

entries[RW]

entry elements (Array containing KGML::Entry objects, or nil)

image[R]

image URL of this pathway map (String or nil) ('pathway' element)

name[R]

KEGG-style ID string of this pathway map (String or nil) ('pathway' element)

number[R]

map number (String or nil) ('pathway' element)

org[R]

“ko” (KEGG Orthology), “ec” (KEGG ENZYME), or the KEGG 3-letter organism code (String or nil) ('pathway' element)

reactions[RW]

reaction elements (Array containing KGML::Reactions objects, or nil)

relations[RW]

relation elements (Array containing KGML::Relations objects, or nil)

title[R]

title (String or nil) ('pathway' element)

Public Class Methods

new(xml) click to toggle source

Creates a new KGML object.


Arguments:

  • (required) str: String containing xml data

Returns

Bio::KEGG::KGML object

# File lib/bio/db/kegg/kgml.rb, line 141
def initialize(xml)
  dom = REXML::Document.new(xml)
  parse_root(dom)
  parse_entry(dom)
  parse_relation(dom)
  parse_reaction(dom)
end

Private Instance Methods

parse_entry(dom) click to toggle source
# File lib/bio/db/kegg/kgml.rb, line 542
def parse_entry(dom)
  @entries = Array.new

  dom.elements.each("/pathway/entry") { |node|
    attr = node.attributes
    entry = Entry.new
    entry.id   = attr["id"].to_i
    entry.name = attr["name"]
    entry.type = attr["type"]
    # implied
    entry.link     = attr["link"]
    entry.reaction = attr["reaction"]
    entry.pathway  = attr["map"]

    node.elements.each("graphics") { |graphics|
      g = Graphics.new
      attr = graphics.attributes
      g.x       = attr["x"].to_i
      g.y       = attr["y"].to_i
      g.type    = attr["type"]
      g.name    = attr["name"]
      g.width    = attr["width"].to_i
      g.height   = attr["height"].to_i
      g.fgcolor  = attr["fgcolor"]
      g.bgcolor  = attr["bgcolor"]
      if str = attr["coords"] then
        coords = []
        tmp = str.split(',')
        tmp.collect! { |n| n.to_i }
        while xx = tmp.shift
          yy = tmp.shift
          coords.push [ xx, yy ]
        end
        g.coords = coords
      else
        g.coords = nil
      end
      entry.graphics ||= []
      entry.graphics.push g
    }

    node.elements.each("component") { |component|
      attr = component.attributes
      entry.components ||= []
      entry.components << attr["id"].to_i
    }

    @entries << entry
  }
end
parse_reaction(dom) click to toggle source
# File lib/bio/db/kegg/kgml.rb, line 612
def parse_reaction(dom)
  @reactions = Array.new

  dom.elements.each("/pathway/reaction") { |node|
    attr = node.attributes
    reaction = Reaction.new
    reaction.id   = attr["id"].to_i
    reaction.name = attr["name"]
    reaction.type = attr["type"]

    substrates = Array.new
    products   = Array.new
    hash        = Hash.new

    node.elements.each("substrate") { |substrate|
      id = substrate.attributes["id"].to_i
      name = substrate.attributes["name"]
      substrates << Substrate.new(id, name)
      substrate.elements.each("alt") { |alt|
        hash[name] ||= Array.new
        hash[name] << alt.attributes["name"]
      }
    }
    node.elements.each("product") { |product|
      id = product.attributes["id"].to_i
      name = product.attributes["name"]
      products << Product.new(id, name)
      product.elements.each("alt") { |alt|
        hash[name] ||= Array.new
        hash[name] << alt.attributes["name"]
      }
    }
    reaction.substrates = substrates
    reaction.products = products
    reaction.alt = hash

    @reactions << reaction
  }
end
parse_relation(dom) click to toggle source
# File lib/bio/db/kegg/kgml.rb, line 593
def parse_relation(dom)
  @relations = Array.new

  dom.elements.each("/pathway/relation") { |node|
    attr = node.attributes
    relation = Relation.new
    relation.entry1 = attr["entry1"].to_i
    relation.entry2 = attr["entry2"].to_i
    relation.type   = attr["type"]

    node.elements.each("subtype") { |subtype|
      attr = subtype.attributes
      relation.name  = attr["name"]
      relation.value = attr["value"]
    }
    @relations << relation
  }
end
parse_root(dom) click to toggle source
# File lib/bio/db/kegg/kgml.rb, line 532
def parse_root(dom)
  root    = dom.root.attributes
  @name   = root["name"]
  @org    = root["org"]
  @number = root["number"]
  @title  = root["title"]
  @image  = root["image"]
  @link   = root["link"]
end