Mercurial > repos > proteore > proteore_venn_diagram
comparison venn_diagram.py @ 1:b17f0fbbd6c9 draft
planemo upload commit ba044b029418f46b923830e8eaa1088833e6ef6b-dirty
| author | proteore |
|---|---|
| date | Wed, 28 Nov 2018 09:55:18 -0500 |
| parents | 8d61f5ca50a4 |
| children | 5ac986568a3d |
comparison
equal
deleted
inserted
replaced
| 0:8d61f5ca50a4 | 1:b17f0fbbd6c9 |
|---|---|
| 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 """ | 111 output = open("venn_diagram_text_output.tsv", "w") |
| 110 Write intersections of input to text output file | |
| 111 """ | |
| 112 output = open("venn_diagram_text_output.txt", "w") | |
| 113 string = "" | |
| 114 lines = [] | 112 lines = [] |
| 115 result = dict((k, v) for k, v in json_result["data"].iteritems() if v != []) | 113 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()) | 114 for key in result : |
| 117 for i in range(max_count): | 115 if 'NA' in result[key] : result[key].remove("NA") |
| 118 lines.append("") | 116 list_names = dict((k, v) for k, v in json_result["name"].iteritems() if v != []) |
| 119 | 117 nb_lines_max = max(len(v) for v in result.values()) |
| 120 for i in range(max_count): | 118 |
| 121 header = "" | 119 #get list names associated to each column |
| 122 for d in range(len(result.keys())): | 120 column_dict = {} |
| 123 data = result.keys()[d] | 121 for key in result : |
| 124 name = "_".join([json_result["name"][x] for x in data]) | 122 if key in list_names : |
| 125 header += name + "\t" | 123 column_dict[key] = list_names[key] |
| 126 if len(result[data]) > i: | 124 else : |
| 127 print("a", result[data][i]) | 125 keys= list(key) |
| 128 lines[i] += result[data][i] + "\t" | 126 column_dict[key] = "_".join([list_names[k] for k in keys]) |
| 129 else: | 127 print(column_dict) |
| 130 lines[i] += "\t" | 128 |
| 131 # Strip last tab in the end of the lines | 129 #construct tsv |
| 132 header = header.rstrip() | 130 for key in result : |
| 133 lines = [line.rstrip() for line in lines] | 131 line = [column_dict[key]] |
| 134 string += header + "\n" | 132 line.extend(result[key]) |
| 135 string += "\n".join(lines) | 133 if len(line) < nb_lines_max : |
| 136 output.write(string) | 134 line.extend(['NA']*(nb_lines_max-len(line))) |
| 137 output.close() | 135 lines.append(line) |
| 136 #transpose tsv | |
| 137 lines=zip(*lines) | |
| 138 | |
| 139 with open("venn_diagram_text_output.tsv", "w") as output: | |
| 140 tsv_output = csv.writer(output, delimiter='\t') | |
| 141 tsv_output.writerows(lines) | |
| 138 | 142 |
| 139 def write_summary(summary_file, inputs): | 143 def write_summary(summary_file, inputs): |
| 140 """ | 144 """ |
| 141 Paste json string into template file | 145 Paste json string into template file |
| 142 """ | 146 """ |
