DESeq2

博客

library(readr)

library(dplyr)

library(ggplot2)

library(tidyverse)

library(DESeq2)

 

mycounts <- read.csv("C:/Users/~/Desktop/datacounts.csv")

metadata <-  read.csv("C:/Users/~/Desktop/datametadata.csv")

countData <- mycounts[!(duplicated(mycounts[[1]]) | duplicated(mycounts[[1]], fromLast=TRUE)), ]

rownames(mycounts) = make.names(mycounts$gene, unique=TRUE)

mycounts <- as.data.frame(mycounts)

metadata <- as.data.frame(metadata)

head(mycounts)

head(metadata)

class(mycounts)

class(metadata)

dds <- DESeqDataSetFromMatrix(countData=mycounts, 

                              colData=metadata, 

                              design=~1, 

                              tidy=TRUE)

dds

sizeFactors(dds)

dispersions(dds)

dds <- DESeq(dds)

res = results(dds, contrast=c("Group", "CNL", "Target"), tidy=TRUE)

res <- as_tibble(res)

res

res %>% 

    filter(padj<0.05) %>% 

    write_csv("sigresults.csv")

#smallestGroupSize <- 3

#keep <- rowSums(counts(dds) >= 10) >= smallestGroupSize

#dds <- dds[keep,]

vsdata <- vst(dds, blind=FALSE)

plotPCA(vsdata, intgroup="Group") + geom_label(aes(label = colnames(vsdata))) + labs(title=" ")

df <- as.data.frame(colData(vsdata)[,c("Group","R","NR")])

H <- c("A","B","C")])

mat <- assay(vsdata)[ H, ]

mat <- as.data.frame(mat)

mat1 <- mat %>% arrange(desc(TD))

pheatmap(mat1, annotation_col=df, cluster_rows=F, cluster_cols=F,main = "")

plotCounts(dds, gene="MYC", intgroup="Group", returnData=TRUE) %>% 

  ggplot(aes(Group, count)) + geom_boxplot(aes(fill=Group)) + scale_y_log10() + ggtitle("MYC")

vsdata <- varianceStabilizingTransformation(dds) (less row)

Volcanoplot

p <- ggplot(data=res, aes(x=log2FoldChange, y=-log10(pvalue))) + geom_point()

# Add more simple "theme"

p <- ggplot(data=res, aes(x=log2FoldChange, y=-log10(pvalue))) + geom_point() + theme_minimal()

# Add vertical lines for log2FoldChange thresholds, and one horizontal line for the p-value threshold 

p2 <- p + geom_vline(xintercept=c(-0.6, 0.6), col="red") +

    geom_hline(yintercept=-log10(0.05), col="red")

# The significantly differentially expressed genes are the ones found in the upper-left and upper-right corners.

# Add a column to the data frame to specify if they are UP- or DOWN- regulated (log2FoldChange respectively positive or negative)

# add a column of NAs

res$diffexpressed <- "NO"

# if log2Foldchange > 0.6 and pvalue < 0.05, set as "UP" 

res$diffexpressed[res$log2FoldChange > 0.6 & res$pvalue < 0.05] <- "UP"

# if log2Foldchange < -0.6 and pvalue < 0.05, set as "DOWN"

res$diffexpressed[res$log2FoldChange < -0.6 & res$pvalue < 0.05] <- "DOWN"

# Re-plot but this time color the points with "diffexpressed"

p <- ggplot(data=res, aes(x=log2FoldChange, y=-log10(pvalue), col=diffexpressed)) + geom_point() + theme_minimal()

# Add lines as before...

p2 <- p + geom_vline(xintercept=c(-0.6, 0.6), col="red") +

        geom_hline(yintercept=-log10(0.05), col="red")

## Change point color 

# 1. by resfault, it is assigned to the categories in an alphabetical orresr):

p3 <- p2 + scale_color_manual(values=c("blue", "black", "red"))

# 2. to automate a bit: ceate a named vector: the values are the colors to be used, the names are the categories they will be assigned to:

mycolors <- c("blue", "red", "black")

names(mycolors) <- c("DOWN", "UP", "NO")

p3 <- p2 + scale_colour_manual(values = mycolors)

# Now write down the name of genes besires the points...

# Create a new column "reslabel" to res, that will contain the name of genes differentially expressed (NA in case they are not)

res$delabel <- NA

res$delabel[res$diffexpressed != "NO"] <- res$row[res$diffexpressed != "NO"]

