<jupyter_start><jupyter_text>Exercises 6 – answers1\. Fetch the all the transcripts of *ESPN* using the lookup function. Fetch the cDNA sequences of all transcripts using a single POST request, and print in FASTA format.<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

def fetch_endpoint_POST(server, request, data, content_type, params):

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

    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
gene_name = "ESPN"
transcripts = []

# define the general URL parameters
server = "http://rest.ensembl.org/"
con = "application/json"
ext_get_gene = "lookup/symbol/homo_sapiens/" + gene_name + "?expand=1;"

get_gene = fetch_endpoint(server, ext_get_gene, con)

for transcript in get_gene['Transcript']:
    transcripts.append(transcript['id'])
 
data = json.dumps({ "ids" : transcripts })

ext_sequence = '/sequence/id/'

##define params in this case cdna only and lowercase mask UTRs
params = {'type':'cdna','mask_feature':1}

sequences = fetch_endpoint_POST(server, ext_sequence, data, con, params)

print()

for query in sequences:
    print (">", query['id'], sep='')
    print (query['seq'])<jupyter_output>>ENST00000468561
gatgcagccccctcccccgcaccccggaaccggcgtcgctggcgcatcctgggtggaggcaggccccgagctcggggaaggggttttcccttcctctctgacccagatctgcgcgcggcctagcccgggcctcatttcttatccccgccaagggtttcctctcagtcatttgtttaccagaaacatgaaaactgcctgtctggccgggccgcacttgtggcccccgggaccccacctctggccccacctccctcaagtctgcgccccgtccccagccagacccactcgctgccgggaccctttcactgccccggtggagtgaatagaggatgaggggccctgaccctgtgtctccaactgctgcaccccatcccgaccctgtctccgccacctcgcagccccattaaagcgctctcatctgggctccggttcactcactcgctgtggccgcgacttgctctctccttctcgggatttaataccgcgatctcagccaaactccggccgagaagttgagaaatgtcttcaccccctctcgacattcgttcgtgcttcttcgccttggctggagcgataggggcgagcaggggtggggccggctggtgctgctacgcagggccgtgccagcggcttaataagtgacataaaatgtctacacgca
>ENST00000477679
TGTTCTCAGGCATCGGGCAGCCGGCCTTCCAGGTAGGCGGGCCCAGCAGGAGCCTGCGACCCGGCTTCCCTGGCCCTAGGCCACCGGGCGCTCAGCCCCACCGCTTCTCCCTGCAGCCCGATTCGCCGCTGCCTTCTGTGTCACCTGCACTGTCACCAGTCCGGAGCCCCACACCGCCAGCTGCGGGGTTTCAGCCGCTGCTCAATGGAAGCTTGGTTCCCGTGCCGCCCACTACTCCTGCGCCGGGAGTGCAGCTGGACGTGGAGGCTCTCATCCCCACGCACGATGAGCAGGGCCGGCCCA[...]<jupyter_text>2\. You have the following list of variants:```rs1415919662, rs957333053, rs762944488, rs1372123943, rs553810871, rs1451237599, rs751376931```Get the variant class, evidence attributes, source and the most_severe_consequence for all variants using the variant POST endpoint.<jupyter_code>import requests, sys, json
from pprint import pprint

def fetch_endpoint_POST(server, request, data, content_type):

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

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

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

server = "http://rest.ensembl.org/"
con = "application/json"

variant_ids = ["rs1415919662", "rs957333053", "rs762944488", "rs1372123943", "rs553810871", "rs1451237599", "rs751376931"]

data = json.dumps({ "ids" : variant_ids })

var_ext = "variation/homo_sapiens"

post_variants = fetch_endpoint_POST(server, var_ext, data, con)

print ("ID\tClass\tEvidence\tSource\tMost severe consequence")


for key, value in post_variants.items():
    print(key)
    #print(value)
 
    id = post_variants[key]['name']
    cls = post_variants[key]['var_class']
    evidence = post_variants[key]['evidence']
    source = post_variants[key]['source']
    severe = post_variants[key]['most_severe_consequence']
 
    print (id + "\t" + cls + "\t" + ", ".join(evidence) + "\t" + source + "\t" + severe)
##example POST optional arguments

import requests, sys
server = "https://rest.ensembl.org"
ext = "/variation/homo_sapiens"
headers={ "Content-Type" : "application/json", "Accept" : "application/json"}
payload = {'pops':1}
r = requests.post(server+ext, headers=headers, params=payload, data='{ "ids" : ["rs56116432", "COSM476" ] }')
if not r.ok:
 r.raise_for_status()
 sys.exit()
decoded = r.json()
print(repr(decoded))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10598290598290598, 'fraction_numerical': 0.03653846153846154, 'mean_word_length': 4.978288633461047, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 11, 'https://': 1, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '247492', 'license': 'Apache-2.0', 'n_tokens_mistral': 1868, 'n_tokens_neox': 1666, 'n_words': 303, 'path': 'Python3/6_Using_POST_answers.ipynb', 'repo_name': 'fabiolib/rest-api-jupyter-course', 'size': '4680'}