Skip to content

Instantly share code, notes, and snippets.

@tryshchenko
Created October 6, 2018 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tryshchenko/254e3a23fce231f9b7f95139f988e87f to your computer and use it in GitHub Desktop.
Save tryshchenko/254e3a23fce231f9b7f95139f988e87f to your computer and use it in GitHub Desktop.
const fs = require('fs');
const BLOCKS_LENGTH = 4;
const GENO_PAIR_OFFSET = 3;
const isCommentLine = (el) => el.indexOf('#') > -1;
const isValidSequenceLine = (line) => line.length !== BLOCKS_LENGTH;
module.exports = (genomePath) => {
const data = fs.readFileSync(genomePath, 'utf-8');
return data.split('\n').reduce((prev, el) => {
if (isCommentLine(el)) {
return prev;
}
const splitter = el.indexOf('\t') > 0 ? '\t' : ' ';
const trimmed = el.split(splitter).filter(x => x !== '');
const geno = trimmed[0];
if (!geno || !trimmed[GENO_PAIR_OFFSET] || isValidSequenceLine(trimmed)) {
return prev;
}
prev[geno] = prev[geno] || [];
prev[geno].push(trimmed[GENO_PAIR_OFFSET]);
return prev;
}, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment