stuart-lab / signac

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

Re-assigning fragment file for a merged ATAC object #953

Closed wht10 closed 2 years ago

wht10 commented 2 years ago

Hey Tim,

Thanks for creating such a useful tool for analyzing scATAC data.

You recently helped out with successfully linking peaks to genes on a merged object that followed the merge and multiome vignettes (Issue #936).

A collaborator is trying to look at the CoveragePlot for the object I created and is getting the following error.

> p1 <- CoveragePlot(
+   object = gdTonly,
+   region = "Rorc",
+   features = "Rorc",
+   expression.assay = "RNA",
+   idents = 0,
+   extend.upstream = 10000,
+   extend.downstream = 10000
+ )
Error: TabixFile: file(s) do not exist:
  'C:\Users\wht32\projects\10xMulti_gd_ILC\Naive\atac_fragments.tsv.gz'
  'C:\Users\wht32\projects\10xMulti_gd_ILC\Naive\atac_fragments.tsv.gz.tbi'

I believe this is related to providing the correct fragment file path as mentioned in issue #791 and #206.

However, I am a little confused because, the fragment file path here only refers to one of the two samples that was used when creating the merged object.

I assume that an accurate CoveragePlot would need to reference a merged fragment file.

Do we need a merged fragment file or can I just re-assign the file path to the "Naive" (only 1 of the 2 merged samples) fragments path? Alternatively, if we do need a merged fragment file is there a way to obtain it from one of the objects created in the Merging vignette?

timoast commented 2 years ago

You don't need to create a merged fragment file, my guess is that you only see an error message for one of the two files because the function stops after encountering the first missing file.

If you moved the Seurat object to a different filesystem, you'll need to update the fragment file paths using the UpdatePath() function

wht10 commented 2 years ago

Hey @timoast,

Thank you that seems to have worked.

I followed the instructions you posted in issue #788.

Just to clarify, when updating the filepath to the fragments object you only provide the path to the .tsv.gz file and not the .tsv.gz.tbi, right?

The coverage plot ran correctly, but it provided an error that said the .tsv.gz.tbi was older, although the file on the two servers (the one it was created on, and the one it is being analyzed on) is exactly the same, so I don't suspect it is causing any problems.

frags <- Fragments(obj)  # get list of fragment objects
Fragments(obj) <- NULL  # remove fragment information from assay
newpath <- "Sample/atac_fragments.tsv.gz"
frags[[1]] <- UpdatePath(frags[[1]], new.path = newpath)  # update path. Do this for any/all fragment objects in the list
Fragments(obj) <- frags  # assign update list of fragment objects back to the assay

Or

frags <- Fragments(obj)  # get list of fragment objects
Fragments(obj) <- NULL  # remove fragment information from assay
newpath <- c("Sample/atac_fragments.tsv.gz","Sample/atac_fragments.tsv.gz.tbi")
frags[[1]] <- UpdatePath(frags[[1]], new.path = newpath)  # update path. Do this for any/all fragment objects in the list
Fragments(obj) <- frags  # assign update list of fragment objects back to the assay
timoast commented 2 years ago

Yep you only need to provide the path to the fragment file itself, not the index. The index must be located in the same directory as the fragment file. The warning is because the index was written to disk earlier than the fragment file itself, which tends to happen when you move data around (since the index is a lot smaller, it's often written first). You can ignore these warnings

jenellewallace commented 1 year ago

Hi, I just wanted to post here for anyone that comes across this and spends a long time being confused like I was: It seems like you need to change the paths for all the fragment files in all of the peak assays (if you have more than one). For example, I had assays called 'peaksprelim' and 'peaks_celltypes' and changing the paths for 'peaksprelim' did not change the paths for 'peaks_celltypes' when I later tried to make a CoveragePlot with that assay. The error I got when I tried to plot was

Error: TabixFile: Fragment file is not indexed 

but when I checked the path in the object right before plotting (after changing the DefaultAssay) with

frags <- Fragments(multi)
frags[[1]]@path

I discovered that the path had not actually been changed for that assay. Hope that helps someone!

QTranCuba commented 8 months ago

Hi @wht10 and @timoast

Thanks for the conversation. I am having a slightly similar trouble and hopefully could get some advice from you. I have a merged object and I want to run the coverage plot on this object. I have already update the new path of fragment files to the object and when I tried to save the new path to the current object, it gave me the error "Error in AddFragments(object = object, fragments = value[[i]]) : Cells already present in a fragment object". I really did not understand how to interpret this error.

Below is my code: new_1 <- UpdateSeuratObject(new_myonuclei)

DefaultAssay(new_1) <- 'ATAC' test <- Fragments(new_1) test

Fragments(new_1) <- NULL #if not remove and create a new path to current main directory newpath_1 <- c("~/Desktop/Personal/Quan/Research Project /Data/scRNAseq meta data/3/LeTran/E14/atac_fragments.tsv.gz") newpath_2 <- c("~/Desktop/Personal/Quan/Research Project /Data/scRNAseq meta data/3/LeTran/E18/atac_fragments.tsv.gz") newpath_3 <- c("~/Desktop/Personal/Quan/Research Project /Data/scRNAseq meta data/3/LeTran/P5/atac_fragments.tsv.gz") newpath_4 <- c("~/Desktop/Personal/Quan/Research Project /Data/scRNAseq meta data/3/LeTran/Adult/atac_fragments.tsv.gz")

test[[1]] <- UpdatePath(test[[1]], new.path = newpath_1, verbose = T)
test[[5]] <- UpdatePath(test[[5]], new.path = newpath_1, verbose = T)
test[[9]] <- UpdatePath(test[[9]], new.path = newpath_1, verbose = T)
test[[13]] <- UpdatePath(test[[13]], new.path = newpath_1, verbose = T)

test[[2]] <- UpdatePath(test[[2]], new.path = newpath_2, verbose = T)
test[[6]] <- UpdatePath(test[[6]], new.path = newpath_2, verbose = T)
test[[10]] <- UpdatePath(test[[10]], new.path = newpath_2, verbose = T)
test[[14]] <- UpdatePath(test[[14]], new.path = newpath_2, verbose = T)

test[[3]] <- UpdatePath(test[[3]], new.path = newpath_3, verbose = T)
test[[7]] <- UpdatePath(test[[7]], new.path = newpath_3, verbose = T)
test[[11]] <- UpdatePath(test[[11]], new.path = newpath_3, verbose = T)
test[[15]] <- UpdatePath(test[[15]], new.path = newpath_3, verbose = T)

test[[4]] <- UpdatePath(test[[4]], new.path = newpath_4, verbose = T)
test[[8]] <- UpdatePath(test[[8]], new.path = newpath_4, verbose = T)
test[[12]] <- UpdatePath(test[[12]], new.path = newpath_4, verbose = T)
test[[16]] <- UpdatePath(test[[16]], new.path = newpath_4, verbose = T)

Fragments(new_1) <- test

Error in AddFragments(object = object, fragments = value[[i]]) : Cells already present in a fragment object