Bioconductor / LoomExperiment

A package to read, write, and manipulate loom files using LoomExperiments. Uses the loom file format from the Linnarson Lab. https://linnarssonlab.org/loompy/
https://www.bioconductor.org/packages/LoomExperiment
6 stars 5 forks source link

How can I add rowname to the matrix? #15

Closed zh-zhang1984 closed 3 years ago

zh-zhang1984 commented 3 years ago

Hi, thank you for the valuable package. When I read a .loom file, how can I add rowname (gene name) to the matrix?

> ldat <- LoomExperiment::import(
+     con ="/Users/zhangzhongheng/Documents/2021/singleCellRNA/loom/HZ01_d1.loom"
+ )
> ldat
class: LoomExperiment 
dim: 32864 13673 
metadata(5): last_modified CreationDate LOOM_SPEC_VERSION velocyto.__version__
  velocyto.logic
assays(4): matrix ambiguous spliced unspliced
rownames: NULL
rowData names(6): Accession Chromosome ... Start Strand
colnames: NULL
colData names(1): CellID
rowGraphs(0): NULL
colGraphs(0): NULL

The above code cannot parse the rownames as indicated in rownames: NULL;

LiNk-NY commented 3 years ago

Hi Zhongheng, @zh-zhang1984

It depends on where the data in the file are. I know that there are some conventions to follow based on http://linnarssonlab.org/loompy/conventions/index.html If your file does not follow that convention you will not get the rownames added to the LoomExperiment object.

Here is an example from the vignette:

suppressPackageStartupMessages({
    library(LoomExperiment)
    library(rhdf5)
})
l1_file <-
    system.file("extdata", "L1_DRG_20_example.loom", package = "LoomExperiment")
h5read(l1_file, name = "/row_attrs/Gene")
#>  [1] "Nnat"          "Rasl10a"       "A3galt2"       "Adamts8"      
#>  [5] "Prmt8"         "Sdc1"          "1700011I03Rik" "Tprg"         
#>  [9] "Otoa"          "Pvrl1"         "Plek2"         "Kcnk12"       
#> [13] "Lypd6b"        "Ccdc60"        "Dmrtb1"        "Gm16364"      
#> [17] "Pi16"          "Klk8"          "Ly86"          "Smim18"

