Class: Chem::PubChem::PubChem

Inherits:
Object
  • Object
show all
Defined in:
lib/chem/db/pubchem.rb

Constant Summary collapse

Searchpath =
"/search/"
Query =
"PreQSrv.cgi"
Boundary =
"-----boundary-----"
Data =
[
Boundary, "Content-Disposition: form-data; name=\"mode\"", "", "simplequery",
Boundary, "Content-Disposition: form-data; name=\"check\"", "", "remote",
Boundary, "Content-Disposition: form-data; name=\"execution\"", "", "remote",
Boundary, "Content-Disposition: form-data; name=\"queue\"", "", "ssquery",
Boundary, "Content-Disposition: form-data; name=\"simple_searchdata\"", "", '%s',
Boundary, "Content-Disposition: form-data; name=\"simple_cid\"", "", "",
Boundary, "Content-Disposition: form-data; name=\"simple_sid\"", "", "",
Boundary, "Content-Disposition: form-data; name=\"file\"; filename=\"\"",
"Content-Type: application/octet-stream", "", "",
Boundary, "Content-Disposition: form-data; name=\"simple_searchtype\"", "", "fs",
Boundary, "Content-Disposition: form-data; name=\"maxhits\"", "", '%s',
Boundary].join("\x0d\x0a")

Class Method Summary collapse

Class Method Details

.smiles_search(smiles, maxhits = 100) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/chem/db/pubchem.rb', line 43

def self.smiles_search(smiles, maxhits=100)
  cid = []
  url = ""
  body = ""
  Net::HTTP.version_1_2
  Net::HTTP.start(Host, 80) do |http|
    body = http.post(Searchpath + Query, Data % [smiles, maxhits],
                     {'Content-Type' => "multipart/form-data; boundary=#{Boundary}",
                       'Referer' => "http://pubchem.ncbi.nlm.nih.gov/search/"}).body
    if m = /url="([^"]+)"/.match(body)
      body = http.get(Searchpath + m[1]).body
    end
    while /setTimeout\('document.location.replace\("([^"]+)"\);', (\d+)\)/ =~ body do
      sleep($2.to_f/100)
      response = http.get(URI.parse($1))
      body = response.body
      url = response['location']
    end
    if /PubChem structure search report:(\s|\S)+No hits/ !~ body
      # text format
      url.sub!(/cmd=Select\+from\+History/, 'cmd=Text&dopt=Brief')
      body = http.get(url).body
      body.scan(/\d+: CID: (\d+)/).each do |id|
        cid.push(PubChemEntry.new(id[0].to_i))
      end
      #           # html format
      #           body = http.get(url).body
      #           while /CID: <a href=\"([^"]+)\">(\d+)<\/a>/ =~ body do
      #            cid.push($2)
      #            body = $'
      #           end
    end
  end
  cid
end