annotate edgeR.pl @ 10:86292c2b0ba9 draft

Uploaded
author fcaramia
date Tue, 14 May 2013 21:40:04 -0400
parents 674c75219f15
children e5fcbabbdea7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
1 #/bin/perl
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
2
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
3 use strict;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
4 use warnings;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
5 use Getopt::Std;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
6 use File::Basename;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
7 use File::Path qw(make_path remove_tree);
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
8 $| = 1;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
9
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
10 # Grab and set all options
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
11 my %OPTIONS = (a => "glm", d => "tag", f => "BH", p => 0.3, r => 5, u => "movingave");
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
12
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
13 getopts('a:d:e:f:h:lmn:o:p:r:tu:', \%OPTIONS);
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
14
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
15 die qq(
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
16 Usage: edgeR.pl [OPTIONS] factor::factor1::levels [factor::factor2::levels ...] cp::cont_pred1::values [cp::cont_pred2::values ...] cnt::contrast1 [cnt::contrast2] matrix
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
17
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
18 OPTIONS: -a STR Type Of Analysis [glm, pw, limma] (default: $OPTIONS{a})
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
19 -d STR The dispersion estimate to use for GLM analysis [tag, trend, common] (default: $OPTIONS{d})
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
20 -e STR Path to place additional output files
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
21 -f STR False discovery rate adjustment method [BH, holm, hochberg, hommel, BY, none] (default: $OPTIONS{f})
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
22 -h STR Name of html file for additional files
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
23 -l Output the normalised digital gene expression matrix in log2 format (only applicable when using limma and -n is also specified)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
24 -m Perform all pairwise comparisons
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
25 -n STR File name to output the normalised digital gene expression matrix (only applicable when usinf glm or limma model)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
26 -o STR File name to output csv file with results
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
27 -p FLT The proportion of all tags/genes to be used for the locally weighted estimation of the tagwise dispersion, ony applicable when 1 factor analysis selected (default: $OPTIONS{p})
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
28 -r INT Common Dispersion Rowsum Filter, ony applicable when 1 factor analysis selected (default: $OPTIONS{r})
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
29 -t Estimate Tagwise Disp when performing 1 factor analysis
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
30 -u STR Method for allowing the prior distribution for the dispersion to be abundance- dependent ["movingave", "tricube", "none"] (default: $OPTIONS{u})
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
31
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
32 ) if(!@ARGV);
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
33
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
34 my $matrix = pop @ARGV;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
35
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
36 make_path($OPTIONS{e});
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
37 open(Rcmd,">$OPTIONS{e}/r_script.R") or die "Cannot open $OPTIONS{e}/r_script.R\n\n";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
38 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
39 zz <- file(\"$OPTIONS{e}/r_script.err\", open=\"wt\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
40 sink(zz)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
41 sink(zz, type=\"message\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
42
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
43 library(edgeR)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
44 library(limma)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
45
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
46 # read in matrix and groups
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
47 toc <- read.table(\"$matrix\", sep=\"\\t\", comment=\"\", as.is=T)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
48 groups <- sapply(toc[1, -1], strsplit, \":\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
49 for(i in 1:length(groups)) { g <- make.names(groups[[i]][2]); names(groups)[i] <- g; groups[[i]] <- groups[[i]][-2] }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
50 colnames(toc) <- make.names(toc[2,])
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
51 toc[,1] <- gsub(\",\", \".\", toc[,1])
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
52 tagnames <- toc[-(1:2), 1]
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
53 rownames(toc) <- toc[,1]
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
54 toc <- toc[-(1:2), -1]
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
55 for(i in colnames(toc)) toc[, i] <- as.numeric(toc[,i])
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
56 norm_factors <- calcNormFactors(as.matrix(toc))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
57
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
58 pw_tests <- list()
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
59 uniq_groups <- unique(names(groups))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
60 for(i in 1:(length(uniq_groups)-1)) for(j in (i+1):length(uniq_groups)) pw_tests[[length(pw_tests)+1]] <- c(uniq_groups[i], uniq_groups[j])
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
61 DGE <- DGEList(toc, lib.size=norm_factors*colSums(toc), group=names(groups))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
62 pdf(\"$OPTIONS{e}/MA_plots_normalisation.pdf\", width=14)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
63 for(i in 1:length(pw_tests)) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
64 j <- c(which(names(groups) == pw_tests[[i]][1])[1], which(names(groups) == pw_tests[[i]][2])[1])
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
65 par(mfrow = c(1, 2))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
66 maPlot(toc[, j[1]], toc[, j[2]], normalize = TRUE, pch = 19, cex = 0.2, ylim = c(-10, 10), main=paste(\"MA Plot\", colnames(toc)[j[1]], \"vs\", colnames(toc)[j[2]]))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
67 grid(col = \"blue\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
68 abline(h = log2(norm_factors[j[2]]), col = \"red\", lwd = 4)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
69 maPlot(DGE\$counts[, j[1]]/DGE\$samples\$lib.size[j[1]], DGE\$counts[, j[2]]/DGE\$samples\$lib.size[j[2]], normalize = FALSE, pch = 19, cex = 0.2, ylim = c(-8, 8), main=paste(\"MA Plot\", colnames(toc)[j[1]], \"vs\", colnames(toc)[j[2]], \"Normalised\"))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
70 grid(col = \"blue\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
71 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
72 dev.off()
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
73 pdf(file=\"$OPTIONS{e}/MDSplot.pdf\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
74 plotMDS(DGE, main=\"MDS Plot\", col=as.numeric(factor(names(groups)))+1, xlim=c(-3,3))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
75 dev.off()
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
76 tested <- list()
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
77 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
78
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
79 my $all_cont;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
80 my @add_cont;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
81 my @fact;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
82 my @fact_names;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
83 my @cp;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
84 my @cp_names;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
85 if(@ARGV) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
86 foreach my $input (@ARGV) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
87 my @tmp = split "::", $input;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
88 if($tmp[0] eq "factor") {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
89 $tmp[1] =~ s/[ \?\(\)\[\]\/\\=+<>:;\"\',\*\^\|\&-]/./g;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
90 push @fact_names, $tmp[1];
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
91 $tmp[2] =~ s/:/\", \"/g;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
92 $tmp[2] = "\"".$tmp[2]."\"";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
93 push @fact, $tmp[2];
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
94 } elsif($tmp[0] eq "cp") {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
95 $tmp[1] =~ s/[ \?\(\)\[\]\/\\=+<>:;\"\',\*\^\|\&-]/./g;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
96 push @cp_names, $tmp[1];
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
97 $tmp[2] =~ s/:/, /g;
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
98 push @cp, $tmp[2];
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
99 } elsif($tmp[0] eq "cnt") {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
100 push @add_cont, $tmp[1];
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
101 } else {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
102 die("Unknown Input: $input\n");
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
103 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
104 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
105 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
106
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
107 if($OPTIONS{a} eq "pw") {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
108 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
109 disp <- estimateCommonDisp(DGE, rowsum.filter=$OPTIONS{r})
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
110 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
111 if(defined $OPTIONS{t}) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
112 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
113 disp <- estimateTagwiseDisp(disp, trend=\"$OPTIONS{u}\", prop.used=$OPTIONS{p})
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
114 pdf(file=\"$OPTIONS{e}/Tagwise_Dispersion_vs_Abundance.pdf\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
115 plot(log2(1e06*disp\$conc\$conc.common), disp\$tagwise.dispersion, xlab=\"Counts per million (log2 scale)\", ylab=\"Tagwise dispersion\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
116 abline(h=disp\$common.dispersion, col=\"firebrick\", lwd=3)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
117 dev.off()
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
118 "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
119 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
120 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
121 for(i in 1:length(pw_tests)) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
122 tested[[i]] <- exactTest(disp, pair=pw_tests[[i]])
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
123 names(tested)[i] <- paste(pw_tests[[i]][2], \"-\", pw_tests[[i]][1], sep=\"\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
124 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
125 pdf(file=\"$OPTIONS{e}/Smear_Plots.pdf\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
126 for(i in 1:length(pw_tests)) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
127 if(nrow(decideTestsDGE(tested[[i]] , p.value=0.05)) > 0) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
128 de_tags <- rownames(decideTestsDGE(tested[[i]] , p.value=0.05, adjust.method=\"$OPTIONS{f}\"))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
129 ttl <- \"(Diff. Exp. Genes With adj. Pvalue < 0.05 highlighted)\"
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
130 } else {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
131 de_tags <- rownames(topTags(tested[[i]], n=100)\$table)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
132 ttl <- \"(Top 100 tags highlighted)\"
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
133 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
134
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
135 plotSmear(disp, pair=pw_tests[[i]], de.tags = de_tags, main = paste(\"FC plot\", ttl))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
136 abline(h = c(-2, 2), col = \"dodgerblue\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
137 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
138 dev.off()
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
139 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
140 } elsif($OPTIONS{a} eq "glm") {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
141 for(my $fct = 0; $fct <= $#fact_names; $fct++) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
142 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
143 $fact_names[$fct] <- c($fact[$fct])
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
144 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
145 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
146 for(my $fct = 0; $fct <= $#cp_names; $fct++) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
147 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
148 $cp_names[$fct] <- c($cp[$fct])
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
149 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
150 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
151 my $all_fact = "";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
152 if(@fact_names) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
153 foreach (@fact_names) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
154 $all_fact .= " + factor($_)";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
155 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
156 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
157 my $all_cp = "";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
158 if(@cp_names) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
159 $all_cp = " + ".join(" + ", @cp_names);
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
160 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
161 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
162 group_fact <- factor(names(groups))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
163 design <- model.matrix(~ -1 + group_fact${all_fact}${all_cp})
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
164 colnames(design) <- sub(\"group_fact\", \"\", colnames(design))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
165 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
166 foreach my $fct (@fact_names) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
167 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
168 colnames(design) <- make.names(sub(\"factor.$fct.\", \"\", colnames(design)))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
169 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
170 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
171 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
172 disp <- estimateGLMCommonDisp(DGE, design)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
173 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
174 if($OPTIONS{d} eq "tag" || $OPTIONS{d} eq "trend") {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
175 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
176 disp <- estimateGLMTrendedDisp(disp, design)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
177 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
178 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
179 if($OPTIONS{d} eq "tag") {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
180 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
181 disp <- estimateGLMTagwiseDisp(disp, design)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
182 fit <- glmFit(disp, design)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
183 pdf(file=\"$OPTIONS{e}/Tagwise_Dispersion_vs_Abundance.pdf\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
184 plot(fit\$abund+log(1e06), sqrt(disp\$tagwise.dispersion), xlab=\"Counts per million (log2 scale)\", ylab=\"Tagwise dispersion\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
185 oo <- order(disp\$abundance)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
186 lines(fit\$abundance[oo]+log(1e06), sqrt(disp\$trended.dispersion[oo]), col=\"dodgerblue\", lwd=3)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
187 abline(h=sqrt(disp\$common.dispersion), col=\"firebrick\", lwd=3)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
188 dev.off()
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
189 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
190 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
191 if(@add_cont) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
192 $all_cont = "\"".join("\", \"", @add_cont)."\"";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
193 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
194 cont <- c(${all_cont})
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
195 for(i in uniq_groups) cont <- gsub(paste(groups[[i]], \"([^0-9])\", sep=\"\"), paste(i, \"\\\\1\", sep=\"\"), cont)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
196 for(i in uniq_groups) cont <- gsub(paste(groups[[i]], \"\$\", sep=\"\"), i, cont)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
197 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
198 } else {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
199 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
200 cont <- NULL
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
201 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
202 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
203 if(defined $OPTIONS{m}) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
204 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
205 for(i in 1:length(pw_tests)) cont <- c(cont, paste(pw_tests[[i]][2], \"-\", pw_tests[[i]][1], sep=\"\"))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
206 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
207 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
208 if(!defined $OPTIONS{m} && !@add_cont){
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
209 die("No Contrasts have been specified, you must at least either select multiple pairwise comparisons or specify a custom contrast\n");
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
210 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
211 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
212 fit <- glmFit(disp, design)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
213 cont <- makeContrasts(contrasts=cont, levels=design)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
214 for(i in colnames(cont)) tested[[i]] <- glmLRT(disp, fit, contrast=cont[,i])
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
215 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
216 if(defined $OPTIONS{n}) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
217 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
218 tab <- data.frame(ID=rownames(fit\$fitted.values), fit\$fitted.values, stringsAsFactors=F)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
219 write.table(tab, \"$OPTIONS{n}\", quote=F, sep=\"\\t\", row.names=F)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
220 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
221 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
222 } elsif($OPTIONS{a} eq "limma") {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
223 for(my $fct = 0; $fct <= $#fact_names; $fct++) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
224 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
225 $fact_names[$fct] <- c($fact[$fct])
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
226 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
227 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
228 for(my $fct = 0; $fct <= $#cp_names; $fct++) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
229 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
230 $cp_names[$fct] <- c($cp[$fct])
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
231 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
232 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
233 my $all_fact = "";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
234 if(@fact_names) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
235 foreach (@fact_names) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
236 $all_fact .= " + factor($_)";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
237 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
238 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
239 my $all_cp = "";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
240 if(@cp_names) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
241 $all_cp = " + ".join(" + ", @cp_names);
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
242 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
243 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
244 group_fact <- factor(names(groups))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
245 design <- model.matrix(~ -1 + group_fact${all_fact}${all_cp})
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
246 colnames(design) <- sub(\"group_fact\", \"\", colnames(design))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
247 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
248 foreach my $fct (@fact_names) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
249 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
250 colnames(design) <- make.names(sub(\"factor.$fct.\", \"\", colnames(design)))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
251 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
252 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
253 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
254 isexpr <- rowSums(cpm(toc)>1) >= 2
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
255 toc <- toc[isexpr, ]
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
256 pdf(file=\"$OPTIONS{e}/LIMMA_voom.pdf\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
257 y <- voom(toc, design, plot=TRUE, lib.size=colSums(toc)*norm_factors)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
258 dev.off()
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
259
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
260 pdf(file=\"$OPTIONS{e}/LIMMA_MDS_plot.pdf\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
261 plotMDS(y, labels=colnames(toc), col=as.numeric(factor(names(groups)))+1, gene.selection=\"common\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
262 dev.off()
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
263 fit <- lmFit(y, design)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
264 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
265 if(defined $OPTIONS{n}) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
266 if(defined $OPTIONS{l}) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
267 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
268 tab <- data.frame(ID=rownames(y\$E), y\$E, stringsAsFactors=F)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
269 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
270 } else {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
271 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
272 tab <- data.frame(ID=rownames(y\$E), 2^y\$E, stringsAsFactors=F)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
273 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
274 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
275 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
276 write.table(tab, \"$OPTIONS{n}\", quote=F, sep=\"\\t\", row.names=F)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
277 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
278 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
279 if(@add_cont) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
280 $all_cont = "\"".join("\", \"", @add_cont)."\"";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
281 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
282 cont <- c(${all_cont})
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
283 for(i in uniq_groups) cont <- gsub(paste(groups[[i]], \"([^0-9])\", sep=\"\"), paste(i, \"\\\\1\", sep=\"\"), cont)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
284 for(i in uniq_groups) cont <- gsub(paste(groups[[i]], \"\$\", sep=\"\"), i, cont)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
285 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
286 } else {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
287 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
288 cont <- NULL
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
289 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
290 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
291 if(defined $OPTIONS{m}) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
292 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
293 for(i in 1:length(pw_tests)) cont <- c(cont, paste(pw_tests[[i]][2], \"-\", pw_tests[[i]][1], sep=\"\"))
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
294 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
295 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
296 if(!defined $OPTIONS{m} && !@add_cont){
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
297 die("No Contrasts have been specified, you must at least either select multiple pairwise comparisons or specify a custom contrast\n");
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
298 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
299 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
300 cont <- makeContrasts(contrasts=cont, levels=design)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
301 fit2 <- contrasts.fit(fit, cont)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
302 fit2 <- eBayes(fit2)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
303 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
304 } else {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
305 die("Anaysis type $OPTIONS{a} not found\n");
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
306
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
307 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
308
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
309 if($OPTIONS{a} ne "limma") {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
310 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
311 options(digits = 6)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
312 tab <- NULL
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
313 for(i in names(tested)) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
314 tab_tmp <- topTags(tested[[i]], n=Inf, adjust.method=\"$OPTIONS{f}\")[[1]]
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
315 colnames(tab_tmp) <- paste(i, colnames(tab_tmp), sep=\":\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
316 tab_tmp <- tab_tmp[tagnames,]
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
317 if(is.null(tab)) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
318 tab <- tab_tmp
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
319 } else tab <- cbind(tab, tab_tmp)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
320 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
321 tab <- cbind(Feature=rownames(tab), tab)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
322 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
323 } else {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
324 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
325 tab <- NULL
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
326 options(digits = 6)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
327 for(i in colnames(fit2)) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
328 tab_tmp <- topTable(fit2, coef=i, n=Inf, sort.by=\"none\", adjust.method=\"$OPTIONS{f}\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
329 colnames(tab_tmp)[-1] <- paste(i, colnames(tab_tmp)[-1], sep=\":\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
330 if(is.null(tab)) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
331 tab <- tab_tmp
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
332 } else tab <- cbind(tab, tab_tmp[,-1])
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
333 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
334 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
335 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
336 print Rcmd "
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
337 write.table(tab, \"$OPTIONS{o}\", quote=F, sep=\"\\t\", row.names=F)
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
338 sink(type=\"message\")
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
339 sink()
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
340 ";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
341 close(Rcmd);
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
342 system("R --no-restore --no-save --no-readline < $OPTIONS{e}/r_script.R > $OPTIONS{e}/r_script.out");
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
343
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
344 open(HTML, ">$OPTIONS{h}");
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
345 print HTML "<html><head><title>EdgeR: Empirical analysis of digital gene expression data</title></head><body><h3>EdgeR Additional Files:</h3><p><ul>\n";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
346 print HTML "<li><a href=MA_plots_normalisation.pdf>MA_plots_normalisation.pdf</a></li>\n";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
347 print HTML "<li><a href=MDSplot.pdf>MDSplot.pdf</a></li>\n";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
348 if($OPTIONS{a} eq "pw") {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
349 if(defined $OPTIONS{t}) {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
350 print HTML "<li><a href=Tagwise_Dispersion_vs_Abundance.pdf>Tagwise_Dispersion_vs_Abundance.pdf</a></li>\n";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
351 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
352 print HTML "<li><a href=Smear_Plots.pdf>Smear_Plots.pdf</a></li>\n";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
353 } elsif($OPTIONS{a} eq "glm" && $OPTIONS{d} eq "tag") {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
354 print HTML "<li><a href=Tagwise_Dispersion_vs_Abundance.pdf>Tagwise_Dispersion_vs_Abundance.pdf</a></li>\n";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
355 } elsif($OPTIONS{a} eq "limma") {
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
356 print HTML "<li><a href=LIMMA_MDS_plot.pdf>LIMMA_MDS_plot.pdf</a></li>\n";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
357 print HTML "<li><a href=LIMMA_voom.pdf>LIMMA_voom.pdf</a></li>\n";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
358 }
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
359 print HTML "<li><a href=r_script.R>r_script.R</a></li>\n";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
360 print HTML "<li><a href=r_script.out>r_script.out</a></li>\n";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
361 print HTML "<li><a href=r_script.err>r_script.err</a></li>\n";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
362 print HTML "</ul></p>\n";
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
363 close(HTML);
674c75219f15 Uploaded
fcaramia
parents:
diff changeset
364