Genome browser for phamerator.org
forked from scresawn's block: genome browser
forked from devssunil's block: genome browser
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<style>
body {
margin:45;
position:fixed;
top:10;
right:0;
bottom:0;
left:0;
}
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see!
var genomelength = 9000;
var tickMarks = {thousand: [], fivehundred: [], onehundred: []};
var genome_positions = [];
for (var i = 1; i <= genomelength; i++) {
genome_positions.push(i);
}
//position for tick marks
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"});
//use json file as a input file to work with
d3.json("genes.json.txt", function(error, json) {
if (error) return console.warn(error);
var rects = svg.selectAll(".genes") //
.data(json)
.enter()
.append("rect")
var rectLabels = svg.selectAll(".name")
.data(json)
.enter()
.append("text")
var rectabove = svg.selectAll(".subnum")
.data(json)
.enter()
.append("text")
// labels inside rectangle
var rectLabels = rectLabels
.attr("x", function(d) {
return((d.stop + d.start)/20)
}
)
.attr("y", function(d){
if (d.direction === "forward" && d.name % 2 === 0){
return 50;
} else if (d.direction === "forward" && d.name % 2 != 0) {
return 82;
} else if (d.direction === "reverse" && d.name % 2 === 0){
return 161;
} else if (d.direction === "reverse" && d.name % 2 != 0){
return 193;
} else {
return 60;
}
}
)
.transition().delay(4000).duration(1000)
.text(function (d) {return d.name})
.style({
"text-anchor": "middle",
"font-family":"verdana",
"font-size":"20px",
"fill":"blue"
});
var rectabove = rectabove
.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 8;
}
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);
//draw rectangles
var rectAttributes = rects
.attr("x", function (d) { return d.start/10; })
.attr("y", function (d) { return 90; })
.attr("height", function(d) { return 0;}) //initial height
.attr("width", function (d) { return (d.stop-d.start)/10; })
.attr("fill",function(d) {
if(d.direction === "forward") {
return "green";
}
else if(d.direction === "reverse"){
return "red";
}
else
return "yellow"
})
.style({"stroke":"black", "stroke-width": "2px"})
.transition().delay(3000).duration(1000)
.attr("y", function(d){
if (d.direction === "forward" && d.name % 2 === 0) {
return 11;
} else if (d.direction === "forward" && d.name %2 != 0) {
return 64;
} else if (d.direction === "reverse" && d.name % 2 === 0) {
return 137;
} else if (d.direction === "reverse" && d.name % 2 != 0) {
return 171;
} else {
return 60;
}
}
)
.attr("height", function (d) {return 30;});
});
//stroke for thousand tick mark
var group = svg.selectAll(".a")
.data(tickMarks.thousand)
.enter()
.append("g");
//rectangle svg
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)"; });
//text inside ruler
group.append("text")
.style({
"font-family":"verdana",
"font-size":"10px",
"fill":"black"
})
.transition().delay(5000).duration(1000)
.attr("y", 112)
.attr("x", function(d) {
return d/10 -8;
} )
.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>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js