The `AMR` package for R is an incredible tool for antimicrobial resistance (AMR) data analysis, providing extensive functionality for working with microbial and antimicrobial properties. But what if you're working in Python and still want to benefit from the robust features of `AMR`?
The best way is to access R directly from Python with the help of `rpy2`, a simple yet powerful Python package. You can easily call functions from the `AMR` package to process your own data in your own Python environment. This post will guide you through setting up `rpy2` and show you how to use R functions from `AMR` in Python to supercharge your antimicrobial resistance analysis.
`rpy2` is a Python library that allows Python users to run R code within their Python scripts. Essentially, it acts as a bridge between the two languages, allowing you to tap into the rich ecosystem of R libraries (like `AMR`) while maintaining the flexibility of Python.
## Key Features of `rpy2`:
- Seamlessly call R functions from Python.
- Convert R data structures into Python data structures like pandas DataFrames.
- Leverage the full power of R libraries without leaving your Python environment.
# Setting Up `rpy2`
Before diving into the examples, you’ll need to install both R and `rpy2`. Here's a step-by-step guide on setting things up.
To ensure everything is set up correctly, you can test your installation by running the following Python script, which essentially runs R in the background:
In this example, a Python dataset with microorganism names like *E. coli* and *S. aureus* is passed to the R function `mo_name()`. The result is an updated `DataFrame` that includes the standardised microorganism names based on the `mo_name()` function from the `AMR` package.
## Example 2: Generating an Antibiogram
One of the core functions of the `AMR` package is generating an antibiogram, a table that summarises the antimicrobial susceptibility of bacterial isolates. Here’s how you can generate an antibiogram from Python:
In this example, we generate an antibiogram by selecting aminoglycosides and carbapenems, two classes of antibiotics, and then convert the resulting R data frame into a Python-readable format.
## Example 3: Filtering Data Based on Gram-Negative Bacteria
Let’s say you want to filter the dataset for Gram-negative bacteria and display their resistance to certain antibiotics:
```python
# Filter for Gram-negative bacteria with intrinsic resistance to cefotaxime
This example uses the AMR functions `mo_is_gram_negative()` and `mo_is_intrinsic_resistant()` to filter the dataset and returns a subset of bacteria with resistance data.
## Example 4: Customising the Antibiogram
You can easily customise the antibiogram by passing different antibiotics or microorganism transformations, as shown below:
```python
# Customise the antibiogram with different settings
Here, we use piperacillin/tazobactam (TZP) in combination with tobramycin (TOB) and gentamicin (GEN) to see how they perform against various Gram-negative bacteria.
# Conclusion
Using `rpy2`, you can easily integrate the power of R's `AMR` package into your Python workflows. Whether you are generating antibiograms, analyzing resistance data, or performing complex filtering, `rpy2` gives you the flexibility to run R code without leaving the Python environment. This makes it a perfect solution for teams working across both R and Python.