<commit_before>function depthFirstSearch(node) {
	if (typeof node.children != "undefined") {
		for (var i=0, c=node.children.length; i<c; i++) {
			depthFirstSearch(node.children[i]);
		}
	}
}

function gatherTips(node, tips) {
	if (typeof node.children != "undefined") {
		for (var i=0, c=node.children.length; i<c; i++) {
			gatherTips(node.children[i], tips);
		}
	}
	else {
		tips.push(node);
	}
	return tips;
}

function gatherInternals(node, internals) {
	if (typeof node.children != "undefined") {
		internals.push(node);
		for (var i=0, c=node.children.length; i<c; i++) {
			gatherInternals(node.children[i], internals);
		}
	}
	return internals;
}

function setDates(internals) {
	internals.forEach(function (node) {
		tips = gatherTips(node, []);
		dates = []
		tips.forEach(function (tip) {
			dates.push(tip.date);
		})
		node.date = d3.min(dates);
	})
}

function getVaccines(tips) {
	vaccineChoice = {};
	vaccineChoice['A/Fujian/411/2002'] = "2003-09-25";
	vaccineChoice['A/California/7/2004'] = "2005-02-21";
	vaccineChoice['A/Wisconsin/67/2005'] = "2006-02-21";
	vaccineChoice['A/Brisbane/10/2007'] = "2007-09-25";
	vaccineChoice['A/Perth/16/2009'] = "2009-09-25";
	vaccineChoice['A/Victoria/361/2011'] = "2012-02-21";
	vaccineChoice['A/Texas/50/2012'] = "2013-09-25";
	vaccineChoice['A/Switzerland/9715293/2013'] = "2014-09-25";
	vaccineStrains = Object.keys(vaccineChoice);
	vaccines = [];
	tips.forEach(function (tip) {
		if (vaccineStrains.indexOf(tip.strain) != -1) {
			tip.date = vaccineChoice[tip.strain];
			vaccines.push(tip);
		}
	})
	return vaccines;
}

function setFrequencies(node) {
	if (typeof node.frequency == "undefined") {
		node.frequency = 0.01;
	}
	if (typeof node.children != "undefined") {
		for (var i=0, c=node.children.length; i<c; i++) {
			setFrequencies(node.children[i]);
		}
	}
}

function setDistances(node) {
	if (typeof node.distance_ep == "undefined") {
		node.distance_ep = 0.0;
	}
	if (typeof node.distance_ne == "undefined") {
		node.distance_ne = 0.0;
	}
	if (typeof node.children != "undefined") {
		for (var i=0, c=node.children.length; i<c; i++) {
			setDistances(node.children[i]);
		}
	}
}

/*
function setFrequencies(node, date) {
	if (typeof node.frequencies != "undefined") {
		var sdate = ymd_format(date);
		var dates = [];
		for (var i=0, c=node.frequencies.length; i<c; i++) {
			dates.push(node.frequencies[i].date);
		}
		var index = d3.bisect(dates, sdate) - 1;
		node.frequency = node.frequencies[index].frequency;
	}
	if (typeof node.children != "undefined") {
		for (var i=0, c=node.children.length; i<c; i++) {
			setFrequencies(node.children[i], date);
		}
	}
}
*/

function calcBranchLength(node){
	if (typeof node.children != "undefined") {
	for (var i=0, c=node.children.length; i<c; i++) {
		calcBranchLength(node.children[i]);
		node.children[i].branch_length = node.children[i].xvalue-node.xvalue;
	}
	}
};

function setNodeAlive(node){
	if (typeof node.children != "undefined") {
	for (var i=0, c=node.children.length; i<c; i++) {
		setNodeAlive(node.children[i]);
	}
	}
	node.alive = (node.dateval<dateCutoff)?true:false;
};

function calcUpPolarizers(node){
	node.up_polarizer = 0;
	if (node.alive){
	if (typeof node.children != "undefined") {
		for (var i=0; i<node.children.length; i++) {
		calcUpPolarizers(node.children[i]);
		node.up_polarizer += node.children[i].up_polarizer;
		}
	}
		bl =  node.branch_length/LBItau;
		node.up_polarizer *= Math.exp(-bl);
		node.up_polarizer += LBItau*(1-Math.exp(-bl));
	}
};

