# Aim: drawing of miRNAs in human chromosomes
# Author: Dr. Qinghua Cui, Department of Biomedical Informatics, PKU Health Science Center, http://cmbi.bjmu.edu.cn/cui
# Date: 05-05-2009
# Reference: Wang et al., Human microRNA oncogenes and tumor suppressors show significantly different biological patterns: from functions to targets. PLoS ONE 2010.
# The source code can be adapted to other projects (by changing files mir_gff.txt, onco_gff.txt, or supp_gff.txt) and other species (by changing the file hsa_chrom.txt).

par(bg="lightgray")# set color of the background
chrom=read.table("hsa_chrom.txt",sep="\t",header=TRUE)#read the length information of each human chromosome from file "hsa_chrom.txt"
mir=read.table("mir_gff.txt",sep="\t",header=TRUE)#read the genome coordinate data of miRNAs from file "mir_gff.txt"
oncomir=read.table("onco_gff.txt",sep="\t",header=TRUE)#read the genome coordinate data of miRNA oncogenes from file "onco_gff.txt"
mirsupp=read.table("supp_gff.txt",sep="\t",header=TRUE)#read the genome coordinate data of miRNA tumor suppressors from file "supp_gff.txt"

midpts=barplot(chrom$len,space=2,col="gray",xlim=c(0,70),ylim=c(-25000000,250000000));# draw the bar plot for chromosomes
text(x=midpts,y=-25000000,labels=c("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","X"),cex=0.8,srt=0,pos=3)
# draw the name of each chromosome
width <- diff(midpts[1:2])/4

m=dim(mir)[1]
n=dim(mir)[2]

num_base=seq(1:23)
num_base[1:23]=0# used to count the number of bases of mirs in each chromsome

heights=0;

for(i in 1:m)
{
		heights=c(heights,mir$start[i]:mir$end[i])
		#if(mir$chr[i]<=7)
		#{
		#	num_base[mir$chr[i]]=num_base[mir$chr[i]]+mir$end[i]-mir$start[i]+1;
		#}
		if(mir$chr[i]>=1 && mir$chr[i]<23)
		{
			num_base[mir$chr[i]]=num_base[mir$chr[i]]+mir$end[i]-mir$start[i]+1;
		}
		if(mir$chr[i]==23)
		{
			num_base[23]=num_base[23]+mir$end[i]-mir$start[i]+1;
		}
}

y=num_base;

length(num_base)

left <- rep(midpts, y) - width
right <- rep(midpts, y) + width

cc=sum(num_base)
qqq=heights[2:(cc+1)]
heights=qqq;

segments(left, heights, right, heights, col="yellow")# draw line segments for all miRNAs in yellow color

########################
num_oncobase=seq(1:23)
num_oncobase[1:23]=0# to count the number of bases of onco-mirs in each chromsome

m=dim(oncomir)[1]
n=dim(oncomir)[2]

heights=0;

for(i in 1:m)
{
		heights=c(heights,oncomir$start[i]:oncomir$end[i])
		num_oncobase[oncomir$chr[i]]=num_oncobase[oncomir$chr[i]]+oncomir$end[i]-oncomir$start[i]+1;
}

y=num_oncobase;

left <- rep(midpts, y) - width
right <- rep(midpts, y) + width

cc=sum(num_oncobase)
qqq=heights[2:(cc+1)]
heights=qqq;

segments(left, heights, right, heights, col="green",lwd=2)# draw line segments for miRNA oncogenes in green color
###########################

num_suppbase=seq(1:23)
num_suppbase[1:23]=0# to count the number of bases of supp-mirs in each chromsome

m=dim(mirsupp)[1]
n=dim(mirsupp)[2]

heights=0;

for(i in 1:m)
{
		heights=c(heights,mirsupp$start[i]:mirsupp$end[i])
		num_suppbase[mirsupp$chr[i]]=num_suppbase[mirsupp$chr[i]]+mirsupp$end[i]-mirsupp$start[i]+1;
}

y=num_suppbase;

left <- rep(midpts, y) - width
right <- rep(midpts, y) + width

cc=sum(num_suppbase)
qqq=heights[2:(cc+1)]
heights=qqq;

segments(left, heights, right, heights, col="red",lwd=2)# draw line segments for miRNA tumor suppressors in red color