Skip to content

Instantly share code, notes, and snippets.

@mcjmigdal
Last active October 28, 2022 22:51
Show Gist options
  • Select an option

  • Save mcjmigdal/56407315d30a2252325517d0a278ae2c to your computer and use it in GitHub Desktop.

Select an option

Save mcjmigdal/56407315d30a2252325517d0a278ae2c to your computer and use it in GitHub Desktop.
Javascript FASTA format validator
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<div>
<label for="fasta">FASTA</label>
<textarea type="text" id="fasta" rows="15" cols="80" required autofocus onchange="validateFasta()"></textarea>
</div>
</body>
<script>
function validateFasta() {
let vheader = new RegExp("^>");
let vfasta = new RegExp("^[ACGT]+$");
let handle = document.getElementById("fasta");
let fasta = handle.value.split("\n");
let i = 0;
let line = ""
let new_header = false;
let iscorrect = 0;
let ok = false;
while (i < fasta.length) {
if (fasta.length == 1 ) {
ok = false;
break;
}
line = fasta[i];
if (! line.length) {
i++;
continue;
}
isheader = vheader.test(line);
iscorrect = isheader + vfasta.test(line);
if (iscorrect == 1) {
ok = true;
} else {
ok = false;
};
if (new_header && isheader) {
ok = false;
}
if (isheader) {
new_header = true;
} else {
new_header = false;
}
i++;
}
if (ok) {
console.log("ok");
handle.style = "border-color: green; border-width: 5px;";
} else {
console.log("bad");
handle.style = "border-color: red; border-width: 5px;"
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment