Class: BEL::Nanopub::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/bel/translator/plugins/rdf/monkey_patch.rb

Instance Method Summary collapse

Instance Method Details

#to_rdf(graph_name = nil, remap = nil) ⇒ Object



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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/bel/translator/plugins/rdf/monkey_patch.rb', line 227

def to_rdf(graph_name = nil, remap = nil)
  uri        = to_uri
  statements = []

  case
  when subject_only?
    (sub_uri, sub_statements) = @subject.to_rdf(graph_name, remap)
    statements << ::RDF::Statement(uri, BELRDF::BELV.hasSubject, sub_uri, :graph_name => graph_name)
    statements.concat(sub_statements)
  when simple?
    (sub_uri, sub_statements) = @subject.to_rdf(graph_name, remap)
    statements.concat(sub_statements)

    (obj_uri, obj_statements) = @object.to_rdf(graph_name, remap)
    statements.concat(obj_statements)

    rel = BELRDF::RELATIONSHIP_TYPE[@relationship.to_s]
    statements << ::RDF::Statement(uri, BELRDF::BELV.hasSubject, sub_uri,  :graph_name => graph_name)
    statements << ::RDF::Statement(uri, BELRDF::BELV.hasObject, obj_uri,   :graph_name => graph_name)
    statements << ::RDF::Statement(uri, BELRDF::BELV.hasRelationship, rel, :graph_name => graph_name)
  when nested?
    (sub_uri, sub_statements) = @subject.to_rdf(graph_name, remap)
    (nsub_uri, nsub_statements) = @object.subject.to_rdf(graph_name, remap)
    (nobj_uri, nobj_statements) = @object.object.to_rdf(graph_name, remap)
    statements.concat(sub_statements)
    statements.concat(nsub_statements)
    statements.concat(nobj_statements)
    rel = BELRDF::RELATIONSHIP_TYPE[@relationship.to_s]
    nrel = BELRDF::RELATIONSHIP_TYPE[@object.relationship.to_s]
    nuri = BELRDF::BELR["#{strip_prefix(nsub_uri)}_#{nrel}_#{strip_prefix(nobj_uri)}"]

    # inner
    statements << ::RDF::Statement.new(nuri, BELRDF::BELV.hasSubject, nsub_uri,  :graph_name => graph_name)
    statements << ::RDF::Statement.new(nuri, BELRDF::BELV.hasObject, nobj_uri,   :graph_name => graph_name)
    statements << ::RDF::Statement.new(nuri, BELRDF::BELV.hasRelationship, nrel, :graph_name => graph_name)

    # outer
    statements << ::RDF::Statement.new(uri, BELRDF::BELV.hasSubject, sub_uri,  :graph_name => graph_name)
    statements << ::RDF::Statement.new(uri, BELRDF::BELV.hasObject, nuri,      :graph_name => graph_name)
    statements << ::RDF::Statement.new(uri, BELRDF::BELV.hasRelationship, rel, :graph_name => graph_name)
  end

  # common statement triples
  statements << ::RDF::Statement.new(uri, BELRDF::RDF.type, BELRDF::BELV.Statement,     :graph_name => graph_name)
  statements << ::RDF::Statement.new(uri, BELRDF::RDFS.label, to_s.force_encoding('UTF-8'),  :graph_name => graph_name)

  # nanopub
  nanopub    = BELRDF::BELE[BELRDF.generate_uuid]
  statements << ::RDF::Statement.new(nanopub, BELRDF::RDF.type, BELRDF::BELV.Nanopub, :graph_name => graph_name)
  statements << ::RDF::Statement.new(uri, BELRDF::BELV.hasNanopub, nanopub,             :graph_name => graph_name)
  statements << ::RDF::Statement.new(nanopub, BELRDF::BELV.hasStatement, uri,            :graph_name => graph_name)

  # citation
  citation = @annotations.delete('Citation')
  if citation
    value = citation.value.map{|x| x.gsub('"', '')}
    if citation and value[0] == 'PubMed'
      pid = value[2]
      statements << ::RDF::Statement.new(nanopub, BELRDF::BELV.hasCitation, BELRDF::PUBMED[pid], :graph_name => graph_name)
    end
  end

  # nanopub
  support = @annotations.delete('Support')
  if support
    value = support.value.gsub('"', '').force_encoding('UTF-8')
    statements << ::RDF::Statement.new(nanopub, BELRDF::BELV.hasSupport, value, :graph_name => graph_name)
  end

  # annotations
  @annotations.each do |_, anno|
    name = anno.name.gsub('"', '')

    # update annotations resource
    anno_rdf_uri = nil
    if remap && remap['annotations']
      ro_ind = remap['annotations'].index { |mapping|
        mapping['remap'] && mapping['remap'].is_a?(Hash) &&
        mapping['remap']['from'] && mapping['remap']['from'].is_a?(Hash) &&
        mapping['remap']['from']['keyword'] == name && (mapping['remap']['from']['type'] == 'url' ||
        mapping['remap']['from']['type'] == 'list' &&
        mapping['remap']['from']['domain'] && mapping['remap']['from']['domain'].is_a?(Array) &&
        mapping['remap']['from']['domain'].include?(anno.value))
      }
      if ro_ind
        anno_rdf_uri = remap['annotations'][ro_ind]['remap']['to']['rdf_uri']
        anno_rdf_uri << '/' unless anno_rdf_uri.end_with?('/')
      end
    end

    if anno_rdf_uri or BELRDF::const_defined? name
      annotation_scheme = anno_rdf_uri ? anno_rdf_uri : BELRDF::const_get(name)
      [anno.value].flatten.map{|x| x.gsub('"', '')}.each do |val|
        value_uri = BELRDF::RDF::URI(URI.encode(annotation_scheme + val.to_s))
        statements << ::RDF::Statement.new(nanopub, BELRDF::BELV.hasAnnotation, value_uri, :graph_name => graph_name)
      end
    end
  end

  [uri, statements]
