module Bio::PhyloXML

Private Instance Methods

parse_annotation() click to toggle source
# File lib/bio/db/phyloxml/phyloxml_parser.rb, line 822
def parse_annotation
  annotation = Annotation.new

  parse_attributes(annotation, ['ref', 'source', 'evidence', 'type'])

  if not @reader.empty_element?
    while not(is_end_element?('annotation'))
      parse_simple_element(annotation, 'desc') if is_element?('desc')

      annotation.confidence  = parse_confidence if is_element?('confidence')

      annotation.properties << parse_property if is_element?('property')

      if is_element?('uri')
        annotation.uri = parse_uri        
      end

      @reader.read
    end
    
  end
  return annotation
end
parse_bc(object, element) click to toggle source
# File lib/bio/db/phyloxml/phyloxml_parser.rb, line 959
def parse_bc(object, element)
  if is_element?(element)
    @reader.read
    while not is_end_element?(element)
      if is_element?('bc')
        @reader.read
        object.send(element) << @reader.value
        @reader.read
        has_reached_end_element?('bc')
      end
    @reader.read
    end
  end
end
parse_binary_characters() click to toggle source
# File lib/bio/db/phyloxml/phyloxml_parser.rb, line 939
def parse_binary_characters
  b = PhyloXML::BinaryCharacters.new
  b.bc_type = @reader['type']

  parse_attributes(b, ['gained_count', 'absent_count', 'lost_count', 'present_count'])
  if not @reader.empty_element?
    @reader.read
    while not is_end_element?('binary_characters')

      parse_bc(b, 'lost')
      parse_bc(b, 'gained')
      parse_bc(b, 'absent')
      parse_bc(b, 'present')

      @reader.read
    end
  end
  return b
end
parse_confidence() click to toggle source
# File lib/bio/db/phyloxml/phyloxml_parser.rb, line 856
def parse_confidence
  type = @reader["type"]
  @reader.read
  value = @reader.value.to_f
  @reader.read
  has_reached_end_element?('confidence')
  return Confidence.new(type, value)
end
parse_distribution() click to toggle source
# File lib/bio/db/phyloxml/phyloxml_parser.rb, line 865
def parse_distribution
  distribution = Distribution.new
  @reader.read
  while not(is_end_element?('distribution')) do

    parse_simple_element(distribution, 'desc')

    distribution.points << parse_point if is_element?('point')
    distribution.polygons << parse_polygon if is_element?('polygon')

    @reader.read
  end
  return distribution
end
parse_domain() click to toggle source
# File lib/bio/db/phyloxml/phyloxml_parser.rb, line 928
def parse_domain
  domain = ProteinDomain.new
  parse_attributes(domain, ["from", "to", "confidence", "id"])
  @reader.read
  domain.value = @reader.value
  @reader.read
  has_reached_end_element?('domain')
  @reader.read
  return domain
end
parse_id(tag_name) click to toggle source
# File lib/bio/db/phyloxml/phyloxml_parser.rb, line 918
def parse_id(tag_name)
  id = Id.new
  id.provider = @reader["provider"]
  @reader.read
  id.value = @reader.value
  @reader.read #@todo shouldn't there be another read?
  has_reached_end_element?(tag_name)
  return id
end
parse_other() click to toggle source
# File lib/bio/db/phyloxml/phyloxml_parser.rb, line 974
def parse_other
  other_obj = PhyloXML::Other.new
  other_obj.element_name = @reader.name
  #parse attributes
  code = @reader.move_to_first_attribute
  while code ==1
    other_obj.attributes[@reader.name] = @reader.value
    code = @reader.move_to_next_attribute        
  end

  while not is_end_element?(other_obj.element_name) do
    @reader.read
    if @reader.node_type == XML::Reader::TYPE_ELEMENT
       other_obj.children << parse_other #recursice call to parse children
    elsif @reader.node_type == XML::Reader::TYPE_TEXT
      other_obj.value = @reader.value
    end
  end
  #just a check
  has_reached_end_element?(other_obj.element_name)
  return other_obj
end
parse_point() click to toggle source
# File lib/bio/db/phyloxml/phyloxml_parser.rb, line 880
def parse_point
  point = Point.new

  point.geodetic_datum = @reader["geodetic_datum"]
  point.alt_unit = @reader["alt_unit"]

  @reader.read
  while not(is_end_element?('point')) do

    parse_simple_elements(point, ['lat', 'long'] )

    if is_element?('alt')
      @reader.read
      point.alt = @reader.value.to_f
      @reader.read
      has_reached_end_element?('alt')
    end
    #advance reader
    @reader.read
  end
  return point
end
parse_polygon() click to toggle source
# File lib/bio/db/phyloxml/phyloxml_parser.rb, line 903
def parse_polygon
  polygon = Polygon.new
  @reader.read
  while not(is_end_element?('polygon')) do
    polygon.points << parse_point if is_element?('point')
    @reader.read
  end

  #@todo should check for it at all? Probably not if xml is valid.
  if polygon.points.length <3
    puts "Warning: <polygon> should have at least 3 points"
  end
  return polygon
end
parse_property() click to toggle source
# File lib/bio/db/phyloxml/phyloxml_parser.rb, line 846
def parse_property
  property = Property.new
  parse_attributes(property, ["ref", "unit", "datatype", "applies_to", "id_ref"])
  @reader.read
  property.value = @reader.value
  @reader.read
  has_reached_end_element?('property')     
  return property
end
parse_sequence() click to toggle source
# File lib/bio/db/phyloxml/phyloxml_parser.rb, line 757
def parse_sequence
  sequence = Sequence.new
  parse_attributes(sequence, ["type", "id_source", "id_ref"])
  
  @reader.read
  while not(is_end_element?('sequence'))

    if @reader.node_type == XML::Reader::TYPE_ELEMENT
      case @reader.name
      when 'symbol'
        @reader.read
        sequence.symbol = @reader.value
        @reader.read
      when 'name'
        @reader.read
        sequence.name = @reader.value
        @reader.read
      when 'location'
        @reader.read
        sequence.location = @reader.value
        @reader.read
      when 'mol_seq'
        sequence.is_aligned = @reader["is_aligned"]
        @reader.read
        sequence.mol_seq = @reader.value
        @reader.read
        has_reached_end_element?('mol_seq')
      when 'accession'
        sequence.accession = Accession.new
        sequence.accession.source = @reader["source"]
        @reader.read
        sequence.accession.value = @reader.value
        @reader.read
        has_reached_end_element?('accession')
      when 'uri'
        sequence.uri = parse_uri
      when 'annotation'
        sequence.annotations << parse_annotation
      when 'domain_architecture'
        sequence.domain_architecture = DomainArchitecture.new
        sequence.domain_architecture.length = @reader["length"]
        @reader.read
        @reader.read
        while not(is_end_element?('domain_architecture'))
          sequence.domain_architecture.domains << parse_domain
          @reader.read #go to next domain element
        end
      else
        sequence.other << parse_other
        #@todo add unit test            
      end
    end

    @reader.read
  end
  return sequence
end
parse_uri() click to toggle source
# File lib/bio/db/phyloxml/phyloxml_parser.rb, line 815
def parse_uri
  uri = Uri.new
  parse_attributes(uri, ["desc", "type"])
  parse_simple_element(uri, 'uri')
  return uri
end