Giter Club home page Giter Club logo

Comments (13)

cafferychen777 avatar cafferychen777 commented on August 15, 2024

Hi,

Based on the images you've shared, it seems like there might be an issue with the code you ran. It appears that all the cells in the heatmap are approximately the same color, indicating that there's no range represented.

It's possible that you haven't set the sample.id in the metadata to be the sample_name, which could be causing this problem. I would suggest checking that to see if it helps.

library(ggpicrust2)
library(tidyverse)
abundance_matrix <- read.csv("~/Microbiome/ggpicrust2总/ggpicrust2测试/ggpicrust2_test/hharder1/abundance_matrix.csv", sep="")
metadata <- read.delim("~/Microbiome/ggpicrust2总/ggpicrust2测试/ggpicrust2_test/hharder1/metadata.txt")
metadata$sample_name <- metadata$sample.id
rownames(abundance_matrix) <- metadata$sample.id
abundance_matrix <- t(abundance_matrix)
ggpicrust2::pathway_heatmap(abundance_matrix, metadata, "Txt")

截屏2023-04-27 14 35 44

However, I don't think it's a good idea to directly plot a heatmap for such a large matrix. Instead, you may want to focus on only plotting the pathways that have statistical significance in the pathway analysis, or you can identify the pathways that you are interested in and plot only those.

I hope this helps. Let me know if you have any further questions.

Best regards,
Chen YANG

from ggpicrust2.

hharder1 avatar hharder1 commented on August 15, 2024

What is the best way to filter the heatmap to only show the significant pathways?

from ggpicrust2.

cafferychen777 avatar cafferychen777 commented on August 15, 2024
library(ggpicrust2)
library(tidyverse)
abundance_matrix <- read.csv("~/Microbiome/ggpicrust2总/ggpicrust2测试/ggpicrust2_test/hharder1/abundance_matrix.csv", sep="")
metadata <- read.delim("~/Microbiome/ggpicrust2总/ggpicrust2测试/ggpicrust2_test/hharder1/metadata.txt")
metadata$sample_name <- metadata$sample.id
rownames(abundance_matrix) <- metadata$sample.id
daa_results_df <- pathway_daa(...)
abundance_matrix_sub <-  abundance_matrix[colnames(abundance_matrix) %in% daa_results_df[daa_results_df$p.adjust <= 0.05,]$feature, ]
abundance_matrix_sub <- t(abundance_matrix_sub)
ggpicrust2::pathway_heatmap(abundance_matrix_sub, metadata, "Txt")

from ggpicrust2.

Nathanielhubert avatar Nathanielhubert commented on August 15, 2024

Hello again Chen Yang,

Thank you for all your help the other day. I am pretty new to R and am still struggling with my heatmap. These commands produce a good heatmap, but I am wondering how I can get labels for the sample names, with the samples grouped by experimental treatment (pre_post_intervention) with a color coded legend bar on the bottom (per the example on your main page).

Your continued help is greatly appreciated, thank you! Nate

kegg_abundance <- ko2kegg_abundance(file = "pred_metagenome_unstrat.tsv")

metadata <-
  read_delim(
    "QIIME_map_comp4_prePostOnly.txt",
    delim = "\t",
    escape_double = FALSE,
    trim_ws = TRUE
  )

metadata2 <- as.data.frame(metadata) %>%
  rename(sample_name = `#SampleID`)

heatmap_plot <- pathway_heatmap(kegg_abundance, metadata2, "pre_post_intervention")
print(heatmap_plot)



## filtered heatmap

sig_Welch <- daa_results_df[daa_results_df$p_adjust <= 0.0001 & daa_results_df$method == "ALDEx2_Welch's t test", ]$feature

# Use the selected pathways to filter the abundance matrix
kegg_abundance_sig <- kegg_abundance[sig_Welch, ]

heatmap_plot2 <- ggpicrust2::pathway_heatmap(kegg_abundance_sig, metadata2, "pre_post_intervention")

daa_results_df.txt
pred_metagenome_unstrat.txt
QIIME_map_comp4_prePostOnly.txt

from ggpicrust2.

cafferychen777 avatar cafferychen777 commented on August 15, 2024

Hello @Nathanielhubert ,

Adding a color label to the bottom of heatmap is a beta function. It can't work stablely so I am still modifing it. I would release the new version with the renewed heatmap. Now you can use image editing software such as Photoshop or Illustrator to add labels to the heatmap.

Best regards,
Chen YANG

from ggpicrust2.

Nathanielhubert avatar Nathanielhubert commented on August 15, 2024

Thank you, Chen Yang,

I was able to get labels using the code below, but it is difficult to be sure the labels are correctly aligned with the data in part because the heatmap is plotting z-scores and I have a table of kegg_abundance.

In hopes of ensuring the correct order of data, I put my metadata and kegg_abundance tables in the same order that I wanted the heatmap plot to show (I was finding the order of the labels may not align correctly if you try re-sorting in R).

Is it possible to plot the abundance data (or log abundance or log relative abundance, whatever is most appropriate) rather than z-score?

I am also running into a scale issue with the z-score legend as the legend only labels values 0.2, 0.4, 0.6, 0.8 which is a very narrow range, smushed together in the middle of the minimum and maximum values. Is there any way to fix that? Is z-score the most appropriate value to be plotting?

Thank you again for your help! Nate

