diff getXenaData.py @ 52:bb840cc2603d

better error handling
author jingchunzhu
date Thu, 13 Aug 2015 17:30:19 -0700
parents 3167c1a26101
children
line wrap: on
line diff
--- a/getXenaData.py	Thu Aug 13 10:24:12 2015 -0700
+++ b/getXenaData.py	Thu Aug 13 17:30:19 2015 -0700
@@ -1,15 +1,17 @@
-# getXenaData.py
+#!/usr/bin/env python
+
 import os, sys, string, json, csv
 import xena_query as xena
 
-if len(sys.argv[:])!=4:
-  print "python getXenaData.py hub datasetId outputfile\n"
-  sys.exit(1)
+if len(sys.argv[:])!=5:
+  print "python getXenaData.py hub datasetId outputfile galaxy_url\n"
+  sys.exit(2)
 
 def main():
   url = sys.argv[1]
   dataset = sys.argv[2]
   output = sys.argv[3]
+  GALAXY_URL = sys.argv[4]
 
   fout = open(output,'w')
 
@@ -22,36 +24,40 @@
     r =json.loads(xena.post(contactUrl, "(+ 1 2)")) 
   
     if  r!=3.0:
-      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."
-      print "You entered hub: %s" % (url)
-      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")
+      fout.write("There is an error\n\n")
       fout.write("You entered hub: %s\n" % (url))
+      fout.write("Possible causes for error:\n")
+      fout.write("1. The hub is not running.\n")
+      fout.write("2. The hub is not accessible to this galaxy instance at: %s.\n" %(GALAXY_URL))
+      fout.write("3. The hub url has a typo.\n")
       fout.close()
       sys.exit(1)
 
   except: 
-    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."
-    print "You entered hub: %s" % (url)
-    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")
+    fout.write("There is an error\n\n")
     fout.write("You entered hub: %s\n" % (url))
+    fout.write("Possible causes for error:\n")
+    fout.write("1. The hub is not running.\n")
+    fout.write("2. The hub is not accessible to this galaxy instance at: %s.\n" %(GALAXY_URL))
+    fout.write("3. The hub url has a typo.\n")
     fout.close()
     sys.exit(1)
 
   samples = xena.dataset_samples (contactUrl, dataset)
   if not samples:
-    print "Dataset does not exist"
-    print "You entered dataset id: %s" % (dataset)
-    fout.write("Dataset does not exists\n")
+    fout.write("There is an error\n\n")
     fout.write("You entered dataset id: %s\n" % (dataset))
+    fout.write("Possible causes for error:\n")
+    fout.write("1. Dataset does not exist.\n")
     fout.close()
     sys.exit(1)
 
   type = xena.dataset_type(contactUrl, dataset)
   if type[0] not in ["genomicMatrix", "clinicalMatrix"]:
-    print "The type of data is not supported"
-    print "datatype=%s" % (type[0])
-    fout.write("The type of data is not supported\n")
-    fout.write("datatype=%s\n" % (type[0]))
+    fout.write("There is an error\n\n")
+    fout.write("You entered dataset id: %s\n" % (dataset))
+    fout.write("It's datatype=%s\n" % (type[0]))
+    fout.write("This type of data is not supported yet.\n")
     fout.close()
     sys.exit(1)
 
@@ -71,10 +77,8 @@
       writer.writerow([probes[i+j]]+results[j])
 
   fout.close()
-  print "done"
   sys.exit(0)
 
-
 if __name__ == "__main__":
     main()