comparison id_converter_UniProt.R @ 0:f2d0b13d9615 draft

planemo upload commit 5774fd6a5a746f36f6bf4671a51a39ea2b978300-dirty
author proteore
date Fri, 16 Feb 2018 03:16:29 -0500
parents
children 134949593a3b
comparison
equal deleted inserted replaced
-1:000000000000 0:f2d0b13d9615
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)
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)
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)
15 # Remove empty rows
16 file <- file[!apply(is.na(file) | file == "", 1, all), , drop=FALSE]
17 }
18 return(file)
19 }
20
21 # Mapping IDs using file built from
22 # - HUMAN_9606_idmapping_selected.tab
23 # Tarball downloaded from ftp://ftp.uniprot.org/pub/databases/uniprot/current_release/knowledgebase/idmapping/by_organism/
24 # - nextprot_ac_list_all.txt
25 # Downloaded from ftp://ftp.nextprot.org/pub/current_release/ac_lists/
26 # Available databases:
27 # UNIPROT_AC: Uniprot accession number (e.g. P31946)
28 # UNIPROT_ID: Uniprot identifiers (e.g 1433B_HUMAN)
29 # GeneID_EntrezGene: Entrez gene ID (serie of digit) (e.g. 7529)
30 # RefSeq: RefSeq (NCBI) protein (e.g. NP_003395.1; NP_647539.1; XP_016883528.1)
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)
32 # PDB: Protein DataBank Identifiers (e.g. 2BR9:A; 3UAL:A; 3UBW:A)
33 # GO_ID: GOterms (Gene Ontology) Identifiers (e.g. GO:0070062; GO:0005925; GO:0042470; GO:0016020; GO:0005739; GO:0005634)
34 # PIR: Protein Information Resource ID (e.g. S34755)
35 # OMIM: OMIM (Online Mendelian Inheritance in Man database) ID (serie of digits) (e.g: 601289)
36 # UniGene: Unigene Identifier (e.g. Hs.643544)
37 # Ensembl_ENSG: Ensembl gene identifiers (e.g. ENSG00000166913)
38 # Ensembl_ENST: Ensembl transcript identifiers (e.g. ENST00000353703; ENST00000372839)
39 # Ensembl_ENSP: Ensembl protein identifiers (e.g. ENSP00000300161; ENSP00000361930)
40
41 mapping = function() {
42 # Extract arguments
43 args = commandArgs(trailingOnly = TRUE)
44 #print(args)
45 if (length(args) != 6) {
46 stop("Not enough/Too many arguments", call. = FALSE)
47 }
48 else {
49 input_id_type = args[1]
50 list_id = args[2]
51 list_id_input_type = args[3]
52 options = strsplit(args[4], ",")[[1]]
53 output = args[5]
54 human_id_mapping_file = args[6]
55
56 # Extract ID maps
57 human_id_map = read.table(human_id_mapping_file, header = TRUE, sep = "\t", stringsAsFactors = FALSE, fill = TRUE, na.strings = "")
58
59 # Extract input IDs
60 if (list_id_input_type == "list") {
61 list_id = strsplit(args[2], " ")[[1]]
62 # Remove isoform accession number (e.g. "-2")
63 list_id = gsub("-.+", "", list_id)
64 }
65 else if (list_id_input_type == "file") {
66 filename = as.character(strsplit(list_id, ",")[[1]][1])
67 column_number = as.numeric(gsub("c", "" ,strsplit(list_id, ",")[[1]][2]))
68 header = strsplit(list_id, ",")[[1]][3]
69 file_all = readfile(filename, header)
70 print(class(file_all))
71 str(file_all)
72 print(class(file_all[,1]))
73 list_id = c()
74 list_id = sapply(strsplit(file_all[,column_number], ";"), "[", 1)
75 # Remove isoform accession number (e.g. "-2")
76 list_id = gsub("-.+", "", list_id)
77 }
78 names = c()
79
80 # Map IDs
81 res = matrix(nrow=length(list_id), ncol=0)
82
83 for (opt in options) {
84 names = c(names, opt)
85 mapped = human_id_map[match(list_id, human_id_map[input_id_type][,]),][opt][,]
86 res = cbind(res, matrix(mapped))
87 }
88
89 # Write output
90 if (list_id_input_type == "list") {
91 res = cbind(as.matrix(list_id), res)
92 names = c(input_id_type, names)
93 colnames(res) = names
94 write.table(res, output, row.names = FALSE, sep = "\t", quote = FALSE)
95 }
96 else if (list_id_input_type == "file") {
97 names(res) = options
98 names = c(names(file_all), names)
99 output_content = cbind(file_all, res)
100 colnames(output_content) = names
101 write.table(output_content, output, row.names = FALSE, sep = "\t", quote = FALSE)
102 }
103 }
104 }
105
106 mapping()
107
108 #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