Skip to content

Instantly share code, notes, and snippets.

@jpnoll
Last active March 3, 2016 15:27
Show Gist options
  • Save jpnoll/24265cbbc76cff2a1302 to your computer and use it in GitHub Desktop.
Save jpnoll/24265cbbc76cff2a1302 to your computer and use it in GitHub Desktop.
genome browser
[{
"name": 1,
"pham": 7,
"sequence": ATATAGCAGCGGCGCGCGTAGCGATGCGCGCGCGGCGCGCGGCGCGCAGCGAGCACGGCGAGAGCACGACCAGGACGAGCGAGCGGCAGGGCGAGCGACGGCGCGCG,
"start": 50,
"stop": 2000,
"direction": "forward"
},
{
"name": 2,
"pham": 201,
"sequence": ATCGATCAGCTACGATCAGCTACGACTACGATCGACTACGACTACGAGCGCGCGGACGAGCTACAGCGACGACACTATCGACGAGCATCTACGACTTGAGCT,
"start": 2150,
"stop": 2900,
"direction": "reverse"
},
{
"name": 3,
"pham": 90,
"sequence": ATAGCTAGCATCAGCTACGATCGACTACGATCAGCATCGACTACGTACGACTAGCTACGATCAGCTGATCGACTAGCATCGACTG,
"start": 3000,
"stop": 4550,
"direction": "reverse"
},
{
"name": 4,
"pham": 3752,
"sequence": ATCGATCGATATCGACTTGGCGGATGAGCTGACTGAGCATCGAGCTAGCGATCAGCGATCGACTAGCTACGATCG,
"start": 4700,
"stop": 7550,
"direction": "forward"
}
]
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.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">
</head>
<body>
<script>
var genomelength = 7800;
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");
genes.append("rect")
.attr("y", function (d) {
if (d.direction == "forward") {
if (d.name % 2 === 0) {
return 70;
}
else { return 40;}
}
else if (d.direction == "reverse") {
if (d.name % 2 === 0) {
return 160;
}
else { return 130; }
}
})
.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("height", function (d) {return 30;})
.attr("width", function (d) { return (d.stop-d.start)/10; })
.style({"stroke":"black", "stroke-width": "2px"})
.transition().delay(3000).duration(1500)
.attr("x", function (d) { return d.start/10; })
.attr("fill", function (d) {
if (d.direction == "forward") {
return "green";
}
else if (d.direction == "reverse") {
return "red";
}
else {
return "black";
}
});
genes.append("text") // gene 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 90;
}
else { return 60;}
}
else if (d.direction == "reverse") {
if (d.name % 2 === 0) {
return 180;
}
else { return 150;}
}
})
.style({"text-anchor": "middle", "fill": "black"})
.attr("font-family", "sans-serif")
.text(function(d) {return d.name})
.attr("fill-opacity", 0)
.transition().delay(4500).duration(1000)
.attr("fill-opacity", 1);
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 60;
}
else { return 30;}
}
else if (d.direction == "reverse") {
if (d.name % 2 === 0) {
return 210;
}
else { return 180;}
}
})
.style({"text-anchor": "middle", "fill": "black"})
.attr("font-family", "sans-serif")
.text(function(d) {return d.pham})
.attr("fill-opacity", 0)
.transition().delay(4500).duration(1000)
.attr("fill-opacity", 1);
});
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})
.attr({x: function (d) { return d/10; }, y: 100, width: "1px", height: 30})
.attr({"fill-opacity": 0})
//.transition().duration(3000)
.attr({"fill-opacity": 1});
//.attr("transform", function (d) { return "translate(" + d/10 + ",0)"; });
group.append("text")
.attr("x", function(d) {return (d/10) + 3;})
.attr("y", (90) + 22)
.attr("font-family", "sans-serif")
.attr("font-size", "15px")
.attr("fill", "green")
.style("text-anchor", "start")
.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