annotate venn_diagram.py @ 8:415ec6611b1d draft

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