Sets, Logic, and Combinations
A pathway analysis starts with collections of gene identifiers. Set operations describe which genes belong to both collections, either collection, or one collection only. Those operations also prepare the notation for probability.
Read algebra and notation first. The identifiers below form an invented example. Their membership does not represent a curated biological pathway.
Membership and overlap
Section titled “Membership and overlap”A set contains distinct elements. Repeated copies of the same identifier count once. The notation g ∈ A means that gene g belongs to set A. The size of a set is its cardinality, written |A|.
The intersection A ∩ B contains elements shared by both sets. The union A ∪ B contains elements in either set. The difference A ∖ B contains elements in A that are absent from B.
selected = {"G1", "G2", "G3", "G4"}pathway = {"G3", "G4", "G5"}print("Intersection:", sorted(selected & pathway))print("Union:", sorted(selected | pathway))print("Selected only:", sorted(selected - pathway))print("Overlap size:", len(selected & pathway))assert len(selected | pathway) == ( len(selected) + len(pathway) - len(selected & pathway))Intersection: ['G3', 'G4']Union: ['G1', 'G2', 'G3', 'G4', 'G5']Selected only: ['G1', 'G2']Overlap size: 2The subtraction in the union formula removes the shared elements counted twice. Sorting only controls the printed order. A set itself does not define a biological ranking.
Complements need a universe
Section titled “Complements need a universe”The complement of a set contains everything outside it within a defined universe. In an enrichment analysis, that universe might contain genes that were measured and eligible for selection. It should not automatically contain every gene in an annotation database.
universe = {"G1", "G2", "G3", "G4", "G5", "G6"}assert selected <= universeassert pathway <= universeprint("Not selected:", sorted(universe - selected))Not selected: ['G5', 'G6']The subset operator checks that the collections belong to the declared universe. Mismatched identifier systems can break this condition even when the biological genes correspond.
Logic becomes a filter
Section titled “Logic becomes a filter”The denominator changes when the universe changes. A selected set that occupies a large fraction of a small measured panel may occupy a small fraction of the whole genome. Both fractions can be calculated correctly, but only one universe may match the experiment.
An intersection measures shared membership, not evidence of enrichment by itself. Large sets can overlap extensively by chance. An enrichment test compares the observed overlap with a model that includes the set sizes and the eligible universe.
The statements “expression exceeds a threshold” and “quality passes” each have a truth value. Their conjunction requires both conditions. Their disjunction requires at least one. Negation reverses a truth value.
import numpy as np
expression = np.array([0.0, 5.0, 12.0, 8.0])quality_passes = np.array([True, True, False, True])keep = (expression > 4.0) & quality_passesprint("Keep:", keep)print("Retained expression:", expression[keep])Keep: [False True False True]Retained expression: [5. 8.]For NumPy arrays, use &, |, and ~ for elementwise Boolean operations. Put parentheses around each comparison. Python’s and and or do not combine arrays element by element.
Count choices without listing them
Section titled “Count choices without listing them”When order does not matter, the number of ways to select k distinct items from n is n! / (k!*(n-k)!). The exclamation mark denotes a factorial. When order matters, the count becomes n! / (n-k)!.
from math import comb, perm
print("Unordered pairs from six genes:", comb(6, 2))print("Ordered pairs from six genes:", perm(6, 2))Unordered pairs from six genes: 15Ordered pairs from six genes: 30These formulas assume selection without replacement. Choosing the same gene twice describes a different experiment. The hypergeometric distribution uses combinations to calculate overlap probabilities under random selection without replacement.
The difference between ordered and unordered pairs explains the extra factorial in the denominator. If a pair contains genes A and B, the sequences A then B and B then A describe the same unordered pair. Counting sequences first therefore counts that pair more than once.
For larger selections, dividing by k! removes every ordering of the selected items. This argument assumes distinct items. Repeated values, replacement, or restrictions on which items can appear together require a different count.
Check your understanding
Section titled “Check your understanding”Does a gene that fails either of two required filters remain in the retained set?
It does not. Requiring both conditions uses their intersection. Negating that requirement retains every gene that fails at least one condition. In symbols, not (A and B) equals (not A) or (not B). This is one of De Morgan’s laws, and it helps check compound filters.
Do not confuse independent events with disjoint events. Disjoint events cannot happen together. Independence concerns whether knowledge of one event changes the probability of the other.
Further reading
Section titled “Further reading”-
Python data structures tutorial
The sets section demonstrates membership, union, intersection, and difference. These operators extend the gene-set comparisons.
-
The comb and perm entries define selections with and without order. Compare their arguments before translating a counting question into a function call.
The over-representation guide applies that idea to gene sets. Continue first with vectors and matrices.