<commit_before>/**
 * This function converts Newick tree into JSON format
 * @param s Newick tree in string format
 */
function NewickToJSON(s) {

    var ancestors = [];
    var tree = {};
    var tokens = s.split(/\s*(;|\(|\)|,|:|\[|\])\s*/);
    var tag = false;

    for (var i = 0; i < tokens.length; i++) {
        var token = tokens[i];
        switch (token) {
            case '(': // new branchset
                var subtree = {};
                tree.children = [subtree];
                ancestors.push(tree);
                tree = subtree;
                break;

            case ',': // another branch

                var subtree = {};
                ancestors[ancestors.length - 1].children.push(subtree);
                tree = subtree;
                break;

            case ')': // optional name next
                tree = ancestors.pop();
                break;

            case ':': // optional length next
                break;

            default:
                var x = tokens[i - 1];
                if (x == ')' || x == '(' || x == ',') {
                    if (token.indexOf("_") > 0) {
                        tree.id = {}
                        tree.id.accession = getGeneIDfromTranscript(token.split("_")[0])
                        tree.sequence = {}
                        tree.sequence.id = [];
                        tree.sequence.id[0] = {}
                        tree.sequence.id[0].accession = getProteinIDfromTranscript(token.split("_")[0]);
                    }


                } else if (x == '[') {

                    tag = true;

                }
                else if (tag == true && x == ':') {
                    if (token == "D=N") {

                        tree.type = "speciation";

                    } else if (token == "D=Y") {

                        tree.type = "duplication";

                    } else if (token == "DD=Y") {

                        tree.type = "dubious";

                    }
                    tag == false
                } else if (x == ':') {

                    tree.branch_length = token;
                }
        }
    }
    return tree;
}

/**
 * finds gene id for the provided transcript
 * @param token transcript_id
 * @returns {*}
 */
function getGeneIDfromTranscript(token) {
    var id = null
    jQuery.each(syntenic_data.member, function (i, obj) {
        for (var j = 0; j < obj.Transcript.length; j++) {
            if (obj.Transcript[j].id == token) {
                id = obj.id.toString()
                break;
            }
        }
    });
    return id;
}

/**
 * finds protein id for the provided transcript if no protein id found assigns transcript id as protein id
 * @param token
 * @returns {*}
 */
function getProteinIDfromTranscript(token) {
    var id = null
    jQuery.each(syntenic_data.member, function(i, obj) {
        for(var j=0; j<obj.Transcript.length; j++){
            if(obj.Transcript[j].id == token){
                id = obj.Transcript[j].Translation.id.toString()
                break;
            }
        }
    });
    if(id == null){
        id = token;
    }
    return id;
}<commit_msg>adds gene id when not present and node does not have to have _<commit_after>/**
 * This function converts Newick tree into JSON format
 * @param s Newick tree in string format
 */
function NewickToJSON(s) {
    console.log("NewickToJSON")

    var ancestors = [];
    var tree = {};
    var tokens = s.split(/\s*(;|\(|\)|,|:|\[|\])\s*/);
    var tag = false;

    for (var i = 0; i < tokens.length; i++) {
        var token = tokens[i];
        switch (token) {
            case '(': // new branchset
                var subtree = {};
                tree.children = [subtree];
                ancestors.push(tree);
                tree = subtree;
                break;

            case ',': // another branch

                var subtree = {};
                ancestors[ancestors.length - 1].children.push(subtree);
                tree = subtree;
                break;

            case ')': // optional name next
                tree = ancestors.pop();
                break;

            case ':': // optional length next
                break;

            default:
                var x = tokens[i - 1];
                if (x == ')' || x == '(' || x == ',') {
                    if (token.indexOf("_") > 0) {
                    console.log(token)

                        tree.id = {}
                        tree.id.accession = getGeneIDfromTranscript(token.split("_")[0])
                        tree.sequence = {}
                        tree.sequence.id = [];
                        tree.sequence.id[0] = {}
                        tree.sequence.id[0].accession = getProteinIDfromTranscript(token.split("_")[0]);
                    }else if(token.length > 0){
                    console.log(token)
                        
                        tree.id = {}
                        tree.id.accession = getGeneIDfromTranscript(token)
                        tree.sequence = {}
                        tree.sequence.id = [];
                        tree.sequence.id[0] = {}
                        tree.sequence.id[0].accession = getProteinIDfromTranscript(token);
                    }


                } else if (x == '[') {

                    tag = true;

                }
                else if (tag == true && x == ':') {
                    if (token == "D=N") {

                        tree.type = "speciation";

                    } else if (token == "D=Y") {

                        tree.type = "duplication";

                    } else if (token == "DD=Y") {

                        tree.type = "dubious";

                    }
                    tag == false
                } else if (x == ':') {

                    tree.branch_length = token;
                }
        }
    }
    console.log(tree)
    return tree;
}

/**
 * finds gene id for the provided transcript
 * @param token transcript_id
 * @returns {*}
 */
function getGeneIDfromTranscript(token) {

    var id = null
    jQuery.each(syntenic_data.member, function (i, obj) {
        for (var j = 0; j < obj.Transcript.length; j++) {
            if (obj.Transcript[j].id == token) {
                id = obj.id.toString()
                break;
            }
        }
    });

    if(id == null){
        id = token;
    }
    
    return id;
}

/**
 * finds protein id for the provided transcript if no protein id found assigns transcript id as protein id
 * @param token
 * @returns {*}
 */
function getProteinIDfromTranscript(token) {

    var id = null
    jQuery.each(syntenic_data.member, function(i, obj) {
        for(var j=0; j<obj.Transcript.length; j++){
            if(obj.Transcript[j].id == token){
                id = obj.Transcript[j].Translation.id.toString()
                break;
            }
        }
    });

    if(id == null){
        id = token;
    }
    
    return id;
}
{'fraction_non_alphanumeric': 0.13378358959620634, 'fraction_numerical': 0.0033050725678976864, 'mean_word_length': 1.1729628473306275, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 6, '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': '1409972', 'n_tokens_mistral': 2008, 'n_tokens_neox': 1842, 'n_words': 448}