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