-
Notifications
You must be signed in to change notification settings - Fork 37
/
igv_web.py
executable file
·175 lines (146 loc) · 4.68 KB
/
igv_web.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
"""
A simple wrapper of igv.js for visulization of BAM, BigWig and etc.
"""
import sys
import argparse
from os.path import basename
from os.path import isfile
def get_opt():
group = argparse.ArgumentParser()
group.add_argument("-r", "--ref", help="reference fasta file", required=True)
group.add_argument("-m", "--bam", help="Input mapping file, in1.bam in2.bam ...", required=False, nargs = "*")
group.add_argument("-w", "--bigwig", help="Input bigwig file, in1.bw in2.bw ...", required=False, nargs = "*")
group.add_argument("-b", "--bed", help="bed annotation", required=False)
group.add_argument("-g", "--gtf", help="gtf annotation", required=False, nargs = "*")
return group.parse_args()
def build_bam_tracks(bams):
tracks = []
for bam in bams:
if not isfile( bam + ".bai"):
print("{}.bai is not existed".format(bam), file=sys.stderr)
sys.exit()
bam_id = basename(bam).replace(".bam","")
track = """
{{
name: "{bam_id}",
type: "alignment",
format: "bam",
url: "{bam}",
indexURL: "{bam}.bai"
}},""".format(bam = bam, bam_id = bam_id)
tracks.append(track)
tracks = "".join(tracks)[:-1]
return tracks
def build_bw_tracks(bigwigs):
tracks = []
for bigwig in bigwigs:
bw_id = basename(bigwig).replace(".bw","").replace(".bigwig","").replace(".bigWig","").replace("BigWig","")
track = """
{{
name: "{bw_id}",
format: "bigwig",
url: "{bigwig}"
}},""".format(bw_id = bw_id, bigwig = bigwig)
tracks.append(track)
tracks = "".join(tracks)[:-1]
return tracks
def build_gtf_tracks(gtfs):
tracks = []
for gtf in gtfs:
track = """
{{
type: "annotation",
format: "gtf",
sourceType: "file",
url: "{gtf}",
visibilityWindow: 500000,
displayMode: "COLLAPSED",
autoHeight: true
}},""".format(gtf = gtf)
tracks.append(track)
tracks = "".join(tracks)[:-1]
return tracks
def build_bed_tracks(bed):
track = """
{{
type: "annotation",
format: "bed",
sourceType: "file",
url: "{bed}",
order: Number.MAX_VALUE,
visibilityWindow: 300000000,
displayMode: "EXPANDED"
}}
""".format(bed = bed)
return track
def build_ref_track(fasta):
if not isfile( fasta + ".fai"):
print("{}.fai is not existed".format(fasta), file=sys.stderr)
sys.exit()
genome = basename(fasta)
genome_id = genome.replace(".fasta","").replace(".fas","").replace(".fa","")
bam_track ="""genome: "{genome}",
reference: {{
id: "{genome_id}",
fastaURL: "{fasta}",
indexURL: "{fasta}.fai"
}}""".format(genome = genome, genome_id = genome_id, fasta = fasta)
return bam_track
def make_html(genome, tracks):
html = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>igv.js</title>
<script src="https://igv.org/web/release/2.6.6/dist/igv.min.js"></script>
</head>
<body>
<div id="igv-div"></div>
</body>
<script>
var igvDiv = document.getElementById("igv-div");
var options = {{
showNavigation: true,
showRuler: true,
{genome},
tracks: [
{tracks}
]
}};
igv.createBrowser(igvDiv, options)
.then(function (browser) {{
console.log("Created IGV browser");
}})
</script>
</html>""".format(genome = genome, tracks = tracks)
return html
def write_html(html):
with open("index.html", "w") as file:
file.writelines(html)
def igv_web(fasta, bams, bws, bed, gtfs):
tracks = ""
genome_track = build_ref_track(fasta)
if bams is not None:
bam_track = build_bam_tracks(bams)
tracks += bam_track
if bws is not None:
bw_tracks = build_bw_tracks(bws)
tracks = tracks + ",\n" + bw_tracks
if gtfs is not None:
gtf_track = build_gtf_tracks(gtfs)
tracks = tracks + ",\n" + gtf_track
if bed is not None:
bed_track = build_bed_tracks(bed)
tracks = tracks + ",\n" + bed_track
html = make_html( genome_track, tracks)
write_html(html)
if __name__ == "__main__":
opts = get_opt()
bams = opts.bam
fasta = opts.ref
bed = opts.bed
gtfs = opts.gtf
bws = opts.bigwig
igv_web(fasta, bams, bws, bed, gtfs)