-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_demo.html
199 lines (172 loc) · 5.79 KB
/
static_demo.html
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
189
190
191
192
193
194
195
196
197
198
199
<!DOCTYPE html>
<!--
Reproducing basic plot function of gbtools in D3.js
Brandon Seah 2016-12-07
-->
<meta charset="utf-8">
<style>
.axis text {
font-size: 10px;
font-family: sans-serif;
}
.axis path,
.axis line {
fill:none;
stroke: black;
shape-rendering: crispEdges;
}
div.tooltip {
position: absolute;
text-align: center;
width: 100px;
height: 50px;
padding: 2px;
font: 10px sans-serif;
background: lightgrey;
border: 0px;
border-radius: 8px;
pointer-events:none;
}
</style>
<body>
<h1 style="font-family:sans-serif;">Interactive Coverage-GC Plot</h1>
<p>Powered by <a href="https://d3js.org">D3.js</a> v4 by Mike Bostock</p>
<div id="menu">
<p>Filter by contig length (bp):
<input id="filter500" type="button" value="500" onclick="filter(500)"/>
<input id="filter1000" type="button" value="1000" onclick="filter(1000)" />
<input id="filter10000" type="button" value="10000" onclick="filter(10000)" />
<input id="filter100000" type="button" value="100000" onclick="filter(100000)" />
<input id="filter1000000" type="button" value="1000000" onclick="filter(1000000)" />
</div>
<!-- SVG canvas -->
<svg class="chart"></svg>
<!-- Load D3 library v4 -->
<!-- Use remote copy
<script src="http://d3js.org/d3.v4.min.js"></script>
-->
<script src="d3/d3.min.js"></script>
<!-- Script starts here -->
<script>
// Define chart size
var margin = { top:25,
right: 100,
left: 50,
bottom: 50};
var height = 600 - margin.top - margin.bottom;
var width = 775 - margin.left - margin.right;
// Define chart object
var chart = d3.select(".chart")
.attr("height",height + margin.bottom + margin.top)
.attr("width",width + margin.left + margin.right)
.append("g") // Append g object to space margins
.attr("transform","translate(" + margin.left + "," + margin.top + ")");
// Background color for plot area
chart.append("rect")
.attr("height",height)
.attr("width",width)
.attr("fill","#feffe7");
// Div element for tooltip animations
var div = d3.select("body")
.append("div")
.attr("class","tooltip")
.style("opacity",0);
// Load tsv file of data
d3.tsv("data.tsv", function(error,data) {
// Interpret relevant fields as numeric - important!
data.forEach(function (d) { d.Avg_fold = +d.Avg_fold; } );
data.forEach(function (d) { d.Length = +d.Length; } );
data.forEach(function (d) { d.Ref_GC = +d.Ref_GC; } );
// Linear scale for x-axis (GC%)
var x = d3.scaleLinear()
.domain([d3.min(data, function(d) {return d.Ref_GC; }) * 0.9,
d3.max(data, function(d) {return d.Ref_GC; }) * 1.1
])
.range([0,width]);
// Condition to ignore extreme low y-axis values in scale
var yMin = d3.min(data, function(d) {return d.Avg_fold;});
if (yMin < 0.05) {
yMin = 0.05;
} else {
yMin = +yMin;
}
// Log scale for y-axis (coverage)
var y = d3.scaleLog()
.domain([yMin / 2,
// Div by 2 to avoid points directly on margin
d3.max(data, function(d) {return d.Avg_fold; }) * 1.5
// Mult by 1.5 to avoid points directly on margin
])
.range([height,1]);
// Invert min and max because SVG coord starts from upper left corner
// Sqrt scale for plot points (length)
var rad = d3.scaleSqrt()
.domain([0,d3.max(data, function(d) {return d.Length;} )])
.range([0,width*0.01]); // Max plot diameter is 2% of plot width
// horizontal axis
var xAxis = d3.axisBottom(x)
.ticks(10, ".0%"); // 10 tickmarks, as percentages
chart.append("g")
.attr("class","x axis")
.attr("transform","translate(0," + height + ")")
.call(xAxis);
// horizontal axis label
chart.append("text")
.attr("x",width/2)
.attr("y",height + margin.bottom + margin.top - 40)
.style("text-anchor","middle")
.style("font-family","sans-serif")
.style("font-size","10pt")
.text("GC %");
// vertical axis
var yAxis = d3.axisLeft(y)
.ticks(10,".1s"); // 10 tickmarks, 1 sig fig with SI prefix
chart.append("g")
.attr("class","y axis")
.call(yAxis);
// vertical axis label
chart.append ("text")
.attr("transform","rotate(-90)")
.attr ("x",0 - (height/2))
.attr("y", -25)
.style("text-anchor","middle")
.style("font-family","sans-serif")
.style("font-size","10pt")
.text("Coverage");
// Plot the points!
var point = chart.selectAll("circle")
.data(data)
.enter().append("circle")
.filter(function(d) {return d.Length > 500; }) // Subset length > 500
.attr("cx", function(d) { return x(d.Ref_GC); })
.attr("cy", function(d) { return y(d.Avg_fold); })
.attr("r", function(d) { return rad(d.Length); })
.style("fill","blue")
.style("fill-opacity", 0.3)
//.style("stroke","grey")
//.style("stroke-opacity",0.1)
// Tooltip animation, modified from: http://bl.ocks.org/d3noob/257c360b3650b9f0a52dd8257d7a2d73
.on("mouseover", function (d) {
div.transition()
.duration(50)
.style("opacity", 0.9);
div.html("ID: " + d.ID + "<br /> Cov: " + d.Avg_fold + "<br />GC: " + d.Ref_GC + "<br /> Len: " + d.Length)
.style("left",(d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout",function(d) {
div.transition()
.duration(90)
.style("opacity",0);
});
});
function filter(lenmin) {
chart.selectAll("circle")
.filter(function(d) { return d.Length < lenmin; })
.attr("display", "none");
chart.selectAll("circle")
.filter(function(d) { return d.Length >= lenmin; })
.attr("display", "initial");
};
</script>
</body>