end

#to_uriObject



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
225
# File 'lib/bel/translator/plugins/rdf/monkey_patch.rb', line 191

def to_uri
  case
  when simple?
    sub_id = @subject.to_s.squeeze(')').gsub(/[")\[\]]/, '').gsub(/[(:, ]/, '_')
    obj_id = @object.to_s.squeeze(')').gsub(/[")\[\]]/, '').gsub(/[(:, ]/, '_')
    rel = BELRDF::RELATIONSHIP_TYPE[@relationship.to_s]
    if rel
      rel = rel.path.split('/')[-1]
    else
      rel = @relationship.to_s
    end
    BELRDF::BELR[URI::encode("#{sub_id}_#{rel}_#{obj_id}")]
  when nested?
    sub_id = @subject.to_s.squeeze(')').gsub(/[")\[\]]/, '').gsub(/[(:, ]/, '_')
    nsub_id = @object.subject.to_s.squeeze(')').gsub(/[")\[\]]/, '').gsub(/[(:, ]/, '_')
    nobj_id = @object.object.to_s.squeeze(')').gsub(/[")\[\]]/, '').gsub(/[(:, ]/, '_')
    rel = BELRDF::RELATIONSHIP_TYPE[@relationship.to_s]
    if rel
      rel = rel.path.split('/')[-1]
    else
      rel = @relationship.to_s
    end
    nrel = BELRDF::RELATIONSHIP_TYPE[@object.relationship.to_s]
    if nrel
      nrel = nrel.path.split('/')[-1]
    else
      nrel = @object.relationship.to_s
    end
    BELRDF::BELR[URI::encode("#{sub_id}_#{rel}_#{nsub_id}_#{nrel}_#{nobj_id}")]
  else
    # Interpret as subject only BEL statement.
    tid = @subject.to_s.squeeze(')').gsub(/[")\[\]]/, '').gsub(/[(:, ]/, '_')
    BELRDF::BELR[URI::encode(tid)]
  end
end