napistu 0.2.5.dev7__py3-none-any.whl → 0.3.1.dev1__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.
Files changed (108) hide show
  1. napistu/__init__.py +1 -3
  2. napistu/__main__.py +126 -96
  3. napistu/constants.py +35 -41
  4. napistu/context/__init__.py +10 -0
  5. napistu/context/discretize.py +462 -0
  6. napistu/context/filtering.py +387 -0
  7. napistu/gcs/__init__.py +1 -1
  8. napistu/identifiers.py +74 -15
  9. napistu/indices.py +68 -0
  10. napistu/ingestion/__init__.py +1 -1
  11. napistu/ingestion/bigg.py +47 -62
  12. napistu/ingestion/constants.py +18 -133
  13. napistu/ingestion/gtex.py +113 -0
  14. napistu/ingestion/hpa.py +147 -0
  15. napistu/ingestion/sbml.py +0 -97
  16. napistu/ingestion/string.py +2 -2
  17. napistu/matching/__init__.py +10 -0
  18. napistu/matching/constants.py +18 -0
  19. napistu/matching/interactions.py +518 -0
  20. napistu/matching/mount.py +529 -0
  21. napistu/matching/species.py +510 -0
  22. napistu/mcp/__init__.py +7 -4
  23. napistu/mcp/__main__.py +128 -72
  24. napistu/mcp/client.py +16 -25
  25. napistu/mcp/codebase.py +201 -145
  26. napistu/mcp/component_base.py +170 -0
  27. napistu/mcp/config.py +223 -0
  28. napistu/mcp/constants.py +45 -2
  29. napistu/mcp/documentation.py +253 -136
  30. napistu/mcp/documentation_utils.py +13 -48
  31. napistu/mcp/execution.py +372 -305
  32. napistu/mcp/health.py +47 -65
  33. napistu/mcp/profiles.py +10 -6
  34. napistu/mcp/server.py +161 -80
  35. napistu/mcp/tutorials.py +139 -87
  36. napistu/modify/__init__.py +1 -1
  37. napistu/modify/gaps.py +1 -1
  38. napistu/network/__init__.py +1 -1
  39. napistu/network/constants.py +101 -34
  40. napistu/network/data_handling.py +388 -0
  41. napistu/network/ig_utils.py +351 -0
  42. napistu/network/napistu_graph_core.py +354 -0
  43. napistu/network/neighborhoods.py +40 -40
  44. napistu/network/net_create.py +373 -309
  45. napistu/network/net_propagation.py +47 -19
  46. napistu/network/{net_utils.py → ng_utils.py} +124 -272
  47. napistu/network/paths.py +67 -51
  48. napistu/network/precompute.py +11 -11
  49. napistu/ontologies/__init__.py +10 -0
  50. napistu/ontologies/constants.py +129 -0
  51. napistu/ontologies/dogma.py +243 -0
  52. napistu/ontologies/genodexito.py +649 -0
  53. napistu/ontologies/mygene.py +369 -0
  54. napistu/ontologies/renaming.py +198 -0
  55. napistu/rpy2/__init__.py +229 -86
  56. napistu/rpy2/callr.py +47 -77
  57. napistu/rpy2/constants.py +24 -23
  58. napistu/rpy2/rids.py +61 -648
  59. napistu/sbml_dfs_core.py +587 -222
  60. napistu/scverse/__init__.py +15 -0
  61. napistu/scverse/constants.py +28 -0
  62. napistu/scverse/loading.py +727 -0
  63. napistu/utils.py +118 -10
  64. {napistu-0.2.5.dev7.dist-info → napistu-0.3.1.dev1.dist-info}/METADATA +8 -3
  65. napistu-0.3.1.dev1.dist-info/RECORD +133 -0
  66. tests/conftest.py +22 -0
  67. tests/test_context_discretize.py +56 -0
  68. tests/test_context_filtering.py +267 -0
  69. tests/test_identifiers.py +100 -0
  70. tests/test_indices.py +65 -0
  71. tests/{test_edgelist.py → test_ingestion_napistu_edgelist.py} +2 -2
  72. tests/test_matching_interactions.py +108 -0
  73. tests/test_matching_mount.py +305 -0
  74. tests/test_matching_species.py +394 -0
  75. tests/test_mcp_config.py +193 -0
  76. tests/test_mcp_documentation_utils.py +12 -3
  77. tests/test_mcp_server.py +156 -19
  78. tests/test_network_data_handling.py +397 -0
  79. tests/test_network_ig_utils.py +23 -0
  80. tests/test_network_neighborhoods.py +19 -0
  81. tests/test_network_net_create.py +459 -0
  82. tests/test_network_ng_utils.py +30 -0
  83. tests/test_network_paths.py +56 -0
  84. tests/{test_precomputed_distances.py → test_network_precompute.py} +8 -6
  85. tests/test_ontologies_genodexito.py +58 -0
  86. tests/test_ontologies_mygene.py +39 -0
  87. tests/test_ontologies_renaming.py +110 -0
  88. tests/test_rpy2_callr.py +79 -0
  89. tests/test_rpy2_init.py +151 -0
  90. tests/test_sbml.py +0 -31
  91. tests/test_sbml_dfs_core.py +134 -10
  92. tests/test_scverse_loading.py +778 -0
  93. tests/test_set_coverage.py +2 -2
  94. tests/test_utils.py +121 -1
  95. napistu/mechanism_matching.py +0 -1353
  96. napistu/rpy2/netcontextr.py +0 -467
  97. napistu-0.2.5.dev7.dist-info/RECORD +0 -98
  98. tests/test_igraph.py +0 -367
  99. tests/test_mechanism_matching.py +0 -784
  100. tests/test_net_utils.py +0 -149
  101. tests/test_netcontextr.py +0 -105
  102. tests/test_rpy2.py +0 -61
  103. /napistu/ingestion/{cpr_edgelist.py → napistu_edgelist.py} +0 -0
  104. {napistu-0.2.5.dev7.dist-info → napistu-0.3.1.dev1.dist-info}/WHEEL +0 -0
  105. {napistu-0.2.5.dev7.dist-info → napistu-0.3.1.dev1.dist-info}/entry_points.txt +0 -0
  106. {napistu-0.2.5.dev7.dist-info → napistu-0.3.1.dev1.dist-info}/licenses/LICENSE +0 -0
  107. {napistu-0.2.5.dev7.dist-info → napistu-0.3.1.dev1.dist-info}/top_level.txt +0 -0
  108. /tests/{test_obo.py → test_ingestion_obo.py} +0 -0
