Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

seurat v4 MapQuery() returns errors about k.weight which is not an explicit argument #4117

Closed
Winnie09 opened this issue Feb 20, 2021 · 14 comments
Assignees
Labels
more-information-needed We need more information before this can be addressed

Comments

@Winnie09
Copy link

Hi,

Thank you for developing Seurat v4. It is very helpful. I would like to map obj2 (a Seruat v3 object) to a reference obj1 (a Seurat v3 object). These two are both integrative objects of several samples and have slots pca, harmony, umap and umap models. I applied seurat v4 for Multimodal reference mapping. FindTransferAnchors() gave outputs successfully, but MapQuery() returned errors that suggested me to set k.weight but k.weight is not its explicit argument. I checked some source codes and found RunIntegration() and PairwiseIntegrateReference() have k.weight as one of their arguments but I am not sure how MapQuery() are involving these two functions. Could you suggest a way to solve this? Thank you!

Here is my code:

library(Seurat)
library(ggplot2)
library(patchwork)
library(here)
setwd(here())
obj1 <- readRDS('obj1.rds')
obj2 <- readRDS('obj2.rds')

obj1$RNA@var.features <- rownames(obj1$RNA@scale.data)
obj2$RNA@var.features <- rownames(obj2$RNA@scale.data)

anchors <- FindTransferAnchors(
   reference = obj1,
   query = obj2,
   normalization.method = "LogNormalize",
   reference.reduction = "pca",
   dims = 1:30
 )

ct = readRDS('ct.rds')

res <- MapQuery(
  anchorset = anchors,
  query = obj2,
  reference = obj1,
  refdata = ct,
  reference.reduction = "pca", 
  reduction.model = "umap"
)

Errors:

Projecting cell embeddings
Finding neighborhoods
Finding anchors
Found 1174 anchors
Filtering anchors
Retained 13 anchors
Error: Please set k.weight to be smaller than the number of anchors (13).
Execution halted

@yuhanH
Copy link
Collaborator

yuhanH commented Feb 26, 2021

Hi,
You would better set k.filter to NA in FindTransferAnchors.
After filtering, there are only 13 anchors left which caused the k.weight error.

@yuhanH yuhanH closed this as completed Feb 26, 2021
@Yale73
Copy link

Yale73 commented May 11, 2021

Hi @yuhanH I ran new error if I set k.filter to NULL.