function calcDownPolarizers(node){
	if (typeof node.children != "undefined") {
	for (var i1=0; i1<node.children.length; i1++) {
		if (node.children[i1].alive){
		node.children[i1].down_polarizer = node.down_polarizer;
		for (var i2=0; i2<node.children.length; i2++) {
			if (i1!=i2){
			node.children[i1].down_polarizer += node.children[i2].up_polarizer;
			}
		}
		bl =  node.children[i1].branch_length/LBItau;
		node.children[i1].down_polarizer *= Math.exp(-bl);
		node.children[i1].down_polarizer += LBItau*(1-Math.exp(-bl));
		calcDownPolarizers(node.children[i1]);
		}else{node.children[i1].down_polarizer=0;}
	}
	}
};

function calcPolarizers(node){
	calcUpPolarizers(node);
	node.down_polarizer = 0;
	calcDownPolarizers(node);
};

function calcLBI(node, allnodes){
	console.log("Calculating LBI for date cutoff "+dateCutoff);
	calcPolarizers(node);
	allnodes.forEach(function (d) {
	d.LBI=0;
	if (d.alive){
		d.LBI+=d.down_polarizer;
		if (typeof d.children != "undefined") {
		for (var i=0; i<d.children.length; i++) {
			d.LBI += d.children[i].up_polarizer;
		}
		}
	}
	});
};

function calc_deltaLBI(node, allnodes, logdelta){
	console.log("Calculating deltaLBI for date cutoff "+dateCutoff);
	dateCutoff.setDate(dateCutoff.getDate()-deltaLBI_boundary_layer);
	setNodeAlive(node);
	calcLBI(node, allnodes);
	allnodes.forEach(function (d){d.delta_LBI = -d.LBI;});
	dateCutoff.setDate(dateCutoff.getDate()+deltaLBI_boundary_layer);
	setNodeAlive(node);
	calcLBI(node, allnodes);
	allnodes.forEach(function (d){
	if (logdelta) {d.delta_LBI = d.LBI/(-d.delta_LBI+1e-10);}
	else {d.delta_LBI += d.LBI;}
	});
	maxLBI = d3.max(allnodes.map(function (d) {return d.LBI;}));
	maxdeltaLBI = d3.max(allnodes.map(function (d) {return d.delta_LBI;}));
	console.log("maximal LBI: "+maxLBI+", maximal deltaLBI: "+maxdeltaLBI);
	allnodes.forEach(function (d){
	d.delta_LBI /= maxdeltaLBI;
	d.LBI /= maxLBI;
	});
};

function minimumAttribute(node, attr, min) {
	if (typeof node.children != "undefined") {
		for (var i=0, c=node.children.length; i<c; i++) {
			min = minimumAttribute(node.children[i], attr, min);
		}
	}
	else {
		if (node[attr] < min) {
			min = node[attr];
		}
	}
	return min;
}

function maximumAttribute(node, attr, max) {
	if (typeof node.children != "undefined") {
		for (var i=0, c=node.children.length; i<c; i++) {
			max = maximumAttribute(node.children[i], attr, max);
		}
	}
	else {
		if (node[attr] > max) {
			max = node[attr];
		}
	}
	return max;
}

var width = 800,
	height = 600;

var globalDate = new Date();
var dateCutoff = globalDate;
var ymd_format = d3.time.format("%Y-%m-%d");

var LBItau = 0.001,
	deltaLBI_boundary_layer = 180;

var tree = d3.layout.tree()
	.size([height, width]);

var treeplot = d3.select("#treeplot")
	.attr("width", width)
	.attr("height", height);

var tooltip = d3.tip()
	.direction('e')
	.attr('class', 'd3-tip')
	.offset([0, 12])
	.html(function(d) {
		string = ""
		if (typeof d.strain != "undefined") {
			string += "Strain: "
			string += d.strain;
		}
		if (typeof d.date != "undefined") {
			string += "<br>Date: "
			string += d.date;
		}
		if (typeof d.distance_ep != "undefined") {
			string += "<br>Epitope distance: "
			string += d.distance_ep;
		}
		if (typeof d.distance_ne != "undefined") {
			string += "<br>Non-epitope distance: "
			string += d.distance_ne;
		}
		if (typeof d.distance_rb != "undefined") {
			string += "<br>Receptor binding distance: "
			string += d.distance_rb;
		}
		if (typeof d.distance_rb != "undefined") {
			string += "<br>Local branching index: "
			string += d.LBI.toFixed(3);
		}
		return string;
	});

treeplot.call(tooltip);

