Module: ClassyFireAPI

Defined in:
lib/classyfire_api.rb

Overview

require 'node_resource.rb'

Constant Summary

URL =
'http://classyfire.wishartlab.com'

Class Method Summary (collapse)

Class Method Details

+ (Text) get_chemont_node(chemontid)

Return data for the TaxNode with ID chemontid.

Use JSON.parse to get a the json object.

Parameters:

  • chemontid (String)

    the ChemOnt ID of the entity.

Returns:

  • (Text)

    A text displaying the classification results for the entity in the specified format.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/classyfire_api.rb', line 63

def ClassyFireAPI.get_chemont_node(chemontid)
  chemont_id = chemontid.to_s.gsub("CHEMONTID:","C")
  begin
    RestClient.get "#{URL}/tax_nodes/#{chemont_id}.json", :accept => :json
  rescue RestClient::ResourceNotFound => e
    e.response
  rescue  RestClient::InternalServerError => e
    e.response
  rescue RestClient::GatewayTimeout => e
    e.response
  rescue RestClient::RequestTimeout => e
    e.response
  end
end

+ (Text) get_entity_classification(inchikey, format = "json")

Retrieves the classification results for a given chemical entity.

Parameters:

  • the

    InChIKey [String] of the entity

  • The

    format [String] the format of the query (Only JSON)

Returns:

  • (Text)

    A text file displaying the classification results for the entity in the specified format.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/classyfire_api.rb', line 105

def ClassyFireAPI.get_entity_classification(inchikey,format="json")
  inchikey_id = inchikey.to_s.gsub('InChIKey=','')
  begin
    if format == "json"
      RestClient.get "#{URL}/entities/#{inchikey_id}.#{format}", :accept => :json
    elsif format == "sdf"
      RestClient.get "#{URL}/entities/#{inchikey_id}.#{format}", :accept => :sdf
    elsif format == "csv"
      RestClient.get "#{URL}/entities/#{inchikey_id}.#{format}", :accept => :csv
    end 
  rescue RestClient::ResourceNotFound => e
    e.response
  rescue  RestClient::InternalServerError => e
    e.response
  rescue RestClient::GatewayTimeout => e
    e.response
  rescue RestClient::RequestTimeout => e
    e.response
  end
end

+ (Text) get_query(query_id, format = "json")

Retrieves the classification results for a given query.

the query's entities in the specified format.

Parameters:

  • query_id (Integer)

    the ID of the query.

  • format (String) (defaults to: "json")

    the format of the query (either JSON, CSV, or SDF)

Returns:

  • (Text)

    A text file displaying the classification results for



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/classyfire_api.rb', line 38

def ClassyFireAPI.get_query(query_id,format="json")
  begin
    if format == "json"
      RestClient.get "#{URL}/queries/#{query_id}.json", :accept => :json
    elsif format == "sdf"
      RestClient.get "#{URL}/queries/#{query_id}.sdf", :accept => :sdf
    elsif format == "csv"
      RestClient.get "#{URL}/queries/#{query_id}.csv", :accept => :csv
    end
  rescue RestClient::ResourceNotFound => e
    e.response
  rescue  RestClient::InternalServerError => e
    e.response
  rescue RestClient::GatewayTimeout => e
    e.response
  rescue RestClient::RequestTimeout => e
    e.response
  end      
end

+ (Text) get_sequence_classification(fingerprint, format = "json")

Retrieves the classification results for a given sequence.

Use JSON.parse to get a the json object.

Parameters:

  • fingerprint (String)

    the fingerprint (generated from the sequence using Digest::MD5).

  • format (String) (defaults to: "json")

    the format of the query (Only JSON)

Returns:

  • (Text)

    A text displaying the classification results for the entity in the specified format.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/classyfire_api.rb', line 84

def ClassyFireAPI.get_sequence_classification(fingerprint,format="json")
  begin
    if format == "json"
      RestClient.get "#{URL}/entities/#{fingerprint}.#{format}", :accept => :json
    end 
  rescue RestClient::ResourceNotFound => e
    e.response
  rescue  RestClient::InternalServerError => e
    e.response
  rescue RestClient::GatewayTimeout => e
    e.response
  rescue RestClient::RequestTimeout => e
    e.response
  end
end

+ (String) query_status(query_id)

Retrieves the status of a query

Parameters:

  • query_id (Integer)

    the ID of the query

Returns:

  • (String)

    the query status, 'Done' or 'In progress', 'string'



130
131
132
133
134
135
136
137
# File 'lib/classyfire_api.rb', line 130

def ClassyFireAPI.query_status(query_id)
  begin
    RestClient.get "#{URL}/queries/#{query_id}/status.json", :accept => :json
  rescue Exception=>e
    $stderr.puts e.message
    nil
  end
end

+ (String) retrieve_classification(input, output)

Reads a tab separated file, and use the structure representation to retrieve the strutcure's classification from ClassyFire.

Parameters:

  • input (String)

    path to the input file.

Returns:

  • (String)

    path to the output file.



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/classyfire_api.rb', line 256

def ClassyFireAPI.retrieve_classification(input,output)
  absolute_path = File.expand_path(input)
  f_input       = File.open(absolute_path, 'r')
  h             = Hash.new
  directory     = absolute_path.split('/')[0...-1].join("/")
  f_output      = File.new(output, 'w')
  res = String.new


  res += "{"
  res += '"id": 1,'
  res += '"label":"' + output + '",' + '"classification_status":"Done",' + '"entities":['

  f_input.each_line do |line|
    sline = line.strip.split("\t")
    if sline.length == 1
      h[sline[0]] = sline[0]
    elsif sline.length == 2
      h[sline[0]]  = line.strip
    end
  end

  puts h.keys.uniq.length
  if h.keys.length > 0
    i = 1
    h.keys.uniq[0..-1].each do |key|
      puts i

      puts "#{key} :: #{h[key]}"
      begin
        qs = submit_query(key,h[key])

        qs_decoded = JSON.parse(qs)
        qr = JSON.parse(get_query(qs_decoded["id"],format="json"))

        res += qr["entities"][0].to_json
        res += ","
        i += 1
      rescue Exception => e
        e.message
      end

    end
    key = h.keys[-1]
    puts "#{key} :: #{h[key]}"
    begin
      qs = submit_query(key,h[key])
      sleep(0.2)
      qs_decoded = JSON.parse(qs)
      qr = JSON.parse(get_query(qs_decoded["id"],format="json"))
      # puts qr["entities"]
      # sleep(0.2)
      # f_output.print qr["entities"][0],"\n"
      res += qr["entities"][0].to_json
      # res += ","
    rescue Exception => e
      e.message
    end        
  end
  res += "]}"
  f_output.print res
end

+ (String) retrieve_entities_json(input, output)

Reads a tab separated file, and use the structure representation to retrieve the strutcure's classification from ClassyFire in a JSON format.

Parameters:

  • input (String)

    path to the input file

Returns:

  • (String)

    path to the output file



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/classyfire_api.rb', line 324

def ClassyFireAPI.retrieve_entities_json(input,output)
  absolute_path = File.expand_path(input)
  f_input       = File.open(absolute_path, 'r')
  h             = Hash.new
  directory     = absolute_path.split('/')[0...-1].join("/")
  f_output      = File.new(output, 'w')
  puts 
  res = String.new

  res += "{"
  res += '"id": 1,'
  res += '"label":"' + output + '",' + '"classification_status":"Done",' + '"entities":['

  f_input.each_line do |line|
    sline = line.strip.split("\t")
    h[sline[0]]  = sline[-1]
  end

  puts h.keys.uniq.length
  if h.keys.length > 0
    i = 1
    h.keys.uniq[0...-1].each do |key|
      puts i
      # puts "#{key} :: #{h[key]}"
      begin
        inchikey = %x(obabel -:"#{h[key]}" -oinchikey).strip.split("\t")[0] 
        # puts inchikey
        qr = JSON.parse(ClassyFireAPI.get_entity_classification(inchikey,format="json"))
        qr['identifier'] = key
        res += qr.to_json
        res += ","
        puts "#{key} :: RETURN NIL" if qr.nil? || qr['direct_parent']['name'].nil?
      rescue Exception => e
        e.message
      end
      i += 1
    end
    key = h.keys[-1]
    # puts "#{key} :: #{h[key]}"
    begin
      inchikey = %x(obabel -:"#{h[key]}" -oinchikey).strip.split("\t")[0]
      # puts inchikey
      qr = JSON.parse(ClassyFireAPI.get_entity_classification(inchikey,format="json"))
      qr['identifier'] = key
      res += qr.to_json
      puts "#{key} :: RETURN NIL" if qr.nil? || qr['direct_parent']['name'].nil?
      # res += ","
    rescue Exception => e
      e.message
    end        
  end
  res += "]}"
  f_output.print res
