Tag Archives: function

Validate DNA FASTA file with a javascript function

gitlogoI couldn’t find a quick function that validates a single DNA FASTA file in javascript, so here a setup (it uses the same structure as this protein FASTA validator from the Oxford Protein Informatics Group).

function validateDNA (seq)	{
	//Based on: http://www.blopig.com/blog/2013/03/a-javascript-function-to-validate-fasta-sequences/

	// immediately remove trailing spaces
	seq = seq.trim();

	// split on newlines... 
	var lines = seq.split('\n');

	// check for header
	if (seq[0] == '>') {
		// remove one line, starting at the first position	
		lines.splice(0, 1);
	
	}

	// join the array back into a single string without newlines and 
	// trailing or leading spaces
	seq = lines.join('').trim();
	
	//Search for charaters that are not G, A, T or C.
	if (seq.search(/[^gatc\s]/i) != -1) {	
		//The seq string contains non-DNA characters
		return false;
		/// The next line can be used to return a cleaned version of the DNA
		/// return seq.replace(/[^gatcGATC]/g, "");
	}
	else
	{
		//The seq string contains only GATC
		return true;
	}
	
}

Have fun

Leave a Comment

Filed under Tools