-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmCross2logo.R
executable file
·153 lines (135 loc) · 5.57 KB
/
mCross2logo.R
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env Rscript
motifsummary <- function(motif.mat, trim=NULL, seqformat='dna'){
motif.info <- list()
motif <- readLines(motif.mat)
motif.info$name <- strsplit(motif[grep("AC",motif)], split = "\t")[[1]][2]
motif.info$score <- as.numeric(strsplit(motif[grep("Score", motif)], split ="=")[[1]][4])
motif.info$information_content <- gsub(" ", ";", strsplit(motif[grep("IC",motif)], split = "\t")[[1]][2])
crosslinkCountsIdx <- grep("XL", motif)+1
## change 10 to a fixed variable??
crosslink_counts <- unlist(lapply(motif[(crosslinkCountsIdx):(crosslinkCountsIdx+10)], function(x) return(strsplit(x, split = "\t")[[1]][2])))
motif.info$crosslink_counts <- paste(crosslink_counts, collapse = ";")
motif.info$num_sites <- sum(as.numeric(crosslink_counts))
consensusIdx <- grep("P0", motif)+1
consensus <- unlist(lapply(motif[(consensusIdx):(consensusIdx+10)], function(x) return(strsplit(x, split = "\t")[[1]][6])))
motif.info$consensus <- paste(consensus, collapse = "")
if(!is.null(trim)){
IC <- as.numeric(strsplit(motif.info$information_content, split = ";")[[1]])
trimIdx <- which(IC > trim) - 1
}else{
trimIdx <- 0:10
}
motif.info$pcm <-sapply(motif[consensusIdx+trimIdx],function(x) as.numeric(strsplit(x, split = "\t")[[1]][2:5]))
if (seqformat == 'dna') {
rownames(motif.info$pcm) <- c("A", "C", "G", "T")
}else if(seqformat == 'rna') {
rownames(motif.info$pcm) <- c("A", "C", "G", "U")
}else {
stop("sequence format must be dna or rna")
}
colnames(motif.info$pcm) <- 1:dim(motif.info$pcm)[2]
motif.info
}
plotMotifwithCL <- function(motif.info, ...){
pcm.mat <- list()
pcm.mat[[motif.info$name]] <- motif.info$pcm
creat_pcmobj <- function(x, name){
motif <- new("pcm", mat=x, name=name)
motif
}
pfm.mat <- list()
for (i in 1:length(pcm.mat)){
pfm.mat[[names(pcm.mat)[i]]] <- creat_pcmobj(pcm.mat[[i]] , names(pcm.mat)[i])
}
pfm.mat <- lapply(pfm.mat, pcm2pfm)
#layout(matrix(c(1,1,2), nrow = 3, byrow=T))
#names(pfm.mat[[1]]) <- NULL
#par(mfrow=c(2,1))
#par(mar=c(0,5,5,5))
#title(main=motif.info$name)
#plot(pfm.mat[[1]],xaxis=F, xlab="", ylab="", yaxis=F, font="Helvetica")
#par(mar=c(5,5.2,0,5.2)) #the left and right margin are tweaked to align with the motif logo
#barplot(-as.numeric(unlist(strsplit(motif.info$crosslink_counts, split = ";")))/motif.info$num_sites, axes = F, ...)
df <- data.frame(xmin=0, ymin=0, xmax=1, ymax=2,
fontfamily="Helvetica", fontface="plain")
df$motif <- pfm.mat
gmotif <- ggplot(df, aes(xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax, motif=motif,
fontfamily=fontfamily, fontface=fontface)) +
geom_motif() + theme_bw() + ylim(0, 2) + xlim(0, 1)+theme_void()
gbar <- ggplot(data.frame(freq=-as.numeric(unlist(strsplit(motif.info$crosslink_counts, split = ";")))/motif.info$num_sites, loc=1:ncol(pfm.mat[[1]]$mat)),aes(x=loc, y=freq))+geom_bar(stat="identity", ...)+theme_void()
#grid.arrange(gmotif, gbar, nrow=2)
p <- plot_grid(gmotif, gbar, align = "hv", nrow = 2, rel_heights = c(2/3, 1/3))
return(p)
}
options(warn=-1)
suppressMessages(library(getopt, quietly=T))
suppressMessages(library(motifStack))
suppressMessages(library(ggplot2))
suppressMessages(library(gridExtra))
suppressMessages(library(cowplot))
#argument mask: 0 - no argument, 1 - required argument, 2 - optional argument
optionSpec = matrix(c(
'file', 'i', 1, "character",
'seqformat', 's', 2, "character",
'plotformat', 'p', 2, "character",
'plotfile', 'o', 1, "character",
'trim', 't', 2, "double",
'verbose', 'v', 2, "integer",
'help' , 'h', 0, "logical"
), byrow=TRUE, ncol=4)
opt = getopt(optionSpec)
##default parameters
seqformat <- "dna"
plotformat <- "pdf"
verbose <- 0
motiffile <- opt$file
plotfile <- opt$plotfile
trim <- NULL
if (!is.null(opt$seqformat)) {seqformat <- opt$seqformat}
if (!is.null(opt$plotformat)) {plotformat <- opt$plotformat}
if (!is.null(opt$trim)) {trim <- opt$trim}
if (!is.null(opt$verbose)) {verbose <- opt$verbose}
if ( !is.null(opt$help) | is.null(opt$file) | is.null(opt$plotfile))
{
cat (
'Plot motif logo with crosslinking sites number from mCross\n',
'Usage: Rscript ', get_Rscript_filename(),"[options] -i <motif mat> -o <plot name>\n",
' -i, motif file Motif mat file generated by mCross\n',
' -o, plot file name Plot file name(For example:/home/tmp/RBFOX2.pdf)\n',
'[options]\n',
' -s, sequence format Sequence format: dna/rna(default: dna)\n',
' -p, plot format Plot format: pdf/png(default: pdf)\n',
' -t, trim cutoff Trim cutoff for motif\n',
' -v, verbose Verbose mode\n',
' -h, help Print usage\n'
);
q(status=1);
}
## Set working directory as the user input directory
#setwd(dirname(motiffile))
if(verbose){
cat("Read Motif file.\n")
}
motif.info <- motifsummary(motiffile, trim = trim, seqformat = seqformat)
barplot.col <- rgb(79,129,189, maxColorValue=255)
#barplot.border <- F
barplot.space <- 0.9
if(plotformat == "pdf"){
# pdf(plotfile)
p <- plotMotifwithCL(motif.info, fill=barplot.col, width=barplot.space)
save_plot(plotfile, p)
#invisible(dev.off())
#dev.off()
}else if(plotformat == "png"){
# png(plotfile)
# plotMotifwithCL(motif.info, col=barplot.col, width=barplot.space)
# invisible(dev.off())
#dev.off()
p <- plotMotifwithCL(motif.info, fill=barplot.col, width=barplot.space)
save_plot(plotfile, p)
}else{
stop("Only pdf and png formats are supported")
}
if(verbose){
cat("Plot file generated.\n")
}