Skip to content

Instantly share code, notes, and snippets.

@lambrex
Last active April 7, 2016 18:57
Show Gist options
  • Save lambrex/6222544da1551c11d55b505edf4b88fa to your computer and use it in GitHub Desktop.
Save lambrex/6222544da1551c11d55b505edf4b88fa to your computer and use it in GitHub Desktop.
genome browser
[{
"phagename": "U2",
"genes": [{
"name": 1,
"pham": "Z1",
"pham_abundance": "20",
"start": 50,
"stop": 2000,
"direction": "reverse",
"sequence": "acgtcgtagt"
},
{
"name": 2,
"pham": "Z2",
"pham_abundance": "3",
"start": 2150,
"stop": 2900,
"direction": "reverse",
"sequence": "tgcatcgtacgta"
},
{
"name": 3,
"pham": "Z3",
"pham_abundance": "50",
"start": 2800,
"stop": 4550,
"direction": "reverse",
"sequence": "cagtcgatcgatcga"
},
{
"name": 4,
"pham": "Z4",
"pham_abundance": "70",
"start": 5700,
"stop": 7550,
"direction": "forward",
"sequence": "cgatcgtagc"
}],
"genomelength": 14000
},
{
"phagename": "L5",
"genes": [{
"name": 1,
"pham": "F1",
"pham_abundance": "85",
"start": 500,
"stop": 1600,
"direction": "forward",
"sequence": "tgactagctagc"
},
{
"name": 2,
"pham": "F2",
"pham_abundance": "100",
"start": 1500,
"stop": 2000,
"direction": "reverse",
"sequence": "cgtacgtacgtacgta"
},
{
"name": 3,
"pham": "F3",
"pham_abundance": "20",
"start": 2200,
"stop": 3300,
"direction": "reverse",
"sequence": "tagctagctagcta"
},
{
"name": 4,
"pham": "F4",
"pham_abundance": "150",
"start": 4000,
"stop": 9500,
"direction": "forward",
"sequence": "acgatcgatcgatc"
}],
"genomelength": 10000
}]
-<!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="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Roboto+Slab:400,100,300,700">
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Roboto:400,100,100italic,300,300italic,400italic,500,500italic,700,700italic,900,900italic">
<style>
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
.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;
}
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 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 tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return d.sequence;
})
svg.call(tip);
var phage = svg.selectAll(".genomes")
.data(json)
.enter()
.append("g");
phage.attr("transform", function(d, i) { return "translate(0," + (100 + (i*225)) + ")"; });
phage.append("rect")
.attr({x: 0, y: 0, width: function(d) { return d.genomelength/10; }, height: 30})
.style({"stroke-width": "2px", "fill": "white", "stroke": "black"})
.attr("stroke-opacity", 0)
.transition().duration(1000)
.attr("stroke-opacity", 1);
var group = phage.selectAll(".thousandticks")
.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: function (d) { return d/10; }, y: 0, width: "1px", height: 30})
.attr({"fill-opacity": 0})
.transition().duration(1500)
.attr({"fill-opacity": 1});
group.append("text") // kbp label
.attr("x", function(d) {return (d/10) + 3;})
.attr("y", 12)
.attr("font-family", "sans-serif")
.attr("font-size", "14px")
.attr("fill", "green")
.style("text-anchor", "start")
.text(function(d) { return d/1000; })
.attr({"fill-opacity": 0})
.transition().duration(1500)
.attr({"fill-opacity": 1});
var group2 = phage.selectAll(".fivehundredticks")
.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: function(d) {return d/10;}, y: 0, width: "1px", height: 15})
.attr({"fill-opacity": 0})
.transition().duration(1500)
.attr({"fill-opacity": 1});
var group3 = phage.selectAll(".onehundredticks")
.data(function (d) {
ticks = [];
genome_positions = d3.range(d.genomelength);
genome_positions.forEach(function (currentValue, index, myArray) {
if (currentValue % 100 === 0 & currentValue % 1000 !== 0 & currentValue % 500 !== 0) {
ticks.push(currentValue);
}
})
return ticks;
})
.enter()
.append("g");
group3.append("rect")
.style({"fill": "black"})
.attr({x: function (d) { return d/10; }, y: 15, width: "1px", height: 15})
.attr("fill-opacity", 0)
.transition().duration(1500)
.attr("fill-opacity", 1);
gene = phage.selectAll(".genes")
.data(function(d, i) { console.log(i, d); return d.genes;})
.enter()
.append("g");
gene.append("rect")
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
.attr("y", function (d) {
if (d.direction == "forward") {
if (d.name % 2 === 0) {
return -70;
}
else { return -30;}
}
else if (d.direction == "reverse") {
if (d.name % 2 === 0) {
return 30;
}
else { return 60;}
}
})
.attr("x", function (d) {
if (d.direction === "forward") {
return (0 - ((d.stop-d.start)/10)) - 2;
}
else if (d.direction === "reverse") {
w = d3.select("svg").style("width");
return w;
}
})
.attr("height", function (d) {return 30;})
.attr("width", function (d) { return (d.stop-d.start)/10; })
.style({"stroke":"black", "stroke-width": "2px"})
.attr("fill", function (d) {
if (d.direction == "forward") {
return "purple";
}
else if (d.direction == "reverse") {
return "blue";
}
else {
return "black";
}
})
.transition().delay(1000).duration(1500)
.attr("x", function (d) { return d.start/10; });
gene.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 -50;
}
else { return -10;}
}
else if (d.direction == "reverse") {
if (d.name % 2 === 0) {
return 50;
}
else { return 80;}
}
})
.style({"text-anchor": "middle", "fill": "white"})
.attr("font-family", "sans-serif")
.text(function(d) {return d.name})
.attr("fill-opacity", 0)
.transition().delay(2000).duration(1500)
.attr("fill-opacity", 1);
gene.append("text") // pham name
.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 -80;
}
else { return -40;}
}
else if (d.direction == "reverse") {
if (d.name % 2 === 0) {
return 80;
}
else { return 110;}
}
})
.style({"text-anchor": "middle", "fill": "blue"})
.attr("font-family", "sans-serif")
.text(function(d) {return d.pham + " ("+ d.pham_abundance +")"})
.attr("fill-opacity", 0)
.transition().delay(3500).duration(1500)
.attr("fill-opacity", 1);
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment