annotate tools/data_source/ucsc_proxy.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 import urllib
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 import sys, os
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 CHUNK = 2**20 # 1Mb
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 MAXSIZE = CHUNK * 100
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 if __name__ == '__main__':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 if len(sys.argv) != 3:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 print 'Usage ucsc.py input_params output_file'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 inp_file = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 out_file = sys.argv[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 DEFAULT_URL = "http://genome.ucsc.edu/hgTables?"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 # this must stay a list to allow multiple selections for the same widget name (checkboxes)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 params = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 for line in file(inp_file):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 line = line.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 if line:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 parts = line.split('=')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 if len(parts) == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 key = ""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 value = ""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 elif len(parts) == 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 key = parts[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 value = ""
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 key = parts[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 value = parts[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 if key == 'display':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 print value
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 # get url from params, refered from proxy.py, initialized by the tool xml
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 elif key == 'proxy_url':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 DEFAULT_URL = value
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 params.append( (key, value) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 #print params
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 encoded_params = urllib.urlencode(params)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 url = DEFAULT_URL + encoded_params
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 #print url
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 page = urllib.urlopen(url)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 fp = open(out_file, 'wt')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 size = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 while 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 data = page.read(CHUNK)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 if not data:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 if size > MAXSIZE:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 fp.write('----- maximum datasize exceeded ---\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 break
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 size += len(data)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 fp.write(data)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 fp.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65