- Using babel now instead of molconvert to generate inchikeys

This commit is contained in:
djoumbou 2016-04-19 22:44:35 -06:00
parent b7c175e327
commit e6a10deb22
1 changed files with 21 additions and 17 deletions

View File

@ -1,5 +1,6 @@
require 'JSON' require 'JSON'
require 'csv' require 'csv'
require 'rest-client'
module ClassyFireAPI module ClassyFireAPI
URL = 'http://classyfire.wishartlab.com' URL = 'http://classyfire.wishartlab.com'
@ -60,14 +61,14 @@ module ClassyFireAPI
# @param format [String] the format of the query, 'text' (either JSON, CSV, or SDF) # @param format [String] the format of the query, 'text' (either JSON, CSV, or SDF)
# @return [Text] A text file displaying the classification results for the entity in the specified format. # @return [Text] A text file displaying the classification results for the entity in the specified format.
def ClassyFireAPI.get_entity_classification(inchikey,format="json") def ClassyFireAPI.get_entity_classification(inchikey,format="json")
inchikey_id = inchikey.to_s.gsub('InChIKey=','') # inchikey_id = inchikey.to_s.gsub('InChIKey=','')
begin begin
if format == "json" if format == "json"
RestClient.get "#{URL}/entities/#{inchikey_id}.#{format}", :accept => :json RestClient.get "#{URL}/entities/#{inchikey}.#{format}", :accept => :json
elsif format == "sdf" elsif format == "sdf"
RestClient.get "#{URL}/entities/#{inchikey_id}.#{format}", :accept => :sdf RestClient.get "#{URL}/entities/#{inchikey}.#{format}", :accept => :sdf
elsif format == "csv" elsif format == "csv"
RestClient.get "#{URL}/entities/#{inchikey_id}.#{format}", :accept => :csv RestClient.get "#{URL}/entities/#{inchikey}.#{format}", :accept => :csv
end end
rescue RestClient::ResourceNotFound => e rescue RestClient::ResourceNotFound => e
e.response e.response
@ -148,7 +149,7 @@ module ClassyFireAPI
# @param: slice_length [Integer] The maximum number of entries for each query input (the whole file # @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). # is fragmented into n part of #slice_length entries each).
# @param: start [Integer] The starting index. Submit framgments from the index 'start'. # @param: start [Integer] The starting index. Submit framgments from the index 'start'.
def ClassyFireAPI.submit_query_input_in_chunks(input_file,slice_length, start, type='STRUCTURE') def ClassyFireAPI.submit_query_input_in_chunks(input_file,slice_length=1000, start=1, type='STRUCTURE')
@start_time = Time.now @start_time = Time.now
absolute_path = File.expand_path(input_file) absolute_path = File.expand_path(input_file)
f = File.open(absolute_path, 'r') f = File.open(absolute_path, 'r')
@ -161,7 +162,7 @@ module ClassyFireAPI
if sline.length == 1 if sline.length == 1
input <<"#{sline[0]}" input <<"#{sline[0]}"
elsif sline.length >= 2 elsif sline.length >= 2
input <<"#{sline[0]}\t#{sline[1]}" input <<"#{sline[1]}\t#{sline[0]}"
end end
end end
@ -169,8 +170,8 @@ module ClassyFireAPI
subdivised_groups = input.uniq.each_slice(slice_length).to_a subdivised_groups = input.uniq.each_slice(slice_length).to_a
puts "nr of subdivised_groups: #{subdivised_groups.length}" puts "nr of subdivised_groups: #{subdivised_groups.length}"
puts subdivised_groups[0] puts subdivised_groups[0]
sleeping_time = 120 sleeping_time = 360
initial_nr_of_jobs = 60 initial_nr_of_jobs = 40
i = start i = start
while i < initial_nr_of_jobs while i < initial_nr_of_jobs
@ -186,6 +187,8 @@ module ClassyFireAPI
puts e.backtrace.inspect puts e.backtrace.inspect
end end
i = i + 1 i = i + 1
else
break
end end
end end
@ -193,7 +196,7 @@ module ClassyFireAPI
sleep(sleeping_time) sleep(sleeping_time)
puts "Waking up at #{Time.now - @start_time}" puts "Waking up at #{Time.now - @start_time}"
while i < subdivised_groups.length while i >= initial_nr_of_jobs && i < subdivised_groups.length
k = 0 k = 0
for k in (i..(i+80)) for k in (i..(i+80))
title = File.basename(absolute_path).split(".")[0] + "_yannick" + "_part_#{k}" title = File.basename(absolute_path).split(".")[0] + "_yannick" + "_part_#{k}"
@ -367,7 +370,6 @@ module ClassyFireAPI
e.message e.message
end end
end end
res += "]}" res += "]}"
f_output.print res f_output.print res
end end
@ -402,7 +404,8 @@ module ClassyFireAPI
puts i puts i
# puts "#{key} :: #{h[key]}" # puts "#{key} :: #{h[key]}"
begin begin
inchikey = %x(/Applications/ChemAxon/JChem/bin/molconvert inchikey -s "#{h[key]}").strip inchikey = %x(obabel -:"#{h[key]}" -oinchikey).strip.split("\t")[0]
# inchikey = %x(/Applications/ChemAxon/JChem/bin/molconvert inchikey -s "#{h[key]}").strip
# puts inchikey # puts inchikey
qr = JSON.parse(ClassyFireAPI.get_entity_classification(inchikey,format="json")) qr = JSON.parse(ClassyFireAPI.get_entity_classification(inchikey,format="json"))
qr['identifier'] = key qr['identifier'] = key
@ -417,7 +420,8 @@ module ClassyFireAPI
key = h.keys[-1] key = h.keys[-1]
# puts "#{key} :: #{h[key]}" # puts "#{key} :: #{h[key]}"
begin begin
inchikey = %x(/Applications/ChemAxon/JChem/bin/molconvert inchikey -s "#{h[key]}").strip inchikey = %x(obabel -:"#{h[key]}" -oinchikey).strip.split("\t")[0]
# inchikey = %x(/Applications/ChemAxon/JChem/bin/molconvert inchikey -s "#{h[key]}").strip
# puts inchikey # puts inchikey
qr = JSON.parse(ClassyFireAPI.get_entity_classification(inchikey,format="json")) qr = JSON.parse(ClassyFireAPI.get_entity_classification(inchikey,format="json"))
qr['identifier'] = key qr['identifier'] = key
@ -459,7 +463,8 @@ module ClassyFireAPI
puts i puts i
# puts "#{key} :: #{h[key]}" # puts "#{key} :: #{h[key]}"
begin begin
inchikey = %x(/Applications/ChemAxon/JChem/bin/molconvert inchikey -s "#{h[key]}").strip inchikey = %x(obabel -:"#{h[key]}" -oinchikey).strip.split("\t")[0]
# inchikey = %x(/Applications/ChemAxon/JChem/bin/molconvert inchikey -s "#{h[key]}").strip
# puts inchikey # puts inchikey
qr = ClassyFireAPI.get_entity_classification(inchikey,format="sdf") qr = ClassyFireAPI.get_entity_classification(inchikey,format="sdf")
if qr.include?("The page you were looking for doesn't exist") if qr.include?("The page you were looking for doesn't exist")
@ -478,9 +483,9 @@ module ClassyFireAPI
i += 1 i += 1
end end
key = h.keys[-1] key = h.keys[-1]
# puts "#{key} :: #{h[key]}"
begin begin
inchikey = %x(/Applications/ChemAxon/JChem/bin/molconvert inchikey -s "#{h[key]}").strip inchikey = %x(obabel -:"#{h[key]}" -oinchikey).strip.split("\t")[0]
# inchikey = %x(/Applications/ChemAxon/JChem/bin/molconvert inchikey -s "#{h[key]}").strip
# puts inchikey # puts inchikey
qr = ClassyFireAPI.get_entity_classification(inchikey,format="sdf") qr = ClassyFireAPI.get_entity_classification(inchikey,format="sdf")
if qr.include?("The page you were looking for doesn't exist") if qr.include?("The page you were looking for doesn't exist")
@ -497,6 +502,5 @@ module ClassyFireAPI
e.message e.message
end end
end end
# f_output.print res
end end
end end