Mercurial > repos > melissacline > ucsc_xena_platform
annotate getXenaData.py @ 42:bc9784300015
more reliable, error handling
author | jingchunzhu <jingchunzhu@gmail.com> |
---|---|
date | Mon, 27 Jul 2015 12:01:42 -0700 |
parents | 02b0824c7d60 |
children | 78d6e6772e30 |
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 |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
9 url = sys.argv[1] |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
10 dataset = sys.argv[2] |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
11 output = sys.argv[3] |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
12 |
42
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
13 fout = open(output,'w') |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
14 |
41
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
15 if string.find(url,"galaxyxena") !=-1 and string.find(url,"ucsc.edu")!=-1: |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
16 url = "https://galaxyxena.soe.ucsc.edu:443/xena" |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
17 |
42
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
18 #testing if the url is reachable |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
19 #try: |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
20 r =json.loads(xena.post(url, "(+ 1 2)")) |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
21 if r!=3.0: |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
22 print "The hub seems can not be reached, either it is not running, the url has a typo, or it is not accessible to you." |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
23 print "You entered hub: %s" % (url) |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
24 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 you.\n") |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
25 fout.write("You entered hub: %s\n" % (url)) |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
26 fout.close() |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
27 sys.exit(1) |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
28 |
41
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
29 samples = xena.dataset_samples (url, dataset) |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
30 if not samples: |
42
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
31 print "Dataset does not exist" |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
32 print "You entered dataset id: %s" % (dataset) |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
33 fout.write("Dataset does not exists\n") |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
34 fout.wriet("You entered dataset id: %s\n" % (dataset)) |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
35 fout.close() |
41
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
36 sys.exit(1) |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
37 |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
38 type = xena.dataset_type(url, dataset) |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
39 if type[0] not in ["genomicMatrix", "clinicalMatrix"]: |
42
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
40 print "The type of data is not supported" |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
41 print "datatype=%s" % (type[0]) |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
42 fout.write("The type of data is not supported\n") |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
43 fout.write("datatype=%s\n" % (type[0])) |
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
44 fout.close() |
41
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
45 sys.exit(1) |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
46 |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
47 writer = csv.writer(fout, delimiter='\t') |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
48 writer.writerow(["sample"]+samples) |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
49 |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
50 probes = xena.dataset_field(url, dataset) |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
51 start=0 |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
52 size =100 |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
53 N= len(probes) |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
54 for i in range (start, N,size): |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
55 results = xena.dataset_probe_values (url, dataset, samples, probes[i:i+size]) |
42
bc9784300015
more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents:
41
diff
changeset
|
56 print ".", |
41
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
57 for j in range (0, size): |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
58 if i+j == N: |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
59 break |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
60 writer.writerow([probes[i+j]]+results[j]) |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
61 |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
62 fout.close() |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
63 print "done" |
02b0824c7d60
Download data from any hub in the federated xena platform
jingchunzhu <jingchunzhu@gmail.com>
parents:
diff
changeset
|
64 sys.exit(0) |