Mercurial > repos > proteore > proteore_prot_features
comparison add_protein_features.R @ 9:0b46a7aead62 draft
planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
| author | proteore |
|---|---|
| date | Tue, 18 Dec 2018 08:26:55 -0500 |
| parents | |
| children | 8df559ad14a7 |
comparison
equal
deleted
inserted
replaced
| 8:2e53ba3b1697 | 9:0b46a7aead62 |
|---|---|
| 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=ncol(file)} | |
| 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 = gsub("NA","",list) | |
| 25 list = list[list != ""] #remove empty entry | |
| 26 list = gsub("-.+", "", list) #Remove isoform accession number (e.g. "-2") | |
| 27 return(list) | |
| 28 } | |
| 29 | |
| 30 get_args <- function(){ | |
| 31 | |
| 32 ## Collect arguments | |
| 33 args <- commandArgs(TRUE) | |
| 34 | |
| 35 ## Default setting when no arguments passed | |
| 36 if(length(args) < 1) { | |
| 37 args <- c("--help") | |
| 38 } | |
| 39 | |
| 40 ## Help section | |
| 41 if("--help" %in% args) { | |
| 42 cat("Selection and Annotation HPA | |
| 43 Arguments: | |
| 44 --inputtype: type of input (list of id or filename) | |
| 45 --input: input | |
| 46 --nextprot: path to nextprot information file | |
| 47 --column: the column number which you would like to apply... | |
| 48 --header: true/false if your file contains a header | |
| 49 --type: the type of input IDs (Uniprot_AC/EntrezID) | |
| 50 --pc_features: IsoPoint,SeqLength,MW | |
| 51 --localization: Chr,SubcellLocations | |
| 52 --diseases_info: Diseases | |
| 53 --output: text output filename \n") | |
| 54 | |
| 55 q(save="no") | |
| 56 } | |
| 57 | |
| 58 parseArgs <- function(x) strsplit(sub("^--", "", x), "=") | |
| 59 argsDF <- as.data.frame(do.call("rbind", parseArgs(args))) | |
| 60 args <- as.list(as.character(argsDF$V2)) | |
| 61 names(args) <- argsDF$V1 | |
| 62 | |
| 63 return(args) | |
| 64 } | |
| 65 | |
| 66 str2bool <- function(x){ | |
| 67 if (any(is.element(c("t","true"),tolower(x)))){ | |
| 68 return (TRUE) | |
| 69 }else if (any(is.element(c("f","false"),tolower(x)))){ | |
| 70 return (FALSE) | |
| 71 }else{ | |
| 72 return(NULL) | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 #take data frame, return data frame | |
| 77 split_ids_per_line <- function(line,ncol){ | |
| 78 | |
| 79 #print (line) | |
| 80 header = colnames(line) | |
| 81 line[ncol] = gsub("[[:blank:]]|\u00A0","",line[ncol]) | |
| 82 | |
| 83 if (length(unlist(strsplit(as.character(line[ncol]),";")))>1) { | |
| 84 if (length(line)==1 ) { | |
| 85 lines = as.data.frame(unlist(strsplit(as.character(line[ncol]),";")),stringsAsFactors = F) | |
| 86 } else { | |
| 87 if (ncol==1) { #first column | |
| 88 lines = suppressWarnings(cbind(unlist(strsplit(as.character(line[ncol]),";")), line[2:length(line)])) | |
| 89 } else if (ncol==length(line)) { #last column | |
| 90 lines = suppressWarnings(cbind(line[1:ncol-1],unlist(strsplit(as.character(line[ncol]),";")))) | |
| 91 } else { | |
| 92 lines = suppressWarnings(cbind(line[1:ncol-1], unlist(strsplit(as.character(line[ncol]),";"),use.names = F), line[(ncol+1):length(line)])) | |
| 93 } | |
| 94 } | |
| 95 colnames(lines)=header | |
| 96 return(lines) | |
| 97 } else { | |
| 98 return(line) | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 #create new lines if there's more than one id per cell in the columns in order to have only one id per line | |
| 103 one_id_one_line <-function(tab,ncol){ | |
| 104 | |
| 105 if (ncol(tab)>1){ | |
| 106 | |
| 107 tab[,ncol] = sapply(tab[,ncol],function(x) gsub("[[:blank:]]","",x)) | |
| 108 header=colnames(tab) | |
| 109 res=as.data.frame(matrix(ncol=ncol(tab),nrow=0)) | |
| 110 for (i in 1:nrow(tab) ) { | |
| 111 lines = split_ids_per_line(tab[i,],ncol) | |
| 112 res = rbind(res,lines) | |
| 113 } | |
| 114 }else { | |
| 115 res = unlist(sapply(tab[,1],function(x) strsplit(x,";")),use.names = F) | |
| 116 res = data.frame(res[which(!is.na(res[res!=""]))],stringsAsFactors = F) | |
| 117 colnames(res)=colnames(tab) | |
| 118 } | |
| 119 return(res) | |
| 120 } | |
| 121 | |
| 122 # Get information from neXtProt | |
| 123 get_nextprot_info <- function(nextprot,input,pc_features,localization,diseases_info){ | |
| 124 if(diseases_info){ | |
| 125 cols = c("NextprotID",pc_features,localization,"Diseases") | |
| 126 } else { | |
| 127 cols = c("NextprotID",pc_features,localization) | |
| 128 } | |
| 129 | |
| 130 cols=cols[cols!="None"] | |
| 131 info = nextprot[match(input,nextprot$NextprotID),cols] | |
| 132 return(info) | |
| 133 } | |
| 134 | |
| 135 protein_features = function() { | |
| 136 | |
| 137 args <- get_args() | |
| 138 | |
| 139 #save(args,file="/home/dchristiany/proteore_project/ProteoRE/tools/add_human_protein_features/args.rda") | |
| 140 #load("/home/dchristiany/proteore_project/ProteoRE/tools/add_human_protein_features/args.rda") | |
| 141 | |
| 142 #setting variables | |
| 143 inputtype = args$inputtype | |
| 144 if (inputtype == "copy_paste") { | |
| 145 input = get_list_from_cp(args$input) | |
| 146 file = data.frame(input,stringsAsFactors = F) | |
| 147 ncol=1 | |
| 148 } else if (inputtype == "file") { | |
| 149 filename = args$input | |
| 150 ncol = args$column | |
| 151 # Check ncol | |
| 152 if (! as.numeric(gsub("c", "", ncol)) %% 1 == 0) { | |
| 153 stop("Please enter an integer for level") | |
| 154 } else { | |
| 155 ncol = as.numeric(gsub("c", "", ncol)) | |
| 156 } | |
| 157 | |
| 158 header = str2bool(args$header) | |
| 159 file = read_file(filename, header) # Get file content | |
| 160 if (any(grep(";",file[,ncol]))) {file = one_id_one_line(file,ncol)} | |
| 161 if (args$type == "NextprotID" && ! "NextprotID" %in% colnames(file)) { colnames(file)[ncol] <- "NextprotID" | |
| 162 } else if (args$type == "NextprotID" && "NextprotID" %in% colnames(file) && match("NextprotID",colnames(file))!=ncol ) { | |
| 163 colnames(file)[match("NextprotID",colnames(file))] <- "old_NextprotID" | |
| 164 colnames(file)[ncol] = "NextprotID" | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 # Read reference file | |
| 169 nextprot = read_file(args$nextprot,T) | |
| 170 | |
| 171 # Parse arguments | |
| 172 id_type = args$type | |
| 173 pc_features = strsplit(args$pc_features, ",")[[1]] | |
| 174 localization = strsplit(args$localization, ",")[[1]] | |
| 175 diseases_info = str2bool(args$diseases_info) | |
| 176 output = args$output | |
| 177 | |
| 178 # Change the sample ids if they are Uniprot_AC ids to be able to match them with | |
| 179 # Nextprot data | |
| 180 if (id_type=="Uniprot_AC"){ | |
| 181 NextprotID = gsub("^NX_$","",gsub("^","NX_",file[,ncol])) | |
| 182 file = cbind(file,NextprotID) | |
| 183 if (inputtype=="copy_paste") {colnames(file)[1]="Uniprot-AC"} | |
| 184 ncol=ncol(file) | |
| 185 } | |
| 186 NextprotID = file[,ncol] | |
| 187 | |
| 188 #Select user input protein ids in nextprot | |
| 189 #NextprotID = unique(NextprotID[which(!is.na(NextprotID[NextprotID!=""]))]) | |
| 190 if (all(!NextprotID %in% nextprot[,1])){ | |
| 191 write.table("None of the input ids can be found in Nextprot",file=output,sep="\t",quote=FALSE,col.names=TRUE,row.names=FALSE) | |
| 192 } else { | |
| 193 res <- get_nextprot_info(nextprot,NextprotID,pc_features,localization,diseases_info) | |
| 194 res = res[!duplicated(res$NextprotID),] | |
| 195 output_content = merge(file, res,by.x=ncol,by.y="NextprotID",incomparables = NA,all.x=T) | |
| 196 output_content = order_columns(output_content,ncol,id_type,file) | |
| 197 output_content <- as.data.frame(apply(output_content, c(1,2), function(x) gsub("^$|^ $", NA, x))) #convert "" et " " to NA | |
| 198 write.table(output_content, output, row.names = FALSE, sep = "\t", quote = FALSE) | |
| 199 } | |
| 200 | |
| 201 } | |
| 202 protein_features() |
