Class: OpenTox::QMRFReport
- Inherits:
-
Object
- Object
- OpenTox::QMRFReport
- Defined in:
- lib/qmrf-report.rb
Overview
Class for QMRF reporting.
Provides a ruby OpenTox class to prepare an initial version of a QMRF report. The XML output is in QMRF version 1.3 and can be finalized with the QMRF editor 2.0 (sourceforge.net/projects/qmrf/)
Constant Summary collapse
- SCHEMA_FILE =
QMRF XML Schema file
File.join(File.dirname(__FILE__),"template/qmrf.xsd")
- TEMPLATE_FILE =
QMRF XML Template file
File.join(File.dirname(__FILE__),"template/qmrf.xml")
- CATALOGS =
QMRF catalogs. Entries of a catalog can be referenced in certain tags.
["software_catalog", "algorithms_catalog", "descriptors_catalog", "endpoints_catalog", "publications_catalog", "authors_catalog"]
- ATTRIBUTE_TAGS =
QMRF XML tags with attributes to edit
["training_set_availability", "training_set_data", "training_set_descriptors", "dependent_var_availability", "validation_set_availability", "validation_set_data", "validation_set_descriptors", "validation_dependent_var_availability"]
Instance Attribute Summary collapse
-
#report ⇒ Object
Returns the value of attribute report.
-
#xml ⇒ Object
Returns the value of attribute xml.
Instance Method Summary collapse
-
#catalog_exists?(catalog) ⇒ Error, true
Check if a catalog exists in this QMRF version.
-
#change_attributes(tagname, valuehash) ⇒ Error
Set attributes of an report XML tag.
-
#change_catalog(catalog, id, valuehash) ⇒ Error
Change a catalog.
-
#get_catalog_value(catalog, id, key) ⇒ String, false
get an attribute from a catalog entry.
-
#initialize ⇒ QMRFReport
constructor
Initialize a new report instance from qmrf template.
-
#open(file) ⇒ Object
Open an existing QMRF xml report.
-
#ref_catalog(chapter, catalog, id) ⇒ Object
Set reference to a catalog entry.
-
#to_xml ⇒ String
returns XML representation (QMRF XML report) of report instance.
-
#validate ⇒ Object
Validates a report instance against qmrf.xsd (XML Structure Definition).
-
#value(key, value = nil) ⇒ Error, String
Get or Set a value e.G.:.
Constructor Details
#initialize ⇒ QMRFReport
Initialize a new report instance from qmrf template
39 40 41 42 |
# File 'lib/qmrf-report.rb', line 39 def initialize xml = File.read(TEMPLATE_FILE) @report = Nokogiri.XML(xml) end |
Instance Attribute Details
#report ⇒ Object
Returns the value of attribute report.
29 30 31 |
# File 'lib/qmrf-report.rb', line 29 def report @report end |
#xml ⇒ Object
Returns the value of attribute xml.
29 30 31 |
# File 'lib/qmrf-report.rb', line 29 def xml @xml end |
Instance Method Details
#catalog_exists?(catalog) ⇒ Error, true
Check if a catalog exists in this QMRF version
144 145 146 147 |
# File 'lib/qmrf-report.rb', line 144 def catalog_exists? catalog raise "Unknown catalog: #{catalog}" unless CATALOGS.include? catalog.to_s true end |
#change_attributes(tagname, valuehash) ⇒ Error
Set attributes of an report XML tag. Some of the QMRF XML tags have attributes to be edited. This applies to 6.1 to 6.4 and 7.1 to 7.4 see also: ATTRIBUTE_TAGS.
e.G. “Available information for the training set” at 6.2 of the report:
<training_set_data cas="Yes" chapter="6.2" chemname="Yes" formula="Yes" help="" inchi="Yes" mol="Yes" name="Available information for the training set" smiles="Yes"/>
76 77 78 79 80 81 82 |
# File 'lib/qmrf-report.rb', line 76 def change_attributes tagname, valuehash raise "Can not edit the attributes of tag: #{tagname}." unless ATTRIBUTE_TAGS.include? tagname tag = @report.at_css tagname valuehash.each do |key, value| tag.attributes["#{key}"].value = value end end |
#change_catalog(catalog, id, valuehash) ⇒ Error
Change a catalog
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/qmrf-report.rb', line 89 def change_catalog catalog, id, valuehash catalog_exists? catalog if @report.at_css("#{catalog}").at("[@id='#{id}']") valuehash.each do |key, value| @report.at_css("#{catalog}").at("[@id='#{id}']")["#{key}"]= value end else cat = @report.at_css("#{catalog}") newentry = Nokogiri::XML::Node.new("#{catalog.to_s.gsub(/s?_catalog/,'')}", self.report) newentry["id"] = id valuehash.each do |key, value| newentry["#{key}"] = value end cat << newentry end end |
#get_catalog_value(catalog, id, key) ⇒ String, false
get an attribute from a catalog entry
132 133 134 135 136 137 138 139 |
# File 'lib/qmrf-report.rb', line 132 def get_catalog_value catalog, id, key catalog_exists? catalog if @report.at_css("#{catalog}").at("[@id='#{id}']") @report.at_css("#{catalog}").at("[@id='#{id}']")["#{key}"] else return false end end |
#open(file) ⇒ Object
Open an existing QMRF xml report
33 34 35 36 |
# File 'lib/qmrf-report.rb', line 33 def open file xml = File.read("#{file}") @report = Nokogiri.XML(xml) end |
#ref_catalog(chapter, catalog, id) ⇒ Object
Set reference to a catalog entry. e.g.: reference an author entry from authors_catalog to Chapter 2.2 QMRF authors
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/qmrf-report.rb', line 113 def ref_catalog chapter, catalog, id catalog_exists? catalog if @report.at_css("#{catalog}").at("//*[@id='#{id}']") chap = @report.at_css("#{chapter}") if chap.at("[@idref='#{id}']").nil? newentry = Nokogiri::XML::Node.new("#{catalog.to_s.gsub(/s?_catalog/,'_ref')}", self.report) newentry["idref"] = id chap << newentry end else raise "catalog entry with id: #{id} do not exist." end end |
#to_xml ⇒ String
returns XML representation (QMRF XML report) of report instance
46 47 48 |
# File 'lib/qmrf-report.rb', line 46 def to_xml @report.to_xml end |
#validate ⇒ Object
Validates a report instance against qmrf.xsd (XML Structure Definition)
150 151 152 153 154 155 156 157 158 |
# File 'lib/qmrf-report.rb', line 150 def validate xsd = Nokogiri::XML::Schema(File.read(SCHEMA_FILE)) out = "" xsd.validate(@report).each do |error| out << error. unless error. == "Element 'algorithm', attribute 'publication_ref': '' is not a valid value of the atomic type 'xs:IDREF'." || error. == "Element 'descriptor', attribute 'publication_ref': '' is not a valid value of the atomic type 'xs:IDREF'." # @todo ignore case sensitivity error: error.message The value 'NO' is not an.Element of the set {'Yes', 'No'}. end return out end |
#value(key, value = nil) ⇒ Error, String
Get or Set a value e.G.:
60 61 62 63 64 65 |
# File 'lib/qmrf-report.rb', line 60 def value key, value=nil raise "Can not edit attribute #{key} directly. Edit the catalog with 'report.change_catalog(catalog, key, value)'." if ["QSAR_software","QSAR_Algorithm", ""].include? key t = @report.at_css key t.content = value unless value.nil? t.content end |