`library(readr)
library(ggpicrust2)
library(tidyverse)
library(patchwork)
library(ggprism)
library(dplyr)

kegg_abundance <- ko2kegg_abundance(file = "pred_metagenome_unstrat.tsv")

metadata <-
read_delim(
"metadata.txt",
delim = "\t",
escape_double = FALSE,
trim_ws = TRUE
)

metadata2 <- as.data.frame(metadata) %>%
rename(sample_name = sample_name)

group <-
"pre_post_intervention"

# Create a heatmap

heatmap_plot <- pathway_heatmap(t(kegg_abundance), metadata2, "pre_post_intervention")

heatmap_plot <- heatmap_plot +
scale_x_discrete(labels = rownames(kegg_abundance)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
print(heatmap_plot)

ggsave("picrust_heatmap.pdf", plot = heatmap_plot, width = 10, height = 40)`

picrust_heatmap.pdf

from ggpicrust2.

cafferychen777 avatar cafferychen777 commented on August 15, 2024

Hello @Nathanielhubert ,

You can achieve the desired result by using the following code:

library(readr)
library(ggpicrust2)
library(tidyverse)
library(patchwork)
library(ggprism)

# Update to the latest version
devtools::install_github("cafferychen777/ggpicrust2")

daa_Welchs_results_0001_df_annotated <- read.delim("~/Microbiome/ggpicrust2总/ggpicrust2测试/ggpicrust2_test/Nathanielhubert/daa_Welchs_results_0001_df_annotated.txt")

kegg_abundance <- read.delim("~/Microbiome/ggpicrust2总/ggpicrust2测试/ggpicrust2_test/Nathanielhubert/kegg_abundance.txt", row.names = 1)

metadata <- read_delim("Nathanielhubert/QIIME_map_comp4_prePostOnly.txt", delim = "\t", escape_double = FALSE, trim_ws = TRUE)

daa_Welchs_results_0001_df_annotated$p_adjust <- round(daa_Welchs_results_0001_df_annotated$p_adjust, 5)

daa_Welchs_results_0001_df_annotated <- daa_Welchs_results_0001_df_annotated[!is.na(daa_Welchs_results_0001_df_annotated$pathway_name),]

p <- pathway_errorbar(
  abundance = kegg_abundance,
  daa_results_df = daa_Welchs_results_0001_df_annotated,
  Group = metadata$pre_post_intervention,
  p_values_threshold = 0.0001,
  order = "pathway_class",
  select = NULL,
  ko_to_kegg = TRUE,
  p_value_bar = TRUE,
  colors = NULL,
  x_lab = "pathway_name"
)

metadata2 <- metadata %>% rename(sample_name = `#SampleID`)

group <- "pre_post_intervention"

# Create a heatmap
heatmap_plot <- ggpicrust2::pathway_heatmap(abundance = kegg_abundance, metadata = metadata2, group = "pre_post_intervention")

heatmap_plot <- heatmap_plot +
  scale_x_discrete(labels = levels(heatmap_plot$data$Sample)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(heatmap_plot)

ggsave("picrust_heatmap.pdf", plot = heatmap_plot, width = 10, height = 40)

Please make sure to update to the latest version by running the following command before executing the code:

devtools::install_github("cafferychen777/ggpicrust2")

picrust_heatmap.pdf

Feel free to modify the code as per your requirements. Let me know if you need any further assistance!
截屏2023-05-19 19 12 51

from ggpicrust2.

Nathanielhubert avatar Nathanielhubert commented on August 15, 2024

Hi again, If you look at the two attached heatmaps here, the z-score patterns are the same but the order of the samples is different - labels must be wrong in one of them. Is there a way to know the labels are right? Do you have to plot z-score or can you plot the abundance patterns?

picrust_heatmap 4.18.21 PM.pdf
picrust_heatmap_pre_next_to_post.pdf

from ggpicrust2.

Nathanielhubert avatar Nathanielhubert commented on August 15, 2024

Screen Shot 2023-05-19 at 6 23 18 PM

Also, the legend is not very informative

from ggpicrust2.

cafferychen777 avatar cafferychen777 commented on August 15, 2024

If you are using heatmap_plot +
scale_x_discrete(labels = levels(heatmap_plot$data$Sample)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)), the plot will be right. Because the levels(heatmap_plot$data$Sample determine the order of x axis of heatmap.

from ggpicrust2.

cafferychen777 avatar cafferychen777 commented on August 15, 2024

The code inside pathway_heatmap automatically attaches same group level together.

from ggpicrust2.

cafferychen777 avatar cafferychen777 commented on August 15, 2024
Screenshot 2023-06-03 at 09 26 01 Screenshot 2023-06-03 at 09 20 57

Hello @Nathanielhubert ,

I just update ggpicrust2 and I think the new function will help you. Now you don't need to add a label manually.

# # Create a heatmap
heatmap_plot <- pathway_heatmap(abundance = kegg_abundance[daa_Welchs_results_0001_df_annotated$feature,], metadata = metadata2, group = "pre_post_intervention")

pathway_heatmap(abundance = kegg_abundance %>% rownames_to_column("feature") %>% right_join(daa_Welchs_results_0001_df_annotated %>% select(all_of(c("feature","pathway_name"))), by = "feature") %>% column_to_rownames("pathway_name") %>% select(-all_of("feature")), metadata = metadata2, group = "pre_post_intervention")

Best Regards,

from ggpicrust2.

Nathanielhubert avatar Nathanielhubert commented on August 15, 2024

from ggpicrust2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.