annotate region_motif_compare.r @ 3:cab2db9d058b draft

Uploaded
author jeremyjliu
date Sat, 16 May 2015 22:35:26 -0400
parents
children 4803f5186f1a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
1 # Name: region_motif_compare.r
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
2 # Description: Reads in two count files and determines enriched and depleted
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
3 # motifs (or any location based feature) based on poisson tests and gc
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
4 # corrections. All enrichment ratios relative to overall count / gc ratios.
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
5 # Author: Jeremy liu
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
6 # Email: jeremy.liu@yale.edu
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
7 # Date: 15/02/11
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
8 # Note: This script can be invoked with the following command
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
9 # R --slave --vanilla -f ./region_motif_compare.r --args <workingdir> <pwm_file>
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
10 # <intab1> <intab2> <enriched_tab> <depleted_tab> <plots_png>
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
11 # <workingdir> is the directory where plotting.r is saved
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
12 # Dependencies: region_motif_data_manager, plotting.r,
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
13
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
14 # Auxiliary function to concatenate multiple strings
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
15 concat <- function(...) {
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
16 input_list <- list(...)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
17 return(paste(input_list, sep="", collapse=""))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
18 }
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
19
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
20 # Supress all warning messages to prevent Galaxy treating warnings as errors
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
21 options(warn=-1)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
22
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
23 # Set common and data directories
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
24 args <- commandArgs()
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
25 workingDir = args[7]
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
26 pwmFile = unlist(strsplit(args[8], ','))[1] # If duplicate entires, take first one
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
27
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
28 # Set input and reference files
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
29 inTab1 = args[9]
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
30 inTab2 = args[10]
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
31 enrichTab = args[11]
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
32 depleteTab = args[12]
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
33 plotsPng = args[13]
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
34
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
35 # Load dependencies
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
36 source(concat(workingDir, "/plotting.r"))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
37
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
38 # Auxiliary function to read in tab file and prepare the data
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
39 read_tsv <- function(file) {
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
40 data = read.table(file, sep="\t", stringsAsFactors=FALSE)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
41 names(data)[names(data) == "V1"] = "motif"
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
42 names(data)[names(data) == "V2"] = "counts"
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
43 return(data)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
44 }
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
45
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
46 startTime = Sys.time()
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
47 cat("Running ... Started at:", format(startTime, "%a %b %d %X %Y"), "...\n")
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
48
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
49 # Loading motif position weight matrix (pwm) file
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
50 cat("Loading motif postion weight matrices...\n")
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
51 lines = scan(pwmFile, what="character", sep="\n", quiet=TRUE)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
52 indices = which(grepl("MOTIF", lines))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
53 names(indices) = lapply(indices, function(i) {
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
54 nameline = lines[i]
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
55 name = substr(nameline, 7, nchar(nameline))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
56 })
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
57
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
58 pwms = sapply(indices, function(i) {
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
59 infoline = unlist(strsplit(lines[i+1], " "))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
60 alength = as.numeric(infoline[4])
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
61 width = as.numeric(infoline[6])
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
62 subset = lines[(i+2):(i+2+width-1)]
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
63 motiflines = strsplit(subset, " ")
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
64 motif = t(do.call(rbind, motiflines))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
65 motif = apply(motif, 2, as.numeric)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
66 }, simplify=FALSE, USE.NAMES=TRUE)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
67
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
68 # Loading input tab files
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
69 cat("Loading and reading input region motif count files...\n")
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
70 region1DF = read_tsv(inTab1)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
71 region2DF = read_tsv(inTab2)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
72 region1Counts = region1DF$counts
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
73 region2Counts = region2DF$counts
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
74 names(region1Counts) = region1DF$motif
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
75 names(region2Counts) = region2DF$motif
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
76
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
77 # Processing count vectors to account for missing 0 count motifs, then sorting
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
78 cat("Performing 0 count correction and sorting...\n")
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
79 allNames = union(names(region1Counts), names(region2Counts))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
80 region1Diff = setdiff(allNames, names(region1Counts))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
81 region2Diff = setdiff(allNames, names(region2Counts))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
82 addCounts1 = rep(0, length(region1Diff))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
83 addCounts2 = rep(0, length(region2Diff))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
84 names(addCounts1) = region1Diff
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
85 names(addCounts2) = region2Diff
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
86 newCounts1 = append(region1Counts, addCounts1)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
87 newCounts2 = append(region2Counts, addCounts2)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
88 region1Counts = newCounts1[sort.int(names(newCounts1), index.return=TRUE)$ix]
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
89 region2Counts = newCounts2[sort.int(names(newCounts2), index.return=TRUE)$ix]
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
90
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
91 # Generate gc content matrix
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
92 gc = sapply(pwms, function(i) mean(i[2:3,3:18]))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
93
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
94 # Apply poisson test, calculate p and q values, and filter significant results
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
95 cat("Applying poisson test...\n")
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
96 rValue = sum(region2Counts) / sum(region1Counts)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
97 pValue = sapply(seq(along=region1Counts), function(i) {
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
98 poisson.test(c(region1Counts[i], region2Counts[i]), r=1/rValue)$p.value
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
99 })
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
100 qValue = p.adjust(pValue, "fdr")
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
101 indices = which(qValue<0.1 & abs(log2(region1Counts/region2Counts/rValue))>log2(1.5))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
102
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
103 # Setting up output diagnostic plots, 4 in 1 png image
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
104 png(plotsPng, width=800, height=800)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
105 xlab = "region1_count"
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
106 ylab = "region2_count"
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
107 lim = c(0.5, 5000)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
108 layout(matrix(1:4, ncol=2))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
109 par(mar=c(5, 5, 5, 1))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
110
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
111 # Plot all motif counts along the linear correlation coefficient
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
112 plot.scatter(region1Counts+0.5, region2Counts+0.5, log="xy", xlab=xlab, ylab=ylab,
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
113 cex.lab=2.2, cex.axis=1.8, xlim=lim, ylim=lim*rValue)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
114 abline(0, rValue, untf=T)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
115 abline(0, rValue*2, untf=T, lty=2)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
116 abline(0, rValue/2, untf=T, lty=2)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
117
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
118 # Plot enriched and depleted motifs in red, housed in second plot
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
119 plot.scatter(region1Counts+0.5, region2Counts+0.5, log="xy", xlab=xlab, ylab=ylab,
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
120 cex.lab=2.2, cex.axis=1.8, xlim=lim, ylim=lim*rValue)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
121 points(region1Counts[indices]+0.5, region2Counts[indices]+0.5, col="red")
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
122 abline(0, rValue, untf=T)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
123 abline(0, rValue*2, untf=T, lty=2)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
124 abline(0, rValue/2, untf=T, lty=2)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
125
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
126 # Apply and plot gc correction and loess curve
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
127 cat("Applying gc correction, rerunning poisson test...\n")
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
128 ind = which(region1Counts>5)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
129 gc = gc[names(region2Counts)] # Reorder the indices of pwms to match input data
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
130 lo = plot.scatter(gc,log2(region2Counts/region1Counts),draw.loess=T,
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
131 xlab="gc content of motif",ylab=paste("log2(",ylab,"/",xlab,")"),
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
132 cex.lab=2.2,cex.axis=1.8,ind=ind) # This function is in plotting.r
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
133 gcCorrection = 2^approx(lo$loess,xout=gc,rule=2)$y
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
134
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
135 # Recalculate p and q values, and filter for significant entries
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
136 pValueGC = sapply(seq(along=region1Counts),function(i) {
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
137 poisson.test(c(region1Counts[i],region2Counts[i]),r=1/gcCorrection[i])$p.value
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
138 })
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
139 qValueGC=p.adjust(pValueGC,"fdr")
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
140 indicesGC = which(qValueGC<0.1 & abs(log2(region1Counts/region2Counts*gcCorrection))>log2(1.5))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
141
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
142 # Plot gc corrected motif counts
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
143 plot.scatter(region1Counts+0.5, (region2Counts+0.5)/gcCorrection, log="xy",
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
144 xlab=xlab, ylab=paste(ylab,"(normalized)"), cex.lab=2.2, cex.axis=1.8,
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
145 xlim=lim, ylim=lim)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
146 points(region1Counts[indicesGC]+0.5,
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
147 (region2Counts[indicesGC]+0.5)/gcCorrection[indicesGC], col="red")
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
148 abline(0,1)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
149 abline(0,1*2,untf=T,lty=2)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
150 abline(0,1/2,untf=T,lty=2)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
151
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
152 # Trim results, compile statistics and output to file
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
153 # Only does so if significant results are computed
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
154 if(length(indicesGC) > 0) {
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
155 # Calculate expected counts and enrichment ratios
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
156 cat("Calculating statistics...\n")
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
157 nullExpect = region1Counts * gcCorrection
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
158 enrichment = region2Counts / nullExpect
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
159
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
160 # Reorder selected indices in ascending pvalue
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
161 cat("Reordering by ascending pvalue...\n")
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
162 indicesReorder = indicesGC[order(pValueGC[indicesGC])]
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
163
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
164 # Combine data into one data frame and output to two files
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
165 cat("Splitting and outputting data...\n")
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
166 outDF = data.frame(motif=names(pValueGC), p=as.numeric(pValueGC), q=qValueGC,
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
167 stringsAsFactors=F, region_1_count=region1Counts,
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
168 null_expectation=round(nullExpect,2), region_2_count=region2Counts,
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
169 enrichment=enrichment)[indicesReorder,]
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
170 names(outDF)[which(names(outDF)=="region_1_count")]=xlab
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
171 names(outDF)[which(names(outDF)=="region_2_count")]=ylab
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
172 indicesEnrich = which(outDF$enrichment>1)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
173 indicesDeplete = which(outDF$enrichment<1)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
174 outDF$enrichment = ifelse(outDF$enrichment>1,
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
175 round(outDF$enrichment,3),
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
176 paste("1/",round(1/outDF$enrichment,3)))
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
177 write.table(outDF[indicesEnrich,], file=enrichTab, quote=FALSE,
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
178 sep="\t", append=FALSE, row.names=FALSE, col.names=TRUE)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
179 write.table(outDF[indicesDeplete,], file=depleteTab, quote=FALSE,
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
180 sep="\t", append=FALSE, row.names=FALSE, col.names=TRUE)
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
181 }
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
182
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
183 # Catch display messages and output timing information
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
184 catchMessage = dev.off()
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
185 cat("Done. Job started at:", format(startTime, "%a %b %d %X %Y."),
cab2db9d058b Uploaded
jeremyjliu
parents:
diff changeset
186 "Job ended at:", format(Sys.time(), "%a %b %d %X %Y."), "\n")