Mercurial > repos > proteore > proteore_id_converter
annotate id_converter.py @ 21:6e65e1c78705 draft default tip
"planemo upload commit 540dd383c0617193db43bf11457011888751b022-dirty"
author | proteore |
---|---|
date | Thu, 23 Jan 2020 08:43:51 -0500 |
parents | 9d758344d36e |
children |
rev | line source |
---|---|
18
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
1 import sys, os, argparse, re, csv, itertools |
16
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
2 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
3 def get_args() : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
4 parser = argparse.ArgumentParser() |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
5 parser.add_argument("-d", "--ref_file", help="path to reference file: <species>_id_mapping.tsv", required=True) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
6 parser.add_argument("--input_type", help="type of input (list of id or filename)", required=True) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
7 parser.add_argument("-t", "--id_type", help="type of input IDs", required=True) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
8 parser.add_argument("-i", "--input", help="list of IDs (text or filename)", required=True) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
9 parser.add_argument("-c", "--column_number", help="list of IDs (text or filename)") |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
10 parser.add_argument("--header", help="true/false if your file contains a header") |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
11 parser.add_argument("--target_ids", help="target IDs to map to", required=True) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
12 parser.add_argument("-o", "--output", help="output filename", required=True) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
13 args = parser.parse_args() |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
14 return args |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
15 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
16 #return list of (unique) ids from string |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
17 def get_input_ids_from_string(input) : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
18 ids_list = list(set(re.split(r'\s+',input.replace("\r","").replace("\n"," ").replace("\t"," ")))) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
19 if "" in ids_list : ids_list.remove("") |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
20 #if "NA" in ids_list : ids_list.remove("NA") |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
21 return ids_list |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
22 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
23 #return input_file and list of unique ids from input file path |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
24 def get_input_ids_from_file(input,nb_col,header) : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
25 with open(input, "r") as csv_file : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
26 input_file= list(csv.reader(csv_file, delimiter='\t')) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
27 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
28 input_file, ids_list = one_id_one_line(input_file,nb_col,header) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
29 if "" in ids_list : ids_list.remove("") |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
30 #if "NA" in ids_list : ids_list.remove("NA") |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
31 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
32 return input_file, ids_list |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
33 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
34 #return input file by adding lines when there are more than one id per line |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
35 def one_id_one_line(input_file,nb_col,header) : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
36 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
37 if header : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
38 new_file = [input_file[0]] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
39 input_file = input_file[1:] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
40 else : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
41 new_file=[] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
42 ids_list=[] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
43 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
44 for line in input_file : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
45 if line != [] and set(line) != {''}: |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
46 line[nb_col] = re.sub(r"\s+","",line[nb_col]) |
17
1e45ea50f145
planemo upload commit 640429adac783e2354f5caf12d7b6b81b8ddf54f-dirty
proteore
parents:
16
diff
changeset
|
47 if line[nb_col] == "" : line[nb_col]='NA' |
16
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
48 if ";" in line[nb_col] : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
49 ids = line[nb_col].split(";") |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
50 for id in ids : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
51 new_file.append(line[:nb_col]+[id]+line[nb_col+1:]) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
52 ids_list.append(id) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
53 else : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
54 new_file.append(line) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
55 ids_list.append(line[nb_col]) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
56 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
57 ids_list= list(set(ids_list)) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
58 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
59 return new_file, ids_list |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
60 |
19
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
61 #not used |
18
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
62 def output_one_id_one_line(line,convert_ids,target_ids): |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
63 |
19
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
64 #ids_not_processed = ["GI","PDB","GO","PIR","MIM","UniGene","BioGrid","STRING"] #ids with multiple ids per line in output file |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
65 ids_not_processed = ["UniProt-AC","UniProt-AC_reviewed","UniProt-ID","GeneID","RefSeq","GI","PDB","GO","PIR","MIM","UniGene","Ensembl_Gene","Ensembl_Transcript","Ensembl_Protein","BioGrid","STRING","KEGG"] # All Ids |
18
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
66 ids_not_processed = [id for id in ids_not_processed if id in target_ids] #ids present in target_ids with multiple ids per line in output file |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
67 |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
68 for id_not_processed in ids_not_processed : |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
69 index = target_ids.index(id_not_processed) |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
70 convert_ids[index] = [";".join(convert_ids[index])] |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
71 |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
72 res = itertools.product(*convert_ids) #getting all possibilities between lists of ids |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
73 res = [list(e) for e in res] #convert to lists |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
74 res = [line+list(ids) for ids in res] #adding the rest of the line |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
75 |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
76 return(res) |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
77 |
16
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
78 #return the column number in int format |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
79 def nb_col_to_int(nb_col): |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
80 try : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
81 nb_col = int(nb_col.replace("c", "")) - 1 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
82 return nb_col |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
83 except : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
84 sys.exit("Please specify the column where you would like to apply the filter with valid format") |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
85 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
86 #replace all blank cells to NA |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
87 def blank_to_NA(csv_file) : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
88 tmp=[] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
89 for line in csv_file : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
90 line = ["NA" if cell=="" or cell==" " or cell=="NaN" else cell for cell in line] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
91 tmp.append(line) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
92 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
93 return tmp |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
94 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
95 def str2bool(v): |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
96 if v.lower() in ('yes', 'true', 't', 'y', '1'): |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
97 return True |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
98 elif v.lower() in ('no', 'false', 'f', 'n', '0'): |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
99 return False |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
100 else: |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
101 raise argparse.ArgumentTypeError('Boolean value expected.') |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
102 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
103 #return result dictionary |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
104 def map_to_dictionary(ids,ids_dictionary,id_in,id_out) : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
105 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
106 result_dict = {} |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
107 for id in ids : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
108 for target_id in id_out : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
109 if id in ids_dictionary : |
18
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
110 res = ids_dictionary[id][target_id] |
16
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
111 else : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
112 res="" |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
113 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
114 if id in result_dict : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
115 result_dict[id].append(res) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
116 else : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
117 result_dict[id]=[res] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
118 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
119 return result_dict |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
120 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
121 #create empty dictionary with index for tab |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
122 def create_ids_dictionary (ids_list) : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
123 ids_dictionary = {} |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
124 ids_dictionary_index={} |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
125 for i,id in enumerate(ids_list) : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
126 ids_dictionary_index[i]=id |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
127 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
128 return(ids_dictionary,ids_dictionary_index) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
129 |
19
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
130 def create_header(input_file,ncol,id_type,target_ids): |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
131 col_names = list(range(1,len(input_file[0])+1)) |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
132 col_names = ["col"+str(e) for e in col_names] |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
133 col_names[ncol]=id_type |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
134 col_names = col_names+target_ids |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
135 return(col_names) |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
136 |
16
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
137 def main(): |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
138 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
139 #Get args from command line |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
140 args = get_args() |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
141 target_ids = args.target_ids.split(",") |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
142 header=False |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
143 if args.id_type in target_ids : target_ids.remove(args.id_type) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
144 if args.input_type=="file" : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
145 args.column_number = nb_col_to_int(args.column_number) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
146 header = str2bool(args.header) |
17
1e45ea50f145
planemo upload commit 640429adac783e2354f5caf12d7b6b81b8ddf54f-dirty
proteore
parents:
16
diff
changeset
|
147 |
16
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
148 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
149 #Get ref file to build dictionary |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
150 csv.field_size_limit(sys.maxsize) # to handle big files |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
151 with open(args.ref_file, "r") as csv_file : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
152 tab = csv.reader(csv_file, delimiter='\t') |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
153 tab = [line for line in tab] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
154 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
155 ids_list=tab[0] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
156 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
157 #create empty dictionary and dictionary index |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
158 ids_dictionary, ids_dictionary_index = create_ids_dictionary(ids_list) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
159 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
160 #fill dictionary and sub dictionaries with ids |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
161 id_index = ids_list.index(args.id_type) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
162 for line in tab[1:] : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
163 ref_ids=line[id_index] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
164 other_id_type_index = [accession_id for accession_id in ids_dictionary_index.keys() if accession_id!=id_index] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
165 for id in ref_ids.replace(" ","").split(";") : #if there's more than one id, one key per id (example : GO) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
166 if id not in ids_dictionary : #if the key is not created yet |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
167 ids_dictionary[id]={} |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
168 for other_id_type in other_id_type_index : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
169 if ids_dictionary_index[other_id_type] not in ids_dictionary[id] : |
19
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
170 ids_dictionary[id][ids_dictionary_index[other_id_type]] = set(line[other_id_type].replace("NA","").replace(" ","").split(";")) |
16
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
171 else : |
19
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
172 ids_dictionary[id][ids_dictionary_index[other_id_type]] |= set(line[other_id_type].replace("NA","").replace(" ","").split(";")) |
16
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
173 if len(ids_dictionary[id][ids_dictionary_index[other_id_type]]) > 1 and '' in ids_dictionary[id][ids_dictionary_index[other_id_type]] : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
174 ids_dictionary[id][ids_dictionary_index[other_id_type]].remove('') |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
175 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
176 #Get file and/or ids from input |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
177 if args.input_type == "list" : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
178 ids = get_input_ids_from_string(args.input) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
179 elif args.input_type == "file" : |
17
1e45ea50f145
planemo upload commit 640429adac783e2354f5caf12d7b6b81b8ddf54f-dirty
proteore
parents:
16
diff
changeset
|
180 input_file, ids = get_input_ids_from_file(args.input,args.column_number,header) |
16
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
181 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
182 #Mapping ids |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
183 result_dict = map_to_dictionary(ids,ids_dictionary,args.id_type,target_ids) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
184 |
18
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
185 #creating output file |
16
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
186 with open(args.output,"w") as output : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
187 writer = csv.writer(output,delimiter="\t") |
18
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
188 #writer.writerows(output_file) |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
189 |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
190 #write header |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
191 if header : |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
192 writer.writerow(input_file[0]+target_ids) |
19
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
193 input_file = input_file[1:] |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
194 elif args.input_type=="file": |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
195 col_names = create_header(input_file,args.column_number,args.id_type,target_ids) |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
196 writer.writerow(col_names) |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
197 else : |
18
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
198 writer.writerow([args.id_type]+target_ids) |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
199 |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
200 #write lines |
19
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
201 previous_line="" |
18
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
202 if args.input_type=="file" : |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
203 for line in input_file : |
19
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
204 res = [";".join(list(res_ids)) for res_ids in result_dict[line[args.column_number]]] |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
205 line = ["NA" if cell=="" or cell==" " or cell=="NaN" else cell for cell in line+res] |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
206 if previous_line != line : |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
207 writer.writerow(line) |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
208 previous_line=line |
18
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
209 elif args.input_type=="list" : |
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
210 for id in ids : |
19
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
211 res = [";".join(list(res_ids)) for res_ids in result_dict[id]] |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
212 line = ["NA" if cell=="" or cell==" " or cell=="NaN" else cell for cell in [id]+res] |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
213 if previous_line != line : |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
214 writer.writerow(line) |
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
215 previous_line=line |
18
5252bbcfbdd7
planemo upload commit dc6d9d8f1808c4c6bcc42ac6861a8b811e4cee58-dirty
proteore
parents:
17
diff
changeset
|
216 |
19
9d758344d36e
planemo upload commit 339ab77a83db03409c8001324f10b36ff5b13a39-dirty
proteore
parents:
18
diff
changeset
|
217 #print ("output file created") |
16
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
218 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
219 if __name__ == "__main__": |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
220 main() |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
221 |