Mercurial > repos > proteore > proteore_expression_rnaseq_abbased
comparison add_expression_HPA.R @ 6:71214d6034e7 draft
planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
author | proteore |
---|---|
date | Mon, 19 Mar 2018 10:09:01 -0400 |
parents | |
children | c9943f867413 |
comparison
equal
deleted
inserted
replaced
5:f15cdeeba4b4 | 6:71214d6034e7 |
---|---|
1 # Read file and return file content as data.frame | |
2 readfile = function(filename, header) { | |
3 if (header == "true") { | |
4 # Read only first line of the file as header: | |
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 = "") | |
6 #Read the data of the files (skipping the first row) | |
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 = "") | |
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 } | |
13 else { | |
14 file <- read.table(filename, header = FALSE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE, na.strings=c("", "NA"), blank.lines.skip = TRUE, quote = "", comment.char = "") | |
15 # Remove empty rows | |
16 file <- file[!apply(is.na(file) | file == "", 1, all), , drop=FALSE] | |
17 } | |
18 return(file) | |
19 } | |
20 | |
21 add_expression = function(input, atlas, options) { | |
22 if (all(!input %in% atlas$Ensembl)) { | |
23 return(NULL) | |
24 } | |
25 else { | |
26 res = matrix(nrow=length(input), ncol=0) | |
27 names = c() | |
28 for (opt in options) { | |
29 names = c(names, opt) | |
30 info = atlas[match(input, atlas$Ensembl,incomparable="NA"),][opt][,] | |
31 res = cbind(res, info) | |
32 } | |
33 colnames(res) = names | |
34 return(res) | |
35 } | |
36 } | |
37 | |
38 main = function() { | |
39 args <- commandArgs(TRUE) | |
40 if(length(args)<1) { | |
41 args <- c("--help") | |
42 } | |
43 | |
44 # Help section | |
45 if("--help" %in% args) { | |
46 cat("Selection and Annotation HPA | |
47 Arguments: | |
48 --inputtype: type of input (list of id or filename) | |
49 --input: either a file name (e.g : input.txt) or a list of blank-separated | |
50 ENSG identifiers (e.g : ENSG00000283071 ENSG00000283072) | |
51 --atlas: path to protein atlas file | |
52 --column: the column number which you would like to apply... | |
53 --header: true/false if your file contains a header | |
54 --select: information from HPA to select, maybe: | |
55 RNA.tissue.category,Reliability..IH.,Reliability..IF. (comma-separated) | |
56 --output: text output filename \n") | |
57 q(save="no") | |
58 } | |
59 | |
60 # Parse arguments | |
61 parseArgs <- function(x) strsplit(sub("^--", "", x), "=") | |
62 argsDF <- as.data.frame(do.call("rbind", parseArgs(args))) | |
63 args <- as.list(as.character(argsDF$V2)) | |
64 names(args) <- argsDF$V1 | |
65 | |
66 inputtype = args$inputtype | |
67 if (inputtype == "copypaste") { | |
68 input = strsplit(args$input, " ")[[1]] | |
69 } | |
70 else if (inputtype == "tabfile") { | |
71 filename = args$input | |
72 ncol = args$column | |
73 # Check ncol | |
74 if (! as.numeric(gsub("c", "", ncol)) %% 1 == 0) { | |
75 stop("Please enter an integer for level") | |
76 } | |
77 else { | |
78 ncol = as.numeric(gsub("c", "", ncol)) | |
79 } | |
80 header = args$header | |
81 # Get file content | |
82 file = readfile(filename, header) | |
83 # Extract Protein IDs list | |
84 input = c() | |
85 for (row in as.character(file[,ncol])) { | |
86 input = c(input, strsplit(row, ";")[[1]][1]) | |
87 } | |
88 } | |
89 | |
90 # Read protein atlas | |
91 protein_atlas = args$atlas | |
92 protein_atlas = readfile(protein_atlas, "true") | |
93 | |
94 # Add expression | |
95 output = args$output | |
96 names = c() | |
97 options = strsplit(args$select, ",")[[1]] | |
98 res = add_expression(input, protein_atlas, options) | |
99 | |
100 # Write output | |
101 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) | |
103 } | |
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 } | |
120 | |
121 main() |