Created on 2021-09-02 by the reprex package (v2.0.1)

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.1.1 Patched (2021-08-22 r80813) #> os Ubuntu 20.10 #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/New_York #> date 2021-09-02 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> backports 1.2.1 2020-12-09 [1] CRAN (R 4.1.0) #> Biobase * 2.53.0 2021-05-19 [1] Bioconductor #> BiocGenerics * 0.39.2 2021-08-18 [1] Bioconductor #> BiocIO * 1.3.0 2021-05-19 [1] Bioconductor #> bitops 1.0-7 2021-04-24 [1] RSPM (R 4.2.0) #> cli 3.0.1 2021-07-17 [1] RSPM (R 4.1.0) #> crayon 1.4.1 2021-02-08 [1] CRAN (R 4.1.0) #> DelayedArray 0.19.1 2021-06-25 [1] Bioconductor #> digest 0.6.27 2020-10-24 [1] RSPM (R 4.1.0) #> ellipsis 0.3.2 2021-04-29 [1] RSPM (R 4.2.0) #> evaluate 0.14 2019-05-28 [1] CRAN (R 4.1.0) #> fansi 0.5.0 2021-05-25 [1] RSPM (R 4.1.0) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.0) #> fs 1.5.0 2020-07-31 [1] CRAN (R 4.1.0) #> GenomeInfoDb * 1.29.4 2021-08-25 [1] Bioconductor #> GenomeInfoDbData 1.2.6 2021-05-12 [1] Bioconductor #> GenomicRanges * 1.45.0 2021-05-19 [1] Bioconductor #> glue 1.4.2 2020-08-27 [1] CRAN (R 4.1.0) #> HDF5Array 1.21.0 2021-05-19 [1] Bioconductor #> highr 0.9 2021-04-16 [1] RSPM (R 4.2.0) #> htmltools 0.5.2 2021-08-25 [1] RSPM (R 4.1.1) #> IRanges * 2.27.2 2021-08-23 [1] Bioconductor #> knitr 1.33 2021-04-24 [1] RSPM (R 4.2.0) #> lattice 0.20-44 2021-05-02 [2] CRAN (R 4.1.1) #> lifecycle 1.0.0 2021-02-15 [1] RSPM (R 4.1.0) #> LoomExperiment * 1.11.1 2021-09-02 [1] Bioconductor #> magrittr 2.0.1 2020-11-17 [1] RSPM (R 4.1.0) #> Matrix 1.3-4 2021-06-01 [2] CRAN (R 4.1.1) #> MatrixGenerics * 1.5.4 2021-08-26 [1] Bioconductor #> matrixStats * 0.60.1 2021-08-23 [1] RSPM (R 4.1.1) #> pillar 1.6.2 2021-07-29 [1] RSPM (R 4.1.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.0) #> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.1.0) #> RCurl 1.98-1.4 2021-08-17 [1] RSPM (R 4.1.0) #> reprex 2.0.1 2021-08-05 [1] RSPM (R 4.1.0) #> rhdf5 * 2.37.0 2021-05-19 [1] Bioconductor #> rhdf5filters 1.5.0 2021-05-19 [1] Bioconductor #> Rhdf5lib 1.15.2 2021-07-01 [1] Bioconductor #> rlang 0.4.11 2021-04-30 [1] RSPM (R 4.2.0) #> rmarkdown 2.10 2021-08-06 [1] RSPM (R 4.1.0) #> S4Vectors * 0.31.3 2021-08-26 [1] Bioconductor #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.1.0) #> SingleCellExperiment * 1.15.2 2021-08-27 [1] Bioconductor #> stringi 1.7.4 2021-08-25 [1] RSPM (R 4.1.1) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.1.0) #> styler 1.5.1 2021-07-13 [1] RSPM (R 4.1.0) #> SummarizedExperiment * 1.23.4 2021-08-25 [1] Bioconductor #> tibble 3.1.4 2021-08-25 [1] RSPM (R 4.1.1) #> utf8 1.2.2 2021-07-24 [1] RSPM (R 4.1.0) #> vctrs 0.3.8 2021-04-29 [1] RSPM (R 4.2.0) #> withr 2.4.2 2021-04-18 [1] RSPM (R 4.2.0) #> xfun 0.25 2021-08-06 [1] RSPM (R 4.1.0) #> XVector 0.33.0 2021-05-19 [1] Bioconductor #> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.1.0) #> zlibbioc 1.39.0 2021-05-19 [1] Bioconductor #> #> [1] /media/mr148/1D24A0EA4286043C1/bioc-devel #> [2] /home/mr148/src/svn/r-release/R/lib/R/library ```

If you already have the gene names, you can use the rownames<- setter method For example:

l1_file <- system.file(
    "extdata", "L1_DRG_20_example.loom", package = "LoomExperiment"
)
le <- import(l1_file, type="LoomExperiment")
rownames(le) <- LETTERS[1:20]
zh-zhang1984 commented 3 years ago

Thank you for the useful suggestion, I successfully added therownames. However, How may I add colnames for the dataset?

LiNk-NY commented 3 years ago

colnames(le) <- LETTERS[1:20]

mtmorgan commented 3 years ago

Note that these steps are adding row names to the in-memory representation; I think it's better to stick with the Loom file format restriction and access row attributes, e.g., rowData(loom), maybe selecting particular rows with row_index <- rowData(loom)$Gene %in% my_genes; loom[row_index,]`

zh-zhang1984 commented 3 years ago

I can solve this issue; thank you so much for the help.