aertslab / SCopeLoomR

R package (compatible with SCope) to create generic .loom files and extend them with other data e.g.: SCENIC regulons, Seurat clusters and markers, ...
MIT License
38 stars 15 forks source link

add_seurat_clustering fails when clusters are not integers #15

Closed KatjaRM closed 4 years ago

KatjaRM commented 4 years ago

Hi Team,

I am trying to add my Seurat clustering from a v3 object to a loom file and get the following error with add_seurat_clustering(loom = loom, seurat = seurat.object, default.clustering.overwrite = T)`

Error in add_seurat_clustering(loom = loom, seurat = seurat.object, default.clustering.overwrite = T) : The given seurat.clustering.prefix is required. E.g.: RNA_snn_res.

What information do I add for seurat.clustering.prefix = ? The clustering is not simply a single Seurat clustering round with one resolution factor, but I have subclustering and partly clustering by marker expression, so I am really confused as to what I have to provide.

I would be thankful for any help!

Best,

Katja

dweemx commented 4 years ago

Hi @KatjaRM, Is/are your clustering(s) part of a Seurat object ? If it is the case could you share the results of

colnames(x = seurat@metadata)

?

KatjaRM commented 4 years ago

Hi!

Thanks for the answer! Yes: colnames(seurat.object@meta.data)

[1] "orig.ident" "nCount_RNA" "nFeature_RNA"
[4] "dataset" "integrated_snn_res.0.1" "seurat_clusters"
[7] "CellfindR"

The clustering I would like to add is stored in orig.ident. I could either add it from there or from seurat.object@active.ident.

dweemx commented 4 years ago

I see, you should be able to add the clustering using this code:

add_clustering(loom = loom,
   group = "[a-group-for-your-clustering, e.g.: Seurat]",
   name = "[a-name-for-your-clustering]",
   clusters = seurat@active.ident
)
KatjaRM commented 4 years ago

Hm, now I get the error message

The following from values were not present in x: NA [1] "Clusterings already exists..." [1] 1 Warning message: In sort(as.integer(levels(clusters)), decreasing = F) : NAs introduced by coercion

Even though I set overwrite.default = TRUE

dweemx commented 4 years ago

Thanks for the report bug. I might have a closer lookup to it today. I'll will let you know once I fixed the bug

dweemx commented 4 years ago

Hey @KatjaRM, I cannot reproduce your error unfortunately. The following snippet works for me:

loom <- build_loom(
  file.name = "tests/Seurat_v3/Seurat.loom",
  title = "Seurat",
  dgem = seurat@assays$RNA@counts,
  ...
)
add_clustering(
  loom = loom,
  group = "Seurat",
  name = "Seurat Active Ident",
  clusters = seurat@active.ident
)

However, here are some things you could check,

KatjaRM commented 4 years ago

Hi Max,

Thank you so much for taking all the time and looking into it. I checked all the points you mentioned and everything was fine.

I have played around with it a little more and what fixed the issue in the end renaming the clusters to just numbers. Weird, but I'll take it.

Thank you again for your time!

Katja

dweemx commented 4 years ago

Mmhh, could you post the result of:

seurat@active.ident[1:10]

I'm still interested to understand why it was not working if it were not numbers

KatjaRM commented 4 years ago

For my initial cluster names:

seurat2@active.ident[1:10] dataset2_AAAGTAGGTACGACCC dataset2_AACTGGTCAACACCCG 4.1 older germ cell 4.0 undifferentiated germ cell dataset2_AAGGAGCAGATATACG dataset2_ACATCAGTCTACCAGA 4.0 undifferentiated germ cell 4.1 older germ cell dataset2_ACCAGTAGTGGTCTCG dataset2_ACGATGTCATGTAGTC 4.0 undifferentiated germ cell 4.0 undifferentiated germ cell dataset2_ACGCAGCGTTCCACGG dataset2_ACTGTCCGTATAAACG 4.0 undifferentiated germ cell 4.0 undifferentiated germ cell dataset2_ACTTTCAGTGTGACGA dataset2_AGCATACGTCGCCATG 4.1 older germ cell 4.1 older germ cell 25 Levels: 5 muscle cell ... 0 MB 1-5

After renaming the clusters:

seurat_2@active.ident[1:10] dataset2_AAAGTAGGTACGACCC dataset2_AACTGGTCAACACCCG dataset2_AAGGAGCAGATATACG 25 24 24 dataset2_ACATCAGTCTACCAGA dataset2_ACCAGTAGTGGTCTCG dataset2_ACGATGTCATGTAGTC 25 24 24 dataset2_ACGCAGCGTTCCACGG dataset2_ACTGTCCGTATAAACG dataset2_ACTTTCAGTGTGACGA 24 24 25 dataset2_AGCATACGTCGCCATG 25 Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 21 22 23 24 25 26

The only thing I changed is using SetIdent from Seurat to rename each cluster.