comparison 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
comparison
equal deleted inserted replaced
7:087011d9e3aa 8:415ec6611b1d
2 2
3 import argparse 3 import argparse
4 import csv 4 import csv
5 import json 5 import json
6 import os 6 import os
7 import sys # noqa 401
8 import operator # noqa 401
9 import re 7 import re
10 from itertools import combinations 8 from itertools import combinations
9
11 10
12 CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) 11 CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
13 12
14 ######################################################################## 13 ########################################################################
15 # FUNCTIONS 14 # FUNCTIONS
69 file_content = inputs[i][0].split() 68 file_content = inputs[i][0].split()
70 file_content = [x.split(";") for x in file_content] 69 file_content = [x.split(";") for x in file_content]
71 70
72 # flat list of list of lists, remove empty items 71 # flat list of list of lists, remove empty items
73 file_content = [item.strip() for sublist in file_content for item in sublist if item != ''] # noqa 501 72 file_content = [item.strip() for sublist in file_content for item in sublist if item != ''] # noqa 501
74
75 ids.update(file_content) 73 ids.update(file_content)
76 if 'NA' in ids: 74 if 'NA' in ids:
77 ids.remove('NA') 75 ids.remove('NA')
78 comp_dict[title] = ids 76 comp_dict[title] = ids
79 77
90 others = set() 88 others = set()
91 [others.add(name) for name in names if name not in group] 89 [others.add(name) for name in names if name not in group]
92 difference = [] 90 difference = []
93 intersected = set.intersection(*(comp_dict[k] for k in group)) 91 intersected = set.intersection(*(comp_dict[k] for k in group))
94 if len(others) > 0: 92 if len(others) > 0:
95 difference = intersected.difference(set.union(*(comp_dict[k] for k in others))) # noqa 501 93 difference = intersected.difference(set.union(*(comp_dict[k] for k in others))) # noqa 501
96 yield group, list(intersected), list(difference) 94 yield group, list(intersected), list(difference)
97 95
98 96
99 def diagram(comp_dict, title_dict): 97 def diagram(comp_dict, title_dict):
100 """ 98 """
107 105
108 result["data"] = {} 106 result["data"] = {}
109 result["values"] = {} 107 result["values"] = {}
110 for group, intersected, difference in intersect(comp_dict): 108 for group, intersected, difference in intersect(comp_dict):
111 if len(group) == 1: 109 if len(group) == 1:
112 result["data"]["".join(group)] = difference 110 result["data"]["".join(group)] = sorted(difference)
113 result["values"]["".join(group)] = len(difference) 111 result["values"]["".join(group)] = len(difference)
114 elif len(group) > 1 and len(group) < len(comp_dict): 112 elif len(group) > 1 and len(group) < len(comp_dict):
115 result["data"]["".join(group)] = difference 113 result["data"]["".join(group)] = sorted(difference)
116 result["values"]["".join(group)] = len(difference) 114 result["values"]["".join(group)] = len(difference)
117 elif len(group) == len(comp_dict): 115 elif len(group) == len(comp_dict):
118 result["data"]["".join(group)] = intersected 116 result["data"]["".join(group)] = sorted(intersected)
119 result["values"]["".join(group)] = len(intersected) 117 result["values"]["".join(group)] = len(intersected)
120 118
121 return result 119 return result
122 120
123 # Write intersections of input to text output file 121 # Write intersections of input to text output file