有两种方法,先看比较简单的一种:
library(CMplot)
mydata<-read.table("snp_density.csv",header=TRUE,sep=",")
head(mydata)
# snp chr pos
# snp1_1 1 2041
# snp1_2 1 2062
# snp1_3 1 2190
CMplot(mydata,plot.type="d",bin.size=1e6,col=c("darkgreen","yellow", "red"),file="jpg",memo="snp_density",dpi=300)
结果:
第二种方法就比较复杂了,需要准备两个文件:
一个是包含染色体长度的文件chr_length.txt,格式如下:
chr start end
Chr1 0 43270923
Chr2 0 35937250
Chr3 0 36413819
Chr4 0 35502694
Chr5 0 29958434
Chr6 0 31248787
Chr7 0 29697621
Chr8 0 28443022
一个是包含各个基因的起始位置的文件gene_length.txt:
chr start end
Chr1 2903 2904
Chr1 11218 11219
Chr1 12648 12649
Chr1 16292 16293
Chr1 22841 22842
Chr1 27136 27137
Chr1 29818 29819
然后画图:
source("http://bioconductor.org/biocLite.R")
biocLite("gtrellis")
library(gtrellis)
library(RColorBrewer)
library(circlize)
library(ComplexHeatmap)
bed1<-read.table("chr_length.txt",head=T,sep='\t')
bed2<-read.table("gene_length.txt",head=F,sep='\t')
gene_density = genomicDensity(bed2,window.size = 1e6)
col_fun = colorRamp2(seq(0, max(gene_density[[4]]), length = 11),rev(brewer.pal(10, "RdYlBu")))
cm = ColorMapping(col_fun = col_fun)
lgd = color_mapping_legend(cm, plot = TRUE, title = "",color_bar="continuous")
gtrellis_layout(bed1,byrow = FALSE,ncol = 1,xpadding = c(0.1, 0),
gap = unit(2, "mm"),border = FALSE,asist_ticks=FALSE,
track_axis = FALSE,legend=lgd)
add_heatmap_track(gene_density, gene_density[[4]], fill = col_fun,track=1)
add_track(track = 1, clip = FALSE, panel_fun = function(gr) {
chr = get_cell_meta_data("name")
if(chr == "Chr12") {
grid.lines(get_cell_meta_data("xlim"),
unit(c(0, 0), "npc"),
default.units = "native") }
grid.text(chr,x =0.02, y = 0.38, just = c("left", "bottom"))
})
下面这个方法其实也可以用来画拷贝数变异的密度图,只需要把start和end变成范围即可。