Consensus and Profile Matrix

Advanced Computational Biology Consensus Profile Matrix Alignment
Significance:

A profile matrix summarises a set of aligned sequences into per-position base frequencies, and the
consensus string reads off the most common base at each position. This is exactly how position
weight matrices for transcription-factor binding sites are built, and it is the representation
that motif-finding and sequence-logo tools operate on.

Statement

Given a collection of DNA strings of equal length, build the profile matrix and the consensus
string.

The first line holds n, the number of sequences; the next n lines hold them.

Print five lines:

  1. The consensus string — at each position, the most frequent base. If several bases tie, choose
    the one that comes first alphabetically (A < C < G < T), so the answer is unique.
  2. A: followed by the count of A at each position, space-separated.
  3. The same for C:.
  4. The same for G:.
  5. The same for T:.
Input — read from standard input
Variable Type Description
n
line 1
int How many sequences follow
1 <= n <= 20
sequences
line 2
list[str] The aligned DNA sequences, one per line, all the same length
each 1 <= length <= 200, uppercase A, C, G, T only

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

Output

str the consensus string, then four count rows labelled 'A: ', 'C: ', 'G: ', 'T: '

Sample Cases
Sample 1
Input
7
ATCCAGCT
GGGCAACT
ATGGATCT
AAGCAACC
TTGGAACT
ATGCCATT
ATGGCACT
Expected Output
ATGCAACT
A: 5 1 0 0 5 5 0 0
C: 0 0 1 4 2 0 6 1
G: 1 1 6 3 0 1 0 0
T: 1 5 0 0 0 1 1 6
Seven aligned 8-mers; the consensus reads the most common base at each of the 8 columns.
Sample 2
Input
1
ACGT
Expected Output
ACGT
A: 1 0 0 0
C: 0 1 0 0
G: 0 0 1 0
T: 0 0 0 1
A single sequence is its own consensus, with counts of 1 and 0.

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 <= n <= 20
  • Every sequence has the same length, between 1 and 200
  • Ties in the consensus are broken alphabetically (A < C < G < T)
Further Reading
  • Transpose the sequences so you can count each column independently.
  • Iterating the bases in the order "ACGT" and keeping the first maximum gives the alphabetical
    tie-break for free.

My Notes
Log in to save personal notes.
Console output will appear here when you click Run Code or Submit...
Expected: n (int), sequences (list[str])
Next Problem
Approximate Motif Matching