stuart-lab / signac

R toolkit for the analysis of single-cell chromatin data
https://stuartlab.org/signac/
Other
331 stars 88 forks source link

CallPeaks error: cannot open file 'path/filename_peaks.narrowPeak': No such file or directory #1435

Closed elisulvaran closed 1 year ago

elisulvaran commented 1 year ago

Hi,

Firstly, thanks for creating this incredibly useful tool. I've been having some issues while trying to run CallPeaks on my object. I followed some of the suggestions in other issues (#473, #560, #648, #676, #1376, etc), but I haven't been able to solve it. I am running this in a MacBook in RStudio. I already confirmed I have permissions to my output directory (I ran chmod +rw output to make sure), and I also confirmed I can run macs2 outside of R.

This is how my object looks like:

> chrom_assay
An object of class Seurat 
171904 features across 25950 samples within 1 assay 
Active assay: peaks (171904 features, 0 variable features)
 2 layers present: counts, data

And here's my code:

macs2.path <- "/my/path/to/macs2"
outdir <- "/my/output/folder"
setwd(outdir)
peaks <- CallPeaks(chrom_assay, macs2.path = macs2.path,  
                   group.by = "celltypes", outdir = outdir,
                   fragment.tempdir = outdir)

When I run the CallPeaks function, it runs well for the first steps, but then the following error comes up:

Done Processing 24 million lines                  

sh: /my/path/to/macs2: is a directory
Warning in file(file, "rt") :
  cannot open file '/my/output/folder/Macrophages_peaks.narrowPeak': No such file or directory
Error in file(file, "rt") : cannot open the connection

Making sure the directory exists:

> dir.exists(outdir)
[1] TRUE

Attaching session info:

> sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.2.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] ggplot2_3.4.2           Signac_1.10.0           Seurat_4.9.9.9045      
[4] SeuratObject_4.9.9.9084 sp_1.6-1                edgeR_3.40.2           
[7] limma_3.54.2           

loaded via a namespace (and not attached):
  [1] rappdirs_0.3.3                scattermore_1.1              
  [3] tidyr_1.3.0                   bit64_4.0.5                  
  [5] knitr_1.43                    irlba_2.3.5.1                
  [7] DelayedArray_0.24.0           data.table_1.14.8            
  [9] KEGGREST_1.38.0               RCurl_1.98-1.12              
 [11] doParallel_1.0.17             generics_0.1.3               
 [13] BiocGenerics_0.44.0           ScaledMatrix_1.6.0           
 [15] cowplot_1.1.1                 RSQLite_2.3.1                
 [17] RANN_2.6.1                    future_1.32.0         
 [19] bit_4.0.5                     tzdb_0.4.0                   
 [21] spatstat.data_3.0-1           xml2_1.3.4                   
 [23] httpuv_1.6.11                 SummarizedExperiment_1.28.0  
 [25] xfun_0.39                     celldex_1.8.0                
 [27] hms_1.1.3                     evaluate_0.21                
 [29] promises_1.2.0.1              fansi_1.0.4                  
 [31] progress_1.2.2                dbplyr_2.3.2                 
 [33] readxl_1.4.2                  igraph_1.4.3                 
 [35] DBI_1.1.3                     htmlwidgets_1.6.2            
 [37] spatstat.geom_3.2-1           stats4_4.2.1                 
 [39] purrr_1.0.1                   ellipsis_0.3.2               
 [41] RSpectra_0.16-1               dplyr_1.1.2                  
 [43] backports_1.4.1               deldir_1.0-9                 
 [45] sparseMatrixStats_1.10.0      MatrixGenerics_1.10.0        
 [47] vctrs_0.6.2                   SingleCellExperiment_1.20.1  
 [49] Biobase_2.58.0                ROCR_1.0-11                  
 [51] abind_1.4-5                   cachem_1.0.8                 
 [53] withr_2.5.0                   progressr_0.13.0             
 [55] checkmate_2.2.0               sctransform_0.3.5            
 [57] prettyunits_1.1.1             scran_1.26.2                 
 [59] goftest_1.2-3                 cluster_2.1.4                
 [61] ExperimentHub_2.6.0           dir.expiry_1.6.0             
 [63] dotCall64_1.0-2               lazyeval_0.2.2               
 [65] crayon_1.5.2                  basilisk.utils_1.11.1        
 [67] spatstat.explore_3.2-1        labeling_0.4.2               
 [69] pkgconfig_2.0.3               GenomeInfoDb_1.34.9          
 [71] nlme_3.1-162                  rlang_1.1.1                  
 [73] globals_0.16.2                lifecycle_1.0.3       
 [75] miniUI_0.1.1.1                filelock_1.0.2               
 [77] fastDummies_1.6.3             BiocFileCache_2.6.1          
 [79] rsvd_1.0.5                    AnnotationHub_3.6.0          
 [81] cellranger_1.1.0              polyclip_1.10-4              
 [83] RcppHNSW_0.4.1                matrixStats_1.0.0            
 [85] lmtest_0.9-40                 Matrix_1.5-4.1               
 [87] zoo_1.8-12                    ggridges_0.5.4               
 [89] GlobalOptions_0.1.2           png_0.1-8                    
 [91] viridisLite_0.4.2             rjson_0.2.21                 
 [93] bitops_1.0-7                  KernSmooth_2.23-21           
 [95] spam_2.9-1                    Biostrings_2.66.0            
 [97] blob_1.2.4                    DelayedMatrixStats_1.20.0    
 [99] shape_1.4.6                   stringr_1.5.0                
[101] parallelly_1.36.0             spatstat.random_3.1-5        
[103] readr_2.1.4                   S4Vectors_0.36.2             
[105] beachmat_2.14.2               scales_1.2.1                 
[107] memoise_2.0.1                 magrittr_2.0.3               
[109] plyr_1.8.8                    ica_1.0-3                    
[111] zlibbioc_1.44.0               compiler_4.2.1               
[113] dqrng_0.3.0                   RColorBrewer_1.1-3           
[115] clue_0.3-64                   fitdistrplus_1.1-11          
[117] Rsamtools_2.14.0              cli_3.6.1                    
[119] XVector_0.38.0                listenv_0.9.0                
[121] patchwork_1.1.2               pbapply_1.7-0                
[123] MASS_7.3-60                   tidyselect_1.2.0             
[125] liana_0.1.9                   stringi_1.7.12               
[127] yaml_2.3.7                    BiocSingular_1.14.0          
[129] locfit_1.5-9.7                ggrepel_0.9.3                
[131] grid_4.2.1                    fastmatch_1.1-3              
[133] tools_4.2.1                   future.apply_1.11.0 
[135] parallel_4.2.1                circlize_0.4.15              
[137] rstudioapi_0.14               bluster_1.8.0                
[139] foreach_1.5.2                 metapod_1.6.0                
[141] gridExtra_2.3                 farver_2.1.1                 
[143] Rtsne_0.16                    digest_0.6.31                
[145] BiocManager_1.30.20           shiny_1.7.4                  
[147] Rcpp_1.0.10                   GenomicRanges_1.50.2         
[149] scuttle_1.8.4                 BiocVersion_3.16.0           
[151] later_1.3.1                   RcppAnnoy_0.0.20             
[153] httr_1.4.6                    AnnotationDbi_1.60.2         
[155] ComplexHeatmap_2.14.0         colorspace_2.1-0             
[157] rvest_1.0.3                   tensor_1.5                   
[159] reticulate_1.29               IRanges_2.32.0               
[161] splines_4.2.1                 uwot_0.1.14                  
[163] RcppRoll_0.3.0                statmod_1.5.0                
[165] OmnipathR_3.7.0               spatstat.utils_3.0-3         
[167] basilisk_1.11.2               plotly_4.10.2                
[169] xtable_1.8-4                  jsonlite_1.8.5               
[171] R6_2.5.1                      pillar_1.9.0                 
[173] htmltools_0.5.5               mime_0.12                    
[175] glue_1.6.2                    fastmap_1.1.1                
[177] BiocParallel_1.32.6           BiocNeighbors_1.16.0         
[179] interactiveDisplayBase_1.36.0 codetools_0.2-19             
[181] utf8_1.2.3                    lattice_0.21-8               
[183] spatstat.sparse_3.0-1         tibble_3.2.1                 
[185] logger_0.2.2                  curl_5.0.0                   
[187] leiden_0.4.3                  survival_3.5-5         
[189] rmarkdown_2.22                munsell_0.5.0                
[191] GetoptLong_1.0.5              GenomeInfoDbData_1.2.9       
[193] iterators_1.0.14              reshape2_1.4.4               
[195] gtable_0.3.3          

