diff --git a/lib/classyfire_api.rb b/lib/classyfire_api.rb index 2c16c24..6c86705 100644 --- a/lib/classyfire_api.rb +++ b/lib/classyfire_api.rb @@ -1,5 +1,6 @@ require 'JSON' require 'csv' +require 'rest-client' module ClassyFireAPI 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) # @return [Text] A text file displaying the classification results for the entity in the specified format. def ClassyFireAPI.get_entity_classification(inchikey,format="json") - inchikey_id = inchikey.to_s.gsub('InChIKey=','') + # inchikey_id = inchikey.to_s.gsub('InChIKey=','') begin if format == "json" - RestClient.get "#{URL}/entities/#{inchikey_id}.#{format}", :accept => :json + RestClient.get "#{URL}/entities/#{inchikey}.#{format}", :accept => :json elsif format == "sdf" - RestClient.get "#{URL}/entities/#{inchikey_id}.#{format}", :accept => :sdf + RestClient.get "#{URL}/entities/#{inchikey}.#{format}", :accept => :sdf elsif format == "csv" - RestClient.get "#{URL}/entities/#{inchikey_id}.#{format}", :accept => :csv + RestClient.get "#{URL}/entities/#{inchikey}.#{format}", :accept => :csv end rescue RestClient::ResourceNotFound => e e.response @@ -148,7 +149,7 @@ module ClassyFireAPI # @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'. - 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 absolute_path = File.expand_path(input_file) f = File.open(absolute_path, 'r') @@ -161,7 +162,7 @@ module ClassyFireAPI if sline.length == 1 input <<"#{sline[0]}" elsif sline.length >= 2 - input <<"#{sline[0]}\t#{sline[1]}" + input <<"#{sline[1]}\t#{sline[0]}" end end @@ -169,12 +170,12 @@ module ClassyFireAPI 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 + sleeping_time = 360 + initial_nr_of_jobs = 40 i = start while i < initial_nr_of_jobs - title = File.basename(absolute_path).split(".")[0] + "_yannick" + "_part_#{i}" + title = File.basename(absolute_path).split(".")[0] + "_yannick" + "_part_#{i}" if i <= subdivised_groups.length begin @@ -186,6 +187,8 @@ module ClassyFireAPI puts e.backtrace.inspect end i = i + 1 + else + break end end @@ -193,7 +196,7 @@ module ClassyFireAPI sleep(sleeping_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 for k in (i..(i+80)) title = File.basename(absolute_path).split(".")[0] + "_yannick" + "_part_#{k}" @@ -367,7 +370,6 @@ module ClassyFireAPI e.message end end - res += "]}" f_output.print res end @@ -402,7 +404,8 @@ module ClassyFireAPI puts i # puts "#{key} :: #{h[key]}" 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 qr = JSON.parse(ClassyFireAPI.get_entity_classification(inchikey,format="json")) qr['identifier'] = key @@ -417,7 +420,8 @@ module ClassyFireAPI key = h.keys[-1] # puts "#{key} :: #{h[key]}" 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 qr = JSON.parse(ClassyFireAPI.get_entity_classification(inchikey,format="json")) qr['identifier'] = key @@ -459,7 +463,8 @@ module ClassyFireAPI puts i # puts "#{key} :: #{h[key]}" 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 qr = ClassyFireAPI.get_entity_classification(inchikey,format="sdf") if qr.include?("The page you were looking for doesn't exist") @@ -478,9 +483,9 @@ module ClassyFireAPI i += 1 end key = h.keys[-1] - # puts "#{key} :: #{h[key]}" 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 qr = ClassyFireAPI.get_entity_classification(inchikey,format="sdf") if qr.include?("The page you were looking for doesn't exist") @@ -497,6 +502,5 @@ module ClassyFireAPI e.message end end - # f_output.print res end end \ No newline at end of file