← All examples
30

Canonical Grid model

Biology embedding methods

Compare feature-centered PCA with uncentered SVD and count overlapping DNA motifs.

Scale
Small
Source
30-biology-embedding-methods.grid
Length
36 lines
Collection
Advanced language
Level
Intermediate
Runtime
Portable
Version
1.1.0
30-biology-embedding-methods.grid
Get Grid
MODEL "Biology embedding methods"
DESCRIPTION "Compare feature-centered PCA with uncentered SVD and count overlapping DNA motifs."
VERSION "1.1.0"
AUTHOR "Grid examples"
TAGS "biology", "omics", "pca", "svd"

# Rows are observations; columns are features. The third observation is empty.
Counts = BIO_PARSE_MATRIX_MARKET("%%MatrixMarket matrix coordinate real general\n3 2 4\n1 1 80\n1 2 20\n2 1 20\n2 2 80\n")
ShiftedCounts = BIO_PARSE_MATRIX_MARKET("%%MatrixMarket matrix coordinate real general\n3 2 6\n1 1 90\n1 2 30\n2 1 30\n2 2 90\n3 1 10\n3 2 10\n")

# Center features, without scaling. The method is part of the saved source.
Centered = BIO_OMICS_EMBED_V2(Counts, 1, "pca")
ShiftedCentered = BIO_OMICS_EMBED_V2(ShiftedCounts, 1, "pca")
Uncentered = BIO_OMICS_EMBED_V2(Counts, 1, "svd")

# Expected PCA magnitudes: 42.426407, 42.426407, 0; the first two oppose.
# A global sign reversal is equivalent. Translation preserves these scores.
A1 = INDEX(Centered, 1, 1)
A2 = INDEX(Centered, 2, 1)
A3 = INDEX(Centered, 3, 1)
C1 = INDEX(ShiftedCentered, 1, 1)
C2 = INDEX(ShiftedCentered, 2, 1)
C3 = INDEX(ShiftedCentered, 3, 1)

# Uncentered SVD gives equal first two scores, magnitude 70.710678.
E1 = INDEX(Uncentered, 1, 1)
E2 = INDEX(Uncentered, 2, 1)
E3 = INDEX(Uncentered, 3, 1)

# Overlapping matches start at positions 1, 2, and 3. Expected count: 3.
MotifMatches = DNA_MOTIF_COUNT("AAAAA", "AAA")
MotifPosition = DNA_FIND_MOTIF("CCATG", "ATG")
# MotifPosition is 3 (1-based). An absent motif returns 0.
# A number used as a motif is an input error: quote the DNA sequence.

END MODEL