reshape_tox21_datasets_for_all_compounds = function(array_of_datasets, array_of_compounds) {
var i;
var arr = [];
var j;
var hitcall;
var ic50;
for (i = 0; i < array_of_compounds.length; i++) {
const compound = array_of_compounds[i];
var compound_datasets = array_of_datasets.filter(item => item["Substance name"] == compound);
var unique_compound_datasets = Array.from(new Set(compound_datasets.map(item => item["Summary data"])));
var results = [];
for (j = 0; j < unique_compound_datasets.length; j++) {
const dataset_id = unique_compound_datasets[j];
const datasets = compound_datasets.filter(item => item["Summary data"] == dataset_id);
const hitcalls = datasets.map(item => item["Hit call"]);
const unique_hitcalls = Array.from(new Set(hitcalls));
const ic50_values = datasets.map(item => item["IC50"]);
if (unique_hitcalls.length > 1) {
hitcall = "ambiguous";
} else {
hitcall = unique_hitcalls[0];
};
if (hitcall == "active") {
const arrSum = ic50_values => ic50_values.reduce((a,b) => a + b, 0);
ic50 = arrSum / (ic50_values.length);
} else {
ic50 = null;
}
const dataset_object = datasets[0];
if (colors.includes(hitcall)) {
results.push({
"Hit call": hitcall,
"Organism": dataset_object["Organism"],
"Cell type": dataset_object["Cell type"],
"Design type": dataset_object["Design Type"],
"Target type": dataset_object["Target type"],
"Target family": dataset_object["Target family"],
"Biological process": dataset_object["Biolological process"],
"Dataset name": dataset_object["Dataset name"].replace("EPA-InVitroDBV3.2-", "").replace("_", " "),
"IC50": ic50,
});
}
};
var compound_object = {
"Compound": compound,
"Results": results,
};
arr.push(compound_object);
}
return arr;
}