Class: OpenBEL::Nanopub::Nanopub

Inherits:
Object
  • Object
show all
Includes:
Mongo, API
Defined in:
lib/openbel/api/nanopub/mongo.rb

Instance Method Summary collapse

Methods included from API

#update_nanopub_by_query

Constructor Details

#initialize(options = {}) ⇒ Nanopub

Returns a new instance of Nanopub.



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
# File 'lib/openbel/api/nanopub/mongo.rb', line 15

def initialize(options = {})
  host = options[:host]
  port = options[:port]
  db   = options[:database]
  @db  = MongoClient.new(
    host,
    port,
    :op_timeout => nil,
    :pool_size  => 30
  ).db(db)

  # Authenticate user if provided.
  username = options[:username]
  password = options[:password]
  if username && password
    auth_db = options[:authentication_database] || db
    @db.authenticate(username, password, nil, auth_db)
  end

  @collection      = @db[:nanopub]
  @nanopub_facets = NanopubFacets.new(options)

  # ensure all indexes are created and maintained
  ensure_all_indexes
end

Instance Method Details

#count_nanopub(filters = []) ⇒ Object



223
224
225
226
# File 'lib/openbel/api/nanopub/mongo.rb', line 223

def count_nanopub(filters = [])
  query_hash = to_query(filters)
  @collection.count(:query => query_hash)
end

#create_nanopub(nanopub) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/openbel/api/nanopub/mongo.rb', line 41

def create_nanopub(nanopub)
  # insert nanopub; acknowledge journal
  if nanopub.respond_to?(:each_pair)
    _id = @collection.insert(nanopub, :w => 1, :j => true)

    # remove nanopub_facets after insert to facets
    remove_nanopub_facets(_id)
    _id
  elsif nanopub.respond_to?(:each)
    @collection.insert(nanopub.to_a, :w => 1, :j => true)
  else
    raise "nanopub type #{nanopub.class} cannot be inserted into Mongo."
  end
end

#delete_dataset(identifier) ⇒ Object



246
247
248
249
250
251
252
# File 'lib/openbel/api/nanopub/mongo.rb', line 246

def delete_dataset(identifier)
  @collection.remove(
    { :"_dataset" => identifier },
    :j => true
  )
  @nanopub_facets.delete_all_facets
end

#delete_facetsObject



242
243
244
# File 'lib/openbel/api/nanopub/mongo.rb', line 242

def delete_facets
  @nanopub_facets.delete_all_facets
end

#delete_nanopub(value) ⇒ Object



254
255
256
257
258
259
260
261
262
# File 'lib/openbel/api/nanopub/mongo.rb', line 254

def delete_nanopub(value)
  if value.respond_to?(:each)
    value.each do |v|
      delete_nanopub_by_id(v)
    end
  else
    delete_nanopub_by_id(value)
  end
end

#delete_nanopub_by_id(value) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/openbel/api/nanopub/mongo.rb', line 271

def delete_nanopub_by_id(value)
  # convert to ObjectId
  begin
    _id = to_id(value)
  rescue BSON::InvalidObjectId
    # indicate that a delete was unsuccessful
    false
  end

  # remove nanopub_facets before nanopub removal
  remove_nanopub_facets(_id)

  # remove nanopub; returns true
  @collection.remove(
    {
        :_id => _id
    },
    :j => true
  )
end

#delete_nanopub_by_query(query) ⇒ Object



264
265
266
267
268
269
# File 'lib/openbel/api/nanopub/mongo.rb', line 264

def delete_nanopub_by_query(query)
  @collection.remove(
    query,
    :j => true
  )
end

#ensure_all_indexesObject



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/openbel/api/nanopub/mongo.rb', line 292

def ensure_all_indexes
  @collection.ensure_index(
    { :bel_statement => Mongo::ASCENDING },
    :background => true
  )
  @collection.ensure_index(
    { :"$**" => Mongo::TEXT },
    :background => true
  )
  @collection.ensure_index(
    { :_dataset => Mongo::ASCENDING },
    :background => true
  )
  @collection.ensure_index(
    { :"experiment_context.name" => Mongo::ASCENDING },
    :background => true
  )
  @collection.ensure_index(
    { :"metadata.name" => Mongo::ASCENDING },
    :background => true
  )
end

#find_all_annotation_referencesObject



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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/openbel/api/nanopub/mongo.rb', line 163

