comparison kegg_identification.R @ 3:e7b3609160c5 draft

planemo upload commit 6854718828a7478905b41fb92d6f96ab41896e84-dirty
author proteore
date Tue, 14 May 2019 08:51:46 -0400
parents
children dc39f12f96d1
comparison
equal deleted inserted replaced
2:179bfc3c7938 3:e7b3609160c5
1 options(warn=-1) #TURN OFF WARNINGS !!!!!!
2
3 suppressMessages(library(KEGGREST))
4
5 get_args <- function(){
6
7 ## Collect arguments
8 args <- commandArgs(TRUE)
9
10 ## Default setting when no arguments passed
11 if(length(args) < 1) {
12 args <- c("--help")
13 }
14
15 ## Help section
16 if("--help" %in% args) {
17 cat("Pathview R script
18 Arguments:
19 --help Print this test
20 --input tab file
21 --id_list id list ',' separated
22 --id_type type of input ids (kegg-id, uniprot_AC,geneID)
23 --id_column number og column containg ids of interest
24 --nb_pathways number of pathways to return
25 --header boolean
26 --output output path
27 --species species used to get specific pathways (hsa,mmu,rno)
28
29 Example:
30 Rscript keggrest.R --input='P31946,P62258' --id_type='uniprot' --id_column 'c1' --header TRUE \n\n")
31
32 q(save="no")
33 }
34
35 parseArgs <- function(x) strsplit(sub("^--", "", x), "=")
36 argsDF <- as.data.frame(do.call("rbind", parseArgs(args)))
37 args <- as.list(as.character(argsDF$V2))
38 names(args) <- argsDF$V1
39
40 return(args)
41 }
42
43 str2bool <- function(x){
44 if (any(is.element(c("t","true"),tolower(x)))){
45 return (TRUE)
46 }else if (any(is.element(c("f","false"),tolower(x)))){
47 return (FALSE)
48 }else{
49 return(NULL)
50 }
51 }
52
53 read_file <- function(path,header){
54 file <- try(read.csv(path,header=header, sep="\t",stringsAsFactors = FALSE, quote="\"", check.names = F),silent=TRUE)
55 if (inherits(file,"try-error")){
56 stop("File not found !")
57 }else{
58 return(file)
59 }
60 }
61
62 get_pathways_list <- function(species){
63 ##all available pathways for the species
64 pathways <-keggLink("pathway", species)
65 tot_path<-unique(pathways)
66
67 ##formating the dat into a list object
68 ##key= pathway ID, value = genes of the pathway in the kegg format
69 pathways_list <- sapply(tot_path, function(pathway) names(which(pathways==pathway)))
70 return (pathways_list)
71 }
72
73 get_list_from_cp <-function(list){
74 list = gsub(";","\t",list)
75 list = strsplit(list, "[ \t\n]+")[[1]]
76 list = gsub("[[:blank:]]|\u00A0|NA","",list)
77 list = list[which(!is.na(list[list != ""]))] #remove empty entry
78 list = unique(gsub("-.+", "", list)) #Remove isoform accession number (e.g. "-2")
79 return(list)
80 }
81
82 geneID_to_kegg <- function(vector,species){
83 vector <- sapply(vector, function(x) paste(species,x,sep=":"),USE.NAMES = F)
84 return (vector)
85 }
86
87 to_keggID <- function(id_list,id_type){
88 if (id_type == "ncbi-geneid") {
89 id_list <- unique(geneID_to_kegg(id_list,args$species))
90 } else if (id_type=="uniprot"){
91 id_list <- unique(sapply(id_list, function(x) paste(id_type,":",x,sep=""),USE.NAMES = F))
92 if (length(id_list)>250){
93 id_list <- split(id_list, ceiling(seq_along(id_list)/250))
94 id_list <- sapply(id_list, function(x) keggConv("genes",x))
95 id_list <- unique(unlist(id_list))
96 } else {
97 id_list <- unique(keggConv("genes", id_list))
98 }
99 } else if (id_type=="kegg-id") {
100 id_list <- unique(id_list)
101 }
102 return (id_list)
103 }
104
105 #take data frame, return data frame
106 split_ids_per_line <- function(line,ncol){
107
108 #print (line)
109 header = colnames(line)
110 line[ncol] = gsub("[[:blank:]]|\u00A0","",line[ncol])
111
112 if (length(unlist(strsplit(as.character(line[ncol]),";")))>1) {
113 if (length(line)==1 ) {
114 lines = as.data.frame(unlist(strsplit(as.character(line[ncol]),";")),stringsAsFactors = F)
115 } else {
116 if (ncol==1) { #first column
117 lines = suppressWarnings(cbind(unlist(strsplit(as.character(line[ncol]),";")), line[2:length(line)]))
118 } else if (ncol==length(line)) { #last column
119 lines = suppressWarnings(cbind(line[1:ncol-1],unlist(strsplit(as.character(line[ncol]),";"))))
120 } else {
121 lines = suppressWarnings(cbind(line[1:ncol-1], unlist(strsplit(as.character(line[ncol]),";"),use.names = F), line[(ncol+1):length(line)]))
122 }
123 }
124 colnames(lines)=header
125 return(lines)
126 } else {
127 return(line)
128 }
129 }
130
131 #create new lines if there's more than one id per cell in the columns in order to have only one id per line
132 one_id_one_line <-function(tab,ncol){
133
134 if (ncol(tab)>1){
135
136 tab[,ncol] = sapply(tab[,ncol],function(x) gsub("[[:blank:]]","",x))
137 header=colnames(tab)
138 res=as.data.frame(matrix(ncol=ncol(tab),nrow=0))
139 for (i in 1:nrow(tab) ) {
140 lines = split_ids_per_line(tab[i,],ncol)
141 res = rbind(res,lines)
142 }
143 }else {
144 res = unlist(sapply(tab[,1],function(x) strsplit(x,";")),use.names = F)
145 res = data.frame(res[which(!is.na(res[res!=""]))],stringsAsFactors = F)
146 colnames(res)=colnames(tab)
147 }
148 return(res)
149 }
150
151 kegg_mapping<- function(kegg_id_list,id_type,ref_ids) {
152
153 #mapping
154 map<-lapply(ref_ids, is.element, unique(kegg_id_list))
155 names(map) <- sapply(names(map), function(x) gsub("path:","",x),USE.NAMES = FALSE) #remove the prefix "path:"
156
157 in.path<-sapply(map, function(x) length(which(x==TRUE)))
158 tot.path<-sapply(map, length)
159
160 ratio <- (as.numeric(in.path[which(in.path!=0)])) / (as.numeric(tot.path[which(in.path!=0)]))
161 ratio <- as.numeric(format(round(ratio*100, 2), nsmall = 2))
162
163 ##useful but LONG
164 ## to do before : in step 1
165 path.names<-names(in.path[which(in.path!=0)])
166 name <- sapply(path.names, function(x) keggGet(x)[[1]]$NAME,USE.NAMES = FALSE)
167
168 res<-data.frame(I(names(in.path[which(in.path!=0)])), I(name), ratio, as.numeric(in.path[which(in.path!=0)]), as.numeric(tot.path[which(in.path!=0)]))
169 res <- res[order(as.numeric(res[,3]),decreasing = TRUE),]
170 colnames(res)<-c("pathway_ID", "Description" , "Ratio IDs mapped/total IDs (%)" ,"nb KEGG genes IDs mapped in the pathway", "nb total of KEGG genes IDs present in the pathway")
171
172 return(res)
173
174 }
175
176 #get args from command line
177 args <- get_args()
178
179 #save(args,file="/home/dchristiany/proteore_project/ProteoRE/tools/kegg_identification/args.Rda")
180 #load("/home/dchristiany/proteore_project/ProteoRE/tools/kegg_identification/args.Rda")
181
182 ###setting variables
183 header = str2bool(args$header)
184 if (!is.null(args$id_list)) {id_list <- get_list_from_cp(args$id_list)} #get ids from copy/paste input
185 if (!is.null(args$input)) { #get ids from input file
186 csv <- read_file(args$input,header)
187 ncol <- as.numeric(gsub("c", "" ,args$id_column))
188 csv <- one_id_one_line(csv,ncol)
189 id_list <- as.vector(csv[,ncol])
190 id_list <- unique(id_list[which(!is.na(id_list[id_list!=""]))])
191 }
192
193 #convert to keggID if needed
194 id_list <- to_keggID(id_list,args$id_type)
195
196 #get pathways of species with associated KEGG ID genes
197 pathways_list <- get_pathways_list(args$species)
198
199 #mapping on pathways
200 res <- kegg_mapping(id_list,args$id_type,pathways_list)
201 if (nrow(res) > as.numeric(args$nb_pathways)) { res <- res[1:args$nb_pathways,] }
202
203 write.table(res, file=args$output, quote=FALSE, sep='\t',row.names = FALSE, col.names = TRUE)
204