Multi-FASTA Parser

Foundational Bioinformatics FASTA Parsing File Formats Records
Significance:

FASTA is the single most common format in all of genomics, and almost every
bioinformatics task begins by parsing one. The format looks trivial until you meet real files: sequences
wrapped across multiple lines at 60 or 70 characters, headers carrying database identifiers and
descriptions, and blank lines between records. Writing this parser from scratch once — rather than
reaching straight for a library — is what teaches you why the format behaves the way it does.

Statement

You are given a multi-FASTA file on standard input. Each record begins with a header
line starting with >, followed by one or more lines of sequence which must be concatenated together.

For each record, in the order it appears in the file, print one line containing:

<identifier> <sequence length>

The identifier is the header text with the leading > removed, up to but not including the first
space. Ignore any description after that first space.

Input — read from standard input
Variable Type Description
fasta
line 1..n
str A multi-FASTA formatted block of text
1 <= records <= 50, total input <= 20000 characters

These variables are already read for you in the starter code on the right.

Output

str one line per record, identifier and concatenated sequence length separated by a single space

Sample Cases
Sample 1
Input
>seq1 first sequence
ACGTACGTAC
GTACGT
>seq2
TTTT
Expected Output
seq1 16
seq2 4
Record seq1 wraps across two lines and totals 16 bases; the description after the space is ignored.
Sample 2
Input
>only
ACGT
Expected Output
only 4
A single record with a single sequence line.

Submit also runs your code against 4 hidden test cases. Hidden inputs are never shown — if one fails you'll get its number and a description of the mismatch, not the data.

Constraints
  • 1 <= number of records <= 50
  • Sequence lines may be wrapped at any width and may vary within a single record
  • Blank lines may appear between records and must be ignored
  • Identifiers contain no spaces; any text after the first space in a header is a description
Further Reading
  • Accumulate sequence lines into a buffer and flush it when you hit the next > or end of input.
  • Do not forget to emit the final record after the loop ends — this is the most common bug.
  • header[1:].split()[0] extracts the identifier cleanly in Python.

My Notes
Log in to save personal notes.
Console output will appear here when you click Run Code or Submit...
Expected: fasta — the whole input (A multi-FASTA formatted block of text)
Next Problem
k-mer Frequency Array