forked from ontoportal/ontologies_api
-
Notifications
You must be signed in to change notification settings - Fork 10
/
mappings_controller.rb
204 lines (191 loc) · 7.06 KB
/
mappings_controller.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
class MappingsController < ApplicationController
# Get mappings for a class
get '/ontologies/:ontology/classes/:cls/mappings' do
ontology = ontology_from_acronym(@params[:ontology])
submission = ontology.latest_submission
cls_id = @params[:cls]
cls = LinkedData::Models::Class.find(RDF::URI.new(cls_id)).in(submission).first
if cls.nil?
error(404, "Class with id `#{cls_id}` not found in ontology")
end
mappings = LinkedData::Mappings.mappings_ontology(submission,
0,0,
cls.id)
populate_mapping_classes(mappings.to_a)
reply mappings
end
# Get mappings for an ontology
get '/ontologies/:ontology/mappings' do
ontology = ontology_from_acronym(@params[:ontology])
if ontology.nil?
error(404, "Ontology not found")
end
page, size = page_params
submission = ontology.latest_submission
if submission.nil?
error(404, "Submission not found for ontology " + ontology.acronym)
end
mappings = LinkedData::Mappings.mappings_ontology(submission,
page,size,
nil)
populate_mapping_classes(mappings)
reply mappings
end
namespace "/mappings" do
# Display all mappings
get do
ontologies = ontology_objects_from_params
if ontologies.length != 2
error(400,
"/mappings/ endpoint only supports filtering " +
"on two ontologies using `?ontologies=ONT1,ONT2`")
end
page, size = page_params
ont1 = ontologies.first
ont2 = ontologies[1]
sub1, sub2 = ont1.latest_submission, ont2.latest_submission
if sub1.nil?
error(404, "Submission not found for ontology " + ontologies[0].id.to_s)
end
if sub2.nil?
error(404, "Submission not found for ontology " + ontologies[1].id.to_s)
end
mappings = LinkedData::Mappings.mappings_ontologies(sub1,sub2,
page,size)
populate_mapping_classes(mappings)
reply mappings
end
get "/recent" do
check_last_modified_collection(LinkedData::Models::RestBackupMapping)
size = params[:size] || 5
size = Integer(size)
if size > 50
error 422, "Recent mappings only processes calls under 50"
else
mappings = LinkedData::Mappings.recent_rest_mappings(size + 15)
populate_mapping_classes(mappings)
reply mappings[0..size-1]
end
end
# Display a single mapping - only rest
get '/:mapping' do
mapping_id = nil
if params[:mapping] and params[:mapping].start_with?("http")
mapping_id = params[:mapping]
mapping_id = mapping_id.gsub("/mappings/","/rest_backup_mappings/")
mapping_id = RDF::URI.new(params[:mapping])
else
mapping_id =
"http://data.bioontology.org/rest_backup_mappings/#{mapping_id}"
mapping_id = RDF::URI.new(mapping_id)
end
mapping = LinkedData::Mappings.get_rest_mapping(mapping_id)
if mapping
reply populate_mapping_classes([mapping].first)
else
error(404, "Mapping with id `#{mapping_id.to_s}` not found")
end
end
# Create a new mapping
post do
error(400, "Input does not contain classes") if !params[:classes]
if params[:classes].length > 2
error(400, "Input does not contain at least 2 terms")
end
error(400, "Input does not contain mapping relation") if !params[:relation]
error(400, "Input does not contain user creator ID") if !params[:creator]
classes = []
params[:classes].each do |class_id,ontology_id|
o = ontology_id
o = o.start_with?("http://") ? ontology_id :
ontology_uri_from_acronym(ontology_id)
o = LinkedData::Models::Ontology.find(RDF::URI.new(o))
.include(submissions:
[:submissionId, :submissionStatus]).first
if o.nil?
error(400, "Ontology with ID `#{ontology_id}` not found")
end
submission = o.latest_submission
if submission.nil?
error(400,
"Ontology with id #{ontology_id} does not have parsed valid submission")
end
submission.bring(ontology: [:acronym])
c = LinkedData::Models::Class.find(RDF::URI.new(class_id))
.in(submission)
.first
if c.nil?
error(400, "Class ID `#{id}` not found in `#{submission.id.to_s}`")
end
classes << c
end
user_id = params[:creator].start_with?("http://") ?
params[:creator].split("/")[-1] : params[:creator]
user_creator = LinkedData::Models::User.find(user_id)
.include(:username).first
if user_creator.nil?
error(400, "User with id `#{params[:creator]}` not found")
end
process = LinkedData::Models::MappingProcess.new(
:creator => user_creator, :name => "REST Mapping")
process.relation = RDF::URI.new(params[:relation])
process.date = DateTime.now
process_fields = [:source,:source_name, :comment]
process_fields.each do |att|
process.send("#{att}=",params[att]) if params[att]
end
process.save
mapping = LinkedData::Mappings.create_rest_mapping(classes,process)
reply(201, mapping)
end
# Delete a mapping
delete '/:mapping' do
mapping_id = RDF::URI.new(replace_url_prefix(params[:mapping]))
mapping = LinkedData::Mappings.delete_rest_mapping(mapping_id)
if mapping.nil?
error(404, "Mapping with id `#{mapping_id.to_s}` not found")
else
halt 204
end
end
end
namespace "/mappings/statistics" do
get '/ontologies' do
expires 86400, :public
persistent_counts = {}
f = Goo::Filter.new(:pair_count) == false
LinkedData::Models::MappingCount.where.filter(f)
.include(:ontologies,:count)
.all
.each do |m|
persistent_counts[m.ontologies.first] = m.count
end
reply persistent_counts
end
# Statistics for an ontology
get '/ontologies/:ontology' do
expires 86400, :public
ontology = ontology_from_acronym(@params[:ontology])
if ontology.nil?
error(404, "Ontology #{@params[:ontology]} not found")
end
sub = ontology.latest_submission
if sub.nil?
error(404, "Ontology #{@params[:ontology]} does not have a submission")
end
persistent_counts = {}
LinkedData::Models::MappingCount.where(pair_count: true)
.and(ontologies: ontology.acronym)
.include(:ontologies,:count)
.all
.each do |m|
other = m.ontologies.first
if other == ontology.acronym
other = m.ontologies[1]
end
persistent_counts[other] = m.count
end
reply persistent_counts
end
end
end