comparison add_protein_features_mouse.R @ 0:1e856941a888 draft

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