module BioTable::RDF

Public Class Methods

header(row) click to toggle source

Write a table header as Turtle RDF.

If we have a column name ‘AXB1’ the following gets written:

[:AXB1 rdf:label “AXB1”; a :colname; :index 3 ].

This method returns a list of these.

# File lib/bio-table/rdf.rb, line 20
def RDF::header(row)
  list = []
  if row - row.uniq != []
    $stderr.print row - row.uniq
    raise "RDF expects unique column names!" 
  end
  row.each_with_index do | field,i |
    s = ":#{make_identifier(field)} rdf:label \"#{field}\" ; a :colname; :index #{i} ."
    list << s
  end
  list
end
namespaces() click to toggle source
# File lib/bio-table/rdf.rb, line 6
    def RDF::namespaces
      """
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix : <http://biobeat.org/rdf/biotable/ns#>  .
      """
    end
row(row, header, use_blank_nodes = false) click to toggle source

Write a table row as Turtle RDF

If we have a row, each row element gets written with the header colname as an id, e.g.

[:rs13475701 rdf:label “rs13475701”; a :rowname; :Chromosome 1 ; :Pos 0 ; :AXB1 “AA”].

The method returns a String.

# File lib/bio-table/rdf.rb, line 41
def RDF::row(row, header, use_blank_nodes = false)
  list = []
  rowname = make_identifier(row[0])
  list << ":#{rowname}"+(use_blank_nodes ? " :row [ " : " ") + "rdf:label \"#{row[0]}\" ; a :rowname" 
  row.each_with_index do | field,i |
    s = ":#{make_identifier(header[i])} "
    if BioTable::Filter.valid_number?(field)
      s += field.to_s
    else
      s += "\"#{field}\""
    end
    list << s
  end
  list.join(" ; ")+(use_blank_nodes ? " ] ." : " .")
end

Private Class Methods

make_identifier(s) click to toggle source

An identifier is used for the subject and predicate in RDF. This is a case-sensitive (shortened) URI. You can change default behaviour for identifiers using the options –transform-ids (i.e. in the input side, rather than the output side)

# File lib/bio-table/rdf.rb, line 95
def RDF::make_identifier(s)
  id = s.gsub(/[^[:print:]]/, '').gsub(/[#)(,]/,"").gsub(/[%]/,"perc").gsub(/(\s|\.|\$|\/|\\)+/,"_")
  if id != s 
    logger = Bio::Log::LoggerPlus['bio-table']
    logger.warn "Changed identifier <#{s}> to <#{id}>"
  end
  valid_id = if id =~ /^\d/
               'r' + id
             else
               id
             end
  valid_id
end