All files / src/sequence-utils index.ts

95.23% Statements 20/21
90.9% Branches 10/11
100% Functions 3/3
95.23% Lines 20/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 529x 9x     3x 3x         3x 3x 3x 18x 18x 2x     16x       3x           11x   3x 2x   3x           6x       6x     6x     6x    
export const commentLineRE = /^\s*[>;](.*)$/m;
const whitespacesRE = /\s+/g;
 
export function formatFASTA(fasta: string, chunkSize = 10, chunksPerLine = 6) {
  const aaPerChunkRE = new RegExp(`(.{1,${chunkSize}})`, 'g');
  const chunksPerLineRE = new RegExp(
    `(.{1,${chunkSize * chunksPerLine}})`,
    'g'
  );
 
  let header = '';
  let sequence = '';
  for (const line of fasta.split('\n')) {
    const trimmedLine = line.trim();
    if (commentLineRE.test(trimmedLine)) {
      header += `\n${trimmedLine}`;
    } else {
      // concatenate all the sequence lines into a single one
      sequence += trimmedLine;
    }
  }
 
  let output = sequence
    .replace(whitespacesRE, '')
    // split the sequence per requested aa per lines
    .replace(chunksPerLineRE, '$1\n')
    .split('\n')
    // separate aa per chunk size within the lines themselves
    .map((line) => line.replace(aaPerChunkRE, '$1 ').trim())
    .join('\n');
  if (header) {
    output = `${header}\n${output}`;
  }
  return output.trim();
}
 
// extract a name as an NCBI identifier from a FASTA header
// See: https://en.wikipedia.org/wiki/FASTA_format#NCBI_identifiers
export function extractNameFromFASTAHeader(fasta?: string) {
  Iif (!fasta) {
    return;
  }
 
  const [, header = ''] = fasta.match(commentLineRE) || [];
 
  // separate by space, first word will be the extracted name
  const [name] = header.split(' ').filter(Boolean);
 
  // eslint-disable-next-line consistent-return
  return name;
}