comparison venn_diagram.py @ 3:145f347dc0e1 draft

planemo upload commit b5beb4f7f3c60838b88a4cc32dfcb94c399d20ce-dirty
author proteore
date Tue, 20 Mar 2018 06:35:27 -0400
parents 9d4f20618ab4
children d1fd04dcb13a
comparison
equal deleted inserted replaced
2:774d9cbe6937 3:145f347dc0e1
16 16
17 def isnumber(format, n): 17 def isnumber(format, n):
18 """ 18 """
19 Check if an element is integer or float 19 Check if an element is integer or float
20 """ 20 """
21 float_format = re.compile("^[\-]?[1-9][0-9]*\.?[0-9]+$") 21 float_format = re.compile(r"^[-]?[1-9][0-9]*.?[0-9]+$")
22 int_format = re.compile("^[\-]?[1-9][0-9]*$") 22 int_format = re.compile(r"^[-]?[1-9][0-9]*$")
23 test = "" 23 test = ""
24 if format == "int": 24 if format == "int":
25 test = re.match(int_format, n) 25 test = re.match(int_format, n)
26 elif format == "float": 26 elif format == "float":
27 test = re.match(float_format, n) 27 test = re.match(float_format, n)
53 if isnumber("int", ncol.replace("c", "")): 53 if isnumber("int", ncol.replace("c", "")):
54 if header == "true": 54 if header == "true":
55 file_content = [x.strip() for x in [line.split("\t")[int(ncol.replace("c", ""))-1].split(";")[0] for line in file_content[1:]]] # take only first IDs 55 file_content = [x.strip() for x in [line.split("\t")[int(ncol.replace("c", ""))-1].split(";")[0] for line in file_content[1:]]] # take only first IDs
56 else: 56 else:
57 file_content = [x.strip() for x in [line.split("\t")[int(ncol.replace("c", ""))-1].split(";")[0] for line in file_content]] # take only first IDs 57 file_content = [x.strip() for x in [line.split("\t")[int(ncol.replace("c", ""))-1].split(";")[0] for line in file_content]] # take only first IDs
58 #print(file_content[1:13])
59 else: 58 else:
60 raise ValueError("Please fill in the right format of column number") 59 raise ValueError("Please fill in the right format of column number")
61 else: 60 else:
62 ids = set() 61 ids = set()
63 file_content = inputs[i][0].split() 62 file_content = inputs[i][0].split()
76 for group in combinations(sorted(comp_dict), i): 75 for group in combinations(sorted(comp_dict), i):
77 others = set() 76 others = set()
78 [others.add(name) for name in names if name not in group] 77 [others.add(name) for name in names if name not in group]
79 difference = [] 78 difference = []
80 intersected = set.intersection(*(comp_dict[k] for k in group)) 79 intersected = set.intersection(*(comp_dict[k] for k in group))
81 n = "".join(group)
82 if len(others) > 0: 80 if len(others) > 0:
83 difference = intersected.difference(set.union(*(comp_dict[k] for k in others))) 81 difference = intersected.difference(set.union(*(comp_dict[k] for k in others)))
84 yield group, list(intersected), list(difference) 82 yield group, list(intersected), list(difference)
85 83
86 def diagram(comp_dict, title_dict): 84 def diagram(comp_dict, title_dict):
113 """ 111 """
114 output = open("venn_diagram_text_output.txt", "w") 112 output = open("venn_diagram_text_output.txt", "w")
115 string = "" 113 string = ""
116 lines = [] 114 lines = []
117 result = dict((k, v) for k, v in json_result["data"].iteritems() if v != []) 115 result = dict((k, v) for k, v in json_result["data"].iteritems() if v != [])
118 print(result)
119 max_count = max(len(v) for v in result.values()) 116 max_count = max(len(v) for v in result.values())
120 print(max_count)
121 for i in range(max_count): 117 for i in range(max_count):
122 lines.append("") 118 lines.append("")
123 119
124 for i in range(max_count): 120 for i in range(max_count):
125 header = "" 121 header = ""
126 for d in range(len(result.keys())): 122 for d in range(len(result.keys())):
127 data = result.keys()[d] 123 data = result.keys()[d]
128 name = "_".join([json_result["name"][x] for x in data]) 124 name = "_".join([json_result["name"][x] for x in data])
129 header += name + "\t" 125 header += name + "\t"
130 if len(result[data]) > i: 126 if len(result[data]) > i:
127 print("a", result[data][i])
131 lines[i] += result[data][i] + "\t" 128 lines[i] += result[data][i] + "\t"
132 else: 129 else:
133 lines[i] += "\t" 130 lines[i] += "\t"
131 # Strip last tab in the end of the lines
132 header = header.rstrip()
133 lines = [line.rstrip() for line in lines]
134 string += header + "\n" 134 string += header + "\n"
135 string += "\n".join(lines) 135 string += "\n".join(lines)
136 print(string)
137 output.write(string) 136 output.write(string)
138 output.close() 137 output.close()
139 138
140 def write_summary( summary_file, inputs): 139 def write_summary(summary_file, inputs):
141 """ 140 """
142 Paste json string into template file 141 Paste json string into template file
143 """ 142 """
144 a, b = input_to_dict(inputs) 143 a, b = input_to_dict(inputs)
145 data = diagram(a, b) 144 data = diagram(a, b)