annotate add_expression_HPA.R @ 15:208b87582b4c draft default tip

"planemo upload commit 9a871575622a1318adc64dad63fa5e3a9d2a19fd"
author proteore
date Wed, 29 Apr 2020 08:51:13 -0400
parents 133309fd6875
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
1 # Read file and return file content as data.frame
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
2 read_file <- function(path,header){
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
3 file <- try(read.csv(path,header=header, sep="\t",stringsAsFactors = FALSE, quote="\"", check.names = F),silent=TRUE)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
4 if (inherits(file,"try-error")){
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
5 stop("File not found !")
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
6 }else{
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
7 return(file)
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
8 }
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
9 }
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
10
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
11 #convert a string to boolean
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
12 str2bool <- function(x){
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
13 if (any(is.element(c("t","true"),tolower(x)))){
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
14 return (TRUE)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
15 }else if (any(is.element(c("f","false"),tolower(x)))){
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
16 return (FALSE)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
17 }else{
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
18 return(NULL)
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
19 }
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
20 }
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
21
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
22 stopQuietly <- function(...) {
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
23 blankMsg <- sprintf("\r%s\r", paste(rep(" ", getOption("width")-1L), collapse=" "));
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
24 stop(simpleError(blankMsg));
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
25 } # stopQuietly()
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
26
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
27 check_ensembl_geneids <- function(vector,type) {
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
28 ensembl_geneid_pattern = "^ENS[A-Z]+[0-9]{11}$|^[A-Z]{3}[0-9]{3}[A-Za-z](-[A-Za-z])?$|^CG[0-9]+$|^[A-Z0-9]+[.][0-9]+$|^YM[A-Z][0-9]{3}[a-z][0-9]$"
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
29 res = grepl(ensembl_geneid_pattern,vector)
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
30 if (all(!res)){
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
31 cat("No Ensembl geneIDs found in entered ids")
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
32 stopQuietly()
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
33 } else if (any(!res)) {
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
34 cat(paste(sep="",collapse = " ",c(sum(!res, na.rm=TRUE),'IDs are not ENSG IDs, please check:\n')))
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
35 not_geneids <- sapply(vector[which(!res)], function(x) paste(sep="",collapse = "",x,"\n"),USE.NAMES = F)
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
36 cat(not_geneids)
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
37 }
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
38 }
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
39
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
40 add_expression = function(input, atlas, options) {
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
41 input <- unique(input[!is.na(input)])
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
42 input <- gsub("[[:blank:]]|\u00A0","",input)
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
43 if (all(!input %in% atlas$Ensembl)) {
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
44 return(NULL)
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
45 } else {
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
46 res = atlas[match(input,atlas$Ensembl),c("Ensembl",options)]
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
47 res = res[which(!is.na(res[,1])),]
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
48 row.names(res)=res[,1]
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
49 res=res[2:ncol(res)]
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
50 res <- as.data.frame(apply(res, c(1,2), function(x) gsub("^$|^ $", NA, x))) #convert "" et " " to NA
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
51 return(res)
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
52 }
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
53 }
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
54
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
55 order_columns <- function (df,ncol){
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
56 if (ncol==1){ #already at the right position
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
57 return (df)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
58 } else {
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
59 df = df[,c(2:ncol,1,(ncol+1):dim.data.frame(df)[2])]
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
60 }
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
61 return (df)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
62 }
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
63
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
64 #take data frame, return data frame
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
65 split_ids_per_line <- function(line,ncol){
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
66
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
67 #print (line)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
68 header = colnames(line)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
69 line[ncol] = gsub("[[:blank:]]","",line[ncol])
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
70
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
71 if (length(unlist(strsplit(as.character(line[ncol]),";")))>1) {
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
72 if (length(line)==1 ) {
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
73 lines = as.data.frame(unlist(strsplit(as.character(line[ncol]),";")),stringsAsFactors = F)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
74 } else {
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
75 if (ncol==1) { #first column
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
76 lines = suppressWarnings(cbind(unlist(strsplit(as.character(line[ncol]),";")), line[2:length(line)]))
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
77 } else if (ncol==length(line)) { #last column
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
78 lines = suppressWarnings(cbind(line[1:ncol-1],unlist(strsplit(as.character(line[ncol]),";"))))
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
79 } else {
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
80 lines = suppressWarnings(cbind(line[1:ncol-1], unlist(strsplit(as.character(line[ncol]),";"),use.names = F), line[(ncol+1):length(line)]))
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
81 }
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
82 }
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
83 colnames(lines)=header
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
84 return(lines)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
85 } else {
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
86 return(line)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
87 }
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
88 }
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
89
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
90 #create new lines if there's more than one id per cell in the columns in order to have only one id per line
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
91 one_id_one_line <-function(tab,ncol){
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
92
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
93 if (ncol(tab)>1){
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
94
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
95 tab[,ncol] = sapply(tab[,ncol],function(x) gsub("[[:blank:]]","",x))
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
96 header=colnames(tab)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
97 res=as.data.frame(matrix(ncol=ncol(tab),nrow=0))
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
98 for (i in 1:nrow(tab) ) {
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
99 lines = split_ids_per_line(tab[i,],ncol)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
100 res = rbind(res,lines)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
101 }
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
102 }else {
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
103 res = unlist(sapply(tab[,1],function(x) strsplit(x,";")),use.names = F)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
104 res = data.frame(res[which(!is.na(res[res!=""]))],stringsAsFactors = F)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
105 colnames(res)=colnames(tab)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
106 }
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
107 return(res)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
108 }
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
109
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
110 get_args <- function(){
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
111 args <- commandArgs(TRUE)
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
112 if(length(args)<1) {
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
113 args <- c("--help")
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
114 }
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
115
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
116 # Help section
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
117 if("--help" %in% args) {
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
118 cat("Selection and Annotation HPA
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
119 Arguments:
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
120 --inputtype: type of input (list of id or filename)
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
121 --input: either a file name (e.g : input.txt) or a list of blank-separated
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
122 ENSG identifiers (e.g : ENSG00000283071 ENSG00000283072)
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
123 --atlas: path to protein atlas file
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
124 --column: the column number which you would like to apply...
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
125 --header: true/false if your file contains a header
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
126 --select: information from HPA to select, maybe:
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
127 RNA.tissue.category,Reliability..IH.,Reliability..IF. (comma-separated)
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
128 --output: text output filename \n")
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
129 q(save="no")
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
130 }
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
131
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
132 # Parse arguments
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
133 parseArgs <- function(x) strsplit(sub("^--", "", x), "=")
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
134 argsDF <- as.data.frame(do.call("rbind", parseArgs(args)))
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
135 args <- as.list(as.character(argsDF$V2))
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
136 names(args) <- argsDF$V1
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
137
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
138 return(args)
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
139 }
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
140
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
141 is_col_in_file <- function(file,ncol) {
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
142 is_in_file = (ncol <= ncol(file) && ncol > 0)
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
143 if (!is_in_file){
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
144 cat(paste(sep = "", collapse = " ", c("Column",ncol,"not found in file") ))
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
145 stopQuietly()
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
146 }
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
147 }
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
148
14
133309fd6875 "planemo upload commit c4edbc8a0ffd86395f088cb5b6592f77db658ac7"
proteore
parents: 12
diff changeset
149 convert_to_previous_header <- function(options){
15
208b87582b4c "planemo upload commit 9a871575622a1318adc64dad63fa5e3a9d2a19fd"
proteore
parents: 14
diff changeset
150 header = c('Gene','Gene description','Evidence','Antibody','RNA tissue specificity','Reliability (IH)','Reliability (IF)','Subcellular location','RNA tissue specific NX','TPM max in non-specific')
208b87582b4c "planemo upload commit 9a871575622a1318adc64dad63fa5e3a9d2a19fd"
proteore
parents: 14
diff changeset
151 names(header) = c('Gene','Gene description','Evidence','Antibody','RNA tissue category','Reliability (IH)','Reliability (IF)','Subcellular location','RNA TS TPM','TPM max in non-specific')
14
133309fd6875 "planemo upload commit c4edbc8a0ffd86395f088cb5b6592f77db658ac7"
proteore
parents: 12
diff changeset
152 options = names(header[which(header %in% options)])
133309fd6875 "planemo upload commit c4edbc8a0ffd86395f088cb5b6592f77db658ac7"
proteore
parents: 12
diff changeset
153 return(options)
133309fd6875 "planemo upload commit c4edbc8a0ffd86395f088cb5b6592f77db658ac7"
proteore
parents: 12
diff changeset
154 }
133309fd6875 "planemo upload commit c4edbc8a0ffd86395f088cb5b6592f77db658ac7"
proteore
parents: 12
diff changeset
155
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
156 main = function() {
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
157
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
158 args = get_args()
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
159
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
160 #save(args,file="/home/dchristiany/proteore_project/ProteoRE/tools/add_expression_data_HPA/args.rda")
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
161 #load("/home/dchristiany/proteore_project/ProteoRE/tools/add_expression_data_HPA/args.rda")
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
162
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
163 inputtype = args$inputtype
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
164 if (inputtype == "copypaste") {
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
165 ids = strsplit(args$input, "[ \t\n]+")[[1]]
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
166 } else if (inputtype == "tabfile") {
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
167 filename = args$input
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
168 ncol = args$column
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
169 # Check ncol
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
170 if (! as.numeric(gsub("c", "", ncol)) %% 1 == 0) {
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
171 stop("Please enter an integer for level")
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
172 } else {
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
173 ncol = as.numeric(gsub("c", "", ncol))
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
174 }
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
175 header = str2bool(args$header)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
176 file = read_file(filename, header)
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
177 is_col_in_file(file,ncol)
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
178 file = one_id_one_line(file,ncol)
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
179 ids = unlist(sapply(as.character(file[,ncol]),function(x) rapply(strsplit(x,";"),c),USE.NAMES = FALSE))
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
180 ids = ids[which(!is.na(ids))]
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
181 }
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
182 check_ensembl_geneids(ids)
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
183
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
184 # Read protein atlas
14
133309fd6875 "planemo upload commit c4edbc8a0ffd86395f088cb5b6592f77db658ac7"
proteore
parents: 12
diff changeset
185 protein_atlas = read_file(args$atlas, T)
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
186
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
187 # Add expression
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
188 output = args$output
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
189 options = strsplit(args$select, ",")[[1]]
14
133309fd6875 "planemo upload commit c4edbc8a0ffd86395f088cb5b6592f77db658ac7"
proteore
parents: 12
diff changeset
190 if (tail(unlist(strsplit(args$atlas,"/")),1) == "HPA_full_atlas_23-10-2018.tsv"){
133309fd6875 "planemo upload commit c4edbc8a0ffd86395f088cb5b6592f77db658ac7"
proteore
parents: 12
diff changeset
191 options = convert_to_previous_header(options)
133309fd6875 "planemo upload commit c4edbc8a0ffd86395f088cb5b6592f77db658ac7"
proteore
parents: 12
diff changeset
192 } else {
133309fd6875 "planemo upload commit c4edbc8a0ffd86395f088cb5b6592f77db658ac7"
proteore
parents: 12
diff changeset
193 options = options[which(options != 'TPM max in non-specific')]
133309fd6875 "planemo upload commit c4edbc8a0ffd86395f088cb5b6592f77db658ac7"
proteore
parents: 12
diff changeset
194 }
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
195 res = add_expression(ids, protein_atlas, options)
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
196
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
197 # Write output
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
198 if (is.null(res)) {
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
199 write.table("None of the ENSG ids entered can be found in HPA data file",file=output,sep="\t",quote=FALSE,col.names=TRUE,row.names=FALSE)
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
200 } else {
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
201 if (inputtype == "copypaste") {
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
202 ids <- data.frame(ids)
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
203 output_content = merge(ids,res,by.x=1,by.y="row.names",incomparables = NA, all.x=T)
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
204 colnames(output_content)[1] = "Ensembl"
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
205 } else if (inputtype == "tabfile") {
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
206 output_content = merge(file, res, by.x=ncol, by.y="row.names", incomparables = NA, all.x=T)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
207 output_content = order_columns(output_content,ncol)
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
208 }
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
209 output_content <- as.data.frame(apply(output_content, c(1,2), function(x) gsub("^$|^ $", NA, x)))
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
210 write.table(output_content, output, row.names = FALSE, sep = "\t", quote = FALSE)
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
211 }
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
212 }
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
213
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
214 main()