Help on this matter would be greatly appreciated! Thanks!

timoast commented 1 year ago

Can you check if you still have this issue when using the seurat5 branch version (https://github.com/stuart-lab/signac/tree/seurat5)? This branch is needed for Seurat v5

elisulvaran commented 1 year ago

Unfortunately no. Here's my session info:

R version 4.2.1 (2022-06-23)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.2.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] ggplot2_3.4.2           Signac_1.10.0           Seurat_4.9.9.9049      
[4] SeuratObject_4.9.9.9085 sp_2.0-0               

loaded via a namespace (and not attached):
  [1] spam_2.9-1             fastmatch_1.1-3        plyr_1.8.8             igraph_1.5.0          
  [5] lazyeval_0.2.2         splines_4.2.1          RcppHNSW_0.4.1         BiocParallel_1.32.6   
  [9] listenv_0.9.0          scattermore_1.2        GenomeInfoDb_1.34.9    digest_0.6.32         
 [13] htmltools_0.5.5        fansi_1.0.4            magrittr_2.0.3         tensor_1.5            
 [17] cluster_2.1.4          ROCR_1.0-11            remotes_2.4.2          globals_0.16.2        
 [21] Biostrings_2.66.0      matrixStats_1.0.0      spatstat.sparse_3.0-2  prettyunits_1.1.1     
 [25] colorspace_2.1-0       ggrepel_0.9.3          xfun_0.39              dplyr_1.1.2           
 [29] callr_3.7.3            crayon_1.5.2           RCurl_1.98-1.12        jsonlite_1.8.5        
 [33] progressr_0.13.0       spatstat.data_3.0-1    survival_3.5-5         zoo_1.8-12            
 [37] glue_1.6.2             polyclip_1.10-4        gtable_0.3.3           zlibbioc_1.44.0       
 [41] XVector_0.38.0         leiden_0.4.3           pkgbuild_1.4.0         future.apply_1.11.0   
 [45] BiocGenerics_0.44.0    abind_1.4-5            scales_1.2.1           spatstat.random_3.1-5 
 [49] miniUI_0.1.1.1         Rcpp_1.0.10            viridisLite_0.4.2      xtable_1.8-4          
 [53] reticulate_1.30        dotCall64_1.0-2        stats4_4.2.1           htmlwidgets_1.6.2     
 [57] httr_1.4.6             RColorBrewer_1.1-3     ellipsis_0.3.2         ica_1.0-3             
 [61] farver_2.1.1           pkgconfig_2.0.3        uwot_0.1.15            deldir_1.0-9          
 [65] utf8_1.2.3             labeling_0.4.2         tidyselect_1.2.0       rlang_1.1.1           
 [69] reshape2_1.4.4         later_1.3.1            munsell_0.5.0          tools_4.2.1           
 [73] cli_3.6.1              generics_0.1.3         ggridges_0.5.4         stringr_1.5.0         
 [77] fastmap_1.1.1          goftest_1.2-3          processx_3.8.1         knitr_1.43            
 [81] fitdistrplus_1.1-11    purrr_1.0.1            RANN_2.6.1             pbapply_1.7-0         
 [85] future_1.32.0          nlme_3.1-162           mime_0.12              RcppRoll_0.3.0        
 [89] compiler_4.2.1         rstudioapi_0.14        plotly_4.10.2          curl_5.0.1            
 [93] png_0.1-8              spatstat.utils_3.0-3   tibble_3.2.1           stringi_1.7.12        
 [97] ps_1.7.5               desc_1.4.2             RSpectra_0.16-1        lattice_0.21-8        
[101] Matrix_1.5-4.1         vctrs_0.6.3            pillar_1.9.0           lifecycle_1.0.3       
[105] spatstat.geom_3.2-1    lmtest_0.9-40          RcppAnnoy_0.0.20       data.table_1.14.8     
[109] cowplot_1.1.1          bitops_1.0-7           irlba_2.3.5.1          httpuv_1.6.11         
[113] patchwork_1.1.2        GenomicRanges_1.50.2   R6_2.5.1               promises_1.2.0.1      
[117] KernSmooth_2.23-21     gridExtra_2.3          IRanges_2.32.0         parallelly_1.36.0     
[121] codetools_0.2-19       fastDummies_1.6.3      MASS_7.3-60            rprojroot_2.0.3       
[125] withr_2.5.0            sctransform_0.3.5      Rsamtools_2.14.0       S4Vectors_0.36.2      
[129] GenomeInfoDbData_1.2.9 parallel_4.2.1         grid_4.2.1             tidyr_1.3.0           
[133] Rtsne_0.16             spatstat.explore_3.2-1 shiny_1.7.4   
timoast commented 1 year ago

