Gviz绘制基因isoform结构

The Gviz User Guide
isoform结构绘图----R包Gviz (IGV菀菀类卿)
需要从GTF文件中提取并准备数据

rm(list = ls())
suppressMessages(library(tidyverse))
#首先是我自己的数据可以忽略,总而言之,换成它需要的格式,其他的可以随意定义,包含"chromosome","start","end","width","strand","feature",   
                          "gene","exon","transcript" 等
data<-read.table("~/structure/human_nanopore_vs_v33.defined.exon.bed",sep="\t",header = T)
data$exon<-paste0(data$transcript_id,"_",data$exon_number)
data_rt<- data %>%
  dplyr::select(class_code_define,gene_name,exon,transcript_id)

gtf_data = rtracklayer::import('~/gffcompare/human_nanopore_vs_v33.annotated.gtf') %>% 
  as.data.frame() %>% 
  filter(type == 'exon') %>% 
  .[,c(1:5,10)] %>% 
  left_join(data_rt,by="transcript_id") %>% 
  dplyr::select(seqnames,start,end,width,strand,class_code_define,gene_name,exon,transcript_id) %>%
  dplyr::rename(chromosome=seqnames,feature=class_code_define,gene=gene_name,transcript=transcript_id)

write.table(gtf_data,"~/structure/human_nanopore_vs_v33.defined.exon.tsv",sep="\t",quote = F,row.names = F)

###gencode的GTF数据可以参考这个
reference = rtracklayer::import('~/Desktop/genome/gencode.v33.annotation.gtf') %>% 
  as.data.frame() %>% filter(type == 'exon') %>% .[,c(1:5, 11, 12,23,16)]
colnames(reference) = c("chromosome","start","end","width","strand","feature",   
                          "gene","exon","transcript")
write.table(reference,"~/structure/gencode.v33.annotation.exon.tsv",sep="\t",quote = F,row.names = F)

读取数据并运行,效果图如下,可以进一步美化

rm(list = ls())
suppressMessages(library(Gviz))
suppressMessages(library(GenomicRanges))
suppressMessages(library(tidyverse))
par(pin = c(6,4))
gtf_data<-read.table("~/structure/human_nanopore_vs_v33.defined.exon.tsv",sep = "\t",header = T)
reference<-read.table("~/structure/gencode.v33.annotation.exon.tsv",sep = "\t",header = T)
target<-"HMGCS1"
test<-gtf_data %>% filter(gene==target)
ref_test<-reference %>% filter(gene==target)
chr=as.character(unique(test$chromosome))
itrack <- IdeogramTrack(genome = "hg38", chromosome = chr,fontcolor="black",fontsize=12)
gtrack <- GenomeAxisTrack(fontcolor="black",fontsize=12)
grtrack <- GeneRegionTrack(test, genome = "hg38",
                           name = paste0(target," ","Nanopore"), 
                           transcriptAnnotation = 'transcript',
                           background.title = "#E64B35CC",fontsize=14,
                           )

grtrack_ref <- GeneRegionTrack(ref_test, genome = "hg38",
                           name = paste0(target," ","Reference"), 
                           transcriptAnnotation = 'transcript',
                           background.title = "#4DBBD5CC",fontsize=14)


displayPars(grtrack) <- list(background.panel = "white",fill= "#E64B3599",col = "black",fontcolor="black",lwd=1)
displayPars(grtrack_ref) <- list(background.panel = "white", fill= "#4DBBD599",col = "black",fontcolor="black",lwd=1)

plotTracks(list(itrack,gtrack,grtrack,grtrack_ref))

效果图

你可能感兴趣的:(Gviz绘制基因isoform结构)