Skip to content

Instantly share code, notes, and snippets.

@nguyenbq
Last active March 17, 2016 16:09
Show Gist options
  • Save nguyenbq/b18d0144ce09e1acee26 to your computer and use it in GitHub Desktop.
Save nguyenbq/b18d0144ce09e1acee26 to your computer and use it in GitHub Desktop.
Genome Viewer
[{
"name": 1,
"start": 124,
"stop": 1410,
"direction": "forward",
"product": "dnaA",
"subnum": "13",
"sequence": "ATAGTCGATGCTAGTATGCGCATCCGGCGCGATCGTGCGTGCTAGC",
"translation": "amino acids"
},
{
"name": 2,
"start": 1413,
"stop": 2522,
"direction": "forward",
"product": "DNA Polymerase III",
"sequence": "ATAGTCGATGCTAGTATGCGCGTGCGTGCTAGC",
"subnum": "123",
"translation": "amino acids"
},
{
"name": 3,
"start": 2617,
"stop": 5070,
"direction": "forward",
"product": "DNA gyrase",
"sequence": "ATAGTCGATGCGTGCTAGC",
"subnum": "76",
"translation": "amino acids"
},
{
"name": 4,
"start": 5189,
"stop": 5917,
"direction": "reverse",
"product": "Transcriptional regulator",
"sequence": "ATAGTCGATCGCGGCGGCATGCATGCATGCGCGTGCTAGC",
"subnum": "654",
"translation": "amino acids"
},
{
"name": 5,
"start": 6100,
"stop": 7527,
"direction": "reverse",
"product": "probable amidase",
"sequence": "ATAGTCGATGCTAGTATGCATCGTACGATCCGAGCGCGCGATCGTGCGTGCTAGC",
"subnum": "82",
"translation": "amino acids"
},
{
"name": 6,
"start": 7566,
"stop": 8537,
"direction": "forward",
"product": "Tricarboxylate transport protein TctC",
"sequence": "ATAGTCGATGCTAGTATGCGCGCGCTATCGCGGCGCGCGCGCGCGGATCGTGCGTGCTAGC",
"subnum": "41",
"translation": "amino acids"
},
{
"name": 7,
"start": 8956,
"stop": 10119,
"direction": "forward",
"product": "L-lactate dehydrogenase",
"sequence": "ATAGTCGATGCTAGTATGCGCGCGCCGCGGCGCGGGGCGCGCGATCGTGCGTGCTAGC",
"subnum": "38",
"translation": "amino acids"
}
]
<!DOCTYPE html>
<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="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<style>
body {
margin:45;
position:fixed;
top:0;
right:0;
bottom:0;
left:0;
}
/* 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;
}
</style>
</head>
<body>
<script>
// Genome size
var genomelength = 100000;
var tickMarks = {
thousand: [],
fivehundred: [],
onehundred: []
};
var genome_positions = [];
for (var i = 1; i <= genomelength; i++) {
genome_positions.push(i);
}
genome_positions.forEach(function(currentValue, index, myArray){
if (currentValue % 1000 === 0) {
tickMarks.thousand.push(currentValue);
}
else if (currentValue % 500 === 0) {
tickMarks.fivehundred.push(currentValue);
}
else if (currentValue % 100 === 0) {
tickMarks.onehundred.push(currentValue);
}
});
var svg = d3.select("body")
.append("svg")
.attr({height: 500,width: genomelength/10});
svg.append("rect")
.attr({x: 0, y: 100, width: genomelength/10, height: 30})
.style({"stroke-width":"2px","fill":"white","stroke":"black"});
d3.json("genes.json.txt", function(error, json) {
if (error) return console.warn(error);
var genes = svg.selectAll(".genes")
.data(json)
.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 [-55, -48];
}
else if (d.direction == "reverse") {
return [-55, -48];
}
})
.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 [70, -48];
}
else if (d.direction == "reverse") {
return [70, -48];
}
})
.html(function(d) {
return "<a style=color:white target=_blank href=http://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 (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);
});
//grouping the 3 tick groups and displaying them
var group = svg.selectAll(".a")
.data(tickMarks.thousand)
.enter()
.append("g");
group.append("rect")
.style({"fill":"black"})
.attr({x: 0, 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:110})
.attr("x", function(d) {
return (d/10) + 1
})
.style({"font-size":"20px","fill":"red"})
.transition().duration(2000).delay(1000)
.style({"font-family":"verdana","font-size":"10px","fill":"green"})
.text(function(d) {return d/1000})
var group2 = svg.selectAll(".b")
.data(tickMarks.fivehundred)
.enter()
.append("g");
group2.append("rect")
.style({"fill": "black"})
.attr({x: 0, y: 100, width: "1px", height: 15})
.transition().duration(2000)
.attr("transform", function (d) {
return "translate(" + d/10 + ",0)";
});
var group3 = svg.selectAll(".c")
.data(tickMarks.onehundred)
.enter()
.append("g");
group3.append("rect")
.style({"fill": "black"})
.attr({x: 0, y: 115, width: "1px", height: 15})
.transition().duration(1000)
.attr("transform", function (d) {
return "translate(" + d/10 + ",0)";
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment