From c7edf2df2bac8642fa1635a364e0314c1be79c5c Mon Sep 17 00:00:00 2001 From: rautenberg Date: Wed, 5 Oct 2016 12:52:19 +0200 Subject: fix descriptor calculation --- api/api.json | 7 ++++--- lib/compound.rb | 29 ++++++++++++++++++----------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/api/api.json b/api/api.json index b329bae..0a323de 100644 --- a/api/api.json +++ b/api/api.json @@ -860,7 +860,8 @@ "enum": [ "text/csv", "application/json" - ] + ], + "default": "text/csv" }, { "name": "identifier", @@ -912,7 +913,7 @@ "compound", "descriptor" ], - "description": "Get a list of a single or all descriptors", + "description": "Get a list of a descriptors", "parameters": [ { "name": "accept", @@ -928,7 +929,7 @@ { "name": "descriptor", "in": "path", - "description": "descriptor", + "description": "descriptor name or ID", "required": true, "type": "string" } diff --git a/lib/compound.rb b/lib/compound.rb index c8dd98c..693cdc7 100644 --- a/lib/compound.rb +++ b/lib/compound.rb @@ -1,32 +1,39 @@ # Get a list of a single or all descriptors # @param [Header] Accept one of text/plain, application/json -# @param [Path] Descriptor name (e.G.: Openbabel.HBA1) +# @param [Path] Descriptor name or descriptor ID (e.G.: Openbabel.HBA1, 5755f8eb3cf99a00d8fedf2f) # @return [text/plain, application/json] list of all prediction models get "/compound/descriptor/?:descriptor?" do case @accept when "application/json" return "#{JSON.pretty_generate PhysChem::DESCRIPTORS} " unless params[:descriptor] - return {params[:descriptor] => PhysChem::DESCRIPTORS[params[:descriptor]]}.to_json + return {params[:descriptor] => PhysChem::DESCRIPTORS[params[:descriptor]]}.to_json if PhysChem::DESCRIPTORS.include?(params[:descriptor]) + return {PhysChem.find(params[:descriptor])}.to_json if PhysChem.find(params[:descriptor]) else return PhysChem::DESCRIPTORS.collect{|k, v| "#{k}: #{v}\n"} unless params[:descriptor] - return PhysChem::DESCRIPTORS[params[:descriptor]] + return PhysChem::DESCRIPTORS[params[:descriptor]] if PhysChem::DESCRIPTORS.include?(params[:descriptor]) + return "#{PhysChem.find(params[:descriptor]).name}: #{PhysChem.find(params[:descriptor]).description}" if PhysChem.find(params[:descriptor]) end end post "/compound/descriptor/?" do bad_request_error "Missing Parameter " unless params[:identifier] && params[:descriptor] - descriptor = params['descriptor'].split(',') + descriptors = params['descriptor'].split(',') compound = Compound.from_smiles params[:identifier] - d = compound.physchem descriptor - csv = d.to_csv - csv = "SMILES,#{params[:descriptor]}\n#{params[:identifier]},#{csv}" if params[:identifier] + physchem_descriptors = [] + descriptors.each do |descriptor| + physchem_descriptors << PhysChem.find_by(:name => descriptor) + end + result = compound.physchem physchem_descriptors + csv = result.collect{|k,v| "\"#{PhysChem.find(k).name}\",#{v}" }.join("\n") + csv = "SMILES,#{params[:identifier]}\n#{csv}" if params[:identifier] case @accept - when "application/csv" + when "text/csv","application/csv" return csv when "application/json" - lines = CSV.parse(csv) - keys = lines.delete lines.first - data = lines.collect{|values|Hash[keys.zip(values)]} + result_hash = result.collect{|k,v| {"#{PhysChem.find(k).name}" => "#{v}"}} # result.collect{|k,v| "\"#{PhysChem.find(k).name}\"" => "#{v}"}.join(",") + data = {"compound" => {"SMILES" => "#{params[:identifier]}"}} + data["compound"]["InChI"] = "#{compound.inchi}" if compound.inchi + data["compound"]["results"] = result_hash return JSON.pretty_generate(data) end end -- cgit v1.2.3