/
transform.jl
181 lines (124 loc) · 5.19 KB
/
transform.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
"""
normalize(prof, method; omicsname=:RNA, layer=:count, kwargs...)
normalize(X, method; kwargs...)
Normalize counts per cell.
# Arguments
- `prof::AnnotatedProfile`: The profile object to calculate on.
- `X::AbstractMatrix`: The count matrix to calculate on.
- `method`: Method to calculate highly variable genes, available for `LogNormalization`,
`RelativeNormalization`, `CenteredLogRatioNormalization` and `CustomNormalization`.
# Specific keyword arguments
- `omicsname::Symbol`: The `OmicsProfile` specified to calculate on.
- `layer::Symbol`: The layer specified to calculate on.
# Common keyword arguments
- `scaled_size::Real=1e4`: Scaled total counts for each cell over all genes.
# Examples
See also [`normalize!`](@ref) for inplace operation.
"""
function normalize end
"""
normalize!(prof, method; omicsname=:RNA, layer=:count, kwargs...)
normalize!(X, method; kwargs...)
Normalize counts per cell.
# Arguments
- `prof::AnnotatedProfile`: The profile object to calculate on.
- `X::AbstractMatrix`: The count matrix to calculate on.
- `method`: Method to calculate highly variable genes, available for `LogNormalization`,
`RelativeNormalization`, `CenteredLogRatioNormalization` and `CustomNormalization`.
# Specific keyword arguments
- `omicsname::Symbol`: The `OmicsProfile` specified to calculate on.
- `layer::Symbol`: The layer specified to calculate on.
# Common keyword arguments
- `scaled_size::Real=1e4`: Scaled total counts for each cell over all genes.
# Examples
See also [`normalize`](@ref) for non-inplace operation.
"""
function normalize! end
normalize(p::AnnotatedProfile, method::NormalizationMethod=LogNormalization(); kwargs...) =
normalize!(deepcopy(p), method; kwargs...)
normalize(X::AbstractMatrix, method::NormalizationMethod; kwargs...) =
normalize!(copy(X), method; kwargs...)
function normalize!(p::AnnotatedProfile, method::NormalizationMethod=LogNormalization();
omicsname::Symbol=:RNA, layer::Symbol=:count, kwargs...)
omic = p.omics[omicsname]
@assert haskey(omic.layers, layer) "$layer not found in layers."
X = getlayer(omic, layer)
@info "Normalizing $omicsname.$layer using $method method:"
normalize!(X, method; kwargs...)
@info " => normalized $omicsname.$layer"
setpipeline!(omic, :normalize, Dict(:method => Symbol(method), :layer => layer))
@info " => added :normalize to pipeline in $omicsname"
return p
end
normalize!(X::AbstractMatrix, method::NormalizationMethod; kwargs...) =
normalize!(X, method; kwargs...)
function normalize!(X::AbstractMatrix, ::LogNormalization; scaled_size::Real=1e4)
factors = scaled_size ./ sum(X, dims=1)
X .= @. log1p(X * factors)
return X
end
function normalize!(X::AbstractMatrix, ::RelativeNormalization; scaled_size::Real=1e4)
factors = scaled_size ./ sum(X, dims=1)
X .*= factors
return X
end
function normalize!(X::AbstractMatrix, ::CenteredLogRatioNormalization)
factor = sum(log1p.(X[X .> 0])) / length(X)
X .= @. log1p(X / exp(factor))
return X
end
function normalize!(X::AbstractMatrix, ::CustomNormalization; scaled_size::Real=1e4,
dims::Int=1)
error("This method is not implemented.")
end
"""
logarithmize(prof; omicsname=:RNA, layer=:count, kwargs...)
logarithmize(X; kwargs...)
Logarithmize the data matrix.
# Arguments
- `prof::AnnotatedProfile`: The profile object to calculate on.
- `X::AbstractMatrix`: The count matrix to calculate on.
# Specific keyword arguments
- `omicsname::Symbol`: The `OmicsProfile` specified to calculate on.
- `layer::Symbol`: The layer specified to calculate on.
# Common keyword arguments
- `base::Real=ℯ`: Base of the logarithm.
# Examples
See also [`logarithmize!`](@ref) for inplace operation.
"""
function logarithmize end
"""
logarithmize!(prof; omicsname=:RNA, layer=:count, kwargs...)
logarithmize!(X; kwargs...)
Logarithmize the data matrix.
# Arguments
- `prof::AnnotatedProfile`: The profile object to calculate on.
- `X::AbstractMatrix`: The count matrix to calculate on.
# Specific keyword arguments
- `omicsname::Symbol`: The `OmicsProfile` specified to calculate on.
- `layer::Symbol`: The layer specified to calculate on.
# Common keyword arguments
- `base::Real=ℯ`: Base of the logarithm.
# Examples
See also [`logarithmize`](@ref) for non-inplace operation.
"""
function logarithmize! end
logarithmize(p::AnnotatedProfile; kwargs...) = logarithmize!(deepcopy(p); kwargs...)
logarithmize(X::AbstractMatrix; kwargs...) = logarithmize!(copy(X); kwargs...)
function logarithmize!(p::AnnotatedProfile; omicsname::Symbol=:RNA, layer::Symbol=:count,
kwargs...)
omic = p.omics[omicsname]
@assert haskey(omic.layers, layer) "$layer not found in layers."
X = getlayer(omic, layer)
@info "Logarithmizing $omicsname.$layer:"
logarithmize!(X; kwargs...)
@info " => calculated log1p for $layer"
base = haskey(kwargs, :base) ? kwargs[:base] : ℯ
setpipeline!(omic, :log1p, Dict{Symbol,Any}(:base => base))
@info " => added :log1p to pipeline in $omicsname"
return p
end
function logarithmize!(X::AbstractMatrix; base::Real=ℯ)
X .= @. log1p(X) / log(base)
return X
end