Scale¶
Scaling and normalization transforms. Use add_pseudocount before
log_transform to avoid log(0). Use normalize to make tracks from
different sequencing depths comparable. Use standardize for z-scores
or rank for rank-based comparisons.
add_pseudocount
¶
Add a constant to every score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track
|
Track
|
The track to transform. |
required |
value
|
float
|
The pseudocount to add. Defaults to |
1.0
|
Returns:
| Type | Description |
|---|---|
Track
|
A new Track with value added to all scores. |
Examples:
>>> from seqchain.track import TableTrack, TrackLabel
>>> t = TableTrack(TrackLabel("t"), {"a": 0.0, "b": 5.0})
>>> add_pseudocount(t, 1.0).get("a")
1.0
Source code in src/seqchain/transform/scale.py
log_transform
¶
Log-transform scores.
Edge cases
log(0)→-inf- negative scores →
NaN NaN→NaN
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track
|
Track
|
The track to transform. |
required |
base
|
float
|
Logarithm base. Defaults to |
2
|
Returns:
| Type | Description |
|---|---|
Track
|
A new Track with log-transformed scores. |
Examples:
>>> from seqchain.track import TableTrack, TrackLabel
>>> t = TableTrack(TrackLabel("t"), {"a": 8.0})
>>> log_transform(t, base=2).get("a")
3.0
Source code in src/seqchain/transform/scale.py
clamp
¶
Clip scores to a floor and/or ceiling.
NaN values pass through unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track
|
Track
|
The track to transform. |
required |
floor
|
float | None
|
Minimum score. |
None
|
ceiling
|
float | None
|
Maximum score. |
None
|
Returns:
| Type | Description |
|---|---|
Track
|
A new Track with clamped scores. |
Examples:
>>> from seqchain.track import TableTrack, TrackLabel
>>> t = TableTrack(TrackLabel("t"), {"a": -5.0, "b": 50.0, "c": 3.0})
>>> t2 = clamp(t, floor=0.0, ceiling=10.0)
>>> t2.get("a"), t2.get("b"), t2.get("c")
(0.0, 10.0, 3.0)
Source code in src/seqchain/transform/scale.py
normalize
¶
Rescale scores by a normalization method.
Supported methods:
"cpm": counts per million. Each score is divided by the sum of all non-NaN scores and multiplied by 1,000,000. If total is provided, it overrides the computed sum."fraction": each score divided by the sum of all non-NaN scores. If total is provided, it overrides the computed sum."median_ratio": each score divided by the median of non-zero, non-NaN scores. This is a simple median normalization, not DESeq2-style size factors.
NaN values pass through unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track
|
Track
|
The track to transform. |
required |
method
|
str
|
Normalization method. Defaults to |
'cpm'
|
total
|
float | None
|
Override for the computed sum (cpm, fraction only). |
None
|
Returns:
| Type | Description |
|---|---|
Track
|
A new Track with normalized scores. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If method is not recognized. |
Examples:
>>> from seqchain.track import TableTrack, TrackLabel
>>> t = TableTrack(TrackLabel("t"), {"a": 500.0, "b": 500.0})
>>> t2 = normalize(t, method="cpm")
>>> t2.get("a")
500000.0
Source code in src/seqchain/transform/scale.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
standardize
¶
Z-score normalization: (score - mean) / std.
Computes mean and standard deviation over non-NaN scores, then transforms each score. NaN values pass through unchanged. If fewer than 2 non-NaN values exist, all scores become NaN.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track
|
Track
|
The track to transform. |
required |
Returns:
| Type | Description |
|---|---|
Track
|
A new Track with z-scored values. |
Examples:
>>> from seqchain.track import TableTrack, TrackLabel
>>> t = TableTrack(TrackLabel("t"), {"a": 10.0, "b": 20.0, "c": 30.0})
>>> t2 = standardize(t)
>>> abs(t2.get("b")) < 1e-10 # mean is at center
True
Source code in src/seqchain/transform/scale.py
rank
¶
Replace scores with their rank (1-based, average ties).
NaN scores receive NaN rank.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
track
|
Track
|
The track to transform. |
required |
Returns:
| Type | Description |
|---|---|
Track
|
A new Track with rank values. |
Examples:
>>> from seqchain.track import TableTrack, TrackLabel
>>> t = TableTrack(TrackLabel("t"), {"a": 30.0, "b": 10.0, "c": 20.0})
>>> t2 = rank(t)
>>> t2.get("b"), t2.get("c"), t2.get("a")
(1.0, 2.0, 3.0)