Mercurial > repos > proteore > proteore_ms_observation_pepatlas
comparison Get_ms-ms_observations.R @ 2:6ab9d2778f04 draft
planemo upload commit bdd7e8a1f08c11db2a9f1b6db5535c6d32153b2b
author | proteore |
---|---|
date | Tue, 18 Dec 2018 09:50:50 -0500 |
parents | |
children | e77c0f3e9bab |
comparison
equal
deleted
inserted
replaced
1:23671dd35026 | 2:6ab9d2778f04 |
---|---|
1 # Read file and return file content as data.frame | |
2 read_file <- function(path,header){ | |
3 file <- try(read.csv(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 return(file) | |
8 } | |
9 } | |
10 | |
11 str2bool <- function(x){ | |
12 if (any(is.element(c("t","true"),tolower(x)))){ | |
13 return (TRUE) | |
14 }else if (any(is.element(c("f","false"),tolower(x)))){ | |
15 return (FALSE) | |
16 }else{ | |
17 return(NULL) | |
18 } | |
19 } | |
20 | |
21 nb_obs_PeptideAtlas <- function(input, atlas_file) { | |
22 ## Calculate the sum of n_observations for each ID in input | |
23 atlas = read_file(atlas_file, T) | |
24 return(atlas$nb_obs[match(input,atlas$Uniprot_AC)]) | |
25 } | |
26 | |
27 main = function() { | |
28 args <- commandArgs(TRUE) | |
29 if(length(args)<1) { | |
30 args <- c("--help") | |
31 } | |
32 | |
33 # Help section | |
34 if("--help" %in% args) { | |
35 cat("Selection and Annotation HPA | |
36 Arguments: | |
37 --input_type: type of input (list of id or filename) | |
38 --input: input | |
39 --atlas: list of file(s) path to use | |
40 --output: text output filename \n") | |
41 q(save="no") | |
42 } | |
43 | |
44 # Parse arguments | |
45 parseArgs <- function(x) strsplit(sub("^--", "", x), "=") | |
46 argsDF <- as.data.frame(do.call("rbind", parseArgs(args))) | |
47 args <- as.list(as.character(argsDF$V2)) | |
48 names(args) <- argsDF$V1 | |
49 | |
50 #save(args,file="/home/dchristiany/proteore_project/ProteoRE/tools/retrieve_msbased_pepatlas/args.Rda") | |
51 #load("/home/dchristiany/proteore_project/ProteoRE/tools/retrieve_msbased_pepatlas/args.Rda") | |
52 | |
53 # Extract input | |
54 input_type = args$input_type | |
55 if (input_type == "list") { | |
56 input = strsplit(args$input, "[ \t\n]+")[[1]] | |
57 } else if (input_type == "file") { | |
58 filename = args$input | |
59 ncol = args$column | |
60 # Check ncol | |
61 if (! as.numeric(gsub("c", "", ncol)) %% 1 == 0) { | |
62 stop("Please enter an integer for level") | |
63 } else { | |
64 ncol = as.numeric(gsub("c", "", ncol)) | |
65 } | |
66 header = str2bool(args$header) | |
67 file = read_file(filename, header) | |
68 input = sapply(file[,ncol],function(x) strsplit(as.character(x),";")[[1]][1],USE.NAMES = F) | |
69 } | |
70 | |
71 output = args$output | |
72 | |
73 #function to create a list of infos from file path | |
74 extract_info_from_path <- function(path) { | |
75 file_name=strsplit(tail(strsplit(path,"/")[[1]],n=1),"\\.")[[1]][1] | |
76 date=tail(strsplit(file_name,"_")[[1]],n=1) | |
77 tissue=paste(strsplit(file_name,"_")[[1]][1:2],collapse="_") | |
78 return (c(date,tissue,file_name,path)) | |
79 } | |
80 | |
81 #data_frame building | |
82 paths=strsplit(args$atlas,",")[[1]] | |
83 tmp <- sapply(paths, extract_info_from_path,USE.NAMES = FALSE) | |
84 df <- as.data.frame(t(as.data.frame(tmp)),row.names = c(""),stringsAsFactors = FALSE) | |
85 names(df) <- c("date","tissue","filename","path") | |
86 | |
87 # Annotations | |
88 res = sapply(df$path, function(x) nb_obs_PeptideAtlas(input, x), USE.NAMES = FALSE) | |
89 | |
90 colnames(res)=df$filename | |
91 | |
92 # Write output | |
93 if (input_type == "list") { | |
94 res = cbind(as.matrix(input), res) | |
95 colnames(res)[1] = "Uniprot accession number" | |
96 } else if (input_type == "file") { | |
97 res = cbind(file, res) | |
98 } | |
99 res = as.data.frame(apply(res, c(1,2), function(x) gsub("^$|^ $", NA, x))) | |
100 write.table(res, output, row.names = FALSE, sep = "\t", quote = FALSE) | |
101 | |
102 } | |
103 | |
104 main() | |
105 #Rscript retrieve_peptideatlas.R --input_type="file" --input="test-data/FKW_Lacombe_et_al_2017_OK.txt" --atlas_brain="Human_Brain_201803_PeptideAtlas.txt" --column="c1" --header="true" --output="test-data/PeptideAtlas_output.txt" --atlas_urine="Human_Urine_201803_PeptideAtlas.txt" --atlas="brain,urine" | |
106 |