← Lucas B. C. Rocha

Artificial Intelligence course · Python + numpy · interactive demo

Grouping DNA sequences with k-means, without aligning them

Each sequence becomes a vector with the frequency of every k-mer, and k-means splits those vectors into the number of groups you choose. This page runs the same algorithm as kmeans.py (k-means++ start, Lloyd iterations, best of 10 runs) on the 16 hepatitis B virus sequences of the repository.

1 Input

  1. k-mer vectors
  2. k-means++ start
  3. Lloyd iterations
  4. Best of 10 runs
  5. Result

2 Sequences in k-mer space

2-D projection (PCA) of the vectors

3 Terminal


        

4 k-mer frequency vectors

one row per sequence, one column per k-mer; darker = more frequent

How k-means works here

Each sequence is cut into overlapping k-mers (for k = 3: AAA, AAC, …, TTT, 64 in all) and the counts are divided by the total, so long and short sequences can be compared. Distance between two sequences is the Euclidean distance between their vectors.

  • k-means++: the first centroid is a random sequence; each next one is drawn with probability proportional to the squared distance to the nearest centroid already chosen, so the starts are spread out.
  • Lloyd: each sequence goes to its nearest centroid; each centroid moves to the mean of its members; repeat until no sequence changes cluster.
  • Best of 10: the whole thing runs 10 times from different starts and keeps the run with the lowest inertia (sum of squared distances to the centroids).

The picture is a 2-D projection of the 4k-dimensional space, so points that look close may be farther apart in the real space.

5 Inertia

per iteration of the run shown

final inertia of each of the 10 runs

6 Clusters