annotate region_motif_compare.r @ 35:4ce22698acb0 draft

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