ggplot(data=res, aes(x=log2FoldChange, y=-log10(pvalue), col=diffexpressed, label=delabel)) + 

    geom_point() + 

    theme_minimal() +

    geom_text()

library(ggrepel)

# plot adding up all layers we have seen so far

ggplot(data=res, aes(x=log2FoldChange, y=-log10(pvalue), col=diffexpressed, label=delabel)) +

        geom_point() + 

        theme_minimal() +

        geom_text_repel() +

        scale_color_manual(values=c("blue", "black", "red")) +

        geom_vline(xintercept=c(-0.6, 0.6), col="red") +

        geom_hline(yintercept=-log10(0.05), col="red")

 

plotCounts(dds, gene="FSTL5", intgroup="Treatment", returnData=TRUE) %>% 

   ggplot(aes(Treatment, count)) + geom_boxplot(aes(fill=Treatment)) + scale_y_log10() + ggtitle("FSTL5")

P + scale_x_discrete(name ="Group", limits=c("CNL","Target"))

 

 

 

library("genefilter")

topVarGenes <- head(order(-rowVars(assay(rld))),30)

The heatmap becomes more interesting if we do not look at absolute expression strength but rather at the amount by which each gene deviates in a specific sample from the gene’s average across all samples. Hence, we center each genes’ values across samples, and plot a heatmap. We provide the column side colors to help identify the treated samples (in blue) from the untreated samples (in grey).

 

vsdata <- vst(dds, blind=FALSE)

plotPCA(vsdata, intgroup="Group")

plotPCA(vsdata, intgroup="Group") + geom_label(aes(label = colnames(vsdata)))

topVarGenes <- head(order(-rowVars(assay(vsdata))),50)

mat <- assay(vsdata)[ topVarGenes, ]

mat <- mat - rowMeans(mat)

df <- as.data.frame(colData(vsdata)[,c("ID","R","D","M","Treatment")])

pheatmap(mat, annotation_col=df, cluster_rows=FALSE, cluster_cols=FALSE)

 

res1 <-  res %>% filter(padj<0.05) %>% arrange(padj)

res2 <- res1[1:100,] %>% arrange(desc(log2FoldChange))

Sig <- res2$row

mat <- assay(vsdata)[ Sig, ]

 mat <- mat - rowMeans(mat)

df <- as.data.frame(colData(vsdata)[,c("ID","Treatment","HPV","PIK3CAmutation","Response.Primary","Responder")])

df <- as.data.frame(colData(vsdata)[,c("ID","Group")])

pheatmap(mat, annotation_col=df, cluster_rows=F, cluster_cols=F)

 

mat <- assay(vsdata)[ G, ]

mat1 <- as.data.frame(mat)

mat1 <- mat1 %>% arrange(desc(TD3))

pheatmap(mat1, annotation_col=df, cluster_rows=F, cluster_cols=F)

zmat <- scale(tmat1, center = TRUE, scale = TRUE)  upon column

 

sacPlsda <- opls(vsdata, "Treatment", orthoI = 1)

PLS-DA

GSEA

library(org.Hs.eg.db)

ens2symbol <- AnnotationDbi::select(org.Hs.eg.db,

                                    key=res$row, 

                                    columns="SYMBOL",

                                    keytype="SYMBOL")

ens2symbol <- as_tibble(ens2symbol)

ens2symbol

res2 <- res %>% 

  dplyr::select(SYMBOL, stat) %>% 

  na.omit() %>% 

  distinct() %>% 

  group_by(SYMBOL) %>% 

  summarize(stat=mean(stat))

res2

 

library(fgsea)

ranks <- deframe(res2)

head(ranks, 20)

 

pathways.hallmark <- gmtPathways("C:/Users/~/Desktop/GSEA dataset/ h.all.v2023.1.Hs.symbols.gmt")

fgseaRes <- fgsea(pathways=pathways.hallmark, stats=ranks, nperm=1000)

fgseaRes <- fgsea(pathways=pathways.hallmark, stats=ranks, eps      = 0.0,

                  minSize  = 15,

                  maxSize  = 500)

fgseaResTidy <- fgseaRes %>%

  as_tibble() %>%

  arrange(desc(NES))

 

# Show in a nice table:

fgseaResTidy %>% 

  dplyr::select(-leadingEdge, -ES, -nMoreExtreme) %>% 

  arrange(padj) 

