Answers.Body: <p>Fixed it myself actually, not that hard. I found <code>regular expression</code> for <code>ensembl ids</code> here:</p>
  
  <p><a href="https://www.biostars.org/p/99142/" rel="nofollow noreferrer">https://www.biostars.org/p/99142/</a></p>
  
  <p>And used the <code>re</code> package in <code>python</code>:</p>
  
  <pre><code>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']) 
  </code></pre>
  
  <p>Where <code>gene_symbols</code> is a <code>numpy</code> array that I need to check whether it has <code>ensembl ids</code>, and if yes, switch to using <code>gene symbols</code> which are going to be under <code>ensembl_ids</code> key due to a mistake of someone who made the <code>hdf5</code> dataset.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-02T11:13:17.173Z
Answers.Text: 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.
Answers.UserDisplayName: terdon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-02T20:34:34.503Z
Answers.Text: 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’ ?
Answers.UserDisplayName: Nikita Vlasenko
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-03T08:22:32.727Z
Answers.Text: 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.
Answers.UserDisplayName: terdon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-03T09:30:11.397Z
Answers.Text: Of course, this is still not entirely correct since it will allow something like `ENSABCDEFGHIJKL12345678901` which is clearly not a valid ID.
Answers.UserDisplayName: terdon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-01T23:19:42.553Z
Answers.LastActivityDate: 2018-09-03T08:22:47.860Z
Answers.LastEditDate: 2018-09-03T08:22:47.860Z
Answers.UserDisplayName: Nikita Vlasenko
Answers.Body: <p>The <a href="https://bioinformatics.stackexchange.com/a/4966/9750">answer by terdon</a> is probably good enough, but some commenters have noted it will allow something like <code>ENSABCDEFGHIJKL12345678901</code>.</p>
  <p>It's helpful to reference the official documentation of <a href="https://uswest.ensembl.org/info/genome/stable_ids/index.html" rel="nofollow noreferrer">Ensembl Stable Ids</a>:</p>
  <blockquote>
  <p>Stable IDs are created in the form ENS[species prefix][feature type prefix][a unique eleven digit number]. For example a mouse gene might be ENSMUSG###########. This means that we can immediately tell from a stable ID what kind of feature they refer to and what species they are in. The prefixes are listed on our <a href="https://uswest.ensembl.org/info/genome/stable_ids/prefixes.html" rel="nofollow noreferrer">prefix page</a>.</p>
  </blockquote>
  <p>The two places I look first for regexes are identifiers.org and wikidata. If these are wrong, best to report that upstream. Here are their ensembl regexes:</p>
  <p>The regex used by the <a href="https://registry.identifiers.org/registry/ensembl" rel="nofollow noreferrer">identifiers.org registry</a> (for any ensembl ID) is quite long:</p>
  <pre><code>^((ENS[FPTG]\\d{11}(\\.\\d+)?)|(FB\\w{2}\\d{7})|(Y[A-Z]{2}\\d{3}[a-zA-Z](\\-[A-Z])?)|([A-Z_a-z0-9]+(\\.)?(t)?(\\d+)?([a-z])?))$
  </code></pre>
  <p>Meanwhile Wikidata has this regex for an <a href="https://www.wikidata.org/wiki/Property:P594" rel="nofollow noreferrer">Ensembl gene ID</a>:</p>
  <pre><code>(ENS(|MUS|RNO)G\d{11})|(Y[A-P][LR]\d{3}[CW](-[A-G])?)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-11T17:02:57.150Z
Answers.LastActivityDate: 2020-12-11T17:02:57.150Z
Answers.UserDisplayName: Daniel Himmelstein
Body: <p>I have many <code>scRNAseq</code> datasets saved as <code>hdf5</code> files (hierarchical data format*), and when I open them keys sometimes are confused, so I need a way of determining from string values themselves whether the array contains <code>gene symbols</code> or <code>ensembl ids</code>. What would be a regular expression in python to check for it?</p>
  <p>*, A database format for quickly accessing large amounts of data, which is accessed using <code>keys</code>. The format is quicker than pickle/unpickle and can be viewed via a hdf5 viewer, a bit of info is <a href="https://support.hdfgroup.org/HDF5/whatishdf5.html" rel="nofollow noreferrer">here</a></p>
  
Tags: <python><ensembl>
Title: Check if a string is ensembl gene id or not in python
ThreadText: # Check if a string is ensembl gene id or not in python
  
  (Asked by: username_0 on 2018-09-01)
  
  I have many `scRNAseq` datasets saved as `hdf5` files (hierarchical data format\*), 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?
  
  \*, A database format for quickly accessing large amounts of data, which is accessed using `keys`. The format is quicker than pickle/unpickle and can be viewed via a hdf5 viewer, a bit of info is [here](https://support.hdfgroup.org/HDF5/whatishdf5.html)
  
  **Tags:** python, ensembl
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2018-09-01. Score: 2
  
  Fixed it myself actually, not that hard. I found `regular expression` for `ensembl ids` here:
  
  <https://www.biostars.org/p/99142/>
  
  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.
  
  > **Comments:**
  > **username_1**: 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.  
  > **username_0**: 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’ ?  
  > **username_1**: 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.  
  > **username_1**: Of course, this is still not entirely correct since it will allow something like `ENSABCDEFGHIJKL12345678901` which is clearly not a valid ID.  
  
  ---
  
  ### Answer by username_2 on 2020-12-11. Score: 2
  
  The [answer by username_1](https://bioinformatics.stackexchange.com/a/4966/9750) is probably good enough, but some commenters have noted it will allow something like `ENSABCDEFGHIJKL12345678901`.
  
  It's helpful to reference the official documentation of [Ensembl Stable Ids](https://uswest.ensembl.org/info/genome/stable_ids/index.html):
  
  > Stable IDs are created in the form ENS[species prefix][feature type prefix][a unique eleven digit number]. For example a mouse gene might be ENSMUSG###########. This means that we can immediately tell from a stable ID what kind of feature they refer to and what species they are in. The prefixes are listed on our [prefix page](https://uswest.ensembl.org/info/genome/stable_ids/prefixes.html).
  
  The two places I look first for regexes are identifiers.org and wikidata. If these are wrong, best to report that upstream. Here are their ensembl regexes:
  
  The regex used by the [identifiers.org registry](https://registry.identifiers.org/registry/ensembl) (for any ensembl ID) is quite long:
  
  ```
  ^((ENS[FPTG]\\d{11}(\\.\\d+)?)|(FB\\w{2}\\d{7})|(Y[A-Z]{2}\\d{3}[a-zA-Z](\\-[A-Z])?)|([A-Z_a-z0-9]+(\\.)?(t)?(\\d+)?([a-z])?))$
  
  ```
  
  Meanwhile Wikidata has this regex for an [Ensembl gene ID](https://www.wikidata.org/wiki/Property:P594):
  
  ```
  (ENS(|MUS|RNO)G\d{11})|(Y[A-P][LR]\d{3}[CW](-[A-G])?)
  
  ```
  
  
  ---
  
  