1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-27 12:46:12 +01:00

Compare commits

..

No commits in common. "cfd31f0f0ce5c1f15d342145cc0f31d053d68632" and "40edc16fdfb135642d7452d2e1cce1bb667fd843" have entirely different histories.

7 changed files with 239 additions and 251 deletions

View File

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

View File

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

View File

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

View File

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

File diff suppressed because it is too large Load Diff

View File

@ -24,42 +24,17 @@ knitr::opts_chunk$set(
# Introduction # 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 Index](https://pypi.org/project/AMR/). 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.
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. In this document, we explain how this works and provide simple examples of using the `AMR` Python package.
# Install ## How It Works
1. First make sure you have R installed. There is **no need to install the `AMR` R package**, as it will be installed automatically. 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.
For Linux: 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.
```bash ## Example of Usage
# 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: Heres an example that demonstrates how to clean microorganism and drug names using the `AMR` Python package:
@ -95,8 +70,7 @@ 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". * **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: 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:
@ -137,7 +111,42 @@ AMR.antibiotics
| ZFD | NaN | Zoliflodacin | None | NaN | None | NaN | None | | ZFD | NaN | Zoliflodacin | None | NaN | None | NaN | None |
## Calculating AMR # 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
```python ```python
import AMR import AMR
@ -152,7 +161,7 @@ print(result)
[0.59555556] [0.59555556]
``` ```
## Generating Antibiograms ## Example 2: 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: 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:
@ -191,6 +200,4 @@ 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. 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 just running `import AMR`, users can seamlessly integrate the robust features of the R `AMR` package into Python workflows. 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.
Whether you're cleaning data or analysing resistance patterns, the `AMR` Python package makes it easy to work with AMR data in Python.