mirror of
https://github.com/msberends/AMR.git
synced 2024-12-27 12:06:14 +01:00
Compare commits
No commits in common. "cfd31f0f0ce5c1f15d342145cc0f31d053d68632" and "40edc16fdfb135642d7452d2e1cce1bb667fd843" have entirely different histories.
cfd31f0f0c
...
40edc16fdf
1
.github/prehooks/pre-commit
vendored
1
.github/prehooks/pre-commit
vendored
@ -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
|
||||
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/*
|
||||
|
@ -1,6 +1,6 @@
|
||||
Package: AMR
|
||||
Version: 2.1.1.9099
|
||||
Date: 2024-10-17
|
||||
Version: 2.1.1.9095
|
||||
Date: 2024-10-15
|
||||
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
|
||||
|
2
NEWS.md
2
NEWS.md
@ -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).)*
|
||||
|
||||
|
@ -1,13 +1,7 @@
|
||||
#!/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="data-raw/gpt_training_text.txt"
|
||||
output_file="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"
|
||||
@ -18,16 +12,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"
|
||||
@ -35,7 +29,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"
|
||||
@ -44,16 +38,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"
|
||||
|
@ -50,31 +50,14 @@ 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 AMR package is installed in R
|
||||
# Check if the R package is installed
|
||||
if not isinstalled('AMR'):
|
||||
utils = importr('utils')
|
||||
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)
|
||||
utils.install_packages('AMR')
|
||||
|
||||
# Activate the automatic conversion between R and pandas DataFrames
|
||||
pandas2ri.activate()
|
||||
|
||||
# example_isolates
|
||||
example_isolates = pandas2ri.rpy2py(robjects.r('''
|
||||
df <- AMR::example_isolates
|
||||
@ -85,7 +68,6 @@ df[] <- lapply(df, function(x) {
|
||||
x
|
||||
}
|
||||
})
|
||||
df <- df[, !sapply(df, is.list)]
|
||||
df
|
||||
'''))
|
||||
example_isolates['date'] = pd.to_datetime(example_isolates['date'])
|
||||
@ -93,7 +75,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[, !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)
|
||||
EOL
|
||||
@ -238,7 +220,6 @@ 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
@ -24,42 +24,17 @@ 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 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
|
||||
# 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
|
||||
## Example of Usage
|
||||
|
||||
Here’s 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".
|
||||
|
||||
|
||||
## 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:
|
||||
|
||||
@ -137,7 +111,42 @@ AMR.antibiotics
|
||||
| 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, let’s walk through some practical examples of using the `AMR` package within Python.
|
||||
|
||||
## Example 1: Calculating AMR
|
||||
|
||||
```python
|
||||
import AMR
|
||||
@ -152,7 +161,7 @@ print(result)
|
||||
[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. Here’s 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.
|
||||
|
||||
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.
|
||||
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.
|
||||
|
Loading…
Reference in New Issue
Block a user