Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Added new action group-peaks-density #15

Open
wants to merge 40 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ad31d87
added test data to package data explicit test
VinzentRisch Jan 10, 2025
615eecf
added MSBackendDataFormat
VinzentRisch Jan 10, 2025
5389b22
added MSExperimentLinkMColsFormat
VinzentRisch Jan 10, 2025
b160dc8
added MSExperimentSampleDataLinksSpectra
VinzentRisch Jan 10, 2025
c090217
added MSExperimentSampleDataFormat
VinzentRisch Jan 10, 2025
60e72f9
added SpectraProcessingQueueFormat
VinzentRisch Jan 10, 2025
4f70318
added SpectraSlotsFormat
VinzentRisch Jan 10, 2025
c9b6ded
added XCMSExperimentChromPeakDataFormat
VinzentRisch Jan 10, 2025
6c95c3d
added XCMSExperimentChromPeaksFormat
VinzentRisch Jan 10, 2025
db00970
changed json to XCMSExperimentJSONFormat to accomodate the second jso…
VinzentRisch Jan 10, 2025
1344a27
added XCMSExperimentFeatureDefinitionsFormat
VinzentRisch Jan 10, 2025
96193c2
added XCMSExperimentFeaturePeakIndexFormat
VinzentRisch Jan 10, 2025
16cfbae
added XCMSExperimentDirFmt
VinzentRisch Jan 10, 2025
98b3475
clean up test data
VinzentRisch Jan 10, 2025
ad0403b
register all formats created type xcmsexperiment
VinzentRisch Jan 10, 2025
4118700
Merge branch 'main' into 7_xcmsexperiment_formats
VinzentRisch Jan 15, 2025
6915cd2
fix paths in tests
VinzentRisch Jan 15, 2025
880cd6b
added utils find peaks
VinzentRisch Jan 15, 2025
15c8126
lotta stuff
VinzentRisch Jan 17, 2025
a090206
fixed msbackend validation
VinzentRisch Jan 17, 2025
8e00af9
Merge branch '7_xcmsexperiment_formats' into 6_peak_picking
VinzentRisch Jan 17, 2025
6de90b9
fixed MSExperimentSampleDataFormat validation
VinzentRisch Jan 17, 2025
14d60df
Merge branch '7_xcmsexperiment_formats' into 6_peak_picking
VinzentRisch Jan 17, 2025
68d21e5
added dump to tsv and resolved probelm with r library ()
VinzentRisch Jan 23, 2025
e540377
renamed input to spectra
VinzentRisch Jan 23, 2025
4d49fa5
capital Peaks
VinzentRisch Jan 23, 2025
b2e2c2e
added new action for retcorr
VinzentRisch Jan 23, 2025
a57ed16
created utils fucntion run_r_script
VinzentRisch Jan 24, 2025
af62890
added tests for utils run fucntion
VinzentRisch Jan 24, 2025
76b4f06
comment
VinzentRisch Jan 24, 2025
f9d8c26
Merge branch '6_peak_picking' into 12_rt_correction
VinzentRisch Jan 24, 2025
b0a5d6c
changed over to utils function
VinzentRisch Jan 24, 2025
8d9d20d
chenges in XCMSExperimentFeatureDefinitionsFormat
VinzentRisch Jan 27, 2025
18ecf59
Merge branch '7_xcmsexperiment_formats' into 14_grouping
VinzentRisch Jan 27, 2025
2473508
added action
VinzentRisch Jan 27, 2025
960c83a
added action to chnage paths in plaintext xcms files
VinzentRisch Jan 30, 2025
976df0a
added new helpter fake mzml files
VinzentRisch Jan 31, 2025
c7a509a
Merge branch '18_change_paths' into 14_grouping
VinzentRisch Jan 31, 2025
91df31e
Merge branch '21_fake_mzml_files' into 14_grouping
VinzentRisch Jan 31, 2025
140983f
added create_fake_mzml_files and removed the need for teh spectra input
VinzentRisch Jan 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions q2_ms/tests/tests_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import importlib
import subprocess
import unittest
from unittest.mock import call, patch

from qiime2.plugin.testing import TestPluginBase

from q2_ms.utils import EXTERNAL_CMD_WARNING, run_command
from q2_ms.utils import EXTERNAL_CMD_WARNING, run_command, run_r_script


class TestRunCommand(TestPluginBase):
Expand All @@ -19,7 +22,7 @@ def test_run_command_verbose(self, mock_print, mock_subprocess_run):
run_command(cmd, cwd=cwd, verbose=True)

# Check if subprocess.run was called with the correct arguments
mock_subprocess_run.assert_called_once_with(cmd, check=True, cwd=cwd)
mock_subprocess_run.assert_called_once_with(cmd, check=True, cwd=cwd, env=None)

# Check if the correct print statements were called
mock_print.assert_has_calls(
Expand All @@ -41,7 +44,41 @@ def test_run_command_non_verbose(self, mock_print, mock_subprocess_run):
run_command(cmd, cwd=cwd, verbose=False)

# Check if subprocess.run was called with the correct arguments
mock_subprocess_run.assert_called_once_with(cmd, check=True, cwd=cwd)
mock_subprocess_run.assert_called_once_with(cmd, check=True, cwd=cwd, env=None)

# Ensure no print statements were made
mock_print.assert_not_called()

@patch("subprocess.run")
def test_run_r_script_success(self, mock_subprocess):
# Call function
run_r_script(
params={"param1": "value1", "param2": 42},
script_name="test_script",
package_name="q2_ms",
)

# Check if subprocess.run was called correctly
expected_script_path = str(
importlib.resources.files("q2_ms") / "assets/test_script.R"
)
expected_cmd = [
"/usr/local/bin/Rscript",
"--vanilla",
expected_script_path,
"--param1",
"value1",
"--param2",
"42",
]

mock_subprocess.assert_called_once_with(
expected_cmd, check=True, cwd=None, env=unittest.mock.ANY
)

@patch("subprocess.run", side_effect=subprocess.CalledProcessError(1, "cmd"))
def test_run_r_script_failure(self, mock_subprocess):
with self.assertRaises(Exception) as context:
run_r_script({}, "", "q2_ms")

self.assertIn("q2_ms", str(context.exception))
27 changes: 27 additions & 0 deletions q2_ms/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import importlib
import os
import subprocess

EXTERNAL_CMD_WARNING = (
Expand All @@ -15,3 +17,28 @@ def run_command(cmd, cwd, verbose=True, env=None):
print("\nCommand:", end=" ")
print(" ".join(cmd), end="\n\n")
subprocess.run(cmd, check=True, cwd=cwd, env=env)


def run_r_script(params, script_name, package_name):
script_path = str(importlib.resources.files("q2_ms") / f"assets/{script_name}.R")
cmd = ["/usr/local/bin/Rscript", "--vanilla", script_path]

for key, value in params.items():
cmd.extend([f"--{key}", str(value)])

# Add /usr/local/bin to PATH to use system installation of Rscript
env = os.environ.copy()
env["PATH"] = "/usr/local/bin:" + env["PATH"]

# Unset Conda-related R variables to prevent it from overriding the system R library
for var in ["R_LIBS", "R_LIBS_USER", "R_HOME", "CONDA_PREFIX"]:
env.pop(var, None)

try:
run_command(cmd, verbose=True, cwd=None, env=env)
except subprocess.CalledProcessError as e:
raise Exception(
f"An error was encountered while running {package_name}, "
f"(return code {e.returncode}), please inspect "
"stdout and stderr to learn more."
)
32 changes: 2 additions & 30 deletions q2_ms/xcms/find_peaks_centwave.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import copy
import importlib
import os
import subprocess
import tempfile

from qiime2 import Metadata

from q2_ms.types import XCMSExperimentDirFmt, mzMLDirFmt
from q2_ms.utils import run_command
from q2_ms.utils import run_r_script


def find_peaks_centwave(
Expand Down Expand Up @@ -43,32 +41,6 @@ def find_peaks_centwave(
params["sample_metadata"] = tsv_path

# Run R script
run_find_chrom_peaks(params)
run_r_script(params, "find_peaks_centwave.R", "XCMS")

return xcms_experiment


def run_find_chrom_peaks(params):
script_path = str(
importlib.resources.files("q2_ms") / "assets/find_peaks_centwave.R"
)
cmd = ["/usr/local/bin/Rscript", "--vanilla", script_path]

for key, value in params.items():
cmd.extend([f"--{key}", str(value)])

env = os.environ.copy() # Copy the current environment variables
env["PATH"] = "/usr/local/bin:" + env["PATH"]

# Unset Conda-related R variables to prevent it from overriding the system R library
for var in ["R_LIBS", "R_LIBS_USER", "R_HOME", "CONDA_PREFIX"]:
env.pop(var, None)

try:
run_command(cmd, verbose=True, cwd=None, env=env)
except subprocess.CalledProcessError as e:
raise Exception(
"An error was encountered while running XCMS, "
f"(return code {e.returncode}), please inspect "
"stdout and stderr to learn more."
)