The complement to a typical string of DNA bases can be found by reversing the string then replacing each base with it's complement (A->T, T->A, C->G, G->C). For example, the complement of CTTAACCAGCGGACACGGGCTTGGC is GCCAAGCCCGTGTCCGCTGGTTAAG.
I wrote a couple algorithms to do this in javascript and am wondering if they are equivalent, i.e. equivalent time complexity for example, but also equivalent for any other considerations in this context of finding complements in a browser and updating an HTML document with the output. I am trying to understand Regexes and how javacript gets interpreted, compiled and executed.
First the script validates the input string s
with this Regex: /[^CGAT]/
. If the input string has a character besides a C, G, A, or T, the script stops and alerts the user why. Next, the script generates the reverse string with var reverseSeq=s.split('').reverse().join('')
(which I found here).
Then either of these javascripts converts the reversed string to the original's complement.
1.)
var complementSeq='';
for (i in reverseSeq){complementSeq=complementSeq+COMPLEMENT_BASES[reverseSeq[i]]};
2.)
complementSeq=reverseSeq.replace(/(A)|(C)|(T)|(G)/g, function (match) {return COMPLEMENT_BASES[match]})
2) depends on this object: COMPLEMENT_BASES={'A':'T','T':'A','G':'C','C':'G'};
.
Are these methods equally efficient, and basically equivalent otherwise? Is there any other approach I'm missing that might be more efficient?