<jupyter_start><jupyter_text>Exercises 3 – answers1\. Write a script to lookup the gene called BCH1 in barley and print the stable ID of this gene.<jupyter_code>import requests, sys, json
from pprint import pprint

def fetch_endpoint(server, request, content_type):

    r = requests.get(server+request, headers={ "Accept" : content_type})

    if not r.ok:
        r.raise_for_status()
        sys.exit()

    if content_type == 'application/json':
        return r.json()
    else:
        return r.text

# Get the gene name from the command line
gene_name = "BCH1"

# define the general URL parameters
server = "http://rest.ensembl.org/"
con = "application/json"
ext_get_lookup = "lookup/symbol/hordeum_vulgare/" + gene_name + "?"

# submit the query
get_lookup = fetch_endpoint(server, ext_get_lookup, con)

print (get_lookup['id'])<jupyter_output><empty_output><jupyter_text>2\. Get all phenotypes that are associated with variants in the *Arabidopsis thaliana* gene AT3G52430. For each variant print:   a. The variant ID.      b. The external reference.      c. The phenotype.<jupyter_code>import requests, sys, json
from pprint import pprint

def fetch_endpoint(server, request, content_type):

    r = requests.get(server+request, headers={ "Accept" : content_type})

    if not r.ok:
        r.raise_for_status()
        sys.exit()

    if content_type == 'application/json':
        return r.json()
    else:
        return r.text

print ("Variant\tExternal reference\tPhenotype")

# define the general URL parameters
server = "http://rest.ensembl.org/"
ext_phen = "/phenotype/gene/arabidopsis_thaliana/AT3G52430?include_associated=1"
con = "application/json"

# submit the query
get_phen = fetch_endpoint(server, ext_phen, con)

for variant in get_phen:
    id = variant['Variation']
    ref = variant['attributes']['external_reference']
    phen = variant['description']
 
    print (id + "\t" + ref + "\t" + phen)<jupyter_output><empty_output><jupyter_text>3\. Get the maize (*Zea mays*) homologue of the *Arabidopsis thaliana* CP33 and print the ID and the aligned sequence of both.Note that the JSON for the endpoint you need is several layers deep, containing nested lists (appear as square brackets [ ] in the JSON) and key value sets (appear as curly brackets { } in the JSON). Pretty print (pprint) comes in very useful here for the intermediate stage when you're trying to work out the json.<jupyter_code>import requests, sys, json
from pprint import pprint

def fetch_endpoint(server, request, content_type):

    r = requests.get(server+request, headers={ "Accept" : content_type})

    if not r.ok:
        r.raise_for_status()
        sys.exit()

    if content_type == 'application/json':
        return r.json()
    else:
        return r.text

gene = "CP33"

# define the general URL parameters
server = "http://rest.ensembl.org/"
ext_hom = "homology/symbol/arabidopsis_thaliana/" + gene + "?target_species=zea_mays"
con = "application/json"

get_hom = fetch_endpoint(server, ext_hom, con)

for datum in get_hom['data']:
    for homology in datum['homologies']:
        source_id = homology['source']['id']
        source_species = homology['source']['species']
        source_seq = homology['source']['align_seq']
        target_id = homology['target']['id']
        target_seq = homology['target']['align_seq']
        target_species = homology['target']['species']
        
        print (">", source_id + " " + source_species + "\n" + source_seq + "\n>", target_id + " " + target_species + "\n" + target_seq)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11627906976744186, 'fraction_numerical': 0.00644438217988232, 'mean_word_length': 3.857142857142857, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '655412', 'license': 'Apache-2.0', 'n_tokens_mistral': 1155, 'n_tokens_neox': 1083, 'n_words': 330, 'path': 'Plants Python3/3_Using_results_answers.ipynb', 'repo_name': 'fabiolib/rest-api-jupyter-course', 'size': '3569'}