annotate getXenaData.py @ 50:3167c1a26101

fix
author jingchunzhu
date Wed, 12 Aug 2015 16:38:12 -0700
parents 78d6e6772e30
children bb840cc2603d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
jingchunzhu
parents: 43
diff changeset
9 def main():
jingchunzhu
parents: 43
diff changeset
10 url = sys.argv[1]
jingchunzhu
parents: 43
diff changeset
11 dataset = sys.argv[2]
jingchunzhu
parents: 43
diff changeset
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
jingchunzhu
parents: 43
diff changeset
14 fout = open(output,'w')
42
bc9784300015 more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents: 41
diff changeset
15
50
jingchunzhu
parents: 43
diff changeset
16 contactUrl = url
jingchunzhu
parents: 43
diff changeset
17 if string.find(url,"galaxyxena") !=-1 and string.find(url,"ucsc.edu")!=-1:
jingchunzhu
parents: 43
diff changeset
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
jingchunzhu
parents: 43
diff changeset
20 #testing if the url is reachable
jingchunzhu
parents: 43
diff changeset
21 try:
jingchunzhu
parents: 43
diff changeset
22 r =json.loads(xena.post(contactUrl, "(+ 1 2)"))
jingchunzhu
parents: 43
diff changeset
23
jingchunzhu
parents: 43
diff changeset
24 if r!=3.0:
jingchunzhu
parents: 43
diff changeset
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."
jingchunzhu
parents: 43
diff changeset
26 print "You entered hub: %s" % (url)
jingchunzhu
parents: 43
diff changeset
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")
jingchunzhu
parents: 43
diff changeset
28 fout.write("You entered hub: %s\n" % (url))
jingchunzhu
parents: 43
diff changeset
29 fout.close()
jingchunzhu
parents: 43
diff changeset
30 sys.exit(1)
jingchunzhu
parents: 43
diff changeset
31
jingchunzhu
parents: 43
diff changeset
32 except:
jingchunzhu
parents: 43
diff changeset
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
78d6e6772e30 more reliable
jingchunzhu <jingchunzhu@gmail.com>
parents: 42
diff changeset
34 print "You entered hub: %s" % (url)
50
jingchunzhu
parents: 43
diff changeset
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
78d6e6772e30 more reliable
jingchunzhu <jingchunzhu@gmail.com>
parents: 42
diff changeset
36 fout.write("You entered hub: %s\n" % (url))
78d6e6772e30 more reliable
jingchunzhu <jingchunzhu@gmail.com>
parents: 42
diff changeset
37 fout.close()
78d6e6772e30 more reliable
jingchunzhu <jingchunzhu@gmail.com>
parents: 42
diff changeset
38 sys.exit(1)
42
bc9784300015 more reliable, error handling
jingchunzhu <jingchunzhu@gmail.com>
parents: 41
diff changeset
39
50
jingchunzhu
parents: 43
diff changeset
40 samples = xena.dataset_samples (contactUrl, dataset)
jingchunzhu
parents: 43
diff changeset
41 if not samples:
jingchunzhu
parents: 43
diff changeset
42 print "Dataset does not exist"
jingchunzhu
parents: 43
diff changeset
43 print "You entered dataset id: %s" % (dataset)
jingchunzhu
parents: 43
diff changeset
44 fout.write("Dataset does not exists\n")
jingchunzhu
parents: 43
diff changeset
45 fout.write("You entered dataset id: %s\n" % (dataset))
jingchunzhu
parents: 43
diff changeset
46 fout.close()
jingchunzhu
parents: 43
diff changeset
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
jingchunzhu
parents: 43
diff changeset
49 type = xena.dataset_type(contactUrl, dataset)
jingchunzhu
parents: 43
diff changeset
50 if type[0] not in ["genomicMatrix", "clinicalMatrix"]:
jingchunzhu
parents: 43
diff changeset
51 print "The type of data is not supported"
jingchunzhu
parents: 43
diff changeset
52 print "datatype=%s" % (type[0])
jingchunzhu
parents: 43
diff changeset
53 fout.write("The type of data is not supported\n")
jingchunzhu
parents: 43
diff changeset
54 fout.write("datatype=%s\n" % (type[0]))
jingchunzhu
parents: 43
diff changeset
55 fout.close()
jingchunzhu
parents: 43
diff changeset
56 sys.exit(1)
jingchunzhu
parents: 43
diff changeset
57
jingchunzhu
parents: 43
diff changeset
58 writer = csv.writer(fout, delimiter='\t')
jingchunzhu
parents: 43
diff changeset
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
jingchunzhu
parents: 43
diff changeset
61 probes = xena.dataset_field(contactUrl, dataset)
jingchunzhu
parents: 43
diff changeset
62 start=0
jingchunzhu
parents: 43
diff changeset
63 size =100
jingchunzhu
parents: 43
diff changeset
64 N= len(probes)
jingchunzhu
parents: 43
diff changeset
65 for i in range (start, N,size):
jingchunzhu
parents: 43
diff changeset
66 results = xena.dataset_probe_values (contactUrl, dataset, samples, probes[i:i+size])
jingchunzhu
parents: 43
diff changeset
67 print ".",
jingchunzhu
parents: 43
diff changeset
68 for j in range (0, size):
jingchunzhu
parents: 43
diff changeset
69 if i+j == N:
jingchunzhu
parents: 43
diff changeset
70 break
jingchunzhu
parents: 43
diff changeset
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
jingchunzhu
parents: 43
diff changeset
73 fout.close()
jingchunzhu
parents: 43
diff changeset
74 print "done"
jingchunzhu
parents: 43
diff changeset
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
jingchunzhu
parents: 43
diff changeset
77
jingchunzhu
parents: 43
diff changeset
78 if __name__ == "__main__":
jingchunzhu
parents: 43
diff changeset
79 main()
jingchunzhu
parents: 43
diff changeset
80