annotate kegg_identification.R @ 4:1fb202eb3609 draft

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