1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-26 20:06:11 +01:00

Compare commits

..

2 Commits

7 changed files with 251 additions and 239 deletions

View File

@ -49,6 +49,7 @@ if command -v Rscript > /dev/null; then
if [ "$(Rscript -e 'cat(all(c('"'pkgload'"', '"'devtools'"', '"'dplyr'"') %in% rownames(installed.packages())))')" = "TRUE" ]; then
Rscript -e "source('data-raw/_pre_commit_checks.R')"
currentpkg=$(Rscript -e "cat(pkgload::pkg_name())")
bash data-raw/AMRforRGPT.sh
echo "- Adding changed files in ./data-raw and ./man to this commit"
git add data-raw/*
git add man/*

View File

@ -1,6 +1,6 @@
Package: AMR
Version: 2.1.1.9095
Date: 2024-10-15
Version: 2.1.1.9099
Date: 2024-10-17
Title: Antimicrobial Resistance Data Analysis
Description: Functions to simplify and standardise antimicrobial resistance (AMR)
data analysis and to work with microbial and antimicrobial properties by

View File

@ -1,4 +1,4 @@
# AMR 2.1.1.9095
# AMR 2.1.1.9099
*(this beta version will eventually become v3.0. We're happy to reach a new major milestone soon, which will be all about the new One Health support! Install this beta using [the instructions here](https://msberends.github.io/AMR/#latest-development-version).)*

View File

@ -1,7 +1,13 @@
#!/bin/bash
# Check if the current directory is named 'AMR'
if [ "$(basename "$PWD")" != "AMR" ]; then
echo "Error: The script must be run from the 'AMR' directory."
exit 1
fi
# Define the output file, located in ./data-raw
output_file="gpt_training_text.txt"
output_file="data-raw/gpt_training_text.txt"
# Clear the output file if it exists
echo "This files contains all context you must know about the AMR package for R."> "$output_file"
@ -12,16 +18,16 @@ remove_header() {
sed '/# =\{6,\}/,/# =\{6,\}/d' "$1"
}
# Process all .R files in the '../R' folder
for file in ../R/*.R; do
# Process all .R files in the 'R' folder
for file in R/*.R; do
echo "THE NEXT PART CONTAINS CONTENTS FROM FILE $file" >> "$output_file"
echo -e "\n\n" >> "$output_file"
remove_header "$file" >> "$output_file"
echo -e "\n\n" >> "$output_file"
done
# Process all .Rmd files in the '../vignettes' folder
for file in ../vignettes/*.Rmd; do
# Process all .Rmd files in the 'vignettes' folder
for file in vignettes/*.Rmd; do
echo "THE NEXT PART CONTAINS CONTENTS FROM FILE $file" >> "$output_file"
echo -e "\n\n" >> "$output_file"
remove_header "$file" >> "$output_file"
@ -29,7 +35,7 @@ for file in ../vignettes/*.Rmd; do
done
# Process important metadata files (DESCRIPTION, NAMESPACE, index.md)
for file in ../DESCRIPTION ../NAMESPACE ../index.md; do
for file in DESCRIPTION NAMESPACE index.md; do
if [[ -f $file ]]; then
echo "THE NEXT PART CONTAINS CONTENTS FROM FILE $file" >> "$output_file"
echo -e "\n\n" >> "$output_file"
@ -38,16 +44,16 @@ for file in ../DESCRIPTION ../NAMESPACE ../index.md; do
fi
done
# Process test files (if available) in the '../tests' folder
for file in ../tests/*.R; do
# Process test files (if available) in the 'tests' folder
for file in tests/*.R; do
echo "THE NEXT PART CONTAINS CONTENTS FROM FILE $file" >> "$output_file"
echo -e "\n\n" >> "$output_file"
remove_header "$file" >> "$output_file"
echo -e "\n\n" >> "$output_file"
done
# Process all .Rd files from the '../man' folder
for file in ../man/*.Rd; do
# Process all .Rd files from the 'man' folder
for file in man/*.Rd; do
echo "THE NEXT PART CONTAINS CONTENTS FROM FILE $file" >> "$output_file"
echo -e "\n\n" >> "$output_file"
remove_header "$file" >> "$output_file"

View File

@ -50,14 +50,31 @@ from rpy2 import robjects
from rpy2.robjects import pandas2ri
from rpy2.robjects.packages import importr, isinstalled
import pandas as pd
# import importlib.metadata as metadata
# Check if the R package is installed
# Check if AMR package is installed in R
if not isinstalled('AMR'):
utils = importr('utils')
utils.install_packages('AMR')
utils.install_packages('AMR', repos='https://msberends.r-universe.dev')
# Python package version of AMR
python_amr_version = metadata.version('AMR')
# R package version of AMR
# r_amr_version = robjects.r('packageVersion("AMR")')[0]
# Compare R and Python package versions
# if r_amr_version != python_amr_version:
# print(f"{BLUE}AMR:{RESET} Version mismatch detected. Updating AMR R package version to {python_amr_version}...", flush=True)
# try:
# # Re-install the specific version of AMR in R
# utils = importr('utils')
# utils.install_packages('AMR', repos='https://msberends.r-universe.dev')
# except Exception as e:
# print(f"{BLUE}AMR:{RESET} Could not update: {e}{RESET}", flush=True)
# Activate the automatic conversion between R and pandas DataFrames
pandas2ri.activate()
# example_isolates
example_isolates = pandas2ri.rpy2py(robjects.r('''
df <- AMR::example_isolates
@ -68,6 +85,7 @@ df[] <- lapply(df, function(x) {
x
}
})
df <- df[, !sapply(df, is.list)]
df
'''))
example_isolates['date'] = pd.to_datetime(example_isolates['date'])
@ -75,7 +93,7 @@ example_isolates['date'] = pd.to_datetime(example_isolates['date'])
# microorganisms
microorganisms = pandas2ri.rpy2py(robjects.r('AMR::microorganisms[, !sapply(AMR::microorganisms, is.list)]'))
antibiotics = pandas2ri.rpy2py(robjects.r('AMR::antibiotics[, !sapply(AMR::antibiotics, is.list)]'))
clinical_breakpoints = pandas2ri.rpy2py(robjects.r('AMR::clinical_breakpoints'))
clinical_breakpoints = pandas2ri.rpy2py(robjects.r('AMR::clinical_breakpoints[, !sapply(AMR::clinical_breakpoints, is.list)]'))
print(f"{BLUE}AMR:{RESET} {GREEN}Done.{RESET}", flush=True)
EOL
@ -220,6 +238,7 @@ echo "Python wrapper functions generated in $functions_file."
echo "Python wrapper functions listed in $init_file."
cp ../vignettes/AMR_for_Python.Rmd python_wrapper/AMR/README.md
sed -i '1,/^# Introduction$/d' python_wrapper/AMR/README.md
echo "README copied"

File diff suppressed because it is too large Load Diff

View File

@ -24,17 +24,42 @@ knitr::opts_chunk$set(
# Introduction
The `AMR` package for R is a powerful tool for antimicrobial resistance (AMR) analysis. It provides extensive features for handling microbial and antimicrobial data. However, for those who work primarily in Python, we now have a more intuitive option available: the `AMR` Python package, which uses `rpy2` internally. This package allows Python users to access all the functions from the R `AMR` package without the need to set up `rpy2` themselves. Since this Python package is not a true 'port' (which would require all R functions to be rewritten into Python), R and the AMR R package are still required to be installed. Yet, Python users can now easily work with AMR data directly through Python code.
The `AMR` package for R is a powerful tool for antimicrobial resistance (AMR) analysis. It provides extensive features for handling microbial and antimicrobial data. However, for those who work primarily in Python, we now have a more intuitive option available: the [`AMR` Python Package Index](https://pypi.org/project/AMR/).
In this document, we explain how this works and provide simple examples of using the `AMR` Python package.
This Python package is a wrapper round the `AMR` R package. It uses the `rpy2` package internally. Despite the need to have R installed, Python users can now easily work with AMR data directly through Python code.
## How It Works
# Install
The `AMR` Python package acts as a wrapper around the functions in the `AMR` R package. The package simplifies the process of calling R functions in Python, eliminating the need to manually manage the `rpy2` setup, which Python uses internally to be able to work with the R package. By just using `import AMR`, Python users can directly use the functions from the `AMR` R package as if they were native Python functions.
1. First make sure you have R installed. There is **no need to install the `AMR` R package**, as it will be installed automatically.
Internally, `rpy2` is still being used, but all complexity is hidden from the user. This approach keeps the Python code clean and Pythonic, while still leveraging the full power of the R `AMR` package.
For Linux:
## Example of Usage
```bash
# Ubuntu / Debian
sudo apt install r-base
# Fedora:
sudo dnf install R
# CentOS/RHEL
sudo yum install R
```
For macOS (using [Homebrew](https://brew.sh)):
```bash
brew install r
```
For Windows, visit the [CRAN download page](https://cran.r-project.org) to download and install R.
2. Since the Python package is available on the official [Python Package Index](https://pypi.org/project/AMR/), you can just run:
```bash
pip install AMR
```
# Examples of Usage
## Cleaning Taxonomy
Heres an example that demonstrates how to clean microorganism and drug names using the `AMR` Python package:
@ -70,7 +95,8 @@ print(df)
* **ab_name**: Similarly, this function standardises antimicrobial names. The different representations of ciprofloxacin (e.g., "Cipro", "CIP", "J01MA02", and "Ciproxin") are all converted to the standard name, "Ciprofloxacin".
### Taxonomic Data Sets Now in Python!
## Taxonomic Data Sets Now in Python!
As a Python user, you might like that the most important data sets of the `AMR` R package, `microorganisms`, `antibiotics`, `clinical_breakpoints`, and `example_isolates`, are now available as regular Python data frames:
@ -111,42 +137,7 @@ AMR.antibiotics
| ZFD | NaN | Zoliflodacin | None | NaN | None | NaN | None |
# Installation
To be able to use the `AMR` Python package, it is required to install both R and the `AMR` R package.
### Preparation: Install R and `AMR` R package
For Linux and macOS, this is just:
```bash
# Ubuntu / Debian
sudo apt install r-base && Rscript -e 'install.packages("AMR")'
# Fedora:
sudo dnf install R && Rscript -e 'install.packages("AMR")'
# CentOS/RHEL
sudo yum install R && Rscript -e 'install.packages("AMR")'
# Arch Linux
sudo pacman -S r && Rscript -e 'install.packages("AMR")'
# macOS
brew install r && Rscript -e 'install.packages("AMR")'
```
For Windows, visit the [CRAN download page](https://cran.r-project.org) in install R, then afterwards install the 'AMR' package manually.
### Install `AMR` Python Package
Since the Python package is available on the official [Python Package Index](https://pypi.org/project/AMR/), you can just run:
```bash
pip install AMR
```
# Working with `AMR` in Python
Now that we have everything set up, lets walk through some practical examples of using the `AMR` package within Python.
## Example 1: Calculating AMR
## Calculating AMR
```python
import AMR
@ -161,7 +152,7 @@ print(result)
[0.59555556]
```
## Example 2: Generating Antibiograms
## Generating Antibiograms
One of the core functions of the `AMR` package is generating an antibiogram, a table that summarises the antimicrobial susceptibility of bacterial isolates. Heres how you can generate an antibiogram from Python:
@ -200,4 +191,6 @@ In this example, we generate an antibiogram by selecting various antibiotics.
With the `AMR` Python package, Python users can now effortlessly call R functions from the `AMR` R package. This eliminates the need for complex `rpy2` configurations and provides a clean, easy-to-use interface for antimicrobial resistance analysis. The examples provided above demonstrate how this can be applied to typical workflows, such as standardising microorganism and antimicrobial names or calculating resistance.
By using `import AMR`, you can seamlessly integrate the robust features of the R `AMR` package into your Python workflows. Whether you're cleaning data or analysing resistance patterns, the `AMR` Python package makes it easy to work with AMR data in Python.
By just running `import AMR`, users can seamlessly integrate the robust features of the R `AMR` package into Python workflows.
Whether you're cleaning data or analysing resistance patterns, the `AMR` Python package makes it easy to work with AMR data in Python.