end

+ (String) retrieve_entities_sdf(input, output)

Reads a tab separated file, and use the structure representation to retrieve the strutcure's classification from ClassyFire in a SDF format.

Parameters:

  • input (String)

    path to the input file

Returns:

  • (String)

    path to the output file



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/classyfire_api.rb', line 386

def ClassyFireAPI.retrieve_entities_sdf(input,output)
  absolute_path = File.expand_path(input)
  f_input       = File.open(absolute_path, 'r')
  h             = Hash.new
  directory     = absolute_path.split('/')[0...-1].join("/")
  f_output      = File.new(output, 'w')
  res = String.new

  f_input.each_line do |line|
    sline = line.strip.split("\t")
    h[sline[0]]  = sline[-1]
  end

  puts h.keys.uniq.length
  if h.keys.length > 0
    i = 1
    h.keys.uniq[0...-1].each do |key|
      puts i
      # puts "#{key} :: #{h[key]}"
      begin
        inchikey = %x(obabel -:"#{h[key]}" -oinchikey).strip.split("\t")[0]
        # puts inchikey
        qr = ClassyFireAPI.get_entity_classification(inchikey,format="sdf")
        if qr.include?("The page you were looking for doesn't exist")
          puts "The page you were looking for doesn't exist"
        elsif qr.empty?

        else
          input = qr.split("\n")[1..-1].join("\n")
          puts input
          f_output.puts "#{key}\n"
          f_output.puts input
        end
      rescue Exception => e
        e.message
      end
      i += 1
    end
    key = h.keys[-1]
    begin
      inchikey = %x(obabel -:"#{h[key]}" -oinchikey).strip.split("\t")[0]
      # puts inchikey
      qr = ClassyFireAPI.get_entity_classification(inchikey,format="sdf")
      if qr.include?("The page you were looking for doesn't exist")
        puts "The page you were looking for doesn't exist"
      elsif qr.empty?

      else
        input = qr.split("\n")[1..-1].join("\n")
        puts input
        f_output.puts "#{key}\n"
        f_output.puts input
      end
    rescue Exception => e
      e.message
    end        
  end
end

+ (Object) submit_queries_from_directory(folder, slice_length, type = "STRUCTURE")

Takes each file in a folder, and submit the contained structures in bluks of a given size.

For 'STRUCTURE' or 'IUPAC_NAME'query types, each line must contain either

1) Only a structural represenation: SMILES, InChI for the 'STRUCTURE' query_type or a IUPAC name
  for the 'IUPAC NAME' query type.
2) a tab-separated pair of an ID and the corresponding sructure representation: SMILES, InChI for the 
  'STRUCTURE' query_type or a IUPAC name for the 'IUPAC NAME' query type.

