1
|
#!/usr/bin/env python
|
2
|
# Scrubs the taxonlabels in VegBIEN using TNRS.
|
3
|
# Runs continuously until no new rows are added after max_pause.
|
4
|
|
5
|
import os.path
|
6
|
import StringIO
|
7
|
import sys
|
8
|
import time
|
9
|
|
10
|
sys.path.append(os.path.dirname(__file__)+"/../lib")
|
11
|
|
12
|
import csvs
|
13
|
import opts
|
14
|
import profiling
|
15
|
import sql
|
16
|
import sql_gen
|
17
|
import sql_io
|
18
|
import streams
|
19
|
import strings
|
20
|
import tnrs
|
21
|
|
22
|
# Config
|
23
|
pause = 2*60*60 # sec; = 2 hr
|
24
|
max_pause = 9*60*60 # sec; = 9 hr; must be >= max partition import time (1.5 hr)
|
25
|
assert pause <= max_pause
|
26
|
|
27
|
tnrs_data = sql_gen.Table('tnrs')
|
28
|
|
29
|
def main():
|
30
|
# Input
|
31
|
env_names = []
|
32
|
db_config = opts.get_env_vars(sql.db_config_names, None, env_names)
|
33
|
verbosity = float(opts.get_env_var('verbosity', 3, env_names))
|
34
|
wait = opts.env_flag('wait', False, env_names)
|
35
|
if not 'engine' in db_config: raise SystemExit('Usage: '
|
36
|
+opts.env_usage(env_names)+' '+sys.argv[0]+' 2>>log')
|
37
|
|
38
|
def log(msg, level=1):
|
39
|
'''Higher level -> more verbose'''
|
40
|
if level <= verbosity:
|
41
|
sys.stderr.write(strings.to_raw_str(msg.rstrip('\n')+'\n'))
|
42
|
|
43
|
# Connect to DB
|
44
|
db = sql.connect(db_config, log_debug=log)
|
45
|
|
46
|
tnrs_profiler = profiling.ItersProfiler(iter_text='name')
|
47
|
|
48
|
# Iterate over unscrubbed verbatim taxonlabels
|
49
|
total_pause = 0
|
50
|
tables = ['taxonlabel', sql_gen.Join('tnrs',
|
51
|
{'Name_submitted': 'identifyingtaxonomicname'}, sql_gen.filter_out)]
|
52
|
cols = ['identifyingtaxonomicname']
|
53
|
# Has a concatenated name and not already linked to an accepted name
|
54
|
conds = [('identifyingtaxonomicname', sql_gen.CompareCond(None, '!=')),
|
55
|
('matched_label_id', None)]
|
56
|
while True:
|
57
|
# Fetch next set
|
58
|
cur = sql.select(db, tables, cols, conds, cols, limit=tnrs.max_names,
|
59
|
cacheable=False)
|
60
|
this_ct = cur.rowcount
|
61
|
log('Processing '+str(this_ct)+' taxonlabels')
|
62
|
if this_ct == 0:
|
63
|
if not wait: break
|
64
|
log('Waited '+str(total_pause)+' sec total')
|
65
|
total_pause += pause
|
66
|
if total_pause > max_pause: break
|
67
|
log('Waiting '+str(pause)+' sec...')
|
68
|
time.sleep(pause) # wait for more rows
|
69
|
continue # try again
|
70
|
# otherwise, rows found
|
71
|
total_pause = 0
|
72
|
names = list(sql.values(cur))
|
73
|
|
74
|
# Run TNRS
|
75
|
log('Making TNRS request')
|
76
|
tnrs_profiler.start()
|
77
|
try:
|
78
|
try: stream = tnrs.repeated_tnrs_request(names)
|
79
|
finally:
|
80
|
tnrs_profiler.stop(iter_ct=this_ct)
|
81
|
log('Cumulatively: '+tnrs_profiler.msg())
|
82
|
except tnrs.InvalidResponse: pass # skip set in case it caused error
|
83
|
else:
|
84
|
log('Storing TNRS response data')
|
85
|
sql_io.append_csv(db, tnrs_data, *csvs.reader_and_header(stream))
|
86
|
|
87
|
main()
|