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

Error in tcrossprod(x, y) : requires numeric/complex matrix/vector arguments #27

Open
MartaSanchezCarbonell opened this issue Feb 9, 2024 · 5 comments
Labels
answered answer provided

Comments

@MartaSanchezCarbonell
Copy link

Dear Lamian team,

Thank you very much for creating this tool, it looks really promising and useful for my analysis. However, I am facing an error.
I am studying the gene expression over time from two conditions (WT and KO) and I would like to apply your XDE test from module 3. I performed the pseudotime analysis with Slingshot and I am trying to use the function lamian_test() with test.type = 'variable' to know the genes that are differentially expressed between conditions along pseudotime.

My working object is a Seurat object where I have the pseudotime calculated with Slingshot in a slot in metadata. I have tried to supply all the arguments following your tutorial https://winnie09.github.io/Wenpin_Hou/pages/Lamian.html and trying to give them in the correct format (either data frames, matrices or vectors). But, I am obtaining the following error: Error in tcrossprod(x, y) : requires numeric/complex matrix/vector arguments

I am pasting my code here:
#Checking if all cells have pseudotime value
summary(data$slingshot_pseudotime_separated)
#Creating exp matrix
exp<-as.matrix(data@assays[["SCT"]]@DaTa)
#Creating design
intercept<-c("1","1")
group<-c("1","0")
Genotype<-c("WT","KO")
design<-data.frame(intercept,group)
row.names(design) <- Genotype
design<-as.matrix(design)
#Creating cell annotation
Geno<-as.data.frame(data$Genotype)
Cell<-row.names(Geno)
Geno_1<-Geno$data$Genotype
cellanno<-data.frame(Cell,Geno_1)
#Creating pseudotime numeric vector with cell names
pseudotime<-data$slingshot_pseudotime_separated
names(pseudotime) <-Cell
#XDE test
Res <- lamian_test(
expr = as.matrix(data@assays[["RNA"]]@CountS),
cellanno = cellanno,
pseudotime = pseudotime,
design = design,
test.type = 'variable',
testvar = 2,
permuiter = 100,

This is for permutation test only.

We suggest that users use default permuiter = 100.

Alternatively, we can use test.method = 'chisq' to swich to the chi-square test.

ncores = 1
)

Could you please help me to find where is the error?

Thank you very much in advance and I wish you a nice day!

Marta

@Winnie09
Copy link
Owner

Winnie09 commented Feb 11, 2024

Dear Marta,

Thank you for reaching out and for your interest in using the Lamian package for your analysis. Based on the error message you've encountered (Error in tcrossprod(x, y) : requires numeric/complex matrix/vector arguments), it seems the issue arises from the input format or type of the arguments you're passing to the lamian_test() function, particularly those involved in the tcrossprod() operation within the function. This error typically occurs when tcrossprod() is called with non-numeric or incorrectly structured arguments. Here are a few suggestions to troubleshoot and resolve this issue:

Check the Expression Matrix Format: Ensure that the expression matrix (expr) you're passing is a numeric matrix. The error might stem from using data@assays[["RNA"]]@CountS directly without ensuring it's in the correct format. You've correctly converted it to a matrix with as.matrix(), but it's worth double-checking that this matrix contains only numeric values.

Verify the Design Matrix: Your design matrix construction seems correct, but ensure that it is indeed numeric and properly formatted. Sometimes, data conversion or subsetting operations might unintentionally alter the data type. It could be helpful to explicitly convert the intercept and group to numeric types if they are not already.

Pseudotime Vector: Ensure that the pseudotime vector is purely numeric and correctly named with cell names. The naming of cells in the pseudotime vector should precisely match those in the expression matrix and cell annotation dataframe.

Cell Annotation Data Frame: Double-check the cellanno dataframe to ensure that it's structured correctly with cell names and sample/group identifiers. The cell names (Cell) should match exactly those used in the expression matrix and pseudotime vector.

Debugging Tips:

Try running the lamian_test() function with a smaller subset of your data to see if the error persists. This could help isolate the issue.
Print out or inspect the structures of expr, cellanno, pseudotime, and design just before calling lamian_test() to confirm they're all in the expected format.
Expression Matrix in expr Parameter: There seems to be a discrepancy in your code regarding which expression matrix is used. Initially, you create exp from data@assays[["SCT"]]@DaTa, but for the lamian_test() function, you switch to data@assays[["RNA"]]@CountS. Ensure you're using the intended matrix for your analysis.

