Skip to content

Instantly share code, notes, and snippets.

@jczaplew
Created February 13, 2014 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jczaplew/8978255 to your computer and use it in GitHub Desktop.
Save jczaplew/8978255 to your computer and use it in GitHub Desktop.
"buildTaxonHierarchy": function(data) {
var occurrenceTree = {"phyla": []};
// Some preproccessing
data.records.forEach(function(d) {
d.rank = (d.rnk) ? taxaBrowser.rankMap(d.rnk) : "Unknown";
d.itallics = (d.rnk < 6) ? "itallics" : "";
d.old_name = (d.tna.split(" ")[0] != d.idt) ? d.tna : "";
d.display_name1 = (d.tna === (d.idt + " " + d.ids)) ? d.tna : d.idt;
d.display_name2 = (d.tna === (d.idt + " " + d.ids)) ? d.tna : d.ids;
});
// Find unique phyla
data.records.forEach(function(d) {
var phyla = [];
for (var i = 0; i < occurrenceTree.phyla.length; i++) {
phyla.push(occurrenceTree.phyla[i].phylum);
}
if (phyla.indexOf(d.phl) < 0) {
var newPhylum = {"phylum": d.phl, "classes": []};
occurrenceTree.phyla.push(newPhylum);
}
});
// Find unique phylum/class combinations
data.records.forEach(function(d) {
var phyla_classes = [];
for (var i = 0; i < occurrenceTree.phyla.length; i++) {
for (var j = 0; j < occurrenceTree.phyla[i].classes.length; j++) {
phyla_classes.push(occurrenceTree.phyla[i].phylum + "-" + occurrenceTree.phyla[i].classes[j].nameClass);
}
}
if (phyla_classes.indexOf(d.phl + "-" + d.cll) < 0) {
var newClass = {"nameClass": d.cll, "families": []},
phylumIndex = navMap.getIndex(occurrenceTree.phyla, d.phl, "phylum");
occurrenceTree.phyla[phylumIndex]["classes"].push(newClass);
}
});
// Find unique phylum/class/family combinations
data.records.forEach(function(d) {
var phyla_class_family = [];
for (var i = 0; i < occurrenceTree.phyla.length; i++) {
for (var j = 0; j < occurrenceTree.phyla[i].classes.length; j++) {
for (var k = 0; k < occurrenceTree.phyla[i].classes[j].families.length; k++) {
phyla_class_family.push(occurrenceTree.phyla[i].phylum + "-" + occurrenceTree.phyla[i].classes[j].nameClass + "-" + occurrenceTree.phyla[i].classes[j].families[k].family);
}
}
}
if (phyla_class_family.indexOf(d.phl + "-" + d.cll + "-" + d.fml) < 0) {
var newFamily = {"family": d.fml, "genera": []},
phylumIndex = navMap.getIndex(occurrenceTree.phyla, d.phl, "phylum"),
classIndex = navMap.getIndex(occurrenceTree.phyla[phylumIndex].classes, d.cll, "nameClass");
occurrenceTree.phyla[phylumIndex].classes[classIndex]["families"].push(newFamily);
}
});
// Place genera into the right phylum/class/family
data.records.forEach(function(d) {
var phylumIndex = navMap.getIndex(occurrenceTree.phyla, d.phl, "phylum"),
classIndex = navMap.getIndex(occurrenceTree.phyla[phylumIndex].classes, d.cll, "nameClass"),
familyIndex = navMap.getIndex(occurrenceTree.phyla[phylumIndex].classes[classIndex].families, d.fml, "family");
occurrenceTree.phyla[phylumIndex].classes[classIndex].families[familyIndex].genera.push(d);
});
occurrenceTree.phyla.forEach(function(d) {
if (typeof(d.phylum) === "undefined") {
d.phylum = "Unranked taxa";
d.unranked = true;
}
});
return occurrenceTree;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment