美文网首页生信绘图
2022-04-16:一天就搞了一个这

2022-04-16:一天就搞了一个这

作者: 小明的数据分析笔记本 | 来源:发表于2022-04-16 00:31 被阅读0次
library(readr)
library(ggtree)
library(treeio)
library(DESeq2)
library(ggplot2)
library(stringr)
library(tidyverse)

df_handle<-function(
  otu_table,
  treefile,
  tiplabelfile,
  pattern){
  dat01<-read_tsv(otu_table)
  tiplabgroup<-read_tsv(tiplabelfile)
  tree<-read.tree(treefile)
  new.tree<-full_join(tree,tiplabgroup,by='label')
  ggtree(new.tree)+
    #geom_tiplab(align = TRUE)+
    geom_tippoint(aes(color=GROUP))+
    #xlim(NA,1.5)+
    theme(legend.position = "none")-> p1
  
  group.info.01<-data.frame(
    row.names = dat01 %>% select(matches(pattern)) %>% 
      colnames(),
    conditions = c(rep("trt",5),
                   rep("ctl",5))
  )
  group.info.01$conditions<-factor(group.info.01$conditions)
  dat01 %>% 
    select(matches(paste(pattern,"OTU ID",sep = "|"))) %>% 
    column_to_rownames('OTU ID') -> dat01.1
  dds <- DESeq2::DESeqDataSetFromMatrix(countData = dat01.1,
                                        colData=group.info.01,
                                        design = ~ conditions)
  dds_res <- DESeq2::DESeq(dds, sfType = "poscounts")
  
  res <- results(dds_res, 
                 tidy=T, 
                 format="DataFrame",
                 contrast = c("conditions","trt","ctl"))
  res %>% 
    mutate(str_extract(row,"f__\\S+;") %>% 
             str_split_fixed(pattern = ";",n=10) %>% 
             as.data.frame() %>% 
             select(1)) -> res.01
  
  
  res.01[which(res.01$V1%in%tree$tip.label),] %>% 
    filter(!is.na(log2FoldChange)) %>% 
    group_by(V1) %>% 
    summarise(log2FoldChange=mean(log2FoldChange)) -> res.02
  merge(res.02,tiplabgroup,by.x='V1',by.y="label") -> res.02
  res.02$V1<-factor(res.02$V1,
                    levels = p1$data %>% na.omit() %>% arrange(y) %>% pull(label))
  dat01.1 %>% rownames_to_column(var = "family") %>% 
    mutate(str_extract(family,"f__\\S+;") %>% 
             str_split_fixed(pattern = ";",n=10) %>% 
             as.data.frame() %>% 
             select(1)) -> dat01.2
  dat01.1 %>% rownames_to_column(var = "family") %>% 
    mutate(str_extract(family,"f__\\S+;") %>% 
             str_split_fixed(pattern = ";",n=10) %>% 
             as.data.frame() %>% 
             select(1)) %>% 
    merge(tiplabgroup,by.x="V1",by.y='label') %>% 
    select(-c('family','GROUP')) %>% 
    group_by(V1) %>% 
    summarise_all(mean) %>% 
    mutate(mrd=rowSums(.[2:11])/10) %>% 
    select(V1,mrd) -> dat01.2
  res.02$V1<-str_replace(res.02$V1,"f__","")
  res.02$GROUP<-str_replace(res.02$GROUP,"c__","")
  dat01.2$V1<-str_replace(dat01.2$V1,"f__","")
  new.tree@phylo$tip.label<-
    str_replace(new.tree@phylo$tip.label,
                "f__","")
  new.tree@data$GROUP<-
    str_replace(new.tree@data$GROUP,
                "c__","")
  return(list(bubble.1 = res.02,
              bubble.2 = dat01.2,
              tree.1 = new.tree))
}




bubble_figure<-function(dat){
  ggplot(data=dat,
         aes(x=log2FoldChange,y=V1))+
    geom_vline(xintercept = 0,lty="dashed",
               color="grey",size=1)+
    geom_point(aes(size=log2FoldChange,
                   fill=GROUP),
               shape=21)+
    guides(size='none',
           fill=guide_legend(ncol=6))+
    scale_fill_discrete(name=NULL)+
    #ggtitle("F_0_20VSW_0_20")+
    theme_minimal()+
    theme(legend.position = "bottom",
          legend.direction = "vertical",
          legend.justification = c(0,0),
          axis.ticks.y = element_blank(),
          axis.text.y = element_blank(),
          panel.grid = element_blank(),
          axis.line.x = element_line(),
          axis.ticks.x = element_line())+
    labs(y=NULL) -> p
  return(p)
}
#bubble_figure(plot_data[["bubble.1"]])

