ID mapping using mygene module in Python

ID mapping is a very common, often not fun, task for every bioinformatician. Supposedly you have a list of gene symbols or reporter ids from an upstream analysis, and then your next analysis requires to use gene ids (e.g. Entrez gene ids or Ensembl gene ids). So you want to convert that list of gene symbols or reporter ids to corresponding gene ids.

Here we want to show you how to use mygene module in Python to do ID mapping quickly and easily. mygene is essentially a convenient Python module to access MyGene.info gene query web services.

Install mygene

Install mygene is easy, as pip is your friend:

   pip install mygene

Now you just need to import it and instantiate MyGeneInfo class:

In [1]:
import mygene

mg = mygene.MyGeneInfo()

Mapping gene symbols to Entrez gene ids

Suppose xli is a list of gene symbols you want to convert to entrez gene ids:

In [2]:
xli = ['DDX26B',
 'CCDC83',
 'MAST3',
 'FLOT1',
 'RPL11',
 'ZDHHC20',
 'LUC7L3',
 'SNORD49A',
 'CTSH',
 'ACOT8']

you can then call querymany method, telling it your input is "symbol", and you want "entrezgene" (Entrez gene ids) back.

In [3]:
out = mg.querymany(xli, scopes='symbol', fields='entrezgene', species='human')
Finished.

Returned "out" looks like this:

In [4]:
out
Out[4]:
[{u'_id': u'203522', u'entrezgene': 203522, u'query': u'DDX26B'},
 {u'_id': u'220047', u'entrezgene': 220047, u'query': u'CCDC83'},
 {u'_id': u'23031', u'entrezgene': 23031, u'query': u'MAST3'},
 {u'_id': u'10211', u'entrezgene': 10211, u'query': u'FLOT1'},
 {u'_id': u'6135', u'entrezgene': 6135, u'query': u'RPL11'},
 {u'_id': u'253832', u'entrezgene': 253832, u'query': u'ZDHHC20'},
 {u'_id': u'51747', u'entrezgene': 51747, u'query': u'LUC7L3'},
 {u'_id': u'26800', u'entrezgene': 26800, u'query': u'SNORD49A'},
 {u'_id': u'1512', u'entrezgene': 1512, u'query': u'CTSH'},
 {u'_id': u'10005', u'entrezgene': 10005, u'query': u'ACOT8'}]

The mapping result is returned as a list of dictionaries. Each dictionary contains the fields you asked to return, in this case, "entrezgene" field. Each dictionary also returns the matching query term, "query", and an internal id, "_id", which is the same as "entrezgene" most of time (will be an ensembl gene id if a gene is available from Ensembl only).

Mapping gene symbols to Ensembl gene ids

Now if you want Ensembl gene ids back:

In [5]:
mg.querymany(xli, scopes='symbol', fields='ensembl.gene', species='human')
Finished.
Out[5]:
[{u'_id': u'203522',
  u'ensembl.gene': [u'ENSG00000165359', u'ENSG00000268630'],
  u'query': u'DDX26B'},
 {u'_id': u'220047', u'ensembl.gene': u'ENSG00000150676', u'query': u'CCDC83'},
 {u'_id': u'23031', u'ensembl.gene': u'ENSG00000099308', u'query': u'MAST3'},
 {u'_id': u'10211',
  u'ensembl.gene': [u'ENSG00000137312',
   u'ENSG00000206379',
   u'ENSG00000206480',
   u'ENSG00000223654',
   u'ENSG00000224740',
   u'ENSG00000230143',
   u'ENSG00000232280',
   u'ENSG00000236271'],
  u'query': u'FLOT1'},
 {u'_id': u'6135', u'ensembl.gene': u'ENSG00000142676', u'query': u'RPL11'},
 {u'_id': u'253832',
  u'ensembl.gene': u'ENSG00000180776',
  u'query': u'ZDHHC20'},
 {u'_id': u'51747', u'ensembl.gene': u'ENSG00000108848', u'query': u'LUC7L3'},
 {u'_id': u'26800',
  u'ensembl.gene': [u'ENSG00000206956', u'ENSG00000175061'],
  u'query': u'SNORD49A'},
 {u'_id': u'1512', u'ensembl.gene': u'ENSG00000103811', u'query': u'CTSH'},
 {u'_id': u'10005', u'ensembl.gene': u'ENSG00000101473', u'query': u'ACOT8'}]

When an input id has no matching gene

In case that an input id has no matching gene, you will be notified from the output.The returned dictionary for this query term contains "notfound" value as True.

In [6]:
xli = ['DDX26B',
 'CCDC83',
 'MAST3',
 'FLOT1',
 'RPL11',
 'Gm10494']
In [7]:
mg.querymany(xli, scopes='symbol', fields='entrezgene', species='human')
Finished.
1 input query terms found no hit:
	[u'Gm10494']
Pass "returnall=True" to return complete lists of duplicate or missing query terms.
Out[7]:
[{u'_id': u'203522', u'entrezgene': 203522, u'query': u'DDX26B'},
 {u'_id': u'220047', u'entrezgene': 220047, u'query': u'CCDC83'},
 {u'_id': u'23031', u'entrezgene': 23031, u'query': u'MAST3'},
 {u'_id': u'10211', u'entrezgene': 10211, u'query': u'FLOT1'},
 {u'_id': u'6135', u'entrezgene': 6135, u'query': u'RPL11'},
 {u'notfound': True, u'query': u'Gm10494'}]

