stefpeschel / NetCoMi

Network construction, analysis, and comparison for microbial compositional data
GNU General Public License v3.0
146 stars 26 forks source link

Error in diffnet(). 'net' is a single network #66

Closed Valentin-Bio-zz closed 1 year ago

Valentin-Bio-zz commented 2 years ago

Hello , I've got a problem with the diffnet(). I elaborated a net with netConstruct():

oral_NO_AR_net <- netConstruct(data = adjma_oral_AR,
                               data2 = adjma_oral_NO,
                               dataType = "condDependence", normMethod = "none", 
                               sparsMethod = "none", zeroMethod = "none", 
                               verbose = 3, seed = 1234)

Then when I run diffnet() diff_oral_NO_AR <- diffnet(oral_NO_AR_net, diffMethod = "fisherTest", adjust = "adaptHB")

This is the error message:

Error in diffnet(oral_NO_AR_net, diffMethod = "fisherTest", adjust = "adaptHB") : 'net' is a single network. A group vector must be passed to 'NetConstruct()' for network comparison.

seems like diffnet() is not recognizing the other network from the oral_NO_AR_net object.

I took a look to the oral_NO_AR_net$assoMat1 and oral_NO_AR_net$assoMat2 variables to see if there is some lacking data but the association matrices are present.

In consideration, both nets have different number of samples and different number of OTUs, in what way does it affect the analysis?

stefpeschel commented 2 years ago

Hi Valentin,

This is indeed a bug. I'm on it ...

Sorry for the inconvenience!

Best, Stefanie

stefpeschel commented 2 years ago

@Valentin-Bio, could you please install NetCoMi from develop branch and check whether everything works?

You'll have to set the new arguments n1 and n2 because sample sizes are needed for Fisher's z-test and can't be taken from the count matrices in this case.

Please, let me know if there are further problems.

Best, Stefanie

Valentin-Bio-zz commented 2 years ago

Hello Stefanie, thanks for the fast answer!. I'm having problems uninstalling the master branch repo with remove.packages("NetCoMi") on Ubuntu 22.04. First I need to uninstall the program, then to install the developer branch using devtools::install_github("stefpeschel/NetCoMi@develop") isnt it ?. if so I'm solving this problem on that way.

best,

Valentín

Valentin-Bio-zz commented 2 years ago

Ok , so I ran the following code:

oral_NO_AR_net <- netConstruct(data = adjma_oral_AR,
                               data2 = adjma_oral_NO,
                               dataType = "condDependence", normMethod = "none", 
                               sparsMethod = "none", zeroMethod = "none", 
                               verbose = 3, seed = 1234)

then executed the diffnet()

diff_oral_NO_AR <- diffnet(oral_NO_AR_net, diffMethod = "fisherTest", adjust = "adaptBH",
                           n1 = 182, n2 = 98)

I have the following error message:

_

**Checking input arguments ... 
Done.
Adjust for multiple testing using 'adaptBH' ... 
 Proportion of true null hypotheses: 1
Done.
Error in assoMat1 - assoMat2 : non-conformable arrays
In addition: Warning message:
In z1 - z2 :
  longer object length is not a multiple of shorter object length**

my data have different number of samples as shown in teh n1 and n2 commands, but what can I do?

Best,

Valerntín

stefpeschel commented 2 years ago

Hey Valentín,

Regarding the installation: Actually, you don't have to remove the version from main branch before installing from develop. I would generally recommend closing all active R sessions and re-install in base R to ensure there are no dependencies of NetCoMi loaded. The code for installing from develop branch can be found in the readme. Using force = TRUE could also help to force the installation.

And now back to diffnet: It seems that your association/adjacency matrices have different dimensions leading to different lengths of z1 and z2. The two matrices must contain the same taxa - and thus have the same dimensions - to be comparable.

A different number of samples is no problem for the approach. I also tested with different values for n1 and n2.

You could run the following code to check whether diffnet works in principle:

data("amgut2.filt.phy")

# Split the phyloseq object into two groups
amgut_split <- metagMisc::phyloseq_sep_variable(amgut2.filt.phy, 
                                                "SEASONAL_ALLERGIES")

