Mercurial > repos > proteore > proteore_id_converter
annotate id_converter.py @ 16:b6607b7e683f draft
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
author | proteore |
---|---|
date | Mon, 28 Jan 2019 11:08:47 -0500 |
parents | |
children | 1e45ea50f145 |
rev | line source |
---|---|
16
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
1 import sys, os, argparse, re, csv |
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]) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
47 if ";" in line[nb_col] : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
48 ids = line[nb_col].split(";") |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
49 for id in ids : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
50 new_file.append(line[:nb_col]+[id]+line[nb_col+1:]) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
51 ids_list.append(id) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
52 else : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
53 new_file.append(line) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
54 ids_list.append(line[nb_col]) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
55 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
56 ids_list= list(set(ids_list)) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
57 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
58 return new_file, ids_list |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
59 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
60 #return the column number in int format |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
61 def nb_col_to_int(nb_col): |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
62 try : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
63 nb_col = int(nb_col.replace("c", "")) - 1 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
64 return nb_col |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
65 except : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
66 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
|
67 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
68 #replace all blank cells to NA |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
69 def blank_to_NA(csv_file) : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
70 tmp=[] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
71 for line in csv_file : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
72 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
|
73 tmp.append(line) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
74 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
75 return tmp |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
76 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
77 def str2bool(v): |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
78 if v.lower() in ('yes', 'true', 't', 'y', '1'): |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
79 return True |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
80 elif v.lower() in ('no', 'false', 'f', 'n', '0'): |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
81 return False |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
82 else: |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
83 raise argparse.ArgumentTypeError('Boolean value expected.') |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
84 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
85 #return result dictionary |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
86 def map_to_dictionary(ids,ids_dictionary,id_in,id_out) : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
87 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
88 result_dict = {} |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
89 for id in ids : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
90 for target_id in id_out : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
91 if id in ids_dictionary : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
92 res = ";".join(ids_dictionary[id][target_id]) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
93 else : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
94 res="" |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
95 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
96 if id in result_dict : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
97 result_dict[id].append(res) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
98 else : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
99 result_dict[id]=[res] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
100 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
101 return result_dict |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
102 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
103 #create empty dictionary with index for tab |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
104 def create_ids_dictionary (ids_list) : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
105 ids_dictionary = {} |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
106 ids_dictionary_index={} |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
107 for i,id in enumerate(ids_list) : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
108 ids_dictionary_index[i]=id |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
109 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
110 return(ids_dictionary,ids_dictionary_index) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
111 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
112 def main(): |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
113 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
114 #Get args from command line |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
115 args = get_args() |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
116 target_ids = args.target_ids.split(",") |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
117 header=False |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
118 if args.id_type in target_ids : target_ids.remove(args.id_type) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
119 if args.input_type=="file" : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
120 args.column_number = nb_col_to_int(args.column_number) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
121 header = str2bool(args.header) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
122 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
123 #Get ref file to build dictionary |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
124 csv.field_size_limit(sys.maxsize) # to handle big files |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
125 with open(args.ref_file, "r") as csv_file : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
126 tab = csv.reader(csv_file, delimiter='\t') |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
127 tab = [line for line in tab] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
128 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
129 ids_list=tab[0] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
130 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
131 #create empty dictionary and dictionary index |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
132 ids_dictionary, ids_dictionary_index = create_ids_dictionary(ids_list) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
133 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
134 #fill dictionary and sub dictionaries with ids |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
135 id_index = ids_list.index(args.id_type) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
136 for line in tab[1:] : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
137 ref_ids=line[id_index] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
138 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
|
139 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
|
140 if id not in ids_dictionary : #if the key is not created yet |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
141 ids_dictionary[id]={} |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
142 for other_id_type in other_id_type_index : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
143 if ids_dictionary_index[other_id_type] not in ids_dictionary[id] : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
144 ids_dictionary[id][ids_dictionary_index[other_id_type]] = set(line[other_id_type].replace(" ","").split(";")) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
145 else : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
146 ids_dictionary[id][ids_dictionary_index[other_id_type]] |= set(line[other_id_type].replace(" ","").split(";")) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
147 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
|
148 ids_dictionary[id][ids_dictionary_index[other_id_type]].remove('') |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
149 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
150 #Get file and/or ids from input |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
151 if args.input_type == "list" : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
152 ids = get_input_ids_from_string(args.input) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
153 elif args.input_type == "file" : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
154 input_file, ids = get_input_ids_from_file(args.input,args.column_number,args.header) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
155 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
156 #Mapping ids |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
157 result_dict = map_to_dictionary(ids,ids_dictionary,args.id_type,target_ids) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
158 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
159 #creating output file |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
160 if header : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
161 output_file=[input_file[0]+target_ids] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
162 input_file = input_file[1:] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
163 else : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
164 output_file=[[args.id_type]+target_ids] |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
165 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
166 if args.input_type=="file" : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
167 for line in input_file : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
168 output_file.append(line+result_dict[line[args.column_number]]) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
169 elif args.input_type=="list" : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
170 for id in ids : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
171 output_file.append([id]+result_dict[id]) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
172 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
173 #convert blank to NA |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
174 output_file = blank_to_NA(output_file) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
175 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
176 #write output file |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
177 with open(args.output,"w") as output : |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
178 writer = csv.writer(output,delimiter="\t") |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
179 writer.writerows(output_file) |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
180 |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
181 if __name__ == "__main__": |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
182 main() |
b6607b7e683f
planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
proteore
parents:
diff
changeset
|
183 |