Last active
December 8, 2015 04:08
-
-
Save philippbayer/5c51f1a373e45c55747e to your computer and use it in GitHub Desktop.
Simple D3 gene expression example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.node { | |
stroke: #fff; | |
stroke-width: 1.5px; | |
} | |
.link { | |
stroke: #999; | |
stroke-opacity: .6; | |
} | |
.text { | |
pointer-events: none; | |
font: 10px sans-serif; | |
} | |
</style> | |
<body> | |
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var color = d3.scale.category20(); | |
var force = d3.layout.force() | |
.charge(-1000) | |
.linkDistance(200) | |
.size([width, height]); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
d3.json("./proteins.json", function(error, graph) { | |
if (error) throw error; | |
force | |
.nodes(graph.nodes) | |
.links(graph.links) | |
.start(); | |
var link = svg.selectAll(".link") | |
.data(graph.links) | |
.enter().append("line") | |
.attr("class", "link") | |
.style("stroke-width", function(d) { return d.value; })//Math.sqrt(d.value); }); | |
.style("stroke", function(d) { | |
if (d.type == "is_expressed_in") { | |
return "green"; | |
} | |
} | |
); | |
link.append("title") | |
.text(function (d) { return d.type; }); | |
var node = svg.selectAll(".node") | |
.data(graph.nodes) | |
.enter().append("g") | |
.style("fill", function(d) { return color(d.group); }) | |
.call(force.drag); | |
node.append("circle") | |
.attr("r", 10); | |
node.append("title") | |
.text(function(d) { return d.name; }); | |
node.append("text") | |
.attr("dx", 12) | |
.attr("dy", ".35em") | |
.text(function (d) { | |
return d.name; | |
}); | |
force.on("tick", function() { | |
link.attr("x1", function(d) { return d.source.x; }) | |
.attr("y1", function(d) { return d.source.y; }) | |
.attr("x2", function(d) { return d.target.x; }) | |
.attr("y2", function(d) { return d.target.y; }); | |
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }); | |
}); | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nodes":[ | |
{"name":"ATG_1","group":1}, | |
{"name":"BRAGP2","group":2}, | |
{"name":"ATG_2","group":1}, | |
{"name":"BR_3","group":2}, | |
{"name":"Brapa_4","group":2}, | |
{"name":"Brassica_9","group":2}, | |
{"name":"Root tissue", "group":3}, | |
{"name":"Flower tissue", "group":3}, | |
{"name":"Leaf tissue", "group":3} | |
], | |
"links":[ | |
{"source":0, "target":2, "value":10, "type":"is_homologous"}, | |
{"source":0, "target":1, "value":3, "type":"is_homologous"}, | |
{"source":2, "target":3, "value":2, "type":"is_homologous"}, | |
{"source":2, "target":4, "value":2, "type":"is_homologous"}, | |
{"source":2, "target":5, "value":2, "type":"is_homologous"}, | |
{"source":1, "target":6, "value":5, "type":"is_expressed_in"}, | |
{"source":1, "target":7, "value":10, "type":"is_expressed_in"}, | |
{"source":1, "target":8, "value":1, "type":"is_expressed_in"}, | |
{"source":3, "target":6, "value":1, "type":"is_expressed_in"}, | |
{"source":3, "target":7, "value":10, "type":"is_expressed_in"}, | |
{"source":3, "target":8, "value":1, "type":"is_expressed_in"}, | |
{"source":4, "target":6, "value":1, "type":"is_expressed_in"}, | |
{"source":4, "target":7, "value":1, "type":"is_expressed_in"}, | |
{"source":4, "target":8, "value":1, "type":"is_expressed_in"}, | |
{"source":5, "target":6, "value":1, "type":"is_expressed_in"}, | |
{"source":5, "target":7, "value":1, "type":"is_expressed_in"}, | |
{"source":5, "target":8, "value":10, "type":"is_expressed_in"} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment