Module: ClassyFireAPI

Defined in:
lib/classyfire_api.rb

Constant Summary

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

Class Method Summary (collapse)

Class Method Details

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

Retrieves the classification results for a given entity.

Parameters:

  • inchikey (String)

    the InChIKey of the entity.

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

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

Returns:

  • (Text)

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



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

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



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

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.

Parameters:

  • the

    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 file displaying the classification results for the entity in the specified format.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/classyfire_api.rb', line 87

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'



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

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.



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
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
# File 'lib/classyfire_api.rb', line 309

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



378
379
380
381
382
383
384
385
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
# File 'lib/classyfire_api.rb', line 378

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(/Applications/ChemAxon/JChem/bin/molconvert inchikey -s "#{h[key]}").strip
        # 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(/Applications/ChemAxon/JChem/bin/molconvert inchikey -s "#{h[key]}").strip
      # 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



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/classyfire_api.rb', line 440

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(/Applications/ChemAxon/JChem/bin/molconvert inchikey -s "#{h[key]}").strip
        # 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]
    # puts "#{key} :: #{h[key]}"
    begin
      inchikey = %x(/Applications/ChemAxon/JChem/bin/molconvert inchikey -s "#{h[key]}").strip
      # 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
  # f_output.print res
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'



227
228
229
230
231
232
233
234
235
236
# File 'lib/classyfire_api.rb', line 227

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, which should be returned in a specific format.

if there is an error.

Parameters:

  • label (String)

    the label of the query.

  • input (String)

    the input of the query.

Returns:

  • (Hash)

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



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

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, start, 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.



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
205
206
207
208
209
210
211
212
# File 'lib/classyfire_api.rb', line 150

def ClassyFireAPI.submit_query_input_in_chunks(input_file,slice_length, start, 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)

  lines.uniq.each do |line|
    sline = line.strip.split("\t")
    if sline.length == 1
      input <<"#{sline[0]}"
    elsif sline.length >= 2
      input <<"#{sline[0]}\t#{sline[2]}"
    end
  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 = 120
  initial_nr_of_jobs = 60
  i = start

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

    begin
      puts "submitting #{title}"
      q       = submit_query(title,subdivised_groups[i-1].join("\n"),type)
      query_ids << JSON.parse(q)['id']
    rescue Exception => e  
      puts e.message  
      puts e.backtrace.inspect
    end
    i = i + 1
  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 < subdivised_groups.length
    k = 0
    for k in (i..(i+80))
      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

+ (Object) submit_random_subset_of_query_input_in_chunks(tab_separated_input_file, size, type = 'STRUCTURE')

Takes a tab-separated file and submit randomly selected 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. is fragmented into n part of #slice_length entries each).

Parameters:

  • tab_separated_input_file (Text)

    The path to the input file.

  • size (Integer)

    The maximum number of entries for each query input (the whole file

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

    The query_type, 'STRUCTURE' (default) or 'IUPAC_NAME' or 'FASTA'.



252
253
254
255
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
# File 'lib/classyfire_api.rb', line 252

def ClassyFireAPI.submit_random_subset_of_query_input_in_chunks(tab_separated_input_file,size, type='STRUCTURE')
  @start_time = Time.now
  absolute_path = File.expand_path(tab_separated_input_file)
  f             = File.open(absolute_path, 'r')
  input         = []

  f.each_line do |line|

    sline = line.strip.split("\t")
    if sline.length == 1
      input <<"#{sline[0]}"
    elsif sline.length == 2
      input <<"#{sline[0]}\t#{sline[1]}"
    end
  end

  query_ids = []
  indexes = []
  r = 1
  while r <= (size)
    s = rand(0..(input.length - 1))
    unless indexes.include?(s)
      indexes<<s
      r += 1
    end
  end

  random_subset = indexes.map{|x| input[x]}
  subdivised_groups = random_subset.each_slice(100).to_a
  sleeping_time = 120

  i = 0
  while i < subdivised_groups.length
    k = 0
    for k in (i..(i + 5))
      title     = File.basename(absolute_path).split(".")[0] + "_yannick" + "_part_#{k+1}"
      i = i + 1
      begin
        puts "submitting #{title}"
        q = submit_query(title,subdivised_groups[k].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