-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_doc.rb
146 lines (119 loc) · 3.14 KB
/
generate_doc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
#!/usr/bin/env ruby
require 'rdf'
require 'rdf/rdfxml'
require 'json'
require 'json/ld'
require 'net/http'
require 'uri'
require 'erb'
require 'pry'
include RDF
### Constants ###
# Ontology remotes
PSDO_URI = "https://raw.githubusercontent.com/Display-Lab/psdo/master/psdo.owl"
# List of causal pathways urls
PSDO_RELATIVE_DOCS_DIR = File.join(File.dirname(__FILE__), 'docs' )
PSDO_FRAGMENT_TEMPLATE = <<~HEREDOC
<h2 id="<%= name %>">
<a href="#<%= name %>"><%= name %></a>
<%= definition %>
</h2>
HEREDOC
PSDO_HTML_TEMPLATE = <<~WRAPPER
<!DOCTYPE html>
<html>
<body>
<h1> Ontology </h1>
<%= content %>
</body>
</html>
WRAPPER
### Functions ###
# Get the ontology from the remote
def fetch_ontology(uri)
p_uri = URI.parse(uri)
response = Net::HTTP.get_response(p_uri)
if(response.code != "200")
puts "Unable to fetch #{uri}"
abort
end
response.body
end
# Return local file name
def retrieve_ontology(uri)
case uri
when PSDO_URI
file_path = File.join(Dir.tmpdir, "psdo.owl")
else
puts "bad uri"
abort
end
if !File.exists?(file_path)
ontology = fetch_ontology(uri)
File.open(file_path, "w") do |f|
f.write(ontology)
end
end
RDF::Graph.load(file_path, format: :rdfxml)
end
# Save causal pathways to disk
def retrieve_psdo_pathways(uri)
file_path = File.join(Dir.tmpdir, "psdo.owl")
if !File.exists?(file_path)
cp = fetch_causal_pathways(uri)
File.open(file_path, "w") do |f|
f.write(cp)
end
end
end
# Create a hash of IRI => labels from the graph of an ontology.
def extract_labels( graph )
rdfschema = RDF::Vocabulary.new("http://www.w3.org/2000/01/rdf-schema#")
label_query = RDF::Query.new({
term: { rdfschema.label => :label }
})
solutions = graph.query label_query
zipped = solutions.map{|s| [s.term.value.split("/")[-1], s.label.value]}
Hash[zipped]
end
# Substutite values in the hash that match the label mapping.
def substitute_labels(cp, labels)
cp.each do |k, v|
if v.is_a?(String) && labels.keys.include?(v)
v.replace labels[v]
elsif v.is_a?(Hash)
substitute_labels v, labels
elsif v.is_a?(Array)
v.flatten.each { |x| substitute_labels(x, labels) if x.is_a?(Hash) }
end
end
cp
end
def generate_valid_html(psdo)
content = psdo
ERB.new(PSDO_HTML_TEMPLATE).result(binding)
end
# Given a causal pathway, return html using baked in html template
def generate_psdo_html(psdo)
name = psdo[0]
definition = psdo[1]
ERB.new(PSDO_FRAGMENT_TEMPLATE).result(binding)
end
### SCRIPT START ###
puts "Generating."
# Load Ontologies
# Grab from hard coded local location for now.
# Use fetch_ontology to grap from remote
psdo_owl = retrieve_ontology PSDO_URI
psdo_labels = extract_labels psdo_owl
puts "Looking in #{Dir.tmpdir} \n"
# Create html content
psdo_fragments = psdo_labels.map{|psdo| generate_psdo_html psdo}
psdo_joined_fragments = psdo_fragments.join("\n")
# wrap fragments in html tag
psdo_html = generate_valid_html psdo_joined_fragments
# Create list of file names
psdo_path = File.join(PSDO_RELATIVE_DOCS_DIR, "psdo.html")
# Write html to disk
File.open(psdo_path, 'w'){|file| file << psdo_html}
puts "Done."