Skip to content

Commit

Permalink
Made the code compactible with libxml-ruby 1.1.3 (previous was 0.9.4)…
Browse files Browse the repository at this point in the history
… version.
  • Loading branch information
latvianlinuxgirl authored and ngoto committed Sep 19, 2009
1 parent ce3bacd commit 401eea1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
58 changes: 29 additions & 29 deletions lib/bio/db/phyloxml/phyloxml_elements.rb
Expand Up @@ -19,7 +19,7 @@

module Bio

# This is general Taxonomy class.
# This is general Taxonomy class.

class Taxonomy
#pattern = [a-zA-Z0-9_]{2,10} Can refer to any code/abbreviation/mnemonic, such as Bsu for Bacillus subtilis.
Expand Down Expand Up @@ -76,7 +76,7 @@ def initialize

# Converts elements to xml representation. Called by PhyloXML::Writer class.
def to_xml
taxonomy = XML::Node.new('taxonomy')
taxonomy = LibXML::XML::Node.new('taxonomy')
taxonomy["type"] = @type if @type != nil
taxonomy["id_source"] = @id_source if @id_source != nil

Expand Down Expand Up @@ -271,13 +271,13 @@ def extract_biosequence(seq_i=0)

# Converts elements to xml representation. Called by PhyloXML::Writer class.
def to_xml(branch_length, write_branch_length_as_subelement)
clade = XML::Node.new('clade')
clade = LibXML::XML::Node.new('clade')

PhyloXML::Writer.generate_xml(clade, self, [[:simple, 'name', @name]])

