-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_fusion_report.py
executable file
·104 lines (68 loc) · 3.27 KB
/
create_fusion_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
# encoding: utf-8
import os, sys, re
import json
from igv_reports import datauri
def create_fusion_report(template, fusions, output_filename, input_file_prefix):
basedir = os.path.dirname(fusions)
data_uris = {}
## read in template
with open(template, "r") as f:
data = f.readlines()
## determine where in the html template that the igv code should be inserted.
for i, line in enumerate(data):
j = line.find('<!-- start igv report here -->')
if j >= 0:
space = ' ' * j
report_start = i + 1
break
else:
print("file must contain the line \"<!-- start igv report here -->\"")
return
input_lines = data[report_start:]
output_lines = []
#insert json for fusion selection table
with open(fusions, "r") as f:
j = json.loads(f.read())
flattend_json = json.dumps(j)
output_lines.append('var tableJson = ' + flattend_json)
## make data uri's for each of the referenced input filenames in the html doc
for line_index, line in enumerate(input_lines):
is_index = line.find('indexURL:') > 0
if is_index:
# ignore the index files
continue
line = re.sub("__PREFIX__", input_file_prefix, line)
m = re.search("url:\s*([\"\']([^\"\']+)[\"\'])", line, flags=re.IGNORECASE)
if (m):
core_match = m.group(1)
filename = m.group(2)
(line, count) = re.subn(core_match, "data[\"{}\"]".format(filename), line, count=1)
if count != 1:
raise(RuntimeError("couldnt perform replacement at: {}".format(line)))
if os.path.exists(os.path.join(basedir, filename)):
data_uris[filename] = datauri.file_to_data_uri(os.path.join(basedir, filename))
else:
sys.stderr.write("Warning - not locating file: {}\n".format(os.path.join(basedir, filename)))
output_lines.append(line)
report_header = data[:report_start]
report_data_uris = create_data_var(data_uris, space)
report_body = output_lines
new_html_data = report_header + report_data_uris + report_body
with open(output_filename, 'w') as f:
f.writelines(new_html_data)
def create_data_var(data_uris, space=''):
data = []
for i, (key, value) in enumerate(data_uris.items()):
data.append('{}"{}": "{}"{}\n'.format(space + ' ' * 4, key, value, ',' if i < len(data_uris) - 1 else ''))
return [space + "var data = {\n"] + data + [space+"};\n"]
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--html_template", help="the html file to be converted", required=True, type=str)
parser.add_argument("--fusions_json", help="json file defining the fusions (fusion inspector output)", required=True, type=str)
parser.add_argument("--html_output", help="filename for html output", required=True, type=str)
parser.add_argument("--input_file_prefix", help="prefix for input files", required=True, type=str)
args = parser.parse_args()
create_fusion_report(args.html_template, args.fusions_json, args.html_output, args.input_file_prefix)
sys.exit(0)