Locked History Actions

attachment:datasets_3.py of Events/GCC2013/TrainingDay/API

Attachment 'datasets_3.py'

Download

   1 #!/usr/bin/env python
   2 
   3 """
   4 Access a Galaxy instance and get data for Datasets.
   5 """
   6 import os
   7 import sys
   8 import pprint
   9 
  10 import setup
  11 import common
  12 
  13 RESOURCE_URL = '/api/datasets'
  14 
  15 # ----------------------------------------------------------------------------- functions
  16 def get_datasets():
  17     """
  18     Return a list of dictionaries that describe datasets.
  19     """
  20     apikey = setup.get_apikey()
  21     full_url = setup.get_base_url() + RESOURCE_URL
  22     return common.get( apikey, full_url )
  23 
  24 def get_dataset( dataset_id ):
  25     """
  26     Return a dictionary of detailed information for a specific dataset.
  27     :param dataset_id: the id of the dataset which we want to query.
  28     """
  29     apikey = setup.get_apikey()
  30     resource_url = setup.get_base_url() + RESOURCE_URL
  31 
  32     full_url = resource_url + '/' + dataset_id
  33     return common.get( apikey, full_url )
  34 
  35 def get_dataset_column( dataset_id, column ):
  36     """
  37     Return a dictionary of detailed information for a specific dataset.
  38     :param dataset_id: the id of the dataset which we want to query.
  39     """
  40     # here, we'll pass some extra arguments into the URL to get what we want - column data from the dataset's file
  41     # first, build the base url for the dataset resource (in this case, datasets.show):
  42     apikey = setup.get_apikey()
  43     resource_url = setup.get_base_url() + RESOURCE_URL
  44     datasets_show_url = resource_url + '/' + dataset_id
  45 
  46     # arguments to the api can be added in two different ways:
  47     #   1. on the query string (you can see query string arguments when you do a google search -
  48     #        they appear in your address bar separated by '&', '=', etc. - all after a '?')
  49     #   2. post data - data that gets attached (gen. in JSON format) to your request
  50     #       without adding it directly to the url
  51 
  52     # something to keep in mind when using GET API functions:
  53     #   every argument needs to be a well formed string
  54     # if you have problems calling a particular GET API function, one thing to check is how your arguments are
  55     #   being converted to strings
  56 
  57     # our arguments to the datasets API function will be:
  58     # 1. the type of data we want: raw_data
  59     data_type = 'raw_data'
  60     # 2. which provider we want - in this case we need a 'column' data provider
  61     provider = 'column'
  62     # 3. which column we want in JSON array format - we'll also make sure it's a string
  63     #   (you can use a csv string to get more than one column but we'll keep things simple here)
  64     columns = '[' + str( column ) + ']'
  65 
  66     # here we'll use common.make_url - this is a convenience function that will
  67     #   place all the arguments we need into the /api/datasets URL in the proper GET style
  68     # we'll need to bundle them into a python list of tuples first: [ ( arg1_name, arg1_value ), ... ]
  69     arg_list = [ ( 'data_type', data_type ), ( 'provider', provider ), ( 'columns', columns ) ]
  70     full_url = common.make_url( apikey, datasets_show_url, args=arg_list )
  71 
  72     # (let's also print the final url to see what make_url did:)
  73     print 'get_dataset_column, full_url:', full_url
  74 
  75     return common.get( apikey, full_url )
  76 
  77 
  78 # ----------------------------------------------------------------------------- main
  79 if __name__ == '__main__':
  80     if len( sys.argv ) <= 2:
  81         print 'USAGE: datasets_3.py <dataset_id> <0-based column index>'
  82         sys.exit( 1 )
  83 
  84     dataset_id = sys.argv[1]
  85     column = sys.argv[2]
  86     returned = get_dataset_column( dataset_id, column )
  87     pprint.pprint( returned, indent=2 )

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2013-06-24 16:01:09, 8.2 KB) [[attachment:Galaxy-Workflow-Joined_Solexa_QC.ga]]
  • [get | view] (2013-06-24 16:46:36, 14.0 KB) [[attachment:all-scripts.tar.gz]]
  • [get | view] (2013-06-24 16:01:23, 5.2 KB) [[attachment:common.py]]
  • [get | view] (2013-06-24 16:04:07, 0.9 KB) [[attachment:datasets_1.py]]
  • [get | view] (2013-06-24 16:04:15, 1.2 KB) [[attachment:datasets_2.py]]
  • [get | view] (2013-06-24 16:04:22, 3.4 KB) [[attachment:datasets_3.py]]
  • [get | view] (2013-06-24 16:04:33, 1.5 KB) [[attachment:hdas_1.py]]
  • [get | view] (2013-06-24 16:04:50, 1.7 KB) [[attachment:hdas_2.py]]
  • [get | view] (2013-06-24 16:05:07, 2.2 KB) [[attachment:hdas_3.py]]
  • [get | view] (2013-06-24 16:03:38, 0.9 KB) [[attachment:histories_1.py]]
  • [get | view] (2013-06-24 16:03:52, 2.1 KB) [[attachment:histories_2.py]]
  • [get | view] (2013-06-24 16:05:25, 2.2 KB) [[attachment:histories_3.py]]
  • [get | view] (2013-06-24 16:05:34, 3.3 KB) [[attachment:lddas_1.py]]
  • [get | view] (2013-06-24 16:05:41, 1.3 KB) [[attachment:libraries_1.py]]
  • [get | view] (2013-06-24 16:01:36, 0.8 KB) [[attachment:setup.py]]
  • [get | view] (2013-06-24 16:02:45, 1.1 KB) [[attachment:step_1.py]]
  • [get | view] (2013-06-24 16:07:15, 9.9 KB) [[attachment:step_10.py]]
  • [get | view] (2013-06-24 16:03:28, 1.4 KB) [[attachment:step_2.py]]
  • [get | view] (2013-06-24 16:06:01, 1.6 KB) [[attachment:step_3.py]]
  • [get | view] (2013-06-24 16:06:11, 1.1 KB) [[attachment:step_4.py]]
  • [get | view] (2013-06-24 16:06:20, 1.9 KB) [[attachment:step_5.py]]
  • [get | view] (2013-06-24 16:06:44, 2.5 KB) [[attachment:step_6.py]]
  • [get | view] (2013-06-24 16:06:53, 4.9 KB) [[attachment:step_7.py]]
  • [get | view] (2013-06-24 16:07:05, 5.9 KB) [[attachment:step_8.py]]
  • [get | view] (2013-06-30 13:51:46, 8.0 KB) [[attachment:step_9.py]]
  • [get | view] (2013-06-24 16:41:51, 1.8 KB) [[attachment:tools_1.py]]
  • [get | view] (2013-06-24 16:44:32, 2.0 KB) [[attachment:upload_to_history.py]]
  • [get | view] (2013-06-24 16:03:03, 1.3 KB) [[attachment:users_1.py]]
  • [get | view] (2013-06-24 16:44:44, 2.9 KB) [[attachment:workflows_1.py]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.