annotate venn_diagram.py @ 0:6f22d2726103 draft default tip

planemo upload
author lnguyen
date Fri, 15 Sep 2017 09:32:19 -0400 (2017-09-15)
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
1 #!/usr/bin/env python2.7
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
2
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
3 import os
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
4 import sys
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
5 import json
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
6 import operator
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
7 import argparse
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
8 import re
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
9 from itertools import combinations
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
10
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
11 CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
12
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
13 ##################################################################################################################################################
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
14 # FUNCTIONS
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
15 ##################################################################################################################################################
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
16
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
17 def isnumber(format, n):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
18 """
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
19 Check if an element is integer or float
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
20 """
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
21 float_format = re.compile("^[\-]?[1-9][0-9]*\.?[0-9]+$")
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
22 int_format = re.compile("^[\-]?[1-9][0-9]*$")
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
23 test = ""
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
24 if format == "int":
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
25 test = re.match(int_format, n)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
26 elif format == "float":
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
27 test = re.match(float_format, n)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
28 if test:
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
29 return True
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
30 else:
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
31 return False
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
32
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
33 def input_to_dict(inputs):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
34 """
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
35 Parse input and return a dictionary of name and data of each lists/files
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
36 """
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
37 comp_dict = {}
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
38 title_dict = {}
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
39 c = ["A", "B", "C", "D", "E", "F"]
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
40 for i in range(len(inputs)):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
41 input_file = inputs[i][0]
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
42 name = inputs[i][1]
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
43 input_type = inputs[i][2]
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
44 title = c[i]
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
45 title_dict[title] = name
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
46 ids = set()
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
47 if input_type == "file":
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
48 header = inputs[i][3]
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
49 ncol = inputs[i][4]
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
50 file_content = open(input_file, "r").readlines()
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
51
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
52 # Check if column number is in right form
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
53 if isnumber("int", ncol.replace("c", "")):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
54 if header == "true":
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
55 file_content = [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
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
56 else:
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
57 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
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
58 #print(file_content[1:13])
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
59 else:
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
60 raise ValueError("Please fill in the right format of column number")
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
61 else:
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
62 ids = set()
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
63 file_content = inputs[i][0].split()
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
64
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
65 ids.update(file_content)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
66 comp_dict[title] = ids
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
67
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
68 return comp_dict, title_dict
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
69
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
70 def intersect(comp_dict):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
71 """
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
72 Calculate the intersections of input
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
73 """
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
74 names = set(comp_dict)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
75 for i in range(1, len(comp_dict) + 1):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
76 for group in combinations(sorted(comp_dict), i):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
77 others = set()
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
78 [others.add(name) for name in names if name not in group]
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
79 difference = []
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
80 intersected = set.intersection(*(comp_dict[k] for k in group))
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
81 n = "".join(group)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
82 if len(others) > 0:
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
83 difference = intersected.difference(set.union(*(comp_dict[k] for k in others)))
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
84 yield group, list(intersected), list(difference)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
85
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
86 def diagram(comp_dict, title_dict):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
87 """
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
88 Create json string for jvenn diagram plot
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
89 """
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
90 result = {}
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
91 result["name"] = {}
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
92 for k in comp_dict.keys():
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
93 result["name"][k] = title_dict[k]
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
94
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
95 result["data"] = {}
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
96 result["values"] = {}
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
97 for group, intersected, difference in intersect(comp_dict):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
98 if len(group) == 1:
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
99 result["data"]["".join(group)] = difference
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
100 result["values"]["".join(group)] = len(difference)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
101 elif len(group) > 1 and len(group) < len(comp_dict):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
102 result["data"]["".join(group)] = difference
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
103 result["values"]["".join(group)] = len(difference)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
104 elif len(group) == len(comp_dict):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
105 result["data"]["".join(group)] = intersected
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
106 result["values"]["".join(group)] = len(intersected)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
107
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
108 return result
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
109
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
110 def write_text_venn(json_result):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
111 """
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
112 Write intersections of input to text output file
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
113 """
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
114 output = open("venn_diagram_text_output.txt", "w")
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
115 string = ""
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
116 lines = []
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
117 result = dict((k, v) for k, v in json_result["data"].iteritems() if v != [])
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
118 print(result)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
119 max_count = max(len(v) for v in result.values())
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
120 print(max_count)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
121 for i in range(max_count):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
122 lines.append("")
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
123
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
124 for i in range(max_count):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
125 header = ""
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
126 for d in range(len(result.keys())):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
127 data = result.keys()[d]
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
128 name = "_".join([json_result["name"][x] for x in data])
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
129 header += name + "\t"
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
130 if len(result[data]) > i:
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
131 lines[i] += result[data][i] + "\t"
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
132 else:
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
133 lines[i] += "\t"
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
134 string += header + "\n"
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
135 string += "\n".join(lines)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
136 print(string)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
137 output.write(string)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
138 output.close()
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
139
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
140 def write_summary( summary_file, inputs):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
141 """
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
142 Paste json string into template file
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
143 """
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
144 a, b = input_to_dict(inputs)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
145 data = diagram(a, b)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
146 write_text_venn(data)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
147
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
148 to_replace = {
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
149 "series": [data],
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
150 "displayStat": "true",
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
151 "displaySwitch": "true",
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
152 "shortNumber": "true",
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
153 }
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
154
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
155 FH_summary_tpl = open(os.path.join(CURRENT_DIR, "jvenn_template.html"))
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
156 FH_summary_out = open(summary_file, "w" )
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
157 for line in FH_summary_tpl:
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
158 if "###JVENN_DATA###" in line:
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
159 line = line.replace("###JVENN_DATA###", json.dumps(to_replace))
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
160 FH_summary_out.write(line)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
161
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
162 FH_summary_out.close()
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
163 FH_summary_tpl.close()
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
164
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
165 def process(args):
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
166 write_summary(args.summary, args.input)
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
167
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
168
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
169 ##################################################################################################################################################
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
170 # MAIN
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
171 ##################################################################################################################################################
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
172 if __name__ == '__main__':
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
173 # Parse parameters
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
174 parser = argparse.ArgumentParser(description='Filters an abundance file')
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
175 group_input = parser.add_argument_group( 'Inputs' )
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
176 group_input.add_argument('--input', nargs="+", action="append", required=True, help="The input tabular file.")
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
177 group_output = parser.add_argument_group( 'Outputs' )
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
178 group_output.add_argument('--summary', default="summary.html", help="The HTML file containing the graphs. [Default: %(default)s]")
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
179 args = parser.parse_args()
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
180
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
181 # Process
6f22d2726103 planemo upload
lnguyen
parents:
diff changeset
182 process( args )