annotate id_converter.py @ 17:1e45ea50f145 draft

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