Class: ODDB::Interaction::InteractionPlugin

Inherits:
Plugin show all
Defined in:
src/plugin/interaction.rb,
test/test_plugin/interaction.rb

Constant Summary

REFETCH_PAGES =
false
CONNECTION_TYPES =
[
  :substrates, :inhibitors, :inducers,
]
UPDATE_MESSAGES =

ERROR_MESSAGES = {

      :no_flock_conn => "There's no matching Flockhart connection for:",
      :no_hayes_conn => "There's no matching Hayes connection for:",
      }
{
  :cyp450_created     =>  "The following CyP450s have been created:",
  :substance_created  => "The following Substances have been created:",
  :inhibitors_created =>  "The following inhibitor connections have been created:",
  :inhibitors_deleted =>  "The following inhibitor connections have been deleted:",
  :inducers_created   => "The following inducer connections have been created:",
  :inducers_deleted   => "The following inducer connections have been deleted:",
  :substrates_created =>  "The following substrate connections have been created:",
  :substrates_deleted =>  "The following substrate connections have been deleted:",
}

Constants inherited from Plugin

ARCHIVE_PATH, RECIPIENTS

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Plugin

#l10n_sessions, #log_info, #recipients, #resolve_link, #update_rss_feeds

Methods included from HttpFile

#http_body, #http_file

Constructor Details

- (InteractionPlugin) initialize(app)

A new instance of InteractionPlugin



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
# File 'src/plugin/interaction.rb', line 105

def initialize(app)
  @app = app
  @hayes = {}
  @flockhart = {}
  @updated_substances = {}
  @flock_conn_not_found = 0
  @hayes_conn_not_found = 0
=begin
  @merging_errors = {
    :no_flock_conn => [], 
    :no_hayes_conn => [],
  }
=end


  @update_reports = {
    :cyp450_created      => [],
    :substance_created =>  [],
    :inhibitors_created  => [],
    :inhibitors_deleted  => [],
    :inducers_created    =>  [],
    :inducers_deleted    =>  [],
    :substrates_created  => [],
    :substrates_deleted  => [],
  }
end

Instance Attribute Details

- (Object) flock_conn_not_found

Returns the value of attribute flock_conn_not_found



17
18
19
# File 'test/test_plugin/interaction.rb', line 17

def flock_conn_not_found
  @flock_conn_not_found
end

- (Object) flockhart

Returns the value of attribute flockhart



16
17
18
# File 'test/test_plugin/interaction.rb', line 16

def flockhart
  @flockhart
end

- (Object) hayes

Returns the value of attribute hayes



16
17
18
# File 'test/test_plugin/interaction.rb', line 16

def hayes
  @hayes
end

- (Object) hayes_conn_not_found

Returns the value of attribute hayes_conn_not_found



17
18
19
# File 'test/test_plugin/interaction.rb', line 17

def hayes_conn_not_found
  @hayes_conn_not_found
end

- (Object) update_reports

Returns the value of attribute update_reports



18
19
20
# File 'test/test_plugin/interaction.rb', line 18

def update_reports
  @update_reports
end

- (Object) updated_substances

Returns the value of attribute updated_substances



18
19
20
# File 'test/test_plugin/interaction.rb', line 18

def updated_substances
  @updated_substances
end

Instance Method Details

- (Object) flock_conn_name(flock_conn)



129
130
131
132
133
134
135
136
137
138
139
# File 'src/plugin/interaction.rb', line 129

def flock_conn_name(flock_conn)
  name = flock_conn.name
  case name 
  when /=/u
    name.split("=").first
  when /in part/u
    name.split(" ").first
  else
    name
  end
end

- (Object) format_connection_key(key)



140
141
142
# File 'src/plugin/interaction.rb', line 140

def format_connection_key(key)
  Substance.format_connection_key(key)
end

- (Object) merge_data(hayes, flockhart)



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
# File 'src/plugin/interaction.rb', line 143

def merge_data(hayes, flockhart)
  flock_conn_arr = nil
  hayes.each { |hayes_cyt_id, hayes_cyt|
    CONNECTION_TYPES.each { |type|
      if(flock_cyt = flockhart[hayes_cyt_id])
        flock_conn_arr = flock_cyt.send(type)
      else
        flock_conn_arr = []
      end
      hayes_conn_arr = hayes_cyt.send(type)
      hayes_conn_arr.each { |hayes_conn|
        found_conn = false
        flock_conn_found = []
        flock_conn_arr.each { |flock_conn|
          # if it's covered by hayes we only want 
          # flockhart's links, auc_factor and category
          if(similar_name?(hayes_conn.name, flock_conn_name(flock_conn)))
            found_conn = true
            hayes_conn.auc_factor = flock_conn.auc_factor
            hayes_conn.category = flock_conn.category
            hayes_conn.links.concat(flock_conn.links)
            flock_conn_found.push(flock_conn)
          end
        }
        unless(found_conn)
          @flock_conn_not_found += 1
        end
        flock_conn_arr -= flock_conn_found
      }
      flock_conn_arr.each { |flock_conn|
        hayes_cyt.add_connection(flock_conn)
      }
      if(flock_conn_arr)
        @hayes_conn_not_found += flock_conn_arr.size
      end
    }
  }
  hayes
