annotate venn_diagram.py @ 0:8d61f5ca50a4 draft

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