<commit_before>// var tntTreeNode = require("tnt.tree.node");

// Given a flat structure of associations
// convert and return that structure to a tree
// TODO: Should this be moved to the cttvApi package?
var rootId = "cttv_root";
var flat2tree = function (data) {
    var associations = data.data;
    var therapeuticAreas = data.therapeutic_areas;

    // Unfold the associations with more than 1 path
    var unfoldedAssociations = unfoldAssoc(associations);

    // Sort them by min path length
    var sortedAssociations = unfoldedAssociations.sort(sortByPathLength);

    var tree = {
        "__id": rootId,
        "name" : "cttv_disease",
        "disease": {
            "id": rootId,
            "name": "cttv_disease"
        }
    };
    var index = {};
    index[rootId] = tree;

    var tas = getTAs(therapeuticAreas);

    for (var i=0; i<sortedAssociations.length; i++) {
        addNode(sortedAssociations[i], tree, tas, index);
    }
    return tree;
};

function unfoldAssoc (arr) {
    var unfold = [];
    for (var i=0; i<arr.length; i++) {
        var node = arr[i];
        if (node.disease.path.length > 1) {
            for (var j=1; j<node.disease.path.length; j++) {
                var clone = cloneNode(node);
                clone.currPath = node.disease.path[j];
                unfold.push(clone);
            }
            node.currPath = node.disease.path[0];
        } else {
            node.currPath = node.disease.path[0];
        }
        // We are getting the root here for now, so filter out the root (ie, path is empty)
        if (!node.currPath) {
            continue;
        }
        unfold.push(node);
    }
    return unfold;
}


function addNode (node, tree, tas, index) {
    // nodes don't have a name, we put one there
    node.__id = node.disease.id;
    node.__name = node.disease.name;
    node.name = node.disease.name;
    // for (var i=0; i<node.disease.path.length; i++) {
        //var path = node.disease.path[i];
        var path = node.currPath;
        // debugger;
        var parent = findParent(path, index);
        delete(node.currPath);

        // If the parent is cttv_root and the node is not a therapeutic area (path.length > 2) we search for the TA in the set of TAs
        if ((parent.__id === rootId) && (path.length > 2)) {
            var ta = tas[node.__id];
            if (!ta) {
                ta = {};
                ta.disease = {};
                ta.disease.id = path[1];
                ta.disease.name = path[1];
            }
            ta.__id = path[1];
            // var ta = {};
            // ta.id = path[1];
            // ta.name = findTAname(path[1], node);
            if (!tree.children) {
                tree.children = [];
            }
            tree.children.push(ta);
            parent = ta;
            index[ta.__id] = ta;
        }

        if (!parent.children) {
            parent.children = [];
        }

        // If the parent doesn't have a children with the same ID add it<commit_msg>fixed ta name for direct true in tree construction<commit_after>// var tntTreeNode = require("tnt.tree.node");

// Given a flat structure of associations
// convert and return that structure to a tree
// TODO: Should this be moved to the cttvApi package?
var rootId = "cttv_root";
var flat2tree = function (data) {
    var associations = data.data;
    var therapeuticAreas = data.therapeutic_areas;

    // Unfold the associations with more than 1 path
    var unfoldedAssociations = unfoldAssoc(associations);

    // Sort them by min path length
    var sortedAssociations = unfoldedAssociations.sort(sortByPathLength);

    var tree = {
        "__id": rootId,
        "name" : "cttv_disease",
        "disease": {
            "id": rootId,
            "name": "cttv_disease"
        }
    };
    var index = {};
    index[rootId] = tree;

    var tas = getTAs(therapeuticAreas);

    for (var i=0; i<sortedAssociations.length; i++) {
        addNode(sortedAssociations[i], tree, tas, index);
    }
    return tree;
};

function unfoldAssoc (arr) {
    var unfold = [];
    for (var i=0; i<arr.length; i++) {
        var node = arr[i];
        if (node.disease.path.length > 1) {
            for (var j=1; j<node.disease.path.length; j++) {
                var clone = cloneNode(node);
                clone.currPath = node.disease.path[j];
                unfold.push(clone);
            }
            node.currPath = node.disease.path[0];
        } else {
            node.currPath = node.disease.path[0];
        }
        // We are getting the root here for now, so filter out the root (ie, path is empty)
        if (!node.currPath) {
            continue;
        }
        unfold.push(node);
    }
    return unfold;
}


function addNode (node, tree, tas, index) {
    // nodes don't have a name, we put one there
    node.__id = node.disease.id;
    node.__name = node.disease.name;
    node.name = node.disease.name;
    // for (var i=0; i<node.disease.path.length; i++) {
        //var path = node.disease.path[i];
        var path = node.currPath;
        // debugger;
        var parent = findParent(path, index);
        delete(node.currPath);

        // If the parent is cttv_root and the node is not a therapeutic area (path.length > 2) we search for the TA in the set of TAs
        if ((parent.__id === rootId) && (path.length > 2)) {
            var taId = node.target.id + "-" + path[1];
            var ta = tas[taId];
            if (!ta) {
                ta = {};
                ta.__name = ta.disease.name;
                ta.name = ta.disease.name;
                ta.disease = {};
                ta.disease.id = path[1];
                ta.disease.name = path[1];
            }
            ta.__id = path[1];
            ta.__name = ta.disease.name;
            ta.name = ta.disease.name;
            if (!tree.children) {
                tree.children = [];
            }
            tree.children.push(ta);
            parent = ta;
            index[ta.__id] = ta;
        }

        if (!parent.children) {
            parent.children = [];
        }

        // If the parent doesn't have a children with the same ID add it
{'fraction_non_alphanumeric': 0.12514165452485024, 'fraction_numerical': 0.005018617451837462, 'mean_word_length': 1.7324192835028749, 'pattern_counts': {'":': 8, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '717548', 'n_tokens_mistral': 1984, 'n_tokens_neox': 1872, 'n_words': 524}