<?xml version='1.0'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:atom="http://www.w3.org/2005/Atom" >
<channel>
	<title><![CDATA[BOL: R script for Circos plot !]]></title>
	<link>https://bioinformaticsonline.com/snippets/view/44340/r-script-for-circos-plot?</link>
	<atom:link href="https://bioinformaticsonline.com/snippets/view/44340/r-script-for-circos-plot?" rel="self" type="application/rss+xml" />
	<description><![CDATA[]]></description>
	
	<item>
	<guid isPermaLink="true">https://bioinformaticsonline.com/snippets/view/44340/r-script-for-circos-plot</guid>
	<pubDate>Tue, 11 Jul 2023 01:41:03 -0500</pubDate>
	<link>https://bioinformaticsonline.com/snippets/view/44340/r-script-for-circos-plot</link>
	<title><![CDATA[R script for Circos plot !]]></title>
	<description><![CDATA[<code>#!/usr/bin/env Rscript
library(RCircos)

# usage: Rscript make_circos.r &lt;sv table&gt; &lt;sample name&gt; &lt;gene label table&gt; &lt;cnv data&gt; &lt;out&gt;

# parse args
args = commandArgs(trailingOnly=TRUE)
sv.file &lt;- args[1]
sample.name &lt;- args[2]
gene.label.file &lt;- args[3]
cnv.file &lt;- args[4]
out.file &lt;- args[5]
# TMP &lt;- Sys.getenv(&quot;TMP_DIR&quot;) 
# tmp.bed = paste0(TMP ,&quot;/&quot; , sample.name, &quot;_bkpts.bed&quot;)
tmp.bed = paste0(sample.name, &quot;_bkpts.bed&quot;)

# load prereq data
data(UCSC.HG19.Human.CytoBandIdeogram)

# set core parameters
chr.exclude &lt;- NULL;
cyto.info &lt;- UCSC.HG19.Human.CytoBandIdeogram;
tracks.inside &lt;- 10;
tracks.outside &lt;- 5;
RCircos.Set.Core.Components(cyto.info, chr.exclude, tracks.inside, tracks.outside);
rcircos.params &lt;- RCircos.Get.Plot.Parameters();
rcircos.params$text.size &lt;- 1
RCircos.Reset.Plot.Parameters(rcircos.params)
rcircos.cyto &lt;- RCircos.Get.Plot.Ideogram();
rcircos.position &lt;- RCircos.Get.Plot.Positions();
RCircos.List.Plot.Parameters()

link.data &lt;- tryCatch(read.table(sv.file, sep = &#039;,&#039;, stringsAsFactors = F, header = T), error=function(e) data.frame())
                    
if (nrow(link.data) != 0) {
  
  link.data &lt;- transform(link.data,
                         chromStart = as.numeric(chromStart),
                         chromEnd = as.numeric(chromEnd),
                         chromStart.1 = as.numeric(chromStart.1),
                         chromEnd.1 = as.numeric(chromEnd.1))
  
  # write a bed file of all breakpoints to intersect with gene label table
  bkpts.1 &lt;- link.data[c(&quot;Chromosome&quot;, &quot;chromStart&quot;, &quot;chromEnd&quot;)]
  bkpts.2 &lt;- link.data[c(&quot;Chromosome.1&quot;, &quot;chromStart.1&quot;, &quot;chromEnd.1&quot;)]
  colnames(bkpts.2) &lt;- colnames(bkpts.1)
  write.table(rbind(bkpts.1, bkpts.2), tmp.bed, sep = &#039;\t&#039;, quote = F, col.names = F, row.names = F)
  
  # only keep labels that fall within an event
  print(paste0(&quot;bedtools intersect -wb -a &quot;, tmp.bed, &quot; -b &quot;, gene.label.file))
  gene.labels &lt;- system(paste0(&quot;bedtools intersect -wb -a &quot;, tmp.bed, &quot; -b &quot;, gene.label.file), intern = T)
  gene.labels &lt;- data.frame(do.call(&#039;rbind&#039;, strsplit(gene.labels, &#039;\t&#039;, fixed=TRUE)), stringsAsFactors = F)
  if (nrow(gene.labels) &gt; 0) {
    gene.labels &lt;- gene.labels[,4:ncol(gene.labels)]
    
    # deduplicate labels
    gene.labels &lt;- gene.labels[!duplicated(gene.labels),]
    colnames(gene.labels) &lt;- c(&quot;Chromosome&quot;, &quot;chromStart&quot;, &quot;chromEnd&quot;, &quot;gene&quot;)
    gene.labels &lt;- transform(gene.labels,
                             chromStart = as.numeric(chromStart),
                             chromEnd = as.numeric(chromEnd))
  }
  
  # make the plot
  png(file=out.file, height=3000, width=3000, res = 500)
  RCircos.Set.Plot.Area()
  RCircos.Chromosome.Ideogram.Plot()
  track.num &lt;- 2
  RCircos.Link.Plot(link.data, track.num, TRUE)
  title(sample.name, line=-1)
  
  # label the genes
  if (nrow(gene.labels) &gt; 0) {
    name.col &lt;- 4
    side &lt;- &quot;out&quot;
    track.num &lt;- 1
    RCircos.Gene.Connector.Plot(gene.labels, track.num, side);
    track.num &lt;- 2
    RCircos.Gene.Name.Plot(gene.labels, name.col, track.num, side);
  }
    
  # remove intermediate file
  system(paste0(&quot;rm -f &quot;, tmp.bed))
  
} else {
  # make empty plot
  png(file=out.file, height=3000, width=3000, res = 500)
  RCircos.Set.Plot.Area()
  RCircos.Chromosome.Ideogram.Plot()
  title(sample.name, line=-1)
}
                    
# parse cnv data
cnv = tryCatch(read.csv(cnv.file, stringsAsFactors = F), error=function(e) data.frame())
               
if (nrow(cnv) != 0) {
    colnames(cnv) &lt;- c(&quot;Chromosome&quot;, &quot;chromStart&quot;, &quot;chromEnd&quot;, &quot;cnv&quot;)
    cnv$Chromosome &lt;- paste0(&#039;chr&#039;, cnv$Chromosome)
    cnv$GeneName &lt;- &quot;gene&quot;
    cnv &lt;- cnv[, c(&quot;Chromosome&quot;, &quot;chromStart&quot;, &quot;chromEnd&quot;, &quot;GeneName&quot;, &quot;cnv&quot;)]
}
                    
# add CNV heatmap track
if (nrow(cnv) != 0) {
  RCircos.Heatmap.Plot(cnv, data.col = 5, track.num = 1, side = &quot;in&quot;)
}
                   
dev.off()

#-------- DATA FORMAT ------
chr1	11869	14412	DDX11L1
chr1	14363	29806	WASH7P
chr1	29554	31109	MIR1302-10
chr1	34554	36081	FAM138A
chr1	52473	54936	OR4G4P
chr1	62948	63887	OR4G11P
chr1	69091	70008	OR4F5
chr1	131025	134836	CICP27
chr1	134901	139379	AL627309.1
chr1	157784	157887	RNU6-1100P
chr1	227615	267253	AP006222.2
chr1	228292	228775	AP006222.1
chr1	317720	453948	RP4-669L17.10
chr1	326096	328112	RP4-669L17.8
chr1	329431	332236	CICP7
chr1	334126	334305	RP4-669L17.4
chr1	367640	368634	OR4F29
chr1	379105	379467	WBP1LP7</code>]]></description>
	<dc:creator>Abhi</dc:creator>
</item>

</channel>
</rss>