This session info still says that you have Signac 1.10.0 installed, please install from the seurat5 branch and try again

elisulvaran commented 1 year ago

Hi! So I ran the code on the page you linked and I'm getting the following message:

> if (!requireNamespace("remotes", quietly = TRUE))
     install.packages("remotes")
> remotes::install_github("stuart-lab/signac", ref = "develop")
Skipping install of 'Signac' from a github remote, the SHA1 (2ad6c3c9) has not changed since last install.
  Use `force = TRUE` to force installation

I used force = TRUE and the package downloaded, but when I import it it's still the same version. How could I work around it in that case?

Thanks!

timoast commented 1 year ago

To install the seurat5 branch you need to run:

remotes::install_github("stuart-lab/signac", ref = "seurat5")
elisulvaran commented 1 year ago

Hello! Unfortunately, that didn't solve it either. However, I'm not sure if Signac was properly updated, because after running the line you provided, it looks like now I have version 1.9.0.9000. I ran it multiple times and I also tried uninstalling it and re installing it and that was the version that was in all cases. Here's my session info:

R version 4.2.1 (2022-06-23)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.2.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
[1] ggplot2_3.4.2           Signac_1.9.0.9000       Seurat_4.9.9.9049      
[4] SeuratObject_4.9.9.9085 sp_2.0-0                edgeR_3.40.2           
[7] limma_3.54.2           

loaded via a namespace (and not attached):
  [1] rappdirs_0.3.3                scattermore_1.2               tidyr_1.3.0                  
  [4] bit64_4.0.5                   knitr_1.43                    irlba_2.3.5.1                
  [7] DelayedArray_0.24.0           data.table_1.14.8             KEGGREST_1.38.0              
 [10] RCurl_1.98-1.12               doParallel_1.0.17             generics_0.1.3               
 [13] BiocGenerics_0.44.0           ScaledMatrix_1.6.0            cowplot_1.1.1                
 [16] RSQLite_2.3.1                 RANN_2.6.1                    future_1.32.0                
 [19] bit_4.0.5                     tzdb_0.4.0                    spatstat.data_3.0-1          
 [22] xml2_1.3.4                    httpuv_1.6.11                 SummarizedExperiment_1.28.0  
 [25] xfun_0.39                     celldex_1.8.0                 hms_1.1.3                    
 [28] evaluate_0.21                 promises_1.2.0.1              fansi_1.0.4                  
 [31] progress_1.2.2                dbplyr_2.3.2                  readxl_1.4.2                 
 [34] igraph_1.5.0                  DBI_1.1.3                     htmlwidgets_1.6.2            
 [37] spatstat.geom_3.2-1           stats4_4.2.1                  purrr_1.0.1                  
 [40] ellipsis_0.3.2                RSpectra_0.16-1               dplyr_1.1.2                  
 [43] backports_1.4.1               deldir_1.0-9                  sparseMatrixStats_1.10.0     
 [46] MatrixGenerics_1.10.0         vctrs_0.6.3                   SingleCellExperiment_1.20.1  
 [49] Biobase_2.58.0                ROCR_1.0-11                   abind_1.4-5                  
 [52] cachem_1.0.8                  withr_2.5.0                   progressr_0.13.0             
 [55] checkmate_2.2.0               sctransform_0.3.5             prettyunits_1.1.1            
 [58] scran_1.26.2                  goftest_1.2-3                 cluster_2.1.4                
 [61] ExperimentHub_2.6.0           dir.expiry_1.6.0              dotCall64_1.0-2              
 [64] lazyeval_0.2.2                crayon_1.5.2                  basilisk.utils_1.11.1        
 [67] spatstat.explore_3.2-1        pkgconfig_2.0.3               GenomeInfoDb_1.34.9          
 [70] nlme_3.1-162                  rlang_1.1.1                   globals_0.16.2               
 [73] lifecycle_1.0.3               miniUI_0.1.1.1                filelock_1.0.2               
 [76] fastDummies_1.6.3             BiocFileCache_2.6.1           rsvd_1.0.5                   
 [79] AnnotationHub_3.6.0           cellranger_1.1.0              polyclip_1.10-4              
 [82] RcppHNSW_0.4.1                matrixStats_1.0.0             lmtest_0.9-40                
 [85] Matrix_1.5-4.1                zoo_1.8-12                    ggridges_0.5.4               
 [88] GlobalOptions_0.1.2           png_0.1-8                     viridisLite_0.4.2            
 [91] rjson_0.2.21                  bitops_1.0-7                  KernSmooth_2.23-21           
 [94] spam_2.9-1                    Biostrings_2.66.0             blob_1.2.4                   
 [97] DelayedMatrixStats_1.20.0     shape_1.4.6                   stringr_1.5.0                
[100] parallelly_1.36.0             spatstat.random_3.1-5         readr_2.1.4                  
[103] S4Vectors_0.36.2              beachmat_2.14.2               scales_1.2.1                 
[106] memoise_2.0.1                 magrittr_2.0.3                plyr_1.8.8                   
[109] ica_1.0-3                     zlibbioc_1.44.0               compiler_4.2.1               
[112] dqrng_0.3.0                   RColorBrewer_1.1-3            clue_0.3-64                  
[115] fitdistrplus_1.1-11           Rsamtools_2.14.0              cli_3.6.1                    
[118] XVector_0.38.0                listenv_0.9.0                 patchwork_1.1.2              
[121] pbapply_1.7-0                 MASS_7.3-60                   tidyselect_1.2.0             
[124] liana_0.1.9                   stringi_1.7.12                yaml_2.3.7                   
[127] BiocSingular_1.14.0           locfit_1.5-9.7                ggrepel_0.9.3                
[130] grid_4.2.1                    fastmatch_1.1-3               tools_4.2.1                  
[133] future.apply_1.11.0           parallel_4.2.1                circlize_0.4.15              
[136] rstudioapi_0.14               bluster_1.8.0                 foreach_1.5.2                
[139] metapod_1.6.0                 gridExtra_2.3                 Rtsne_0.16                   
[142] digest_0.6.32                 BiocManager_1.30.20           shiny_1.7.4                  
[145] Rcpp_1.0.10                   GenomicRanges_1.50.2          scuttle_1.8.4                
[148] BiocVersion_3.16.0            later_1.3.1                   RcppAnnoy_0.0.20             
[151] httr_1.4.6                    AnnotationDbi_1.60.2          ComplexHeatmap_2.14.0        
[154] colorspace_2.1-0              rvest_1.0.3                   tensor_1.5                   
[157] reticulate_1.30               IRanges_2.32.0                splines_4.2.1                
[160] uwot_0.1.15                   RcppRoll_0.3.0                statmod_1.5.0                
[163] OmnipathR_3.7.0               spatstat.utils_3.0-3          basilisk_1.11.2              
[166] plotly_4.10.2                 xtable_1.8-4                  jsonlite_1.8.5               
[169] R6_2.5.1                      pillar_1.9.0                  htmltools_0.5.5              
[172] mime_0.12                     glue_1.6.2                    fastmap_1.1.1                
[175] BiocParallel_1.32.6           BiocNeighbors_1.16.0          interactiveDisplayBase_1.36.0
[178] codetools_0.2-19              utf8_1.2.3                    lattice_0.21-8               
[181] spatstat.sparse_3.0-2         tibble_3.2.1                  logger_0.2.2                 
[184] curl_5.0.1                    leiden_0.4.3                  survival_3.5-5               
[187] rmarkdown_2.22                munsell_0.5.0                 GetoptLong_1.0.5             
[190] GenomeInfoDbData_1.2.9        iterators_1.0.14              reshape2_1.4.4               
[193] gtable_0.3.3        
timoast commented 1 year ago

Ok this message sh: /my/path/to/macs2: is a directory suggests that the path you're supplying for macs2 is not correct, it's pointing to a directory rather than the macs2 program itself. If you run which macs2 in your terminal you can copy the path that it gives you and that should work (alternatively, if macs2 is in your path you should be able to leave the macs2.path parameter empty in the CallPeaks() function). I think the issue is it doesn't find macs2, and so no outputs are created.