dat01<-read_tsv("data/20220414/data/otu_taxon_otu.full.xls")
dat01 %>% colnames()
dat01 %>% select(matches("[FW]\\d_0_20")) %>% colnames()
dat01 %>% select(matches("[FW]\\d_20_40")) %>% colnames()
dat01 %>% select(matches("[FW]\\d_40_60")) %>% colnames()

dat01 %>% select(matches("[HW]\\d_0_20")) %>% colnames()
dat01 %>% select(matches("[HW]\\d_20_40")) %>% colnames()
dat01 %>% select(matches("[HW]\\d_40_60")) %>% colnames()

dat01 %>% select(matches("[MW]\\d_0_20")) %>% colnames()
dat01 %>% select(matches("[MW]\\d_20_40")) %>% colnames()
dat01 %>% select(matches("[MW]\\d_40_60")) %>% colnames()

df_handle(otu_table = "data/20220414/data/otu_taxon_otu.full.xls",
          treefile = "data/20220414/data/phylo_tree .tre",
          tiplabelfile = "data/20220414/data/species_group .xls",
          pattern = "[FW]\\d_0_20") -> plot_data

trt_ctl<-c("[FW]\\d_0_20","[FW]\\d_20_40","[FW]\\d_40_60",
           "[HW]\\d_0_20","[HW]\\d_20_40","[HW]\\d_40_60",
           "[MW]\\d_0_20","[MW]\\d_20_40","[MW]\\d_40_60")

plot_list = list()
for (i in 1:9){
  df_handle(otu_table = "data/20220414/data/otu_taxon_otu.full.xls",
            treefile = "data/20220414/data/phylo_tree .tre",
            tiplabelfile = "data/20220414/data/species_group .xls",
            pattern = trt_ctl[i]) -> plot_data
  #print(plot_data)
  #print(bubble_figure(plot_data[["bubble.1"]]))
  bubble_figure(plot_data[["bubble.1"]]) -> plot_list[[i]]
}

p1<-ggtree(plot_data[["tree.1"]])+
  geom_tiplab(align = T,offset = 0.01)+
  geom_tippoint(aes(color=GROUP))+
  xlim(NA,2)+
  theme(legend.position = "none")
p1
p1+plot_spacer()+
  (plot_list[[1]]+
  plot_list[[2]]+
  plot_list[[3]]+
  plot_list[[4]]+
  plot_list[[5]]+
  plot_list[[6]]+
  plot_list[[7]]+
  plot_list[[8]]+
  plot_list[[9]]+
  plot_layout(guides = "collect",
              nrow=1)&theme(legend.position = "bottom"))+
  plot_layout(widths = c(1,1,9))
image.png

相关文章

  • 2022-04-16:一天就搞了一个这

  • 我怎么就搞了前端?

    想开始下一件事情前,我必须对之前的做一下总结,养成一个习惯。 我是一个半路出家的所谓前端。大学毕业时,其实我是想做...

  • 2018-07-27

    盘发最难画,搞了一晚上,就这效果。师夷长技以制夷~

  • 深夜回家的感觉

    深夜回家是一种什么体验? 今天又是11:30才回家,还去楼下打了个水,回来的路上想了想,唉,这搞了一天,究竟搞了些...

  • 橙子的ScalersTalk第六轮新概念朗读持续力训练Day 1

    练习材料:[Day 2760 2022-04-16] L43-2: Fully insured Shortlyaf...

  • 没有文字陪伴的日子是空虚的

    2022-04-16 渐渐发现,如果人不写写字,哪怕是在电脑上敲敲文字,就觉得生活没什么意思了。 现在的人过得都很...

  • 我在他乡挺好的

    原创 茹雪餐风 茹雪潇馨 2022-04-16 20:36 今天是社区临时管控第一天,凌晨被通知,全民居家。出门核...

  • 0166觉察日记:我无所事事的一天

    2022-04-16 北京 天晴 今日周六看起来是无所事事的一天 接近6点自然醒了,生物钟,眼皮明明很困却下意识醒...

  • 2022-08-10

    今天一上午仅搞了一餐中饭,然后是吃饭。 一上午便过了,这日子怎么过得这么快。 吃完晚餐,这一天就过了。 一天一天这...

  • 期末考试倒计时

    今天是上课的最后一周,下一周就随堂考试了,基本上一周这10多节课,今天一天就搞了8节课下来,非常的疲倦,但是总体来...

网友评论

    本文标题:2022-04-16:一天就搞了一个这

    本文链接:https://www.haomeiwen.com/subject/bvscertx.html