/
entrez.go
684 lines (608 loc) · 15.8 KB
/
entrez.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
package stats
import (
"bytes"
"fmt"
"github.com/biogo/ncbi"
"github.com/biogo/ncbi/entrez"
"github.com/biogo/ncbi/entrez/search"
"github.com/hscells/cqr"
"github.com/hscells/groove/pipeline"
"github.com/hscells/guru"
"github.com/hscells/transmute"
"github.com/hscells/transmute/backend"
"github.com/hscells/trecresults"
"github.com/jdkato/prose/v2"
"github.com/mailru/easyjson"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
type EntrezStatisticsSource struct {
Limit int
tool string
key string
email string
db string
parameters map[string]float64
rank bool
options SearchOptions
// The size of PubMed.
N float64
}
type term struct {
count int
token string
}
//type EntrezDocument struct {
// ID string
// Title string
// Text string
// MeSHHeadings []string
// PublicationTypes []string
//}
func formatTerm(term string) string {
buff := bytes.NewBufferString("")
for _, c := range term {
if c == '[' {
return buff.String()
}
buff.WriteRune(c)
}
return buff.String()
}
func extractTerms(node search.Node) (terms []term) {
switch n := node.(type) {
case *search.Op:
for _, o := range n.Operands {
t := extractTerms(o)
terms = append(terms, t...)
}
case *search.Term:
terms = append(terms, term{count: n.Count, token: formatTerm(n.Term)})
}
return
}
func mapTerms(terms []term) map[string]float64 {
m := make(map[string]float64)
for _, term := range terms {
m[term.token] = float64(term.count)
}
return m
}
type Search struct {
Count int `xml:"Count"`
}
func (e EntrezStatisticsSource) SetDB(db string) EntrezStatisticsSource {
e.db = db
return e
}
func (e EntrezStatisticsSource) Count(term, field string) float64 {
var s Search
count:
err := entrez.SearchURL.GetXML(map[string][]string{"field": {field}, "api_key": {e.key}, "term": {term}}, e.tool, e.email, entrez.Limit, &s)
if err != nil {
log.Println(err)
goto count
}
return float64(s.Count)
}
func (e EntrezStatisticsSource) SearchStart(n int) func(p *entrez.Parameters) {
return func(p *entrez.Parameters) {
p.RetStart = n
}
}
func (e EntrezStatisticsSource) SearchSize(n int) func(p *entrez.Parameters) {
return func(p *entrez.Parameters) {
p.RetMax = n
}
}
// fillParams adds elements to v based on the "param" tag of p if the value is not the
// zero value for that type.
func fillParams(p *entrez.Parameters, v url.Values) {
if p == nil {
return
}
pv := reflect.ValueOf(p).Elem()
n := pv.NumField()
t := pv.Type()
for i := 0; i < n; i++ {
tf := t.Field(i)
if tf.PkgPath != "" && !tf.Anonymous {
continue
}
tag := tf.Tag.Get("param")
if tag != "" {
in := pv.Field(i).Interface()
switch cv := in.(type) {
case int:
if cv != 0 {
v[tag] = []string{fmt.Sprint(cv)}
}
case string:
if cv != "" {
v[tag] = []string{cv}
}
default:
panic("cannot reach")
}
}
}
}
type esearch struct {
EsearchResult esearchresult `json:"esearchresult"`
}
type esearchresult struct {
RetStart string `json:"retstart"`
Count string `json:"count"`
Idlist []string `json:"idlist"`
}
// Search uses the entrez eutils to get the pmids for a given query.
func (e EntrezStatisticsSource) Search(query string, options ...func(p *entrez.Parameters)) ([]int, error) {
ncbi.SetTimeout(10 * time.Minute)
//fmt.Printf("%s", query)
p := &entrez.Parameters{}
p.RetMax = e.options.Size
for _, option := range options {
option(p)
}
if e.options.Size == 0 {
e.options.Size = 100000
p.RetMax = e.options.Size
}
p.APIKey = e.key
p.RetMode = "json"
if e.rank {
p.Sort = "rank"
p.RetMax = e.options.Size
}
v := url.Values{}
v["db"] = []string{e.db}
v["term"] = []string{query}
fillParams(p, v)
fmt.Print(".")
fails, nfails := 20, 20
search:
r, err := entrez.SearchURL.Get(v, e.tool, e.email, entrez.Limit)
if err != nil {
if fails > 0 {
log.Printf("search error: %v, retrying %d more times for %s", err, fails, time.Duration(((nfails-fails)*5)*int(time.Second)))
fails--
time.Sleep(time.Duration((nfails-fails)*5) * time.Second)
goto search
}
goto search
}
defer r.Close()
fmt.Print(".")
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
var s esearch
fmt.Print(".")
err = easyjson.Unmarshal(b, &s)
if err != nil {
fmt.Println(string(b))
return nil, err
}
fmt.Print(".")
pmids := make([]int, len(s.EsearchResult.Idlist))
for i, pmid := range s.EsearchResult.Idlist {
pmids[i], err = strconv.Atoi(pmid)
if err != nil {
fmt.Println(string(b))
return nil, err
}
}
retstart, err := strconv.Atoi(s.EsearchResult.RetStart)
if err != nil {
fmt.Println(string(b))
return nil, err
}
fmt.Printf("[%d/%s]", retstart+len(pmids), s.EsearchResult.Count)
// If the number of pmids equals the execute size, there might be more to come.
if e.rank || (e.Limit > 0 && len(pmids) >= e.Limit) {
return pmids, nil
} else if len(pmids) == e.options.Size {
fails, nfails := 20, 20
retry:
l, err := e.Search(query, e.SearchStart(p.RetStart+len(pmids)), e.SearchSize(e.SearchOptions().Size))
if err != nil {
if fails > 0 {
log.Printf("recursive search error: %v, retrying %d more times for %f seconds", err, fails, time.Duration(((nfails-fails)*5)*int(time.Second)).Seconds())
fails--
time.Sleep(time.Duration((nfails-fails)*5) * time.Second)
goto retry
}
return nil, err
}
pmids = append(pmids, l...)
}
return pmids, nil
}
// Summary uses the entrez eutils to obtain summary documents for the ids.
func (e EntrezStatisticsSource) Summary(ids []string, value interface{}, options ...func(p *entrez.Parameters)) error {
p := &entrez.Parameters{}
for _, option := range options {
option(p)
}
v := url.Values{}
v["db"] = []string{e.db}
v["id"] = []string{strings.Join(ids, ",")}
fillParams(p, v)
p.RetMax = e.options.Size
p.RetMode = "xml"
p.APIKey = e.key
return entrez.SummaryURL.GetXML(v, e.tool, e.email, entrez.Limit, value)
}
// Fetch uses the entrez eutils to fetch the pubmed Article given a set of pubmed identifiers.
func (e EntrezStatisticsSource) Fetch(pmids []int, options ...func(p *entrez.Parameters)) ([]guru.MedlineDocument, error) {
if len(pmids) == 0 {
return guru.MedlineDocuments{}, nil
}
p := &entrez.Parameters{}
//p.RetMode = "asn.1"
p.RetMode = "text"
p.RetType = "medline"
p.APIKey = e.key
for _, option := range options {
option(p)
}
fails, nfails := 20, 20
retry:
r, err := entrez.Fetch(e.db, p, e.tool, e.email, nil, pmids...)
if err != nil {
if fails > 0 {
log.Printf("fetch error: %v, retrying %d more times for %f seconds", err, fails, time.Duration(((nfails-fails)*5)*int(time.Second)).Seconds())
fails--
time.Sleep(time.Duration((nfails-fails)*5) * time.Second)
goto retry
}
return nil, err
}
//log.Println("reading")
//ncbi.SetTimeout(1 * time.Hour
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
//d := make(map[string]interface{})
//s, err := asn1.Unmarshal(b, &d)
//if err != nil {
// return nil, err
//}
//
//fmt.Println(s)
//log.Println("unmarshalling")
//s := guru.UnmarshalAbstract(bytes.NewReader(b))
s := guru.UnmarshalMedline(bytes.NewReader(b))
//log.Println("done")
return s, r.Close()
}
func (e EntrezStatisticsSource) Link(pmids []int, linkname string) ([]int, error) {
link, err := entrez.DoLink(e.db, "pubmed", "neighbor", "", &entrez.Parameters{
LinkName: linkname,
}, e.tool, e.email, nil, pmids)
if err != nil {
return nil, err
}
var links []int
for _, ls := range link.LinkSets {
for _, n := range ls.Neighbor {
for _, l := range n.Link {
links = append(links, l.Id.Id)
}
}
}
return links, nil
}
func (e EntrezStatisticsSource) SearchOptions() SearchOptions {
return e.options
}
func (e EntrezStatisticsSource) Parameters() map[string]float64 {
return e.parameters
}
func (e EntrezStatisticsSource) TermFrequency(term, field, document string) (float64, error) {
d, err := strconv.ParseInt(document, 10, 64)
if err != nil {
return 0, err
}
r, err := entrez.Fetch(e.db, &entrez.Parameters{RetMode: "xml", APIKey: e.key}, e.tool, e.email, nil, int(d))
if err != nil {
return 0, err
}
defer r.Close()
docs := guru.UnmarshalMedline(r)
if len(docs) == 0 {
return 0, nil
}
var n int
t := docs[0].TI
a := docs[0].AB
n += strings.Count(fmt.Sprintf("%s %s", t, a), term)
return float64(n), nil
}
func (e EntrezStatisticsSource) TermVector(document string) (TermVector, error) {
// Get the pmid as an int.
d, err := strconv.ParseInt(document, 10, 64)
if err != nil {
return nil, err
}
docs, err := e.Fetch([]int{int(d)})
if err != nil {
return nil, err
}
// Do not continue if we did not retrieve anything.
if len(docs) == 0 {
return nil, nil
}
// Extract the title and the Abstract.
doc, err := prose.NewDocument(strings.ToLower(strings.Join([]string{docs[0].TI, docs[0].AB}, ". ")))
if err != nil {
return nil, err
}
var terms []string
for _, tok := range doc.Tokens() {
switch tok.Tag {
case "JJ", "JJR", "JJS", "NN", "NNP", "NNPS", "NNS":
terms = append(terms, tok.Text)
default:
continue
}
}
// Create the term vector and populate it with all the statistics.
termFrequencies := make(map[string]float64)
for _, term := range terms {
for _, t := range terms {
if t == term {
termFrequencies[t]++
}
}
}
ch := make(chan TermVectorTerm)
var vec TermVector
var wg sync.WaitGroup
go func() {
for tv := range ch {
vec = append(vec, tv)
}
}()
for term, tf := range termFrequencies {
wg.Add(1)
//go func(x string, y float64) {
log.Println(term)
s := e.Count(term, "tiab")
ch <- TermVectorTerm{
DocumentFrequency: s,
TotalTermFrequency: s,
TermFrequency: tf,
Field: "tiab",
Term: term,
}
wg.Done()
//}(term, tf)
}
wg.Wait()
close(ch)
return vec, nil
}
func (e EntrezStatisticsSource) DocumentFrequency(term, field string) (float64, error) {
s, err := entrez.DoSearch(e.db, term, &entrez.Parameters{APIKey: e.key}, nil, e.tool, e.email)
if err != nil {
return 0, err
}
return float64(s.Count), nil
}
func (e EntrezStatisticsSource) TotalTermFrequency(term, _ string) (float64, error) {
pmids, err := e.Search(fmt.Sprintf("%s[Title/Abstract]", term))
if err != nil {
return 0, err
}
if len(pmids) == 0 {
return 0, nil
}
docs, err := e.Fetch(pmids)
if err != nil {
return 0, err
}
var n int
for _, doc := range docs {
t := strings.ToLower(doc.TI)
a := strings.ToLower(doc.AB)
n += strings.Count(fmt.Sprintf("%s %s", t, a), term)
}
return float64(n), nil
}
func (e EntrezStatisticsSource) InverseDocumentFrequency(term, field string) (float64, error) {
nt := e.Count(term, field)
return idf(e.N, nt), nil
}
func (e EntrezStatisticsSource) RetrievalSize(query cqr.CommonQueryRepresentation) (float64, error) {
// First we need to transform the query into a PubMed query (suitable for entrez)
d, err := backend.NewCQRQuery(query).String()
if err != nil {
return 0, err
}
bq, err := transmute.Cqr2Pubmed.Execute(d)
if err != nil {
return 0, err
}
q, err := bq.String()
if err != nil {
return 0, err
}
retry:
entrez.Limit.Wait()
c := &http.Client{}
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&rettype=count&term=%s&api_key=%s&tool=%s&email=%s", url.QueryEscape(q), e.key, e.tool, e.email), nil)
if err != nil {
return 0, err
}
resp, err := c.Do(req)
if err != nil {
return 0, err
}
if resp.StatusCode != http.StatusOK {
goto retry
}
var buff bytes.Buffer
_, err = buff.ReadFrom(resp.Body)
if err != nil {
return 0, err
}
re := regexp.MustCompile("<Count>(?P<count>[0-9]+)</Count>")
matches := re.FindSubmatch(buff.Bytes())
if len(matches) >= 2 {
return strconv.ParseFloat(string(bytes.TrimSpace(matches[1])), 32)
}
return 0, nil
}
func (e EntrezStatisticsSource) VocabularySize(field string) (float64, error) {
i, err := entrez.DoInfo(e.db, e.tool, e.email)
if err != nil {
return 0, err
}
for _, f := range i.DbInfo.FieldList {
if f.Name == field {
return float64(f.TermCount), nil
}
}
return 0, nil
}
func (e EntrezStatisticsSource) Execute(query pipeline.Query, options SearchOptions) (trecresults.ResultList, error) {
// First we need to transform the query into a PubMed query (suitable for entrez)
d, err := backend.NewCQRQuery(query.Query).String()
if err != nil {
return nil, err
}
bq, err := transmute.Cqr2Pubmed.Execute(d)
if err != nil {
return nil, err
}
q, err := bq.String()
if err != nil {
return nil, err
}
fails := 20
execute:
pmids, err := e.Search(q)
if err != nil {
if fails > 0 {
log.Printf("search execution error: %v, retrying %d more times for query: %s", err, fails, q)
fails--
time.Sleep(5 * time.Second)
goto execute
}
return nil, err
}
r := make(trecresults.ResultList, len(pmids))
for i, pmid := range pmids {
r[i] = &trecresults.Result{
DocId: strconv.Itoa(pmid),
RunName: e.options.RunName,
Topic: query.Topic,
}
}
return r, nil
}
var pmCollectionSize float64
func (e EntrezStatisticsSource) CollectionSize() (float64, error) {
if e.N > 0 {
return e.N, nil
}
info, err := entrez.DoInfo(e.db, e.tool, e.email)
if err != nil {
return 0, err
}
if pmCollectionSize == 0 {
pmCollectionSize = float64(info.DbInfo.Count)
}
return pmCollectionSize, nil
}
func (e EntrezStatisticsSource) Translation(term string) ([]string, error) {
s, err := entrez.DoSearch("pubmed", term, nil, nil, e.tool, e.email)
if err != io.EOF && err != nil {
return nil, err
}
if s == nil || len(s.TranslationStack) == 0 {
return nil, nil
}
var translations []string
_, nodes := s.TranslationStack[0].Consume(s.TranslationStack)
for _, node := range nodes {
if t, ok := node.(*search.Term); ok {
translations = append(translations, strings.ReplaceAll(strings.ToLower(t.Term), "[all fields]", ""))
}
}
return translations, nil
}
// EntrezTool sets the tool name for entrez.
func EntrezTool(tool string) func(source *EntrezStatisticsSource) {
return func(source *EntrezStatisticsSource) {
source.tool = tool
}
}
// EntrezTool sets the email for entrez.
func EntrezEmail(email string) func(source *EntrezStatisticsSource) {
return func(source *EntrezStatisticsSource) {
source.email = email
}
}
// EntrezTool sets the API key for entrez.
func EntrezAPIKey(key string) func(source *EntrezStatisticsSource) {
return func(source *EntrezStatisticsSource) {
source.key = key
}
}
// EntrezOptions sets any additional options for the entrez statistics source.
func EntrezOptions(options SearchOptions) func(source *EntrezStatisticsSource) {
return func(source *EntrezStatisticsSource) {
source.options = options
}
}
// EntrezOptions sets any additional options for the entrez statistics source.
func EntrezLimiter(limit time.Duration) func(source *EntrezStatisticsSource) {
return func(source *EntrezStatisticsSource) {
entrez.Limit = ncbi.NewLimiter(limit)
}
}
// EntrezDb sets the database to search.
func EntrezDb(db string) func(source *EntrezStatisticsSource) {
return func(source *EntrezStatisticsSource) {
source.db = db
}
}
func EntrezRank(rank bool) func(source *EntrezStatisticsSource) {
return func(source *EntrezStatisticsSource) {
source.rank = rank
}
}
// NewEntrezStatisticsSource creates a new entrez statistics source for searching pubmed.
// When an API key is specified, the entrez request Limit is raised to 10 per second instead of the default 3.
func NewEntrezStatisticsSource(options ...func(source *EntrezStatisticsSource)) (EntrezStatisticsSource, error) {
e := &EntrezStatisticsSource{
db: "pubmed",
rank: false,
}
//if len(e.key) > 0 {
entrez.Limit = ncbi.NewLimiter(time.Second / 9)
//}
//ncbi.SetTimeout(0)
for _, option := range options {
option(e)
}
var err error
e.N, err = e.CollectionSize()
if err != nil {
return EntrezStatisticsSource{}, err
}
return *e, nil
}