napistu/rpy2/__init__.py CHANGED
@@ -1,127 +1,270 @@
1
+ """
2
+ napistu.rpy2
3
+ ============
4
+
5
+ This subpackage provides utilities for interacting with R and the rpy2 bridge, including:
6
+ - Checking rpy2 availability
7
+ - Importing and caching core and extended rpy2 modules
8
+ - Handling R session information and error reporting
9
+ - Decorators for requiring rpy2 and reporting R-related exceptions
10
+
11
+ All rpy2-related imports are performed lazily and cached, so that the package can be imported even if rpy2 is not installed.
12
+ """
13
+
1
14
  from __future__ import annotations
2
15
 
3
16
  import functools
4
17
  import logging
5
18
  import os
6
19
  import sys
20
+ from functools import lru_cache
21
+ from typing import Any, Callable, TYPE_CHECKING
22
+
23
+ if TYPE_CHECKING:
24
+ import rpy2.robjects.conversion
25
+ import rpy2.robjects
26
+ import rpy2.robjects.packages
27
+ import rpy2.robjects.pandas2ri
28
+ import rpy2.robjects.ListVector
29
+ import pyarrow
30
+ import rpy2_arrow.arrow
7
31
 
8
32
  logger = logging.getLogger(__name__)
9
33
 
10
- try:
11
- import rpy2 # noqa
12
34
 
