I have many scRNAseq datasets saved as hdf5 files, and when I open them keys sometimes are confused, so I need a way of determining from string values themselves whether the array contains gene symbols or ensembl ids. What would be a regular expression in python to check for it?


Fixed it myself actually, not that hard. I found regular expression for ensembl ids here:


And used the re package in python:

import re

ensembl_regexp = 'ENS[A-Z]+[0-9]{11}'

is_ensembl = np.all([bool(re.match(ensembl_regexp, gene)) for gene in gene_symbols])
    if is_ensembl:
        gene_symbols = list(f['ensembl_ids']) 

Where gene_symbols is a numpy array that I need to check whether it has ensembl ids, and if yes, switch to using gene symbols which are going to be under ensembl_ids key due to a mistake of someone who made the hdf5 dataset.

| improve this answer | |
  • $\begingroup$ What kind of Ensembl IDs are matched by the second part of that regex? ENS[A-Z]+[0-9]{11} matches the standard Ensembl IDs, what does the rest do? It looks like it matches FlyBase IDs (CG[0-9]+), and all sorts of other things. For example, that regex would consider CG1 or ABC123bobbie valid Ensembl IDs. $\endgroup$ – terdon Sep 2 '18 at 11:13
  • $\begingroup$ Not sure, that’s what I got from the ‘Biostars’ link that is present in the answer. Maybe ask the person who answered it on ‘Biostars’ ? $\endgroup$ – Nikita Vlasenko Sep 2 '18 at 20:34
  • 1
    $\begingroup$ Is this your question originally? Did you test this solution? The regex matches all sorts of IDs, many of which are not Ensembl. I have edited your answer so that it only matches Ensembl IDs as the question asks for. $\endgroup$ – terdon Sep 3 '18 at 8:22
  • $\begingroup$ Of course, this is still not entirely correct since it will allow something like ENSABCDEFGHIJKL12345678901 which is clearly not a valid ID. $\endgroup$ – terdon Sep 3 '18 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.