Built with blockbuilder.org
forked from JohnWall64's block: fresh block
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="//d3js.org/d3.v3.min.js" language="JavaScript"></script>
<script src="https://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<style>
body {
margin: ;
position:fixed;
top:0;
right:0;
bottom:0;
left:2;
}
/* Tip box */
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 11px;
background: rgba(0, 0, 0, 0.8);
color: #ff0505;
border-radius: 2px;
}
/* This is for the little triangle for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
content: "\25BC";
position: absolute;
text-align: center;
}
/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
/* Lower tip box */
.d3-hidden {
line-height: 1;
font-weight: bold;
padding: 11px;
background: rgba(1, 34, 34, 0.8);
color: #1fe429;
border-radius: 2px;
}
/* This is for the little triangle for the tooltip */
.d3-hidden:after {
box-sizing: border-box;
display: inline;
font-size: 20px;
width: 100%;
line-height: 1;
color: rgba(1, 0, 0, 0.8);
content: "\25BC";
position: absolute;
text-align: center;
}
/* Style northward tooltips differently */
.d3-hidden.n:after {
margin: -60px 0 0 0;
top: 100%;
left: 0;
}
a {
cursor: pointer;
text-decoration: underline;
color: black;
}
#download {
border: 1px solid silver;
position: absolute;
opacity: 0;
}
</style>
</head>
<body>
<script>
var svg = d3.select("body")
.append("svg")
.attr("height", 1000);
d3.json("genes.json.txt", function(error, json) {
if (error) return console.warn(error);
svg.attr("width", function(d) {
return d3.max(json, function(d) {
return d.genomelength/10;
})
});
var sequences = svg.selectAll(".genomes")
.data(json)
.enter()
.append("g");
sequences.attr("transform", function(d, i) {
return "translate(0," + (10 + (i*225)) + ")";
});
//starting the ruler
sequences.append("rect")
.attr({x: -500, y: 100, width: function(d) {
return d.genomelength/10; }, height: 30
})
.style({"stroke-width": "2px", "fill": "white", "stroke": "black"})
.attr("stroke-opacity", 0)
.transition(1000).duration(1000)
.attr({x: 0})
.attr("stroke-opacity", 1);
//grouping the 3 tick groups and displaying them
var group = sequences.selectAll(".a")
.data(function (d) {
ticks = [];
genome_positions = d3.range(d.genomelength);
genome_positions.forEach(function (currentValue, index, myArray) {
if (currentValue % 1000 === 0) {
ticks.push(currentValue);
}
});
return ticks;
})
.enter()
.append("g");
group.append("rect")
.style({"fill":"black"})
//.attr({x: 0, y: 100, width: "1px", height: 30})
.attr({x: function (d) { return d/10; }, y: 100, width: "1px", height: 30})
.transition().duration(3000)
.attr("transform", function (d) { return "translate(" + d/10 + ",0)"; });
//numbers @1000 ticks
group.append("text")
.attr("y", 112)
.attr("x", function(d) {
return (d/10) + 1
})
.style({"font-size":"20px","fill":"red"})
.transition().duration(2000).delay(2000)
.style({"font-family":"verdana","font-size":"10px","fill":"green"})
.text(function(d,i) {
return i;
})
var group2 = sequences.selectAll(".b")
.data(function(d) {
ticks = [];
genome_positions = d3.range(d.genomelength);
genome_positions.forEach(function (currentValue, index, myArray) {
if (currentValue % 500 === 0) {
ticks.push(currentValue);
}
});
return ticks;
})
.enter()
.append("g");
group2.append("rect")
.style({"fill": "black"})
//.attr({x: 0, y: 100, width: "1px", height: 15})
.attr({x: function(d) {return d/10;}, y: 100, width: "1px", height: 15})
.transition().duration(3000)
.attr("transform", function (d) {
return "translate(" + d/10 + ",0)";
});
var group3 = sequences.selectAll(".onehundredticks")
.data(function(d) {
ticks = [];
genome_positions = d3.range(d.genomelength);
genome_positions.forEach(function (currentValue, index, myArray) {
if (currentValue % 100 === 0 & currentValue % 500 !== 0
& currentValue % 1000 !== 0) {
ticks.push(currentValue);
}
});
return ticks;
})
.enter()
.append("g");
group3.append("rect")
.style({"fill": "black"})
//.attr({x: 0, y: 115, width: "1px", height: 15})
.attr({x: function (d) { return d/10; }, y: 115, width: "1px", height: 15})
.transition().duration(3000)
.attr("transform", function (d) {
return "translate(" + d/10 + ",0)";
});
var genes = sequences.selectAll(".genes")
.data(function(d) { return d.genes;})
.enter()
.append("g");
//Hover box when you mouseover genes
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset(function (d) {
if (d.direction == "forward") {
return [-20, -8];
}
else if (d.direction == "reverse") {
return [-20, -8];
}
})
.html(function(d) {
return "<b>Product:</b> <span style='color:white'>"
+ d.product + "</span>";
})
//Clickable tip box attr
var hidden = d3.tip()
.attr('class', 'd3-hidden')
.offset(function (d) {
if (d.direction == "forward") {
return [80, -8];
}
else if (d.direction == "reverse") {
return [80, -8];
}
})
.html(function(d) {
return "<a style=color:white target=_blank href=https://blast.ncbi.nlm.nih.gov/Blast.cgi>BLAST:</a><span style='color:white'>"
+ " " + d.translation + "</span>";
})
//call the "tool-tips"
svg.call(tip);
svg.call(hidden);
//add in gene boxes with transistion
genes.append("rect")
.on('mouseover', tip.show).on('mouseout', tip.hide)
.on('click', hidden.show).on('dblclick', hidden.hide)
.attr("x", function (d) {
if (d.direction === "forward") {
return (0 - ((d.stop-d.start)/10)) - 2;
}
else if (d.direction === "reverse") {
return (d.genomelength/10) + 2;
}
})
.attr("y", function (d) { return 100; })
.attr("height", function(d) { return 0; })
.attr("width", function (d) { return (d.stop-d.start)/10; })
.style("fill", function(d) {
if (d.direction === "forward") {
return "009900";
}
else if (d.direction === "reverse"){
return "FF6600";
}
else {
return "yellow";
}
})
.style({"stroke":"black", "stroke-width":"2px"})
.transition().delay(2000).duration(2000)
.attr("x", function (d) {
return d.start/10;
})
.attr("y", function (d) {
if (d.direction == "forward" && d.name%2==0) {
return 34;
}
else if (d.direction == "forward" && d.name%2!==0) {
return 67;
}
else if (d.direction == "reverse" && d.name%2==0) {
return 133;
}
else if (d.direction == "reverse" && d.name%2!==0) {
return 165;
}
else {
return 60;
}
})
.attr("height", function (d) {return 30;})
group.on("click", function() {
console.log("rect");
})
//text in gene boxes
genes.append("text")
.attr("x", function (d) { return ((d.start+d.stop)/10)/2})
.style("text-anchor","middle")
.style({"font-family":"verdana","font-size":"18px","fill":"white"})
.attr("y", function (d) {
if (d.direction === "forward" && d.name%2==0) {
return 58;
}
else if (d.direction === "forward" && d.name%2!==0) {
return 91;
}
else if (d.direction === "reverse" && d.name%2==0) {
return 151;
}
else if (d.direction === "reverse" && d.name%2!==0) {
return 185;
}
else {
return 80;
}
})
.text(function (d) {
return d.name;})
//var c = 0;
//var g = 0;
//for (var i=0; i<=d.sequence.length; i++) {
// if (d.sequence[i] === "C") {c++; }
// else if (d.sequence[i] === "G") {g++;}
//}
// return (((c+g)/d.sequence.length)*100);})
//.attr("fill-opacity",0)
//.transition().duration(3000).delay(2000)
//.attr("fill-opacity",1);
// text above or below boxes
genes.append("text")
.attr("x", function(d) { return ((d.start + d.stop)/2)/10;})
.attr("y", function (d) {
if (d.direction == "forward") {
if (d.name % 2 === 0) {
return 28;
}
else { return 59;}
}
else if (d.direction == "reverse") {
if (d.name % 2 === 0) {
return 181;
}
else { return 213;}
}
})
.style({"text-anchor":"middle","fill":"black","font-weight":"bold"})
.attr("font-family","sans-serif")
.text(function(d) {return d.subnum})
.attr("fill-opacity", 0)
.transition().delay(4500).duration(1000)
.attr("fill-opacity", 1);
});
</script>
</body>
Modified http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js to a secure url
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js
https://d3js.org/d3.v3.min.js
https://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js