用for循环解决冗余的代码
2021-12-17 本文已影响0人
宗肃書
涉及个人课题,仅贴出图片一部分,如果有人需要源代码,可以找我要(耗费了一天的脑细胞到凌晨1点40,包括输入文件的制作等细节)
image.png
为了画这个图,刚开始写了接近400行代码来生成这个图
如下
image.png
都说偷懒是第一劳动力,我为了节省体力,写了一个for循环,仅用了30行就可以生成这个图。还是动脑子的效率更快。。。。。。。。
#设置背景染色体文件的时候,如果作图是按照窗口的话,需要把end坐标除以窗口的大小
setwd("D:/桌面/毕业课题/结果/477只鸡/Autosomes/pure-selectsignal/ihs-CLR并集/ALLGROUP")
library(OmicCircos)
library(ggplot2)
seg.name = c(1:28,30:33)
seg.f=read.table(file="../chicken_chr_region.txt",header=T,sep="\t")
db= segAnglePo ( seg.f , seg=seg.name )
ihsBL=read.table(file="BL_window.ihs.txt",header=T,sep="\t")
for( i in (1:28,30:33) ){
ihsBL.i=subset(ihsBL,chr==i)}
clrBL=read.csv(file="BL.CLR.csv",header=T)
Position=1
Likelihood=max(clrBL$Likelihood)
clrBL1=subset(clrBL,chr==1)
chr=1
chr1=data.frame(chr,Position,Likelihood)
clrBL1=rbind(chr1,clrBL1)
clrBL2=subset(clrBL,chr==2)
chr=2
chr2=data.frame(chr,Position,Likelihood)
clrBL2=rbind(chr2,clrBL2)
clrBL3=subset(clrBL,chr==3)
chr=3
chr3=data.frame(chr,Position,Likelihood)
clrBL3=rbind(chr3,clrBL3)
clrBL4=subset(clrBL,chr==4)
chr=4
chr4=data.frame(chr,Position,Likelihood)
clrBL4=rbind(chr4,clrBL4)
clrBL5=subset(clrBL,chr==5)
chr=5
chr5=data.frame(chr,Position,Likelihood)
clrBL5=rbind(chr5,clrBL5)
clrBL6=subset(clrBL,chr==6)
chr=6
chr6=data.frame(chr,Position,Likelihood)
clrBL6=rbind(chr6,clrBL6)
clrBL7=subset(clrBL,chr==7)
chr=7
chr7=data.frame(chr,Position,Likelihood)
clrBL7=rbind(chr7,clrBL7)
clrBL8=subset(clrBL,chr==8)
chr=8
chr8=data.frame(chr,Position,Likelihood)
clrBL8=rbind(chr8,clrBL8)
clrBL9=subset(clrBL,chr==9)
chr=9
chr9=data.frame(chr,Position,Likelihood)
clrBL9=rbind(chr9,clrBL9)
clrBL10=subset(clrBL,chr==10)
chr=10
chr10=data.frame(chr,Position,Likelihood)
clrBL10=rbind(chr10,clrBL10)
clrBL11=subset(clrBL,chr==11)
chr=11
chr11=data.frame(chr,Position,Likelihood)
clrBL11=rbind(chr11,clrBL11)
clrBL12=subset(clrBL,chr==12)
chr=12
chr12=data.frame(chr,Position,Likelihood)
clrBL12=rbind(chr12,clrBL12)
clrBL13=subset(clrBL,chr==13)
chr=13
chr13=data.frame(chr,Position,Likelihood)
clrBL13=rbind(chr13,clrBL13)
clrBL14=subset(clrBL,chr==14)
chr=14
chr14=data.frame(chr,Position,Likelihood)
clrBL14=rbind(chr14,clrBL14)
clrBL15=subset(clrBL,chr==15)
chr=15
chr15=data.frame(chr,Position,Likelihood)
clrBL15=rbind(chr15,clrBL15)
clrBL16=subset(clrBL,chr==16)
chr=16
chr16=data.frame(chr,Position,Likelihood)
clrBL16=rbind(chr16,clrBL16)
clrBL17=subset(clrBL,chr==17)
chr=17
chr17=data.frame(chr,Position,Likelihood)
clrBL17=rbind(chr17,clrBL17)
clrBL18=subset(clrBL,chr==18)
chr=18
chr18=data.frame(chr,Position,Likelihood)
clrBL18=rbind(chr18,clrBL18)
clrBL19=subset(clrBL,chr==19)
chr=19
chr19=data.frame(chr,Position,Likelihood)
clrBL19=rbind(chr19,clrBL19)
clrBL20=subset(clrBL,chr==20)
chr=20
chr20=data.frame(chr,Position,Likelihood)
clrBL20=rbind(chr20,clrBL20)
clrBL21=subset(clrBL,chr==21)
chr=21
chr21=data.frame(chr,Position,Likelihood)
clrBL21=rbind(chr21,clrBL21)
clrBL22=subset(clrBL,chr==22)
chr=22
chr22=data.frame(chr,Position,Likelihood)
clrBL22=rbind(chr22,clrBL22)
clrBL23=subset(clrBL,chr==23)
chr=23
chr23=data.frame(chr,Position,Likelihood)
clrBL23=rbind(chr23,clrBL23)
clrBL24=subset(clrBL,chr==24)
chr=24
chr24=data.frame(chr,Position,Likelihood)
clrBL24=rbind(chr24,clrBL24)
clrBL25=subset(clrBL,chr==25)
chr=25
chr25=data.frame(chr,Position,Likelihood)
clrBL25=rbind(chr25,clrBL25)
clrBL26=subset(clrBL,chr==26)
chr=26
chr26=data.frame(chr,Position,Likelihood)
clrBL26=rbind(chr26,clrBL26)
clrBL27=subset(clrBL,chr==27)
chr=27
chr27=data.frame(chr,Position,Likelihood)
clrBL27=rbind(chr27,clrBL27)
clrBL28=subset(clrBL,chr==28)
chr=28
chr28=data.frame(chr,Position,Likelihood)
clrBL28=rbind(chr28,clrBL28)
clrBL30=subset(clrBL,chr==30)
chr=30
chr30=data.frame(chr,Position,Likelihood)
clrBL30=rbind(chr30,clrBL30)
clrBL31=subset(clrBL,chr==31)
chr=31
chr31=data.frame(chr,Position,Likelihood)
clrBL31=rbind(chr31,clrBL31)
clrBL32=subset(clrBL,chr==32)
chr=32
chr32=data.frame(chr,Position,Likelihood)
clrBL32=rbind(chr32,clrBL32)
clrBL33=subset(clrBL,chr==33)
chr=33
chr33=data.frame(chr,Position,Likelihood)
clrBL33=rbind(chr33,clrBL33)
tiff("BL.tiff",height=800,width=800)
par(mar=c(0,0,0,0))
plot(c(1,800),c(1,800),type="n",axes=F,xlab="",ylab="",main="")
circos (R=340 , type="chr" , cir=db , col="blue", print.chr.lab=TRUE, W=40, scale=F)
circos(R=250, cir=db, W=80, mapping=ihsBL, col.v=3,col=rainbow(32), type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL1, col.v=3,col="#ff353c", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL2, col.v=3,col="#60df56", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL3, col.v=3,col="#eebe4c", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL4, col.v=3,col="#a910a8", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL5, col.v=3,col="#2a0d8d", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL6, col.v=3,col="#ff0ba4", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL7, col.v=3,col="#ff353c", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL8, col.v=3,col="#60df56", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL9, col.v=3,col="#eebe4c", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL10, col.v=3,col="#a910a8", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL11, col.v=3,col="#2a0d8d", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL12, col.v=3,col="#ff0ba4", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL13, col.v=3,col="#ff353c", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL14, col.v=3,col="#60df56", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL15, col.v=3,col="#eebe4c", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL16, col.v=3,col="#a910a8", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL17, col.v=3,col="#2a0d8d", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL18, col.v=3,col="#ff0ba4", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL19, col.v=3,col="#ff353c", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL20, col.v=3,col="#60df56", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL21, col.v=3,col="#eebe4c", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL22, col.v=3,col="#a910a8", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL23, col.v=3,col="#2a0d8d", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL24, col.v=3,col="#ff0ba4", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL25, col.v=3,col="#ff353c", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL26, col.v=3,col="#60df56", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL27, col.v=3,col="#eebe4c", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL28, col.v=3,col="#a910a8", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL30, col.v=3,col="#2a0d8d", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL31, col.v=3,col="#ff0ba4", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL32, col.v=3,col="#ff353c", type="s", cex =0.05,B = F,scale=F)
circos(R=160, cir=db, W=90, mapping=clrBL33, col.v=3,col="#60df56", type="s", cex =0.05,B = F,scale=F)
dev.off()
- 精简版
setwd("D:/桌面/毕业课题/结果/477只鸡/Autosomes/pure-selectsignal/ihs-CLR并集/ALLGROUP")
library(OmicCircos)
library(ggplot2)
seg.name = c(1:22,25:30,32:35)
seg.f=read.table(file="../chicken_chr_region.txt",header=T,sep="\t")
db= segAnglePo (seg.f,seg=seg.name)
ihsBL=read.table(file="BL_window.ihs.txt",header=T,sep="\t")
clrBL=read.csv(file="BL.CLR.csv",header=T)
color=c("#ff353c","#60df56","#eebe4c","#a910a8","#2a0d8d","#ff0ba4","#ff353c","#60df56","#eebe4c","#a910a8","#2a0d8d","#ff0ba4","#ff353c","#60df56","#eebe4c","#a910a8","#2a0d8d","#ff0ba4","#ff353c","#60df56","#eebe4c","#a910a8","white","white","#2a0d8d","#ff0ba4","#ff353c","#60df56","#eebe4c","#a910a8","white","#2a0d8d","#ff0ba4","#ff353c","#60df56")
tiff("BL.tiff",height=1300,width=1300)
par(mar=c(0,0,0,0))
plot(c(1,800),c(1,800),type="n",axes=F,xlab="",ylab="",main="")
circos (R=380 , type="chr" , cir=db , col="blue", print.chr.lab=F, W=60, scale=F)
for( i in c(1:22,25:30,32:35) ){
ihsBL.i=subset(ihsBL,CHR==i)
POS=1
MEAN_MRK=max(ihsBL$MEAN_MRK) #把最大值加在每个染色体的起始位置是为了保证整个环形图的尺度一样
CHR=i
chr1=data.frame(CHR,POS,MEAN_MRK)
ihsBL.t=rbind(chr1,ihsBL.i) #用t的意思是每一次都生成一个新的数据框,如果用i会累加数据框
circos(R=280, cir=db, W=85, mapping=ihsBL.t, col.v=3,col=color[i], type="s", cex =0.05,B = F,scale=F)
clrBL.i=subset(clrBL,chr==i)
Position=1
Likelihood=max(clrBL$Likelihood)
chr=i
chr1=data.frame(chr,Position,Likelihood)
clrBL.t=rbind(chr1,clrBL.i)
circos(R=180, cir=db, W=110, mapping=clrBL.t, col.v=3,col=color[i], type="s", cex =0.05,B = F,scale=F)
}
dev.off()