Mercurial > repos > melissacline > ucsc_xena_platform
annotate getXenaData.py @ 50:3167c1a26101
fix
author | jingchunzhu |
---|---|
date | Wed, 12 Aug 2015 16:38:12 -0700 |
parents | 78d6e6772e30 |
children | bb840cc2603d |
rev | line source |
---|---|
41
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
1 # getXenaData.py |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
2 import os, sys, string, json, csv |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
3 import xena_query as xena |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
4 |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
5 if len(sys.argv[:])!=4: |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
6 print "python getXenaData.py hub datasetId outputfile\n" |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
7 sys.exit(1) |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
8 |
50 | 9 def main(): |
10 url = sys.argv[1] | |
11 dataset = sys.argv[2] | |
12 output = sys.argv[3] | |
41
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
13 |
50 | 14 fout = open(output,'w') |
42
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
15 |
50 | 16 contactUrl = url |
17 if string.find(url,"galaxyxena") !=-1 and string.find(url,"ucsc.edu")!=-1: | |
18 contactUrl = "https://galaxyxena.soe.ucsc.edu:443/xena" | |
41
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
19 |
50 | 20 #testing if the url is reachable |
21 try: | |
22 r =json.loads(xena.post(contactUrl, "(+ 1 2)")) | |
23 | |
24 if r!=3.0: | |
25 print "The hub seems can not be reached, either it is not running, the url has a typo, or it is not accessible to this galaxy instance." | |
26 print "You entered hub: %s" % (url) | |
27 fout.write("The hub seems can not be reached, either it is not running, the url has a typo, or it is not accessible to this galaxy instance.\n") | |
28 fout.write("You entered hub: %s\n" % (url)) | |
29 fout.close() | |
30 sys.exit(1) | |
31 | |
32 except: | |
33 print "The hub seems can not be reached, either it is not running, the url has a typo, or it is not accessible to this galaxy instance." | |
43 | 34 print "You entered hub: %s" % (url) |
50 | 35 fout.write("The hub seems can not be reached, either it is not running, the url has a typo, or it is not accessible to this galaxy instance.\n") |
43 | 36 fout.write("You entered hub: %s\n" % (url)) |
37 fout.close() | |
38 sys.exit(1) | |
42
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
39 |
50 | 40 samples = xena.dataset_samples (contactUrl, dataset) |
41 if not samples: | |
42 print "Dataset does not exist" | |
43 print "You entered dataset id: %s" % (dataset) | |
44 fout.write("Dataset does not exists\n") | |
45 fout.write("You entered dataset id: %s\n" % (dataset)) | |
46 fout.close() | |
47 sys.exit(1) | |
41
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
48 |
50 | 49 type = xena.dataset_type(contactUrl, dataset) |
50 if type[0] not in ["genomicMatrix", "clinicalMatrix"]: | |
51 print "The type of data is not supported" | |
52 print "datatype=%s" % (type[0]) | |
53 fout.write("The type of data is not supported\n") | |
54 fout.write("datatype=%s\n" % (type[0])) | |
55 fout.close() | |
56 sys.exit(1) | |
57 | |
58 writer = csv.writer(fout, delimiter='\t') | |
59 writer.writerow(["sample"]+samples) | |
41
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
60 |
50 | 61 probes = xena.dataset_field(contactUrl, dataset) |
62 start=0 | |
63 size =100 | |
64 N= len(probes) | |
65 for i in range (start, N,size): | |
66 results = xena.dataset_probe_values (contactUrl, dataset, samples, probes[i:i+size]) | |
67 print ".", | |
68 for j in range (0, size): | |
69 if i+j == N: | |
70 break | |
71 writer.writerow([probes[i+j]]+results[j]) | |
41
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
72 |
50 | 73 fout.close() |
74 print "done" | |
75 sys.exit(0) | |
41
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
76 |
50 | 77 |
78 if __name__ == "__main__": | |
79 main() | |
80 |