annotate id_converter_UniProt.R @ 5:0584344186eb draft

planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
author proteore
date Tue, 20 Mar 2018 10:51:41 -0400
parents 134949593a3b
children 659f1248f535
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
1 # Read file and return file content as data.frame
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
2 readfile = function(filename, header) {
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
3 if (header == "true") {
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
4 # Read only first line of the file as header:
4
134949593a3b planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents: 0
diff changeset
5 headers <- read.table(filename, nrows = 1, header = FALSE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE, na.strings=c("", "NA"), blank.lines.skip = TRUE, quote = "")
0
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
6 #Read the data of the files (skipping the first row)
4
134949593a3b planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents: 0
diff changeset
7 file <- read.table(filename, skip = 1, header = FALSE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE, na.strings=c("", "NA"), blank.lines.skip = TRUE, quote = "")
0
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
8 # Remove empty rows
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
9 file <- file[!apply(is.na(file) | file == "", 1, all), , drop=FALSE]
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
10 #And assign the header to the data
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
11 names(file) <- headers
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
12 }
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
13 else {
4
134949593a3b planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents: 0
diff changeset
14 file <- read.table(filename, header = FALSE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE, na.strings=c("", "NA"), blank.lines.skip = TRUE, quote = "")
0
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
15 # Remove empty rows
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
16 file <- file[!apply(is.na(file) | file == "", 1, all), , drop=FALSE]
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
17 }
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
18 return(file)
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
19 }
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
20
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
21 # Mapping IDs using file built from
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
22 # - HUMAN_9606_idmapping_selected.tab
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
23 # Tarball downloaded from ftp://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/idmapping/by_organism/
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
24 # - nextprot_ac_list_all.txt
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
25 # Downloaded from ftp://ftp.nextprot.org/pub/current_release/ac_lists/
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
26 # Available databases:
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
27 # UNIPROT_AC: Uniprot accession number (e.g. P31946)
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
28 # UNIPROT_ID: Uniprot identifiers (e.g 1433B_HUMAN)
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
29 # GeneID_EntrezGene: Entrez gene ID (serie of digit) (e.g. 7529)
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
30 # RefSeq: RefSeq (NCBI) protein (e.g. NP_003395.1; NP_647539.1; XP_016883528.1)
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
31 # GI_number: GI (NCBI GI number) ID (serie of digits) assigned to each sequence record processed by NCBI (e.g; 21328448; 377656701; 67464627; 78101741)
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
32 # PDB: Protein DataBank Identifiers (e.g. 2BR9:A; 3UAL:A; 3UBW:A)
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
33 # GO_ID: GOterms (Gene Ontology) Identifiers (e.g. GO:0070062; GO:0005925; GO:0042470; GO:0016020; GO:0005739; GO:0005634)
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
34 # PIR: Protein Information Resource ID (e.g. S34755)
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
35 # OMIM: OMIM (Online Mendelian Inheritance in Man database) ID (serie of digits) (e.g: 601289)
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
36 # UniGene: Unigene Identifier (e.g. Hs.643544)
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
37 # Ensembl_ENSG: Ensembl gene identifiers (e.g. ENSG00000166913)
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
38 # Ensembl_ENST: Ensembl transcript identifiers (e.g. ENST00000353703; ENST00000372839)
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
39 # Ensembl_ENSP: Ensembl protein identifiers (e.g. ENSP00000300161; ENSP00000361930)
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
40
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
41 mapping = function() {
5
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
42 args <- commandArgs(TRUE)
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
43 if(length(args)<1) {
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
44 args <- c("--help")
0
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
45 }
5
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
46
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
47 # Help section
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
48 if("--help" %in% args) {
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
49 cat("Selection and Annotation HPA
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
50 Arguments:
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
51 --ref_file: path to reference file (human_id_mapping_file.txt)
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
52 --input_type: type of input (list of id or filename)
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
53 --id_type: type of input IDs
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
54 --input: list of IDs (text or filename)
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
55 --column_number: the column number which contains list of input IDs
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
56 --header: true/false if your file contains a header
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
57 --target_ids: target IDs to map to
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
58 --output: output filename \n")
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
59 q(save="no")
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
60 }
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
61
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
62 # Parse arguments
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
63 parseArgs <- function(x) strsplit(sub("^--", "", x), "=")
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
64 argsDF <- as.data.frame(do.call("rbind", parseArgs(args)))
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
65 args <- as.list(as.character(argsDF$V2))
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
66 names(args) <- argsDF$V1
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
67
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
68 input_id_type = args$id_type # Uniprot, ENSG....
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
69 list_id_input_type = args$input_type # list or file
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
70 options = strsplit(args$target_ids, ",")[[1]]
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
71 output = args$output
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
72 human_id_mapping_file = args$ref_file
0
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
73
5
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
74 # Extract input IDs
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
75 if (list_id_input_type == "list") {
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
76 print(args$input)
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
77 list_id = strsplit(args$input, "[ \t\n]+")[[1]]
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
78 # Remove isoform accession number (e.g. "-2")
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
79 list_id = gsub("-.+", "", list_id)
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
80 }
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
81 else if (list_id_input_type == "file") {
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
82 filename = args$input
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
83 column_number = as.numeric(gsub("c", "" ,args$column_number))
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
84 header = args$header
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
85 file_all = readfile(filename, header)
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
86 list_id = c()
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
87 list_id = sapply(strsplit(file_all[,column_number], ";"), "[", 1)
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
88 # Remove isoform accession number (e.g. "-2")
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
89 list_id = gsub("-.+", "", list_id)
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
90 }
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
91
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
92 # Extract ID maps
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
93 human_id_map = read.table(human_id_mapping_file, header = TRUE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE, na.strings = "", quote = "")
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
94
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
95 names = c()
0
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
96
5
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
97 # Map IDs
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
98 res = matrix(nrow=length(list_id), ncol=0)
0
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
99
5
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
100 for (opt in options) {
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
101 names = c(names, opt)
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
102 mapped = human_id_map[match(list_id, human_id_map[input_id_type][,]),][opt][,]
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
103 res = cbind(res, matrix(mapped))
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
104 }
0
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
105
5
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
106 # Write output
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
107 if (list_id_input_type == "list") {
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
108 res = cbind(as.matrix(list_id), res)
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
109 names = c(input_id_type, names)
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
110 colnames(res) = names
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
111 write.table(res, output, row.names = FALSE, sep = "\t", quote = FALSE)
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
112 }
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
113 else if (list_id_input_type == "file") {
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
114 names(res) = options
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
115 names = c(names(file_all), names)
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
116 output_content = cbind(file_all, res)
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
117 colnames(output_content) = names
0584344186eb planemo upload commit 6e27a0eaf8172154128960497271e8f54341acd7-dirty
proteore
parents: 4
diff changeset
118 write.table(output_content, output, row.names = FALSE, sep = "\t", quote = FALSE)
0
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
119 }
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
120 }
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
121
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
122 mapping()
f2d0b13d9615 planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
proteore
parents:
diff changeset
123
4
134949593a3b planemo upload commit 4af7ac25de19ca10b1654820e909c647a2d337b2-dirty
proteore
parents: 0
diff changeset
124 #Rscript id_converter_UniProt.R "UniProt.AC" "test-data/UnipIDs.txt,c1,false" "file" "Ensembl_PRO,Ensembl,neXtProt_ID" "test-data/output.txt" ../../utils/mapping_file.txt