Skip to content

Instantly share code, notes, and snippets.

@smith13mr
Last active March 3, 2016 15:26
Show Gist options
  • Save smith13mr/4742802e9cf761912270 to your computer and use it in GitHub Desktop.
Save smith13mr/4742802e9cf761912270 to your computer and use it in GitHub Desktop.
genome browser 2 with numbers
[{
"name": 1,
"sequence": "ATGCTACGTAGCTACGTACGATCGTACGATCGATCGTACG",
"start": 50,
"stop": 2000,
"direction": "forward"
},
{
"name": 2,
"sequence": "GACTGACTGACTAGCTGACTGATCGTACTCGAT",
"start": 2150,
"stop": 2900,
"direction": "forward"
},
{
"name": 3,
"sequence": "ATCGTACGTACGTACGTACGATCGTACGATCGCGCGCGCGGCGCATCGATCGATCGTACTA",
"start": 3000,
"stop": 4550,
"direction": "reverse"
},
{
"name": 4,
"sequence": "TTCTAGTCGATCGTACGTGTACGTGCATTATTGATGCTATATATCGTATATACGTA",
"start": 4700,
"stop": 7550,
"direction": "reverse"
}
]
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
</head>
<body>
<script>
var genomelength = 9000;
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("x", function (d) { return d.start/10; })
.attr("y", function (d) { return 100; })
.attr("height", function(d) { return 0;})
.attr("width", function (d) { return (d.stop-d.start)/10; })
.attr("style", function (d) { return (d.dirction)})
.style ("fill", function (d) {
if (d.direction === "forward") {
return "cyan";
}
else if (d.direction === "reverse") {
return "pink";
}
else {
return "black";
}
})
.attr("y", function (d) {
if (d.direction === "forward") {
if (d.name%2 === 0)
return 70;
else {
return 40;
}
}
else {
if (d.name%2 === 0)
return 130;
else {
return 170;
}
}
})
.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;
}
})
.style ({"stroke-width": "2px", "stroke": "black"})
.transition().delay(3000).duration(1000)
.attr("height", function (d) {
return 30;
})
.attr("x", function (d) {
return d.start/10;
});
genes.append("text") // gene name
.attr("x", function (d) {
return ((d.stop+d.start)/2)/10;
})
.attr("y", function (d) {
if (d.direction === "forward") {
if (d.name%2 === 0)
return 90;
else {
return 60;
}
}
else {
if (d.name%2 === 0)
return 150;
else {
return 190;
}
}
})
.transition().delay(4000).duration(1000)
.text ( function (d) { return d.name; })
.style({"text-anchor": "middle", "font-family": "ariel", "font-weight": "bold"})
.style ("fill", function(d) {
if (d.direction === "forward") {
return "black";
}
else {
return "black";
}
});
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)"; });
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)"; })
group.append("text")
.text(function (d,i) {
return i+1;
})
.attr({y: 115, width: "1px", height: 15})
.attr("x", function(d) {return (d/10)+2;})
.attr({"font-size": "10px", "fill":"grey"});
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment