py-ruvseq-omicverse 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- py_ruvseq_omicverse-0.1.0.dist-info/METADATA +260 -0
- py_ruvseq_omicverse-0.1.0.dist-info/RECORD +9 -0
- py_ruvseq_omicverse-0.1.0.dist-info/WHEEL +5 -0
- py_ruvseq_omicverse-0.1.0.dist-info/licenses/ATTRIBUTION.md +33 -0
- py_ruvseq_omicverse-0.1.0.dist-info/licenses/LICENSE +197 -0
- py_ruvseq_omicverse-0.1.0.dist-info/top_level.txt +1 -0
- pyruvseq/__init__.py +3 -0
- pyruvseq/_other.py +132 -0
- pyruvseq/_ruvg.py +139 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py-ruvseq-omicverse
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Reference-validated py-RUVSeq algorithm subset
|
|
5
|
+
License: Artistic License 2.0
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2000-2006, The Perl Foundation.
|
|
8
|
+
|
|
9
|
+
Everyone is permitted to copy and distribute verbatim copies of this
|
|
10
|
+
license document, but changing it is not allowed.
|
|
11
|
+
|
|
12
|
+
Preamble
|
|
13
|
+
********
|
|
14
|
+
|
|
15
|
+
This license establishes the terms under which a given free software
|
|
16
|
+
Package may be copied, modified, distributed, and/or redistributed. The
|
|
17
|
+
intent is that the Copyright Holder maintains some artistic control over
|
|
18
|
+
the development of that Package while still keeping the Package
|
|
19
|
+
available as open source and free software.
|
|
20
|
+
|
|
21
|
+
You are always permitted to make arrangements wholly outside of this
|
|
22
|
+
license directly with the Copyright Holder of a given Package. If the
|
|
23
|
+
terms of this license do not permit the full use that you propose to
|
|
24
|
+
make of the Package, you should contact the Copyright Holder and seek a
|
|
25
|
+
different licensing arrangement.
|
|
26
|
+
|
|
27
|
+
Definitions
|
|
28
|
+
***********
|
|
29
|
+
|
|
30
|
+
"Copyright Holder" means the individual(s) or organization(s) named in
|
|
31
|
+
the copyright notice for the entire Package.
|
|
32
|
+
|
|
33
|
+
"Contributor" means any party that has contributed code or other
|
|
34
|
+
material to the Package, in accordance with the Copyright Holder's
|
|
35
|
+
procedures.
|
|
36
|
+
|
|
37
|
+
"You" and "your" means any person who would like to copy, distribute, or
|
|
38
|
+
modify the Package.
|
|
39
|
+
|
|
40
|
+
"Package" means the collection of files distributed by the Copyright
|
|
41
|
+
Holder, and derivatives of that collection and/or of those files. A
|
|
42
|
+
given Package may consist of either the Standard Version, or a Modified
|
|
43
|
+
Version.
|
|
44
|
+
|
|
45
|
+
"Distribute" means providing a copy of the Package or making it
|
|
46
|
+
accessible to anyone else, or in the case of a company or organization,
|
|
47
|
+
to others outside of your company or organization.
|
|
48
|
+
|
|
49
|
+
"Distributor Fee" means any fee that you charge for Distributing this
|
|
50
|
+
Package or providing support for this Package to another party. It does
|
|
51
|
+
not mean licensing fees.
|
|
52
|
+
|
|
53
|
+
"Standard Version" refers to the Package if it has not been modified, or
|
|
54
|
+
has been modified only in ways explicitly requested by the Copyright
|
|
55
|
+
Holder.
|
|
56
|
+
|
|
57
|
+
"Modified Version" means the Package, if it has been changed, and such
|
|
58
|
+
changes were not explicitly requested by the Copyright Holder.
|
|
59
|
+
|
|
60
|
+
"Original License" means this Artistic License as Distributed with the
|
|
61
|
+
Standard Version of the Package, in its current version or as it may be
|
|
62
|
+
modified by The Perl Foundation in the future.
|
|
63
|
+
|
|
64
|
+
"Source" form means the source code, documentation source, and
|
|
65
|
+
configuration files for the Package.
|
|
66
|
+
|
|
67
|
+
"Compiled" form means the compiled bytecode, object code, binary, or any
|
|
68
|
+
other form resulting from mechanical transformation or translation of
|
|
69
|
+
the Source form.
|
|
70
|
+
|
|
71
|
+
Permission for Use and Modification Without Distribution
|
|
72
|
+
********************************************************
|
|
73
|
+
|
|
74
|
+
(1) You are permitted to use the Standard Version and create and use
|
|
75
|
+
Modified Versions for any purpose without restriction, provided that you
|
|
76
|
+
do not Distribute the Modified Version.
|
|
77
|
+
|
|
78
|
+
Permissions for Redistribution of the Standard Version
|
|
79
|
+
******************************************************
|
|
80
|
+
|
|
81
|
+
(2) You may Distribute verbatim copies of the Source form of the
|
|
82
|
+
Standard Version of this Package in any medium without restriction,
|
|
83
|
+
either gratis or for a Distributor Fee, provided that you duplicate all
|
|
84
|
+
of the original copyright notices and associated disclaimers. At your
|
|
85
|
+
discretion, such verbatim copies may or may not include a Compiled form
|
|
86
|
+
of the Package.
|
|
87
|
+
|
|
88
|
+
(3) You may apply any bug fixes, portability changes, and other
|
|
89
|
+
modifications made available from the Copyright Holder. The resulting
|
|
90
|
+
Package will still be considered the Standard Version, and as such will
|
|
91
|
+
be subject to the Original License.
|
|
92
|
+
|
|
93
|
+
Distribution of Modified Versions of the Package as Source
|
|
94
|
+
**********************************************************
|
|
95
|
+
|
|
96
|
+
(4) You may Distribute your Modified Version as Source (either gratis or
|
|
97
|
+
for a Distributor Fee, and with or without a Compiled form of the
|
|
98
|
+
Modified Version) provided that you clearly document how it differs from
|
|
99
|
+
the Standard Version, including, but not limited to, documenting any
|
|
100
|
+
non-standard features, executables, or modules, and provided that you do
|
|
101
|
+
at least ONE of the following:
|
|
102
|
+
|
|
103
|
+
(a) make the Modified Version available to the Copyright Holder of the
|
|
104
|
+
Standard Version, under the Original License, so that the Copyright
|
|
105
|
+
Holder may include your modifications in the Standard Version.
|
|
106
|
+
|
|
107
|
+
(b) ensure that installation of your Modified Version does not prevent
|
|
108
|
+
the user installing or running the Standard Version. In addition, the
|
|
109
|
+
Modified Version must bear a name that is different from the name of the
|
|
110
|
+
Standard Version.
|
|
111
|
+
|
|
112
|
+
(c) allow anyone who receives a copy of the Modified Version to make the
|
|
113
|
+
Source form of the Modified Version available to others under
|
|
114
|
+
|
|
115
|
+
(i) the Original License or
|
|
116
|
+
|
|
117
|
+
(ii) a license that permits the licensee to freely copy, modify and
|
|
118
|
+
redistribute the Modified Version using the same licensing terms that
|
|
119
|
+
apply to the copy that the licensee received, and requires that the
|
|
120
|
+
Source form of the Modified Version, and of any works derived from it,
|
|
121
|
+
be made freely available in that license fees are prohibited but
|
|
122
|
+
Distributor Fees are allowed.
|
|
123
|
+
|
|
124
|
+
Distribution of Compiled Forms of the Standard Version or Modified
|
|
125
|
+
******************************************************************
|
|
126
|
+
Versions without the Source
|
|
127
|
+
***************************
|
|
128
|
+
|
|
129
|
+
(5) You may Distribute Compiled forms of the Standard Version without
|
|
130
|
+
the Source, provided that you include complete instructions on how to
|
|
131
|
+
get the Source of the Standard Version. Such instructions must be valid
|
|
132
|
+
at the time of your distribution. If these instructions, at any time
|
|
133
|
+
while you are carrying out such distribution, become invalid, you must
|
|
134
|
+
provide new instructions on demand or cease further distribution. If
|
|
135
|
+
you provide valid instructions or cease distribution within thirty days
|
|
136
|
+
after you become aware that the instructions are invalid, then you do
|
|
137
|
+
not forfeit any of your rights under this license.
|
|
138
|
+
|
|
139
|
+
(6) You may Distribute a Modified Version in Compiled form without the
|
|
140
|
+
Source, provided that you comply with Section 4 with respect to the
|
|
141
|
+
Source of the Modified Version.
|
|
142
|
+
|
|
143
|
+
Aggregating or Linking the Package
|
|
144
|
+
**********************************
|
|
145
|
+
|
|
146
|
+
(7) You may aggregate the Package (either the Standard Version or
|
|
147
|
+
Modified Version) with other packages and Distribute the resulting
|
|
148
|
+
aggregation provided that you do not charge a licensing fee for the
|
|
149
|
+
Package. Distributor Fees are permitted, and licensing fees for other
|
|
150
|
+
components in the aggregation are permitted. The terms of this license
|
|
151
|
+
apply to the use and Distribution of the Standard or Modified Versions
|
|
152
|
+
as included in the aggregation.
|
|
153
|
+
|
|
154
|
+
(8) You are permitted to link Modified and Standard Versions with other
|
|
155
|
+
works, to embed the Package in a larger work of your own, or to build
|
|
156
|
+
stand-alone binary or bytecode versions of applications that include the
|
|
157
|
+
Package, and Distribute the result without restriction, provided the
|
|
158
|
+
result does not expose a direct interface to the Package.
|
|
159
|
+
|
|
160
|
+
Items That are Not Considered Part of a Modified Version
|
|
161
|
+
********************************************************
|
|
162
|
+
|
|
163
|
+
(9) Works (including, but not limited to, modules and scripts) that
|
|
164
|
+
merely extend or make use of the Package, do not, by themselves, cause
|
|
165
|
+
the Package to be a Modified Version. In addition, such works are not
|
|
166
|
+
considered parts of the Package itself, and are not subject to the terms
|
|
167
|
+
of this license.
|
|
168
|
+
|
|
169
|
+
General Provisions
|
|
170
|
+
******************
|
|
171
|
+
|
|
172
|
+
(10) Any use, modification, and distribution of the Standard or Modified
|
|
173
|
+
Versions is governed by this Artistic License. By using, modifying or
|
|
174
|
+
distributing the Package, you accept this license. Do not use, modify,
|
|
175
|
+
or distribute the Package, if you do not accept this license.
|
|
176
|
+
|
|
177
|
+
(11) If your Modified Version has been derived from a Modified Version
|
|
178
|
+
made by someone other than you, you are nevertheless required to ensure
|
|
179
|
+
that your Modified Version complies with the requirements of this
|
|
180
|
+
license.
|
|
181
|
+
|
|
182
|
+
(12) This license does not grant you the right to use any trademark,
|
|
183
|
+
service mark, tradename, or logo of the Copyright Holder.
|
|
184
|
+
|
|
185
|
+
(13) This license includes the non-exclusive, worldwide, free-of-charge
|
|
186
|
+
patent license to make, have made, use, offer to sell, sell, import and
|
|
187
|
+
otherwise transfer the Package with respect to any patent claims
|
|
188
|
+
licensable by the Copyright Holder that are necessarily infringed by the
|
|
189
|
+
Package. If you institute patent litigation (including a cross-claim or
|
|
190
|
+
counterclaim) against any party alleging that the Package constitutes
|
|
191
|
+
direct or contributory patent infringement, then this Artistic License
|
|
192
|
+
to you shall terminate on the date that such litigation is filed.
|
|
193
|
+
|
|
194
|
+
(14) Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT
|
|
195
|
+
HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED
|
|
196
|
+
WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
197
|
+
PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT
|
|
198
|
+
PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT
|
|
199
|
+
HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
200
|
+
INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
|
|
201
|
+
OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
202
|
+
|
|
203
|
+
Project-URL: Repository, https://github.com/omicverse/py-RUVSeq
|
|
204
|
+
Project-URL: Issues, https://github.com/omicverse/py-RUVSeq/issues
|
|
205
|
+
Requires-Python: >=3.10
|
|
206
|
+
Description-Content-Type: text/markdown
|
|
207
|
+
License-File: LICENSE
|
|
208
|
+
License-File: ATTRIBUTION.md
|
|
209
|
+
Requires-Dist: numpy>=1.23
|
|
210
|
+
Requires-Dist: scipy>=1.10
|
|
211
|
+
Requires-Dist: pandas>=1.5
|
|
212
|
+
Provides-Extra: test
|
|
213
|
+
Requires-Dist: pytest>=7; extra == "test"
|
|
214
|
+
Dynamic: license-file
|
|
215
|
+
|
|
216
|
+
# py-RUVSeq
|
|
217
|
+
|
|
218
|
+
Review-stage Python reconstruction of a **subset** of RUVSeq, following the discovery, preregistration and reference-validation workflow in [omicverse-rebuildr](https://github.com/omicverse/omicverse-rebuildr).
|
|
219
|
+
|
|
220
|
+
RUVg, RUVr and RUVs matrix methods plus replicate-index construction. No EDASeq objects, automatic control selection or downstream DE.
|
|
221
|
+
|
|
222
|
+
## Install and test
|
|
223
|
+
|
|
224
|
+
This project has not been published on PyPI. From this checkout:
|
|
225
|
+
|
|
226
|
+
```bash
|
|
227
|
+
python -m pip install ".[test]"
|
|
228
|
+
python -m pytest -q
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## Usage and function map
|
|
232
|
+
|
|
233
|
+
See [the executed tutorial](examples/tutorial_fixture.ipynb), [R-to-Python parameter dictionary](examples/function_by_function_R_parity.ipynb), and [complete upstream export audit](AUDIT.md).
|
|
234
|
+
Inputs, orientations and unsupported modes are documented in function docstrings. No R process is used at runtime.
|
|
235
|
+
|
|
236
|
+
## Reproduce the R oracle
|
|
237
|
+
|
|
238
|
+
```bash
|
|
239
|
+
git clone https://github.com/drisso/RUVSeq.git ../RUVSeq-ref
|
|
240
|
+
git -C ../RUVSeq-ref checkout f560e84eb493f0c2d72279612f9e4ca99493b434
|
|
241
|
+
Rscript tests/reference.R ../RUVSeq-ref
|
|
242
|
+
python -m pytest -q
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
The runner sources the pinned upstream functions, not a rewritten R approximation. RUVSeq S4 registration is intercepted to execute only matrix methods. SPARK sparse sum/mean helpers are supplied by Matrix, with unchanged statistical functions. MuSiC and muscat functions are sourced with their R dependencies loaded. muscat's source declares R >=4.6; this selected function subset was executed on R 4.5.2, not installed as the full package.
|
|
246
|
+
|
|
247
|
+
muscat's public fixture additionally uses `Rscript tests/public_reference.R ../muscat-ref`.
|
|
248
|
+
All other primary fixtures are synthetic and do not establish biological accuracy. Numerical gates were committed before implementation and have not been widened.
|
|
249
|
+
|
|
250
|
+
## Validation and limitations
|
|
251
|
+
|
|
252
|
+
28 focused tests passed locally without skips. See [reconstruction report](RECONSTRUCTION_REPORT.md) for scope and remaining release checks. This is fixture-level equivalence, not full-domain or full-package equivalence. Runtime objects and failure messages are Python-native.
|
|
253
|
+
|
|
254
|
+
## Citation and license
|
|
255
|
+
|
|
256
|
+
Upstream [RUVSeq](https://github.com/drisso/RUVSeq) at `f560e84eb493f0c2d72279612f9e4ca99493b434`; see ATTRIBUTION.md for upstream authors and package citation context, and LICENSE for terms.
|
|
257
|
+
|
|
258
|
+
## Distribution preparation
|
|
259
|
+
|
|
260
|
+
The intended PyPI distribution is `py-ruvseq-omicverse`; the import remains `pyruvseq`. See [release preparation](RELEASE.md) for installed-wheel validation and the manual Trusted Publishing procedure. This preparation does not mean a PyPI release is available.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
py_ruvseq_omicverse-0.1.0.dist-info/licenses/ATTRIBUTION.md,sha256=cFiNeQb8-RYBn0LBc8jjAutnddpyugFmUnc2L-NXuHQ,1300
|
|
2
|
+
py_ruvseq_omicverse-0.1.0.dist-info/licenses/LICENSE,sha256=TKuwBAaxCrlg0-u1wU5QQs9U0rlwzEBHWIqijHA1Jf4,9073
|
|
3
|
+
pyruvseq/__init__.py,sha256=pQ6ZCGg8Vm6CWWD2kuhZQhlOBYwIJ_QSO8LckiBCr6E,118
|
|
4
|
+
pyruvseq/_other.py,sha256=yIp0rC9fJ5TppU6TL5hny1xbD1DVsGuSexILFjlmjr4,6485
|
|
5
|
+
pyruvseq/_ruvg.py,sha256=pdN2IS3llx5GpACTv3KKQThJK_LwhYAVyRJi8PpOQL0,7365
|
|
6
|
+
py_ruvseq_omicverse-0.1.0.dist-info/METADATA,sha256=ZWsWgBRJUE8BeiZFY7_BSgmS8nB8SWGYFFw30rGGsao,13788
|
|
7
|
+
py_ruvseq_omicverse-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
8
|
+
py_ruvseq_omicverse-0.1.0.dist-info/top_level.txt,sha256=NzTk8o6EvLgWCVdF5SowpkgiVCZ3bYCx0gzw5EcpX9Q,9
|
|
9
|
+
py_ruvseq_omicverse-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Attribution
|
|
2
|
+
|
|
3
|
+
Upstream RUVSeq 1.33.1, commit f560e84eb493f0c2d72279612f9e4ca99493b434.
|
|
4
|
+
Algorithms reconstructed from the upstream source under the accompanying LICENSE.
|
|
5
|
+
Upstream author declarations:
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
Package: RUVSeq
|
|
9
|
+
Version: 1.33.1
|
|
10
|
+
Title: Remove Unwanted Variation from RNA-Seq Data
|
|
11
|
+
Description: This package implements the remove unwanted variation
|
|
12
|
+
(RUV) methods of Risso et al. (2014) for the normalization of
|
|
13
|
+
RNA-Seq read counts between samples.
|
|
14
|
+
Authors@R: c(person("Davide", "Risso", email = "risso.davide@gmail.com",
|
|
15
|
+
role = c("aut", "cre", "cph")),
|
|
16
|
+
person("Sandrine", "Dudoit", role = "aut"),
|
|
17
|
+
person("Lorena", "Pantano", role = "ctb"),
|
|
18
|
+
person("Kamil", "Slowikowski", role = "ctb"))
|
|
19
|
+
Author: Davide Risso [aut, cre, cph], Sandrine Dudoit [aut], Lorena Pantano [ctb], Kamil Slowikowski [ctb]
|
|
20
|
+
Maintainer: Davide Risso <risso.davide@gmail.com>
|
|
21
|
+
Date: 04-15-2014
|
|
22
|
+
Imports: methods, MASS
|
|
23
|
+
Depends: Biobase, EDASeq (>= 1.99.1), edgeR
|
|
24
|
+
Suggests: BiocStyle, knitr, RColorBrewer, zebrafishRNASeq, DESeq2
|
|
25
|
+
VignetteBuilder: knitr
|
|
26
|
+
License: Artistic-2.0
|
|
27
|
+
LazyLoad: yes
|
|
28
|
+
biocViews: ImmunoOncology, DifferentialExpression, Preprocessing, RNASeq, Software
|
|
29
|
+
URL: https://github.com/drisso/RUVSeq
|
|
30
|
+
BugReports: https://github.com/drisso/RUVSeq/issues
|
|
31
|
+
RoxygenNote: 6.0.1
|
|
32
|
+
|
|
33
|
+
```
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
Artistic License 2.0
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2000-2006, The Perl Foundation.
|
|
4
|
+
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies of this
|
|
6
|
+
license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
Preamble
|
|
9
|
+
********
|
|
10
|
+
|
|
11
|
+
This license establishes the terms under which a given free software
|
|
12
|
+
Package may be copied, modified, distributed, and/or redistributed. The
|
|
13
|
+
intent is that the Copyright Holder maintains some artistic control over
|
|
14
|
+
the development of that Package while still keeping the Package
|
|
15
|
+
available as open source and free software.
|
|
16
|
+
|
|
17
|
+
You are always permitted to make arrangements wholly outside of this
|
|
18
|
+
license directly with the Copyright Holder of a given Package. If the
|
|
19
|
+
terms of this license do not permit the full use that you propose to
|
|
20
|
+
make of the Package, you should contact the Copyright Holder and seek a
|
|
21
|
+
different licensing arrangement.
|
|
22
|
+
|
|
23
|
+
Definitions
|
|
24
|
+
***********
|
|
25
|
+
|
|
26
|
+
"Copyright Holder" means the individual(s) or organization(s) named in
|
|
27
|
+
the copyright notice for the entire Package.
|
|
28
|
+
|
|
29
|
+
"Contributor" means any party that has contributed code or other
|
|
30
|
+
material to the Package, in accordance with the Copyright Holder's
|
|
31
|
+
procedures.
|
|
32
|
+
|
|
33
|
+
"You" and "your" means any person who would like to copy, distribute, or
|
|
34
|
+
modify the Package.
|
|
35
|
+
|
|
36
|
+
"Package" means the collection of files distributed by the Copyright
|
|
37
|
+
Holder, and derivatives of that collection and/or of those files. A
|
|
38
|
+
given Package may consist of either the Standard Version, or a Modified
|
|
39
|
+
Version.
|
|
40
|
+
|
|
41
|
+
"Distribute" means providing a copy of the Package or making it
|
|
42
|
+
accessible to anyone else, or in the case of a company or organization,
|
|
43
|
+
to others outside of your company or organization.
|
|
44
|
+
|
|
45
|
+
"Distributor Fee" means any fee that you charge for Distributing this
|
|
46
|
+
Package or providing support for this Package to another party. It does
|
|
47
|
+
not mean licensing fees.
|
|
48
|
+
|
|
49
|
+
"Standard Version" refers to the Package if it has not been modified, or
|
|
50
|
+
has been modified only in ways explicitly requested by the Copyright
|
|
51
|
+
Holder.
|
|
52
|
+
|
|
53
|
+
"Modified Version" means the Package, if it has been changed, and such
|
|
54
|
+
changes were not explicitly requested by the Copyright Holder.
|
|
55
|
+
|
|
56
|
+
"Original License" means this Artistic License as Distributed with the
|
|
57
|
+
Standard Version of the Package, in its current version or as it may be
|
|
58
|
+
modified by The Perl Foundation in the future.
|
|
59
|
+
|
|
60
|
+
"Source" form means the source code, documentation source, and
|
|
61
|
+
configuration files for the Package.
|
|
62
|
+
|
|
63
|
+
"Compiled" form means the compiled bytecode, object code, binary, or any
|
|
64
|
+
other form resulting from mechanical transformation or translation of
|
|
65
|
+
the Source form.
|
|
66
|
+
|
|
67
|
+
Permission for Use and Modification Without Distribution
|
|
68
|
+
********************************************************
|
|
69
|
+
|
|
70
|
+
(1) You are permitted to use the Standard Version and create and use
|
|
71
|
+
Modified Versions for any purpose without restriction, provided that you
|
|
72
|
+
do not Distribute the Modified Version.
|
|
73
|
+
|
|
74
|
+
Permissions for Redistribution of the Standard Version
|
|
75
|
+
******************************************************
|
|
76
|
+
|
|
77
|
+
(2) You may Distribute verbatim copies of the Source form of the
|
|
78
|
+
Standard Version of this Package in any medium without restriction,
|
|
79
|
+
either gratis or for a Distributor Fee, provided that you duplicate all
|
|
80
|
+
of the original copyright notices and associated disclaimers. At your
|
|
81
|
+
discretion, such verbatim copies may or may not include a Compiled form
|
|
82
|
+
of the Package.
|
|
83
|
+
|
|
84
|
+
(3) You may apply any bug fixes, portability changes, and other
|
|
85
|
+
modifications made available from the Copyright Holder. The resulting
|
|
86
|
+
Package will still be considered the Standard Version, and as such will
|
|
87
|
+
be subject to the Original License.
|
|
88
|
+
|
|
89
|
+
Distribution of Modified Versions of the Package as Source
|
|
90
|
+
**********************************************************
|
|
91
|
+
|
|
92
|
+
(4) You may Distribute your Modified Version as Source (either gratis or
|
|
93
|
+
for a Distributor Fee, and with or without a Compiled form of the
|
|
94
|
+
Modified Version) provided that you clearly document how it differs from
|
|
95
|
+
the Standard Version, including, but not limited to, documenting any
|
|
96
|
+
non-standard features, executables, or modules, and provided that you do
|
|
97
|
+
at least ONE of the following:
|
|
98
|
+
|
|
99
|
+
(a) make the Modified Version available to the Copyright Holder of the
|
|
100
|
+
Standard Version, under the Original License, so that the Copyright
|
|
101
|
+
Holder may include your modifications in the Standard Version.
|
|
102
|
+
|
|
103
|
+
(b) ensure that installation of your Modified Version does not prevent
|
|
104
|
+
the user installing or running the Standard Version. In addition, the
|
|
105
|
+
Modified Version must bear a name that is different from the name of the
|
|
106
|
+
Standard Version.
|
|
107
|
+
|
|
108
|
+
(c) allow anyone who receives a copy of the Modified Version to make the
|
|
109
|
+
Source form of the Modified Version available to others under
|
|
110
|
+
|
|
111
|
+
(i) the Original License or
|
|
112
|
+
|
|
113
|
+
(ii) a license that permits the licensee to freely copy, modify and
|
|
114
|
+
redistribute the Modified Version using the same licensing terms that
|
|
115
|
+
apply to the copy that the licensee received, and requires that the
|
|
116
|
+
Source form of the Modified Version, and of any works derived from it,
|
|
117
|
+
be made freely available in that license fees are prohibited but
|
|
118
|
+
Distributor Fees are allowed.
|
|
119
|
+
|
|
120
|
+
Distribution of Compiled Forms of the Standard Version or Modified
|
|
121
|
+
******************************************************************
|
|
122
|
+
Versions without the Source
|
|
123
|
+
***************************
|
|
124
|
+
|
|
125
|
+
(5) You may Distribute Compiled forms of the Standard Version without
|
|
126
|
+
the Source, provided that you include complete instructions on how to
|
|
127
|
+
get the Source of the Standard Version. Such instructions must be valid
|
|
128
|
+
at the time of your distribution. If these instructions, at any time
|
|
129
|
+
while you are carrying out such distribution, become invalid, you must
|
|
130
|
+
provide new instructions on demand or cease further distribution. If
|
|
131
|
+
you provide valid instructions or cease distribution within thirty days
|
|
132
|
+
after you become aware that the instructions are invalid, then you do
|
|
133
|
+
not forfeit any of your rights under this license.
|
|
134
|
+
|
|
135
|
+
(6) You may Distribute a Modified Version in Compiled form without the
|
|
136
|
+
Source, provided that you comply with Section 4 with respect to the
|
|
137
|
+
Source of the Modified Version.
|
|
138
|
+
|
|
139
|
+
Aggregating or Linking the Package
|
|
140
|
+
**********************************
|
|
141
|
+
|
|
142
|
+
(7) You may aggregate the Package (either the Standard Version or
|
|
143
|
+
Modified Version) with other packages and Distribute the resulting
|
|
144
|
+
aggregation provided that you do not charge a licensing fee for the
|
|
145
|
+
Package. Distributor Fees are permitted, and licensing fees for other
|
|
146
|
+
components in the aggregation are permitted. The terms of this license
|
|
147
|
+
apply to the use and Distribution of the Standard or Modified Versions
|
|
148
|
+
as included in the aggregation.
|
|
149
|
+
|
|
150
|
+
(8) You are permitted to link Modified and Standard Versions with other
|
|
151
|
+
works, to embed the Package in a larger work of your own, or to build
|
|
152
|
+
stand-alone binary or bytecode versions of applications that include the
|
|
153
|
+
Package, and Distribute the result without restriction, provided the
|
|
154
|
+
result does not expose a direct interface to the Package.
|
|
155
|
+
|
|
156
|
+
Items That are Not Considered Part of a Modified Version
|
|
157
|
+
********************************************************
|
|
158
|
+
|
|
159
|
+
(9) Works (including, but not limited to, modules and scripts) that
|
|
160
|
+
merely extend or make use of the Package, do not, by themselves, cause
|
|
161
|
+
the Package to be a Modified Version. In addition, such works are not
|
|
162
|
+
considered parts of the Package itself, and are not subject to the terms
|
|
163
|
+
of this license.
|
|
164
|
+
|
|
165
|
+
General Provisions
|
|
166
|
+
******************
|
|
167
|
+
|
|
168
|
+
(10) Any use, modification, and distribution of the Standard or Modified
|
|
169
|
+
Versions is governed by this Artistic License. By using, modifying or
|
|
170
|
+
distributing the Package, you accept this license. Do not use, modify,
|
|
171
|
+
or distribute the Package, if you do not accept this license.
|
|
172
|
+
|
|
173
|
+
(11) If your Modified Version has been derived from a Modified Version
|
|
174
|
+
made by someone other than you, you are nevertheless required to ensure
|
|
175
|
+
that your Modified Version complies with the requirements of this
|
|
176
|
+
license.
|
|
177
|
+
|
|
178
|
+
(12) This license does not grant you the right to use any trademark,
|
|
179
|
+
service mark, tradename, or logo of the Copyright Holder.
|
|
180
|
+
|
|
181
|
+
(13) This license includes the non-exclusive, worldwide, free-of-charge
|
|
182
|
+
patent license to make, have made, use, offer to sell, sell, import and
|
|
183
|
+
otherwise transfer the Package with respect to any patent claims
|
|
184
|
+
licensable by the Copyright Holder that are necessarily infringed by the
|
|
185
|
+
Package. If you institute patent litigation (including a cross-claim or
|
|
186
|
+
counterclaim) against any party alleging that the Package constitutes
|
|
187
|
+
direct or contributory patent infringement, then this Artistic License
|
|
188
|
+
to you shall terminate on the date that such litigation is filed.
|
|
189
|
+
|
|
190
|
+
(14) Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT
|
|
191
|
+
HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED
|
|
192
|
+
WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
193
|
+
PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT
|
|
194
|
+
PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT
|
|
195
|
+
HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
196
|
+
INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
|
|
197
|
+
OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyruvseq
|
pyruvseq/__init__.py
ADDED
pyruvseq/_other.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"""RUVr/RUVs matrix methods; see pinned RUVSeq source in manifest.yaml.
|
|
2
|
+
|
|
3
|
+
RUVSeq authors: Davide Risso and Sandrine Dudoit; Artistic-2.0.
|
|
4
|
+
"""
|
|
5
|
+
import warnings
|
|
6
|
+
import numpy as np
|
|
7
|
+
import pandas as pd
|
|
8
|
+
from scipy import sparse
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _prepare(x, controls, k, epsilon, tolerance, is_log):
|
|
12
|
+
labels = x if isinstance(x, pd.DataFrame) else None
|
|
13
|
+
values = x.to_numpy() if labels is not None else x
|
|
14
|
+
values = values.toarray() if sparse.issparse(values) else np.asarray(values)
|
|
15
|
+
if (values.ndim != 2 or min(values.shape) == 0 or
|
|
16
|
+
not np.issubdtype(values.dtype, np.number) or np.iscomplexobj(values) or
|
|
17
|
+
not np.isfinite(values).all()):
|
|
18
|
+
raise ValueError('x must be a finite genes-by-samples matrix.')
|
|
19
|
+
if isinstance(k, (bool, np.bool_)) or not isinstance(k, (int, np.integer)) or not 0 < k < values.shape[1]:
|
|
20
|
+
raise ValueError('k must be a positive integer less than the sample count.')
|
|
21
|
+
if not np.isfinite(epsilon) or epsilon <= 0 or not np.isfinite(tolerance) or tolerance < 0:
|
|
22
|
+
raise ValueError('Require positive epsilon and nonnegative tolerance.')
|
|
23
|
+
if not is_log and (np.any(values < 0) or not np.allclose(values, np.rint(values), rtol=0, atol=1e-8)):
|
|
24
|
+
raise ValueError('Use raw integer counts or set is_log=True.')
|
|
25
|
+
selected = np.asarray(controls)
|
|
26
|
+
if selected.ndim != 1 or not len(selected):
|
|
27
|
+
raise ValueError('Select a nonempty one-dimensional set of controls.')
|
|
28
|
+
if labels is not None and (not labels.index.is_unique or not labels.columns.is_unique):
|
|
29
|
+
raise ValueError('Gene and sample labels must be unique.')
|
|
30
|
+
if selected.dtype.kind == 'b':
|
|
31
|
+
if len(selected) != values.shape[0]:
|
|
32
|
+
raise ValueError('Control mask length mismatch.')
|
|
33
|
+
idx = np.flatnonzero(selected)
|
|
34
|
+
elif labels is not None:
|
|
35
|
+
idx = labels.index.get_indexer(selected)
|
|
36
|
+
elif selected.dtype.kind in 'iu':
|
|
37
|
+
idx = selected
|
|
38
|
+
else:
|
|
39
|
+
raise ValueError('Array controls must be integer indices or a boolean mask.')
|
|
40
|
+
if not len(idx) or len(np.unique(idx)) != len(idx) or np.any(idx < 0) or np.any(idx >= values.shape[0]):
|
|
41
|
+
raise ValueError('Controls must be distinct valid genes.')
|
|
42
|
+
y = values.astype(float).T
|
|
43
|
+
return (y if is_log else np.log(y + epsilon)), idx, labels
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _rank(d, k, tolerance):
|
|
47
|
+
effective = min(k, int(np.count_nonzero(d > tolerance)))
|
|
48
|
+
if not effective:
|
|
49
|
+
raise ValueError('No nonzero control subspace remains.')
|
|
50
|
+
if effective < k:
|
|
51
|
+
warnings.warn(f'Rank reduces k from {k} to {effective}.', UserWarning, stacklevel=3)
|
|
52
|
+
return effective
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _result(w, corrected, labels, epsilon, is_log, round_counts):
|
|
56
|
+
if not is_log:
|
|
57
|
+
with np.errstate(over='raise'):
|
|
58
|
+
corrected = np.exp(corrected) - epsilon
|
|
59
|
+
if round_counts:
|
|
60
|
+
corrected = np.maximum(np.rint(corrected), 0)
|
|
61
|
+
corrected = corrected.T
|
|
62
|
+
if labels is not None:
|
|
63
|
+
corrected = pd.DataFrame(corrected, index=labels.index, columns=labels.columns)
|
|
64
|
+
w = pd.DataFrame(w, index=labels.columns, columns=[f'W_{i+1}' for i in range(w.shape[1])])
|
|
65
|
+
return {'W': w, 'normalized_counts': corrected, 'k_used': w.shape[1]}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def ruvr(x, controls, k, residuals, *, center=True, round_counts=True,
|
|
69
|
+
epsilon=1., tolerance=1e-8, is_log=False):
|
|
70
|
+
"""Estimate unwanted factors from caller-supplied genes-by-samples residuals.
|
|
71
|
+
|
|
72
|
+
Residuals should come from an appropriate first-pass biological model; this
|
|
73
|
+
function does not fit that model. Use returned W with original counts in DE.
|
|
74
|
+
"""
|
|
75
|
+
y, idx, labels = _prepare(x, controls, k, epsilon, tolerance, is_log)
|
|
76
|
+
if isinstance(residuals, pd.DataFrame) and labels is not None:
|
|
77
|
+
if not residuals.index.equals(labels.index) or not residuals.columns.equals(labels.columns):
|
|
78
|
+
raise ValueError('Residual labels must match expression axes in order.')
|
|
79
|
+
e = np.asarray(residuals, dtype=float).T
|
|
80
|
+
if e.shape != y.shape or not np.isfinite(e).all():
|
|
81
|
+
raise ValueError('Residuals must be finite and have the same shape as x.')
|
|
82
|
+
if center:
|
|
83
|
+
e = e - e.mean(axis=0)
|
|
84
|
+
u, d, _ = np.linalg.svd(e[:, idx], full_matrices=False)
|
|
85
|
+
w = u[:, :_rank(d, k, tolerance)]
|
|
86
|
+
return _result(w, y - w @ (w.T @ y), labels, epsilon, is_log, round_counts)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def make_groups(groups):
|
|
90
|
+
"""Return zero-based replicate indices, padded with -1 (R uses one-based)."""
|
|
91
|
+
groups = np.asarray(groups)
|
|
92
|
+
if groups.ndim != 1 or not len(groups) or pd.isna(groups).any():
|
|
93
|
+
raise ValueError('groups must contain one nonmissing label per sample.')
|
|
94
|
+
rows = [np.flatnonzero(groups == value) for value in sorted(pd.unique(groups))]
|
|
95
|
+
result = np.full((len(rows), max(map(len, rows))), -1, dtype=int)
|
|
96
|
+
for i, row in enumerate(rows):
|
|
97
|
+
result[i, :len(row)] = row
|
|
98
|
+
return result
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def ruvs(x, controls, k, replicate_indices, *, round_counts=True,
|
|
102
|
+
epsilon=1., tolerance=1e-8, is_log=False):
|
|
103
|
+
"""Estimate factors using replicate groups with constant biological signal.
|
|
104
|
+
|
|
105
|
+
replicate_indices is a matrix of zero-based sample positions padded with
|
|
106
|
+
-1, as returned by make_groups. Singleton groups do not estimate variation.
|
|
107
|
+
"""
|
|
108
|
+
y, idx, labels = _prepare(x, controls, k, epsilon, tolerance, is_log)
|
|
109
|
+
groups = np.asarray(replicate_indices)
|
|
110
|
+
if groups.ndim != 2 or groups.dtype.kind not in 'iu' or np.any(groups < -1) or np.any(groups >= len(y)):
|
|
111
|
+
raise ValueError('replicate_indices must contain valid zero-based positions or -1.')
|
|
112
|
+
centered = []
|
|
113
|
+
for row in groups:
|
|
114
|
+
row = row[row >= 0]
|
|
115
|
+
if len(np.unique(row)) != len(row):
|
|
116
|
+
raise ValueError('Replicate rows cannot repeat a sample.')
|
|
117
|
+
if len(row) >= 2:
|
|
118
|
+
centered.extend(y[row] - y[row].mean(axis=0))
|
|
119
|
+
if not centered:
|
|
120
|
+
raise ValueError('At least one replicate group needs two samples.')
|
|
121
|
+
yc = np.asarray(centered)
|
|
122
|
+
yc = yc[yc.sum(axis=1) != 0] # preserve upstream row filtering semantics
|
|
123
|
+
if not len(yc):
|
|
124
|
+
raise ValueError('Replicates provide no variation.')
|
|
125
|
+
_, d, vt = np.linalg.svd(yc, full_matrices=False)
|
|
126
|
+
effective = _rank(d, k, tolerance)
|
|
127
|
+
alpha = d[:effective, None] * vt[:effective]
|
|
128
|
+
ac = alpha[:, idx]
|
|
129
|
+
if np.linalg.matrix_rank(ac) < effective:
|
|
130
|
+
raise ValueError('Control loadings cannot identify all requested factors.')
|
|
131
|
+
w = y[:, idx] @ np.linalg.solve(ac @ ac.T, ac).T
|
|
132
|
+
return _result(w, y - w @ alpha, labels, epsilon, is_log, round_counts)
|
pyruvseq/_ruvg.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""Control-gene removal of unwanted variation (RUVg matrix method).
|
|
2
|
+
|
|
3
|
+
Reference: Risso et al., Nature Biotechnology (2014), doi:10.1038/nbt.2931.
|
|
4
|
+
Numerical contract checked against drisso/RUVSeq, commit
|
|
5
|
+
f560e84eb493f0c2d72279612f9e4ca99493b434 (RUVSeq 1.33.1).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import warnings
|
|
11
|
+
from numbers import Integral
|
|
12
|
+
|
|
13
|
+
import numpy as np
|
|
14
|
+
import pandas as pd
|
|
15
|
+
from scipy import sparse
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def ruvg(expression, controls, k=1, *, drop=0, center=True,
|
|
20
|
+
round_counts=True, epsilon=1.0, tolerance=1e-8, is_log=False):
|
|
21
|
+
"""Estimate RUVg factors from a genes-by-samples expression matrix.
|
|
22
|
+
|
|
23
|
+
Parameters
|
|
24
|
+
----------
|
|
25
|
+
expression : pandas.DataFrame or two-dimensional array or scipy sparse matrix
|
|
26
|
+
Genes on rows and independent samples on columns. Counts must be finite,
|
|
27
|
+
nonnegative integers. With ``is_log=True``, supply already transformed
|
|
28
|
+
finite expression instead. Sparse input is explicitly densified: the
|
|
29
|
+
transformed/centered matrix and returned correction are dense.
|
|
30
|
+
controls : sequence
|
|
31
|
+
Negative control gene names for a DataFrame, zero-based row indices for
|
|
32
|
+
an array, or a boolean row mask. DataFrame controls are always labels.
|
|
33
|
+
Controls should not respond to the biological condition of interest.
|
|
34
|
+
k : int
|
|
35
|
+
Number of leading control-gene singular vectors considered, before
|
|
36
|
+
removing ``drop`` vectors. Must be less than the number of samples.
|
|
37
|
+
drop : int
|
|
38
|
+
Discard this many leading vectors; the output has at most k-drop factors.
|
|
39
|
+
center : bool
|
|
40
|
+
Center each transformed gene across samples before learning factors.
|
|
41
|
+
round_counts : bool
|
|
42
|
+
For count input, round reconstructed counts and clip negatives to zero.
|
|
43
|
+
If False, negative reconstructed values are retained, as in RUVSeq.
|
|
44
|
+
epsilon : float
|
|
45
|
+
Positive offset in the natural-log transformation log(counts + epsilon).
|
|
46
|
+
tolerance : float
|
|
47
|
+
Absolute singular-value threshold used to reduce k on deficient controls.
|
|
48
|
+
is_log : bool
|
|
49
|
+
Skip log transformation and return correction on the supplied scale.
|
|
50
|
+
|
|
51
|
+
Returns
|
|
52
|
+
-------
|
|
53
|
+
dict
|
|
54
|
+
``W``: samples-by-factors; ``normalized_counts``: genes-by-samples.
|
|
55
|
+
Both are DataFrames with preserved axes for DataFrame input, otherwise
|
|
56
|
+
ndarrays. ``k_used`` is the number of returned factors. Input is unchanged.
|
|
57
|
+
|
|
58
|
+
Notes
|
|
59
|
+
-----
|
|
60
|
+
For count-based DE, use W as covariates alongside the biological design and
|
|
61
|
+
the original counts. The reconstructed matrix is not a substitute for raw
|
|
62
|
+
counts in a count likelihood. Confounded biology and unwanted variation
|
|
63
|
+
cannot be separated by this procedure without suitable controls.
|
|
64
|
+
|
|
65
|
+
This is the RUVg matrix algorithm, not full RUVSeq (RUVr/RUVs, EDASeq objects,
|
|
66
|
+
or downstream DE). Unlike R's warning-and-continue behavior, fractional raw
|
|
67
|
+
counts and a control subspace with no retained factors are rejected.
|
|
68
|
+
Singular-vector signs are arbitrary; compare W @ W.T across implementations.
|
|
69
|
+
"""
|
|
70
|
+
frame = isinstance(expression, pd.DataFrame)
|
|
71
|
+
if frame and (not expression.index.is_unique or not expression.columns.is_unique):
|
|
72
|
+
raise ValueError("Gene and sample labels must be unique.")
|
|
73
|
+
values = expression.to_numpy() if frame else expression
|
|
74
|
+
values = values.toarray() if sparse.issparse(values) else np.asarray(values)
|
|
75
|
+
if (values.ndim != 2 or min(values.shape) < 1
|
|
76
|
+
or not np.issubdtype(values.dtype, np.number)
|
|
77
|
+
or np.iscomplexobj(values) or not np.isfinite(values).all()):
|
|
78
|
+
raise ValueError("expression must be a nonempty finite numeric matrix.")
|
|
79
|
+
for name, flag in (("center", center), ("round_counts", round_counts), ("is_log", is_log)):
|
|
80
|
+
if not isinstance(flag, (bool, np.bool_)):
|
|
81
|
+
raise ValueError(f"{name} must be boolean.")
|
|
82
|
+
if isinstance(k, (bool, np.bool_)) or not isinstance(k, Integral) or not 0 < k < values.shape[1]:
|
|
83
|
+
raise ValueError("k must be a positive integer smaller than the sample count.")
|
|
84
|
+
if isinstance(drop, (bool, np.bool_)) or not isinstance(drop, Integral) or not 0 <= drop < k:
|
|
85
|
+
raise ValueError("drop must be an integer with 0 <= drop < k.")
|
|
86
|
+
for name, value, positive in (("epsilon", epsilon, True), ("tolerance", tolerance, False)):
|
|
87
|
+
if (not np.isscalar(value) or isinstance(value, (str, bool))
|
|
88
|
+
or not np.isreal(value) or not np.isfinite(value)
|
|
89
|
+
or (value <= 0 if positive else value < 0)):
|
|
90
|
+
raise ValueError(f"{name} must be finite and {'positive' if positive else 'nonnegative'}.")
|
|
91
|
+
if not is_log and (np.any(values < 0) or
|
|
92
|
+
not np.all(np.abs(values - np.rint(values)) < np.sqrt(np.finfo(float).eps))):
|
|
93
|
+
raise ValueError("Supply nonnegative integer counts, or set is_log=True for transformed data.")
|
|
94
|
+
selected = np.asarray(controls)
|
|
95
|
+
if selected.ndim != 1 or selected.size == 0:
|
|
96
|
+
raise ValueError("controls must be a nonempty one-dimensional sequence.")
|
|
97
|
+
if selected.dtype.kind == "b":
|
|
98
|
+
if len(selected) != values.shape[0]:
|
|
99
|
+
raise ValueError("The control mask must have one entry per gene.")
|
|
100
|
+
indices = np.flatnonzero(selected)
|
|
101
|
+
elif frame:
|
|
102
|
+
indices = expression.index.get_indexer(selected)
|
|
103
|
+
if np.any(indices < 0):
|
|
104
|
+
raise ValueError("All control gene labels must occur in expression.index.")
|
|
105
|
+
else:
|
|
106
|
+
if selected.dtype.kind not in "iu":
|
|
107
|
+
raise ValueError("Array controls must be zero-based integer row indices or a boolean mask.")
|
|
108
|
+
indices = selected
|
|
109
|
+
if np.any(indices < 0) or np.any(indices >= values.shape[0]):
|
|
110
|
+
raise ValueError("Control indices are out of bounds.")
|
|
111
|
+
if not len(indices) or len(np.unique(indices)) != len(indices):
|
|
112
|
+
raise ValueError("Select at least one control gene without duplicates.")
|
|
113
|
+
|
|
114
|
+
transformed = values.astype(np.float64, copy=True).T
|
|
115
|
+
if not is_log:
|
|
116
|
+
transformed = np.log(transformed + epsilon)
|
|
117
|
+
control_matrix = transformed[:, indices]
|
|
118
|
+
if center:
|
|
119
|
+
control_matrix = control_matrix - control_matrix.mean(axis=0)
|
|
120
|
+
vectors, singular_values, _ = np.linalg.svd(control_matrix, full_matrices=False)
|
|
121
|
+
effective = min(k, int(np.count_nonzero(singular_values > tolerance)))
|
|
122
|
+
if effective <= drop:
|
|
123
|
+
raise ValueError("Control matrix rank leaves no factors after drop; revise controls, k or drop.")
|
|
124
|
+
if effective < k:
|
|
125
|
+
warnings.warn(f"Control matrix rank reduces k from {k} to {effective}.", UserWarning, stacklevel=2)
|
|
126
|
+
factors = vectors[:, drop:effective]
|
|
127
|
+
# Orthogonal projection is invariant to SVD sign flips and basis rotations.
|
|
128
|
+
corrected = transformed - factors @ (factors.T @ transformed)
|
|
129
|
+
if not is_log:
|
|
130
|
+
with np.errstate(over="raise", invalid="raise"):
|
|
131
|
+
corrected = np.exp(corrected) - epsilon
|
|
132
|
+
if round_counts:
|
|
133
|
+
corrected = np.maximum(np.rint(corrected), 0)
|
|
134
|
+
corrected = corrected.T
|
|
135
|
+
if frame:
|
|
136
|
+
factors = pd.DataFrame(factors, index=expression.columns,
|
|
137
|
+
columns=[f"W_{i + 1}" for i in range(factors.shape[1])])
|
|
138
|
+
corrected = pd.DataFrame(corrected, index=expression.index, columns=expression.columns)
|
|
139
|
+
return {"W": factors, "normalized_counts": corrected, "k_used": effective - drop}
|