Answers.Body: <p>There’s no 1:1 mapping between the IDs of different database schemas. As a consequence, some Ensembl gene IDs map to multiple MGI symbols, or to none (and vice versa).</p>
  
  <p>Therefore, you can’t assume that the number of result rows will be equal to the number of query IDs.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-03T12:31:05.400Z
Answers.LastActivityDate: 2018-05-03T12:31:05.400Z
Answers.UserDisplayName: Konrad Rudolph
Body: <p>I have a matrix with <code>ensemblIds</code> which I am trying to convert to gene symbols using <code>BioMart</code>. Some of the <code>ensemblIds</code> will not be mapped, so I need to subset the rows of the matrix. It seems like <code>BioMart</code> adds one <code>ensemblId</code> or at least I see one more result ultimately than should be. Here is what I am trying to do:</p>
  
  <pre><code>library(dplyr)
  library(tibble)
  ids &lt;- rownames(mtx_combined)
  mart &lt;- useDataset("mmusculus_gene_ensembl", useMart("ensembl"))
  list &lt;- getBM(filters= "ensembl_gene_id", attributes= c("ensembl_gene_id",
                             "mgi_symbol","description"),values=ids,mart= mart)
  mtx_mapped &lt;- as.matrix(as.data.frame(mtx_combined) %&gt;% 
  rownames_to_column("gene") %&gt;%
  filter(rownames(mtx_combined) %in% list$ensembl_gene_id) %&gt;%
  column_to_rownames("gene"))
  
  rownames(mtx_mapped) &lt;- list$mgi_symbol   //Fails 
  </code></pre>
  
  <p>Length of the returned <code>list</code> is <code>18337</code>, number of rows in the initial matrix is <code>18376</code>. After subsetting I expect to get <code>18337</code> rows, but getting one less - <code>18336</code>. What am I doing wrong here?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-02T08:58:45.853Z
Comments.Text: What is the extra line? Just print them out and see. Could it be adding a header, for example?
Comments.UserDisplayName: terdon
Tags: <r><biomart>
Title: Off-by-one error when mapping Ensembl IDs to gene symbols
ThreadText: # Off-by-one error when mapping Ensembl IDs to gene symbols
  
  (Asked by: username_0 on 2018-05-01)
  
  I have a matrix with `ensemblIds` which I am trying to convert to gene symbols using `BioMart`. Some of the `ensemblIds` will not be mapped, so I need to subset the rows of the matrix. It seems like `BioMart` adds one `ensemblId` or at least I see one more result ultimately than should be. Here is what I am trying to do:
  
  ```
  library(dplyr)
  library(tibble)
  ids <- rownames(mtx_combined)
  mart <- useDataset("mmusculus_gene_ensembl", useMart("ensembl"))
  list <- getBM(filters= "ensembl_gene_id", attributes= c("ensembl_gene_id",
                             "mgi_symbol","description"),values=ids,mart= mart)
  mtx_mapped <- as.matrix(as.data.frame(mtx_combined) %>% 
  rownames_to_column("gene") %>%
  filter(rownames(mtx_combined) %in% list$ensembl_gene_id) %>%
  column_to_rownames("gene"))
  
  rownames(mtx_mapped) <- list$mgi_symbol   //Fails 
  
  ```
  
  Length of the returned `list` is `18337`, number of rows in the initial matrix is `18376`. After subsetting I expect to get `18337` rows, but getting one less - `18336`. What am I doing wrong here?
  
  **Tags:** r, biomart
  
  > **Comments:**
  > **username_1**: What is the extra line? Just print them out and see. Could it be adding a header, for example?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-05-03. Score: 6 | Accepted answer
  
  There’s no 1:1 mapping between the IDs of different database schemas. As a consequence, some Ensembl gene IDs map to multiple MGI symbols, or to none (and vice versa).
  
  Therefore, you can’t assume that the number of result rows will be equal to the number of query IDs.
  
  
  ---
  
  