For 'FASTA' query type, just submit the query as a standard FASTA text. @param: input_file [String] The path to the folder. @param: slice_length [Integer] The maximum number of entries for each query input (each file is fragmented into n part of #slice_length entries each), 'integer'

Parameters:

  • type (String) (defaults to: "STRUCTURE")

    the query_type 'STRUCTURE' (default) or 'IUPAC_NAME' or 'FASTA'



239
240
241
242
243
244
245
246
247
248
# File 'lib/classyfire_api.rb', line 239

def ClassyFireAPI.submit_queries_from_directory(folder,slice_length,type="STRUCTURE")
  if File.directory?(folder)
    Dir.foreach(folder) do |filename|
      puts "Filename: #{filename}"
      ClassyFireAPI.submit_query_input_in_chunks(folder+"/"+filename,slice_length, type) unless filename == "." || filename == ".." || filename == ".DS_Store"
    end
  else
    $stderr.puts "#{folder} is not a folder."
  end
end

+ (Hash) submit_query(label, input, type = 'STRUCTURE')

Submits a ClassyFire query in a JSON format.

if there is an error. Use JSON.parse to get a the json object.

Parameters:

  • label (String)

    the label of the query.

  • input (String)

    the input of the query.

  • type (String) (defaults to: 'STRUCTURE')

    the type of the query.

Returns:

  • (Hash)

    A Ruby Hash with the id (and other attributes) of the Query or nil



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/classyfire_api.rb', line 15

def ClassyFireAPI.submit_query(label,input, type='STRUCTURE')
  begin
    q = RestClient.post URL+'/queries', {:label => label, :query_input => input, :query_type => type}.to_json, :accept => :json, :content_type => :json
  rescue RestClient::BadRequest => e
    e.response
  rescue  RestClient::InternalServerError => e
    e.response
  rescue RestClient::GatewayTimeout => e
    e.response
  rescue RestClient::RequestTimeout => e
    e.response
  rescue RestClient::UnprocessableEntity => e
    e.response
  end
  q
end

+ (Object) submit_query_input_in_chunks(input_file, slice_length = 1000, start = 1, type = 'STRUCTURE')

Takes a tab-separated file and submit the contained structures in bulks of a given size

For 'STRUCTURE' or 'IUPAC_NAME'query types, each line must contain either

1) Only a structural represenation: SMILES, InChI for the 'STRUCTURE' query_type or a IUPAC name
  for the 'IUPAC NAME' query type.
2) a tab-separated pair of an ID and the corresponding sructure representation: SMILES, InChI for the 
  'STRUCTURE' query_type or a IUPAC name for the 'IUPAC NAME' query type.

For 'FASTA' query type, just submit the query as a standard FASTA text. @param: slice_length [Integer] The maximum number of entries for each query input (the whole file is fragmented into n part of #slice_length entries each). @param: start [Integer] The starting index. Submit framgments from the index 'start'.

Parameters:

  • input_file (Text)

    The path to the input file.



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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/classyfire_api.rb', line 152

def ClassyFireAPI.submit_query_input_in_chunks(input_file,slice_length=1000, start=1, type='STRUCTURE')
  @start_time = Time.now
  absolute_path = File.expand_path(input_file)
  f             = File.open(absolute_path, 'r')
  input         = []

  lines = File.readlines(absolute_path)
  i = 0
  lines.uniq.each do |line|
    i += 1
    sline = line.strip.split("\t")
    if sline.length == 1
      input <<"#{sline[0]}"
    elsif sline.length == 2
      #ID\tSMILES (OR INCHI, OR VALID IUPAC NAME)
      input <<"#{sline[0]}\t#{sline[1]}"
    end
    input <<"#{sline[0]}"
  end

  query_ids = []
  subdivised_groups = input.uniq.each_slice(slice_length).to_a
  puts "nr of subdivised_groups: #{subdivised_groups.length}"
  # puts subdivised_groups[0]
  sleeping_time = 240
  initial_nr_of_jobs = 30
  i = start

  while i <  initial_nr_of_jobs

    title = File.basename(absolute_path).split(".")[0] + "_yannick" + "_part_#{i}"

    if i <= subdivised_groups.length
      puts "\n\n\n\n---------------------- -----------"
      # begin
        puts "submitting #{title}"
        puts subdivised_groups[i-1].join("\n")
        q       = submit_query(title,subdivised_groups[i-1].join("\n"),type)
        # puts q
        query_ids << JSON.parse(q)['id']
      # rescue Exception => e  
        # puts e.message  
        # puts e.backtrace.inspect
      # end
      i = i + 1
    else
      break
    end
  end

  puts "Going to sleep at #{Time.now - @start_time} for #{sleeping_time} s."
  sleep(sleeping_time)
  puts "Waking up at #{Time.now - @start_time}"

  while i >= initial_nr_of_jobs && i < subdivised_groups.length
    k = 0
    for k in (i..(i + initial_nr_of_jobs))
      title     = File.basename(absolute_path).split(".")[0] + "_yannick" + "_part_#{k}"
      i = i + 1
      begin
        puts "submitting #{title}"
        q = submit_query(title,subdivised_groups[k-1].join("\n"),type)
      rescue Exception => e  
        puts e.message  
        puts e.backtrace.inspect
      end
    end
    i = k
    puts "Going to sleep at #{Time.now - @start_time} for #{sleeping_time} s."
    sleep(sleeping_time)
    puts "Waking up at #{Time.now - @start_time}"
  end
end