# 10. Finding Samples with Similar Expression

> Compress the expression patterns of tens of thousands of genes into two dimensions with UMAP, then interpret nearby groups in large cancer and normal reference datasets.

Comparing one gene at a time makes it difficult to see which tissue or cancer a sample resembles overall. Comparing the expression patterns of tens of thousands of genes together can reveal similar samples.

> **Question for this lesson**: Which normal tissue or cancer samples are closest to my sample's overall expression pattern?

Treating one sample as a feature vector containing tens of thousands of numbers gives the same shape as recommendation or embedding search. Samples that are similar in the original vector space are connected as neighbours, and that neighbourhood structure is compressed into coordinates people can see.

| Stage | Data shape | Operation |
| --- | --- | --- |
| Input | samples × about 20,000 genes | Build an expression vector for each sample |
| Feature selection | samples × about 2,000 genes | Select genes that reveal sample differences |
| Neighbour search | samples × neighbour list | Connect nearby samples in high dimensions |
| UMAP output | samples × 2 coordinates | Place the neighbourhood graph in two dimensions |

## 1. UMAP: finding samples with similar overall expression patterns

**Dimensionality reduction** summarizes the expression patterns of tens of thousands of genes in **two dimensions**, placing samples with similar expression near one another. Here, values were transformed with log2(TPM + 1), about 2,000 highly variable genes were selected, and those genes were passed to UMAP. [Sample distance and dimensionality reduction](/en/reference/distance-and-dimension-reduction/) explains distance, the difference between PCA and UMAP, and the limits of interpreting the map.

```python
X = expression.loc[samples, variable_genes]  # sample × gene
embedding = UMAP().fit_transform(X)           # sample × [x, y]
```

The two coordinates are not two original genes. They are **lossy display coordinates** made for inspection. Local relationships among nearby points can be useful, but screen distance and direction between distant clusters are not biological quantities.

The [PCA stage of the GSE251845 DEG exercise](/en/practice/colorectal-deg-gse251845/#3-2-view-the-global-expression-pattern-with-pca) uses another dimensionality-reduction method to inspect the global pattern of tumour-normal samples and their patient pairs.

Projecting your sample together with a large reference such as TCGA or GTEx lets you infer biological context from **which tissue or cancer subtype lies nearby**.

Preprocessing is an interface contract here. If the query and reference differ in gene IDs, normalization, log transformation, or batch, the algorithm may treat pipeline differences as its strongest features instead of biology.

![UMAP projecting two osteosarcoma samples together with TCGA Pan-Cancer tumors and GTEx normal samples](/images/lessons/sam-and-bulk-rna/umap-pancancer.png)

*A UMAP containing 9,358 TCGA tumors, 2,502 GTEx normal samples, and two query samples. Cancer types form clusters, and the osteosarcoma samples from Boston and Tempus lie near the sarcoma group. The map provides an at-a-glance view of which cancers the query samples resemble.*



## Summary

UMAP compresses differences across tens of thousands of genes into two-dimensional coordinates and places similar samples near one another.

> **Overall expression pattern for each sample → select highly variable genes → dimensionality reduction → identify nearby reference groups**

Being nearby means that expression patterns are similar. It is not a diagnosis that the samples are the same cancer type. Preprocessing, batch, and the selected genes can also change the shape of the map.

The next lesson, [Testing differential expression across multiple samples](/en/lessons/bulk-rna-deg/), uses replicate samples to test differences between two groups statistically.