Java Script - Find Transcription Factors Algorithm

The Find Transcription Factors Algorithm in JavaScript

In molecular biology, a transcription factor (or sequence-specific DNA-binding factor) is a protein that controls the rate of transcription of genetic information from DNA to messenger RNA, by binding to a specific DNA sequence .

A, C, G, and T, representing the four nucleotide bases of a DNA strand — adenine, cytosine, guanine, thymine. By Wikipedia

Group of transcription factors and relative binding sites:

ATF6: "TGACGT"  
CREB: "TGACGCA"  
cMyc: "CACGTG"  
Gata1: "GATT"  
AhR: "TGCGTG"  

(a Simple Example Group of transcription factors and relative binding using in Algorithm show case.)

Transcription Factors Algorithm -- indexOf Method

/* Find Transcription Factors Algorithm by childofcode.com */

function TransFactors_IndexOf_Method(seq) {  
    var res = {};
    var theIndex = 0;
    var Results = [];

/*RelativeGroup - You can Define Your relative binding Here .*/
    var relativeGroup = [{
            'Factor': 'ATF6',
            'Cell': 'TGACGT'
        },
        {
            'Factor': 'CREB',
            'Cell': 'TGACGCA'
        },
        {
            'Factor': 'cMyc',
            'Cell': 'CACGTG'
        },
        {
            'Factor': 'Gata1',
            'Cell': 'GATT'
        },
        {
            'Factor': 'AhR',
            'Cell': 'TGCGTG'
        }
    ]

    while (relativeGroup[theIndex]) {

        for (var curCell in seq) {
            var searchFactor = seq.indexOf(relativeGroup[theIndex].Cell, curCell);

            if ((Results.indexOf(searchFactor + 1) < 0) && searchFactor != -1) {
                Results.push(searchFactor + 1);
            }
        }

        if (Results.length > 0) {
            res[relativeGroup[theIndex].Factor] = Results;
            Results = [];
        }

        theIndex++;
    }
    return res;
}

Transcription Factors Algorithm -- RegExp Method

/* Find Transcription Factors Algorithm by childofcode.com */
function TransFactors_RegExp_Method(seq) {

    /*RelativeGroup - You can Define Your relative binding Here .*/
    res = {
        ATF6: "TGACGT",
        CREB: "TGACGCA",
        cMyc: "CACGTG",
        Gata1: "GATT",
        AhR: "TGCGTG"
    }

    for (var count in res) {

        var regex = new RegExp(res[count], "g"),
            matches = [],
            match;

        while (match = regex.exec(seq)) {
            matches.push(match.index + 1);
            regex.lastIndex = match.index + 1;
            res[count] = matches;
        }

        if (matches.length < 1) {
            delete res[count];
        }

    }
    return res;
}

Note : some transcription factors could have overlapping binding sites with others (or with themselves). In case the sequence is empty, too short, or doesn't contain a match, the function should return and empty object {}.

Example 1: GATTTGCGTGTGACGTGATT Output Result {"ATF6":[11],"Gata1":[1,17],"AhR":[5]}

Example 2: TGACGTGACGT Output Result {"ATF6":[1,6]}

Example 3: TGAAATGTCCACC Output Result {}