13
- has_rpy2 = True
35
+ @lru_cache(maxsize=1)
36
+ def get_rpy2_availability() -> bool:
37
+ """
38
+ Check if rpy2 is available in the current environment.
39
+
40
+ Returns
41
+ -------
42
+ bool
43
+ True if rpy2 is importable, False otherwise.
44
+ """
45
+ try:
46
+ import rpy2 # noqa: F401 - needed for rpy2 availability check
47
+
48
+ return True
49
+ except ImportError:
50
+ return False
51
+ except Exception as e:
52
+ logger.debug(f"rpy2 initialization failed: {e}")
53
+ return False
54
+
55
+
56
+ @lru_cache(maxsize=1)
57
+ def get_rpy2_core_modules() -> tuple[
58
+ "rpy2.robjects.conversion.Converter",
59
+ "rpy2.robjects.conversion.Converter",
60
+ "rpy2.robjects.packages.importr",
61
+ ]:
62
+ """
63
+ Import and cache core rpy2 modules (conversion, default_converter, importr).
64
+
65
+ Returns
66
+ -------
67
+ tuple
68
+ (conversion, default_converter, importr) from rpy2.robjects
69
+
70
+ Raises
71
+ ------
72
+ ImportError
73
+ If rpy2 is not available or import fails.
74
+ """
75
+ if not get_rpy2_availability():
76
+ raise ImportError(
77
+ "This function requires `rpy2`. "
78
+ "Please install `napistu` with the `rpy2` extra dependencies. "
79
+ "For example: `pip install napistu[rpy2]`"
80
+ )
81
+ try:
82
+ from rpy2.robjects import conversion, default_converter
83
+ from rpy2.robjects.packages import importr
84
+
85
+ return conversion, default_converter, importr
86
+ except Exception as e:
87
+ logger.error(f"Failed to import core rpy2 modules: {e}")
88
+ raise
89
+
90
+
91
+ @lru_cache(maxsize=1)
92
+ def get_rpy2_extended_modules() -> tuple[
93
+ "rpy2.robjects.pandas2ri",
94
+ "pyarrow",
95
+ "rpy2_arrow.arrow",
96
+ "rpy2.robjects",
97
+ "rpy2.robjects.ListVector",
98
+ ]:
99
+ """
100
+ Import and cache extended rpy2 modules (pandas2ri, pyarrow, rpy2_arrow, etc.).
101
+
102
+ Returns
103
+ -------
104
+ tuple
105
+ (pandas2ri, pyarrow, rpy2_arrow.arrow, ro, ListVector)
106
+
107
+ Raises
108
+ ------
109
+ ImportError
110
+ If rpy2 or dependencies are not available or import fails.
111
+ """
112
+ if not get_rpy2_availability():
113
+ raise ImportError(
114
+ "This function requires `rpy2`. "
115
+ "Please install `napistu` with the `rpy2` extra dependencies. "
116
+ "For example: `pip install napistu[rpy2]`"
117
+ )
118
+ try:
119
+ from rpy2.robjects import pandas2ri
120
+ import pyarrow
121
+
122
+ # loading rpy2_arrow checks whether the R arrow package is found
123
+ # this is the first time when a non-standard R package is loaded
124
+ # so a bad R setup can cause issues at this stage
125
+ try:
126
+ import rpy2_arrow.arrow as pyra
127
+ except Exception as e:
128
+ rsession_info()
129
+ raise e
130
+ import rpy2.robjects.conversion # noqa: F401 - needed for R conversion setup
131
+ import rpy2.rinterface # noqa: F401 - needed for R interface initialization
132
+ import rpy2.robjects as ro
133
+ from rpy2.robjects import ListVector
134
+
135
+ return pandas2ri, pyarrow, pyra, ro, ListVector
136
+ except Exception as e:
137
+ logger.error(f"Failed to import extended rpy2 modules: {e}")
138
+ raise
139
+
140
+
141
+ @lru_cache(maxsize=1)
142
+ def get_napistu_r_package() -> Any:
143
+ """
144
+ Import and cache the napistu R package using rpy2.
145
+
146
+ Returns
147
+ -------
148
+ Any
149
+ The imported napistu.r R package object.
150
+
151
+ Raises
152
+ ------
153
+ ImportError
154
+ If rpy2 or the napistu R package is not available.
155
+ """
156
+ conversion, default_converter, importr = get_rpy2_core_modules()
157
+ try:
158
+ napistu_r = importr("napistu.r")
159
+ return napistu_r
160
+ except Exception as e:
161
+ logger.error(f"Failed to import napistu.r R package: {e}")
162
+ raise
14
163
 
