annotate sort_by_tissue.py @ 0:3155d867c056 draft default tip

planemo upload
author lnguyen
date Fri, 15 Sep 2017 11:04:37 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
1 import argparse
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
2 import re
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
3
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
4 def options():
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
5 parser = argparse.ArgumentParser()
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
6 parser.add_argument("--input",nargs="+", required=True, help="MaxQuant file")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
7 parser.add_argument("--hpa", required=True, help="HPA file")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
8 parser.add_argument("--tissues_del", required=True, help="List of tissues which expressed genes in are discarded")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
9 parser.add_argument("--tissues_keep", help="List of tissues to keep regardless being expressed in list tissues_del..")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
10 parser.add_argument("-o", "--output", default="HPA_selection.txt")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
11 parser.add_argument("--trash", default="Trash.txt", help="Write filtered genes into a file")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
12 parser.add_argument("--trash_file_detail", default="Trash_detail.txt", help="Write filtered genes with detailed information into a file")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
13 parser.add_argument("--na_file", default="NaN.txt", help="Write genes whose name not found in HPA file")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
14
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
15 args = parser.parse_args()
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
16 #print(args.mq, args.hpa, args.tissues_del, args.tissues_keep, args.output, args.trash, args.trash_file_detail)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
17
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
18 filterHPA(args.input, args.hpa, args.tissues_del, args.tissues_keep, args.output, args.trash, args.trash_file_detail, args.na_file)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
19
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
20 def isnumber(format, n):
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
21 # Check if an element is integer or float number
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
22 float_format = re.compile("^[\-]?[1-9][0-9]*\.?[0-9]+$")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
23 int_format = re.compile("^[\-]?[1-9][0-9]*$")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
24 test = ""
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
25 if format == "int":
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
26 test = re.match(int_format, n)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
27 elif format == "float":
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
28 test = re.match(float_format, n)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
29 if test:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
30 return True
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
31 else:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
32 return False
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
33
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
34 def readHPA(HPAfile, tissues_del, tissues_keep):
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
35 # Read HPA file
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
36 hpa = open(HPAfile, "r")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
37 hpa = hpa.readlines()
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
38 # Extract lists of genes expressed in tissues to keep and in tissue to delete
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
39 tdel_dict = {}
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
40 if tissues_del:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
41 tissues_del = tissues_del.split(",")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
42 else:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
43 tissues_del = []
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
44 #print("List of tissues to del", tissues_del)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
45 tkeep_dict = {}
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
46 if tissues_keep:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
47 tissues_keep = tissues_keep.split(",")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
48 else:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
49 tissues_keep = []
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
50 #print("List of tissues to keep", tissues_keep)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
51 for line in hpa[1:]:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
52 ensg = line.replace('"', "").split(",")[0]
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
53 tissue = line.replace('"', "").split(",")[2]
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
54 for t in tissues_del:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
55 if tissue == t:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
56 if t not in tdel_dict:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
57 tdel_dict[t] = [ensg]
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
58 else:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
59 if ensg not in tdel_dict[t]:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
60 tdel_dict[t].append(ensg)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
61 for k in tissues_keep:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
62 if tissue == k:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
63 if k not in tkeep_dict:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
64 tkeep_dict[k] = [ensg]
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
65 else:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
66 if ensg not in tkeep_dict[k]:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
67 tkeep_dict[k].append(ensg)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
68
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
69 return tdel_dict, tkeep_dict
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
70
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
71 def filterHPA(input, HPAfile, tissues_del, tissues_keep, output, trash_file, trash_file_detail, na_file):
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
72 input_type = input[1]
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
73 if input_type == "file":
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
74 input_file = input[0]
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
75 header = input[2]
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
76 ncol = input[3]
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
77 file_content = open(input_file, "r").readlines()
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
78 if isnumber("int", ncol.replace("c", "")):
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
79 if header == "true":
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
80 header = file_content[0]
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
81 content = file_content[1:] #[x.strip() for x in [line.split("\t")[int(ncol.replace("c", ""))-1].split(";")[0] for line in file_content[1:]]] # take only first IDs
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
82 else:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
83 header = ""
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
84 content = file_content[:] #[x.strip() for x in [line.split("\t")[int(ncol.replace("c", ""))-1].split(";")[0] for line in file_content]] # take only first IDs
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
85 #print(file_content[1:13])
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
86 ncol = int(ncol.replace("c", "")) - 1
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
87 else:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
88 raise ValueError("Please fill in the right format of column number")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
89 else:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
90 print(input[0])
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
91 header = ""
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
92 content = input[0].split()
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
93
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
94 # Read HPA file
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
95 hpa = open(HPAfile, "r")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
96 hpa = hpa.readlines()
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
97
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
98 # Get dictionary of tissues : genes
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
99 tdel_dict, tkeep_dict = readHPA(HPAfile, tissues_del, tissues_keep)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
100 #print("Dictionary of tissue:genes to del", tdel_dict)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
101 #print("Dictionary of tissue:genes to keep", tkeep_dict)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
102
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
103 # Filter
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
104 string = header.strip() + "\t" + "Filtered" + "\n"
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
105 filtered_genes = []
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
106 filtered_lines = []
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
107 na_genes = []
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
108 #print(len(mq))
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
109 for l in content:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
110 line_string = l.rstrip() + "\t" #.replace("^M", "")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
111 if input_type == "file":
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
112 gene = l.split("\t")[ncol].split(";")[0].replace('"', "")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
113 else:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
114 gene = l
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
115 if gene == "":
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
116 line_string += "NA - No ENSG ID" + "\n"
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
117 string += line_string
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
118 elif gene == "NA":
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
119 line_string += "NA - No ENSG ID" + "\n"
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
120 else:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
121 tissue = sorted(set([t.split(",")[2].replace('"', "") for t in hpa if gene in t]))
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
122 if all (gene not in genes for genes in tdel_dict.values()):
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
123 if len(tissue) != 0:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
124 print("Not in del list", gene, len(tissue))
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
125 line_string += ",".join(tissue) + "\n"
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
126 string += line_string
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
127 else:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
128 print("No tissue information", gene)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
129 line_string += "NA - no tissue information" + "\n"
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
130 string += line_string
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
131 na_genes.append(gene)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
132 else:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
133 if all (gene not in genes for genes in tkeep_dict.values()):
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
134 print("In del list only", gene)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
135 filtered_genes.append(gene)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
136 filtered_lines.append(l)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
137 else:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
138 print("In both del and keep", gene, len(tissue))
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
139 line_string += ",".join(tissue) + "\n"
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
140 string += line_string
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
141
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
142 # Generate output file
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
143 output = open(output, "w")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
144 output.write(string)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
145
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
146 # Generate file of unknown gene name
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
147 na_file = open(na_file, "w")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
148 na_file.write("\n".join(na_genes))
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
149
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
150 # Generate trash files
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
151 output_trash = open(trash_file, "w")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
152 output_trash.write("\n".join(filtered_lines))
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
153
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
154 output_trash_detail = open(trash_file_detail, "w")
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
155 print("Deleted genes", filtered_genes)
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
156 for gene in filtered_genes:
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
157 lines = [line for line in hpa if gene in line]
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
158 output_trash_detail.write("".join(lines))
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
159
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
160 if __name__ == "__main__":
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
161 options()
3155d867c056 planemo upload
lnguyen
parents:
diff changeset
162