# Network construction
net_season <- netConstruct(data = amgut_split$no, 
                           data2 = amgut_split$yes,  
                           filtTax = "highestVar",
                           filtTaxPar = list(highestVar = 50),
                           measure = "spieceasi",
                           measurePar = list(method = "mb",
                                             pulsar.params = list(rep.num = 10),
                                             symBetaMode = "ave"),
                           normMethod = "none", 
                           zeroMethod = "none",
                           sparsMethod = "none", 
                           dissFunc = "signed",
                           verbose = 3,
                           seed = 123456)

adjma_oral_AR <- net_season$adjaMat1
adjma_oral_NO <- net_season$adjaMat2

oral_NO_AR_net <- netConstruct(data = adjma_oral_AR,
                               data2 = adjma_oral_NO,
                               dataType = "condDependence", normMethod = "none", 
                               sparsMethod = "none", zeroMethod = "none", 
                               verbose = 3, seed = 1234)

diff_oral_NO_AR <- diffnet(oral_NO_AR_net, 
                           diffMethod = "fisherTest", 
                           adjust = "adaptBH",
                           n1 = 182, n2 = 98)

Best, Stefanie

Valentin-Bio-zz commented 1 year ago

Hey Stefanie , I ran the diffnet() fucntion with the suggested code and I got the following error message:

Error in plot.diffnet(diff_oral_NO_AR, cexNodes = 0.8, cexLegend = 3,  : 
  Network is empty.

Besides that, to run the fisher test, I understand I need to have the same dimensions on both matrices and input the sample sizes for each net.

As input data I only have the adjacency matrices from SpiecEasi and they have different dimensions. (different ASVs quantity). To handle that I looked for the common ASVs on the following manner:

Matrix 1 subset

which(rownames(adjma1) %in% rownames(adjma2))

subfordiff_adjma1 <- adjma1[which(rownames(adjma1) %in% rownames(adjma2)),
                                       which(rownames(adjma1) %in% rownames(adjma2))]

Matrix 2 subset

which(rownames(adjma2) %in% rownames(adjma1))

subfordiff_adjma2 <- adjma2[which(rownames(adjma2) %in% rownames(adjma1)),
                                       which(rownames(adjma2) %in% rownames(adjma_2))]

Edit: the above code is a bad approach. ASVs's IDs are not taxa specific

From there, both matrices have the same taxa (thus same dimensions)

nrow(subfordiff_adjma1) == nrow(subfordiff_adjma2)

TRUE

So I built the netcomi networks and ran diffnet():

nets_fordiff <- netConstruct(data = subfordiff_adjma1,
                                    data2 = subfordiff_adjma2,
                                    normMethod = "none", zeroMethod = "none",
                                    sparsMethod = "none", dataType = "condDependence",
                                    verbose = 3, seed = 1234)

message:

Checking input arguments ... Done. Adjust for multiple testing using 'lfdr' ... Execute fdrtool() ... Step 1... determine cutoff point Step 2... estimate parameters of null distribution and eta0 Step 3... compute p-values and estimate empirical PDF/CDF Step 4... compute q-values and local fdr

Done.

But when I plot the nets_fordiff() I got this:

plot(nets_fordiff, 
     cexNodes = 0.9, cexLegend = 3, cexTitle = 4, mar = c(2,2,8,5), 
     legendGroupnames = c("group 'no'", "group 'yes'"), legendPos = c(0,7,1,6) )

Error in plot.diffnet(diff_oral_NO_AR, cexNodes = 0.9, cexLegend = 3, : Network is empty.

Examining the nets_fordiff$diffAdjustMat, it is a full 0s matrix but the nets_fordiff$diffMat has non 0 values.

stefpeschel commented 1 year ago

Hi Valentín,

As stated in the documentation of diffnet(), $diffMat contains the differences without any adjustment, and $diffAdjustMat are the differences after adjusting for multiple testing.

The plot function has an adjusted argument, which can be set to "FALSE" to plot the unadjusted differences. The argument is "TRUE" by default leading to the error that the network is empty if the diffAdjustMat contains zeros only.

Best, Stefanie

Valentin-Bio-zz commented 1 year ago

Ok I used the unadjusted differences, thanks so much Stefanie.

Best,

Valentín.