15
- from rpy2.robjects import conversion, default_converter # noqa
16
- from rpy2.robjects.packages import importr # noqa
17
164
 
18
- except ImportError:
19
- has_rpy2 = False
20
- logger.warning(
21
- "rpy2 is not installed. "
22
- "Some functions will not work. "
23
- "Consider installing `cpr[rpy2]`."
24
- )
25
- except Exception as e:
26
- has_rpy2 = False
27
- print(e)
28
- logger.warning("rpy2 initialization failed with an unrecognized exception.")
165
+ def require_rpy2(func: Callable) -> Callable:
166
+ """
167
+ Decorator to ensure rpy2 is available before calling the decorated function.
29
168
 
169
+ Raises
170
+ ------
171
+ ImportError
172
+ If rpy2 is not available.
173
+ """
30
174
 
31
- def warn_if_no_rpy2(func):
32
175
  @functools.wraps(func)
33
- def warn_if_no_rpy2_wrapper(*args, **kwargs):
34
- if not has_rpy2:
176
+ def wrapper(*args, **kwargs):
177
+ if not get_rpy2_availability():
35
178
  raise ImportError(
36
- "This function requires `rpy2`. \n"
37
- "Please install `cpr` with the `rpy2` extra dependencies. \n"
38
- "For example: `pip install cpr[rpy2]`\n"
179
+ f"Function '{func.__name__}' requires `rpy2`. "
180
+ "Please install `napistu` with the `rpy2` extra dependencies. "
181
+ "For example: `pip install napistu[rpy2]`"
39
182
  )
40
183
  return func(*args, **kwargs)
41
184
 
42
- return warn_if_no_rpy2_wrapper
185
+ return wrapper
186
+
187
+
188
+ def report_r_exceptions(func: Callable) -> Callable:
189
+ """
190
+ Decorator to provide helpful error reporting for R-related exceptions.
191
+
192
+ If an exception occurs, logs the error and prints R session info.
193
+ """
194
+
195
+ @functools.wraps(func)
196
+ def wrapper(*args, **kwargs):
197
+ if not get_rpy2_availability():
198
+ raise ImportError(
199
+ f"Function '{func.__name__}' requires `rpy2`. "
200
+ "Please install `napistu` with the `rpy2` extra dependencies. "
201
+ "For example: `pip install napistu[rpy2]`"
202
+ )
203
+ try:
204
+ return func(*args, **kwargs)
205
+ except Exception as e:
206
+ logger.error(f"Exception in {func.__name__}: {e}")
207
+ rsession_info()
208
+ raise e
209
+
210
+ return wrapper
43
211
 
44
212
 
45
213
  def rsession_info() -> None:
46
- # report summaries of the R installation found by rpy2
47
- # default converters bundled with rpy2 are used
48
- # for this step rather than those bundled with rpy2_arrow
49
- # because rpy2_arrow requires the arrow R package so
50
- # it can be difficult to import this package without
51
- # a valid R setup.
52
-
53
- with conversion.localconverter(default_converter):
54
- base = importr("base")
55
- utils = importr("utils")
56
-
57
- lib_paths = base._libPaths()
58
- session_info = utils.sessionInfo()
59
-
60
- logger.warning(
61
- "An exception occurred when running some rpy2-related functionality\n"
62
- "Here is a summary of your R session\n"
63
- f"Using R version in {base.R_home()[0]}\n"
64
- ".libPaths ="
65
- )
66
- logger.warning("\n".join(lib_paths))
67
- logger.warning(f"sessionInfo = {session_info}")
68
- # suggest a fix
69
- logger.warning(_r_homer_warning())
214
+ """
215
+ Report summaries of the R installation found by rpy2.
70
216
 
71
- return None
217
+ This function logs the R version, library paths, and session info using rpy2.
218
+ If R is not available or an error occurs, logs a warning.
219
+ """
220
+ try:
221
+ conversion, default_converter, importr = get_rpy2_core_modules()
222
+ with conversion.localconverter(default_converter):
223
+ base = importr("base")
224
+ utils = importr("utils")
225
+ lib_paths = base._libPaths()
226
+ session_info = utils.sessionInfo()
227
+ logger.warning(
228
+ "An exception occurred when running some rpy2-related functionality\n"
229
+ "Here is a summary of your R session\n"
230
+ f"Using R version in {base.R_home()[0]}\n"
231
+ ".libPaths ="
232
+ )
233
+ logger.warning("\n".join(lib_paths))
234
+ logger.warning(f"sessionInfo = {session_info}")
235
+ logger.warning(_r_homer_warning())
236
+ except Exception as e:
237
+ logger.warning(f"Could not generate R session info: {e}")
72
238
 
73
239
 
74
- def _r_homer_warning() -> None:
75
- # utility function to suggest installation directions for R
76
- # as part of rsession
240
+ def _r_homer_warning() -> str:
241
+ """
242
+ Utility function to suggest installation directions for R based on environment.
77
243
 
244
+ Returns
245
+ -------
246
+ str
247
+ Installation instructions for R in the current environment.
248
+ """
78
249
  is_conda = os.path.exists(os.path.join(sys.prefix, "conda-meta"))
79
250
  if is_conda:
80
251
  r_lib_path = os.path.join(sys.prefix, "lib", "R")
81
252
  if os.path.isdir(r_lib_path):
