/
Dalliance.jl
189 lines (149 loc) · 5.25 KB
/
Dalliance.jl
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
module Dalliance
export genomebrowser
const dalliancejs = Pkg.dir("Dalliance", "data", "dalliance-compiled.js")
import Base: writemime
using Compat, HttpServer, JSON, Bio, Bio.Intervals
import HttpCommon
const HTTP_SERVER_PORT = 8001
immutable DallianceBrowser
genome_config::Dict
sources::Vector{Dict}
end
function prepare_dataset(dataset::IntervalCollection{BEDMetadata})
out = IOBuffer()
write(out, BigBed, dataset)
return takebuf_array(out)
end
function prepare_dataset(dataset::IntervalStream{BEDMetadata})
ic = IntervalCollection{BEDMetadata}()
for interval in dataset
push!(ic, interval)
end
return prepare_dataset(ic)
end
function prepare_dataset{T <: Number}(dataset::IntervalStream{T})
out = IOBuffer()
write(out, BigWig, dataset)
return takebuf_array(out)
end
# catchall
function prepare_dataset(dataset)
error("Dalliance does not currently support data of type $(typeof(dataset))")
end
function genomebrowser(genome::String, datasets...)
if !haskey(supported_genomes, genome)
error("Genome $(genome) is not currently supported.")
end
sources = Dict[]
for dataset in datasets
id = string(Base.Random.uuid4())
DATASETS[id] = prepare_dataset(dataset)
push!(sources, @compat Dict(
"name" => "User Data", # TODO: some way to set names
"bwgURI" => "http://localhost:$(HTTP_SERVER_PORT)/$(id)"))
end
push!(sources,
@compat Dict("name" => "Genome",
"twoBitURI" => supported_genomes[genome]["sequence"],
"tier_type" => "sequence"))
return DallianceBrowser(supported_genomes[genome], sources)
end
const supported_genomes = @compat Dict(
"GRCh38" => Dict(
"speciesName" => "Human",
"taxon" => 9606,
"auth" => "GRCh",
"version" => "38",
"ucscName" => "hg38",
"chr" => "1",
"viewStart" => 1,
"viewEnd" => 100000,
"sequence" => "http://www.biodalliance.org/datasets/hg38.2bit"
),
"GRCh37" => Dict(
"speciesName" => "Human",
"taxon" => 9606,
"auth" => "GRCh",
"version" => "37",
"ucscName" => "hg19",
"chr" => "1",
"viewStart" => 1,
"viewEnd" => 100000,
"sequence" => "http://www.biodalliance.org/datasets/hg19.2bit"
)
# TODO: More genomes.
)
# Associate unique identifiers with datasets being served through http to dalliance.
const DATASETS = Dict{String, Vector{Uint8}}()
# HTTP Server instance.
let HTTP_SERVER = @compat Nullable{Server}()
global start_server
function start_server()
if isnull(HTTP_SERVER)
HTTP_SERVER = Nullable{Server}(Server(http_handler))
@async run(get(HTTP_SERVER), host=IPv4(127,0,0,1), port=8001)
#run(get(HTTP_SERVER), host=IPv4(127,0,0,1), port=8001)
# TODO: if dalliance tries to access a track before the server is
# ready, things break. Presumably there is a better solution than
# this. Or does it...?
sleep(5)
end
end
end
function http_handler(req::Request, res::Response)
# debug nonsense:
#logfile = open("/Users/dcjones/dialliance-log.txt", "a")
#println(logfile, req)
#close(logfile)
h = HttpCommon.headers()
h["Access-Control-Allow-Origin"] = "*"
h["Access-Control-Allow-Headers"] = "Range"
h["Access-Control-Max-Age"] = "36000"
if req.method == "OPTIONS"
return Response(200, h)
elseif req.method != "GET" || !haskey(req.headers, "Range")
return Response(501, h)
end
resource = match(r"^/([^?]*)", req.resource).captures[1]
if !haskey(DATASETS, resource)
return Response(404, h)
end
data = DATASETS[resource]
m = match(r"bytes=(\d+)-(\d+)", req.headers["Range"])
first = parse(Int, m.captures[1]) + 1
last = parse(Int, m.captures[2]) + 1
range = first:min(last, length(data))
return Response(data[range], h)
end
function writemime(io::IO, ::MIME"text/html", browser::DallianceBrowser)
start_server()
divid = string(Base.Random.uuid4())
config = @compat Dict(
"pageName" => divid,
"chr" => browser.genome_config["chr"],
"viewStart" => browser.genome_config["viewStart"],
"viewEnd" => browser.genome_config["viewEnd"],
"coordSystem" => Dict(
"speciesName" => browser.genome_config["speciesName"],
"taxon" => browser.genome_config["taxon"],
"auth" => browser.genome_config["auth"],
"version" => browser.genome_config["version"],
"ucscName" => browser.genome_config["ucscName"]
),
"sources" => browser.sources)
#println(io, "<script language=\"javascript\" src=\"file://", dalliancejs, "\"></script>")
println(io, "<script language=\"javascript\">\n", readall(dalliancejs), "</script>")
println(io,
"""
<script language="javascript">
new window.Browser(
""")
println(io, json(config))
println(io,
"""
);
</script>
""")
println(io, "<div id=\"", divid, "\"/>")
end
end