Class: BEL::Translator::Plugins::Xbel::XBELYielder
- Inherits:
-
Object
- Object
- BEL::Translator::Plugins::Xbel::XBELYielder
- Defined in:
- lib/bel/translator/plugins/xbel/xbel_yielder.rb
Constant Summary collapse
Class Method Summary collapse
- .annotation_definitions(list) ⇒ Object
- .annotation_group(nanopub) ⇒ Object
- .citation(citation) ⇒ Object
- .document ⇒ Object
- .header(hash) ⇒ Object
- .namespace_definitions(list) ⇒ Object
- .nanopub(nanopub) ⇒ Object
- .object(object) ⇒ Object
- .parameter(parameter) ⇒ Object
- .statement(statement, el_statement) ⇒ Object
- .statement_group(declare_namespaces = false) ⇒ Object
- .subject(subject) ⇒ Object
- .term(term) ⇒ Object
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(data, options = {}) ⇒ XBELYielder
constructor
A new instance of XBELYielder.
Constructor Details
#initialize(data, options = {}) ⇒ XBELYielder
Returns a new instance of XBELYielder.
9 10 11 12 13 14 15 16 |
# File 'lib/bel/translator/plugins/xbel/xbel_yielder.rb', line 9 def initialize(data, = {}) @data = data @streaming = .fetch(:streaming, false) @annotation_reference_map = .fetch(:annotation_reference_map, nil) @namespace_reference_map = .fetch(:namespace_reference_map, nil) # default BEL version to 2 for default behavior @bel_version = 2 end |
Class Method Details
.annotation_definitions(list) ⇒ Object
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/bel/translator/plugins/xbel/xbel_yielder.rb', line 404 def self.annotation_definitions(list) el_ad = REXML::Element.new('bel:annotationDefinitionGroup') internal = [] external = [] list.each do |annotation| case annotation.type.to_sym when :url el = REXML::Element.new('bel:externalAnnotationDefinition') el.add_attribute 'bel:id', annotation.keyword external << el if @bel_version == 1 el.add_attribute 'bel:url', annotation.domain else url_el = REXML::Element.new('bel:url') url_el.text = annotation.domain rsrc_el = REXML::Element.new('bel:resource') rsrc_el << url_el el << rsrc_el end when :uri el = REXML::Element.new('bel:externalAnnotationDefinition') el.add_attribute 'bel:id', annotation.keyword external << el if @bel_version == 1 el.add_attribute 'bel:url', annotation.domain else uri_el = REXML::Element.new('bel:uri') uri_el.text = annotation.domain rsrc_el = REXML::Element.new('bel:resource') rsrc_el << uri_el el << rsrc_el end when :pattern el = REXML::Element.new('bel:internalAnnotationDefinition') el.add_attribute 'bel:id', annotation.keyword el.add_element(REXML::Element.new('bel:description')) el.add_element(REXML::Element.new('bel:usage')) el_pattern = REXML::Element.new('bel:patternAnnotation') el_pattern.text = annotation.domain.respond_to?(:source) ? annotation.domain.source : annotation.domain el.add_element(el_pattern) internal << el when :list el = REXML::Element.new('bel:internalAnnotationDefinition') el.add_attribute 'bel:id', annotation.keyword el.add_element(REXML::Element.new('bel:description')) el.add_element(REXML::Element.new('bel:usage')) el_list = REXML::Element.new('bel:listAnnotation') if annotation.domain.respond_to?(:each) annotation.domain.each do |value| el_list_value = REXML::Element.new('bel:listValue') el_list_value.text = value el_list.add_element(el_list_value) end else el_list_value = REXML::Element.new('bel:listValue') el_list_value.text = annotation.domain el_list.add_element(el_list_value) end el.add_element(el_list) internal << el else el = nil end end # first add internal annotation definitions internal.each do |internal_element| el_ad.add_element(internal_element) end # second add external annotation definitions external.each do |external_element| el_ad.add_element(external_element) end el_ad end |
.annotation_group(nanopub) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/bel/translator/plugins/xbel/xbel_yielder.rb', line 162 def self.annotation_group(nanopub) el_ag = REXML::Element.new('bel:annotationGroup') # XBEL citation if nanopub.citation && nanopub.citation.valid? el_ag.add_element(self.citation(nanopub.citation)) end # XBEL nanopub (::BEL::Nanopub::Support) if nanopub.support && nanopub.support.value if @bel_version == 1 xbel_evidence = REXML::Element.new('bel:evidence') xbel_evidence.text = nanopub.support.value el_ag.add_element(xbel_evidence) else xbel_support = REXML::Element.new('bel:support') xbel_support.text = nanopub.support.value el_ag.add_element(xbel_support) end end nanopub.experiment_context.each do |annotation| name, value = annotation.values_at(:name, :value) if value.respond_to?(:each) value.each do |v| el_anno = REXML::Element.new('bel:annotation') el_anno.text = v el_anno.add_attribute 'bel:refID', name.to_s el_ag.add_element(el_anno) end else el_anno = REXML::Element.new('bel:annotation') el_anno.text = value el_anno.add_attribute 'bel:refID', name.to_s el_ag.add_element(el_anno) end end = nanopub..keys - [:document_header, :bel_version] .each do |k| v = nanopub.[k] if v.respond_to?(:each) v.each do |value| el_anno = REXML::Element.new('bel:annotation') el_anno.text = value el_anno.add_attribute 'bel:refID', k.to_s el_ag.add_element(el_anno) end else el_anno = REXML::Element.new('bel:annotation') el_anno.text = v el_anno.add_attribute 'bel:refID', k.to_s el_ag.add_element(el_anno) end end el_ag end |
.citation(citation) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/bel/translator/plugins/xbel/xbel_yielder.rb', line 226 def self.citation(citation) el_citation = REXML::Element.new('bel:citation') if citation.type && !citation.type.to_s.empty? el_citation.add_attribute 'bel:type', citation.type end if citation.id && !citation.id.to_s.empty? el_reference = REXML::Element.new('bel:reference') el_reference.text = citation.id el_citation.add_element(el_reference) end if citation.name && !citation.name.to_s.empty? el_name = REXML::Element.new('bel:name') el_name.text = citation.name el_citation.add_element(el_name) end if citation.date && !citation.date.to_s.empty? el_date = REXML::Element.new('bel:date') el_date.text = citation.date el_citation.add_element(el_date) end if citation.comment && !citation.comment.to_s.empty? el_comment = REXML::Element.new('bel:comment') el_comment.text = citation.comment el_citation.add_element(el_comment) end if citation. && !citation..to_s.empty? = REXML::Element.new('bel:authorGroup') citation..each do || = REXML::Element.new('bel:author') .text = .add_element() end el_citation.add_element() end el_citation end |
.document ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/bel/translator/plugins/xbel/xbel_yielder.rb', line 270 def self.document el = REXML::Element.new('bel:document') if @bel_version == 1 el.add_namespace('bel', 'http://belframework.org/schema/1.0/xbel') el.add_namespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance') el.add_attribute('xsi:schemaLocation', 'http://belframework.org/schema/1.0/xbel http://resource.belframework.org/belframework/1.0/schema/xbel.xsd') else el.add_namespace('bel', 'http://resources.openbel.org/v2.0/lang/xml-schema/version_2.0/xbel') el.add_namespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance') el.add_attribute('xsi:schemaLocation', 'http://resources.openbel.org/v2.0/lang/xml-schema/version_2.0/xbel') end el end |
.header(hash) ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/bel/translator/plugins/xbel/xbel_yielder.rb', line 304 def self.header(hash) el_header = REXML::Element.new('bel:header') # normalize hash keys hash = Hash[ hash.map { |k, v| k = k.to_s.dup k.downcase! k.gsub!(/[-_ .]/, '') [k, v] } ] (%w(name description version copyright) & hash.keys).each do |field| el = REXML::Element.new("bel:#{field}") el.text = hash[field] el_header.add_element(el) end # add contactinfo with different element name (contactInfo) if hash.has_key?('contactinfo') el = REXML::Element.new('bel:contactInfo') el.text = hash['contactinfo'] el_header.add_element(el) end # add bel_version with different element name (belVersion) if hash.has_key?('belversion') el = REXML::Element.new('bel:belVersion') el.text = hash['belversion'] el_header.add_element(el) else # apply defaults el = REXML::Element.new('bel:belVersion') el.text = '2.0' el_header.add_element(el) end # handle grouping for authors and licenses if hash.has_key?('authors') = hash['authors'] el_group = REXML::Element.new('bel:authorGroup') [].flatten.each do || el = REXML::Element.new('bel:author') el.text = .to_s el_group.add_element(el) end el_header.add_element(el_group) end if hash.has_key?('licenses') licenses = hash['licenses'] el_group = REXML::Element.new('bel:licenseGroup') [licenses].flatten.each do |license| el = REXML::Element.new('bel:license') el.text = license.to_s el_group.add_element(el) end el_header.add_element(el_group) end el_header end |
.namespace_definitions(list) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/bel/translator/plugins/xbel/xbel_yielder.rb', line 369 def self.namespace_definitions(list) el_nd = REXML::Element.new('bel:namespaceGroup') list.each do |namespace| if namespace.url? el = REXML::Element.new('bel:namespace') el.add_attribute 'bel:prefix', namespace.keyword if @bel_version == 1 el.add_attribute 'bel:resourceLocation', namespace.uri else url_el = REXML::Element.new('bel:url') url_el.text = namespace.uri rsrc_el = REXML::Element.new('bel:resource') rsrc_el << url_el el << rsrc_el end el_nd.add_element(el) elsif namespace.uri? el = REXML::Element.new('bel:namespace') el.add_attribute 'bel:prefix', namespace.keyword if @bel_version == 1 el.add_attribute 'bel:resourceLocation', namespace.uri else uri_el = REXML::Element.new('bel:uri') uri_el.text = namespace.uri rsrc_el = REXML::Element.new('bel:resource') rsrc_el << uri_el el << rsrc_el end el_nd.add_element(el) end end el_nd end |
.nanopub(nanopub) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/bel/translator/plugins/xbel/xbel_yielder.rb', line 90 def self.nanopub(nanopub) statement = nanopub.bel_statement el_statement = REXML::Element.new('bel:statement') el_statement.add_element(self.annotation_group(nanopub)) self.statement(statement, el_statement) el_statement end |
.object(object) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/bel/translator/plugins/xbel/xbel_yielder.rb', line 125 def self.object(object) el_object = REXML::Element.new('bel:object') case object when ::BELParser::Expression::Model::Term el_object.add_element(self.term(object)) when ::BELParser::Expression::Model::Statement el_statement = REXML::Element.new('bel:statement') el_object.add_element(el_statement) self.statement(object, el_statement) end el_object end |
.parameter(parameter) ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/bel/translator/plugins/xbel/xbel_yielder.rb', line 154 def self.parameter(parameter) el_parameter = REXML::Element.new('bel:parameter') el_parameter.text = parameter.value keyword = parameter.namespace && parameter.namespace.keyword el_parameter.add_attribute 'bel:ns', keyword el_parameter end |
.statement(statement, el_statement) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/bel/translator/plugins/xbel/xbel_yielder.rb', line 100 def self.statement(statement, el_statement) if statement.relationship el_statement.add_attribute 'bel:relationship', statement.relationship.to_s(:long) end if statement.subject el_subject = self.subject(statement.subject) el_statement.add_element(el_subject) end if statement.object el_object = self.object(statement.object) el_statement.add_element(el_object) end el_statement end |
.statement_group(declare_namespaces = false) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/bel/translator/plugins/xbel/xbel_yielder.rb', line 286 def self.statement_group(declare_namespaces = false) el = REXML::Element.new('bel:statementGroup') if declare_namespaces if @bel_version == 1 el.add_namespace('bel', 'http://belframework.org/schema/1.0/xbel') el.add_namespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance') el.add_attribute('xsi:schemaLocation', 'http://belframework.org/schema/1.0/xbel http://resource.belframework.org/belframework/1.0/schema/xbel.xsd') else el.add_namespace('bel', 'http://resources.openbel.org/v2.0/lang/xml-schema/version_2.0/xbel') el.add_namespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance') el.add_attribute('xsi:schemaLocation', 'http://resources.openbel.org/v2.0/lang/xml-schema/version_2.0/xbel') end end el end |
.subject(subject) ⇒ Object
118 119 120 121 122 123 |
# File 'lib/bel/translator/plugins/xbel/xbel_yielder.rb', line 118 def self.subject(subject) el_subject = REXML::Element.new('bel:subject') el_subject.add_element(self.term(subject)) el_subject end |
.term(term) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/bel/translator/plugins/xbel/xbel_yielder.rb', line 139 def self.term(term) el_term = REXML::Element.new('bel:term') el_term.add_attribute 'bel:function', term.function.to_s(:long) term.arguments.each do |arg| case arg when ::BELParser::Expression::Model::Term el_term.add_element(self.term(arg)) when ::BELParser::Expression::Model::Parameter el_term.add_element(self.parameter(arg)) end end el_term end |
Instance Method Details
#each ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/bel/translator/plugins/xbel/xbel_yielder.rb', line 18 def each if block_given? combiner = if @streaming BEL::Nanopub::StreamingNanopubCombiner.new(@data) elsif @annotation_reference_map && @namespace_reference_map BEL::Nanopub::MapReferencesCombiner.new( @data, BEL::Nanopub::HashMapReferences.new( @annotation_reference_map, @namespace_reference_map ) ) else BEL::Nanopub::BufferingNanopubCombiner.new(@data) end header_flag = true # yield <document> el_document = XBELYielder.document yield start_element_string(el_document) el_statement_group = nil nanopub_count = 0 combiner.each { |nanopub| # is BEL version 1.0? if nanopub..bel_version == '1.0' @bel_version = 1 end if header_flag # document header el_statement_group = XBELYielder.statement_group yield element_string( XBELYielder.header(nanopub..document_header) ) yield element_string( XBELYielder.namespace_definitions(combiner.namespace_references) ) yield element_string( XBELYielder.annotation_definitions(combiner.annotation_references) ) yield start_element_string(el_statement_group) header_flag = false end yield element_string(XBELYielder.nanopub(nanopub)) nanopub_count += 1 } if nanopub_count > 0 yield end_element_string(el_statement_group) else # empty head sections; required for XBEL schema empty_header = Hash[%w(name description version).map { |element| [element, ""] }] yield element_string(XBELYielder.header(empty_header)) yield element_string(XBELYielder.namespace_definitions([])) yield element_string(XBELYielder.annotation_definitions([])) yield element_string(XBELYielder.statement_group) end yield end_element_string(el_document) else to_enum(:each) end end |