82
- logging.warning(
253
+ return (
83
254
  "You seem to be working in a conda environment with R installed.\n"
84
- "If this version was not located by rpy2 then then try to set R_HOME using:\n"
85
- f"os.environ['R_HOME'] = {r_lib_path}"
255
+ "If this version was not located by rpy2 then try to set R_HOME using:\n"
256
+ f"os.environ['R_HOME'] = '{r_lib_path}'"
86
257
  )
87
258
  else:
88
- logging.warning(
259
+ return (
89
260
  "You seem to be working in a conda environment but R is NOT installed.\n"
90
261
  "If this is the case then install R, the CPR R package and the R arrow package into your\n"
91
262
  "conda environment and then set the R_HOME environmental variable using:\n"
92
- "os.environ['R_HOME'] = <<PATH_TO_R_lib/R>>"
263
+ "os.environ['R_HOME'] = '<<PATH_TO_R_lib/R>>'"
93
264
  )
94
265
  else:
95
- logging.warning(
266
+ return (
96
267
  "If you don't have R installed or if your desired R library does not match the\n"
97
268
  "one above, then set your R_HOME environmental variable using:\n"
98
- "os.environ['R_HOME'] = <<PATH_TO_lib/R>>"
269
+ "os.environ['R_HOME'] = '<<PATH_TO_lib/R>>'"
99
270
  )
100
-
101
- return None
102
-
103
-
104
- def report_r_exceptions(function):
105
- @functools.wraps(function)
106
- def report_r_exceptions_wrapper(*args, **kwargs):
107
- if not has_rpy2:
108
- raise ImportError(
109
- "This function requires `rpy2`. \n"
110
- "Please install `cpr` with the `rpy2` extra dependencies. \n"
111
- "For example: `pip install cpr[rpy2]`\n"
112
- )
113
- try:
114
- return function(*args, **kwargs)
115
- except Exception as e:
116
- # log the exception
117
- err = "There was an exception in "
118
- err += function.__name__
119
-
120
- logger.warning(err)
121
- # report session info
122
- rsession_info()
123
-
124
- # re-raise the exception
125
- raise e
126
-
127
- return report_r_exceptions_wrapper
napistu/rpy2/callr.py CHANGED
@@ -1,115 +1,82 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import pandas as pd
4
- from napistu.rpy2 import has_rpy2
5
- from napistu.rpy2 import report_r_exceptions
6
- from napistu.rpy2 import rsession_info
7
- from napistu.rpy2 import warn_if_no_rpy2
8
-
9
- if has_rpy2:
10
- from rpy2.robjects import pandas2ri
11
- from rpy2.robjects.packages import importr
12
- from rpy2.robjects.packages import InstalledSTPackage, InstalledPackage
13
- import pyarrow
14
-
15
- # loading rpy2_arrow checks whether the R arrow package is found
16
- # this is the first time when a non-standard R package is loaded
17
- # so a bad R setup can cause issues at this stage
18
- # rsession_info() adds some helpful debugging information
19
- try:
20
- import rpy2_arrow.arrow as pyra
21
- except Exception as e:
22
- rsession_info()
23
- raise e
24
- import rpy2.robjects.conversion
25
- import rpy2.rinterface
26
- import rpy2.robjects as ro
27
-
28
-
29
- @warn_if_no_rpy2
4
+ from napistu.rpy2 import (
5
+ get_rpy2_core_modules,
6
+ get_rpy2_extended_modules,
7
+ get_napistu_r_package,
8
+ require_rpy2,
9
+ report_r_exceptions,
10
+ )
11
+
12
+
13
+ @require_rpy2
30
14
  @report_r_exceptions
31
- def get_rcpr(
32
- r_paths: list[str] | None = None,
33
- ):
15
+ def get_napistu_r(r_paths: list[str] | None = None):
34
16
  """
35
- Get rcpr
36
-
37
- Gets the rcpr R package
17
+ Get Napistu R package.
38
18
 
39
19
  Args:
40
- r_paths (list[str]):
41
- Paths to add to .libPaths() in R
20
+ r_paths (list[str], optional): Paths to add to .libPaths() in R
42
21
 
43
22
  Returns:
44
- rcpr R package
23
+ napistu.r R package
45
24
  """
46
-
47
25
  _ = get_rbase(r_paths)
48
-
49
- # connect the cpr R package
50
- rcpr = importr("rcpr")
51
- return rcpr
26
+ return get_napistu_r_package()
52
27
 
53
28
 
54
- @warn_if_no_rpy2
29
+ @require_rpy2
55
30
  @report_r_exceptions
56
31
  def bioconductor_org_r_function(
57
32
  object_type: str, species: str, r_paths: list[str] | None = None
58
33
  ):
59
34
  """
60
- Bioconuctor Organism R Function
35
+ Bioconductor Organism R Function
61
36
 
62
37
  Calls "bioconductor_org_function" from the R cpr package to pull a mapping object
63
38
  out of a species specific library.
64
39
 
65
40
  Parameters:
66
- object_type (str):
67
- Type of function to call
68
- species (str):
69
- Species name
70
- r_paths: list(str):
71
- Paths to add to .libPaths() in R. Alternatively consider setting the R_HOME env variable.
41
+ object_type (str): Type of function to call
42
+ species (str): Species name
43
+ r_paths (list[str], optional): Paths to add to .libPaths() in R.
44
+ Alternatively consider setting the R_HOME env variable.
72
45
 
73
46
  Returns:
74
47
  pd.DataFrame or a function for non-tabular results
75
48
  """
76
-
77
49
  _ = get_rbase(r_paths)
78
-
79
- # connect the cpr R package
80
- cpr = importr("rcpr")
81
-
82
- results = cpr.bioconductor_org_function(object_type, species)
83
-
50
+ napistu_r = get_napistu_r_package()
51
+ results = napistu_r.bioconductor_org_function(object_type, species)
84
52
  return results
85
53
 
86
54
 
55
+ @require_rpy2
87
56
  @report_r_exceptions
88
- def get_rbase(
89
- r_paths: list[str] | None = None,
90
- ) -> InstalledSTPackage | InstalledPackage:
91
- """Get the base R package
57
+ def get_rbase(r_paths: list[str] | None = None):
58
+ """Get the base R package.
92
59
 
93
60
  Args:
94
- r_paths (list[str], optional): Optional additional
95
- r_paths. Defaults to None.
61
+ r_paths (list[str], optional): Optional additional r_paths. Defaults to None.
96
62
 
97
63
  Returns:
98
- _type_: _description_
64
+ Base R package
99
65
  """
66
+ conversion, default_converter, importr = get_rpy2_core_modules()
100
67
  base = importr("base")
101
68
  if r_paths is not None:
102
69
  base._libPaths(r_paths)
103
70
  return base
104
71
 
105
72
 
106
- @warn_if_no_rpy2
73
+ @require_rpy2
107
74
  @report_r_exceptions
108
- def pandas_to_r_dataframe(df: pd.DataFrame) -> rpy2.robjects.DataFrame:
109
- """Convert a pandas dataframe to an R dataframe
75
+ def pandas_to_r_dataframe(df: pd.DataFrame):
76
+ """Convert a pandas dataframe to an R dataframe.
110
77
 
111
- This uses the rpy2-arrow functionality
112
- to increase the performance of conversion orders of magnitude.
78
+ This uses the rpy2-arrow functionality to increase the performance
79
+ of conversion by orders of magnitude.
113
80
 
114
81
  Args:
115
82
  df (pd.DataFrame): Pandas dataframe
@@ -117,16 +84,18 @@ def pandas_to_r_dataframe(df: pd.DataFrame) -> rpy2.robjects.DataFrame:
117
84
  Returns:
118
85
  rpy2.robjects.DataFrame: R dataframe
119
86
  """
87
+ pandas2ri, pyarrow, pyra, ro, ListVector = get_rpy2_extended_modules()
88
+
120
89
  conv = _get_py2rpy_pandas_conv()
121
90
  with (ro.default_converter + conv).context():
122
91
  r_df = ro.conversion.get_conversion().py2rpy(df)
123
92
  return r_df
124
93
 
125
94
 
126
- @warn_if_no_rpy2
95
+ @require_rpy2
127
96
  @report_r_exceptions
128
- def r_dataframe_to_pandas(rdf: rpy2.robjects.DataFrame) -> pd.DataFrame:
129
- """Convert an R dataframe to a pandas dataframe
97
+ def r_dataframe_to_pandas(rdf):
98
+ """Convert an R dataframe to a pandas dataframe.
130
99
 
131
100
  Args:
132
101
  rdf (rpy2.robjects.DataFrame): R dataframe
@@ -134,28 +103,29 @@ def r_dataframe_to_pandas(rdf: rpy2.robjects.DataFrame) -> pd.DataFrame:
134
103
  Returns:
135
104
  pd.DataFrame: Pandas dataframe
136
105
  """
106
+ pandas2ri, pyarrow, pyra, ro, ListVector = get_rpy2_extended_modules()
107
+
137
108
  with (ro.default_converter + pandas2ri.converter).context():
138
109
  df = ro.conversion.get_conversion().rpy2py(rdf)
139
110
  return df
140
111
 
141
112
 
142
- @warn_if_no_rpy2
113
+ @require_rpy2
143
114
  @report_r_exceptions
144
115
  def _get_py2rpy_pandas_conv():
145
- """Get the py2rpy arrow converter for pandas
116
+ """Get the py2rpy arrow converter for pandas.
146
117
 
147
- This is a high-performance converter using
148
- the rpy2-arrow functionality:
118
+ This is a high-performance converter using the rpy2-arrow functionality:
149
119
  https://rpy2.github.io/rpy2-arrow/version/main/html/index.html
150
120
 
151
121
  Returns:
152
122
  Callable: The converter function
153
123
  """
124
+ pandas2ri, pyarrow, pyra, ro, ListVector = get_rpy2_extended_modules()
125
+
154
126
  base = get_rbase()
155
127
  # We use the converter included in rpy2-arrow as template.
156
- conv = rpy2.robjects.conversion.Converter(
157
- "Pandas to data.frame", template=pyra.converter
158
- )
128
+ conv = ro.conversion.Converter("Pandas to data.frame", template=pyra.converter)
159
129
 
160
130
  @conv.py2rpy.register(pd.DataFrame)
161
131
  def py2rpy_pandas(dataf):
napistu/rpy2/constants.py CHANGED
@@ -21,29 +21,6 @@ BIOC_VALID_EXPANDED_SPECIES_ONTOLOGIES = {
21
21
  ONTOLOGIES.SYMBOL,
22
22
  }
23
23
 
24
- # bioc ontologies used for linking systematic identifiers
25
- # (entrez is not part of this list because it forms the gene index)
26
- BIOC_DOGMATIC_MAPPING_ONTOLOGIES = {
27
- ONTOLOGIES.ENSEMBL_GENE,
28
- ONTOLOGIES.ENSEMBL_TRANSCRIPT,
29
- ONTOLOGIES.ENSEMBL_PROTEIN,
30
- ONTOLOGIES.UNIPROT,
31
- ONTOLOGIES.GENE_NAME,
32
- ONTOLOGIES.SYMBOL,
33
- }
34
- BIOC_PROTEIN_ONTOLOGIES = [ONTOLOGIES.UNIPROT, ONTOLOGIES.ENSEMBL_PROTEIN]
35
- BIOC_GENE_ONTOLOGIES = [
36
- ONTOLOGIES.NCBI_ENTREZ_GENE,
37
- ONTOLOGIES.ENSEMBL_GENE,
38
- ONTOLOGIES.ENSEMBL_TRANSCRIPT,
39
- ]
40
- BIOC_NAME_ONTOLOGIES = {
41
- ONTOLOGIES.GENE_NAME: 0,
42
- ONTOLOGIES.SYMBOL: 1,
43
- ONTOLOGIES.UNIPROT: 2,
44
- ONTOLOGIES.ENSEMBL_PROTEIN: 3,
45
- }
46
-
47
24
  # prefixes for bioconductor mapping tables
48
25
  BIOC_NOMENCLATURE = SimpleNamespace(
49
26
  CHR_TBL="CHR",
@@ -63,6 +40,30 @@ BIOC_NOMENCLATURE = SimpleNamespace(
63
40
  SYMBOL="symbol",
64
41
  )
65
42
 
43
+ # Map ontology to bioconductor table name and column name
44
+ BIOC_ONTOLOGY_MAPPING = {
45
+ ONTOLOGIES.NCBI_ENTREZ_GENE: (
46
+ BIOC_NOMENCLATURE.CHR_TBL,
47
+ BIOC_NOMENCLATURE.NCBI_ENTREZ_GENE,
48
+ ),
49
+ ONTOLOGIES.ENSEMBL_GENE: (
50
+ BIOC_NOMENCLATURE.ENSG_TBL,
51
+ BIOC_NOMENCLATURE.ENSEMBL_GENE,
52
+ ),
53
+ ONTOLOGIES.ENSEMBL_TRANSCRIPT: (
54
+ BIOC_NOMENCLATURE.ENST_TBL,
55
+ BIOC_NOMENCLATURE.ENSEMBL_TRANSCRIPT,
56
+ ),
57
+ ONTOLOGIES.ENSEMBL_PROTEIN: (
58
+ BIOC_NOMENCLATURE.ENSP_TBL,
59
+ BIOC_NOMENCLATURE.ENSEMBL_PROTEIN,
60
+ ),
61
+ ONTOLOGIES.UNIPROT: (BIOC_NOMENCLATURE.UNIPROT_TBL, BIOC_NOMENCLATURE.UNIPROT),
62
+ ONTOLOGIES.GENE_NAME: (BIOC_NOMENCLATURE.NAME_TBL, BIOC_NOMENCLATURE.GENE_NAME),
63
+ ONTOLOGIES.SYMBOL: (BIOC_NOMENCLATURE.SYMBOL_TBL, BIOC_NOMENCLATURE.SYMBOL),
64
+ }
65
+
66
+
66
67
  # netcontextr constants
67
68
 
68
69
  COL_GENE = "gene"