jokergoo / circlize

Circular visualization in R
http://jokergoo.github.io/circlize_book/book/
Other
959 stars 141 forks source link

no non-missing arguments to min; returning Inf when using circos.genomicTrack #394

Closed jonaskohh closed 2 months ago

jonaskohh commented 2 months ago

HI

I am trying to plot a single bacterial genome with the average coverage depth across 1000 bp window.

I am struggling to add the coverage depth histogram with the following code:

Load necessary library

library(dplyr)

Read the coverage data

coverage_data <- read.table("/Users/jonas/Desktop/circos_depth.txt", header = FALSE, col.names = c("Chromosome", "Start", "End", "Coverage"))

Define the window size

window_size <- 1000

Create a function to calculate median coverage in sliding windows

calculate_median_coverage <- function(data, window_size) { data %>% mutate(Window = floor(Start / window_size) + 1) %>% group_by(Chromosome, Window) %>% summarise(Median_Coverage = median(Coverage)) %>% ungroup() %>% mutate(Start = (Window - 1) window_size, End = Window window_size - 1) %>% select(Chromosome, Start, End, Median_Coverage) }

Calculate median coverage

median_coverage <- calculate_median_coverage(coverage_data, window_size)

Write the output to a new file

write.table(median_coverage, "circos_median_coverage.txt", quote = FALSE, sep = "\t", row.names = FALSE, col.names = FALSE)

Load the circlize package

library(circlize) circos.clear() coverage_DF <- data.frame(median_coverage[median_coverage$Chromosome==c("Chromosome"),]) coverage_DF$End <- ifelse(coverage_DF$End == 6051999, 6051261, coverage_DF$End)

coverage_DF$Start <- as.numeric(coverage_DF$Start) coverage_DF$End <- as.numeric(coverage_DF$End) coverage_DF$Median_Coverage <- as.numeric(coverage_DF$Median_Coverage)

rownames(coverage_DF) <- NULL

Define the chromosome name and size

genome_size <- 6051261

chromosome_data <- data.frame( Chromosome = "Chr1", Start = 0, End = genome_size )

Set up circlize parameters

col_text <- "grey40" circos.par("track.height" = 0.8, gap.degree = 5, cell.padding = c(0, 0, 0, 0))

Initialize the circular plot with the chromosome size

circos.initialize( factors = chromosome_data$Chromosome, xlim = matrix(c(rep(0, 1), genome_size), ncol = 2) )

Add a track to display the chromosome name

circos.track( ylim = c(0, 1), panel.fun = function(x, y) { chr = CELL_META$sector.index xlim = CELL_META$xlim ylim = CELL_META$ylim circos.text( mean(xlim), mean(ylim), chr, cex = 0.5, col = col_text, facing = "bending.inside", niceFacing = TRUE ) }, bg.col = "grey90", bg.border = FALSE, track.height = 0.06 )

brk <- c(0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6)*10^6 circos.track(track.index = get.current.track.index(), panel.fun=function(x, y) { circos.axis(h="top", major.at=brk, labels=round(brk/10^6, 1), labels.cex=0.4, col=col_text, labels.col=col_text, lwd=0.7, labels.facing="clockwise") }, bg.border=F)

Check for NA values in coverage_DF

anyNA <- apply(coverage_DF, 2, function(x) any(is.na(x)))

If there are NA values, handle them (e.g., remove or replace)

if (any(anyNA)) { coverage_DF <- coverage_DF[complete.cases(coverage_DF), ] }

Coverage

circos.genomicTrack(coverage_DF, panel.fun = function(region, value, ...) { circos.genomicLines(region, value) })

It gives me Warning messages: 1: In min(x) : no non-missing arguments to min; returning Inf 2: In max(x) : no non-missing arguments to max; returning -Inf

I have attached the coverage depth file here (https://drive.google.com/file/d/17xtSnT5EB8XVVDhLyflSkwuQ8PSrwDYI/view?usp=share_link), will you be able to let me know what did i do wrong? Thank you!