File: GRangePairs-methods.R

package info (click to toggle)
r-bioc-cner 1.26.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 18,216 kB
  • sloc: ansic: 23,458; makefile: 6
file content (104 lines) | stat: -rw-r--r-- 4,669 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
setGeneric("syntenicDotplot", 
           function(x, firstSeqlengths=NULL, secondSeqlengths=NULL,
                    firstChrs=NULL, secondChrs=NULL,
                    col=c("blue", "red"), type=c("line", "dot"))
  standardGeneric("syntenicDotplot"))

setMethod("syntenicDotplot", signature=(x="GRangePairs"),
          function(x, firstSeqlengths=NULL, secondSeqlengths=NULL,
                   firstChrs=NULL, secondChrs=NULL,
                   col=c("blue", "red"), type=c("line", "dot")){
            syntenicPlotGRangePairs(x, firstSeqlengths=firstSeqlengths, 
                                    secondSeqlengths=secondSeqlengths,
                                    firstChrs=firstChrs,
                                    secondChrs=secondChrs,
                                    col=col, type=type)
          })

syntenicPlotGRangePairs <- function(x, firstSeqlengths=NULL, 
                                    secondSeqlengths=NULL,
                                    firstChrs=NULL, secondChrs=NULL,
                                    col=c("blue", "red"),
                                    type=c("line", "dot")){
  type <- match.arg(type)
  
  if(is.null(firstSeqlengths)){
    targetSeqlengths <- seqlengths(first(x))
    if(any(is.na(targetSeqlengths))){
      stop("When firstSeqlengths is NULL, the seqlengths must exist in x!")
    }
  }else{
    targetSeqlengths <- firstSeqlengths
  }
  
  if(is.null(secondSeqlengths)){
    querySeqlengths <- seqlengths(second(x))
    if(any(is.na(querySeqlengths))){
      stop("When secondSeqlengths is NULL, the seqlengths must exist in x!")
    }
  }else{
    querySeqlengths <- secondSeqlengths
  }
  
  if(!is.null(firstChrs)){
    x <- x[seqnames(first(x)) %in% firstChrs]
    targetSeqlengths <- targetSeqlengths[firstChrs]
  }
  if(!is.null(secondChrs)){
    x <- x[seqnames(second(x)) %in% secondChrs]
    querySeqlengths <- querySeqlengths[secondChrs]
  }
  target <- first(x)
  query <- second(x)
  
  ## If we want to put all the segments of syntent in one plot
  ## We need to shift the coordiantes from second chromosomes in seqlengths
  shiftCoordinatesTarget <- c(0, cumsum(as.numeric(targetSeqlengths))[-length(targetSeqlengths)])
  names(shiftCoordinatesTarget) <- names(targetSeqlengths)
  shiftCoordinatesQuery <- c(0, cumsum(as.numeric(querySeqlengths))[-length(querySeqlengths)])
  names(shiftCoordinatesQuery) <- names(querySeqlengths)
  
  startTarget <- start(target) +
    shiftCoordinatesTarget[as.character(seqnames(target))]
  endTarget <- end(target) + 
    shiftCoordinatesTarget[as.character(seqnames(target))]
  startQuery <- start(query) +
    shiftCoordinatesQuery[as.character(seqnames(query))]
  endQuery <- end(query) +
    shiftCoordinatesQuery[as.character(seqnames(query))]
  combinedStrands <- rep("+", length(target))
  combinedStrands[xor(as.character(strand(target))=="+", 
                      as.character(strand(query))=="+")] <- "-"
  
  toPlot <- data.frame(x=startTarget, xEnd=endTarget,
                       y=startQuery, yEnd=endQuery,
                       strand=factor(combinedStrands, levels=c("+", "-")))
  if(type == "line"){
    p <- ggplot(data=toPlot, aes_string(x="x",y="y", xend="xEnd", yend="yEnd")) +
      geom_segment(aes(colour=strand)) + theme_bw() +
      scale_colour_manual(values=col) +
      xlab("First") + ylab("Second") + ggtitle("Syntenic dotplot") +
      scale_x_continuous(breaks=c(0,
                                  cumsum(as.numeric(targetSeqlengths))),
                         limits=c(0, sum(as.numeric(targetSeqlengths))),
                         labels=c("start", names(targetSeqlengths))) +
      scale_y_continuous(breaks=c(0,
                                  cumsum(as.numeric(querySeqlengths))),
                         limits=c(0, sum(as.numeric(querySeqlengths))),
                         labels=c("start", names(querySeqlengths)))
  }else if(type == "dot"){
    p <- ggplot(data=toPlot, aes_string(x="x",y="y")) +
      geom_point(aes(colour=strand), size=0.5) + theme_bw() +
      scale_colour_manual(values=col) +
      xlab("First") + ylab("Second") + ggtitle("Syntenic dotplot") +
      scale_x_continuous(breaks=c(0,
                                  cumsum(as.numeric(targetSeqlengths))),
                         limits=c(0, sum(as.numeric(targetSeqlengths))),
                         labels=c("start", names(targetSeqlengths))) +
      scale_y_continuous(breaks=c(0,
                                  cumsum(as.numeric(querySeqlengths))),
                         limits=c(0, sum(as.numeric(querySeqlengths))),
                         labels=c("start", names(querySeqlengths)))
  }
  p
}