comparison goseq.r @ 2:ab492df30cdf draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/tools/goseq commit 4a3c9f195ba5d899b1a1ce5e80281cdf230f456a
author iuc
date Mon, 23 Oct 2017 11:19:12 -0400
parents ade933eff007
children 783e8b70b047
comparison
equal deleted inserted replaced
1:9d1256d9ef0b 2:ab492df30cdf
9 }) 9 })
10 10
11 option_list <- list( 11 option_list <- list(
12 make_option(c("-d", "--dge_file"), type="character", help="Path to file with differential gene expression result"), 12 make_option(c("-d", "--dge_file"), type="character", help="Path to file with differential gene expression result"),
13 make_option(c("-w","--wallenius_tab"), type="character", help="Path to output file with P-values estimated using wallenius distribution."), 13 make_option(c("-w","--wallenius_tab"), type="character", help="Path to output file with P-values estimated using wallenius distribution."),
14 make_option(c("-s","--sampling_tab"), type="character", default=FALSE, help="Path to output file with P-values estimated using wallenius distribution."), 14 make_option(c("-s","--sampling_tab"), type="character", default=FALSE, help="Path to output file with P-values estimated using sampling distribution."),
15 make_option(c("-n","--nobias_tab"), type="character", default=FALSE, help="Path to output file with P-values estimated using wallenius distribution and no correction for gene length bias."), 15 make_option(c("-n","--nobias_tab"), type="character", default=FALSE, help="Path to output file with P-values estimated using hypergeometric distribution and no correction for gene length bias."),
16 make_option(c("-l","--length_bias_plot"), type="character", default=FALSE, help="Path to length-bias plot."), 16 make_option(c("-l","--length_bias_plot"), type="character", default=FALSE, help="Path to length-bias plot."),
17 make_option(c("-sw","--sample_vs_wallenius_plot"), type="character", default=FALSE, help="Path to plot comparing sampling with wallenius p-values."), 17 make_option(c("-sw","--sample_vs_wallenius_plot"), type="character", default=FALSE, help="Path to plot comparing sampling with wallenius p-values."),
18 make_option(c("-r", "--repcnt"), type="integer", default=100, help="Number of repeats for sampling"), 18 make_option(c("-r", "--repcnt"), type="integer", default=100, help="Number of repeats for sampling"),
19 make_option(c("-lf", "--length_file"), type="character", default="FALSE", help = "Path to tabular file mapping gene id to length"), 19 make_option(c("-lf", "--length_file"), type="character", default="FALSE", help = "Path to tabular file mapping gene id to length"),
20 make_option(c("-cat_file", "--category_file"), default="FALSE", type="character", help = "Path to tabular file with gene_id <-> category mapping."), 20 make_option(c("-cat_file", "--category_file"), default="FALSE", type="character", help = "Path to tabular file with gene_id <-> category mapping."),
21 make_option(c("-g", "--genome"), default=NULL, type="character", help = "Genome [used for looking up correct gene length]"), 21 make_option(c("-g", "--genome"), default=NULL, type="character", help = "Genome [used for looking up correct gene length]"),
22 make_option(c("-i", "--gene_id"), default=NULL, type="character", help = "Gene ID format of genes in DGE file"), 22 make_option(c("-i", "--gene_id"), default=NULL, type="character", help = "Gene ID format of genes in DGE file"),
23 make_option(c("-p", "--p_adj_method"), default="BH", type="character", help="Multiple hypothesis testing correction method to use"), 23 make_option(c("-p", "--p_adj_method"), default="BH", type="character", help="Multiple hypothesis testing correction method to use"),
24 make_option(c("-cat", "--use_genes_without_cat"), default=FALSE, type="logical", 24 make_option(c("-cat", "--use_genes_without_cat"), default=FALSE, type="logical",
25 help="A large number of gene may have no GO term annotated. If this option is set to FALSE, genes without category will be ignored in the calculation of p-values(default behaviour). If TRUE these genes will count towards the total number of genes outside the tested category (default behaviour prior to version 1.15.2)."), 25 help="A large number of gene may have no GO term annotated. If this option is set to FALSE, genes without category will be ignored in the calculation of p-values(default behaviour). If TRUE these genes will count towards the total number of genes outside the tested category (default behaviour prior to version 1.15.2)."),
26 make_option(c("-plots", "--make_plots"), default=FALSE, type="logical", help="produce diagnostic plots?") 26 make_option(c("-plots", "--make_plots"), default=FALSE, type="logical", help="produce diagnostic plots?"),
27 make_option(c("-fc", "--fetch_cats"), default=NULL, type="character", help="Categories to get can include one or more of GO:CC, GO:BP, GO:MF, KEGG"),
28 make_option(c("-rd", "--rdata"), default=NULL, type="character", help="Path to RData output file.")
27 ) 29 )
28 30
29 parser <- OptionParser(usage = "%prog [options] file", option_list=option_list) 31 parser <- OptionParser(usage = "%prog [options] file", option_list=option_list)
30 args = parse_args(parser) 32 args = parse_args(parser)
31 33
42 sample_vs_wallenius_plot = args$sample_vs_wallenius_plot 44 sample_vs_wallenius_plot = args$sample_vs_wallenius_plot
43 repcnt = args$repcnt 45 repcnt = args$repcnt
44 p_adj_method = args$p_adj_method 46 p_adj_method = args$p_adj_method
45 use_genes_without_cat = args$use_genes_without_cat 47 use_genes_without_cat = args$use_genes_without_cat
46 make_plots = args$make_plots 48 make_plots = args$make_plots
49 rdata = args$rdata
50
51 if (!is.null(args$fetch_cats)) {
52 fetch_cats = unlist(strsplit(args$fetch_cats, ","))
53 }
47 54
48 # format DE genes into named vector suitable for goseq 55 # format DE genes into named vector suitable for goseq
49 dge_table = read.delim(dge_file, header = FALSE, sep="\t") 56 # check if header is present
57 first_line = read.delim(dge_file, header = FALSE, nrow=1)
58 second_col = toupper(first_line[, ncol(first_line)])
59 if (second_col == TRUE || second_col == FALSE) {
60 dge_table = read.delim(dge_file, header = FALSE, sep="\t")
61 } else {
62 dge_table = read.delim(dge_file, header = TRUE, sep="\t")
63 }
50 genes = as.numeric(as.logical(dge_table[,ncol(dge_table)])) # Last column contains TRUE/FALSE 64 genes = as.numeric(as.logical(dge_table[,ncol(dge_table)])) # Last column contains TRUE/FALSE
51 names(genes) = dge_table[,1] # Assuming first column contains gene names 65 names(genes) = dge_table[,1] # Assuming first column contains gene names
52 66
53 # gene lengths, assuming last column 67 # gene lengths, assuming last column
54 if (length_file != "FALSE" ) { 68 if (length_file != "FALSE" ) {
55 first_line = read.delim(dge_file, header = FALSE, nrow=1) 69 first_line = read.delim(length_file, header = FALSE, nrow=1)
56 if (is.numeric(first_line[, ncol(first_line)])) { 70 if (is.numeric(first_line[, ncol(first_line)])) {
57 length_table = read.delim(length_file, header=FALSE, sep="\t", check.names=FALSE) 71 length_table = read.delim(length_file, header=FALSE, sep="\t", check.names=FALSE)
58 } else { 72 } else {
59 length_table = read.delim(length_file, header=TRUE, sep="\t", check.names=FALSE) 73 length_table = read.delim(length_file, header=TRUE, sep="\t", check.names=FALSE)
60 } 74 }
64 gene_lengths = getlength(names(genes), genome, gene_id) 78 gene_lengths = getlength(names(genes), genome, gene_id)
65 } 79 }
66 80
67 # Estimate PWF 81 # Estimate PWF
68 82
69 if (make_plots == TRUE) { 83 if (make_plots != 'false') {
70 pdf(length_bias_plot) 84 pdf(length_bias_plot)
71 } 85 }
72 pwf=nullp(genes, genome = genome, id = gene_id, bias.data = gene_lengths, plot.fit=make_plots) 86 pwf=nullp(genes, genome = genome, id = gene_id, bias.data = gene_lengths, plot.fit=make_plots)
73 graphics.off() 87 graphics.off()
74 88
75 # Fetch GO annotations if category_file hasn't been supplied: 89 # Fetch GO annotations if category_file hasn't been supplied:
76 if (category_file == "FALSE") { 90 if (category_file == "FALSE") {
77 go_map=getgo(genes = names(genes), genome = genome, id = gene_id, fetch.cats=c("GO:CC", "GO:BP", "GO:MF", "KEGG")) 91 go_map=getgo(genes = names(genes), genome=genome, id=gene_id, fetch.cats=fetch_cats)
78 } else { 92 } else {
79 # check for header: first entry in first column must be present in genes, else it's a header 93 # check for header: first entry in first column must be present in genes, else it's a header
80 first_line = read.delim(category_file, header = FALSE, nrow=1) 94 first_line = read.delim(category_file, header = FALSE, nrow=1)
81 if (first_line[,1] %in% names(genes)) { 95 if (first_line[,1] %in% names(genes)) {
82 go_map = read.delim(category_file, header = FALSE) 96 go_map = read.delim(category_file, header = FALSE)
101 write.table(GO.nobias, nobias_tab, sep="\t", row.names = FALSE, quote = FALSE) 115 write.table(GO.nobias, nobias_tab, sep="\t", row.names = FALSE, quote = FALSE)
102 } 116 }
103 117
104 # Sampling distribution 118 # Sampling distribution
105 if (repcnt > 0) { 119 if (repcnt > 0) {
120
121 # capture the sampling progress so it doesn't fill stdout
122 zz <- file("/dev/null", open = "wt")
123 sink(zz)
106 GO.samp=goseq(pwf, genome = genome, id = gene_id, method="Sampling", repcnt=repcnt, use_genes_without_cat = use_genes_without_cat, gene2cat=go_map) 124 GO.samp=goseq(pwf, genome = genome, id = gene_id, method="Sampling", repcnt=repcnt, use_genes_without_cat = use_genes_without_cat, gene2cat=go_map)
125 sink()
126
107 GO.samp$p.adjust.over_represented = p.adjust(GO.samp$over_represented_pvalue, method=p_adj_method) 127 GO.samp$p.adjust.over_represented = p.adjust(GO.samp$over_represented_pvalue, method=p_adj_method)
108 GO.samp$p.adjust.under_represented = p.adjust(GO.samp$under_represented_pvalue, method=p_adj_method) 128 GO.samp$p.adjust.under_represented = p.adjust(GO.samp$under_represented_pvalue, method=p_adj_method)
109 write.table(GO.samp, sampling_tab, sep="\t", row.names = FALSE, quote = FALSE) 129 write.table(GO.samp, sampling_tab, sep="\t", row.names = FALSE, quote = FALSE)
110 # Compare sampling with wallenius 130 # Compare sampling with wallenius
111 if (make_plots == TRUE) { 131 if (make_plots == TRUE) {
116 abline(0,1,col=3,lty=2) 136 abline(0,1,col=3,lty=2)
117 graphics.off() 137 graphics.off()
118 } 138 }
119 } 139 }
120 140
141 # Output RData file
142 if (!is.null(args$rdata)) {
143 save.image(file = "goseq_analysis.RData")
144 }
145
146
121 sessionInfo() 147 sessionInfo()