Mercurial > repos > proteore > proteore_venn_diagram
annotate venn_diagram.py @ 6:95bbe3a6b09f draft
"planemo upload commit d78a9195d250f3d873f3c4ab67d1fa3ae58926f5-dirty"
| author | proteore |
|---|---|
| date | Fri, 10 Jan 2020 08:18:06 +0000 |
| parents | c3bbc688dde2 |
| children | 087011d9e3aa |
| 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 |
|
1
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
8 import re, csv |
|
0
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] |
|
4
c3bbc688dde2
planemo upload commit 1316c2feac23e3b1a4f929c3386142d5d5d7e37a-dirty
proteore
parents:
3
diff
changeset
|
50 with open(input_file,"r") as handle : |
|
c3bbc688dde2
planemo upload commit 1316c2feac23e3b1a4f929c3386142d5d5d7e37a-dirty
proteore
parents:
3
diff
changeset
|
51 file_content = csv.reader(handle,delimiter="\t") |
|
c3bbc688dde2
planemo upload commit 1316c2feac23e3b1a4f929c3386142d5d5d7e37a-dirty
proteore
parents:
3
diff
changeset
|
52 file_content = list(file_content) #csv object to list |
|
0
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
53 |
|
4
c3bbc688dde2
planemo upload commit 1316c2feac23e3b1a4f929c3386142d5d5d7e37a-dirty
proteore
parents:
3
diff
changeset
|
54 # Check if column number is in right form |
|
c3bbc688dde2
planemo upload commit 1316c2feac23e3b1a4f929c3386142d5d5d7e37a-dirty
proteore
parents:
3
diff
changeset
|
55 if isnumber("int", ncol.replace("c", "")): |
|
c3bbc688dde2
planemo upload commit 1316c2feac23e3b1a4f929c3386142d5d5d7e37a-dirty
proteore
parents:
3
diff
changeset
|
56 if header == "true": |
|
c3bbc688dde2
planemo upload commit 1316c2feac23e3b1a4f929c3386142d5d5d7e37a-dirty
proteore
parents:
3
diff
changeset
|
57 file_content = [x for x in [line[int(ncol.replace("c", ""))-1].split(";") for line in file_content[1:]]] # gets ids from defined column |
|
c3bbc688dde2
planemo upload commit 1316c2feac23e3b1a4f929c3386142d5d5d7e37a-dirty
proteore
parents:
3
diff
changeset
|
58 else: |
|
c3bbc688dde2
planemo upload commit 1316c2feac23e3b1a4f929c3386142d5d5d7e37a-dirty
proteore
parents:
3
diff
changeset
|
59 file_content = [x for x in [line[int(ncol.replace("c", ""))-1].split(";") for line in file_content]] |
|
0
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
60 else: |
|
4
c3bbc688dde2
planemo upload commit 1316c2feac23e3b1a4f929c3386142d5d5d7e37a-dirty
proteore
parents:
3
diff
changeset
|
61 raise ValueError("Please fill in the right format of column number") |
|
0
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
62 else: |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
63 ids = set() |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
64 file_content = inputs[i][0].split() |
|
4
c3bbc688dde2
planemo upload commit 1316c2feac23e3b1a4f929c3386142d5d5d7e37a-dirty
proteore
parents:
3
diff
changeset
|
65 file_content = [x.split(";") for x in file_content] |
|
0
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
66 |
|
4
c3bbc688dde2
planemo upload commit 1316c2feac23e3b1a4f929c3386142d5d5d7e37a-dirty
proteore
parents:
3
diff
changeset
|
67 file_content = [item.strip() for sublist in file_content for item in sublist if item != ''] #flat list of list of lists, remove empty items |
|
0
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
68 ids.update(file_content) |
|
1
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
69 if 'NA' in ids : ids.remove('NA') |
|
0
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
70 comp_dict[title] = ids |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
71 |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
72 return comp_dict, title_dict |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
73 |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
74 def intersect(comp_dict): |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
75 """ |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
76 Calculate the intersections of input |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
77 """ |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
78 names = set(comp_dict) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
79 for i in range(1, len(comp_dict) + 1): |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
80 for group in combinations(sorted(comp_dict), i): |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
81 others = set() |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
82 [others.add(name) for name in names if name not in group] |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
83 difference = [] |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
84 intersected = set.intersection(*(comp_dict[k] for k in group)) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
85 if len(others) > 0: |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
86 difference = intersected.difference(set.union(*(comp_dict[k] for k in others))) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
87 yield group, list(intersected), list(difference) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
88 |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
89 def diagram(comp_dict, title_dict): |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
90 """ |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
91 Create json string for jvenn diagram plot |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
92 """ |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
93 result = {} |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
94 result["name"] = {} |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
95 for k in comp_dict.keys(): |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
96 result["name"][k] = title_dict[k] |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
97 |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
98 result["data"] = {} |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
99 result["values"] = {} |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
100 for group, intersected, difference in intersect(comp_dict): |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
101 if len(group) == 1: |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
102 result["data"]["".join(group)] = difference |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
103 result["values"]["".join(group)] = len(difference) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
104 elif len(group) > 1 and len(group) < len(comp_dict): |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
105 result["data"]["".join(group)] = difference |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
106 result["values"]["".join(group)] = len(difference) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
107 elif len(group) == len(comp_dict): |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
108 result["data"]["".join(group)] = intersected |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
109 result["values"]["".join(group)] = len(intersected) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
110 |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
111 return result |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
112 |
|
1
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
113 #Write intersections of input to text output file |
|
0
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
114 def write_text_venn(json_result): |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
115 lines = [] |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
116 result = dict((k, v) for k, v in json_result["data"].iteritems() if v != []) |
|
1
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
117 for key in result : |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
118 if 'NA' in result[key] : result[key].remove("NA") |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
119 list_names = dict((k, v) for k, v in json_result["name"].iteritems() if v != []) |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
120 nb_lines_max = max(len(v) for v in result.values()) |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
121 |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
122 #get list names associated to each column |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
123 column_dict = {} |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
124 for key in result : |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
125 if key in list_names : |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
126 column_dict[key] = list_names[key] |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
127 else : |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
128 keys= list(key) |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
129 column_dict[key] = "_".join([list_names[k] for k in keys]) |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
130 |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
131 #construct tsv |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
132 for key in result : |
|
6
95bbe3a6b09f
"planemo upload commit d78a9195d250f3d873f3c4ab67d1fa3ae58926f5-dirty"
proteore
parents:
4
diff
changeset
|
133 line = result[key] |
|
1
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
134 if len(line) < nb_lines_max : |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
135 line.extend(['NA']*(nb_lines_max-len(line))) |
|
6
95bbe3a6b09f
"planemo upload commit d78a9195d250f3d873f3c4ab67d1fa3ae58926f5-dirty"
proteore
parents:
4
diff
changeset
|
136 line = [column_dict[key]] + line #add header |
|
1
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
137 lines.append(line) |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
138 #transpose tsv |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
139 lines=zip(*lines) |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
140 |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
141 with open("venn_diagram_text_output.tsv", "w") as output: |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
142 tsv_output = csv.writer(output, delimiter='\t') |
|
b17f0fbbd6c9
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
proteore
parents:
0
diff
changeset
|
143 tsv_output.writerows(lines) |
|
0
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
144 |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
145 def write_summary(summary_file, inputs): |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
146 """ |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
147 Paste json string into template file |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
148 """ |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
149 a, b = input_to_dict(inputs) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
150 data = diagram(a, b) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
151 write_text_venn(data) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
152 |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
153 to_replace = { |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
154 "series": [data], |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
155 "displayStat": "true", |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
156 "displaySwitch": "true", |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
157 "shortNumber": "true", |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
158 } |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
159 |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
160 FH_summary_tpl = open(os.path.join(CURRENT_DIR, "jvenn_template.html")) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
161 FH_summary_out = open(summary_file, "w" ) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
162 for line in FH_summary_tpl: |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
163 if "###JVENN_DATA###" in line: |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
164 line = line.replace("###JVENN_DATA###", json.dumps(to_replace)) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
165 FH_summary_out.write(line) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
166 |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
167 FH_summary_out.close() |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
168 FH_summary_tpl.close() |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
169 |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
170 def process(args): |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
171 write_summary(args.summary, args.input) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
172 |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
173 |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
174 ################################################################################################################################################## |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
175 # MAIN |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
176 ################################################################################################################################################## |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
177 if __name__ == '__main__': |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
178 # Parse parameters |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
179 parser = argparse.ArgumentParser(description='Filters an abundance file') |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
180 group_input = parser.add_argument_group( 'Inputs' ) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
181 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
|
182 group_output = parser.add_argument_group( 'Outputs' ) |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
183 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
|
184 args = parser.parse_args() |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
185 |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
186 # Process |
|
8d61f5ca50a4
planemo upload commit 9760cde192a15cdf3d2dbec05dd867eaa0392bcd-dirty
proteore
parents:
diff
changeset
|
187 process( args ) |
