genome sequencing

by Laurel Bruggeman

JavaScript

const reference = "ACGACGTCGACGACGACGTCGACGACGACGTCGACGACGACGTCGACGACGACGTCGACGACGACGTCGACGACGACGTCGACGACGACGTCGACGACGACGTCGACGACGACGTCGACGACGACGTCGACGACGACGTCGACGACGACGTCGACGACGACGTCGACGACGACGTCGACGACGACGTCGACGACGACGTCGACGACGACGTCGACGCCCCCCCC";
const fragLength = 3;
const chunkLength = 20;
const index = [];
const frags = [];
let pointer = 0;

for (let i=0; pointer<reference.length; i++) {
	const pointerEnd = pointer+chunkLength;
  frags.push({ string: reference.substr(pointer, chunkLength+fragLength), start: pointer });
	pointer = pointerEnd;
}

// console.log(frags);

const process = (genome, start) => {
  for (let i=0; i+fragLength<=genome.length; i++) {
    const ref = genome.substr(i, fragLength);
    const position = start + i;
    if (!index[ref]) index[ref] = [position];
    else {
    	const exists = index[ref].indexOf(position);
    	if (exists === -1) index[ref].push(position);
    }
  }
}

for (let i=0; i<frags.length; i++) {
	process(frags[i].string, frags[i].start);
}

// console.log('index', index);

const tests = ["ACG", "TCG", "CCC"];
for(let i=0; i<tests.length; i++) {
	console.log(tests[i], ':', index[tests[i]]);
}