elisulvaran commented 1 year ago

Hi,

So that seems to have solved the initial problem, and now it does run longer, but then at some point I keep getting this output (attaching screenshot) and my RStudio simply stops responding. Do you know why this is happening?

Thank you!!

Captura de Pantalla 2023-06-29 a la(s) 11 47 53 a m
timoast commented 1 year ago

I have not seen that before, could you show the error message?

elisulvaran commented 1 year ago

There's no error message, or at least not that I can see. Once those characters start popping up R pretty much freezes and I can't scroll through the output anymore

timoast commented 1 year ago

Can you verify that you can run macs2 on these files outside of R (on the command line) without error?

elisulvaran commented 1 year ago

Yes, I can. I just ran it for one of my samples, and I got the outputs _model.R, _peaks.narrowPeaks, _peaks.xls, and _summits.bed.

timoast commented 1 year ago

How many cells in each group that you're grouping by?

elisulvaran commented 1 year ago

I have the following:

Macrophages 1902 
DC 492 
B_cell 16583 
CD8+ T-cells 1842 
Monocytes 160 
Fibroblasts 314 
NK cells 82 
Smooth muscle 225 
Chondrocytes 266 
Endothelial cells 263 
CD4+ T-cells 3731 
Adipocytes 62 

However, this is from my filtered dataset after applying QC metrics in Seurat.

timoast commented 1 year ago

I'm not sure what the issue is here. You could try running one the whole dataset rather than splitting by cell type to see if it is somehow related to running per-celltype.

mtekman commented 6 months ago

I was getting the (title) error a lot and had no idea what the issue was or how to debug it. After looking through peaks.R I realised that it was a general MACS2 error and had nothing to do with Signac, and that it was indeed telling me that my files were not where they said they were.

To debug this yourself, future reader:

myseurobj[["ATAC"]]$fragments
## it might give you a list, but I'm just going to check the first
myseurobj[["ATAC"]]$fragments[[1]]@path
## This shows you the file path of where the ATAC fragments are.
file.exists(myseurobj[["ATAC"]]$fragments[[1]]@path)  ## Should be TRUE

If you have moved the ATAC fragments to somewhere else, then, dear reader, you need to update this in your Seurat object:

myseurobj[["ATAC"]]$fragments[[1]]@path <- "/my/new/path/to/ATAC_fragments.tsv.gz"
## And then re-run CallPeaks

EDIT: See @timoast's comment below about a native UpdatePath function!


Note, the above example merely edits one path for one fragment. If all your fragment files have been moved to a different location, then you need to loop through your fragments and update the paths.

I've included a small script which does just this:

fixMyPaths_please <- function(seuratobj, oldpath, newpath){
    frags <- seuratobj[["ATAC"]]$fragments

    for (i in seq_along(frags)){
        fpath <- frags[[i]]@path
        if (file.exists(fpath)){
            message(i, " - ", fpath, " - exists, not changing")
        } else {
            message(i, " - ", fpath, " - DOES NOT exist, changing")
            new_fpath <- sub(oldpath, newpath, fpath)
            if (file.exists(new_fpath)){
                seuratobj[["ATAC"]]$fragments[[i]]@path <- new_fpath
                message(i, " - changed to valid path: ", new_fpath)
            } else {
                message(paste0(i, " - INVALID new path: ", new_fpath))
                stop("Check your paths")
            }   
        }
    }
    return(seuratobj)
}
## Assume I moved my fragments from "/home/mtekman/analysis" to "/opt/backup/2023/analysis"
myseurat <- fixMyPaths_please(myseurat, "/home/mtekman", "/opt/backup/2023")
timoast commented 6 months ago

@mtekman we have a function for this, UpdatePath: https://stuartlab.org/signac/reference/updatepath

mtekman commented 6 months ago

Oh lmao, I wish I knew about that sooner! Leaving my comment as is just in case it helps someone.

Edit: Does UpdatePath work for multiple fragment files in an object?

timoast commented 6 months ago

It operates on one fragment object at a time, but see here for an example of how to update for many fragment files: https://stuartlab.org/signac/articles/data_structures#changing-the-fragment-file-path-in-an-existing-fragment-object