ggplot(fgseaResTidy, aes(reorder(pathway, NES), NES)) +
  geom_col(aes(fill=padj<0.05)) +
  coord_flip() +
  labs(x="Pathway", y="Normalized Enrichment Score",
       title="Hallmark pathways NES from GSEA") + 
  theme_minimal()

plotEnrichment(pathways.hallmark[["HALLMARK_APOPTOSIS"]],

                            ranks) + labs(title=" HALLMARK_APOPTOSIS")

 

fgseaRes <- fgsea(pathways=pathways.hallmark, stats=ranks, nperm=1000)

go <- fgsea(pathways=gmtPathways("C:/Users/~/Desktop/GSEA dataset/c5.all.v7.2.symbols.gmt"), ranks, nperm=1000) %>%  as_tibble()

go <- as.matrix(go)

write.csv(go, "go.csv")

 

pathway

pval

padj

NES

size

1

HALLMARK_E2F_TARGETS

0.003125

0.016

1.50255883270908

196

2

HALLMARK_P53_PATHWAY

0.00307692307692308

0.016

1.46976609146818

193

 

ggplot(fgseaResTidy, aes(reorder(pathway, NES), NES)) +

  geom_col(aes(fill=padj<0.05)) +

  coord_flip() +

  labs(x="Pathway", y="Normalized Enrichment Score",

       title="Hallmark pathways NES from GSEA") + 

  theme_minimal()

 

fgseaRes <- fgsea(pathways=pathways.hallmark, stats=ranks, nperm=1000)

go <- fgsea(pathways=gmtPathways("C:/Users/~/Desktop/GSEA dataset/c5.all.v7.2.symbols.gmt"), ranks, nperm=1000) %>%  as_tibble()

Re <- fgsea(pathways=gmtPathways("C:/Users/~/Downloads/c2.cp.reactome.v2023.1.Hs.symbols.gmt"), ranks, nperm=1000) %>% 

   as_tibble()

go <- as.matrix(go)

write.csv(go, "go.csv")

Re <- as.matrix(Re)

write.csv(Re, "Re.csv")

hallmark <- fgsea(pathways=gmtPathways("C:/Users/~/Desktop/GSEA dataset/h.all.v7.2.symbols.gmt "), ranks, nperm=1000) %>%   as_tibble()

hallmark <- as.matrix(hallmark)

write.csv(hallmark, "hallmark Bulk PRE HPV+ R vs NR.csv")

plotEnrichment(pathways.hallmark[["HALLMARK_INTERFERON_GAMMA_RESPONSE"]],

                            ranks) + labs(title="HALLMARK_INTERFERON_GAMMA_RESPONSE")

戳这里 Claim your page
来源: 文学城-longteng24
相关阅读
Chinese Shopping Platforms Phase Out Unpopular Presale Schemes[汽车] 梅赛德斯奔驰EQE SUV 350六月雪.菖兰快、准、稳!NextSeq 1000/2000外显子组测序解决方案,开启下一个测序革新!&OtherStories大促6折!HB保健品买2赠1!Fresh/Essentials 6折起!如果特朗普入主白宮:100%美國人的意志【玉箫吹紫 】跟韵Yongshi 兄为“自由卡车运动”平反而作法律翻译 | 用户个人信息被黑客攻击,企业应否担责?奥斯尼克诉Equifax案enigmaticWest: focus on feedback friend perspectives vs. East, d[大行观点] Preqin:另类资产如何穿越充满挑战的经济周期?舊文:「『车』在古詩里的讀音」年薪$12万!精品投行Jefferies(US)已开放Equity Research Associate项目纽约豪华健身品牌 Equinox 完成18亿美元新一轮融资放过看图配诗美股基本面: 系好安全带!高盛料未来一周交易员抛美股。财报季至今无亮点,特斯拉明天赌一把?木头姐十大重仓股全部下跌!房东凯(5)淘书小记Wilmington海边的冲浪抓拍1.23.24 雪天居家仿车骑For Stressed Young Chinese, Chiikawa Toys Are Digital Ibuprofen疫情沧桑 塘西皇家记忆 (多图)用TJ Pizza Dough 做的老北京椒盐芝麻将烧饼,外酥里软,芝麻酱酱香浓郁,真好吃【小说】学霸双花 - 大学(2)老烟记事(402) 畅游宋家三姐妹两轮游日本 -我的所见,所闻和所想 琵琶湖,乡间小路和二手商店AmEx Delta SkyMiles Reserve Business 商业信用卡【年费上涨,福利更新,110k 开卡奖励】【七律】霁雪初晴 和秦兄 小园初晴
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。