Mercurial > repos > proteore > proteore_kegg_pathways_coverage
comparison kegg_identification.R @ 1:d600ce7f2484 draft
planemo upload commit bdd7e8a1f08c11db2a9f1b6db5535c6d32153b2b-dirty
author | proteore |
---|---|
date | Tue, 18 Dec 2018 10:00:40 -0500 |
parents | |
children | f4e32dee3b28 |
comparison
equal
deleted
inserted
replaced
0:42d0805353b6 | 1:d600ce7f2484 |
---|---|
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 = strsplit(list, "[ \t\n]+")[[1]] | |
75 list = gsub("[[:blank:]]|\u00A0|NA","",list) | |
76 list = list[which(!is.na(list[list != ""]))] #remove empty entry | |
77 list = unique(gsub("-.+", "", list)) #Remove isoform accession number (e.g. "-2") | |
78 return(list) | |
79 } | |
80 | |
81 geneID_to_kegg <- function(vector,species){ | |
82 vector <- sapply(vector, function(x) paste(species,x,sep=":"),USE.NAMES = F) | |
83 return (vector) | |
84 } | |
85 | |
86 to_keggID <- function(id_list,id_type){ | |
87 if (id_type == "ncbi-geneid") { | |
88 id_list <- unique(geneID_to_kegg(id_list,args$species)) | |
89 } else if (id_type=="uniprot"){ | |
90 id_list <- unique(sapply(id_list, function(x) paste(id_type,":",x,sep=""),USE.NAMES = F)) | |
91 if (length(id_list)>250){ | |
92 id_list <- split(id_list, ceiling(seq_along(id_list)/250)) | |
93 id_list <- sapply(id_list, function(x) keggConv("genes",x)) | |
94 id_list <- unique(unlist(id_list)) | |
95 } else { | |
96 id_list <- unique(keggConv("genes", id_list)) | |
97 } | |
98 } else if (id_type=="kegg-id") { | |
99 id_list <- unique(id_list) | |
100 } | |
101 return (id_list) | |
102 } | |
103 | |
104 #take data frame, return data frame | |
105 split_ids_per_line <- function(line,ncol){ | |
106 | |
107 #print (line) | |
108 header = colnames(line) | |
109 line[ncol] = gsub("[[:blank:]]|\u00A0","",line[ncol]) | |
110 | |
111 if (length(unlist(strsplit(as.character(line[ncol]),";")))>1) { | |
112 if (length(line)==1 ) { | |
113 lines = as.data.frame(unlist(strsplit(as.character(line[ncol]),";")),stringsAsFactors = F) | |
114 } else { | |
115 if (ncol==1) { #first column | |
116 lines = suppressWarnings(cbind(unlist(strsplit(as.character(line[ncol]),";")), line[2:length(line)])) | |
117 } else if (ncol==length(line)) { #last column | |
118 lines = suppressWarnings(cbind(line[1:ncol-1],unlist(strsplit(as.character(line[ncol]),";")))) | |
119 } else { | |
120 lines = suppressWarnings(cbind(line[1:ncol-1], unlist(strsplit(as.character(line[ncol]),";"),use.names = F), line[(ncol+1):length(line)])) | |
121 } | |
122 } | |
123 colnames(lines)=header | |
124 return(lines) | |
125 } else { | |
126 return(line) | |
127 } | |
128 } | |
129 | |
130 #create new lines if there's more than one id per cell in the columns in order to have only one id per line | |
131 one_id_one_line <-function(tab,ncol){ | |
132 | |
133 if (ncol(tab)>1){ | |
134 | |
135 tab[,ncol] = sapply(tab[,ncol],function(x) gsub("[[:blank:]]","",x)) | |
136 header=colnames(tab) | |
137 res=as.data.frame(matrix(ncol=ncol(tab),nrow=0)) | |
138 for (i in 1:nrow(tab) ) { | |
139 lines = split_ids_per_line(tab[i,],ncol) | |
140 res = rbind(res,lines) | |
141 } | |
142 }else { | |
143 res = unlist(sapply(tab[,1],function(x) strsplit(x,";")),use.names = F) | |
144 res = data.frame(res[which(!is.na(res[res!=""]))],stringsAsFactors = F) | |
145 colnames(res)=colnames(tab) | |
146 } | |
147 return(res) | |
148 } | |
149 | |
150 kegg_mapping<- function(kegg_id_list,id_type,ref_ids) { | |
151 | |
152 #mapping | |
153 map<-lapply(ref_ids, is.element, unique(kegg_id_list)) | |
154 names(map) <- sapply(names(map), function(x) gsub("path:","",x),USE.NAMES = FALSE) #remove the prefix "path:" | |
155 | |
156 in.path<-sapply(map, function(x) length(which(x==TRUE))) | |
157 tot.path<-sapply(map, length) | |
158 | |
159 ratio <- (as.numeric(in.path[which(in.path!=0)])) / (as.numeric(tot.path[which(in.path!=0)])) | |
160 ratio <- as.numeric(format(round(ratio*100, 2), nsmall = 2)) | |
161 | |
162 ##useful but LONG | |
163 ## to do before : in step 1 | |
164 path.names<-names(in.path[which(in.path!=0)]) | |
165 name <- sapply(path.names, function(x) keggGet(x)[[1]]$NAME,USE.NAMES = FALSE) | |
166 | |
167 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)])) | |
168 res <- res[order(as.numeric(res[,3]),decreasing = TRUE),] | |
169 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") | |
170 | |
171 return(res) | |
172 | |
173 } | |
174 | |
175 #get args from command line | |
176 args <- get_args() | |
177 | |
178 #save(args,file="/home/dchristiany/proteore_project/ProteoRE/tools/kegg_identification/args.Rda") | |
179 #load("/home/dchristiany/proteore_project/ProteoRE/tools/kegg_identification/args.Rda") | |
180 | |
181 ###setting variables | |
182 header = str2bool(args$header) | |
183 if (!is.null(args$id_list)) {id_list <- get_list_from_cp(args$id_list)} #get ids from copy/paste input | |
184 if (!is.null(args$input)) { #get ids from input file | |
185 csv <- read_file(args$input,header) | |
186 ncol <- as.numeric(gsub("c", "" ,args$id_column)) | |
187 csv <- one_id_one_line(csv,ncol) | |
188 id_list <- as.vector(csv[,ncol]) | |
189 id_list <- unique(id_list[which(!is.na(id_list[id_list!=""]))]) | |
190 } | |
191 | |
192 #convert to keggID if needed | |
193 id_list <- to_keggID(id_list,args$id_type) | |
194 | |
195 #get pathways of species with associated KEGG ID genes | |
196 pathways_list <- get_pathways_list(args$species) | |
197 | |
198 #mapping on pathways | |
199 res <- kegg_mapping(id_list,args$id_type,pathways_list) | |
200 if (nrow(res) > as.numeric(args$nb_pathways)) { res <- res[1:args$nb_pathways,] } | |
201 | |
202 write.table(res, file=args$output, quote=FALSE, sep='\t',row.names = FALSE, col.names = TRUE) | |
203 |