annotate id_converter_UniProt.R @ 0:02c549457875 draft default tip

planemo upload
author lnguyen
date Fri, 15 Sep 2017 06:02:03 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
02c549457875 planemo upload
lnguyen
parents:
diff changeset
1 # Read file and return file content as data.frame
02c549457875 planemo upload
lnguyen
parents:
diff changeset
2 readfile = function(filename, header) {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
3 if (header == "true") {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
4 # Read only first line of the file as header:
02c549457875 planemo upload
lnguyen
parents:
diff changeset
5 headers <- read.table(filename, nrows = 1, header = FALSE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
6 #Read the data of the files (skipping the first row)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
7 file <- read.table(filename, skip = 1, header = FALSE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
8 #And assign the header to the data
02c549457875 planemo upload
lnguyen
parents:
diff changeset
9 names(file) <- headers
02c549457875 planemo upload
lnguyen
parents:
diff changeset
10 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
11 else {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
12 file <- read.table(filename, header = FALSE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
13 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
14 return(file)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
15 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
16
02c549457875 planemo upload
lnguyen
parents:
diff changeset
17 # Mapping IDs using file built from Uniprot file source (ftp://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/taxonomic_divisions/uniprot_sprot_human.dat.gz)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
18 # Available databases:
02c549457875 planemo upload
lnguyen
parents:
diff changeset
19 # UNIPROT_AC: Uniprot accession number (e.g. P31946)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
20 # UNIPROT_ID: Uniprot identifiers (e.g 1433B_HUMAN)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
21 # GeneID_EntrezGene: Entrez gene ID (serie of digit) (e.g. 7529)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
22 # RefSeq: RefSeq (NCBI) protein (e.g. NP_003395.1; NP_647539.1; XP_016883528.1)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
23 # GI_number: GI (NCBI GI number) ID (serie of digits) assigned to each sequence record processed by NCBI (e.g; 21328448; 377656701; 67464627; 78101741)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
24 # PDB: Protein DataBank Identifiers (e.g. 2BR9:A; 3UAL:A; 3UBW:A)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
25 # GO_ID: GOterms (Gene Ontology) Identifiers (e.g. GO:0070062; GO:0005925; GO:0042470; GO:0016020; GO:0005739; GO:0005634)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
26 # PIR: Protein Information Resource ID (e.g. S34755)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
27 # OMIM: OMIM (Online Mendelian Inheritance in Man database) ID (serie of digits) (e.g: 601289)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
28 # UniGene: Unigene Identifier (e.g. Hs.643544)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
29 # Ensembl_ENSG: Ensembl gene identifiers (e.g. ENSG00000166913)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
30 # Ensembl_ENST: Ensembl transcript identifiers (e.g. ENST00000353703; ENST00000372839)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
31 # Ensembl_ENSP: Ensembl protein identifiers (e.g. ENSP00000300161; ENSP00000361930)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
32
02c549457875 planemo upload
lnguyen
parents:
diff changeset
33 mapping = function() {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
34 # Extract arguments
02c549457875 planemo upload
lnguyen
parents:
diff changeset
35 args = commandArgs(trailingOnly = TRUE)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
36 #print(args)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
37 if (length(args) != 7) {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
38 stop("Not enough/Too many arguments", call. = FALSE)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
39 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
40 else {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
41 input_id_type = args[1]
02c549457875 planemo upload
lnguyen
parents:
diff changeset
42 list_id = args[2]
02c549457875 planemo upload
lnguyen
parents:
diff changeset
43 list_id_input_type = args[3]
02c549457875 planemo upload
lnguyen
parents:
diff changeset
44 options = strsplit(args[4], ",")[[1]]
02c549457875 planemo upload
lnguyen
parents:
diff changeset
45 output = args[5]
02c549457875 planemo upload
lnguyen
parents:
diff changeset
46 uniprot_map_file = args[6]
02c549457875 planemo upload
lnguyen
parents:
diff changeset
47 np_uniprot_file = args[7]
02c549457875 planemo upload
lnguyen
parents:
diff changeset
48
02c549457875 planemo upload
lnguyen
parents:
diff changeset
49 # Extract ID maps
02c549457875 planemo upload
lnguyen
parents:
diff changeset
50 uniprot_map = read.table(uniprot_map_file, header = TRUE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
51 np_uniprot = read.table(np_uniprot_file, header = TRUE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
52
02c549457875 planemo upload
lnguyen
parents:
diff changeset
53 # Extract input IDs
02c549457875 planemo upload
lnguyen
parents:
diff changeset
54 if (list_id_input_type == "list") {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
55 list_id = strsplit(args[2], " ")[[1]]
02c549457875 planemo upload
lnguyen
parents:
diff changeset
56 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
57 else if (list_id_input_type == "file") {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
58 filename = as.character(strsplit(list_id, ",")[[1]][1])
02c549457875 planemo upload
lnguyen
parents:
diff changeset
59 column_number = as.numeric(gsub("c", "" ,strsplit(list_id, ",")[[1]][2]))
02c549457875 planemo upload
lnguyen
parents:
diff changeset
60 header = strsplit(list_id, ",")[[1]][3]
02c549457875 planemo upload
lnguyen
parents:
diff changeset
61 file_all = readfile(filename, header)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
62 list_id = c()
02c549457875 planemo upload
lnguyen
parents:
diff changeset
63 list_id = sapply(strsplit(file_all[,column_number], ";"), "[", 1)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
64 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
65 names = c()
02c549457875 planemo upload
lnguyen
parents:
diff changeset
66
02c549457875 planemo upload
lnguyen
parents:
diff changeset
67 # Map IDs
02c549457875 planemo upload
lnguyen
parents:
diff changeset
68 res = matrix(nrow=length(list_id), ncol=0)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
69
02c549457875 planemo upload
lnguyen
parents:
diff changeset
70 for (opt in options) {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
71 names = c(names, opt)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
72 # Map to neXtProt ID
02c549457875 planemo upload
lnguyen
parents:
diff changeset
73 if (opt == "neXtProt_ID") {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
74 if (input_id_type == "UNIPROT_AC") {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
75 mapped = sapply(strsplit(np_uniprot[match(list_id, np_uniprot$Uniprot_AC),]$neXtProt_ID, ";"), "[", 1)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
76 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
77 else if (input_id_type == "neXtProt_ID") {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
78 mapped = matrix(list_id)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
79 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
80 else {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
81 uniprot = sapply(strsplit(uniprot_map[match(list_id, uniprot_map[input_id_type][,]),]$UNIPROT_AC, ";"), "[", 1)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
82 mapped = sapply(strsplit(np_uniprot[match(uniprot, np_uniprot$Uniprot_AC),]$neXtProt_ID, ";"), "[", 1)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
83 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
84 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
85 # Map to other ID types
02c549457875 planemo upload
lnguyen
parents:
diff changeset
86 else {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
87 if (input_id_type == "neXtProt_ID") {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
88 uniprot = sapply(strsplit(np_uniprot[match(list_id, np_uniprot$neXtProt_ID),]$Uniprot_AC, ";"), "[", 1)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
89 mapped = sapply(strsplit(uniprot_map[match(uniprot, uniprot_map$UNIPROT_AC),][opt][,], ";"), "[", 1)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
90 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
91 else {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
92 mapped = sapply(strsplit(uniprot_map[match(list_id, uniprot_map[input_id_type][,]),][opt][,], ";"), "[", 1)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
93 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
94 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
95 res = cbind(res, matrix(mapped))
02c549457875 planemo upload
lnguyen
parents:
diff changeset
96 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
97
02c549457875 planemo upload
lnguyen
parents:
diff changeset
98 # Write output
02c549457875 planemo upload
lnguyen
parents:
diff changeset
99 if (list_id_input_type == "list") {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
100 res = cbind(as.matrix(list_id), res)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
101 names = c(input_id_type, names)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
102 colnames(res) = names
02c549457875 planemo upload
lnguyen
parents:
diff changeset
103 write.table(res, output, row.names = FALSE, sep = "\t", quote = FALSE)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
104 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
105 else if (list_id_input_type == "file") {
02c549457875 planemo upload
lnguyen
parents:
diff changeset
106 names(res) = options
02c549457875 planemo upload
lnguyen
parents:
diff changeset
107 names = c(names(file_all), names)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
108 output_content = cbind(file_all, res)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
109 colnames(output_content) = names
02c549457875 planemo upload
lnguyen
parents:
diff changeset
110 write.table(output_content, output, row.names = FALSE, sep = "\t", quote = FALSE)
02c549457875 planemo upload
lnguyen
parents:
diff changeset
111 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
112 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
113 }
02c549457875 planemo upload
lnguyen
parents:
diff changeset
114
02c549457875 planemo upload
lnguyen
parents:
diff changeset
115 mapping()