class Bio::GO::Ontology

Bio::GO::Ontology

Container class for ontologies in the DAG Edit format.

Example

c_data = File.open('component.oontology').read
go_c = Bio::GO::Ontology.new(c_data)
p go_c.bfs_shortest_path('0003673','0005632')

Attributes

header_lines[R]

Returns a Hash instance of the header lines in ontology flatfile.

id2id[R]
id2term[R]

Public Class Methods

new(str) click to toggle source

::new The DAG Edit format ontology data parser.

Calls superclass method Bio::Pathway.new
# File lib/bio/db/go.rb, line 69
def initialize(str)
  @id2term      = {}
  @header_lines = {}
  @id2id        = {}
  adj_list = dag_edit_format_parser(str)
  super(adj_list)
end
parse_goids(line) click to toggle source

Bio::GO::Ontology.parse_ogids(line)

Parsing GOID line in the DAGEdit format

GO:ID[ ; GO:ID...]
# File lib/bio/db/go.rb, line 40
def self.parse_goids(line)
  goids = []
  loop {
    if /^ *[$%<]\S.+?;/ =~ line
      endpoint = line.index(';') + 1
      line = line[endpoint..line.size]
    elsif /^,* GO:(\d{7}),*/ =~ line
      goids << $1.clone
      endpoint = line.index(goids.last) + goids.last.size
      line = line[endpoint..line.size]
    else
      break
    end
  }
  return goids
end

Public Instance Methods

goid2term(goid) click to toggle source

Returns a GO_Term correspondig with the given GO_ID.

# File lib/bio/db/go.rb, line 79
def goid2term(goid)
  term = id2term[goid]
  term = id2term[id2id[goid]] if term == nil
  return term
end

Private Instance Methods

dag_edit_format_parser(str) click to toggle source

constructing adjaency list for the given ontology

# File lib/bio/db/go.rb, line 88
def dag_edit_format_parser(str)
  stack    = []
  adj_list = []
  
  str.each_line {|line|
    if /^!(.+?):\s+(\S.+)$/ =~ line  # Parsing head lines
      tag   = $1
      value = $2
      tag.gsub!(/-/,'_')
      next if tag == 'type'
      instance_eval("@header_lines['#{tag}'] = '#{value}'")
      next
    end
    
    case line
    when /^( *)([$<%])(.+?) ; GO:(\d{7})(\n*)/ # GO Term ; GO:ID
      depth = $1.length.to_i
      rel   = $2
      term  = $3
      goid1 = goid = $4
      en    = $5
      goids = parse_goids(line)   # GO:ID[ ; GO:ID...]
      synonyms = parse_synonyms(line)  # synonym:Term[ ; synonym:Term...]
      stack[depth]   = goids.first
      @id2term[goid] = term
      
      next if depth == 0

      goids.each {|goid|
        @id2term[goid] = term
        @id2id[goid]   = goids.first
        adj_list << Bio::Relation.new(stack[depth - 1], goid, rel)
      }
        
      if en == ""
        loop {
          case line
          when /^\n$/
            break
          when /^ *([<%]) (.+?) ; GO:(\d{7})/ # <%GO Term ; GO:ID
            rel1  = $1
            term1 = $2
            goid1 = $3
            goids1 = parse_goids(line)
            synonyms1 = parse_synonyms(line)
            
            @id2term[goid1] = term1
            goids.each {|goid|
              adj_list << Bio::Relation.new(goid1, goid, rel1)
            }
          else
            break
          end
        }
      end
    end
  }
  return adj_list
end
parse_goids(line) click to toggle source

Returns an ary of GO IDs by parsing an entry line in the DAG Edit format.

# File lib/bio/db/go.rb, line 151
def parse_goids(line)
  Ontology.parse_goids(line)
end
parse_synonyms(line) click to toggle source

#parse_synonyms

# File lib/bio/db/go.rb, line 156
def parse_synonyms(line)
  synonyms = []
  loop {
    if / ; synonym:(\S.+?) *[;<%\n]/ =~ line
      synonyms << $1.clone
      endpoint = line.index(synonyms.last) + synonyms.last.size
      line = line[endpoint..line.size]
    else
      break
    end
  }
  return synonyms
end