Last active
January 22, 2018 22:51
-
-
Save keithshep/6643021 to your computer and use it in GitHub Desktop.
This file contains 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
import requests | |
import json | |
# a helper function that returns the error message in case there is one and | |
# None otherwise | |
def get_err_msg(x): | |
if 'errorMessage' in x: | |
return x['errorMessage'] | |
else: | |
return None | |
# simple function to stringify a list | |
def iterable_to_str(xs, sep=' '): | |
return sep.join([str(x) for x in xs]) | |
# do a conversion using the JSON rest service and print out the results | |
def convert(req_dict): | |
# prepare and POST the JSON request | |
print '======= PERFORMING CONVERSION =======' | |
print \ | |
'converting from "%s" to "%s":' \ | |
% ((req_dict['fromUnits'] if 'fromUnits' in req_dict else 'feature name'), req_dict['toUnits']) | |
resp = requests.post( | |
#'http://localhost:8888/resources/conversion/convert.json', | |
'http://cgd.jax.org/mousemapconverter/resources/conversion/convert.json', | |
data=json.dumps(req_dict), | |
headers={'Content-type': 'application/json', 'Accept': 'application/json'}) | |
# parse the response and print the results checking for error messages | |
print 'status code:', resp.status_code | |
if resp.status_code == 200: | |
print 'JSON response:', resp.text | |
resp_coords = json.loads(resp.text) | |
err_msg = get_err_msg(resp_coords) | |
if err_msg is not None: | |
print 'conversion failed:', err_msg | |
else: | |
# iterate through all of the returned cordinates and print them out | |
for in_coord, out_coord in zip(req_dict['dataToConvert'], resp_coords): | |
err_msg = get_err_msg(out_coord) | |
if err_msg is not None: | |
print 'conversion failed:', err_msg | |
elif isinstance(in_coord, list) or isinstance(in_coord, tuple): | |
print 'input:', iterable_to_str(in_coord), 'output:', iterable_to_str(out_coord) | |
else: | |
print 'input:', in_coord, 'output:', iterable_to_str(out_coord) | |
else: | |
print 'conversion failed' | |
def main(): | |
# start by converting from genome feature IDs (so no fromUnits needed). Note | |
# that some of these IDs will be represented | |
req_dict = { | |
'toUnits' : 'mm10', | |
'dataToConvert' : [ | |
'rs3675244', | |
'rs3711314', | |
# note that the RS# will just return a chromosome and position | |
# while genes like 'Brca1' will return two positions to represent | |
# the start and end of the gene. | |
'Brca1', | |
'rs3023460', | |
'rs3707114', | |
'rs3706767'], | |
} | |
convert(req_dict) | |
# change the toUnits to the male shifman map and start again | |
req_dict['toUnits'] = 'maleShifmanMap' | |
convert(req_dict) | |
# now lets convert from coordinates and also allow per data-point errors | |
req_dict = { | |
'allowPerDataPointErrors' : True, | |
'fromUnits' : 'mm9', | |
'toUnits' : 'averageShifmanMap', | |
'dataToConvert' : [ | |
['1', 92887845], | |
# note that we can have an arbitrary number of coordinates | |
# following the chromosome identifier | |
['2', 7821365, 8821365, 9821365], | |
['1', 172956646], | |
['5', 92572826], | |
['6', 47433029], | |
# because we set allowPerDataPointErrors to True we should | |
# get an error message for this element and successfully | |
# process the others | |
['bogus_chromosome', 47433029], | |
['4', 41364422], | |
['1', 131246731], | |
['Chr7', 61246731], | |
['X', 8574635, 9253654], | |
] | |
} | |
convert(req_dict) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment