This is a test version of Biostars. For the public version, visit https://www.biostars.org.
converting Gene Names to ensembl id

Hi I have a file of gene names and I want to find their Ensembl-id using python, but I have error, how can fix it?

nDis = 100000 
with open('geo.csv', 'rt', encoding='utf-8') as csvfile:
     mg = mygene.MyGeneInfo()
    csvreader = csv.reader(csvfile, delimiter=',')
    raw_file = []
    n = 0
    counter = 0
    for row in csvreader:
        counter = counter + 1
        if counter is not 1 and counter < nDis:
            #print('Preprocessing gene data: ' + str(counter))
            d = {}
            d['geneName'] = row[9]
            raw_file.append(d)
            #print(d)
    for gene in raw_file:
        result = mg.query(gene, scopes="symbol", fields=["ensembl"], species="human", verbose=False)
        hgnc_name = gene
        for hit in result["hits"]:
            if "ensembl" in hit and "gene" in hit["ensembl"]:
                sys.stdout.write("%s\t%s\n" % (hgnc_name, hit["ensembl"]["gene"]))

HTTPError: 400 Client Error: Bad Request for url: http://mygene.info/v3/query/

gene-name ensembl-id

In the future, please properly format your post, in particular the code using the code formatting button (the one with 1s and 0s). This makes the post much easier to understand. I have done it for you this time.

Thank you so much for correcting my post, I didn't know how should I change it to code format.

1 answer

This error is because a valid request is not being made to the MyGene API via the mg.query call. scopes is not a valid parameter for your mg.query call any longer and should be removed. See the docs for how to specify that the input is a symbol, e.g.:

mg.query('symbol:cdk2', species='human')

Log in to answer this question.