def find_all_annotation_references
  references = @collection.aggregate(
    [
      {
        :$project => {"references.annotations" => 1}
      },
      {
        :$unwind => "$references.annotations"
      },
      {
        :$project => {
          keyword: "$references.annotations.keyword",
          type:    "$references.annotations.type",
          domain:  "$references.annotations.domain"
        }
      },
      {
        :$group => {
          _id: "$keyword",
          type: {
            :$addToSet => "$type"
          },
          domain: {
            :$addToSet => "$domain"
          }
        }
      },
      {
        :$unwind => "$type"
      },
      {
        :$unwind => "$domain"
      },
      {
        :$project => {
          keyword: "$_id", type: "$type", domain: "$domain", _id: 0
        }
      }
    ],
    {
      allowDiskUse: true,
      cursor: {}
    }
  )

  union = []
  remap = {}
  references.each do |obj|
    annotation =
      BELParser::Expression::Model::Annotation.new(
        *obj.values_at('keyword', 'type', 'domain')
      )
    union, new_remap =
      BEL::Nanopub.union_annotation_references(union, [annotation], 'incr')
    remap.merge!(new_remap)
  end

  remap
end

#find_all_namespace_referencesObject



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
# File 'lib/openbel/api/nanopub/mongo.rb', line 96

def find_all_namespace_references
  references = @collection.aggregate(
    [
      {
        :$project => {"references.namespaces" => 1}
      },
      {
        :$unwind => "$references.namespaces"
      },
      {
        :$project => {
          keyword: "$references.namespaces.keyword",
          type:    "$references.namespaces.type",
          domain:  "$references.namespaces.domain"
        }
      },
      {
        :$group => {
          _id: "$keyword",
          type: {
            :$addToSet => "$type"
          },
          domain: {
            :$addToSet => "$domain"
          }
        }
      },
      {
        :$unwind => "$type"
      },
      {
        :$unwind => "$domain"
      },
      {
        :$project => {
          keyword: "$_id", type: "$type", domain: "$domain", _id: 0
        }
      }
    ],
    {
      allowDiskUse: true,
      cursor: {}
    }
  )

  union = []
  remap = {}
  references.each do |obj|
    keyword = obj['keyword']
    type = obj['type']
    domain = obj['domain']
    uri = url = nil
    if type == :url
        url = domain
    elsif type == :uri
        uri = domain
    end
    args = [keyword, uri, url]
    namespace = BELParser::Expression::Model::Namespace.new(*args)
    union, new_remap =
      BEL::Nanopub.union_namespace_references(union, [namespace], 'incr')
    remap.merge!(new_remap)
  end

  remap
end

#find_dataset_nanopub(dataset, filters = [], offset = 0, length = 0, facet = false, facet_value_limit = -1)) ⇒ Object



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
# File 'lib/openbel/api/nanopub/mongo.rb', line 69

def find_dataset_nanopub(dataset, filters = [], offset = 0, length = 0, facet = false, facet_value_limit = -1)
  query_hash = to_query(filters)
  query_hash[:$and] ||= []
  query_hash[:$and].unshift({
    :_dataset => dataset[:identifier]
  })

  query_opts = query_options(
    query_hash,
    :skip  => offset,
    :limit => length,
    :sort  => [
      [:bel_statement, :asc]
    ]
  )

  results = {
    :cursor => @collection.find(query_hash, query_opts)
  }
  if facet
    facets_cursor = @nanopub_facets.find_facets(query_hash, filters, facet_value_limit)
    results[:facets] = facets_cursor.to_a
  end

  results
end

#find_nanopub(filters = [], offset = 0, length = 0, facet = false, facet_value_limit = -1)) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/openbel/api/nanopub/mongo.rb', line 60

def find_nanopub(filters = [], offset = 0, length = 0, facet = false, facet_value_limit = -1)
  if includes_fts_search?(filters)
    text_search = get_fts_search(filters)
    nanopub_aggregate(text_search, filters, offset, length, facet, facet_value_limit)
  else
    nanopub_query(filters, offset, length, facet, facet_value_limit)
  end
end

#find_nanopub_by_id(value) ⇒ Object



56
57
58
# File 'lib/openbel/api/nanopub/mongo.rb', line 56

def find_nanopub_by_id(value)
  @collection.find_one(to_id(value))
end

#update_nanopub_by_id(value, nanopub) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/openbel/api/nanopub/mongo.rb', line 228

def update_nanopub_by_id(value, nanopub)
  # add ObjectId to update
  _id = BSON::ObjectId(value)
  nanopub_h = nanopub.to_h
  nanopub_h[:_id] = _id

  # save nanopub; acknowledge journal
  @collection.save(nanopub_h, :j => true)

  # remove nanopub_facets after update to facets
  remove_nanopub_facets(_id)
  nil
end