end

- (Object) parse_flockhart(plugin)



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'src/plugin/interaction.rb', line 201

def parse_flockhart(plugin)
  table_hsh = plugin.parse_table 
  cytochromes = plugin.parse_detail_pages 
  cytochromes.each { |cyt_id, cyt|
    if(cyt1 = table_hsh[cyt_id])
      CONNECTION_TYPES.each { |type|
        if(conns1 = cyt1.send(type))
          conns = cyt.send(type)
          lookup = conns.inject({}) { |memo, conn|
            memo.store(conn.name.downcase, conn)
            memo
          }
          conns1.each { |conn|
            if(other = lookup[conn.name.downcase])
              other.category = conn.category
              other.auc_factor = conn.auc_factor
            else
              lookup.store(conn.name.downcase, conn)
              conns.push(conn)
            end
          }
        end
      }
    end
  }
  cytochromes 
end

- (Object) parse_hayes(plugin)



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'src/plugin/interaction.rb', line 182

def parse_hayes(plugin)
  if(REFETCH_PAGES)
    plugin.fetch_pages
  end
  substr_hsh = plugin.parse_substrate_table
  inter_hsh = plugin.parse_interaction_table
  substr_hsh.each { |cyt_id, cyt|
    CONNECTION_TYPES.each { |type|
      unless(type==:substrates)
        if(inter_hsh.has_key?(cyt_id))
          inter_hsh[cyt_id].send(type).each { |conn|
            cyt.add_connection(conn)
          }
        end
      end
    }
  }
  substr_hsh
end

- (Object) report



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'src/plugin/interaction.rb', line 228

def report
  updates = []
  @update_reports.each { |key, value|
    unless(value == [])
      updates.push(UPDATE_MESSAGES[key])
      updates.push(value.uniq)
    end
  }
  hayes_cyts = []
  @hayes.each { |key, value|
    hayes_cyts.push(key)
  }
  flockhart_cyts = []
  @flockhart.each { |key, value|
    flockhart_cyts.push(key)
  }
  lines = [
    "found hayes cytochromes: #{@hayes.size}",
  ] + [
    hayes_cyts.sort.join(", ")
  ] + [
    "found flock cytochromes: #{@flockhart.size}",
  ] + [
    flockhart_cyts.sort.join(", ")
  ] + [
    "There are no matching hayes connections for #{@hayes_conn_not_found} flockhart connections"
  ] + [
    "There are no matching flockhart connections for #{@flock_conn_not_found} hayes connections"
  ]+ updates
  lines.join("\n")
end

- (Boolean) similar_name?(astring, bstring)

Returns:

  • (Boolean)


259
260
261
# File 'src/plugin/interaction.rb', line 259

def similar_name?(astring, bstring)
  (sub = @app.substance(astring)) && sub.same_as?(bstring)
end

- (Object) update



262
263
264
265
266
267
# File 'src/plugin/interaction.rb', line 262

def update
  @hayes = parse_hayes(HayesPlugin.new(@app))
  @flockhart = parse_flockhart(FlockhartPlugin.new(@app, REFETCH_PAGES))
  merged = merge_data(@hayes, @flockhart)
  update_oddb(merged)
end

- (Object) update_oddb(cytochrome_hsh)



268
269
270
271
272
273
274
275
276
277
# File 'src/plugin/interaction.rb', line 268

def update_oddb(cytochrome_hsh)
  cytochrome_hsh.each { |cyt_id, cyt|
    update_oddb_substances(cyt)
    cyp450 = update_oddb_cyp450(cyt_id, cyt)
    update_oddb_cyp450_connections(cyt_id, cyt, cyp450, :inhibitors)
    update_oddb_cyp450_connections(cyt_id, cyt, cyp450, :inducers)
    update_oddb_substrates(cyt_id, cyt)
  }
  update_oddb_tidy_up
end

- (Object) update_oddb_create_substance(connection)



337
338
339
340
341
342
343
344
345
# File 'src/plugin/interaction.rb', line 337