Error in if (!is.na(x = k.filter) && k.filter > ncol(x = query)) { : 
  missing value where TRUE/FALSE needed

But if I set k.filter to NA, I got 4 anchors, then I ran k.weight error. The new seurat version doesn't allow me to set the k.wight in MapQuery()

Error in MapQuery(anchorset = anchors[[i]], query = Cycle.batches[[i]],  : 
  unused argument (k.weight = 3)

Is there a solution?

Thanks,
Yale

@yuhanH
Copy link
Collaborator

yuhanH commented May 11, 2021

hi @Yale73
Could you show me the scripts you used? You are right that k.filter should sett to NA.

@Yale73
Copy link

Yale73 commented May 11, 2021

Hi @yuhanH ,

Here is my script, I used almost the same code as our vignette (Example 2) https://satijalab.org/seurat/articles/multimodal_reference_mapping.html

All the following codes work well,

Cycle.batches <- SplitObject(Icycle, split.by = "donor")
Cycle.batches <- lapply(X = Cycle.batches, FUN = NormalizeData, verbose = FALSE)

three <- FindNeighbors(
  object = three,
  reduction = "spca",
  dims = 1:32,
  graph.name = "spca.annoy.neighbors", 
  k.param = 50,
  cache.index = TRUE,
  return.neighbor = TRUE,
  l2.norm = TRUE
)

anchors <- list()
for (i in 1:length(Cycle.batches)) {
  anchors[[i]] <- FindTransferAnchors(
    reference = three,
    query = Cycle.batches[[i]],
    k.filter = NA,
    reference.reduction = "spca", 
    reference.neighbors = "spca.annoy.neighbors", 
    dims = 1:32,
    k.anchor = 2, ### I set 2 because some of my samples have 3 cells
    k.score = 2 
  )
}

but when I ran MapQuery, I got k.weight error.

for (i in 1:length(Cycle.batches)) {
  Cycle.batches[[i]] <- MapQuery(
    anchorset = anchors[[i]], 
    query = Cycle.batches[[i]],
    reference = three, 
    refdata = list(
      celltype = "ID", 
      predicted_ADT = "ADT"),
    reference.reduction = "spca",
    reduction.model = "umap"
  )
}

That' s my package versions:
[1] patchwork_1.1.1 SeuratObject_4.0.0 Seurat_4.0.1

I tried to change the k.weight in TransferData or IntegrateEmbeddings but cannot fix it.

Thanks for your help.
--Yale

@yuhanH
Copy link
Collaborator

yuhanH commented May 11, 2021

HI, @Yale73
i answered your issue #4450.
If you find it still doesn't work, we can continue to solve it under #4450.

@Yale73
Copy link

Yale73 commented May 11, 2021

Hi @yuhanH,

Thanks for your reply on #4450, but this issue is different from that one. This one is about mapping objects with few cell numbers. I used Example 1 but the mapping results are terrible, that's why I want to try Example 2.

Can you help me with k.weight for Example 2 code?

Thanks,
Yale

@yuhanH
Copy link
Collaborator

yuhanH commented May 11, 2021

If some of your samples only have three cells, you may need to remove it from the query. I don't think a 3-cell object can fit the anchor framework. k.anchor and k.score are not suggested to change them.
When you set k.weight to NA in FindTransferAnchors, it will not filter out any anchors. MapQuery function doesn't have the k.weight parameter.
Could you show how many anchors you get for each of query object?

@Yale73
Copy link

Yale73 commented May 11, 2021

The output, all are 4 anchors.

Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 4 anchors

Then when I run the MapQuery(), I got the following error:
Error: Please set k.weight to be smaller than the number of anchors (4).

Thanks,
Yale

@yuhanH
Copy link
Collaborator

yuhanH commented May 11, 2021

It looks very wired. The problem is in the FindTransforAnchor step not MapQuery...
How many cells you have for each query object? It seems that query cells and reference cells don't form anchors in the your spca.
Have you changed your previous steps based my suggestions in #4450?

@Yale73
Copy link

Yale73 commented May 11, 2021

@yuhanH I tried it, now the output has different anchors, but I still can not run it.

Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 6 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 20 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 6 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 14 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 6 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 14 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 14 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 114 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 38 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 103 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 31 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 31 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 14 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 148 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 82 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 6 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 84 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 26 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 26 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 33 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 20 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 36 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 91 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 103 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 107 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 99 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 45 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 37 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 33 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 60 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 18 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 63 anchors
Projecting cell embeddings
Finding neighborhoods
Finding anchors
	Found 26 anchors

I checked some of them and still get a similar error except for query objects with more than 50 anchors.

Error: Please set k.weight to be smaller than the number of anchors (20).
Error: Please set k.weight to be smaller than the number of anchors (45).

As long as I have anchors less than 50, I will have the error. As I checked the TransferData and the IntegrateEmbeddings, they have defaultk.weight = 50or k.weight = 100 respectively.

Thanks,
Yale

@yuhanH
Copy link
Collaborator

yuhanH commented May 14, 2021

Hi
I think the issue is related to the small number of anchors in each individual object.
How many cells from each individual object?

@yuhanH yuhanH reopened this May 14, 2021
@yuhanH yuhanH self-assigned this May 14, 2021
@yuhanH yuhanH added the more-information-needed We need more information before this can be addressed label May 14, 2021
@no-response
Copy link

no-response bot commented May 28, 2021

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

@no-response no-response bot closed this as completed May 28, 2021
@ProphetDarkland
Copy link

Hi, I've also encountered this error, and I don't know how to fix it. I will be very appreciated if someone can help with.

I used pbmcMultiome dataset in your SeuratData package , and followed your pipeline of Multimodal reference mapping .

After merging the RNA Seurat object pbmc.rna and ATAC Seurat object pbmc.atac to one Seurat object pbmc.seurat, I split the pbmc.seurat using subset. My code is as follows:

pbmc.seurat <- merge(pbmc.rna, pbmc.atac)
pbmc.seurat <- NormalizeData(pbmc.seurat) %>% FindVariableFeatures() %>% ScaleData() %>% RunPCA()
DefaultAssay(pbmc.seurat) <- 'ATAC'
pbmc.seurat <- RunTFIDF(pbmc.seurat)
pbmc.seurat <- FindTopFeatures(pbmc.seurat, min.cutoff = 'q0')
pbmc.seurat <- RunSVD(pbmc.seurat)
pbmc.seurat <- RunUMAP(pbmc.seurat, reduction = 'lsi', dims = 2:50, reduction.name = "umap.atac", reduction.key = "atacUMAP_")
pbmc.seurat <- FindMultiModalNeighbors(pbmc.seurat, reduction.list = list("pca", "lsi"), dims.list = list(1:50, 2:50))
pbmc.seurat <- RunUMAP(pbmc.seurat, nn.name = "weighted.nn", reduction.name = "wnn.umap", reduction.key = "wnnUMAP_")
pbmc.seurat <- FindClusters(pbmc.seurat, graph.name = "wsnn", algorithm = 3, verbose = FALSE)
pbmc.seurat <- ScaleData(pbmc.seurat, assay = 'RNA')
pbmc.seurat <- RunSPCA(pbmc.seurat, assay = 'RNA', graph = 'wsnn')
DefaultAssay(pbmc.seurat) <- 'RNA'

set <- sample(c(1:length(colnames(pbmc.seurat))), size=length(colnames(pbmc.seurat))/2)
pbmc.set1 <- subset(x = pbmc.seurat, cells = colnames(pbmc.seurat)[set])
pbmc.set2 <- subset(x = pbmc.seurat, cells = colnames(pbmc.seurat)[-set])

Then I want to transfer labels from pbmc.set1 to pbmc.set2, so I did:

anchors <- FindTransferAnchors(
  reference = pbmc.set1,
  query = pbmc.set2,
  k.filter = NA,
  reference.reduction = "spca",
  dims = 1:50
)

pbmc.set2 <- MapQuery(
  anchorset = anchors,
  query = pbmc.set2,
  reference = pbmc.set1,
  refdata = "seurat_annotations",
  reference.reduction = "spca", 
  reduction.model = "wnn.umap"
)

But when I ran MapQuery, I got this issue:

Error: Please set k.weight to be smaller than the number of anchors (25).

@jjia1
Copy link

jjia1 commented Aug 2, 2023

@ProphetDarkland @yuhanH @Yale73 @Winnie09

Not sure if this issue was ever resolved, but I encountered the same problem and found the solution for it. If you're getting a k.weight is too small error with MapQuery you can specify in the transferdata.args parameter for MapQuery. You must provide a list of the arguments. See bottom code for example.

Some code from a test run of mine:

results <- MapQuery(anchorset = anchors, reference = data, query = query, refdata = list(predicted.celltype = "celltype"), reference.reduction = "pca", reduction.model = "umap")
Error: Please set k.weight to be smaller than the number of anchors (27).

Since k.weight is not a parameter in MapQuery but it is in TransferData, you need to set the value to FALSE in either TransferData or MapQuery. TransferData should be

TransferData(anchorset = anchors, refdata = reference, dims = 1:30, k.weight = FALSE)

while MapQuery should be

results <- MapQuery(anchorset = anchors, reference = reference, query = query, refdata = list(predicted_celltype = "celltype"), reference.reduction = "pca", reduction.model = "umap", transferdata.args = list(k.weight = FALSE))
Finding integration vectors
Finding integration vector weights
0%   10   20   30   40   50   60   70   80   90   100%
[----|----|----|----|----|----|----|----|----|----|
**************************************************|
Predicting cell labels
Predicting cell labels
Predicting cell labels
  |                                                  | 0 % ~calculating  
Integrating dataset 2 with reference dataset
Finding integration vectors
Integrating data
  |++++++++++++++++++++++++++++++++++++++++++++++++++| 100% elapsed=02s  
Computing nearest neighbors
Running UMAP projection
20:38:00 Read 74037 rows
20:38:00 Processing block 1 of 1
20:38:00 Commencing smooth kNN distance calibration using 1 thread with target n_neighbors = 30
20:38:02 Initializing by weighted average of neighbor coordinates using 1 thread
20:38:02 Commencing optimization for 167 epochs, with 2221110 positive edges
Using method 'umap'
0%   10   20   30   40   50   60   70   80   90   100%
[----|----|----|----|----|----|----|----|----|----|
**************************************************|
20:38:49 Finished
Warning message:
In RunIntegration(filtered.anchors = filtered.anchors, reference = reference,  :
  Number of anchors is less than k.weight. Lowering k.weight for sample pair.

I'm not really sure if the warning message is a problem, but at least this will allow the code to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
more-information-needed We need more information before this can be addressed
Projects
None yet
Development

No branches or pull requests

5 participants