function rescale(dMin, dMax, lMin, lMax, xScale, yScale, nodes, links, tips, internals, vaccines) {

	var speed = 1500;
	xScale.domain([dMin,dMax]);
	yScale.domain([lMin,lMax]);

	nodes.forEach(function (d) {
		d.x = xScale(d.xvalue);
		d.y = yScale(d.yvalue);
	});

	treeplot.selectAll(".tip").data(tips)
		.transition().duration(speed)
		.attr("cx", function(d) { return d.x; })
		.attr("cy", function(d) { return d.y; });

	treeplot.selectAll(".vaccine").data(vaccines)
		.transition().duration(speed)
		.attr("cx", function(d) { return d.x; })
		.attr("cy", function(d) { return d.y; });

	treeplot.selectAll(".internal").data(internals)
		.transition().duration(speed)
		.attr("x", function(d) {
			if (typeof d.frequency != "undefined") {
				return d.x - 5*Math.sqrt(d.frequency) - 0.5;
			}
			else {
				return d.x - 1;
			}
		})
		.attr("y", function(d) {
			if (typeof d.frequency != "undefined") {
				return d.y - 5*Math.sqrt(d.frequency) - 0.5;
			}
			else {
				return d.y - 1;
			}
		});

	treeplot.selectAll(".link").data(links)
		.transition().duration(speed)
		.attr("points", function(d) {
			var mod = 5*Math.sqrt(d.target.frequency)+0.5;
			return (d.source.x-mod).toString() + "," + d.source.y.toString() + " "
			+ (d.source.x-mod).toString() + "," + d.target.y.toString() + " "
			+ (d.target.x).toString() + "," + d.target.y.toString()
		});

}