def update_oddb_create_substance(connection)
  pointer = Persistence::Pointer.new(:substance)
  args = {
    connection.lang  => connection.name,
  }
  substance = @app.update(pointer.creator, args)
  @updated_substances.store(substance, {})
  update_report(:substance_created, substance.name)
end

- (Object) update_oddb_cyp450(cyt_id, cyt)



288
289
290
291
292
293
294
295
# File 'src/plugin/interaction.rb', line 288

def update_oddb_cyp450(cyt_id, cyt)
  unless(cyp450 = @app.cyp450(cyt_id))
    pointer = Persistence::Pointer.new(['cyp450', cyt_id])
    cyp450 = @app.create(pointer)
    update_report(:cyp450_created, cyp450.cyp_id.to_s)
  end
  cyp450
end

- (Object) update_oddb_cyp450_connections(cyt_id, cyt, cyp450, connection)



296
297
298
299
300
301
302
303
304
305
306
307
308
309
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
# File 'src/plugin/interaction.rb', line 296

def update_oddb_cyp450_connections(cyt_id, cyt, cyp450, connection)
  cyp450_connections = cyp450.send(connection).keys
  cyt.send(connection).each { |conn|
    subs = @app.substance_by_connection_key(conn.name) \
      || @app.substance(conn.name)
    connection_key = subs.primary_connection_key
    conn_pointer = [ 
      'cyp450' + connection.to_s[0..-2], 
      connection_key, 
    ]
    pointer = cyp450.pointer + conn_pointer
    args = {
      :substance  => connection_key,
      :links      => conn.links,
      :category   => conn.category,
      :auc_factor =>  conn.auc_factor,
    }
    if(cyp450.send(connection).keys.include?(connection_key))
      @app.update(pointer, args)
    else
      @app.update(pointer.creator, args)
      info = "#{cyp450.cyp_id} => #{connection_key}" 
      symbol = (connection.to_s + '_created').intern
      update_report(symbol, info)
    end
    if(cyp450_connections.include?(connection_key))
      cyp450_connections.delete(connection_key)
    end
  }
  cyp450_connections.each { |connection_key|
    conn_pointer = [ 
      'cyp450' + connection.to_s[0..-2], 
      connection_key,
    ]
    pointer = cyp450.pointer + conn_pointer 
    @app.delete(pointer)
    info = "#{cyp450.cyp_id} => #{connection_key}"
    symbol = (connection.to_s + '_deleted').intern
    update_report(symbol, info)
  }
end

- (Object) update_oddb_substances(cyt)



346
347
348
349
350
351
352
353
354
355
# File 'src/plugin/interaction.rb', line 346

def update_oddb_substances(cyt)
  (cyt.substrates + cyt.inhibitors + cyt.inducers).each { |connection|
    connection_key = format_connection_key(connection.name)
    if(subs = @app.substance_by_connection_key(connection_key))
      @updated_substances[subs] ||= subs.substrate_connections.dup
    else
      update_oddb_create_substance(connection)  
    end
  }
end

- (Object) update_oddb_substrates(cyt_id, cyt)



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'src/plugin/interaction.rb', line 356

def update_oddb_substrates(cyt_id, cyt)
  cyt.substrates.each { |substrate|
    args = {
      :links    =>  substrate.links,
      :category =>  substrate.category,
      :cyp450   => cyt_id,
    }
    substance = @app.substance_by_connection_key(substrate.name) \
      || @app.substance(substrate.name)
    connection_key = substance.primary_connection_key
    pointer = substance.pointer + [:cyp450substrate, cyt_id]
    if(substance.cyp450substrate(cyt_id))
      @app.update(pointer, args)
    else
      @app.update(pointer.creator, args)
      info = "#{substrate.name} => #{cyt_id}"
      update_report(:substrates_created, info)
    end
    # ensure that update_oddb_tidy_up does not delete us again
    if(connections = @updated_substances[substance])
      connections.delete(cyt_id)
    end
  }
end

- (Object) update_oddb_tidy_up



278
279
280
281
282
283
284
285
286
287
# File 'src/plugin/interaction.rb', line 278

def update_oddb_tidy_up
  @updated_substances.each { |substance, connections|
    connections.each { |cyt_id, conn|
      pointer = substance.pointer + ['cyp450substrate', cyt_id]
      @app.delete(pointer)
      info = "#{substance} => #{cyt_id}"
      update_report(:substrates_deleted, info)  
    }
  }
end

- (Object) update_report(id, info)



380
381
382
# File 'src/plugin/interaction.rb', line 380

def update_report(id, info)
  @update_reports[id].push(info)
end