Class: OpenTox::Serializer::Owl
- Inherits:
-
Object
- Object
- OpenTox::Serializer::Owl
- Defined in:
- lib/serializer.rb
Overview
OWL-DL Serializer, modelled according to to n2.talis.com/wiki/RDF_JSON_Specification
Instance Attribute Summary collapse
-
#object ⇒ Object
Returns the value of attribute object.
Instance Method Summary collapse
-
#add_algorithm(uri, metadata) ⇒ Object
Add a algorithm.
-
#add_compound(uri) ⇒ Object
Add a compound.
-
#add_data_entry(compound, feature, value) ⇒ Object
Add a data entry.
-
#add_dataset(dataset) ⇒ Object
Add a dataset.
-
#add_feature(uri, metadata) ⇒ Object
Add a feature.
-
#add_metadata(uri, metadata) ⇒ Object
Add metadata.
-
#add_model(uri, metadata) ⇒ Object
Add a model.
-
#add_resource(uri, resource_class, content) ⇒ Object
Add a resource defined by resource_class and content (see documentation of add_content for example).
-
#add_task(uri, metadata) ⇒ Object
Add a task.
- #add_uri(uri, type) ⇒ Object
-
#initialize ⇒ Owl
constructor
A new instance of Owl.
-
#to_json ⇒ text/plain
Convert to JSON as specified in n2.talis.com/wiki/RDF_JSON_Specification (Ambit services use a different JSON representation).
-
#to_ntriples ⇒ text/plain
Convert to N-Triples.
-
#to_rdfxml ⇒ text/plain
Convert to RDF/XML.
Constructor Details
#initialize ⇒ Owl
Returns a new instance of Owl.
15 16 17 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/serializer.rb', line 15 def initialize @object = { # this should come from opentox.owl OT.Compound => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.Feature => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.Model => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.NominalFeature => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.NumericFeature => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.StringFeature => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.ModelPrediction => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.Dataset => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.DataEntry => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.FeatureValue => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.Algorithm => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.Parameter => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.Task => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OTA.PatternMiningSupervised => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OTA.ClassificationLazySingleTarget => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OTA.RegressionLazySingleTarget => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , #classes for validation OT.Validation => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.ClassificationStatistics => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.ConfusionMatrix => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.ConfusionMatrixCell => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.ClassValueStatistics => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.RegressionStatistics => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.Crossvalidation => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.CrossvalidationInfo => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.ErrorReport => { RDF["type"] => [{ "type" => "uri", "value" => OWL['Class'] }] } , OT.compound => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.feature => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.dataEntry => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.values => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.algorithm => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.parameters => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.featureDataset => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.dependentVariables => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.predictedVariables => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.paramValue => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , #object props for validation# OT.model => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.trainingDataset => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.predictionFeature => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.predictionDataset => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.crossvalidation => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.testTargetDataset => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.testDataset => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.classificationStatistics => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.confusionMatrix => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.confusionMatrixCell => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.classValueStatistics => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.regressionStatistics => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.validation => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.crossvalidationInfo => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.dataset => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , OT.hasSource => { RDF["type"] => [{ "type" => "uri", "value" => OWL.ObjectProperty }] } , DC.title => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , DC.identifier => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , DC.contributor => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , DC.creator => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , DC.description => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , DC.date => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , #OT.isA => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.Warnings => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , XSD.anyURI => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.hasStatus => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.resultURI => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.percentageCompleted => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.acceptValue => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , # annotation props for validation OT.numUnpredicted => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.crossvalidationFold => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.numInstances => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.numWithoutClass => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.percentWithoutClass => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.percentUnpredicted => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.confusionMatrixActual => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.confusionMatrixPredicted => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.confusionMatrixValue => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.numIncorrect => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.percentCorrect => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.numCorrect => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.accuracy => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.trueNegativeRate => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.truePositiveRate => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.falseNegativeRate => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.falsePositiveRate => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.numTrueNegatives => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.numTruePositives => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.numFalseNegatives => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.numFalsePositives => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.classValue => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.precision => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.areaUnderRoc => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.weightedAreaUnderRoc => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.weightedAccuracy => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.fMeasure => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.percentIncorrect => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.validationType => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.realRuntime => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.sampleCorrelationCoefficient => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.targetVarianceActual => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.targetVariancePredicted => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.meanAbsoluteError => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.sumSquaredError => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.rootMeanSquaredError => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.rSquare => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.stratified => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.numFolds => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.randomSeed => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.reportType => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT. => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.statusCode => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.actor => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , OT.errorCode => { RDF["type"] => [{ "type" => "uri", "value" => OWL.AnnotationProperty }] } , #OT.hasSource => { RDF["type"] => [{ "type" => "uri", "value" => OWL.DatatypeProperty }] } , OT.value => { RDF["type"] => [{ "type" => "uri", "value" => OWL.DatatypeProperty }] } , OT.paramScope => { RDF["type"] => [{ "type" => "uri", "value" => OWL.DatatypeProperty }] } , #OT.paramValue => { RDF["type"] => [{ "type" => "uri", "value" => OWL.DatatypeProperty }] } , } @data_entries = {} @values_id = 0 @parameter_id = 0 @classes = Set.new @object_properties = Set.new @annotation_properties = Set.new @datatype_properties = Set.new @objects = Set.new end |
Instance Attribute Details
#object ⇒ Object
Returns the value of attribute object.
13 14 15 |
# File 'lib/serializer.rb', line 13 def object @object end |
Instance Method Details
#add_algorithm(uri, metadata) ⇒ Object
Add a algorithm
185 186 187 188 |
# File 'lib/serializer.rb', line 185 def add_algorithm(uri,) @object[uri] = { RDF["type"] => [{ "type" => "uri", "value" => OT.Algorithm }] } uri, end |
#add_compound(uri) ⇒ Object
Add a compound
157 158 159 |
# File 'lib/serializer.rb', line 157 def add_compound(uri) @object[uri] = { RDF["type"] => [{ "type" => "uri", "value" => OT.Compound }] } end |
#add_data_entry(compound, feature, value) ⇒ Object
Add a data entry
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 |
# File 'lib/serializer.rb', line 310 def add_data_entry(compound,feature,value) add_compound(compound) unless @object[compound] add_feature(feature,{}) unless @object[feature] unless data_entry = @data_entries[compound] data_entry = "_:dataentry#{@data_entries.size}" @data_entries[compound] = data_entry @object[@dataset][OT.dataEntry] = [] unless @object[@dataset][OT.dataEntry] @object[@dataset][OT.dataEntry] << {"type" => "bnode", "value" => data_entry} @object[data_entry] = { RDF["type"] => [{ "type" => "uri", "value" => OT.DataEntry }], OT.compound => [{ "type" => "uri", "value" => compound }], OT.values => [], } end values = "_:values#{@values_id}" @values_id += 1 @object[data_entry][OT.values] << {"type" => "bnode", "value" => values} case type(value) when "uri" v = [{ "type" => "uri", "value" => value}] when "literal" v = [{ "type" => "literal", "value" => value, "datatype" => datatype(value) }] else raise "Illegal type #{type(value)} for #{value}." end @object[values] = { RDF["type"] => [{ "type" => "uri", "value" => OT.FeatureValue }], OT.feature => [{ "type" => "uri", "value" => feature }], OT.value => v } @object[feature][RDF["type"]] << { "type" => "uri", "value" => featuretype(value) } #@object[feature][RDF["type"]] = { "type" => "uri", "value" => featuretype(value) } end |
#add_dataset(dataset) ⇒ Object
Add a dataset
170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/serializer.rb', line 170 def add_dataset(dataset) @dataset = dataset.uri @object[dataset.uri] = { RDF["type"] => [{ "type" => "uri", "value" => OT.Dataset }] } dataset.uri, dataset. dataset.compounds.each { |compound| add_compound compound } dataset.features.each { |feature,| add_feature feature, } dataset.data_entries.each do |compound,entry| entry.each do |feature,values| values.each { |value| add_data_entry compound,feature,value } end end end |
#add_feature(uri, metadata) ⇒ Object
Add a feature
163 164 165 166 |
# File 'lib/serializer.rb', line 163 def add_feature(uri,) @object[uri] = { RDF["type"] => [{ "type" => "uri", "value" => OT.Feature }] } uri, end |
#add_metadata(uri, metadata) ⇒ Object
Add metadata
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 |
# File 'lib/serializer.rb', line 276 def (uri,) id = 0 if !.nil? .each do |u,v| #if v.is_a? Array and (u == OT.parameters or u == RDF.type) if v.is_a? Array and u == OT.parameters#or u == RDF.type) @object[uri][u] = [] unless @object[uri][u] v.each do |value| id+=1 genid = "_:genid#{id}" @object[uri][u] << {"type" => "bnode", "value" => genid} @object[genid] = { RDF["type"] => [{ "type" => "uri", "value" => OT.Parameter}] } value.each do |name,entry| @object[genid][name] = [{"type" => type(entry), "value" => entry }] end end elsif v.is_a? Array #and u == RDF.type @object[uri] = {} unless @object[uri] v.each do |value| @object[uri][u] = [] unless @object[uri][u] @object[uri][u] << {"type" => type(value), "value" => value } end elsif v.is_a? String @object[uri] = {} unless @object[uri] @object[uri][u] = [{"type" => type(v), "value" => v }] end end end end |
#add_model(uri, metadata) ⇒ Object
Add a model
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/serializer.rb', line 192 def add_model(uri,) @object[uri] = { RDF["type"] => [{ "type" => "uri", "value" => OT.Model }] } uri, @object[[OT.featureDataset]] = { RDF["type"] => [{ "type" => "uri", "value" => OT.Dataset }] } @object[[OT.trainingDataset]] = { RDF["type"] => [{ "type" => "uri", "value" => OT.Dataset }] } @object[[OT.dependentVariables]] = { RDF["type"] => [{ "type" => "uri", "value" => OT.Feature }] } [OT.predictedVariables].each{|feature| @object[feature] = { RDF["type"] => [{ "type" => "uri", "value" => OT.Feature }]}} #unless metadata[OT.predictedVariables].nil? # TODO: add algorithms from parameters @object["http://ot-dev.in-silico.ch/algorithm/fminer/bbrc"] = { RDF["type"] => [{ "type" => "uri", "value" => OT.Algorithm }] } @object["http://ot-dev.in-silico.ch/algorithm/fminer/last"] = { RDF["type"] => [{ "type" => "uri", "value" => OT.Algorithm }] } @object["http://ot-dev.in-silico.ch/algorithm/lazar"] = { RDF["type"] => [{ "type" => "uri", "value" => OT.Algorithm }] } end |
#add_resource(uri, resource_class, content) ⇒ Object
Add a resource defined by resource_class and content (see documentation of add_content for example)
217 218 219 220 221 |
# File 'lib/serializer.rb', line 217 def add_resource(uri, resource_class, content) @object[uri] = { RDF["type"] => [{ "type" => "uri", "value" => resource_class }] } @@content_id = 1 add_content uri, content end |
#add_task(uri, metadata) ⇒ Object
Add a task
207 208 209 210 |
# File 'lib/serializer.rb', line 207 def add_task(uri,) @object[uri] = { RDF["type"] => [{ "type" => "uri", "value" => OT.Task }] } uri, end |
#add_uri(uri, type) ⇒ Object
223 224 225 |
# File 'lib/serializer.rb', line 223 def add_uri(uri,type) @object[uri] = { RDF["type"] => [{ "type" => "uri", "value" => type }] } end |
#to_json ⇒ text/plain
Convert to JSON as specified in n2.talis.com/wiki/RDF_JSON_Specification (Ambit services use a different JSON representation)
388 389 390 391 |
# File 'lib/serializer.rb', line 388 def to_json #rdf_types Yajl::Encoder.encode(@object) end |
#to_ntriples ⇒ text/plain
Convert to N-Triples
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/serializer.rb', line 348 def to_ntriples @triples = Set.new @object.each do |s,entry| s = url(s) if type(s) == "uri" entry.each do |p,objects| p = url(p) objects.each do |o| case o["type"] when "uri" o = url(o["value"]) when "literal" o = literal(o["value"],datatype(o["value"])) when "bnode" o = o["value"] end @triples << [s,p,o] end end end @triples.sort.collect{ |s| s.join(' ').concat(" .") }.join("\n")+"\n" end |
#to_rdfxml ⇒ text/plain
Convert to RDF/XML
373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/serializer.rb', line 373 def to_rdfxml tmpf = Tempfile.open("owl-serializer") tmpf.write(self.to_ntriples) tmpf.flush @path = tmpf.path # TODO: add base uri for ist services res=`rapper -i ntriples -f 'xmlns:ot="#{OT.uri}"' -f 'xmlns:ota="#{OTA.uri}"' -f 'xmlns:dc="#{DC.uri}"' -f 'xmlns:rdf="#{RDF.uri}"' -f 'xmlns:owl="#{OWL.uri}"' -o rdfxml #{@path} 2>/dev/null` tmpf.close tmpf.delete res end |