annotate add_expression_HPA.R @ 12:dbeabf9bf091 draft

planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
author proteore
date Thu, 07 Mar 2019 09:08:33 -0500
parents 5c260bd3552e
children 133309fd6875
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
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
149 main = function() {
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
150
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
151 args = get_args()
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
152
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
153 #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
154 #load("/home/dchristiany/proteore_project/ProteoRE/tools/add_expression_data_HPA/args.rda")
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
155
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
156 inputtype = args$inputtype
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
157 if (inputtype == "copypaste") {
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
158 ids = strsplit(args$input, "[ \t\n]+")[[1]]
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
159 } else if (inputtype == "tabfile") {
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
160 filename = args$input
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
161 ncol = args$column
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
162 # Check ncol
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
163 if (! as.numeric(gsub("c", "", ncol)) %% 1 == 0) {
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
164 stop("Please enter an integer for level")
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
165 } else {
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
166 ncol = as.numeric(gsub("c", "", ncol))
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
167 }
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
168 header = str2bool(args$header)
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
169 file = read_file(filename, header)
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
170 is_col_in_file(file,ncol)
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
171 file = one_id_one_line(file,ncol)
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
172 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
173 ids = ids[which(!is.na(ids))]
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
174 }
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
175 check_ensembl_geneids(ids)
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
176
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
177 # Read protein atlas
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
178 protein_atlas = args$atlas
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
179 protein_atlas = read_file(protein_atlas, T)
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
180
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
181 # Add expression
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
182 output = args$output
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
183 options = strsplit(args$select, ",")[[1]]
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
184 res = add_expression(ids, protein_atlas, options)
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
185
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
186 # Write output
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
187 if (is.null(res)) {
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
188 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
189 } else {
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
190 if (inputtype == "copypaste") {
12
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
191 ids <- data.frame(ids)
dbeabf9bf091 planemo upload commit 51fc514a85c1055cab5bb6e76c90f3da7e648101-dirty
proteore
parents: 9
diff changeset
192 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
193 colnames(output_content)[1] = "Ensembl"
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
194 } else if (inputtype == "tabfile") {
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
195 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
196 output_content = order_columns(output_content,ncol)
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
197 }
9
5c260bd3552e planemo upload commit eb7450f36863f02f036cbc52bf5525d68f22bd9e
proteore
parents: 7
diff changeset
198 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
199 write.table(output_content, output, row.names = FALSE, sep = "\t", quote = FALSE)
6
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
200 }
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
201 }
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
202
71214d6034e7 planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents:
diff changeset
203 main()