Last active
August 29, 2015 14:12
-
-
Save blakesweeney/568f542f80f1bcd14788 to your computer and use it in GitHub Desktop.
Programmatic Access to the Alignment Server
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 time | |
import requests | |
FINISHED = set(['succeeded', 'failed']) | |
# Some large and slow requests may fail because of the retry limit in requests. | |
# Set the retry higher for these cases. | |
# 3 seconds is a pretty good wait most requests should be done in < 10 seconds. | |
def fetch(*args, **kwargs): | |
response = requests.get(*args, **kwargs) | |
response.raise_for_status() | |
data = response.json() | |
while data['status'] not in FINISHED: | |
time.sleep(3) | |
response = requests.get(*args, **kwargs) | |
data = response.json() | |
return response | |
# Returned content type may be set by changing the accept | |
headers = {'Accept': 'application/json'} | |
# Access variations for one nucleotide | |
response = fetch("http://rna.bgsu.edu/r3d-2-msa", | |
params={'units': '2AW7|1|A|A|887'}, headers=headers) | |
data = response.json() | |
# Access variations while omitting the base letter | |
response = fetch("http://rna.bgsu.edu/r3d-2-msa", | |
params={'units': '2AW7|1|A||887'}, headers=headers) | |
data = response.json() | |
# Access variations for a range of nucleotides | |
# We can also use a format parameter to set the returned content type | |
response = fetch("http://rna.bgsu.edu/r3d-2-msa", | |
params={'units': '2AW7|1|A|A|887:2AW7|1|A|A|895', 'format': 'json'}) | |
data = response.json() | |
# Access variations for multiple ranges | |
response = fetch("http://rna.bgsu.edu/r3d-2-msa", | |
params={'units': '2AW7|1|A|A|887:2AW7|1|A|A|895,2AW7|1|A|G|200:2AW7|1|A|C|210', 'format': 'json'}) | |
data = response.json() | |
# It is possible to have up to 5 ranges in the list of ranges |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment