comparison add_protein_features_mouse.R @ 0:b9f57cba3172 draft

planemo upload commit e507ac5916a604c5fd7fd2af8074cc2fbb6707fe-dirty
author proteore
date Tue, 14 May 2019 09:47:13 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:b9f57cba3172
1 options(warn=-1) #TURN OFF WARNINGS !!!!!!
2
3 # Read file and return file content as data.frame
4 order_columns <- function (df,ncol,file){
5 if (ncol==1){ #already at the right position
6 return (df)
7 } else {
8 df = df[,c(2:ncol,1,(ncol+1):dim.data.frame(df)[2])]
9 }
10 return (df)
11 }
12
13 get_list_from_cp <-function(list){
14 list = gsub(";","\t",list)
15 list = strsplit(list, "[ \t\n]+")[[1]]
16 list = gsub("NA","",list)
17 list = list[list != ""] #remove empty entry
18 list = gsub("-.+", "", list) #Remove isoform accession number (e.g. "-2")
19 return(list)
20 }
21
22 check_ids <- function(vector,type) {
23 uniprot_pattern = "^([OPQ][0-9][A-Z0-9]{3}[0-9]|[A-NR-Z][0-9]([A-Z][A-Z0-9]{2}[0-9]){1,2})$"
24 entrez_id = "^([0-9]+|[A-Z]{1,2}_[0-9]+|[A-Z]{1,2}_[A-Z]{1,4}[0-9]+)$"
25 if (type == "entrez")
26 return(grepl(entrez_id,vector))
27 else if (type == "uniprot") {
28 return(grepl(uniprot_pattern,vector))
29 }
30 }
31
32 get_args <- function(){
33
34 ## Collect arguments
35 args <- commandArgs(TRUE)
36
37 ## Default setting when no arguments passed
38 if(length(args) < 1) {
39 args <- c("--help")
40 }
41
42 ## Help section
43 if("--help" %in% args) {
44 cat("Selection and Annotation HPA
45 Arguments:
46 --inputtype: type of input (list of id or filename)
47 --input: input
48 --uniprot_file: path to uniprot reference file
49 --column: the column number which you would like to apply...
50 --header: true/false if your file contains a header
51 --pc_features: IsoPoint,SeqLength,MW
52 --output: text output filename \n")
53
54 q(save="no")
55 }
56
57 parseArgs <- function(x) strsplit(sub("^--", "", x), "=")
58 argsDF <- as.data.frame(do.call("rbind", parseArgs(args)))
59 args <- as.list(as.character(argsDF$V2))
60 names(args) <- argsDF$V1
61
62 return(args)
63 }
64
65 str2bool <- function(x){
66 if (any(is.element(c("t","true"),tolower(x)))){
67 return (TRUE)
68 }else if (any(is.element(c("f","false"),tolower(x)))){
69 return (FALSE)
70 }else{
71 return(NULL)
72 }
73 }
74
75 #take data frame, return data frame
76 split_ids_per_line <- function(line,ncol){
77
78 #print (line)
79 header = colnames(line)
80 line[ncol] = gsub("[[:blank:]]|\u00A0","",line[ncol])
81
82 if (length(unlist(strsplit(as.character(line[ncol]),";")))>1) {
83 if (length(line)==1 ) {
84 lines = as.data.frame(unlist(strsplit(as.character(line[ncol]),";")),stringsAsFactors = F)
85 } else {
86 if (ncol==1) { #first column
87 lines = suppressWarnings(cbind(unlist(strsplit(as.character(line[ncol]),";")), line[2:length(line)]))
88 } else if (ncol==length(line)) { #last column
89 lines = suppressWarnings(cbind(line[1:ncol-1],unlist(strsplit(as.character(line[ncol]),";"))))
90 } else {
91 lines = suppressWarnings(cbind(line[1:ncol-1], unlist(strsplit(as.character(line[ncol]),";"),use.names = F), line[(ncol+1):length(line)]))
92 }
93 }
94 colnames(lines)=header
95 return(lines)
96 } else {
97 return(line)
98 }
99 }
100
101 #create new lines if there's more than one id per cell in the columns in order to have only one id per line
102 one_id_one_line <-function(tab,ncol){
103
104 if (ncol(tab)>1){
105
106 tab[,ncol] = sapply(tab[,ncol],function(x) gsub("[[:blank:]]","",x))
107 header=colnames(tab)
108 res=as.data.frame(matrix(ncol=ncol(tab),nrow=0))
109 for (i in 1:nrow(tab) ) {
110 lines = split_ids_per_line(tab[i,],ncol)
111 res = rbind(res,lines)
112 }
113 }else {
114 res = unlist(sapply(tab[,1],function(x) strsplit(x,";")),use.names = F)
115 res = data.frame(res[which(!is.na(res[res!=""]))],stringsAsFactors = F)
116 colnames(res)=colnames(tab)
117 }
118 return(res)
119 }
120
121 # Get information from neXtProt
122 get_uniprot_info <- function(ids,pc_features,uniprot_file){
123
124 cols = c("Entry",pc_features)
125 cols=cols[cols!="None"]
126 info = uniprot_file[match(ids,uniprot_file$Entry),cols]
127 colnames(info)[1]="UniProt-AC"
128
129 return(info)
130 }
131
132 main <- function() {
133
134 args <- get_args()
135
136 #save(args,file="/home/dchristiany/proteore_project/ProteoRE/tools/add_protein_features_mouse/args.rda")
137 #load("/home/dchristiany/proteore_project/ProteoRE/tools/add_protein_features_mouse/args.rda")
138
139 #setting variables
140 inputtype = args$inputtype
141 if (inputtype == "copy_paste") {
142 ids = get_list_from_cp(args$input)
143
144 #Check for UniProt-AC ids
145 if (all(!check_ids(ids,'uniprot'))){
146 stop ("No UniProt-AC found in ids.")
147 } else if (any(!check_ids(ids,'uniprot'))) {
148 print ('Some ids in ids are not uniprot-AC:')
149 print (ids[which(!check_ids(ids,'uniprot'))])
150 }
151 file = data.frame(ids,stringsAsFactors = F)
152 ncol=1
153
154 } else if (inputtype == "file") {
155 filename = args$input
156 ncol = args$column
157 # Check ncol
158 if (! as.numeric(gsub("c", "", ncol)) %% 1 == 0) {
159 stop("Please enter an integer for level")
160 } else {
161 ncol = as.numeric(gsub("c", "", ncol))
162 }
163
164 header = str2bool(args$header)
165 file = read.table(filename, header = header , sep = "\t", fill = TRUE, stringsAsFactors = FALSE, quote="", check.names = F, comment.char = "") # Get file content
166 if (any(grep(";",file[,ncol]))) {file = one_id_one_line(file,ncol)}
167 ids=file[,ncol]
168 }
169
170 # Read reference file
171 uniprot_file <- read.table(args$uniprot_file, header = TRUE , sep = "\t", fill = TRUE,stringsAsFactors = FALSE, quote="", check.names = F, comment.char = "")
172
173 # Parse arguments
174 pc_features = gsub("__ob__","[",gsub("__cb__","]",strsplit(args$pc_features, ",")[[1]]))
175 output = args$output
176
177 #output file
178 res <- get_uniprot_info(ids,pc_features,uniprot_file)
179 res = res[!duplicated(res$`UniProt-AC`),]
180 output_content = merge(file, res,by.x=ncol,by.y="UniProt-AC",incomparables = NA,all.x=T)
181 output_content = order_columns(output_content,ncol,file)
182 output_content <- as.data.frame(apply(output_content, c(1,2), function(x) gsub("^$|^ $", NA, x))) #convert "" et " " to NA
183 write.table(output_content, output, row.names = FALSE, sep = "\t", quote = FALSE)
184
185 }
186
187 if(!interactive()) {
188 main()
189 }