entrez 0.5.5 → 0.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,7 +17,21 @@ See 'Email & Tool' section below for setup.
17
17
 
18
18
  == Usage
19
19
 
20
- (You can copy/paste the resulting following request URLs into a browser to see what the response would be.)
20
+ Supported Utilities
21
+
22
+ * EFetch
23
+ * EInfo
24
+ * ESearch
25
+ * ESummary
26
+
27
+ Not yet implemented
28
+
29
+ * EPost
30
+ * ELink
31
+ * EGQuery
32
+ * ESpell
33
+
34
+ You can copy/paste the resulting following request URLs into a browser to see what the response would be.
21
35
 
22
36
  === EFetch, ESummary
23
37
 
@@ -39,10 +53,17 @@ args:
39
53
  * params hash (optional)
40
54
 
41
55
  Entrez.ESearch('genomeprj', {WORD: 'hapmap', SEQS: 'inprogress'}, retmode: :xml)
42
- #=> makes request to http://http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=genomeprj&term=hapmap[WORD]+AND+inprogress[SEQS]&retmode=xml.
56
+ #=> makes request to http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=genomeprj&term=hapmap[WORD]+AND+inprogress[SEQS]&retmode=xml.
43
57
  #=> returns XML document with list of Ids of genome projects that match the searc term criteria.
44
58
  #=> i.e. genome projects that have 'hapmap' in the description and whose sequencing status is 'inprogress'.
45
59
 
60
+ ==== Customized Queries
61
+
62
+ You can build your own customized queries if you have something more complex with ANDs and ORs.
63
+ Use Entrez.convert_search_term_hash() to help you.
64
+ It converts a hash into a valid Entrez search string properly joined with the operator of your choosing.
65
+ If you pass in the OR operator, the returned search string will be wrapped in a set of parentheses.
66
+
46
67
  === EInfo
47
68
 
48
69
  args:
@@ -67,14 +88,6 @@ NCBI recommends no more than 3 URL requests per second: http://www.ncbi.nlm.nih.
67
88
  This gem respects this limit. It will delay the next request if the last 3 have been made within 1 second.
68
89
  The amount of delay time is no more than what is necessary to make the next request "respectful".
69
90
 
70
- == Supported Utilities
71
-
72
- * EFetch
73
- * ESummary
74
- * ESearch
75
-
76
- Not yet implemented: EPost, ELink, EGQuery, ESpell.
77
-
78
91
  == Compatibility
79
92
 
80
93
  http://test.rubygems.org/gems/entrez
@@ -41,6 +41,21 @@ class Entrez
41
41
  get utility_path, :query => {db: db}.merge(params)
42
42
  end
43
43
 
44
+ # Take a ruby hash and convert it to an ENTREZ search term.
45
+ # E.g. convert_search_term_hash {WORD: 'low coverage', SEQS: 'inprogress'}
46
+ # #=> 'low coverage[WORD]+AND+inprogress[SEQS]'
47
+ def convert_search_term_hash(hash, operator = 'AND')
48
+ raise UnknownOperator.new(operator) unless ['AND', 'OR'].include?(operator)
49
+ str = hash.map do |field, value|
50
+ value = value.join(',') if value.is_a?(Array)
51
+ "#{value}[#{field}]"
52
+ end.join("+#{operator}+")
53
+ if operator == 'OR'
54
+ str = "(#{str})"
55
+ end
56
+ str
57
+ end
58
+
44
59
  private
45
60
 
46
61
  def respect_query_limit
@@ -59,16 +74,6 @@ class Entrez
59
74
  @request_times ||= []
60
75
  end
61
76
 
62
- # Take a ruby hash and convert it to an ENTREZ search term.
63
- # E.g. convert_search_term_hash {WORD: 'low coverage', SEQS: 'inprogress'}
64
- # #=> 'low coverage[WORD]+AND+inprogress[SEQS]'
65
- def convert_search_term_hash(hash)
66
- hash.map do |field, value|
67
- value = value.join(',') if value.is_a?(Array)
68
- "#{value}[#{field}]"
69
- end.join('+AND+')
70
- end
71
-
72
77
  # Define ids() method which will parse and return the IDs from the XML response.
73
78
  def parse_ids_and_extend(response)
74
79
  response.instance_eval do
@@ -87,4 +92,10 @@ class Entrez
87
92
 
88
93
  end
89
94
 
95
+ class UnknownOperator < StandardError
96
+ def initialize(operator)
97
+ super "Unknown operator: #{operator}"
98
+ end
99
+ end
100
+
90
101
  end
@@ -1,3 +1,3 @@
1
1
  module Entrez
2
- VERSION = "0.5.5"
2
+ VERSION = "0.5.6"
3
3
  end
@@ -62,4 +62,14 @@ describe Entrez do
62
62
  requests.should take_longer_than(1.0)
63
63
  end
64
64
 
65
+ it 'should convert search term hash into query string with AND operator by default' do
66
+ query_string = {TITL: 'BRCA1', ORGN: 'human'}
67
+ Entrez.convert_search_term_hash(query_string).should == 'BRCA1[TITL]+AND+human[ORGN]'
68
+ end
69
+
70
+ it 'should convert search term hash into query string with OR operator with parentheses' do
71
+ query_string = {TITL: 'BRCA1', ORGN: 'human'}
72
+ Entrez.convert_search_term_hash(query_string, 'OR').should == '(BRCA1[TITL]+OR+human[ORGN])'
73
+ end
74
+
65
75
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: entrez
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.5.5
5
+ version: 0.5.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jared Ning
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-31 00:00:00 -05:00
13
+ date: 2011-07-20 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency