美文网首页生物信息学R语言源码R语言 生信
一时兴起写的一个函数,备份

一时兴起写的一个函数,备份

作者: 一只烟酒僧 | 来源:发表于2021-03-31 18:03 被阅读0次
#从gene_info中的到不同指标、不同步长的染色体上的SNP分布
get_distrubution_from_geneinfo<-function(gene_info,filter_key=list("Sample1wt.index==F","Sample2wt.index==F","Sample3wt.index==F"),step=2e6,plot_dir="./"){
  require(patchwork)
  require(stringr)
  require(reshape2)
  require(ggplot2)
  require(Hmisc)
  require(patchwork)
  data("human_karyotype")
  step=step
  test<-lapply(filter_key,function(x){
    eval(parse(text = paste0("subset(gene_info,",x,")")))
  })
  test<-lapply(test,function(x){
    data.frame(SNPid=paste("SNP",1:nrow(x),sep = "_"),
               Chr=str_split(x$loc,"_",simplify = T)[,1],
               position=str_split(x$loc,"_",simplify = T)[,2])
  })
  
  #使用ggplot绘图
  #1、对染色体进行分区
  
  chrom_region<-apply(human_karyotype,1,function(x){
    a=seq(from=1,
          to=x[3],
          by=step)
    a[length(a)+1]=x[3]
    return(a)
  })
  names(chrom_region)<-paste("chr",human_karyotype$Chr,sep = "")
  #2、统计分区内的snp数目
  test<-lapply(test,function(x){
    x<-split(x,x$Chr)
    x<-x[names(chrom_region)]
  })
  
  sampleinfo_stat<-lapply(test,function(x){
    m=lapply(1:length(x),function(y){
      a=as.data.frame(table(cut(as.numeric(as.character(x[[y]]$position)),breaks = as.numeric(chrom_region[[y]]))))
      a[,1]<-chrom_region[[y]][-which(chrom_region[[y]]==max(chrom_region[[y]]))]
      
      return(a)
    })
    names(m)=names(chrom_region)
    return(m)
  })
  #3、使用ggplot可视化
  for (chr_id in names(chrom_region)) {
    
    if(T){
      SNP_samples_stat<-do.call(rbind,lapply(sampleinfo_stat,function(x){x[[chr_id]]}))
      SNP_samples_stat$group=paste("Condition",rep(c(1:length(filter_key)),each=nrow(SNP_samples_stat)/length(filter_key)),sep = "_")
      SNP_samples_stat$Var1<-factor(SNP_samples_stat$Var1,levels = unique(as.numeric(SNP_samples_stat$Var1)))
      p1<-ggplot(SNP_samples_stat,aes(x=Var1,y=Freq,group=group,color=group))+
        geom_point()+geom_line()+
        theme(axis.text.x = element_text(angle = 90))+
        labs(title=capitalize(chr_id))
      p2<-ggplot(SNP_samples_stat,aes(x=Var1,y=group))+
        geom_tile(aes(fill=Freq),color="white")+
        scale_fill_gradient(low = "white",high = "red")+
        theme(axis.text.x = element_text(angle = 90))
      p3=p1/p2
      ggsave(paste(plot_dir,chr_id,".png",sep = ""),plot =p3,width = 15,height = 10 )
    }
    
  }
  
}


get_distrubution_from_geneinfo(gene_info)
image.png

相关文章

  • 一时兴起写的一个函数,备份

  • 随写吧

    随写吧,反正也是一时兴起

  • 英文生词本

    1.recursive function 递归函数 2.andy warhol 安迪.沃霍 3.whim 一时兴起...

  • 一时兴起

    为什么很多人喜欢看电视剧? 因为里面可以看到我们所有幻想过却不曾经历过的样子。 我今天发现了一种温柔。 他们是陌生...

  • 一时兴起

    总是思考,有很多想法和感触,但从没写下来,超怀疑自己的水平,但又有什么呢,总是关注别人的目光,自己也很难成长吧。

  • 一时兴起

    这两天,每天都有超越计划的事发生。 昨天整理厨房,发现还有小半袋糯米粉。回忆了下,是因为一次去饭店吃到了红糖麻糍,...

  • 一时兴起

    突然喜欢上把家里灯关了,一个人坐在窗边看看亮灯的人家。看看人家的装修,看看有几个人在活动,想想这些家庭有...

  • 一时兴起

    今年,情人节的时候,问斌爷有何愿望,他悠悠的望着雾霾的窗外,把口罩退了下来说:如果还有的话,那一定是自驾去拉萨!算...

  • 一时兴起

    嗯,就是这样的,比起刚开始,确实没有当时那种,兴奋。对,就是兴奋。 从来没有在平常那么期待过下班,用...

  • 一时兴起

    红色连衣裙的裙摆随着节奏舞动出炫丽的痕迹。转身扬起妩媚的卷发,嘴角微微上扬,眼波流转间竟有妖精蛊惑的味道。

网友评论

    本文标题:一时兴起写的一个函数,备份

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