comparison venn_diagram.py @ 6:d1fd04dcb13a draft

planemo upload commit ad5f1c5a1a71d7fa2bc8bac408856aa80b0fc2a3
author proteore
date Tue, 18 Dec 2018 10:06:31 -0500
parents 145f347dc0e1
children bca31ac678f1
comparison
equal deleted inserted replaced
5:6d0ecc59728f 6:d1fd04dcb13a
3 import os 3 import os
4 import sys 4 import sys
5 import json 5 import json
6 import operator 6 import operator
7 import argparse 7 import argparse
8 import re 8 import re, csv
9 from itertools import combinations 9 from itertools import combinations
10 10
11 CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) 11 CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
12 12
13 ################################################################################################################################################## 13 ##################################################################################################################################################
60 else: 60 else:
61 ids = set() 61 ids = set()
62 file_content = inputs[i][0].split() 62 file_content = inputs[i][0].split()
63 63
64 ids.update(file_content) 64 ids.update(file_content)
65 if 'NA' in ids : ids.remove('NA')
65 comp_dict[title] = ids 66 comp_dict[title] = ids
66 67
67 return comp_dict, title_dict 68 return comp_dict, title_dict
68 69
69 def intersect(comp_dict): 70 def intersect(comp_dict):
103 result["data"]["".join(group)] = intersected 104 result["data"]["".join(group)] = intersected
104 result["values"]["".join(group)] = len(intersected) 105 result["values"]["".join(group)] = len(intersected)
105 106
106 return result 107 return result
107 108
109 #Write intersections of input to text output file
108 def write_text_venn(json_result): 110 def write_text_venn(json_result):
109 """
110 Write intersections of input to text output file
111 """
112 output = open("venn_diagram_text_output.txt", "w")
113 string = ""
114 lines = [] 111 lines = []
115 result = dict((k, v) for k, v in json_result["data"].iteritems() if v != []) 112 result = dict((k, v) for k, v in json_result["data"].iteritems() if v != [])
116 max_count = max(len(v) for v in result.values()) 113 for key in result :
117 for i in range(max_count): 114 if 'NA' in result[key] : result[key].remove("NA")
118 lines.append("") 115 list_names = dict((k, v) for k, v in json_result["name"].iteritems() if v != [])
119 116 nb_lines_max = max(len(v) for v in result.values())
120 for i in range(max_count): 117
121 header = "" 118 #get list names associated to each column
122 for d in range(len(result.keys())): 119 column_dict = {}
123 data = result.keys()[d] 120 for key in result :
124 name = "_".join([json_result["name"][x] for x in data]) 121 if key in list_names :
125 header += name + "\t" 122 column_dict[key] = list_names[key]
126 if len(result[data]) > i: 123 else :
127 print("a", result[data][i]) 124 keys= list(key)
128 lines[i] += result[data][i] + "\t" 125 column_dict[key] = "_".join([list_names[k] for k in keys])
129 else: 126
130 lines[i] += "\t" 127 #construct tsv
131 # Strip last tab in the end of the lines 128 for key in result :
132 header = header.rstrip() 129 line = [column_dict[key]]
133 lines = [line.rstrip() for line in lines] 130 line.extend(result[key])
134 string += header + "\n" 131 if len(line) < nb_lines_max :
135 string += "\n".join(lines) 132 line.extend(['NA']*(nb_lines_max-len(line)))
136 output.write(string) 133 lines.append(line)
137 output.close() 134 #transpose tsv
135 lines=zip(*lines)
136
137 with open("venn_diagram_text_output.tsv", "w") as output:
138 tsv_output = csv.writer(output, delimiter='\t')
139 tsv_output.writerows(lines)
138 140
139 def write_summary(summary_file, inputs): 141 def write_summary(summary_file, inputs):
140 """ 142 """
141 Paste json string into template file 143 Paste json string into template file
142 """ 144 """