一時興起寫的一個函數(shù),備份

#從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、對染色體進行分區(qū)
  
  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、統(tǒng)計分區(qū)內(nèi)的snp數(shù)目
  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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容