If after these checks the issue remains unresolved, it could be helpful to directly inspect the lamian_test() function's handling of the input arguments, particularly where tcrossprod() is called, to ensure compatibility with the inputs. Sometimes, directly debugging with a smaller set of data can reveal where the mismatch occurs. For example, you might also check if the values in the pseudotime are unique.

I hope these suggestions help you move forward with your analysis. Please let me know how it works. Thanks!

@Winnie09 Winnie09 added the answered answer provided label Feb 11, 2024
@MartaSanchezCarbonell
Copy link
Author

MartaSanchezCarbonell commented Feb 13, 2024

Dear Winnie,

Thank you very much for your answer, I have implemented your suggestions but now I am encountering another error:
"Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 't': error in evaluating the argument 'x' in selecting a method for function 'chol2inv': the leading minor of order 1 is not positive"
I've checked in the function page https://rdrr.io/github/Winnie09/Lamian/src/R/lamian_test.R but I can't find any "chol2inv" function. Could you please help me?

I paste the actual code with the results form the tests that you suggested:

summary(data$slingshot_pseudotime_separated)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 4.019 8.506 9.518 14.187 21.009
#Creating exp matrix
exp<-as.matrix(data@assays[["SCT"]]@DaTa)
is.numeric(exp)
[1] TRUE
#Creating design
intercept<-c(1,1)
group<-c(1,0)
Genotype<-c("WT","KO")
design<-data.frame(intercept,group)
row.names(design) <- Genotype
design<-as.matrix(design)
#Creating cell annotation
Geno<-as.data.frame(data$Genotype)
Cell<-row.names(Geno)
Geno_1<-Geno$data$Genotype
cellanno<-data.frame(Cell,Geno_1)
#Creating pseudotime numeric vector with cell names
pseudotime<-data$slingshot_pseudotime_separated
is.numeric(pseudotime)
[1] TRUE
names(pseudotime) <-Cell
#Are cell names from pseudotime, annotation data frame and expression matrix the same?
exp_cells<-colnames(exp)
setequal(exp_cells,Cell)
[1] TRUE
#Are the values in pseudotime unique?
any(duplicated(pseudotime))
[1] TRUE
#XDE test
Res <- lamian_test(
expr = exp,
cellanno = cellanno,
pseudotime = pseudotime,
design = design,
test.type = 'variable',
testvar = 2,
permuiter = 100,
ncores = 1
)
In may case, pseudotime has values that are not unique because my conditions are different enough to calculate separate trajectories with slingshot. Is this a problem?

Best regards,

Marta

@MartaSanchezCarbonell
Copy link
Author

Dear Winnie,

Just to inform you that I tried to apply lamian_test() after removing the cells with equal pseudotime and I still get the error:

"Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 't': error in evaluating the argument 'x' in selecting a method for function 'chol2inv': the leading minor of order 1 is not positive"

If you could shade some light into my problem would be great.

Thank you very much in advance,

Marta

@Winnie09
Copy link
Owner

Winnie09 commented Apr 5, 2024

Hi Marta,

If you are using the pseudotime output from slingshot, are you also referring to the section 'Apply other pseudotime inference methods in Lamian: take slingshot as an example" of the manual page https://winnie09.github.io/Wenpin_Hou/pages/Lamian.html#apply-other-pseudotime-inference-methods-in-lamian-take-slingshot-as-an-example?

There are some operations to be taken for the pseudotime values before running Lamian. For example, the following lines are important:

pt <- pt[!is.na(pt)] ## important
selectedCell <- names(pt)

@MartaSanchezCarbonell
Copy link
Author

Dear Wenpin,

Thank you for your answer. Yes, I was following the section that you mentioned.
By checking the pseudotime summary, I see that there are no NA values, so I don't think that this is the problem.
You can see the results of the summary from my pseudotime:
summary(pseudotime)

Min. 1st Qu. Median Mean 3rd Qu. Max.

0.000 4.019 8.506 9.518 14.187 21.009

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered answer provided
Projects
None yet
Development

No branches or pull requests

2 participants