This is a test version of Biostars. For the public version, visit https://www.biostars.org.
From Seurat object to a dataframe

I am doing single-cell RNA analysis and I have Seurat files, but now I would like to transform them into dataframes and I cannot find anything that works. This are all the command that I try to perform. Do you have any idea?

data <- CreateSeuratObject(counts = data, project = "scRNA", min.cells = 3, min.features = 200) 
...
G2M_cell <- ScaleData(G2M_cell)
dataf <- as.data.frame(G2M_cell)

Error in as.data.frame.default(G2M_cell) : 
  coercizione di classe ‘structure("Seurat", package = "SeuratObject")’ in data.frame non possibile
a <- as.data.frame(G2M_cell, genes = Seurat::VariableFeatures(G2M_cell),fix_names = TRUE)
Error in as.data.frame.default(G2M_cell, genes = Seurat::VariableFeatures(G2M_cell),  : 
  coercizione di classe ‘structure("Seurat", package = "SeuratObject")’ in data.frame non possibile
S4_to_dataframe <- function(s4obj) {
  nms <- slotNames(s4obj)

  lst <- lapply(nms, function(nm) slot(s4obj, nm))
  as.data.frame(setNames(lst, nms))
}

S4_to_dataframe(G2M_cell)

Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  coercizione di classe ‘structure("Assay", package = "SeuratObject")’ in data.frame non possibile
> sessionInfo()
R version 4.1.3 (2022-03-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)

Matrix products: default

locale:
[1] LC_COLLATE=Italian_Italy.1252  LC_CTYPE=Italian_Italy.1252   
[3] LC_MONETARY=Italian_Italy.1252 LC_NUMERIC=C                  
[5] LC_TIME=Italian_Italy.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] reticulate_1.26    magrittr_2.0.3     forcats_0.5.2      stringr_1.4.1     
 [5] purrr_0.3.4        readr_2.1.3        tidyr_1.2.1        tibble_3.1.8      
 [9] tidyverse_1.3.2    ggplot2_3.3.6      RaceID_0.2.7       sp_1.5-0          
[13] SeuratObject_4.1.2 Seurat_4.2.0       dplyr_1.0.10       R.utils_2.12.1    
[17] R.oo_1.25.0        R.methodsS3_1.8.2 

loaded via a namespace (and not attached):
  [1] rappdirs_0.3.3                          rtracklayer_1.54.0                     
  [3] scattermore_0.8                         princurve_2.1.6                        
  [5] knitr_1.40                              bit64_4.0.5                            
  [7] irlba_2.3.5.1                           DelayedArray_0.20.0                    
  [9] data.table_1.14.2                       rpart_4.1.16                           
 [11] KEGGREST_1.34.0                         RCurl_1.98-1.8                         
 [13] generics_0.1.3                          BiocGenerics_0.40.0                    
 [15] GenomicFeatures_1.46.5                  cowplot_1.1.1                          
 [17] RSQLite_2.2.17                          shadowtext_0.1.2                       
 [19] RANN_2.6.1                              future_1.29.0                          
 [21] tzdb_0.3.0                              bit_4.0.4                              
 [23] enrichplot_1.14.2                       lubridate_1.8.0                        
 [25] spatstat.data_3.0-0                     xml2_1.3.3                             
 [27] httpuv_1.6.6                            SummarizedExperiment_1.24.0            
 [29] assertthat_0.2.1                        gargle_1.2.1                           
 [31] viridis_0.6.2                           xfun_0.34                              
 [33] hms_1.1.2                               evaluate_0.18                          
 [35] promises_1.2.0.1                        fansi_1.0.3                            
 [37] restfulr_0.0.15                         progress_1.2.2                         
 [39] readxl_1.4.1                            caTools_1.18.2                         
 [41] dbplyr_2.2.1                            igraph_1.3.4                           
 [43] DBI_1.1.3                               htmlwidgets_1.5.4                      
 [45] spatstat.geom_3.0-3                     googledrive_2.0.0                      
 [47] stats4_4.1.3                            ellipsis_0.3.2                         
 [49] RSpectra_0.16-1                         backports_1.4.1                        
 [51] permute_0.9-7                           biomaRt_2.50.3                         
 [53] deldir_1.0-6                            MatrixGenerics_1.6.0                   
 [55] SingleCellExperiment_1.16.0             vctrs_0.4.1                            
 [57] Biobase_2.54.0                          ROCR_1.0-11                            
 [59] abind_1.4-5                             cachem_1.0.6                           
 [61] withr_2.5.0                             ggforce_0.3.4                          
 [63] progressr_0.11.0                        sctransform_0.3.5                      
 [65] vegan_2.6-4                             GenomicAlignments_1.30.0               
 [67] FateID_0.2.2                            treeio_1.18.1                          
 [69] prettyunits_1.1.1                       goftest_1.2-3                          
 [71] cluster_2.1.2                           DOSE_3.20.1                            
 [73] ape_5.6-2                               lazyeval_0.2.2                         
 [75] crayon_1.5.2                            labeling_0.4.2                         
 [77] runner_0.4.2                            pkgconfig_2.0.3                        
 [79] tweenr_2.0.2                            GenomeInfoDb_1.30.1                    
 [81] nlme_3.1-155                            rlang_1.0.6                            
 [83] globals_0.16.1                          lifecycle_1.0.3                        
 [85] miniUI_0.1.1.1                          downloader_0.4                         
 [87] filelock_1.0.2                          BiocFileCache_2.2.1                    
 [89] modelr_0.1.9                            cellranger_1.1.0                       
 [91] randomForest_4.7-1.1                    polyclip_1.10-0                        
 [93] matrixStats_0.62.0                      lmtest_0.9-40                          
 [95] som_0.3-5.1                             Matrix_1.5-1                           
 [97] aplot_0.1.8                             boot_1.3-28                            
 [99] zoo_1.8-11                              reprex_2.0.2                           
[101] googlesheets4_1.0.1                     ggridges_0.5.4                         
[103] pheatmap_1.0.12                         png_0.1-7                              
[105] viridisLite_0.4.1                       rjson_0.2.21                           
[107] bitops_1.0-7                            KernSmooth_2.23-20                     
[109] Biostrings_2.62.0                       blob_1.2.3                             
[111] qvalue_2.26.0                           parallelly_1.32.1                      
[113] spatstat.random_3.0-1                   gridGraphics_0.5-1                     
[115] S4Vectors_0.32.4                        scales_1.2.1                           
[117] memoise_2.0.1                           plyr_1.8.7                             
[119] ica_1.0-3                               gplots_3.1.3                           
[121] zlibbioc_1.40.0                         compiler_4.1.3                         
[123] scatterpie_0.1.8                        BiocIO_1.4.0                           
[125] RColorBrewer_1.1-3                      plotrix_3.8-2                          
[127] fitdistrplus_1.1-8                      Rsamtools_2.10.0                       
[129] cli_3.3.0                               XVector_0.34.0                         
[131] listenv_0.8.0                           patchwork_1.1.2                        
[133] pbapply_1.5-0                           MASS_7.3-55                            
[135] mgcv_1.8-39                             tidyselect_1.2.0                       
[137] stringi_1.7.6                           yaml_2.3.5                             
[139] GOSemSim_2.20.0                         askpass_1.1                            
[141] locfit_1.5-9.6                          ggrepel_0.9.1                          
[143] grid_4.1.3                              fastmatch_1.1-3                        
[145] tools_4.1.3                             future.apply_1.10.0                    
[147] parallel_4.1.3                          rstudioapi_0.14                        
[149] TxDb.Hsapiens.UCSC.hg19.knownGene_3.2.2 gridExtra_2.3                          
[151] farver_2.1.1                            Rtsne_0.16                             
[153] ggraph_2.0.6                            BiocManager_1.30.19                    
[155] digest_0.6.29                           rgeos_0.5-9                            
[157] FNN_1.1.3.1                             shiny_1.7.3                            
[159] quadprog_1.5-8                          Rcpp_1.0.9                             
[161] broom_1.0.1                             GenomicRanges_1.46.1                   
[163] later_1.3.0                             RcppAnnoy_0.0.20                       
[165] httr_1.4.4                              AnnotationDbi_1.56.2                   
[167] colorspace_2.0-3                        rvest_1.0.3                            
[169] fs_1.5.2                                XML_3.99-0.10                          
[171] tensor_1.5                              umap_0.2.9.0                           
[173] IRanges_2.28.0                          splines_4.1.3                          
[175] uwot_0.1.14                             yulab.utils_0.0.5                      
[177] tidytree_0.4.1                          spatstat.utils_3.0-1                   
[179] graphlayouts_0.8.1                      ggplotify_0.1.0                        
[181] plotly_4.10.1                           xtable_1.8-4                           
[183] jsonlite_1.8.0                          ggtree_3.2.1                           
[185] tidygraph_1.2.2                         ggfun_0.0.8                            
[187] R6_2.5.1                                pillar_1.8.1                           
[189] htmltools_0.5.3                         mime_0.12                              
[191] glue_1.6.2                              fastmap_1.1.0                          
[193] clusterProfiler_4.2.2                   BiocParallel_1.28.3                    
[195] codetools_0.2-18                        ChIPseeker_1.30.3                      
[197] fgsea_1.20.0                            utf8_1.2.2                             
[199] lattice_0.20-45                         spatstat.sparse_3.0-0                  
[201] curl_4.3.2                              leiden_0.4.3                           
[203] gtools_3.9.3                            GO.db_3.14.0                           
[205] openssl_2.0.4                           limma_3.50.3                           
[207] survival_3.4-0                          rmarkdown_2.17                         
[209] munsell_0.5.0                           DO.db_2.9                              
[211] GenomeInfoDbData_1.2.7                  haven_2.5.1                            
[213] reshape2_1.4.4                          gtable_0.3.1                           
[215] coop_0.6-3                              spatstat.core_2.4-4

Thank you!

seurat

What exactly do you want to convert to a df?

data.frame of what?

A data.frame is just a data data type, but the important part is what data you need to store in it. RaceID probably gives you what columns it needs in the data.frame, in which case you can coerce your data to match the required input.

0 answers

No answers yet.

Log in to answer this question.