When input ids are not just symbols

In [8]:
xli = ['DDX26B',
 'CCDC83',
 'MAST3',
 'FLOT1',
 'RPL11',
 'Gm10494',
 '1007_s_at',
 'AK125780']

Above id list contains symbols, reporters and accession numbers, and supposedly we want to get back both Entrez gene ids and uniprot ids. Parameters scopes, fields, species are all flexible enough to support multiple values, either a list or a comma-separated string:

In [9]:
mg.querymany(xli, scopes='symbol,reporter,accession', fields='entrezgene,uniprot', species='human')
Finished.
1 input query terms found dup hits:
	[(u'1007_s_at', 2)]
1 input query terms found no hit:
	[u'Gm10494']
Pass "returnall=True" to return complete lists of duplicate or missing query terms.
Out[9]:
[{u'_id': u'203522',
  u'entrezgene': 203522,
  u'query': u'DDX26B',
  u'uniprot': {u'Swiss-Prot': u'Q5JSJ4'}},
 {u'_id': u'220047',
  u'entrezgene': 220047,
  u'query': u'CCDC83',
  u'uniprot': {u'Swiss-Prot': u'Q8IWF9'}},
 {u'_id': u'23031',
  u'entrezgene': 23031,
  u'query': u'MAST3',
  u'uniprot': {u'Swiss-Prot': u'O60307'}},
 {u'_id': u'10211',
  u'entrezgene': 10211,
  u'query': u'FLOT1',
  u'uniprot': {u'Swiss-Prot': u'O75955', u'TrEMBL': u'Q5ST80'}},
 {u'_id': u'6135',
  u'entrezgene': 6135,
  u'query': u'RPL11',
  u'uniprot': {u'Swiss-Prot': u'P62913', u'TrEMBL': u'Q5VVD0'}},
 {u'notfound': True, u'query': u'Gm10494'},
 {u'_id': u'100616237', u'entrezgene': 100616237, u'query': u'1007_s_at'},
 {u'_id': u'780',
  u'entrezgene': 780,
  u'query': u'1007_s_at',
  u'uniprot': {u'Swiss-Prot': u'Q08345', u'TrEMBL': [u'Q96T61', u'Q96T62']}},
 {u'_id': u'2978',
  u'entrezgene': 2978,
  u'query': u'AK125780',
  u'uniprot': {u'Swiss-Prot': u'P43080'}}]

When a input id has multiple matching genes

From the previous result, you may have noticed that query term "1007_s_at" matches two genes. In that case, you will be notified from the output, and the returned result will include both matching genes.

By passing "returnall=True", you will get both duplicate or missing query terms, together with the mapping output, from the returned result:

In [10]:
mg.querymany(xli, scopes='symbol,reporter,accession', fields='entrezgene,uniprot', species='human', returnall=True)
Finished.
1 input query terms found dup hits:
	[(u'1007_s_at', 2)]
1 input query terms found no hit:
	[u'Gm10494']
Out[10]:
{'dup': [(u'1007_s_at', 2)],
 'missing': [u'Gm10494'],
 'out': [{u'_id': u'203522',
   u'entrezgene': 203522,
   u'query': u'DDX26B',
   u'uniprot': {u'Swiss-Prot': u'Q5JSJ4'}},
  {u'_id': u'220047',
   u'entrezgene': 220047,
   u'query': u'CCDC83',
   u'uniprot': {u'Swiss-Prot': u'Q8IWF9'}},
  {u'_id': u'23031',
   u'entrezgene': 23031,
   u'query': u'MAST3',
   u'uniprot': {u'Swiss-Prot': u'O60307'}},
  {u'_id': u'10211',
   u'entrezgene': 10211,
   u'query': u'FLOT1',
   u'uniprot': {u'Swiss-Prot': u'O75955', u'TrEMBL': u'Q5ST80'}},
  {u'_id': u'6135',
   u'entrezgene': 6135,
   u'query': u'RPL11',
   u'uniprot': {u'Swiss-Prot': u'P62913', u'TrEMBL': u'Q5VVD0'}},
  {u'notfound': True, u'query': u'Gm10494'},
  {u'_id': u'100616237', u'entrezgene': 100616237, u'query': u'1007_s_at'},
  {u'_id': u'780',
   u'entrezgene': 780,
   u'query': u'1007_s_at',
   u'uniprot': {u'Swiss-Prot': u'Q08345', u'TrEMBL': [u'Q96T61', u'Q96T62']}},
  {u'_id': u'2978',
   u'entrezgene': 2978,
   u'query': u'AK125780',
   u'uniprot': {u'Swiss-Prot': u'P43080'}}]}

The returned result above contains "out" for mapping output, "missing" for missing query terms (a list), and "dup" for query terms with multiple matches (including the number of matches).

Can I convert a very large list of ids?

Yes, you can. If you pass an id list (i.e., xli above) larger than 1000 ids, we will do the id mapping in-batch with 1000 ids at a time, and then concatenate the results all together for you. So, from the user-end, it's exactly the same as passing a shorter list. You don't need to worry about saturating our backend servers.

To read more