Project

General

Profile

1
# TNRS
2

    
3
import json
4
import os.path
5
import re
6
import sys
7
import time
8
import urllib2
9

    
10
import csvs
11
import exc
12
import profiling
13
import streams
14
import strings
15

    
16
# Config
17
initial_pause = 0.35 # sec
18
pause_growth_factor = 1.3
19
max_pause = 30*60 # sec; = 30 min; TNRS sometimes freezes for ~10 min
20
assert initial_pause <= max_pause
21
#max_names = 5000 #according to http://tnrs.iplantcollaborative.org/TNRSapp.html
22
max_names = 500 # the maximum above crashes the live and dev TNRS servers
23

    
24
# Protocol params
25
server = streams.file_get_contents(os.path.dirname(__file__)+"/tnrs.url")
26
#server = 'tnrs.iplantcollaborative.org' # live server
27
url_base = 'http://'+server+'/tnrsdemo/'
28
url = url_base+'search'
29
initial_headers = {
30
    'Content-Type': 'text/x-gwt-rpc; charset=utf-8',
31
    'X-GWT-Module-Base': url_base,
32
    'X-GWT-Permutation': '574AA16D15D917C7704646FD92AFF6B3',
33
}
34
submission_request_template = ('7|0|7|'+url_base+
35
'|1E87C78041CEFBF0992F46BDF84D7D60|org.iplantc.tnrs.demo.client.SearchService|\
36
doSearch|java.lang.String/2004016611|{"sources":"gcc,tpl,tropicos,usda", \
37
"names":"[names]", "type":"matching", "taxonomic":"true", \
38
"classification":"tropicos", "match_to_rank":"true"}|0.05|1|2|3|4|2|5|5|6|7|')
39
submission_response_pattern = r'^//OK\[1,\["(\w+)"\],0,7\]$'
40
retrieval_request_template = ('7|0|15|'+url_base+
41
'|1E87C78041CEFBF0992F46BDF84D7D60|org.iplantc.tnrs.demo.client.SearchService\
42
|getRemoteData|com.extjs.gxt.ui.client.data.PagingLoadConfig|\
43
java.lang.String/2004016611|com.extjs.gxt.ui.client.data.BasePagingLoadConfig/\
44
2011366567|com.extjs.gxt.ui.client.data.RpcMap/3441186752|sortField|sortDir|\
45
com.extjs.gxt.ui.client.Style$SortDir/640452531|offset|java.lang.Integer/\
46
3438268394|limit|{"email":"tnrs@lka5jjs.orv", "key":"[key]", \
47
"taxonomic_constraint":"true", "source_sorting":"true", "first":"false"}\
48
|1|2|3|4|2|5|6|7|0|1|8|4|9|0|10|11|0|12|13|0|14|13|100|15|')
49
	# taxonomic_constraint (Constrain by Higher Taxonomy): selects lower ranks
50
	#  only from within the matched higher ranks. must be turned on, to ensure
51
	#  that higher ranks are always authoritative.
52
	# source_sorting (Constrain by Source): always puts matches in the order of
53
	#  the sources, regardless of match score. should never be turned on,
54
	#  because it selects worse matches instead of better ones. *however*, since
55
	#  this is currently broken and always forced on, we turn it on so that the
56
	#  download settings reflect what TNRS actually used.
57
retrieval_response_pattern = '^//OK\[.*?\["com.extjs.gxt.ui.client.data.\
58
BasePagingLoadResult/496878394","java.util.ArrayList/4159755760","org.iplantc.\
59
tnrs.demo.shared.BeanTNRSEntry/1039545748",".*"\],0,7\]$'
60
retrieval_response_info_pattern = r'(?ms).*^Set-Cookie: JSESSIONID=(\w+);'
61
download_request_template = ('7|0|6|'+url_base+
62
'|1E87C78041CEFBF0992F46BDF84D7D60|org.iplantc.tnrs.demo.client.SearchService|\
63
downloadRemoteResults|java.lang.String/2004016611|{"name":"tnrs_results.txt", \
64
"mode":"All", "type":"Detailed", "encoding":"utf8", "dirty":"false", \
65
"sources":"false", "taxonomic":"true", "email":"tnrs@lka5jjs.orv", \
66
"key":"[key]"}|1|2|3|4|1|5|6|')
67
	# dirty:
68
	#  when on, sometimes marks multiple names as Selected. must be turned *off*
69
	#  so that only one name is marked as Selected. note that this actually used
70
	#  to be on in the web app (see r9910, 2013-6-18), but does not appear to be
71
	#  needed (the source_sorting bug alluded to in r9910 is not fixed by
72
	#  enabling the dirty setting).
73
download_response_pattern = '^//OK\[1,\["(.*)"\],0,7\]$'
74
download_url_suffix = '&name=tnrs_results.txt&encoding=utf8'
75
tsv_to_json_col_map = {
76
	'Name_matched_author': 'Canonical_author',
77
	'Accepted_name_species': 'Accepted_species',
78
	'Accepted_name_family': 'Accepted_family',
79
	'Selected': 'selected',
80
}
81

    
82
class InvalidResponse(Exception): pass
83

    
84
def gwt_encode(str_):
85
    # embedded strs within GWT sections additionally have " JSON-escaped
86
    return strings.esc_quotes(strings.json_encode(str_), '|', quote_esc='\!')
87

    
88
def gwt_decode(str_):
89
    # embedded strs within GWT sections additionally have " JSON-escaped
90
    return strings.json_decode(strings.unesc_quotes(str_, '|', quote_esc='\!'))
91

    
92
def make_spliced_decode_map(decode_map):
93
    return [(r'(?: |(?<=\t)|^)'+re.escape(from_.strip())+r'(?: |(?=\t)|$)',
94
        strings.regexp_repl_esc(to)) for from_, to in decode_map]
95

    
96
padding = ' !pad ' # prepend to empty and whitespace-only strings
97
encode_map = [
98
    ('!', ' !exc '), # our escape char
99
    ('\t', ' !tab '), # TNRS replaces with " "
100
    ('\n', ' !nl '), # used to separate multiple names
101
    ('\r', ' !cr '), # used to separate multiple names
102
    ('"', ' !quo '), # TNRS removes it when at the beginning or end
103
    ('%', ' !pct '), # TNRS URL-decodes it in matched fields
104
    ("'", ' !apo '), # TNRS removes it when at the beginning or end
105
    ('+', ' !plus '), # TNRS removes it from the morphospecies
106
    (';', ' !sem '), # changes TNRS response format
107
    ('\\', ' !bsl '), # TNRS removes it
108
    ('_', ' !und '), # TNRS replaces with " "
109
    ('­', ' !sub '), # TNRS removes it
110
    ('×', ' !mul '), # TNRS replaces with "x"
111
]
112
decode_map = strings.flip_map(encode_map)
113
decode_map.append((padding, ''))
114
spliced_decode_map = make_spliced_decode_map(decode_map)
115

    
116
def encode(str_):
117
    str_ = strings.replace_all(encode_map, str_)
118
    # Empty and whitespace-only strings are ignored by TNRS (no response row)
119
    if str_.strip() == '': str_ = padding+str_
120
    return str_
121

    
122
def decode(str_): return strings.replace_all_re(spliced_decode_map, str_)
123

    
124
decode_for_tsv_map = make_spliced_decode_map([(from_, strings.replace_all(
125
    csvs.tsv_encode_map, to)) for from_, to in decode_map])
126

    
127
def decode_for_tsv(str_):
128
    return strings.replace_all_re(decode_for_tsv_map, str_)
129

    
130
class TnrsOutputStream(streams.FilterStream):
131
    '''Decodes a TNRS response whose names were encoded with encode()'''
132
    def __init__(self, stream):
133
        streams.FilterStream.__init__(self, decode_for_tsv, stream)
134

    
135
def parse_response(name, pattern, str_, response, response_info):
136
    match = re.match(pattern, str_)
137
    if not match:
138
        raise InvalidResponse('Invalid '+name+' response:\n'+response_info+'\n'
139
            +response)
140
    return match.groups()
141

    
142
export_cols = None
143

    
144
def single_tnrs_request(names, debug=False, cumulative_profiler=None,
145
    use_tnrs_export=False):
146
    '''
147
    Note that names containing only whitespace characters (after gwt_encode())
148
    are ignored by TNRS and do not receive a response row. Thus, you should
149
    always match up the Name_submitted returned by TNRS with the actual
150
    submitted name to determine the corresponding TNRS response row.
151
    
152
    @param use_tnrs_export whether to use TNRS's TSV export feature, which
153
        currently returns incorrect selected matches (vegpath.org/issues/943)
154
    '''
155
    global export_cols
156
    
157
    name_ct = len(names)
158
    assert name_ct <= max_names
159
    assert name_ct >= 1 # with no names, TNRS hangs indefinitely
160
    
161
    # Logging
162
    def debug_log(label, str_=''):
163
        if debug: sys.stderr.write('\n'+label+':\n'+str_+'\n')
164
    
165
    if not use_tnrs_export and export_cols == None: # need export_cols
166
        debug_log('fetching export columns')
167
        export_cols = csvs.header(single_tnrs_request([''], debug,
168
            use_tnrs_export=True)) # need at least 1 name
169
        debug_log('TSV export columns', strings.urepr(export_cols))
170
        json_cols = [tsv_to_json_col_map.get(c, c) for c in export_cols]
171
        debug_log('JSON columns', strings.urepr(json_cols))
172
    
173
    ## HTTP
174
    headers = initial_headers.copy() # don't modify global constant!
175
    
176
    def do_request(request):
177
        request_obj = urllib2.Request(url, request, headers)
178
        debug_log('request URL', str(url))
179
        debug_log('request info', str(request_obj.header_items()))
180
        debug_log('request str', str(request_obj.get_data()))
181
        response = urllib2.urlopen(request_obj)
182
        response_str = streams.read_all(response)
183
        response_info = str(response.info())
184
        debug_log('response info', response_info)
185
        debug_log('response str', response_str)
186
        return response_str, response_info
187
    
188
    def do_repeated_request(request):
189
        pause = initial_pause
190
        total_pause = 0
191
        while True:
192
            total_pause += pause
193
            if total_pause > max_pause: raise # error is not temporary
194
            debug_log('total_pause', str(total_pause)+'s')
195
            time.sleep(pause) # wait for job to complete
196
            
197
            try: return do_request(request)
198
            except urllib2.HTTPError: pass # try again
199
            pause *= pause_growth_factor
200
    
201
    profiler = profiling.ItersProfiler(start_now=True, iter_text='name')
202
    try:
203
        debug_log('Submit')
204
        request = submission_request_template.replace('[names]',
205
            gwt_encode('\n'.join(map(encode, names))))
206
        response, response_info = do_request(request)
207
        key, = parse_response('submission', submission_response_pattern,
208
            response, response, response_info)
209
        debug_log('key', key)
210
        key_enc = gwt_encode(key)
211
        
212
        debug_log('Retrieve')
213
        request = retrieval_request_template.replace('[key]', key_enc)
214
        response, response_info = do_repeated_request(request)
215
        parse_response('retrieval', retrieval_response_pattern, response,
216
            response, response_info)
217
        session_id, = parse_response('retrieval info',
218
            retrieval_response_info_pattern, response_info, response,
219
            response_info)
220
        debug_log('session_id', session_id)
221
        headers['Cookie'] = 'JSESSIONID='+session_id
222
        
223
        # the output of the retrieve step is unusable because the array does not
224
        # contain all the columns, contains no column names, and has different
225
        # lengths depending on the taxonomic ranks present in the provided taxon
226
        # name. the extra download step is therefore necessary.
227
        
228
        if use_tnrs_export:
229
            debug_log('Prepare download')
230
            request = download_request_template.replace('[key]', key_enc)
231
            response, response_info = do_request(request)
232
            csv_url, = parse_response('download', download_response_pattern,
233
                response, response, response_info)
234
            csv_url += download_url_suffix
235
            debug_log('csv_url', csv_url)
236
            
237
            debug_log('Download')
238
            request_obj = urllib2.Request(csv_url)
239
            debug_log('request URL', str(csv_url))
240
            debug_log('request info', str(request_obj.header_items()))
241
            debug_log('request str', str(request_obj.get_data()))
242
            response = urllib2.urlopen(request_obj)
243
            response_info = str(response.info())
244
            debug_log('response info', response_info)
245
            output_stream = response
246
        else:
247
            debug_log('Expand')
248
            request = ('7|0|6|http://tnrs.iplantcollaborative.org/tnrsdemo/|\
249
1E87C78041CEFBF0992F46BDF84D7D60|org.iplantc.tnrs.demo.client.SearchService|\
250
requestGroupMembers|java.lang.String/2004016611|{"group":"0", \
251
"email":"tnrs@lka5jjs.orv", "key":"[key]", "source_sorting":"false", \
252
"taxonomic_constraint":"false"}|1|2|3|4|1|5|6|'.replace('[key]', key_enc))
253
            response, response_info = do_request(request)
254
            data_gwt, = parse_response('expand', r'^//OK\[1,\["(.*)"\],0,7\]$',
255
                response, response, response_info)
256
            data_json = json.loads(gwt_decode(data_gwt))
257
            debug_log('JSON data', strings.urepr(data_json))
258
            output_stream = csvs.InputRewriter(
259
                csvs.RowNumFilter(
260
                    csvs.JsonReader(data_json['items'], json_cols[1:], ';'),
261
                    json_cols[0]
262
                )
263
            )
264
        return TnrsOutputStream(output_stream)
265
    finally:
266
        profiler.stop(name_ct)
267
        sys.stderr.write(profiler.msg()+'\n')
268
        
269
        if cumulative_profiler != None:
270
            cumulative_profiler.add_subprofiler(profiler)
271
            sys.stderr.write('Cumulatively: '+cumulative_profiler.msg()+'\n')
272

    
273
def tnrs_request(names, debug=False, **kw_args):
274
    for try_num in xrange(2):
275
        try: return single_tnrs_request(names, debug, **kw_args)
276
        except (urllib2.HTTPError, InvalidResponse), e:
277
            exc.print_ex(e, detail=False)
278
            debug = True
279
            # try again with debug turned on
280
    raise # error is not temporary
(42-42/49)