Skip to content

Instantly share code, notes, and snippets.

@nguyenbq
Last active March 30, 2016 22:33
Show Gist options
  • Save nguyenbq/00c36fbe045e5d0290fc to your computer and use it in GitHub Desktop.
Save nguyenbq/00c36fbe045e5d0290fc to your computer and use it in GitHub Desktop.
Genome Viewer Final
[{
"genomename": "Bp190",
"genomelength": 11000,
"genes": [{
"name": 1,
"start": 124,
"stop": 1410,
"direction": "forward",
"product": "dnaA",
"subnum": "13",
"snp": "1004",
"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"
}]
},
{
"genomename": "Bp1865",
"genomelength": 7528,
"genes": [{
"name": 1,
"start": 124,
"stop": 1000,
"direction": "forward",
"product": "dnaA",
"subnum": "323",
"sequence": "ATAGTCGATGCTAGTATGCGCATCCGGCGCGATCGTGCGTGCTAGC",
"translation": "amino acids"
},
{
"name": 2,
"start": 1200,
"stop": 2100,
"direction": "reverse",
"product": "DNA Polymerase III",
"sequence": "ATAGTCGATGCTAGTATGCGCGTGCGTGCTAGC",
"subnum": "3456",
"translation": "amino acids"
},
{
"name": 3,
"start": 2000,
"stop": 4032,
"direction": "forward",
"product": "DNA gyrase",
"sequence": "ATAGTCGATGCGTGCTAGC",
"subnum": "966",
"translation": "amino acids"
},
{
"name": 4,
"start": 4300,
"stop": 5502,
"direction": "reverse",
"product": "Transcriptional regulator",
"sequence": "ATAGTCGATCGCGGCGGCATGCATGCATGCGCGTGCTAGC",
"subnum": "211",
"translation": "amino acids"
},
{
"name": 5,
"start": 5402,
"stop": 7500,
"direction": "reverse",
"product": "probable amidase",
"sequence": "ATAGTCGATGCTAGTATGCATCGTACGATCCGAGCGCGCGATCGTGCGTGCTAGC",
"subnum": "1082",
"translation": "amino acids"
}]
},
{
"genomename": "Bp194",
"genomelength": 8000,
"genes": [{
"name": 1,
"start": 100,
"stop": 1200,
"direction": "reverse",
"product": "dnaA",
"subnum": "3",
"sequence": "ATAGTCGATGCTAGTATGCGCATCCGGCGCGATCGTGCGTGCTAGC",
"translation": "amino acids"
},
{
"name": 2,
"start": 1213,
"stop": 2000,
"direction": "reverse",
"product": "DNA Polymerase III",
"sequence": "ATAGTCGATGCTAGTATGCGCGTGCGTGCTAGC",
"subnum": "3539",
"translation": "amino acids"
},
{
"name": 3,
"start": 3000,
"stop": 4800,
"direction": "forward",
"product": "DNA gyrase",
"sequence": "ATAGTCGATGCGTGCTAGC",
"subnum": "849",
"translation": "amino acids"
},
{
"name": 4,
"start": 5000,
"stop": 5417,
"direction": "reverse",
"product": "Transcriptional regulator",
"sequence": "ATAGTCGATCGCGGCGGCATGCATGCATGCGCGTGCTAGC",
"subnum": "2031",
"translation": "amino acids"
},
{
"name": 5,
"start": 5700,
"stop": 7600,
"direction": "forward",
"product": "probable amidase",
"sequence": "ATAGTCGATGCTAGTATGCATCGTACGATCCGAGCGCGCGATCGTGCGTGCTAGC",
"subnum": "12",
"translation": "amino acids"
}]
}]
<!DOCTYPE html>
<!-- Check Out BioJS -->
<head>
<meta charset="utf-8">
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js"></script>
<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: ;
position:fixed;
top:0;
right:0;
bottom:0;
left:2;
background-image: linear-gradient(to bottom, #003973, #E5E5BE 100%);
}
/* 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;
}
/* Margins for the little triangle */
.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: "\25B2";
position: absolute;
text-align: center;
}
/* Margins for the little triangle */
.d3-hidden.n:after {
margin: -55px 0 0 0;
top: 100%;
left: 0;
}
/* For download Link */
#download {
cursor: pointer;
text-decoration: none;
color: white;
}
.dot {
stroke: #000;
}
.button {
background-color: #4CAF50;
border: none;
color: white;
padding: 10px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="viz"></div>
<a href="#" id="download">Download SVG</a>
<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)) + ")";
});
//Names of the Sequences
sequences.append("text")
.attr({x:-500})
.style({"font-family":"verdana","font-size":"18px","fill":"orange"})
.transition(1000).delay(3000).duration(1000).ease("bounce")
.attr({x:10, y:20})
.text(function(d) {
return d.genomename;
});
//starting the ruler
sequences.append("rect")
.attr({x: -800, y: 100, width: function(d) {
return d.genomelength/10; }, height: 30
})
.style({"stroke-width": "2px", "fill": "white", "stroke": "#9932CC",})
.attr("stroke-opacity", 0)
.transition(1000).duration(3000)
.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: -100})
.transition().duration(3000)
.attr({x: function (d) { return d/10; }, y: 100, width: "1px", height: 30})
.attr({"fill-opacity": 1})
//.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 & currentValue % 1000 !== 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:-100})
.transition().duration(3000)
.attr({x: function(d) {return d/10;}, y: 100, width: "1px", height: 15})
//.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: -100})
.transition().duration(3000)
.attr({x: function (d) { return d/10; }, y: 115, width: "1px", height: 15})
//.attr("transform", function (d) { return "translate(" + d/10 + ",0)"; });
//Attaching the coming gene boxes to the graph w/the data
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 [-33, -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
var hidden = d3.tip()
.attr('class', 'd3-hidden')
.offset(function (d) {
if (d.direction == "forward") {
return [80, -8];
} else if (d.direction == "reverse") {
return [95, -8];
}
})
.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);
//The gene boxes attributes
var geneboxes = genes.append("rect")
.attr('class', 'boxes')
.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("rx", "10")
.attr("ry", "10")
.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", function(d) {
if (d.direction === "forward") {
return "#006600";
} else if (d.direction === "reverse") {
return "#993d00";
} else {
return "yellow";
}
})
.style({"stroke-width":"2.5px"})
.transition().delay(2000).duration(2000)
.attr("x", function (d) {
return d.start/10;
})
.attr("y", function (d) {
if (d.direction == "forward") {
if (d.name % 2 ==0) {
return 34;
} else {
return 67;
}
} else if (d.direction == "reverse") {
if (d.name %2 == 0) {
return 133;
} else {
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") {
if (d.name %2 == 0) {
return 58;
} else {
return 91;
}
} else if (d.direction === "reverse") {
if (d.name %2 == 0) {
return 151;
} else {
return 185;
}
} else {
return 80;
}
})
.transition(1000).delay(4000)
.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);
//Show SNPs
function SNPs() {
var snp = genes.selectAll(".dot")
.data("genes.json.txt")
.enter().append("circle")
.attr("class", "dot")
.transition().delay(6000).duration(2000)
.attr("r", 4)
.attr({cx:323})
.attr({cy:10})
.style("fill", "red");
}
d3.select("#download")
.on("mouseover", writeDownloadLink);
//Right-Click for Download SVGs
function writeDownloadLink(){
var html = d3.select("svg")
.attr("title", "svg_title")
.attr("version", 1.1)
.attr("xmlns", "http://www.w3.org/2000/svg")
.node().parentNode.innerHTML;
d3.select(this)
.attr("href-lang", "image/svg+xml")
.attr("href", "data:image/svg+xml;base64,\n" + btoa(html))
.on("mousedown", function(){
if(event.button != 2){
d3.select(this)
.attr("href", null)
.html("Use right click");
}
})
.on("mouseout", function(){
d3.select(this)
.html("Download SVG");
});
};
});
</script>
<button onclick="SNPs()" class="button">Show SNPs</button>
</body>
position start color circlesize type
4 69471 1.4 0.2 versicolor
4 153056 1.4 0.2 versicolor
4 297557 1.3 0.2 versicolor
4 297664 1.5 0.2 versicolor
4 361393 1.4 0.2 versicolor
4 551712 1.7 0.4 versicolor
4 559145 1.4 0.3 versicolor
4 559148 1.5 0.2 versicolor
4 559149 1.4 0.2 versicolor
4 569431 1.5 0.1 versicolor
4 690353 1.5 0.2 versicolor
4 712252 1.6 0.2 versicolor
4 856447 1.4 0.1 versicolor
4 988739 1.1 0.1 versicolor
4 1238490 1.2 0.2 versicolor
4 1298210 1.5 0.4 versicolor
4 1301714 1.3 0.4 versicolor
4 1365852 1.4 0.3 versicolor
4 1397585 1.7 0.3 versicolor
4 1464312 1.5 0.3 versicolor
4 1464315 1.7 0.2 versicolor
4 1551478 1.5 0.4 versicolor
4 1595933 1.0 0.2 versicolor
4 1693834 1.7 0.5 versicolor
4 1723917 1.9 0.2 versicolor
4 1786549 1.6 0.2 versicolor
4 1829948 1.6 0.4 versicolor
4 1917635 1.5 0.2 versicolor
4 2212659 1.4 0.2 versicolor
4 2320234 1.6 0.2 versicolor
4 2320818 1.6 0.2 versicolor
4 2455429 1.5 0.4 versicolor
4 2468583 1.5 0.1 versicolor
4 2521544 1.4 0.2 versicolor
4 2558033 1.5 0.2 versicolor
4 2558037 1.2 0.2 versicolor
4 2600545 1.3 0.2 versicolor
4 2655449 1.4 0.1 versicolor
4 2665782 1.3 0.2 versicolor
4 2743657 1.5 0.2 versicolor
4 2756564 1.3 0.3 versicolor
4 2904494 1.3 0.3 versicolor
4 2904495 1.3 0.2 versicolor
4 2905079 1.3 0.2 versicolor
4 2905447 1.9 0.4 versicolor
4 2919430 1.4 0.3 versicolor
4 2989784 1.6 0.2 versicolor
4 3232364 1.4 0.2 versicolor
4 3293144 1.5 0.2 versicolor
4 3325443 1.4 0.2 versicolor
4 3326265 4.7 1.4 versicolor
4 3326278 4.5 1.5 versicolor
4 3326381 4.9 1.5 versicolor
4 3326528 4.0 1.3 versicolor
4 3399991 4.6 1.5 versicolor
4 3502927 4.5 1.3 versicolor
4 3508865 4.7 1.6 versicolor
4 3512596 3.3 1.0 versicolor
4 3787957 4.6 1.3 versicolor
4 3845351 3.9 1.4 versicolor
4 3845754 3.5 1.0 versicolor
6 69471 1.4 0.2 setosa
6 153056 1.4 0.2 setosa
6 297557 1.3 0.2 setosa
6 297664 1.5 0.2 setosa
6 361393 1.4 0.2 setosa
6 551712 1.7 0.4 setosa
6 559145 1.4 0.3 setosa
6 559148 1.5 0.2 setosa
6 559149 1.4 0.2 setosa
6 569431 1.5 0.1 setosa
6 690353 1.5 0.2 setosa
6 712252 1.6 0.2 setosa
6 856447 1.4 0.1 setosa
6 988739 1.1 0.1 setosa
6 1238490 1.2 0.2 setosa
6 1298210 1.5 0.4 setosa
6 1301714 1.3 0.4 setosa
6 1365852 1.4 0.3 setosa
6 1397585 1.7 0.3 setosa
6 1464312 1.5 0.3 setosa
6 1464315 1.7 0.2 setosa
6 1551478 1.5 0.4 setosa
6 1595933 1.0 0.2 setosa
6 1693834 1.7 0.5 setosa
6 1723917 1.9 0.2 setosa
6 1786549 1.6 0.2 setosa
6 1829948 1.6 0.4 setosa
6 1917635 1.5 0.2 setosa
6 2212659 1.4 0.2 setosa
6 2320234 1.6 0.2 setosa
6 2320818 1.6 0.2 setosa
6 2455429 1.5 0.4 setosa
6 2468583 1.5 0.1 setosa
6 2521544 1.4 0.2 setosa
6 2558033 1.5 0.2 setosa
6 2558037 1.2 0.2 setosa
6 2600545 1.3 0.2 setosa
6 2655449 1.4 0.1 setosa
6 2665782 1.3 0.2 setosa
6 2743657 1.5 0.2 setosa
6 2756564 1.3 0.3 setosa
6 2904494 1.3 0.3 setosa
6 2904495 1.3 0.2 setosa
6 2905079 1.3 0.2 setosa
6 2905447 1.9 0.4 setosa
6 2919430 1.4 0.3 setosa
6 2989784 1.6 0.2 setosa
6 3232364 1.4 0.2 setosa
6 3293144 1.5 0.2 setosa
6 3325443 1.4 0.2 setosa
6 3326265 4.7 1.4 setosa
6 3326278 4.5 1.5 setosa
6 3326381 4.9 1.5 setosa
6 3326528 4.0 1.3 setosa
6 3399991 4.6 1.5 setosa
6 3502927 4.5 1.3 setosa
6 3508865 4.7 1.6 setosa
6 3512596 3.3 1.0 setosa
6 3787957 4.6 1.3 setosa
6 3845351 3.9 1.4 setosa
6 3845754 3.5 1.0 setosa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment