03_plot.R 6.44 KB
Newer Older
1

aditya.bhagwat's avatar
aditya.bhagwat committed
2
#' Karyo/Interval Plot GRanges(List)
aditya.bhagwat's avatar
aditya.bhagwat committed
3
#' @param grlist \code{\link[GenomicRanges]{GRanges-class}}
4
#' @param title plot title
aditya.bhagwat's avatar
aditya.bhagwat committed
5
#' @return list
6
#' @seealso  \code{\link{plot_intervals}}
7
8
#' @examples 
#' # Plot GRanges
9
10
11
#'     bedfile <-  system.file('extdata/SRF.bed',  package = 'multicrispr')
#'     gr <- bed_to_granges(bedfile, 'mm10', plot = FALSE)
#'     plot_karyogram(gr)
12
13
#'   
#' # Plot GRangesList
14
#'     flanks  <- up_flank(gr, stranded=FALSE)
15
16
#'     grlist <- GenomicRanges::GRangesList(sites = gr, flanks = flanks)
#'     plot_karyogram(grlist)
17
#' @export
aditya.bhagwat's avatar
aditya.bhagwat committed
18
plot_karyogram <- function(
19
    grlist, 
20
    title = unique(genome(grlist))
aditya.bhagwat's avatar
aditya.bhagwat committed
21
){
22
23
    
    # Assert
24
    . <- NULL
25
26
    if (methods::is(grlist, 'GRanges')){
        grlist <- GenomicRanges::GRangesList(grlist)
aditya.bhagwat's avatar
aditya.bhagwat committed
27
    }
28
    assert_is_all_of(grlist, 'GRangesList')
29
    
30
    # Extract relevant chromosomes and order them
31
    chroms <- union(seqlevelsInUse(grlist), standardChromosomes(grlist))
32
    stri_extract <- function(stri, pattern){
33
        stri %>% extract(stri_detect_regex(., pattern)) 
34
35
36
37
38
39
40
    }
    chrs1  <- chroms %>% stri_extract('^(chr)?[0-9]$')    %>% sort()
    chrs2  <- chroms %>% stri_extract('^(chr)?[0-9]{2}$') %>% sort()
    chrsXY <- chroms %>% stri_extract('^(chr)?[XY]$')     %>% sort()
    chrsM  <- chroms %>% stri_extract('^(chr)?MT?$')
    orderedchrs <-  c(chrs1, chrs2, chrsXY, chrsM) %>% 
                    c(sort(setdiff(chroms, .)))
41
    genomeranges <- as(seqinfo(grlist)[orderedchrs], "GRanges")
42
43

    # Color
44
    n <- length(grlist)
45
46
47
48
49
50
51
52
    if (n>0){
        hues <- seq(15, 375, length = n + 1)
        colors  <-  grDevices::hcl(h = hues, l = 65, c = 100)[seq_len(n)]
    }
    
    # Plot
    kp <- karyoploteR::plotKaryotype(genomeranges, main = title)
    for (i in seq_len(n)){
53
        karyoploteR::kpPlotRegions(kp, grlist[[i]], col = colors[i])
54
55
56
    }
    
    # Add legend
57
    if (has_names(grlist)){
58
        graphics::legend('right', fill = colors, legend = names(grlist))
59
60
61
62
63
    }

}


64
plot_tracks <- function(grlist){
65
66
67
    
    group <- . <-  NULL
    
68
    if (methods::is(grlist, 'GRangesList')) gr <- unlist(grlist)
69
70
71
72
    genome  <- unique(genome(seqinfo(gr)))
    assert_is_a_string(genome)
    chrom   <- unique(as.character(seqnames(gr)))[1]
    assert_is_a_string(chrom)
73
74
    
    # Find continuum groups
aditya.bhagwat's avatar
aditya.bhagwat committed
75
76
    gr$group <- GenomicRanges::findOverlaps(
                    gr, maxgap = 1, ignore.strand = TRUE, select = 'first')
77
78
79
80
81
82
    
    # Plot
    coretracks <- list( ideogram = Gviz::IdeogramTrack(
                                        chromosome = chrom, 
                                        genome     = genome), 
                        genomeaxis = Gviz::GenomeAxisTrack())
83
    selectedgr   <- subset(gr, group==1) %>% split(names(.))
84
    annottracks  <- mapply( Gviz::AnnotationTrack, selectedgr, name = names(gr))
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    Gviz::plotTracks(c(coretracks, annottracks), 
                    background.title = 'gray40', 
                    add = TRUE)

}


to_megabase <- function(y){
    z <- vector('character', length(y))
    
    i <- y>1e6
    z[i] <- paste0(round(y[i]*1e-6), ' M')
    
    i <- y>1e3 & y<=1e6
    z[i] <- paste0(round(y[i]*1e-3), ' K')
    
    i <- y<=1e3
    z[i] <- paste0(round(y[i]), 'b')
aditya.bhagwat's avatar
aditya.bhagwat committed
103
    z %>% set_names(names(y))
104
105
106
}


107
#' Interval plot GRanges
108
#' @param gr          \code{\link[GenomicRanges]{GRanges-class}}
aditya.bhagwat's avatar
aditya.bhagwat committed
109
#' @param yby        'contig' (default) or name of gr variable
110
#' @param color_var   'seqnames' (default) or other gr variable
aditya.bhagwat's avatar
aditya.bhagwat committed
111
112
113
114
#' @param linetype_var NULL (default) or gr variable mapped to linetype
#' @param size_var     NULL (default) or gr variable mapped to size
#' @param facet_var    NULL(default)  or gr variable mapped to facet
#' @param title        NULL or string: plot title
115
116
117
118
119
120
121
122
123
#' @return ggplot object
#' @seealso  \code{\link{plot_karyogram}}
#' @examples 
#' # SRF sites
#'     require(magrittr)
#'     bedfile <-  system.file('extdata/SRF.bed',  package = 'multicrispr')
#'     sites   <- bed_to_granges(bedfile, 'mm10', plot = FALSE)
#'     plot_intervals(sites)
#'     
124
#'     flanks  <- up_flank(sites, stranded = FALSE)
125
126
127
128
129
130
#'     sites$color <- 'sites'
#'     flanks$color <- 'flanks'
#'     plot_intervals(c(sites, flanks))
#'     
#' # PE targets
#'     bs <- BSgenome.Hsapiens.UCSC.hg38::BSgenome.Hsapiens.UCSC.hg38  
aditya.bhagwat's avatar
aditya.bhagwat committed
131
#'     sites <- GenomicRanges::GRanges(
aditya.bhagwat's avatar
Fixes    
aditya.bhagwat committed
132
133
134
135
#'                seqnames= c(PRNP = 'chr20:4699600:+',
#'                            HBB  = 'chr11:5227002:-',
#'                            HEXA = 'chr15:72346580-72346583:-',
#'                            CFTR = 'chr7:117559593-117559595:+'), 
aditya.bhagwat's avatar
aditya.bhagwat committed
136
#'                seqinfo  = BSgenome::seqinfo(bs))
137
138
#'     sites$color <- names(sites)
#'     plot_intervals(sites)
139
#' @export
140
plot_intervals <- function(
aditya.bhagwat's avatar
aditya.bhagwat committed
141
    gr, yby = 'contig', color_var = 'seqnames', linetype_var = NULL, 
aditya.bhagwat's avatar
Fixes    
aditya.bhagwat committed
142
    size_var = NULL, facet_var = 'seqnames', title = NULL, scales = 'free'
143
144
){
    # Assert, Import, Comply
145
    assert_is_all_of(gr, 'GRanges')
146
    assert_is_a_string(color_var)
147
    assert_is_subset(color_var, names(as.data.table(gr)))
aditya.bhagwat's avatar
aditya.bhagwat committed
148
    contig <- group <- .N <- .SD <- seqnames <- start <- NULL
149
    strand <- tmp <- width <- xstart <- xend <- y <- . <- NULL
150

151
152
153
154
    # Identify contigs and order on them
    gr$contig <- GenomicRanges::findOverlaps(
                    gr, maxgap = 30, select = 'first', ignore.strand = TRUE)
    gr %<>% extract(order(.$contig))
155
156
    
    # Prepare plotdt
157
    plotdt <- as.data.table(gr)
158
    head_tail <- function(x, n=1) x %in% c(head(x, n), tail(x, n))
159
    plotdt %<>% extract( , .SD[head_tail(contig)], by = c('seqnames'))
160
    plotdt %<>% extract(order(seqnames, start))
aditya.bhagwat's avatar
aditya.bhagwat committed
161
    plotdt %>%  extract(, y      := min(start), by = yby)
162
163
    plotdt %>%  extract(, y      := factor(format(y, big.mark = " ")))
    #plotdt %>%  extract(, y      := factor(round(y*1e-6)))
164
165
166
167
168
169
170
171
    plotdt %>%  extract(, xstart := start-min(start), by = 'contig')
    plotdt %>%  extract(, xend   := xstart + width)
    plotdt %>%  extract(strand=='-', tmp    := xend)
    plotdt %>%  extract(strand=='-', xend   := xstart)
    plotdt %>%  extract(strand=='-', xstart := tmp)
    plotdt %>%  extract(, tmp := NULL)
    
    # Plot
172
    p <-ggplot( plotdt, 
173
174
175
176
                aes_string(
                    x = 'xstart', xend = 'xend', y = 'y', yend = 'y', 
                    color = color_var, linetype = linetype_var, 
                    size = size_var)) + 
aditya.bhagwat's avatar
Fixes    
aditya.bhagwat committed
177
        facet_wrap(facet_var, scales = scales) + 
178
179
        geom_segment(arrow = arrow(length = unit(0.1, "inches"))) + 
        theme_bw() + 
aditya.bhagwat's avatar
Fixes    
aditya.bhagwat committed
180
        xlab('Offset') + ylab('Start') + ggtitle(title) #+
181
182
183
184
185

    # Print and return
    print(p)
    p
}