d3.json("https://s3.amazonaws.com/augur-data/auspice/tree.json", function(error, root) {
//d3.json("auspice.json", function(error, root) {

	if (error) return console.warn(error);

	var nodes = tree.nodes(root),
		links = tree.links(nodes);

	var rootNode = nodes[0];
	var tips = gatherTips(rootNode, []);
	var internals = gatherInternals(rootNode, []);
	calcBranchLength(rootNode);
	rootNode.branch_length= 0.01;
	setFrequencies(rootNode);
	setDates(internals);
	nodes.forEach(function (d) {d.dateval = new Date(d.date)});
	calc_deltaLBI(rootNode, nodes, false);

	var vaccines = getVaccines(tips);

	var xValues = nodes.map(function(d) {
		return +d.xvalue;
	});

	var yValues = nodes.map(function(d) {
		return +d.yvalue;
	});

	var dateValues = nodes.filter(function(d) {
		return typeof d.date === 'string';
		}).map(function(d) {
		return new Date(d.date);
	});

	var xScale = d3.scale.linear()
		.domain([d3.min(xValues), d3.max(xValues)])
		.range([10, width-10]);

	var yScale = d3.scale.linear()
		.domain([d3.min(yValues), d3.max(yValues)])
		.range([10, height-10]);<commit_msg>Remove cruft.<commit_after>function depthFirstSearch(node) {
	if (typeof node.children != "undefined") {
		for (var i=0, c=node.children.length; i<c; i++) {
			depthFirstSearch(node.children[i]);
		}
	}
}

function gatherTips(node, tips) {
	if (typeof node.children != "undefined") {
		for (var i=0, c=node.children.length; i<c; i++) {
			gatherTips(node.children[i], tips);
		}
	}
	else {
		tips.push(node);
	}
	return tips;
}

function gatherInternals(node, internals) {
	if (typeof node.children != "undefined") {
		internals.push(node);
		for (var i=0, c=node.children.length; i<c; i++) {
			gatherInternals(node.children[i], internals);
		}
	}
	return internals;
}

function getVaccines(tips) {
	vaccineChoice = {};
	vaccineChoice['A/Fujian/411/2002'] = "2003-09-25";
	vaccineChoice['A/California/7/2004'] = "2005-02-21";
	vaccineChoice['A/Wisconsin/67/2005'] = "2006-02-21";
	vaccineChoice['A/Brisbane/10/2007'] = "2007-09-25";
	vaccineChoice['A/Perth/16/2009'] = "2009-09-25";
	vaccineChoice['A/Victoria/361/2011'] = "2012-02-21";
	vaccineChoice['A/Texas/50/2012'] = "2013-09-25";
	vaccineChoice['A/Switzerland/9715293/2013'] = "2014-09-25";
	vaccineStrains = Object.keys(vaccineChoice);
	vaccines = [];
	tips.forEach(function (tip) {
		if (vaccineStrains.indexOf(tip.strain) != -1) {
			tip.date = vaccineChoice[tip.strain];
			vaccines.push(tip);
		}
	})
	return vaccines;
}

function setFrequencies(node) {
	if (typeof node.frequency == "undefined") {
		node.frequency = 0.01;
	}
	if (typeof node.children != "undefined") {
		for (var i=0, c=node.children.length; i<c; i++) {
			setFrequencies(node.children[i]);
		}
	}
}

function setDistances(node) {
	if (typeof node.distance_ep == "undefined") {
		node.distance_ep = 0.0;
	}
	if (typeof node.distance_ne == "undefined") {
		node.distance_ne = 0.0;
	}
	if (typeof node.children != "undefined") {
		for (var i=0, c=node.children.length; i<c; i++) {
			setDistances(node.children[i]);
		}
	}
}

function calcBranchLength(node){
	if (typeof node.children != "undefined") {
	for (var i=0, c=node.children.length; i<c; i++) {
		calcBranchLength(node.children[i]);
		node.children[i].branch_length = node.children[i].xvalue-node.xvalue;
	}
	}
};

function setNodeAlive(node){
	if (typeof node.children != "undefined") {
	for (var i=0, c=node.children.length; i<c; i++) {
		setNodeAlive(node.children[i]);
	}
	}
	node.alive = (node.dateval<dateCutoff)?true:false;
};

function calcUpPolarizers(node){
	node.up_polarizer = 0;
	if (node.alive){
	if (typeof node.children != "undefined") {
		for (var i=0; i<node.children.length; i++) {
		calcUpPolarizers(node.children[i]);
		node.up_polarizer += node.children[i].up_polarizer;
		}
	}
		bl =  node.branch_length/LBItau;
		node.up_polarizer *= Math.exp(-bl);
		node.up_polarizer += LBItau*(1-Math.exp(-bl));
	}
};

function calcDownPolarizers(node){
	if (typeof node.children != "undefined") {
	for (var i1=0; i1<node.children.length; i1++) {
		if (node.children[i1].alive){
		node.children[i1].down_polarizer = node.down_polarizer;
		for (var i2=0; i2<node.children.length; i2++) {
			if (i1!=i2){
			node.children[i1].down_polarizer += node.children[i2].up_polarizer;
			}
		}
		bl =  node.children[i1].branch_length/LBItau;
		node.children[i1].down_polarizer *= Math.exp(-bl);
		node.children[i1].down_polarizer += LBItau*(1-Math.exp(-bl));
		calcDownPolarizers(node.children[i1]);
		}else{node.children[i1].down_polarizer=0;}
	}
	}
};

function calcPolarizers(node){
	calcUpPolarizers(node);
	node.down_polarizer = 0;
	calcDownPolarizers(node);
};

function calcLBI(node, allnodes){
	console.log("Calculating LBI for date cutoff "+dateCutoff);
	calcPolarizers(node);
	allnodes.forEach(function (d) {
	d.LBI=0;
	if (d.alive){
		d.LBI+=d.down_polarizer;
		if (typeof d.children != "undefined") {
		for (var i=0; i<d.children.length; i++) {
			d.LBI += d.children[i].up_polarizer;
		}
		}
	}
	});
};

function calc_deltaLBI(node, allnodes, logdelta){
	console.log("Calculating deltaLBI for date cutoff "+dateCutoff);
	dateCutoff.setDate(dateCutoff.getDate()-deltaLBI_boundary_layer);
	setNodeAlive(node);
	calcLBI(node, allnodes);
	allnodes.forEach(function (d){d.delta_LBI = -d.LBI;});
	dateCutoff.setDate(dateCutoff.getDate()+deltaLBI_boundary_layer);
	setNodeAlive(node);
	calcLBI(node, allnodes);
	allnodes.forEach(function (d){
	if (logdelta) {d.delta_LBI = d.LBI/(-d.delta_LBI+1e-10);}
	else {d.delta_LBI += d.LBI;}
	});
	maxLBI = d3.max(allnodes.map(function (d) {return d.LBI;}));
	maxdeltaLBI = d3.max(allnodes.map(function (d) {return d.delta_LBI;}));
	console.log("maximal LBI: "+maxLBI+", maximal deltaLBI: "+maxdeltaLBI);
	allnodes.forEach(function (d){
	d.delta_LBI /= maxdeltaLBI;
	d.LBI /= maxLBI;
	});
};

function minimumAttribute(node, attr, min) {
	if (typeof node.children != "undefined") {
		for (var i=0, c=node.children.length; i<c; i++) {
			min = minimumAttribute(node.children[i], attr, min);
		}
	}
	else {
		if (node[attr] < min) {
			min = node[attr];
		}
	}
	return min;
}

function maximumAttribute(node, attr, max) {
	if (typeof node.children != "undefined") {
		for (var i=0, c=node.children.length; i<c; i++) {
			max = maximumAttribute(node.children[i], attr, max);
		}
	}
	else {
		if (node[attr] > max) {
			max = node[attr];
		}
	}
	return max;
}

var width = 800,
	height = 600;

var globalDate = new Date();
var dateCutoff = globalDate;
var ymd_format = d3.time.format("%Y-%m-%d");

var LBItau = 0.001,
	deltaLBI_boundary_layer = 180;

var tree = d3.layout.tree()
	.size([height, width]);

var treeplot = d3.select("#treeplot")
	.attr("width", width)
	.attr("height", height);

var tooltip = d3.tip()
	.direction('e')
	.attr('class', 'd3-tip')
	.offset([0, 12])
	.html(function(d) {
		string = ""
		if (typeof d.strain != "undefined") {
			string += "Strain: "
			string += d.strain;
		}
		if (typeof d.date != "undefined") {
			string += "<br>Date: "
			string += d.date;
		}
		if (typeof d.distance_ep != "undefined") {
			string += "<br>Epitope distance: "
			string += d.distance_ep;
		}
		if (typeof d.distance_ne != "undefined") {
			string += "<br>Non-epitope distance: "
			string += d.distance_ne;
		}
		if (typeof d.distance_rb != "undefined") {
			string += "<br>Receptor binding distance: "
			string += d.distance_rb;
		}
		if (typeof d.distance_rb != "undefined") {
			string += "<br>Local branching index: "
			string += d.LBI.toFixed(3);
		}
		return string;
	});

treeplot.call(tooltip);

function rescale(dMin, dMax, lMin, lMax, xScale, yScale, nodes, links, tips, internals, vaccines) {

	var speed = 1500;
	xScale.domain([dMin,dMax]);
	yScale.domain([lMin,lMax]);

	nodes.forEach(function (d) {
		d.x = xScale(d.xvalue);
		d.y = yScale(d.yvalue);
	});

	treeplot.selectAll(".tip").data(tips)
		.transition().duration(speed)
		.attr("cx", function(d) { return d.x; })
		.attr("cy", function(d) { return d.y; });

	treeplot.selectAll(".vaccine").data(vaccines)
		.transition().duration(speed)
		.attr("cx", function(d) { return d.x; })
		.attr("cy", function(d) { return d.y; });

	treeplot.selectAll(".internal").data(internals)
		.transition().duration(speed)
		.attr("x", function(d) {
			if (typeof d.frequency != "undefined") {
				return d.x - 5*Math.sqrt(d.frequency) - 0.5;
			}
			else {
				return d.x - 1;
			}
		})
		.attr("y", function(d) {
			if (typeof d.frequency != "undefined") {
				return d.y - 5*Math.sqrt(d.frequency) - 0.5;
			}
			else {
				return d.y - 1;
			}
		});

	treeplot.selectAll(".link").data(links)
		.transition().duration(speed)
		.attr("points", function(d) {
			var mod = 5*Math.sqrt(d.target.frequency)+0.5;
			return (d.source.x-mod).toString() + "," + d.source.y.toString() + " "
			+ (d.source.x-mod).toString() + "," + d.target.y.toString() + " "
			+ (d.target.x).toString() + "," + d.target.y.toString()
		});

}

d3.json("https://s3.amazonaws.com/augur-data/auspice/tree.json", function(error, root) {
//d3.json("auspice.json", function(error, root) {

	if (error) return console.warn(error);

	var nodes = tree.nodes(root),
		links = tree.links(nodes);

	var rootNode = nodes[0];
	var tips = gatherTips(rootNode, []);
	var internals = gatherInternals(rootNode, []);
	calcBranchLength(rootNode);
	rootNode.branch_length= 0.01;
	setFrequencies(rootNode);
	nodes.forEach(function (d) {d.dateval = new Date(d.date)});
	calc_deltaLBI(rootNode, nodes, false);

	var vaccines = getVaccines(tips);

	var xValues = nodes.map(function(d) {
		return +d.xvalue;
	});

	var yValues = nodes.map(function(d) {
		return +d.yvalue;
	});

	var dateValues = nodes.filter(function(d) {
		return typeof d.date === 'string';
		}).map(function(d) {
		return new Date(d.date);
	});

	var xScale = d3.scale.linear()
		.domain([d3.min(xValues), d3.max(xValues)])
		.range([10, width-10]);

	var yScale = d3.scale.linear()
		.domain([d3.min(yValues), d3.max(yValues)])
		.range([10, height-10]);
{'fraction_non_alphanumeric': 0.19245752624174872, 'fraction_numerical': 0.024618547776214694, 'mean_word_length': 4.1227827050997785, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6458772', 'n_tokens_mistral': 8034, 'n_tokens_neox': 7297, 'n_words': 1269}