changeset 52:bb840cc2603d

better error handling
author jingchunzhu
date Thu, 13 Aug 2015 17:30:19 -0700
parents 47926759295a
children 09fdd4d23a3a
files getXenaData.py ucsc_xena_datapages.xml ucsc_xena_hub.xml xenaGetDataset.py xenaGetDataset.xml
diffstat 5 files changed, 50 insertions(+), 36 deletions(-) [+]
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()
 
--- a/ucsc_xena_datapages.xml	Thu Aug 13 10:24:12 2015 -0700
+++ b/ucsc_xena_datapages.xml	Thu Aug 13 17:30:19 2015 -0700
@@ -1,5 +1,5 @@
 <?xml version="1.0"?>
-<tool name="Explore Data in Xena" id="ucsc_xenadatapages" tool_type="data_source">
+<tool name="Explore Data" id="ucsc_xenadatapages" tool_type="data_source">
   <description>Browse data in the federated UCSC Xena platform</description>
   <inputs action="https://genome-cancer.soe.ucsc.edu/proj/site/xena/datapages/" >
     <param name="GALAXY_URL" type="baseurl" value="" />
--- a/ucsc_xena_hub.xml	Thu Aug 13 10:24:12 2015 -0700
+++ b/ucsc_xena_hub.xml	Thu Aug 13 17:30:19 2015 -0700
@@ -1,8 +1,7 @@
-<?xml version="1.0"?>
 <tool name="Xena Data Hub" id="ucsc_xenadatahub" tool_type="data_source">
     <description>Build your Xena Data Hubs</description>
     <inputs action="https://genome-cancer.soe.ucsc.edu/proj/site/xena/hub/" >
-        <param name="GALAXY_URL" type="baseurl" value="" />
+        <param name="GALAXY_URL" type="baseurl" />
         <param name="tool_id" type="hidden" value="ucsc_xenadatapages" />
     </inputs>
 </tool>
--- a/xenaGetDataset.py	Thu Aug 13 10:24:12 2015 -0700
+++ b/xenaGetDataset.py	Thu Aug 13 17:30:19 2015 -0700
@@ -3,20 +3,25 @@
 import argparse
 import re
 import urllib2
-
+import sys
 
 def main():
-    parser = argparse.ArgumentParser()
-    parser.add_argument("dataHub", type=str)
-    parser.add_argument("datasetId", type=str)
-    parser.add_argument("datafile", type=str)
-    args = parser.parse_args()
+    try:
+        parser = argparse.ArgumentParser()
+        parser.add_argument("dataHub", type=str)
+        parser.add_argument("datasetId", type=str)
+        parser.add_argument("datafile", type=str)
+        parser.add_argument("GALAXY_URL", type=str)
+        args = parser.parse_args()
+    except:
+        print "too few input parameters"
+        sys.exit(2)
 
+    data = open(args.datafile, "w")
     datasetUrlHost = re.sub("/proj/", "/download/", args.dataHub)
     datasetIdTokens = re.split("/", args.datasetId)
     datasetUrl = datasetUrlHost + "/" + "/".join(datasetIdTokens[1:])
 
-    data = open(args.datafile, "w")
 
     try:
         dd = urllib2.urlopen(datasetUrl)
@@ -24,8 +29,10 @@
         dd.close()
         data.close()
     except:
-        data.wriet("You entered dataset id: %s\n" % (args.datasetId))
-        data.write("Dataset does not exist. check typo.")
+        data.write("There is an error\n\n")
+        data.write("You entered dataset id: %s\n" % (args.datasetId))
+        data.write("Possible causes for error:\n")
+        data.write("1. Dataset does not exist. check type.\n")
         data.close()
         sys.exit(1)
 
--- a/xenaGetDataset.xml	Thu Aug 13 10:24:12 2015 -0700
+++ b/xenaGetDataset.xml	Thu Aug 13 17:30:19 2015 -0700
@@ -7,11 +7,11 @@
   </requirements>
   <command interpreter="python">
     #if $hub.dataHub == "https://genome-cancer.ucsc.edu/proj/public/xena":
-        xenaGetDataset.py $hub.dataHub $dataset $dataFile
+        xenaGetDataset.py $hub.dataHub $dataset $dataFile $GALAXY_URL
     #elif $hub.customDataHub:
-        getXenaData.py $hub.customDataHub $dataset $dataFile
+        getXenaData.py $hub.customDataHub $dataset $dataFile $GALAXY_URL
     #else
-        getXenaData.py $hub.dataHub $dataset $dataFile
+        getXenaData.py $hub.dataHub $dataset $dataFile $GALAXY_URL
     #end if
   </command>
   <inputs>
@@ -25,12 +25,16 @@
 	<param type="text" name ="customDataHub" label="Hub url" optional="false"/>
       </when>
     </conditional>  
-    <param type="text" name="dataset" label="Dataset ID" optional="false" help="How do I find the dataset ID? Tools section-> UCSC Xena Platform ->  Explore Data in Xena:  It takes you to the xena data exploration page. Browser to find the cohort of your interest (e.g. TCGA Lung Adenocarcinoma), then click the cohort link, then find the dataset of your interest (e.g. LUAD exon expression (IlluminaHiSeq) ), click the dataset link to go to its own detailed page. Copy and paste from the dataset ID field." />
+    <param type="text" name="dataset" label="Dataset ID" optional="false" help="How do I find the dataset ID? Tools section-> UCSC Xena Platform ->  Explore Data in Xena:  It takes you to the xena data exploration page. Browser to find the cohort of your interest (e.g. TCGA Lung Adenocarcinoma), then click the cohort link, then find the dataset of your interest (e.g. LUAD exon expression (IlluminaHiSeq) ), click the dataset link to go to its own detailed page. Copy and paste from the dataset ID field." />    
+    <param name="GALAXY_URL" type="baseurl" />
   </inputs>
   <outputs>
-    <!-- <data format="txt" name="metadataFile" label="${dataset}.json"/> -->
     <data format="tabular" name="dataFile" label="${dataset}" />
   </outputs>
+  <stdio>
+    <exit_code range="1"   level="fatal"   description="Click the eyeball icon to see details." />
+    <exit_code range="2"   level="fatal"   description="Not enough input arguments" />
+  </stdio>
   <help>
     Given the data hub name and the dataset id, download the dataset into this Galaxy. Xena dataset id can be obtained through the Explore Data in Xena tool.
   </help>