Project

General

Profile

1 4990 aaronmk
# TNRS
2
3
import re
4
import sys
5
import time
6
import urllib2
7
8 5149 aaronmk
import csvs
9 5107 aaronmk
import exc
10 5120 aaronmk
import profiling
11 4990 aaronmk
import streams
12 5144 aaronmk
import strings
13 4990 aaronmk
14
# Config
15
initial_pause = 0.35 # sec
16
pause_growth_factor = 1.3
17 5125 aaronmk
max_pause = 30*60 # sec; = 30 min; TNRS sometimes freezes for ~10 min
18 4990 aaronmk
assert initial_pause <= max_pause
19 5121 aaronmk
max_names = 5000 # according to http://tnrs.iplantcollaborative.org/TNRSapp.html
20
max_names = 500 # the maximum above crashes the TNRS server
21 4990 aaronmk
22
# Protocol params
23
url_base = 'http://tnrs.iplantcollaborative.org/tnrsdemo/'
24
url = url_base+'search'
25
initial_headers = {
26
    'Content-Type': 'text/x-gwt-rpc; charset=utf-8',
27
    'X-GWT-Module-Base': url_base,
28
    'X-GWT-Permutation': '574AA16D15D917C7704646FD92AFF6B3',
29
}
30
submission_request_template = ('7|0|7|'+url_base+
31
'||org.iplantc.tnrs.demo.client.SearchService|doSearch|\
32 5121 aaronmk
java.lang.String/2004016611|{"sources":"gcc,tropicos,usda", "names":"[names]"\
33 4990 aaronmk
, "type":"matching", "taxonomic":"true", "classification":"tropicos", \
34
"match_to_rank":"true"}|0.05|1|2|3|4|2|5|5|6|7|')
35
submission_response_pattern = r'^//OK\[1,\["(\w+)"\],0,7\]$'
36
retrieval_request_template = ('7|0|15|'+url_base+
37
'|1E87C78041CEFBF0992F46BDF84D7D60|org.iplantc.tnrs.demo.client.SearchService\
38
|getRemoteData|com.extjs.gxt.ui.client.data.PagingLoadConfig|\
39
java.lang.String/2004016611|com.extjs.gxt.ui.client.data.BasePagingLoadConfig/\
40
2011366567|com.extjs.gxt.ui.client.data.RpcMap/3441186752|sortField|sortDir|\
41
com.extjs.gxt.ui.client.Style$SortDir/640452531|offset|java.lang.Integer/\
42
3438268394|limit|{"email":"tnrs@lka5jjs.orv", "key":"[key]", \
43
"taxonomic_constraint":"false", "source_sorting":"false", "first":"false"}\
44
|1|2|3|4|2|5|6|7|0|1|8|4|9|0|10|11|0|12|13|0|14|13|100|15|')
45
retrieval_response_pattern = '^//OK\[.*?\["com.extjs.gxt.ui.client.data.\
46
BasePagingLoadResult/496878394","java.util.ArrayList/4159755760","org.iplantc.\
47
tnrs.demo.shared.BeanTNRSEntry/1039545748",".*"\],0,7\]$'
48
retrieval_response_info_pattern = r'(?ms).*^Set-Cookie: JSESSIONID=(\w+);'
49
download_request_template = ('7|0|6|'+url_base+
50
'|1E87C78041CEFBF0992F46BDF84D7D60|org.iplantc.tnrs.demo.client.SearchService|\
51
downloadRemoteResults|java.lang.String/2004016611|{"name":"tnrs_results.txt", \
52
"mode":"Best", "type":"Detailed", "encoding":"utf8", "dirty":"false", \
53
"sources":"false", "taxonomic":"false", "email":"tnrs@lka5jjs.orv", \
54
"key":"[key]"}|1|2|3|4|1|5|6|')
55
download_response_pattern = '^//OK\[1,\["(.*)"\],0,7\]$'
56
download_url_suffix = '&name=tnrs_results.txt&encoding=utf8'
57
58 5083 aaronmk
class InvalidResponse(Exception): pass
59
60 5144 aaronmk
def gwt_encode(str_):
61
    return strings.esc_quotes(strings.json_encode(str_), '|', quote_esc='\!')
62 4990 aaronmk
63 5149 aaronmk
def make_spliced_decode_map(decode_map):
64
    return [(r'(?: |(?<=\t)|^)'+re.escape(from_.strip())+r'(?: |(?=\t)|$)',
65
        strings.regexp_repl_esc(to)) for from_, to in decode_map]
66
67 5154 aaronmk
padding = ' !pad ' # prepend to empty and whitespace-only strings
68 5149 aaronmk
encode_map = [
69 5167 aaronmk
    ('!', ' !exc '), # our escape char
70
    ('\t', ' !tab '), # TNRS replaces with " "
71
    ('\n', ' !nl '), # used to separate multiple names
72
    ('\r', ' !cr '), # used to separate multiple names
73
    ('%', ' !pct '), # TNRS URL-decodes it in matched fields
74
    (';', ' !sem '), # changes TNRS response format
75
    ('\\', ' !bsl '), # TNRS removes it
76 5165 aaronmk
    ('_', ' !und '), # TNRS replaces with " "
77 5149 aaronmk
]
78
decode_map = strings.flip_map(encode_map)
79 5154 aaronmk
decode_map.append((padding, ''))
80 5149 aaronmk
spliced_decode_map = make_spliced_decode_map(decode_map)
81
82 5154 aaronmk
def encode(str_):
83
    str_ = strings.replace_all(encode_map, str_)
84
    # Empty and whitespace-only strings are ignored by TNRS (no response row)
85
    if str_.strip() == '': str_ = padding+str_
86
    return str_
87 5149 aaronmk
88
def decode(str_): return strings.replace_all_re(spliced_decode_map, str_)
89
90
decode_for_tsv_map = make_spliced_decode_map([(from_, strings.replace_all(
91
    csvs.tsv_encode_map, to)) for from_, to in decode_map])
92
93
def decode_for_tsv(str_):
94
    return strings.replace_all_re(decode_for_tsv_map, str_)
95
96
class TnrsOutputStream(streams.FilterStream):
97
    '''Decodes a TNRS response whose names were encoded with encode()'''
98
    def __init__(self, stream):
99
        streams.FilterStream.__init__(self, decode_for_tsv, stream)
100
101 5106 aaronmk
def parse_response(name, pattern, str_, response, response_info):
102
    match = re.match(pattern, str_)
103
    if not match:
104
        raise InvalidResponse('Invalid '+name+' response:\n'+response_info+'\n'
105
            +response)
106 4990 aaronmk
    return match.groups()
107
108 5121 aaronmk
def tnrs_request(names, debug=False):
109 5127 aaronmk
    '''
110
    Note that names containing only whitespace characters (after gwt_encode())
111
    are ignored by TNRS and do not receive a response row. Thus, you should
112
    always match up the Name_submitted returned by TNRS with the actual
113
    submitted name to determine the corresponding TNRS response row.
114
    '''
115 5121 aaronmk
    name_ct = len(names)
116
    assert name_ct <= max_names
117 4990 aaronmk
118
    # Logging
119
    def debug_log(label, str_=''):
120
        if debug: sys.stderr.write('\n'+label+':\n'+str_+'\n')
121
122
    ## HTTP
123 5119 aaronmk
    headers = initial_headers.copy() # don't modify global constant!
124 4990 aaronmk
125 5005 aaronmk
    def do_request(request):
126 4990 aaronmk
        debug_log('request', str(request))
127
        response = urllib2.urlopen(urllib2.Request(url, request, headers))
128
        response_str = streams.read_all(response)
129
        response_info = str(response.info())
130
        debug_log('response info', response_info)
131
        debug_log('response str', response_str)
132
        return response_str, response_info
133
134
    def do_repeated_request(request):
135
        pause = initial_pause
136
        total_pause = 0
137
        while True:
138
            total_pause += pause
139
            if total_pause > max_pause: raise # error is not temporary
140
            debug_log('total_pause', str(total_pause)+'s')
141
            time.sleep(pause) # wait for job to complete
142
143
            try: return do_request(request)
144
            except urllib2.HTTPError: pass # try again
145
            pause *= pause_growth_factor
146
147 5120 aaronmk
    profiler = profiling.ItersProfiler(start_now=True, iter_text='name')
148
    try:
149
        debug_log('Submit')
150 5121 aaronmk
        request = submission_request_template.replace('[names]',
151 5150 aaronmk
            gwt_encode('\n'.join(map(encode, names))))
152 5120 aaronmk
        response, response_info = do_request(request)
153 5151 aaronmk
        key, = parse_response('submission', submission_response_pattern,
154
            response, response, response_info)
155 5120 aaronmk
        debug_log('key', key)
156
        key_enc = gwt_encode(key)
157
158
        debug_log('Retrieve')
159
        request = retrieval_request_template.replace('[key]', key_enc)
160
        response, response_info = do_repeated_request(request)
161 5151 aaronmk
        parse_response('retrieval', retrieval_response_pattern, response,
162
            response, response_info)
163
        session_id, = parse_response('retrieval info',
164
            retrieval_response_info_pattern, response_info, response,
165 5120 aaronmk
            response_info)
166
        debug_log('session_id', session_id)
167
        headers['Cookie'] = 'JSESSIONID='+session_id
168
169
        # The output of the retrieve step is unusable because the array has
170 5151 aaronmk
        # different lengths depending on the taxonomic ranks present in the
171
        # provided taxon name. The extra download step is therefore necessary.
172 5120 aaronmk
173
        debug_log('Prepare download')
174
        request = download_request_template.replace('[key]', key_enc)
175
        response, response_info = do_request(request)
176 5151 aaronmk
        csv_url, = parse_response('download', download_response_pattern,
177
            response, response, response_info)
178 5120 aaronmk
        csv_url += download_url_suffix
179
        debug_log('csv_url', csv_url)
180
181
        debug_log('Download')
182
        response = urllib2.urlopen(urllib2.Request(csv_url))
183
        debug_log('response info', str(response.info()))
184 5150 aaronmk
        return TnrsOutputStream(response)
185 5120 aaronmk
    finally:
186 5121 aaronmk
        profiler.stop(name_ct)
187 5120 aaronmk
        sys.stderr.write(profiler.msg()+'\n')
188 5088 aaronmk
189 5121 aaronmk
def repeated_tnrs_request(names, debug=False, **kw_args):
190 5108 aaronmk
    for try_num in xrange(2):
191 5121 aaronmk
        try: return tnrs_request(names, debug, **kw_args)
192 5160 aaronmk
        except (urllib2.HTTPError, InvalidResponse), e:
193 5107 aaronmk
            exc.print_ex(e, detail=False)
194 5108 aaronmk
            debug = True
195
            # try again with debug turned on
196 5088 aaronmk
    raise # error is not temporary