manhattanly icon indicating copy to clipboard operation
manhattanly copied to clipboard

Colour SNP points according to annotation2 rather than chromosome?

Open sophiegresh opened this issue 6 years ago • 4 comments

Hi Sahir,

First of all thanks for creating such a fantastic package! I've been trying and failing to edit some of the code in the package for a while now in order to recolour the SNP points according to another variable rather than chromosome number in the manhattan plots, and I was wondering whether you could help? I want to colour the SNPs according to geographical region (of which I have two) as I have multiple populations plotted on the same manhattan plot. When transforming my dataset into the manhattanr class I saved this region variable as the annotation2 so each SNP has either the region "Africa" or "MiddleEast" in the annotation2 column. So is there a way I can colour points according to annotation2 rather than chromosome?

Many Thanks, Sophie

sophiegresh avatar Jul 19 '17 16:07 sophiegresh

Thanks for your interest in this package. Several people have asked for this feature. I will try to implement it in a function.

for the time being, here is some code that should get you started:

library(manhattanly)
library(magrittr)

DT <- subset(HapMap, CHR %in% 1:3)
DT$region <- sample(c("Africa", "MiddleEast"), size = nrow(DT), replace = TRUE)

x <- manhattanr(DT, snp = "SNP", gene = "GENE", annotation1 = "region")

d <- x$data
pName <- x$pName
snpName <- x$snpName
geneName <- x$geneName
annotation1Name <- x$annotation1Name
annotation2Name <- x$annotation2Name
labs <- x$labs
xlabel <- x$xlabel
ticks <- x$ticks
nchr <- x$nchr
xmax = ceiling(max(d$pos) * 1.03)
xmin = floor(max(d$pos) * -0.03)

p <- plotly::plot_ly()
p %<>% plotly::layout(p, title = "Title", 
                        xaxis = list(title = "Chromosome", 
                        showgrid = FALSE, range = c(xmin, xmax), autotick = FALSE, 
                        tickmode = "array", tickvals = ticks, ticktext = labs, 
                        ticks = "outside"), yaxis = list(title = "-log10(p)"))

icol <- 1
for (i in unique(d$index)) {
  tmp <- d[d$index == unique(d$index)[i], ]
  TEXT <- paste(if (!is.na(snpName)) 
    paste0(snpName, ": ", tmp[[snpName]]), if (!is.na(geneName)) 
      paste0(geneName, ": ", tmp[[geneName]]), if (!is.na(annotation1Name)) 
        paste0(annotation1Name, ": ", tmp[[annotation1Name]]), 
    if (!is.na(annotation2Name)) 
      paste0(annotation2Name, ": ", tmp[[annotation2Name]]), 
    sep = "<br>")
  
  chromo <- unique(tmp[which(tmp$index == i), "CHR"])
  
  p %<>% add_markers(x = tmp$pos, y = tmp$logp, color = tmp$region, 
                       showlegend = F, text = TEXT, name = paste0("chr", chromo))
  
  icol = icol + 1
}
p

See the resulting plot here:

https://plot.ly/~sahirbhatnagar/146/title/

sahirbhatnagar avatar Jul 19 '17 19:07 sahirbhatnagar

Thanks so much Sahir, I've managed to integrate that into the function code that I have and it's working beautifully!

sophiegresh avatar Jul 19 '17 20:07 sophiegresh

Hi Sahir,

Thanks for creating such a useful package. On a similar note, is it possible to highlight snps/genes in more than just one color? It seems like the current function only accepts a single color. It would be great if the function could be extended to highlight1, highlight2, as you did with annotate.

Thanks again for the contribution!

ghost avatar May 10 '18 18:05 ghost

Hi Sahir, I would vote for highlighting in multiple colors as well

vdejager avatar Sep 24 '20 13:09 vdejager