comparison add_expression_HPA.R @ 9:5c260bd3552e draft

planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
author proteore
date Tue, 18 Dec 2018 08:23:48 -0500
parents c9943f867413
children dbeabf9bf091
comparison
equal deleted inserted replaced
8:2df5166efebb 9:5c260bd3552e
1 # Read file and return file content as data.frame 1 # Read file and return file content as data.frame
2 readfile = function(filename, header) { 2 read_file <- function(path,header){
3 if (header == "true") { 3 file <- try(read.csv(path,header=header, sep="\t",stringsAsFactors = FALSE, quote="\"", check.names = F),silent=TRUE)
4 # Read only first line of the file as header: 4 if (inherits(file,"try-error")){
5 headers <- read.table(filename, nrows = 1, header = FALSE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE, na.strings=c("", "NA"), blank.lines.skip = TRUE, quote = "", comment.char = "") 5 stop("File not found !")
6 #Read the data of the files (skipping the first row) 6 }else{
7 file <- read.table(filename, skip = 1, header = FALSE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE, na.strings=c("", "NA"), blank.lines.skip = TRUE, quote = "", comment.char = "") 7 return(file)
8 # Remove empty rows
9 file <- file[!apply(is.na(file) | file == "", 1, all), , drop=FALSE]
10 #And assign the header to the data
11 names(file) <- headers
12 } 8 }
13 else { 9 }
14 file <- read.table(filename, header = FALSE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE, na.strings=c("", "NA"), blank.lines.skip = TRUE, quote = "", comment.char = "") 10
15 # Remove empty rows 11 #convert a string to boolean
16 file <- file[!apply(is.na(file) | file == "", 1, all), , drop=FALSE] 12 str2bool <- function(x){
13 if (any(is.element(c("t","true"),tolower(x)))){
14 return (TRUE)
15 }else if (any(is.element(c("f","false"),tolower(x)))){
16 return (FALSE)
17 }else{
18 return(NULL)
17 } 19 }
18 return(file)
19 } 20 }
20 21
21 add_expression = function(input, atlas, options) { 22 add_expression = function(input, atlas, options) {
23 input <- unique(input[!is.na(input)])
24 input <- gsub("[[:blank:]]|\u00A0","",input)
22 if (all(!input %in% atlas$Ensembl)) { 25 if (all(!input %in% atlas$Ensembl)) {
23 return(NULL) 26 return(NULL)
24 } 27 } else {
25 else { 28 res = atlas[match(input,atlas$Ensembl),c("Ensembl",options)]
26 res = matrix(nrow=length(input), ncol=0) 29 res = res[which(!is.na(res[,1])),]
27 names = c() 30 row.names(res)=res[,1]
28 for (opt in options) { 31 res=res[2:ncol(res)]
29 names = c(names, opt) 32 res <- as.data.frame(apply(res, c(1,2), function(x) gsub("^$|^ $", NA, x))) #convert "" et " " to NA
30 info = atlas[match(input, atlas$Ensembl,incomparable="NA"),][opt][,]
31 res = cbind(res, info)
32 }
33 colnames(res) = names
34 return(res) 33 return(res)
35 } 34 }
35 }
36
37 order_columns <- function (df,ncol){
38 if (ncol==1){ #already at the right position
39 return (df)
40 } else {
41 df = df[,c(2:ncol,1,(ncol+1):dim.data.frame(df)[2])]
42 }
43 return (df)
44 }
45
46 #take data frame, return data frame
47 split_ids_per_line <- function(line,ncol){
48
49 #print (line)
50 header = colnames(line)
51 line[ncol] = gsub("[[:blank:]]","",line[ncol])
52
53 if (length(unlist(strsplit(as.character(line[ncol]),";")))>1) {
54 if (length(line)==1 ) {
55 lines = as.data.frame(unlist(strsplit(as.character(line[ncol]),";")),stringsAsFactors = F)
56 } else {
57 if (ncol==1) { #first column
58 lines = suppressWarnings(cbind(unlist(strsplit(as.character(line[ncol]),";")), line[2:length(line)]))
59 } else if (ncol==length(line)) { #last column
60 lines = suppressWarnings(cbind(line[1:ncol-1],unlist(strsplit(as.character(line[ncol]),";"))))
61 } else {
62 lines = suppressWarnings(cbind(line[1:ncol-1], unlist(strsplit(as.character(line[ncol]),";"),use.names = F), line[(ncol+1):length(line)]))
63 }
64 }
65 colnames(lines)=header
66 return(lines)
67 } else {
68 return(line)
69 }
70 }
71
72 #create new lines if there's more than one id per cell in the columns in order to have only one id per line
73 one_id_one_line <-function(tab,ncol){
74
75 if (ncol(tab)>1){
76
77 tab[,ncol] = sapply(tab[,ncol],function(x) gsub("[[:blank:]]","",x))
78 header=colnames(tab)
79 res=as.data.frame(matrix(ncol=ncol(tab),nrow=0))
80 for (i in 1:nrow(tab) ) {
81 lines = split_ids_per_line(tab[i,],ncol)
82 res = rbind(res,lines)
83 }
84 }else {
85 res = unlist(sapply(tab[,1],function(x) strsplit(x,";")),use.names = F)
86 res = data.frame(res[which(!is.na(res[res!=""]))],stringsAsFactors = F)
87 colnames(res)=colnames(tab)
88 }
89 return(res)
36 } 90 }
37 91
38 main = function() { 92 main = function() {
39 args <- commandArgs(TRUE) 93 args <- commandArgs(TRUE)
40 if(length(args)<1) { 94 if(length(args)<1) {
61 parseArgs <- function(x) strsplit(sub("^--", "", x), "=") 115 parseArgs <- function(x) strsplit(sub("^--", "", x), "=")
62 argsDF <- as.data.frame(do.call("rbind", parseArgs(args))) 116 argsDF <- as.data.frame(do.call("rbind", parseArgs(args)))
63 args <- as.list(as.character(argsDF$V2)) 117 args <- as.list(as.character(argsDF$V2))
64 names(args) <- argsDF$V1 118 names(args) <- argsDF$V1
65 119
120 #save(args,file="/home/dchristiany/proteore_project/ProteoRE/tools/add_expression_data_HPA/args.rda")
121 #load("/home/dchristiany/proteore_project/ProteoRE/tools/add_expression_data_HPA/args.rda")
122
66 inputtype = args$inputtype 123 inputtype = args$inputtype
67 if (inputtype == "copypaste") { 124 if (inputtype == "copypaste") {
68 input = strsplit(args$input, "[ \t\n]+")[[1]] 125 input = strsplit(args$input, "[ \t\n]+")[[1]]
69 } 126 } else if (inputtype == "tabfile") {
70 else if (inputtype == "tabfile") {
71 filename = args$input 127 filename = args$input
72 ncol = args$column 128 ncol = args$column
73 # Check ncol 129 # Check ncol
74 if (! as.numeric(gsub("c", "", ncol)) %% 1 == 0) { 130 if (! as.numeric(gsub("c", "", ncol)) %% 1 == 0) {
75 stop("Please enter an integer for level") 131 stop("Please enter an integer for level")
76 } 132 } else {
77 else {
78 ncol = as.numeric(gsub("c", "", ncol)) 133 ncol = as.numeric(gsub("c", "", ncol))
79 } 134 }
80 header = args$header 135 header = str2bool(args$header)
81 # Get file content 136 file = read_file(filename, header)
82 file = readfile(filename, header) 137 file = one_id_one_line(file,ncol)
83 # Extract Protein IDs list 138 input = unlist(sapply(as.character(file[,ncol]),function(x) rapply(strsplit(x,";"),c),USE.NAMES = FALSE))
84 input = c() 139 input = input[which(!is.na(input))]
85 for (row in as.character(file[,ncol])) {
86 input = c(input, strsplit(row, ";")[[1]][1])
87 }
88 } 140 }
89 141
90 # Read protein atlas 142 # Read protein atlas
91 protein_atlas = args$atlas 143 protein_atlas = args$atlas
92 protein_atlas = readfile(protein_atlas, "true") 144 protein_atlas = read_file(protein_atlas, T)
93 145
94 # Add expression 146 # Add expression
95 output = args$output 147 output = args$output
96 names = c()
97 options = strsplit(args$select, ",")[[1]] 148 options = strsplit(args$select, ",")[[1]]
98 res = add_expression(input, protein_atlas, options) 149 res = add_expression(input, protein_atlas, options)
99 150
100 # Write output 151 # Write output
101 if (is.null(res)) { 152 if (is.null(res)) {
102 write.table("None of the input ENSG ids are can be found in HPA data file",file=output,sep="\t",quote=FALSE,col.names=TRUE,row.names=FALSE) 153 write.table("None of the input ENSG ids are can be found in HPA data file",file=output,sep="\t",quote=FALSE,col.names=TRUE,row.names=FALSE)
154 } else {
155 if (inputtype == "copypaste") {
156 input <- data.frame(input)
157 output_content = merge(input,res,by.x=1,by.y="row.names",incomparables = NA, all.x=T)
158 colnames(output_content)[1] = "Ensembl"
159 } else if (inputtype == "tabfile") {
160 output_content = merge(file, res, by.x=ncol, by.y="row.names", incomparables = NA, all.x=T)
161 output_content = order_columns(output_content,ncol)
162 }
163 output_content <- as.data.frame(apply(output_content, c(1,2), function(x) gsub("^$|^ $", NA, x)))
164 write.table(output_content, output, row.names = FALSE, sep = "\t", quote = FALSE)
103 } 165 }
104 else {
105 if (inputtype == "copypaste") {
106 names = c("Ensembl", colnames(res))
107 res = cbind(as.matrix(input), res)
108 colnames(res) = names
109 write.table(res, output, row.names = FALSE, sep = "\t", quote = FALSE)
110 }
111 else if (inputtype == "tabfile") {
112 names = c(names(file), colnames(res))
113 output_content = cbind(file, res)
114 colnames(output_content) = names
115 write.table(output_content, output, row.names = FALSE, sep = "\t", quote = FALSE)
116 }
117 }
118
119 } 166 }
120 167
121 main() 168 main()