Created
November 17, 2009 15:45
-
-
Save sb25/237007 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/python | |
| import httplib | |
| import base64 | |
| try: | |
| import json # Python 2.6 | |
| except ImportError: | |
| import simplejson as json # Python 2.4+ - http://pypi.python.org/pypi/simplejson/2.0.9 | |
| DOMAIN = "localhost:3000" | |
| USER, PASS = 'htgt', 'htgt' | |
| # Generic helper class for handling the web requests to the repository. | |
| class Connection(object): | |
| def __init__(self, domain, user, password): | |
| self.connection = httplib.HTTPConnection( domain ) | |
| auth = base64.encodestring( user + ':' + password ).strip() | |
| self.headers = { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': 'Basic ' + auth | |
| } | |
| def request(self, method, url, data = ''): | |
| method = method.upper() | |
| if method not in ['GET', 'POST', 'PUT', 'DELETE']: | |
| raise "This method (%s) is not a valid HTTP method." % method | |
| # Send request, print response status and return response body | |
| self.connection.request( method, "/%s" % url, data, self.headers ) | |
| response = self.connection.getresponse() | |
| print method, url, "-", response.status, response.reason | |
| return response.read() | |
| def close(self): | |
| self.connection.close() | |
| # Set connection to the server | |
| connection = Connection( DOMAIN, USER, PASS ) | |
| ## | |
| ## Scenario 1: search and update an allele | |
| ## | |
| allele = { | |
| 'pipeline_id' : 1, | |
| 'mgi_accession_id' : "MGI:123456", | |
| 'project_design_id' : 1, | |
| 'cassette' : "L1L2_gt2", | |
| 'backbone' : "L3L4_pZero_kan", | |
| 'assembly' : "NCBIM37", | |
| 'chromosome' : "11", | |
| 'strand' : "+", | |
| 'design_type' : "Knock Out", | |
| 'design_subtype' : "Frameshift", | |
| 'homology_arm_start' : 10, | |
| 'homology_arm_end' : 10000, | |
| 'cassette_start' : 50, | |
| 'cassette_end' : 500, | |
| 'loxp_start' : 1000, | |
| 'loxp_end' : 1500 | |
| } | |
| # Let's search for this allele using discriminating attributes | |
| search_params = "mgi_accession_id=%s" % allele['mgi_accession_id'] | |
| search_params += "&project_design_id=%s" % allele['project_design_id'] | |
| # etc. | |
| response = connection.request( 'GET', 'alleles.json?%s' % search_params ) | |
| alleles_found = json.loads( response ) # Returns a list of matches | |
| # NOTE: | |
| # The alleles_found might hold more than one allele in which case the | |
| # search (above) could be run again with more details. | |
| if alleles_found: | |
| # We now want to update our allele. | |
| allele_found = alleles_found[0] | |
| allele_json = json.dumps({ 'molecular_structure': allele }) | |
| connection.request( 'PUT', 'alleles/%s' % allele_found['id'], allele_json ) | |
| else: | |
| # No allele has been found, let's create our allele from the Hash we've made | |
| allele_json = json.dumps( { 'molecular_structure': allele } ) | |
| response = connection.request( 'POST', 'alleles', allele_json ) | |
| allele = json.loads( response ) | |
| # In this example we'll DELETE the created allele | |
| connection.request( 'DELETE', 'alleles/%s' % allele['id'] ) | |
| ## | |
| ## Scenario 2: Create an allele and all its products in a single request | |
| ## /!\ This trick will not work for an UPDATE. | |
| ## | |
| allele_and_products = { | |
| 'pipeline_id' : 1, | |
| 'mgi_accession_id' : "MGI:456789", | |
| 'project_design_id' : 2, | |
| 'cassette' : "L1L2_gt2", | |
| 'backbone' : "L3L4_pZero_kan", | |
| 'assembly' : "NCBIM37", | |
| 'chromosome' : "1", | |
| 'strand' : "+", | |
| 'design_type' : "Knock Out", | |
| 'design_subtype' : "Frameshift", | |
| 'homology_arm_start' : 10, | |
| 'homology_arm_end' : 10000, | |
| 'cassette_start' : 50, | |
| 'cassette_end' : 500, | |
| 'loxp_start' : 1000, | |
| 'loxp_end' : 1500, | |
| 'targeting_vectors' : [ | |
| { | |
| 'name' : 'PRPGD001', | |
| 'intermediate_vector' : 'PGS001', | |
| 'ikmc_project_id' : 1, | |
| 'es_cells' : [ | |
| { 'name': 'EPD001' }, | |
| { 'name': 'EPD002' }, | |
| { 'name': 'EPD003' }, | |
| ] | |
| }, | |
| { | |
| 'name' : 'PRPGD002', | |
| 'intermediate_vector' : 'PGS001', | |
| 'ikmc_project_id' : 1, | |
| 'es_cells' : [ | |
| { 'name': 'EPD004' }, | |
| { 'name': 'EPD005' }, | |
| { 'name': 'EPD006' }, | |
| ] | |
| } | |
| ], | |
| 'genbank_file': { | |
| 'escell_clone' : "A GENBANK FILE IN PLAIN TEXT", | |
| 'targeting_vector': "A GENBANK FILE IN PLAIN TEXT" | |
| } | |
| } | |
| allele_json = json.dumps( {'molecular_structure' : allele_and_products} ) | |
| response = connection.request( 'POST', 'alleles.json', allele_json ) | |
| allele = json.loads( response ) | |
| # DELETE everything | |
| for es_cell in allele['es_cells']: | |
| connection.request( 'DELETE', 'es_cells/%s' % es_cell['id'] ) | |
| for targ_vec in allele['targeting_vectors']: | |
| connection.request( 'DELETE', 'targeting_vectors/%s' % targ_vec['id'] ) | |
| connection.request( 'DELETE', 'alleles/%s' % allele['id']) | |
| # Close connection to the server | |
| connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment