-
Notifications
You must be signed in to change notification settings - Fork 7
/
functional_Hunter.R
243 lines (172 loc) · 10.2 KB
/
functional_Hunter.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#! /usr/bin/env Rscript
#############################################
############## FUNCTIONAL HUNTER ###########
#############################################
# this is wrapped in a tryCatch. The first expression works when source executes, the
# second expression works when R CMD does it.
full.fpath <- tryCatch(normalizePath(parent.frame(2)$ofile), # works when using source
error=function(e) # works when using R CMD
normalizePath(unlist(strsplit(commandArgs()[grep('^--file=', commandArgs())], '='))[2]))
main_path_script <- dirname(full.fpath)
#Loading libraries
suppressPackageStartupMessages(library(optparse))
suppressPackageStartupMessages(library(biomaRt))
suppressPackageStartupMessages(library(topGO))
suppressPackageStartupMessages(library(KEGGREST))
suppressPackageStartupMessages(library(stringr))
suppressPackageStartupMessages(library(plyr))
suppressPackageStartupMessages(library(knitr))
# Load custom libraries
source(file.path(main_path_script, 'lib', 'general_functions.R'))
source(file.path(main_path_script, 'lib', 'functional_analysis_library.R'))
biomaRt_query_info <- read.table(file.path(main_path_script, "lib/biomaRt_organism_table.txt"), header=T, row.names=1, sep="\t")
#############################################
### MAIN
#############################################
### Input/Output (I/O)
#############################################
# Parse command line
#------------------------------------------------
option_list <- list(
make_option(c("-i", "--input_hunter_file"), type="character", default="hunter_DE_results/Common_results/hunter_results_table.txt",
help="DEgenes Hunter's differential expression analysis output file"),
make_option(c("-c", "--countdata_file"), type="character", default="hunter_DE_results/filtered_count_data.txt",
help="Filtered count data file"),
make_option(c("-a", "--annot_file"), type="character", default=NULL,
help="File with annotations for functional analysis of a non-model organism"),
make_option(c("-L", "--List_organisms"), action="store_true", type="logical", default=FALSE,
help="List all organisms provided"),
make_option(c("-m", "--model_organism"), type="character", default=NULL,
help="Ortologue Species. Default=%default"),
make_option(c("-t", "--biomaRt_filter"), type="character", default="E",
help="IDtype"),
make_option(c("-f", "--functional_analysis"), type="character", default=c("GK"),
help="Selection of type of functional analyses to be performed (G = GO, K = KEGG).
By default the following modules Default=%default are performed"),
make_option(c("-G", "--GO_graphs"), type="character", default=c("M"),
help="Modules to able/disable (M = Molecular Function, B = Biological Process, C = Celular Components). By default Default=%default GO cathegory is performed"),
make_option(c("-K", "--Kegg_organism"), type="character", default=NULL,
help="Indicate organism to look for in the Kegg database for doing the path enrichment"),
make_option(c("-q", "--save_query"), type="logical", default=FALSE,
help="Save biomaRt query. By default the biomaRt query is saved"),
make_option(c("-o", "--output_files"), type="character", default="",
help="Output path. Default=%default")
)
opt <- parse_args(OptionParser(option_list=option_list))
###### Input control ######
if(opt$List_organisms == TRUE){
print(as.character(rownames(biomaRt_query_info)))
stop('program ends')
} else if (is.null(opt$model_organism)){
stop('No model organism indicated. Please indicate the
model organism using parameter -m. Use -L to list all model organisms available')
}
# Parsing input data
#--------------------------------------------------
DEG_annot_table <- read.table(opt$input_hunter_file, header=T, row.names=1, sep="\t")
biomaRt_organism_info <- get_specific_dataframe_names(biomaRt_query_info, rownames(biomaRt_query_info), opt$model_organism)
KEGG_enrich_selected <- grepl("K", opt$functional_analysis)
if ((biomaRt_organism_info["KeggCode"] == "NotInKEGG") & (is.null(opt$Kegg_organism)) & (KEGG_enrich_selected==TRUE)){
stop('This organism is not yet available in the KEGG database. You can select an alternative organism to perform
the path enrichment analysis using parameter -K. Use -L to list all model organisms available.')
}
if (!is.null(opt$annot_file)){
annot <- read.table(opt$annot_file, header=F, row.names=NULL, sep="\t")
reference_table <- annot
} else {
reference_table <- read.table(opt$countdata_file, header=T, row.names=NULL, sep="\t")
}
if (opt$biomaRt_filter == "E"){
opt$biomaRt_filter <- as.character(biomaRt_organism_info[1,7])
}
if (opt$biomaRt_filter == "R"){
opt$biomaRt_filter <- as.character(biomaRt_organism_info[1,8])
}
############ CREATE FOLDERS #########3
paths <- list()
dir.create(opt$output_files)
paths$root <-opt$output_files
################# PREPROCESSING INPUT DATA FOR FUNCTIONAL ANALYSIS #############
############# Setting biomaRt query #############
#### Setting biomaRt query
organism_mart <- as.character(biomaRt_organism_info[,"Mart"])
organism_dataset <- as.character(biomaRt_organism_info[,"Dataset"])
organism_host <- as.character(biomaRt_organism_info[,"biomaRt_Host"])
#### Setting biomaRt attributes
attr <- c(opt$biomaRt_filter, as.character(biomaRt_organism_info[,"Attribute_GOs"]))
attr <- c(attr, as.character(biomaRt_organism_info[,"Attribute_entrez"]))
# Getting biomaRt information
# -----------------------------------------------
reference_table <- obtain_info_from_biomaRt(as.character(reference_table[,1]),opt$biomaRt_filter, organism_mart, organism_dataset, organism_host, attr)
print(head(reference_table))
if (opt$save_query == TRUE){
query <- reference_table
saveRDS(query, file=file.path("query_results_temp"))
}
common_DEGs_df <- subset(DEG_annot_table, genes_tag=="PREVALENT_DEG")
common_DEGs <- rownames(common_DEGs_df)
print(head(common_DEGs))
#stopifnot(1>500)
common_annot_DEGs_df <- get_specific_dataframe_names(reference_table, reference_table[,1], common_DEGs)
common_unique_entrez <- unique(common_annot_DEGs_df[,"entrezgene"])
print(head(common_annot_DEGs_df))
#stopifnot(1>500)
union_DEGs_df <- subset(DEG_annot_table, genes_tag=="POSSIBLE_DEG")
union_DEGs_df <- rbind(common_DEGs_df, union_DEGs_df)
union_DEGs <- rownames(union_DEGs_df)
union_annot_DEGs_df <- get_specific_dataframe_names(reference_table, reference_table[,1], union_DEGs)
############ PERFORMING FUNCTIONAL ANALYSIS ######################
module_selected <- grepl("G", opt$functional_analysis)
if (module_selected == TRUE){
pos_logFC_common_DEGs <- rownames(subset(common_DEGs_df, common_DEGs_df["logFC_DESeq2"] > 0))
neg_logFC_common_DEGs <- rownames(subset(common_DEGs_df, common_DEGs_df["logFC_DESeq2"] < 0))
pos_logFC_union_DEGs <- rownames(subset(union_DEGs_df, union_DEGs_df["logFC_DESeq2"] > 0))
neg_logFC_union_DEGs <- rownames(subset(union_DEGs_df, union_DEGs_df["logFC_DESeq2"] < 0))
module_selected <- grepl("M", opt$GO_graphs)
if (module_selected==TRUE){
perform_GSEA_analysis(common_DEGs, union_annot_DEGs_df, "MF", "GOgraph_preval_MF.pdf")
perform_GSEA_analysis(pos_logFC_common_DEGs, union_annot_DEGs_df, "MF", "GOgraph_preval_overex_MF.pdf")
perform_GSEA_analysis(neg_logFC_common_DEGs, union_annot_DEGs_df, "MF", "GOgraph_preval_underex_MF.pdf")
perform_GSEA_analysis(union_DEGs, reference_table, "MF", "GOgraph_allpos_MF.pdf")
perform_GSEA_analysis(pos_logFC_union_DEGs, reference_table, "MF", "GOgraph_allpos_overex_MF.pdf")
perform_GSEA_analysis(neg_logFC_union_DEGs, reference_table, "MF", "GOgraph_allpos_underex_MF.pdf")
}
module_selected <- grepl("B", opt$GO_graphs)
if (module_selected==TRUE){
perform_GSEA_analysis(common_DEGs, union_annot_DEGs_df, "BP", "GOgraph_preval_BP.pdf")
perform_GSEA_analysis(pos_logFC_common_DEGs, union_annot_DEGs_df, "BP", "GOgraph_preval_overex_BP.pdf")
perform_GSEA_analysis(neg_logFC_common_DEGs, union_annot_DEGs_df, "BP", "GOgraph_preval_underex_BP.pdf")
perform_GSEA_analysis(union_DEGs, reference_table, "BP", "GOgraph_allpos_BP.pdf")
perform_GSEA_analysis(pos_logFC_union_DEGs, reference_table, "BP", "GOgraph_allpos_overex_BP.pdf")
perform_GSEA_analysis(neg_logFC_union_DEGs, reference_table, "BP", "GOgraph_allpos_underex_BP.pdf")
}
module_selected <- grepl("C", opt$GO_graphs)
if (module_selected==TRUE){
perform_GSEA_analysis(common_DEGs, union_annot_DEGs_df, "CC", "GOgraph_preval_CC.pdf")
perform_GSEA_analysis(pos_logFC_common_DEGs, union_annot_DEGs_df, "CC", "GOgraph_preval_overex_CC.pdf")
perform_GSEA_analysis(neg_logFC_common_DEGs, union_annot_DEGs_df, "CC", "GOgraph_preval_underex_CC.pdf")
perform_GSEA_analysis(union_DEGs, reference_table, "CC", "GOgraph_allpos_CC.pdf")
perform_GSEA_analysis(pos_logFC_union_DEGs, reference_table, "CC", "GOgraph_allpos_overex_CC.pdf")
perform_GSEA_analysis(neg_logFC_union_DEGs, reference_table, "CC", "GOgraph_allpos_underex_CC.pdf")
}
}
module_selected <- grepl("K", opt$functional_analysis)
if (module_selected == TRUE){
if(!(is.null(opt$Kegg_organism))){
Kegg_organism_line <- get_specific_dataframe_names(biomaRt_query_info, rownames(biomaRt_query_info), opt$Kegg_organism)
biomaRt_KEGG_organism_info["KeggCode"] <- Kegg_organism_line["KeggCode"]
KEGG_table <- obtain_info_from_biomaRt(as.character(reference_table[,1]),opt$biomaRt_filter, organism_mart, organism_dataset, organism_host, "entrezgene")
common_annot_DEGs_df <- get_specific_dataframe_names(KEGG_table, KEGG[,1], common_DEGs)
common_unique_entrez <- unique(common_annot_DEGs_df[,"entrezgene"])
pathway_common_id_EC_df <- find_interesting_pathways(biomaRt_KEGG_organism_info, common_unique_entrez)
visualizing_KEGG_pathways("KEGG_paths.html", pathway_common_id_EC_df)
} else {
pathway_common_id_EC_df <- find_interesting_pathways(biomaRt_organism_info, common_unique_entrez)
visualizing_KEGG_pathways("KEGG_paths.html", pathway_common_id_EC_df)
}
}
################## CREATE FINAL ANNOTATION TABLE ##########
write.table(DEG_annot_table, file=file.path(paths$root, "Annotated_table.txt"), quote=F, col.names=NA, sep="\t")
write.table(reference_table, file=file.path(paths$root, "entrez_Gos.txt"), quote=F, col.names=NA, sep="\t")
#### functional
generate_FA_report()