if branch_length != nil
if write_branch_length_as_subelement
clade << XML::Node.new('branch_length', branch_length)
clade << LibXML::XML::Node.new('branch_length', branch_length.to_s)
else
clade["branch_length"] = branch_length.to_s
end
Expand Down Expand Up @@ -347,7 +347,7 @@ def type=(str)
# Converts elements to xml representation. Called by PhyloXML::Writer class.
def to_xml
#@todo add unit test
events = XML::Node.new('events')
events = LibXML::XML::Node.new('events')
PhyloXML::Writer.generate_xml(events, self, [
[:simple, 'type', @type],
[:simple, 'duplications', @duplications],
Expand Down Expand Up @@ -378,7 +378,7 @@ def to_xml
if @type == nil
raise "Type is a required attribute for confidence."
else
confidence = XML::Node.new('confidence', @value.to_f)
confidence = LibXML::XML::Node.new('confidence', @value.to_s)
confidence["type"] = @type
return confidence
end
Expand All @@ -405,7 +405,7 @@ def initialize

# Converts elements to xml representation. Called by PhyloXML::Writer class.
def to_xml
distr = XML::Node.new('distribution')
distr = LibXML::XML::Node.new('distribution')
PhyloXML::Writer.generate_xml(distr, self, [
[:simple, 'desc', @desc],
[:objarr, 'point', 'points'],
Expand Down Expand Up @@ -453,7 +453,7 @@ def alt=(str)
def to_xml
raise "Geodedic datum is a required attribute of Point element." if @geodetic_datum.nil?

p = XML::Node.new('point')
p = LibXML::XML::Node.new('point')
p["geodetic_datum"] = @geodetic_datum
p["alt_unit"] = @alt_unit if @alt_unit != nil
PhyloXML::Writer.generate_xml(p, self, [
Expand Down Expand Up @@ -482,7 +482,7 @@ def initialize
# Converts elements to xml representation. Called by PhyloXML::Writer class.
def to_xml
if @points.length > 2
pol = XML::Node.new('polygon')
pol = LibXML::XML::Node.new('polygon')
@points.each do |p|
pol << p.to_xml
end
Expand Down Expand Up @@ -566,7 +566,7 @@ def mol_seq=(str)
# Converts elements to xml representation. Called by PhyloXML::Writer class.
def to_xml

seq = XML::Node.new('sequence')
seq = LibXML::XML::Node.new('sequence')
if @type != nil
if ["dna", "rna", "protein"].include?(@type)
seq["type"] = @type
Expand All @@ -584,7 +584,7 @@ def to_xml
[:simple, 'location', @location]])

if @mol_seq != nil
molseq = XML::Node.new('mol_seq', @mol_seq)
molseq = LibXML::XML::Node.new('mol_seq', @mol_seq)
molseq["is_aligned"] = @is_aligned.to_s if @is_aligned != nil
seq << molseq
end
Expand Down Expand Up @@ -641,7 +641,7 @@ class Accession
# Converts elements to xml representation. Called by PhyloXML::Writer class.
def to_xml
raise "Source attribute is required for Accession object." if @source == nil
accession = XML::Node.new('accession', @value)
accession = LibXML::XML::Node.new('accession', @value)
accession['source'] = @source
return accession
end
Expand All @@ -663,7 +663,7 @@ class Uri
# Converts elements to xml representation. Called by PhyloXML::Writer class.
def to_xml
if @uri != nil
xml_node = XML::Node.new('uri', @uri)
xml_node = LibXML::XML::Node.new('uri', @uri)
Writer.generate_xml(xml_node, self, [
[:attr, 'desc'],
[:attr, 'type']])
Expand Down Expand Up @@ -705,7 +705,7 @@ def initialize

# Converts elements to xml representation. Called by PhyloXML::Writer class.
def to_xml
annot = XML::Node.new('annotation')
annot = LibXML::XML::Node.new('annotation')
annot["ref"] = @ref if @ref != nil
PhyloXML::Writer.generate_xml(annot, self, [[:simple, 'desc', @desc],
[:complex, 'confidence', @confidence],
Expand All @@ -723,7 +723,7 @@ class Id

# Converts elements to xml representation. Called by PhyloXML::Writer class.
def to_xml
xml_node = XML::Node.new('id', @value)
xml_node = LibXML::XML::Node.new('id', @value)
xml_node["provider"] = @provider if @provider != nil
return xml_node
end
Expand Down Expand Up @@ -760,7 +760,7 @@ def to_xml
raise "Subelement blue of BranchColor element should not be nil"
end

c = XML::Node.new('branch_color')
c = LibXML::XML::Node.new('branch_color')
PhyloXML::Writer.generate_xml(c, self, [
[:simple, 'red', @red],
[:simple, 'green', @green],
Expand Down Expand Up @@ -808,7 +808,7 @@ def to_s

# Converts elements to xml representation. Called by PhyloXML::Writer class.
def to_xml
date = XML::Node.new('date')
date = LibXML::XML::Node.new('date')
PhyloXML::Writer.generate_xml(date, self, [
[:attr, 'unit'],
[:simple, 'desc', @desc],
Expand Down Expand Up @@ -840,7 +840,7 @@ def initialize

# Converts elements to xml representation. Called by PhyloXML::Writer class.
def to_xml
xml_node = XML::Node.new('domain_architecture')
xml_node = LibXML::XML::Node.new('domain_architecture')
PhyloXML::Writer.generate_xml(xml_node, self,[
[:attr, 'length'],
[:objarr, 'domain', 'domains']])
Expand Down Expand Up @@ -884,7 +884,7 @@ def to_xml
elsif @to == nil
raise "to attribute of ProteinDomain class is required."
else
xml_node = XML::Node.new('domain', @value)
xml_node = LibXML::XML::Node.new('domain', @value)
xml_node["from"] = @from.to_s
xml_node["to"] = @to.to_s
xml_node["id"] = @id if @id != nil
Expand Down Expand Up @@ -945,7 +945,7 @@ def to_xml
raise "datatype is an required element of property" if @datatype.nil?
raise "applies_to is an required element of property" if @applies_to.nil?

property = XML::Node.new('property')
property = LibXML::XML::Node.new('property')
Writer.generate_xml(property, self, [
[:attr, 'ref'],
[:attr, 'unit'],
Expand All @@ -970,7 +970,7 @@ class Reference

# Converts elements to xml representation. Called by PhyloXML::Writer class.
def to_xml
ref = XML::Node.new('reference')
ref = LibXML::XML::Node.new('reference')
Writer.generate_xml(ref, self, [
[:attr, 'doi'],
[:simple, 'desc', @desc]])
Expand Down Expand Up @@ -1002,7 +1002,7 @@ def to_xml
if @id_ref_0 == nil or @id_ref_1 == nil or @type == nil
raise "Attributes id_ref_0, id_ref_1, type are required elements by SequenceRelation element."
else
cr = XML::Node.new('clade_relation')
cr = LibXML::XML::Node.new('clade_relation')
Writer.generate_xml(cr, self, [
[:attr, 'id_ref_0'],
[:attr, 'id_ref_1'],
Expand Down Expand Up @@ -1049,7 +1049,7 @@ def initialize

# Converts elements to xml representation. Called by PhyloXML::Writer class.
def to_xml
bc = XML::Node.new('binary_characters')
bc = LibXML::XML::Node.new('binary_characters')
bc['type'] = @bc_type
PhyloXML::Writer.generate_xml(bc, self, [
[:attr, 'gained_count'],
Expand All @@ -1058,25 +1058,25 @@ def to_xml
[:attr, 'absent_count']])

if not @gained.empty?
gained_xml = XML::Node.new('gained')
gained_xml = LibXML::XML::Node.new('gained')
PhyloXML::Writer.generate_xml(gained_xml, self, [[:simplearr, 'bc', @gained]])
bc << gained_xml
end

if not @lost.empty?
lost_xml = XML::Node.new('lost')
lost_xml = LibXML::XML::Node.new('lost')
PhyloXML::Writer.generate_xml(lost_xml, self, [[:simplearr, 'bc', @lost]])
bc << lost_xml
end

if not @present.empty?
present_xml = XML::Node.new('present')
present_xml = LibXML::XML::Node.new('present')
PhyloXML::Writer.generate_xml(present_xml, self, [[:simplearr, 'bc', @present]])
bc << present_xml
end

if not @absent.empty?
absent_xml = XML::Node.new('absent')
absent_xml = LibXML::XML::Node.new('absent')
PhyloXML::Writer.generate_xml(absent_xml, self, [[:simplearr, 'bc', @absent]])
bc << absent_xml
end
Expand Down Expand Up @@ -1120,7 +1120,7 @@ def to_xml
if @id_ref_0 == nil or @id_ref_1 == nil or @type == nil
raise "Attributes id_ref_0, id_ref_1, type are required elements by SequenceRelation element."
else
sr = XML::Node.new('sequence_relation')
sr = LibXML::XML::Node.new('sequence_relation')
sr['id_ref_0'] = @id_ref_0
sr['id_ref_1'] = @id_ref_1
sr['distance'] = @distance.to_s if @distance != nil
Expand All @@ -1141,7 +1141,7 @@ def initialize

# Converts elements to xml representation. Called by PhyloXML::Writer class.
def to_xml
o = XML::Node.new(@element_name)
o = LibXML::XML::Node.new(@element_name)
@attributes.each do |key, value|
o[key] = value
end
Expand Down
10 changes: 8 additions & 2 deletions lib/bio/db/phyloxml/phyloxml_parser.rb
Expand Up @@ -33,7 +33,11 @@ module PhyloXML

require 'bio/db/phyloxml/phyloxml_elements'

require 'xml'
require 'rubygems' #needed to require the libxml gem

require 'libxml'




# == Description
Expand Down Expand Up @@ -66,6 +70,8 @@ module PhyloXML
#
class Parser

include LibXML

# After parsing all the trees, if there is anything else in other xml format,
# it is saved in this array of PhyloXML::Other objects
attr_reader :other
Expand All @@ -88,7 +94,7 @@ def initialize(filename, validate=true)

if File.exists?(filename)
# By default do validation
unless validate == false
unless validate == false
schema = XML::Schema.document(XML::Document.file(File.join(File.dirname(__FILE__),'phyloxml.xsd')))
xml_instance = XML::Document.file(filename)
xml_instance.validate_schema(schema) do |msg, flag|
Expand Down
8 changes: 5 additions & 3 deletions lib/bio/db/phyloxml/phyloxml_writer.rb
Expand Up @@ -65,6 +65,8 @@ module PhyloXML

class Writer

include LibXML

SCHEMA_LOCATION = 'http://www.phyloxml.org http://www.phyloxml.org/1.10/phyloxml.xsd'

attr_accessor :write_branch_length_as_subelement
Expand All @@ -91,7 +93,7 @@ def initialize(filename, indent=true)
#it gives error NameError: uninitialized constant LibXML::XML::Encoding
#@doc.encoding = XML::Encoding::UTF_8

@doc.save(@filename, true)
@doc.save(@filename, :indent => true)
end

#
Expand Down Expand Up @@ -125,7 +127,7 @@ def write(tree)
[:objarr, 'sequence_relation', 'sequence_relations'],
[:objarr, 'property', 'properties']] )

@doc.save(@filename, @indent)
@doc.save(@filename, :indent => @indent)
end #writer#write


Expand All @@ -149,7 +151,7 @@ def write_other(other_arr)
other_arr.each do |other_obj|
@root << other_obj.to_xml
end
@doc.save(@filename, @indent)
@doc.save(@filename, :indent => @indent)
end

#class method
Expand Down

0 comments on commit 401eea1

Please sign in to comment.