03_plot.R 6.96 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}}
109
#' @param y        '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
#' @return ggplot object
#' @seealso  \code{\link{plot_karyogram}}
#' @examples 
#' # SRF sites
#'     require(magrittr)
120
#'     bsgenome <- BSgenome.Mmusculus.UCSC.mm10::BSgenome.Mmusculus.UCSC.mm10
121
#'     bedfile <-  system.file('extdata/SRF.bed',  package = 'multicrispr')
122
123
124
125
126
127
#'     targets   <- bed_to_granges(bedfile, 'mm10', plot = FALSE)
#'     plot_intervals(targets)
#'     targets %<>% extend(plot = TRUE)
#'     spacers <- find_spacers(targets, bsgenome)
#'     specific <- filter_target_specific(spacers, targets, bsgenome)
#'     efficient <- filter_efficient(spacers, targets, )
128
129
#'     
#' # PE targets
130
131
132
133
134
135
136
137
138
#'     bsgenome <- BSgenome.Hsapiens.UCSC.hg38::BSgenome.Hsapiens.UCSC.hg38  
#'     gr <- char_to_granges(c(PRNP = 'chr20:4699600:+',
#'                             HBB  = 'chr11:5227002:-',
#'                             HEXA = 'chr15:72346580-72346583:-',
#'                             CFTR = 'chr7:117559593-117559595:+'), 
#'                           bsgenome)
#'     plot_intervals(gr)
#'     plot_intervals(extend_for_pe(gr))
#'     
139
#' @export
140
plot_intervals <- function(
141
142
143
144
145
146
147
148
149
150
151
    gr, 
    xref         = 'targetname',
    y            = 'names',
    nperchrom    = 1,
    color_var    = 'targetname', 
    facet_var    = 'seqnames', 
    linetype_var = NULL, 
    size_var     = NULL, 
    alpha_var    = NULL,
    title        = NULL, 
    scales       = 'free'
152
153
){
    # Assert, Import, Comply
154
    assert_is_all_of(gr, 'GRanges')
155
    if (!is.null(color_var)) assert_is_a_string(color_var)
156
    assert_is_subset(color_var, names(as.data.table(gr)))
157
158
    contig <- .N <- .SD <- seqnames <- start <- NULL
    strand <- tmp <- width <- xstart <- xend <- . <- NULL
159

160
    # Identify contigs and order on them
161
162
163
    # gr$contig <- GenomicRanges::findOverlaps(
    #                 gr, maxgap = 30, select = 'first', ignore.strand = TRUE)
    # gr %<>% extract(order(.$contig))
164
165
    
    # Prepare plotdt
166
    plotdt <- data.table::as.data.table(gr) %>% cbind(names = names(gr))
167
    plotdt %<>% extract(order(seqnames, start))
168
169
170
171
172
    head_tail <- function(x, n=nperchrom) x [ x %in% c(head(x, n), tail(x, n)) ]
    plotdt %<>% extract(, edge := targetname %in% head_tail(unique(targetname)), by = 'seqnames')
    plotdt %<>% extract(edge==TRUE)
    #plotdt %<>% extract( , .SD[head_tail(contig)], by = c('seqnames'))
    plotdt %>%  extract(, y      := min(start), by = y)
173
    plotdt %>%  extract(, y      := factor(format(y, big.mark = " ")))
174
175
    #plotdt %>%  extract(, xstart := start-min(start), by = 'contig')
    plotdt %>%  extract(, xstart := start-min(start), by = xref)
176
177
178
179
180
181
182
    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
183
    p <-ggplot( plotdt, 
184
185
186
                aes_string(
                    x = 'xstart', xend = 'xend', y = 'y', yend = 'y', 
                    color = color_var, linetype = linetype_var, 
187
                    size = size_var, alpha = alpha_var)) + 
aditya.bhagwat's avatar
Fixes    
aditya.bhagwat committed
188
        facet_wrap(facet_var, scales = scales) + 
189
190
        geom_segment(arrow = arrow(length = unit(0.1, "inches"))) + 
        theme_bw() + 
191
192
193
        #xlab('Offset') + ylab('Start') + ggtitle(title) #+
        xlab(NULL) + ylab(NULL) + ggtitle(title) #+
    
194
    # Print and return
195
    #print(p)
196
197
    p
}