comparison protein_features.R @ 0:b455ec3f4f33 draft

planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
author proteore
date Mon, 12 Nov 2018 11:10:16 -0500
parents
children 2fc914ab92f5
comparison
equal deleted inserted replaced
-1:000000000000 0:b455ec3f4f33
1 # Read file and return file content as data.frame
2 read_file <- function(path,header){
3 file <- try(read.table(path,header=header, sep="\t",stringsAsFactors = FALSE, quote="", check.names = F),silent=TRUE)
4 if (inherits(file,"try-error")){
5 stop("File not found !")
6 }else{
7 file <- file[!apply(is.na(file) | file == "", 1, all), , drop=FALSE]
8 return(file)
9 }
10 }
11
12 order_columns <- function (df,ncol,id_type,file){
13 if (id_type=="Uniprot_AC"){ncol=dim.data.frame(file)[2]}
14 if (ncol==1){ #already at the right position
15 return (df)
16 } else {
17 df = df[,c(2:ncol,1,(ncol+1):dim.data.frame(df)[2])]
18 }
19 return (df)
20 }
21
22 get_list_from_cp <-function(list){
23 list = strsplit(list, "[ \t\n]+")[[1]]
24 list = list[list != ""] #remove empty entry
25 list = gsub("-.+", "", list) #Remove isoform accession number (e.g. "-2")
26 return(list)
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 --nextprot: path to nextprot information file
46 --column: the column number which you would like to apply...
47 --header: true/false if your file contains a header
48 --type: the type of input IDs (Uniprot_AC/EntrezID)
49 --pc_features: IsoPoint,SeqLength,MW
50 --localization: Chr,SubcellLocations
51 --diseases_info: Diseases
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 # Get information from neXtProt
76 get_nextprot_info <- function(nextprot,input,pc_features,localization,diseases_info){
77 if(diseases_info){
78 cols = c("NextprotID",pc_features,localization,"Diseases")
79 } else {
80 cols = c("NextprotID",pc_features,localization)
81 }
82
83 cols=cols[cols!="None"]
84 info = nextprot[match(input,nextprot$NextprotID),cols]
85 return(info)
86 }
87
88 protein_features = function() {
89
90 args <- get_args()
91
92 #save(args,file="/home/dchristiany/proteore_project/ProteoRE/tools/add_human_protein_features/args.rda")
93 #load("/home/dchristiany/proteore_project/ProteoRE/tools/add_human_protein_features/args.rda")
94
95 #setting variables
96 inputtype = args$inputtype
97 if (inputtype == "copy_paste") {
98 input = get_list_from_cp(args$input)
99 input = input[input!=""]
100 } else if (inputtype == "file") {
101 filename = args$input
102 ncol = args$column
103 # Check ncol
104 if (! as.numeric(gsub("c", "", ncol)) %% 1 == 0) {
105 stop("Please enter an integer for level")
106 } else {
107 ncol = as.numeric(gsub("c", "", ncol))
108 }
109
110 header = str2bool(args$header)
111 file = read_file(filename, header) # Get file content
112 input = sapply(file[,ncol],function(x) strsplit(as.character(x),";")[[1]][1],USE.NAMES = F) # Extract Protein IDs list
113 if (args$type == "NextprotID" && ! "NextprotID" %in% colnames(file)) { colnames(file)[ncol] <- "NextprotID"
114 } else if (args$type == "NextprotID" && "NextprotID" %in% colnames(file) && match("NextprotID",colnames(file))!=ncol ) {
115 colnames(file)[match("NextprotID",colnames(file))] <- "old_NextprotID"
116 colnames(file)[ncol] = "NextprotID"
117 }
118 }
119
120 # Read reference file
121 nextprot = read_file(args$nextprot,T)
122
123 # Parse arguments
124 id_type = args$type
125 pc_features = strsplit(args$pc_features, ",")[[1]]
126 localization = strsplit(args$localization, ",")[[1]]
127 diseases_info = str2bool(args$diseases_info)
128 output = args$output
129
130 # Change the sample ids if they are Uniprot_AC ids to be able to match them with
131 # Nextprot data
132 if (id_type=="Uniprot_AC"){
133 NextprotID = gsub("^","NX_",input)
134 if (inputtype == "file" && "NextprotID" %in% colnames(file)){colnames(file)[match("NextprotID",colnames(file))] <- "old_NextprotID"}
135 file = cbind(file,NextprotID)
136 } else if (id_type=="NextprotID") {
137 if (inputtype == "file") {
138 NextprotID = file$NextprotID
139 } else {
140 NextprotID = input
141 }
142 }
143
144 # Select user input protein ids in nextprot
145 if ((length(NextprotID[NextprotID %in% nextprot[,1]]))==0){
146 write.table("None of the input ids can be found in Nextprot",file=output,sep="\t",quote=FALSE,col.names=TRUE,row.names=FALSE)
147 } else {
148 res <- get_nextprot_info(nextprot,NextprotID,pc_features,localization,diseases_info)
149
150 # Write output
151 if (inputtype == "copy_paste") {
152 if (id_type=="Uniprot_AC"){
153 output_content = cbind(input, res)
154 colnames(output_content)[1] = id_type
155 }
156 if ("res" %in% colnames(output_content)){colnames(output_content)[which(colnames(output_content)=="res")] = "NexprotID" } #if no features are selected
157 } else if (inputtype == "file") {
158 res = res[!duplicated(res$NextprotID),]
159 output_content = merge(file, res,by="NextprotID",incomparables = NA,all.x=T)
160 output_content = order_columns(output_content,ncol,id_type,file)
161 }
162 output_content <- as.data.frame(apply(output_content, c(1,2), function(x) gsub("^$|^ $", NA, x))) #convert "" et " " to NA
163 write.table(output_content, output, row.names = FALSE, sep = "\t", quote = FALSE)
164 }
165
166 }
167 protein_features()