adw96 / DivNet

diversity estimation under ecological networks
83 stars 18 forks source link

simplifyBeta() missing functionality for aitchison distance #116

Closed msmcfarlin closed 2 years ago

msmcfarlin commented 2 years ago

Hello,

I would like to use simplifyBeta() to extract Aitchison distance estimates for PCoA plotting. When I try running the function it returns an error that states ! Can't subset columns that don't exist. Columns A014, A021, A025, A051, A053, etc. don't exist.

simplifyBeta works with my metadata when extracting euclidean and bray-curtis estimates so I don't think it's an error with the sample names. I noticed in the beta diversity divnet object I am using that there isn't an aitchison distance metric. I also saw in the testBetaDiversity() code that it uses the 'fitted_z' object to calculate aitchison distance. So I was thinking of calculating the aitchison distance using some of that code, adding it to the beta divnet object, then using simplifyBeta to extract the estimates.

Does that sounds like it would work? Or maybe I'm missing a step in properly calculating the aitchison distance?

Here is the code I used...

#aitchison distance related functions from testBetaDiversity()
      get_aitchison_distance <- function(comp_matrix){
                  lr_matrix <- log_ratio(comp_matrix)
                  return(as.matrix(dist(lr_matrix)))}

      log_ratio <- function(comp_matrix){
                  lr_matrix <- log(comp_matrix)
                  lr_matrix <- lr_matrix -matrix(apply(lr_matrix,1, mean),ncol = 1)%*%matrix(1, ncol = ncol(lr_matrix))
                  return(lr_matrix)}

#calculate aitchison distance
      aitch_matrix <- get_aitchison_distance(beta_divnet_object$fitted_z)

#add column and rownames that match euclidean object
      colnames(aitch_matrix) <- colnames(beta_divnet_object$euclidean)
      rownames(aitch_matrix) <- rownames(beta_divnet_object$euclidean)

#add to beta divnet object
      beta_divnet_object$aitch_matrix <- aitch_matrix

#Run simplifyBeta() to extract estimates
      estimatesID <- simplifyBeta(beta_divnet_object,
                                                    phyloseq_object,
                                                    "aitch_matrix", "Sample_ID")

Thank you for your assistance!

ailurophilia commented 2 years ago

Hi Mike,

I apologize for the delay in my response. It looks like this is actually an issue that we started to patch and the pull request just didn't get merged, so I'll nip that in the bud shortly.

Your code looks good to me – do you get reasonable results?

Thanks!

Best, David

ailurophilia commented 2 years ago

Just updating to say that simplifyBeta should now accept Aitchison distances.

msmcfarlin commented 2 years ago

Hi David,

Thanks for the update! The results with my code seemed reasonable though I'll check with the updated simplifyBeta() function.

Thanks, Mike