zdk123 / SpiecEasi

Sparse InversE Covariance estimation for Ecological Association and Statistical Inference
GNU General Public License v3.0
191 stars 68 forks source link

Error in i_set_vertex_attr #258

Open kaumudiprabhakara opened 6 months ago

kaumudiprabhakara commented 6 months ago

Hello,

I am a first time user of this package. I am reading in two csv files to create a phyloseq object that I want to run this package on. I am working on a Windows machine, with R version 4.3.3. I updated all packages before running these. Below is my code.

library("phyloseq")
library(SpiecEasi)
library(igraph)
library(Matrix)

X <- read.csv('counts_genusnames_d10.csv') ### read in the ASV data
X1 <- X[,-1] ### Remove first columns, which is the sample names
counts <- data.matrix(X1) ### convert dataframe to matrix
rownames(counts)<- X$Unnamed..0 ## Add row names
countst <- t(counts) ## transpose to get taxa on rows

taxa <- read.csv('asv_phyl_map.csv') ###read in phylogeny
taxa1 <-taxa[,-1] ### Remove first columns, which is the ASV names
rownames(taxa1)<- taxa$X ## Add row names
taxmat <- as.matrix(taxa1) ## Convert to matrix

### Create phyloseq object
OTU = otu_table(countst, taxa_are_rows = TRUE)
TAX = tax_table(taxmat)
physeq = phyloseq(OTU, TAX)

## Apply the MB algorithm
se <- spiec.easi(physeq, method='mb', lambda.min.ratio=1e-2, nlambda=15)

Upto here everything works fine. se$refit gives a sparse square matrix. But, when I run this:

 ig.mb <- adj2igraph(se$refit, vertex.attr=list(name=taxa_names(physeq)))

I get the error: Error in i_set_vertex_attr(graph = graph, name = name, value = value, : Length of new attribute value must be 1, the number of target vertices, not 58 The se$refit matrix is 58 x 58 and the taxa_names(physeq) is also 58 long, but list(name= taxa_name(physeq)) is a list of 1 with 58 characters. There should be 58 vertices not 1, because that's how many ASVs I have in the data. I got these details when I ran traceback():

5: stop("Length of new attribute value must be ", if (length(index) != 
       1) "1 or ", length(index), ", the number of target vertices, not ", 
       length(value))
4: i_set_vertex_attr(graph = graph, name = name, value = value, 
       check = FALSE)
3: set_vertex_attr(graph = graph, name = name, index = index, value = value)
2: igraph::set.vertex.attribute(g, attr, index = igraph::V(g), vertex.attr[[i]])
1: adj2igraph(se$refit, vertex.attr = list(name = taxa_names(physeq)))

I tried to run the example code provided, but my R crashes when I run this line. However, I do see that list(name= taxa_name(amgut.ctl)) is a list of 1, like in my case. So I'm not sure if this crashing is a related problem.

I would appreciate any help.

kaumudiprabhakara commented 5 months ago

@zdk123 Could you please help? Thanks!

zdk123 commented 5 months ago

It's a bit hard to say without your data. Can you make sure that input graph is not empty?

kaumudiprabhakara commented 5 months ago

The se$refit is a square matrix with lots of ones in it. So I don't think the input graph is empty.

I am working on a way to get a MWE.

kaumudiprabhakara commented 5 months ago

Hello @zdk123

I have attached two csv files needed for this minimal working example.

small_counts_file_for_MWE.csv small_asv_phyl_map_for_MWE.csv

library("phyloseq")
library(SpiecEasi)
library(igraph)
library(Matrix)
X <- read.csv('small_counts_file_for_MWE.csv')
X1 <- X[,-1]
counts <- data.matrix(X1)
rownames(counts)<- X$Unnamed..0
countst <- t(counts)

taxa <- read.csv('small_asv_phyl_map_for_MWE.csv')
taxa1 <-taxa[,-1]
rownames(taxa1)<- taxa$X
taxmat <- as.matrix(taxa1)

OTU = otu_table(countst, taxa_are_rows = TRUE)
TAX = tax_table(taxmat)
physeq = phyloseq(OTU, TAX)

se <- spiec.easi(physeq, method='mb', lambda.min.ratio=1e-2, nlambda=15)

Until here, everything works. The following line shows the error mentioned in my first post:

ig.mb <- adj2igraph(se$refit, vertex.attr=list(name=taxa_names(physeq)))

My sessionInfo()

R version 4.4.0 (2024-04-24 ucrt) Platform: x86_64-w64-mingw32/x64 Running under: Windows 11 x64 (build 22621)

Matrix products: default

locale: [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8 [4] LC_NUMERIC=C LC_TIME=English_United States.utf8

time zone: Europe/Berlin tzcode source: internal

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

other attached packages: [1] igraph_2.0.3 SpiecEasi_1.1.3 phyloseq_1.44.0 Matrix_1.7-0

loaded via a namespace (and not attached): [1] shape_1.4.6.1 gtable_0.3.5 ggplot2_3.5.1 rhdf5_2.44.0 Biobase_2.60.0
[6] lattice_0.22-6 rhdf5filters_1.12.1 vctrs_0.6.5 tools_4.4.0 bitops_1.0-7
[11] generics_0.1.3 biomformat_1.28.0 stats4_4.4.0 parallel_4.4.0 tibble_3.2.1
[16] fansi_1.0.6 cluster_2.1.6 pkgconfig_2.0.3 huge_1.3.5 data.table_1.15.4
[21] S4Vectors_0.38.2 lifecycle_1.0.4 GenomeInfoDbData_1.2.10 compiler_4.4.0 stringr_1.5.1
[26] Biostrings_2.68.1 munsell_0.5.1 codetools_0.2-20 permute_0.9-7 GenomeInfoDb_1.36.3
[31] glmnet_4.1-8 RCurl_1.98-1.14 pillar_1.9.0 crayon_1.5.2 MASS_7.3-60.2
[36] vegan_2.6-4 iterators_1.0.14 foreach_1.5.2 nlme_3.1-164 tidyselect_1.2.1
[41] digest_0.6.35 stringi_1.8.3 dplyr_1.1.4 reshape2_1.4.4 splines_4.4.0
[46] ade4_1.7-22 grid_4.4.0 colorspace_2.1-0 cli_3.6.2 magrittr_2.0.3
[51] survival_3.6-4 utf8_1.2.4 ape_5.8 scales_1.3.0 XVector_0.40.0
[56] multtest_2.56.0 pulsar_0.3.11 VGAM_1.1-10 IRanges_2.34.1 mgcv_1.9-1
[61] rlang_1.1.3 Rcpp_1.0.12 glue_1.7.0 BiocGenerics_0.46.0 rstudioapi_0.16.0
[66] jsonlite_1.8.8 R6_2.5.1 Rhdf5lib_1.22.1 plyr_1.8.9 zlibbioc_1.46.0

kaumudiprabhakara commented 4 months ago

@zdk123 Could you please let me know how to fix this? Thanks!