ruspider 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/ruspider.rb +4 -0
- data/lib/ruspider/chemspider.rb +46 -0
- data/lib/ruspider/compound.rb +93 -0
- data/lib/ruspider/inchi_api.rb +32 -0
- data/lib/ruspider/mass_spec_api.rb +47 -0
- data/lib/ruspider/rest_client.rb +40 -0
- data/lib/ruspider/spectra_api.rb +63 -0
- data/lib/ruspider/spectrum.rb +55 -0
- data/lib/ruspider/version.rb +5 -0
- data/lib/ruspider/xml_utils.rb +40 -0
- data/ruspider.gemspec +28 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 840e5463640af4659581e42e847cb539f1dbd92a
|
4
|
+
data.tar.gz: f45f02a9efd829fa20e6b6bcef7b8a9bfcf1b029
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e06032c7102dd7344aa76f1cb13a522e685fac0850e381bebf956024cdda958f4275496d62c6fad1136c43fe2fee86a35940589fbf1d19a9e0c2a722b44aa88
|
7
|
+
data.tar.gz: c8cca67eb2bde5d1fa4ffc210af1294af0d1ceecc60ee0ff5e860dac96cec17e8363a50768175dca1e3729a73bfae0289c71c61de8f7eea7044a1b718c5dd913
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.1
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at qn5043@kit.edu. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 An Nguyen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Ruspider
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ruspider`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'ruspider'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install ruspider
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruspider. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ruspider"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/ruspider.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ruspider/rest_client'
|
4
|
+
require 'ruspider/inchi_api'
|
5
|
+
require 'ruspider/mass_spec_api'
|
6
|
+
require 'ruspider/spectra_api'
|
7
|
+
require 'ruspider/compound'
|
8
|
+
require 'ruspider/spectrum'
|
9
|
+
|
10
|
+
module Ruspider
|
11
|
+
# ChemSpider Ruby interface
|
12
|
+
class ChemSpider
|
13
|
+
include InchIAPI
|
14
|
+
include MassSpecAPI
|
15
|
+
include SpectraAPI
|
16
|
+
|
17
|
+
def initialize(token = '', header = {})
|
18
|
+
@rest = RestClient.new(token, header)
|
19
|
+
InchIAPI.instance_method(:initialize).bind(self).call(@rest)
|
20
|
+
MassSpecAPI.instance_method(:initialize).bind(self).call(@rest)
|
21
|
+
SpectraAPI.instance_method(:initialize).bind(self).call(@rest)
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_compound(csid)
|
25
|
+
Compound.new(csid, self)
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_compounds(csids)
|
29
|
+
csids.map { |x| Compound.new(x, self) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_spectrum(spectrum_id)
|
33
|
+
Spectrum.new(spectrum_id, self)
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_spectra(spectrum_ids)
|
37
|
+
spectrum_ids.map { |x| Spectrum.new(x, self) }
|
38
|
+
end
|
39
|
+
|
40
|
+
def get_compound_spectra(csid)
|
41
|
+
get_spectra_info_list([csid]).map do |x|
|
42
|
+
Spectrum.from_info_dict(x, self)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ruspider
|
4
|
+
# ChemSpider Compound Data container and parser
|
5
|
+
class Compound
|
6
|
+
attr_reader :csid
|
7
|
+
|
8
|
+
def initialize(csid, cs)
|
9
|
+
@csid = csid
|
10
|
+
@cs = cs
|
11
|
+
@extended_compound_info = nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def image_url
|
15
|
+
'http://www.chemspider.com/ImagesHandler.ashx?id=%s' + @csid
|
16
|
+
end
|
17
|
+
|
18
|
+
def extended_compound_info
|
19
|
+
if @extended_compound_info.nil?
|
20
|
+
@extended_compound_info = @cs.get_extended_compound_info(@csid)
|
21
|
+
end
|
22
|
+
|
23
|
+
@extended_compound_info
|
24
|
+
end
|
25
|
+
|
26
|
+
def molecular_formula
|
27
|
+
extended_compound_info[:molecular_formula]
|
28
|
+
end
|
29
|
+
|
30
|
+
def smiles
|
31
|
+
extended_compound_info[:smiles]
|
32
|
+
end
|
33
|
+
|
34
|
+
def inchi
|
35
|
+
extended_compound_info[:inchi]
|
36
|
+
end
|
37
|
+
|
38
|
+
def inchikey
|
39
|
+
extended_compound_info[:inchikey]
|
40
|
+
end
|
41
|
+
|
42
|
+
def average_mass
|
43
|
+
extended_compound_info[:average_mass]
|
44
|
+
end
|
45
|
+
|
46
|
+
def molecular_weight
|
47
|
+
extended_compound_info[:molecular_weight]
|
48
|
+
end
|
49
|
+
|
50
|
+
def monoisotopic_mass
|
51
|
+
extended_compound_info[:monoisotopic_mass]
|
52
|
+
end
|
53
|
+
|
54
|
+
def nominal_mass
|
55
|
+
extended_compound_info[:nominal_mass]
|
56
|
+
end
|
57
|
+
|
58
|
+
def alogp
|
59
|
+
extended_compound_info[:alogp]
|
60
|
+
end
|
61
|
+
|
62
|
+
def xlogp
|
63
|
+
extended_compound_info[:xlogp]
|
64
|
+
end
|
65
|
+
|
66
|
+
def common_name
|
67
|
+
extended_compound_info[:common_name]
|
68
|
+
end
|
69
|
+
|
70
|
+
def mol_2d
|
71
|
+
@cs.get_record_mol(@csid, False)
|
72
|
+
end
|
73
|
+
|
74
|
+
def mol_3d
|
75
|
+
@cs.get_record_mol(@csid, True)
|
76
|
+
end
|
77
|
+
|
78
|
+
def mol_raw
|
79
|
+
@cs.get_original_mol(@csid)
|
80
|
+
end
|
81
|
+
|
82
|
+
def image
|
83
|
+
@cs.get_compound_thumbnail(@csid)
|
84
|
+
end
|
85
|
+
|
86
|
+
def spectra
|
87
|
+
spectra_list = @cs.get_spectra_info_list(@csid)
|
88
|
+
spectra_list.map { |s| Spectrum.new(s) }
|
89
|
+
# [Spectrum.from_info_dict(self._cs, info)
|
90
|
+
# for info in self._cs.get_spectra_info_list([self.csid])]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ruspider/rest_client'
|
4
|
+
require 'ruspider/xml_utils'
|
5
|
+
|
6
|
+
module Ruspider
|
7
|
+
# InChI API http://www.chemspider.com/InChI.asmx
|
8
|
+
module InchIAPI
|
9
|
+
include XmlUtils
|
10
|
+
|
11
|
+
API = 'InChi.asmx'
|
12
|
+
|
13
|
+
def initialize(rest)
|
14
|
+
@rest = rest
|
15
|
+
end
|
16
|
+
|
17
|
+
def get_original_mol(csid)
|
18
|
+
response = @rest.post(API, 'CSIDToMol', csid)
|
19
|
+
response.text
|
20
|
+
end
|
21
|
+
|
22
|
+
def inchi_to_csid(inchi)
|
23
|
+
response = @rest.post(API, 'InChIToCSID', inchi: inchi)
|
24
|
+
get_nodes(Nokogiri::XML(response), 'string').first.content.to_i
|
25
|
+
end
|
26
|
+
|
27
|
+
def inchi_key_to_csid(inchi_key)
|
28
|
+
response = @rest.post(API, 'InChIKeyToCSID', inchi_key: inchi_key)
|
29
|
+
get_nodes(Nokogiri::XML(response), 'string').first.content.to_i
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ruspider/rest_client'
|
4
|
+
require 'ruspider/xml_utils'
|
5
|
+
|
6
|
+
module Ruspider
|
7
|
+
# MassSpec API: http://www.chemspider.com/MassSpecAPI.asmx
|
8
|
+
module MassSpecAPI
|
9
|
+
include XmlUtils
|
10
|
+
|
11
|
+
API = 'MassSpecAPI.asmx'
|
12
|
+
|
13
|
+
FIELDS = {
|
14
|
+
CSID: 'csid',
|
15
|
+
MF: 'molecular_formula',
|
16
|
+
SMILES: 'smiles',
|
17
|
+
InChI: 'inchi',
|
18
|
+
InChIKey: 'inchikey',
|
19
|
+
AverageMass: 'average_mass',
|
20
|
+
MolecularWeight: 'molecular_weight',
|
21
|
+
MonoisotopicMass: 'monoisotopic_mass',
|
22
|
+
NominalMass: 'nominal_mass',
|
23
|
+
ALogP: 'alogp',
|
24
|
+
XLogP: 'xlogp'
|
25
|
+
}.freeze
|
26
|
+
|
27
|
+
def initialize(rest)
|
28
|
+
@rest = rest
|
29
|
+
end
|
30
|
+
|
31
|
+
def databases
|
32
|
+
response = @rest.post(API, 'GetDatabases')
|
33
|
+
array_from_xml(response, 'ArrayOfString', 'string')
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_extended_compound_info(csid)
|
37
|
+
response = @rest.post(API, 'GetExtendedCompoundInfo', csid: csid)
|
38
|
+
hash_from_string(response, 'ExtendedCompoundInfo', FIELDS)
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_extended_compound_info_list(csids)
|
42
|
+
response = @rest.post(API, 'GetExtendedCompoundInfoArray', csids: csids)
|
43
|
+
nodes = get_nodes(response)
|
44
|
+
nodes.map { |node| hash_from_xml(node, FIELDS) }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'httparty'
|
4
|
+
|
5
|
+
module Ruspider
|
6
|
+
# ChemSpider REST Webservice API Client
|
7
|
+
class RestClient
|
8
|
+
include HTTParty
|
9
|
+
|
10
|
+
HOST_NAME = 'www.chemspider.com'
|
11
|
+
|
12
|
+
def initialize(token, header = {})
|
13
|
+
@token = token
|
14
|
+
@header = header
|
15
|
+
end
|
16
|
+
|
17
|
+
def query_string_normalizer(query)
|
18
|
+
query.to_hash.map do |k, v|
|
19
|
+
if v.class == Array
|
20
|
+
v.map { |x| "#{k}=#{x}" }.join('&')
|
21
|
+
else
|
22
|
+
HTTParty::HashConversions.normalize_param(k, v)
|
23
|
+
end
|
24
|
+
end.join
|
25
|
+
end
|
26
|
+
|
27
|
+
def post(api, op, options = {})
|
28
|
+
return -1 if op.empty?
|
29
|
+
ops = { token: @token }.merge(options)
|
30
|
+
res = HTTParty.post(
|
31
|
+
"https://#{HOST_NAME}/#{api}/#{op}",
|
32
|
+
header: { header: @header }, body: ops,
|
33
|
+
query_string_normalizer: ->(h) { query_string_normalizer(h) }
|
34
|
+
)
|
35
|
+
|
36
|
+
raise "#{res.response.code}: #{res.parsed_response}" unless res.success?
|
37
|
+
res.response.body
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ruspider/rest_client'
|
4
|
+
|
5
|
+
module Ruspider
|
6
|
+
# SpectraAPI
|
7
|
+
module SpectraAPI
|
8
|
+
API = 'Spectra.asmx'
|
9
|
+
|
10
|
+
FIELDS = {
|
11
|
+
CommonName: 'common_name',
|
12
|
+
MOL2d: 'mol_2d',
|
13
|
+
MOL3d: 'mol_3d',
|
14
|
+
ReferenceCount: 'reference_count',
|
15
|
+
DataSourceCount: 'datasource_count',
|
16
|
+
PubMedCount: 'pubmed_count',
|
17
|
+
RSCCount: 'rsc_count',
|
18
|
+
ExternalReferences: 'external_references',
|
19
|
+
ds_name: 'datasource_name',
|
20
|
+
ds_url: 'datasource_url',
|
21
|
+
ext_id: 'external_id',
|
22
|
+
ext_url: 'external_url',
|
23
|
+
Status: 'status',
|
24
|
+
Count: 'count',
|
25
|
+
Message: 'message',
|
26
|
+
Elapsed: 'elapsed',
|
27
|
+
spc_id: 'spectrum_id',
|
28
|
+
spc_type: 'spectrum_type',
|
29
|
+
file_name: 'file_name',
|
30
|
+
comments: 'comments',
|
31
|
+
original_url: 'original_url',
|
32
|
+
submitted_date: 'submitted_date'
|
33
|
+
}.freeze
|
34
|
+
|
35
|
+
def initialize(rest)
|
36
|
+
@rest = rest
|
37
|
+
end
|
38
|
+
|
39
|
+
def all_spectra_info
|
40
|
+
# response = post('Spectra', 'GetAllSpectraInfo')
|
41
|
+
# hash_from_xml(response, FIELDS)
|
42
|
+
end
|
43
|
+
|
44
|
+
def get_spectrum_info(spectrum_id)
|
45
|
+
# response = post('Spectra', 'GetSpectrumInfo', spectrum_id)
|
46
|
+
# hash_from_xml(response, FIELDS)
|
47
|
+
end
|
48
|
+
|
49
|
+
def get_compound_spectra_info(csid)
|
50
|
+
# response = post('Spectra', 'GetCompoundSpectraInfo', csid=csid)
|
51
|
+
# hash_from_xml(response, FIELDS)
|
52
|
+
end
|
53
|
+
|
54
|
+
def get_spectra_info_list(csids)
|
55
|
+
# response = post('Spectra', 'GetSpectraInfoArray', csids=csids)
|
56
|
+
# hash_from_xml(response, FIELDS)
|
57
|
+
end
|
58
|
+
|
59
|
+
def all_spectra
|
60
|
+
all_spectra_info.map { |x| Spectrum.from_hash(x) }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Ruspider
|
4
|
+
# ChemSpider Spectrym Data container and parser
|
5
|
+
class Spectrum
|
6
|
+
def initialize(cs, spectrum_id)
|
7
|
+
@cs = cs
|
8
|
+
@spectrum_info[:id] = spectrum_id
|
9
|
+
end
|
10
|
+
|
11
|
+
def spectrum_info
|
12
|
+
if spectrum_info.file_name.nil?
|
13
|
+
@spectrum_info = @cs.get_spectrum_info(@spectrum_info.id)
|
14
|
+
end
|
15
|
+
|
16
|
+
@spectrum_info
|
17
|
+
end
|
18
|
+
|
19
|
+
def spectrum_id
|
20
|
+
spectrum_info[:id]
|
21
|
+
end
|
22
|
+
|
23
|
+
def csid
|
24
|
+
spectrum_info[:csid]
|
25
|
+
end
|
26
|
+
|
27
|
+
def spectrum_type
|
28
|
+
spectrum_info[:spectrum_type]
|
29
|
+
end
|
30
|
+
|
31
|
+
def file_name
|
32
|
+
spectrum_info[:file_name]
|
33
|
+
end
|
34
|
+
|
35
|
+
def comments
|
36
|
+
spectrum_info[:comments]
|
37
|
+
end
|
38
|
+
|
39
|
+
def url
|
40
|
+
'https://www.chemspider.com/FilesHandler.ashx?type=blob&disp=1&id=%s' +
|
41
|
+
spectrum_info[:id]
|
42
|
+
end
|
43
|
+
|
44
|
+
def data
|
45
|
+
end
|
46
|
+
|
47
|
+
def original_url
|
48
|
+
spectrum_info.get('original_url')
|
49
|
+
end
|
50
|
+
|
51
|
+
def submitted_date
|
52
|
+
spectrum_info[:submitted_date]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'nokogiri'
|
4
|
+
|
5
|
+
module Ruspider
|
6
|
+
# XML utilities
|
7
|
+
module XmlUtils
|
8
|
+
def get_nodes(xml, node_name)
|
9
|
+
xml.xpath(
|
10
|
+
"//cs:#{node_name}",
|
11
|
+
'cs' => 'http://www.chemspider.com/'
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
def get_first_node(xml, node_name)
|
16
|
+
get_nodes(xml, node_name).first
|
17
|
+
end
|
18
|
+
|
19
|
+
def hash_from_xml(xml, field)
|
20
|
+
hash = {}
|
21
|
+
field.each do |k, v|
|
22
|
+
node = xml.xpath("//cs:#{k}", 'cs' => 'http://www.chemspider.com/')
|
23
|
+
hash[v.to_sym] = node.first.content
|
24
|
+
end
|
25
|
+
|
26
|
+
hash
|
27
|
+
end
|
28
|
+
|
29
|
+
def hash_from_string(xml_str, obj_name, field)
|
30
|
+
xml = get_first_node(Nokogiri::XML(xml_str), obj_name)
|
31
|
+
hash_from_xml(xml, field)
|
32
|
+
end
|
33
|
+
|
34
|
+
def array_from_xml(xml_str, obj_name, field)
|
35
|
+
xml = get_nodes(Nokogiri::XML(xml_str), obj_name)
|
36
|
+
arr = xml.xpath("//cs:#{field}", 'cs' => 'http://www.chemspider.com/')
|
37
|
+
arr.map(&:content)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/ruspider.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'ruspider/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'ruspider'
|
9
|
+
spec.version = Ruspider::VERSION
|
10
|
+
spec.authors = ['An Nguyen']
|
11
|
+
spec.email = ['an.nguyen@kit.edu']
|
12
|
+
|
13
|
+
spec.summary = 'Ruby gem for ChemSpider'
|
14
|
+
spec.description = 'A wrapper for ChemSpider API'
|
15
|
+
spec.homepage = 'https://github.com/complat/ruspider'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruspider
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- An Nguyen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: A wrapper for ChemSpider API
|
56
|
+
email:
|
57
|
+
- an.nguyen@kit.edu
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".ruby-version"
|
65
|
+
- ".travis.yml"
|
66
|
+
- CODE_OF_CONDUCT.md
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/ruspider.rb
|
74
|
+
- lib/ruspider/chemspider.rb
|
75
|
+
- lib/ruspider/compound.rb
|
76
|
+
- lib/ruspider/inchi_api.rb
|
77
|
+
- lib/ruspider/mass_spec_api.rb
|
78
|
+
- lib/ruspider/rest_client.rb
|
79
|
+
- lib/ruspider/spectra_api.rb
|
80
|
+
- lib/ruspider/spectrum.rb
|
81
|
+
- lib/ruspider/version.rb
|
82
|
+
- lib/ruspider/xml_utils.rb
|
83
|
+
- ruspider.gemspec
|
84
|
+
homepage: https://github.com/complat/ruspider
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata: {}
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.6.12
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Ruby gem for ChemSpider
|
108
|
+
test_files: []
|