comparison getXenaData.py @ 52:bb840cc2603d

better error handling
author jingchunzhu
date Thu, 13 Aug 2015 17:30:19 -0700
parents 3167c1a26101
children
comparison
equal deleted inserted replaced
51:47926759295a 52:bb840cc2603d
1 # getXenaData.py 1 #!/usr/bin/env python
2
2 import os, sys, string, json, csv 3 import os, sys, string, json, csv
3 import xena_query as xena 4 import xena_query as xena
4 5
5 if len(sys.argv[:])!=4: 6 if len(sys.argv[:])!=5:
6 print "python getXenaData.py hub datasetId outputfile\n" 7 print "python getXenaData.py hub datasetId outputfile galaxy_url\n"
7 sys.exit(1) 8 sys.exit(2)
8 9
9 def main(): 10 def main():
10 url = sys.argv[1] 11 url = sys.argv[1]
11 dataset = sys.argv[2] 12 dataset = sys.argv[2]
12 output = sys.argv[3] 13 output = sys.argv[3]
14 GALAXY_URL = sys.argv[4]
13 15
14 fout = open(output,'w') 16 fout = open(output,'w')
15 17
16 contactUrl = url 18 contactUrl = url
17 if string.find(url,"galaxyxena") !=-1 and string.find(url,"ucsc.edu")!=-1: 19 if string.find(url,"galaxyxena") !=-1 and string.find(url,"ucsc.edu")!=-1:
20 #testing if the url is reachable 22 #testing if the url is reachable
21 try: 23 try:
22 r =json.loads(xena.post(contactUrl, "(+ 1 2)")) 24 r =json.loads(xena.post(contactUrl, "(+ 1 2)"))
23 25
24 if r!=3.0: 26 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." 27 fout.write("There is an error\n\n")
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)) 28 fout.write("You entered hub: %s\n" % (url))
29 fout.write("Possible causes for error:\n")
30 fout.write("1. The hub is not running.\n")
31 fout.write("2. The hub is not accessible to this galaxy instance at: %s.\n" %(GALAXY_URL))
32 fout.write("3. The hub url has a typo.\n")
29 fout.close() 33 fout.close()
30 sys.exit(1) 34 sys.exit(1)
31 35
32 except: 36 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." 37 fout.write("There is an error\n\n")
34 print "You entered hub: %s" % (url)
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")
36 fout.write("You entered hub: %s\n" % (url)) 38 fout.write("You entered hub: %s\n" % (url))
39 fout.write("Possible causes for error:\n")
40 fout.write("1. The hub is not running.\n")
41 fout.write("2. The hub is not accessible to this galaxy instance at: %s.\n" %(GALAXY_URL))
42 fout.write("3. The hub url has a typo.\n")
37 fout.close() 43 fout.close()
38 sys.exit(1) 44 sys.exit(1)
39 45
40 samples = xena.dataset_samples (contactUrl, dataset) 46 samples = xena.dataset_samples (contactUrl, dataset)
41 if not samples: 47 if not samples:
42 print "Dataset does not exist" 48 fout.write("There is an error\n\n")
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)) 49 fout.write("You entered dataset id: %s\n" % (dataset))
50 fout.write("Possible causes for error:\n")
51 fout.write("1. Dataset does not exist.\n")
46 fout.close() 52 fout.close()
47 sys.exit(1) 53 sys.exit(1)
48 54
49 type = xena.dataset_type(contactUrl, dataset) 55 type = xena.dataset_type(contactUrl, dataset)
50 if type[0] not in ["genomicMatrix", "clinicalMatrix"]: 56 if type[0] not in ["genomicMatrix", "clinicalMatrix"]:
51 print "The type of data is not supported" 57 fout.write("There is an error\n\n")
52 print "datatype=%s" % (type[0]) 58 fout.write("You entered dataset id: %s\n" % (dataset))
53 fout.write("The type of data is not supported\n") 59 fout.write("It's datatype=%s\n" % (type[0]))
54 fout.write("datatype=%s\n" % (type[0])) 60 fout.write("This type of data is not supported yet.\n")
55 fout.close() 61 fout.close()
56 sys.exit(1) 62 sys.exit(1)
57 63
58 writer = csv.writer(fout, delimiter='\t') 64 writer = csv.writer(fout, delimiter='\t')
59 writer.writerow(["sample"]+samples) 65 writer.writerow(["sample"]+samples)
69 if i+j == N: 75 if i+j == N:
70 break 76 break
71 writer.writerow([probes[i+j]]+results[j]) 77 writer.writerow([probes[i+j]]+results[j])
72 78
73 fout.close() 79 fout.close()
74 print "done"
75 sys.exit(0) 80 sys.exit(0)
76
77 81
78 if __name__ == "__main__": 82 if __name__ == "__main__":
79 main() 83 main()
80 84