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

Change config handling #410

Open
wants to merge 21 commits into
base: refactor_config
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .github/workflows/_run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ jobs:
- name: Conda info
shell: bash -l {0}
run: conda info
- name: Install mono
shell: bash -l {0}
run: |
conda install mono

- name: Perform pip installation with all stable dependencies
shell: bash -l {0}
run: |
Expand Down
166 changes: 38 additions & 128 deletions alphadia/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@

import alphadia
from alphadia import utils
from alphadia.constants.keys import ConfigKeys
from alphadia.exceptions import CustomError
from alphadia.search_plan import SearchPlan
from alphadia.workflow import reporting

logger = logging.getLogger()

epilog = "Parameters passed via CLI will overwrite parameters from config file (except for '--file': will be merged)."

parser = argparse.ArgumentParser(description="Search DIA experiments with alphaDIA")
parser = argparse.ArgumentParser(
description="Search DIA experiments with alphaDIA", epilog=epilog
)
parser.add_argument(
"--version",
"-v",
Expand All @@ -32,17 +36,19 @@
)
parser.add_argument(
"--output",
"--output-directory",
"-o",
type=str,
help="Output directory",
help="Output directory.",
nargs="?",
default=None,
)
parser.add_argument(
"--file",
"--raw-path",
"-f",
type=str,
help="Raw data input files.",
help="Path to raw data input file. Can be passed multiple times.",
action="append",
default=[],
)
Expand All @@ -58,29 +64,31 @@
"--regex",
"-r",
type=str,
help="Regex to match raw files in directory.",
help="Regex to match raw files in 'directory'.",
nargs="?",
default=".*",
)
parser.add_argument(
"--library",
"--library-path",
"-l",
type=str,
help="Spectral library.",
help="Path to spectral library file.",
nargs="?",
default=None,
)
parser.add_argument(
"--fasta",
help="Fasta file(s) used to generate or annotate the spectral library.",
"--fasta-path",
help="Path to fasta file used to generate or annotate the spectral library. Can be passed multiple times.",
action="append",
default=[],
)
parser.add_argument(
"--config",
"-c",
type=str,
help="Config yaml which will be used to update the default config.",
help="Path to config yaml file which will be used to update the default config.",
nargs="?",
default=None,
)
Expand All @@ -93,14 +101,15 @@
parser.add_argument(
"--config-dict",
type=str,
help="Python Dict which will be used to update the default config.",
help="Python dictionary which will be used to update the default config.",
nargs="?",
default="{}",
)
parser.add_argument(
"--quant-dir",
"--quant-dir", # TODO deprecate
"--quant-directory",
type=str,
help="Directory to save the quantification results (psm & frag parquet files) to be reused in a distributed search",
help="Directory to save the quantification results (psm & frag parquet files) to be reused in a distributed search.",
nargs="?",
default=None,
)
Expand Down Expand Up @@ -159,11 +168,11 @@ def parse_output_directory(args: argparse.Namespace, config: dict) -> str:
"""

output_directory = None
if "output_directory" in config:
if config.get(ConfigKeys.OUTPUT_DIRECTORY) is not None:
output_directory = (
utils.windows_to_wsl(config["output_directory"])
utils.windows_to_wsl(config[ConfigKeys.OUTPUT_DIRECTORY])
if args.wsl
else config["output_directory"]
else config[ConfigKeys.OUTPUT_DIRECTORY]
)

if args.output is not None:
Expand All @@ -174,41 +183,6 @@ def parse_output_directory(args: argparse.Namespace, config: dict) -> str:
return output_directory


def parse_quant_dir(args: argparse.Namespace, config: dict) -> str:
"""Parse custom quant path.
1. Use custom quant path from config file if specified.
2. Use custom quant path from command line if specified.

Parameters
----------

args : argparse.Namespace
Command line arguments.

config : dict
Config dictionary.

Returns
-------

quant_dir : str
path to quant directory.
"""

quant_dir = None
if "quant_dir" in config:
quant_dir = (
utils.windows_to_wsl(config["quant_dir"])
if args.wsl
else config["quant_dir"]
)

if args.quant_dir is not None:
quant_dir = utils.windows_to_wsl(args.quant_dir) if args.wsl else args.quant_dir

return quant_dir


def parse_raw_path_list(args: argparse.Namespace, config: dict) -> list:
"""Parse raw file list.
1. Use raw file list from config file if specified.
Expand All @@ -229,7 +203,7 @@ def parse_raw_path_list(args: argparse.Namespace, config: dict) -> list:
raw_path_list : list
List of raw files.
"""
config_raw_path_list = config.get("raw_path_list", [])
config_raw_path_list = config.get("raw_paths", [])
raw_path_list = (
utils.windows_to_wsl(config_raw_path_list) if args.wsl else config_raw_path_list
)
Expand Down Expand Up @@ -259,71 +233,6 @@ def parse_raw_path_list(args: argparse.Namespace, config: dict) -> list:
return raw_path_list


def parse_library(args: argparse.Namespace, config: dict) -> str:
"""Parse spectral library.
1. Use spectral library from config file if specified.
2. Use spectral library from command line if specified.

Parameters
----------

args : argparse.Namespace
Command line arguments.

config : dict
Config dictionary.

Returns
-------

library : str
Spectral library.
"""

library = None
if "library" in config:
library = (
utils.windows_to_wsl(config["library"]) if args.wsl else config["library"]
)

if args.library is not None:
library = utils.windows_to_wsl(args.library) if args.wsl else args.library

return library


def parse_fasta(args: argparse.Namespace, config: dict) -> list:
"""Parse fasta file list.
1. Use fasta file list from config file if specified.
2. Use fasta file list from command line if specified.

Parameters
----------

args : argparse.Namespace
Command line arguments.

config : dict
Config dictionary.

Returns
-------

fasta_path_list : list
List of fasta files.
"""

config_fasta_path_list = config.get("fasta_list", [])
fasta_path_list = (
utils.windows_to_wsl(config_fasta_path_list)
if args.wsl
else config_fasta_path_list
)
fasta_path_list += utils.windows_to_wsl(args.fasta) if args.wsl else args.fasta

return fasta_path_list


def run(*args, **kwargs):
# parse command line arguments
args, unknown = parser.parse_known_args()
Expand All @@ -344,29 +253,30 @@ def run(*args, **kwargs):
# print help message if no output directory specified
parser.print_help()

print("No output directory specified.")
print("No output directory specified. Please do so via CL-argument or config.")
return
reporting.init_logging(output_directory)

quant_dir = parse_quant_dir(args, config)
# TODO revisit the multiple sources of raw files (cli, config, regex, ...)
raw_path_list = parse_raw_path_list(args, config)
library_path = parse_library(args, config)
fasta_path_list = parse_fasta(args, config)
cli_params_config = {
**({ConfigKeys.RAW_PATHS: raw_path_list} if raw_path_list else {}),
**({ConfigKeys.LIBRARY_PATH: args.library} if args.library is not None else {}),
**({ConfigKeys.FASTA_PATHS: args.library} if args.fasta else {}),
**(
{ConfigKeys.QUANT_DIRECTORY: args.library}
if args.quant_dir is not None
else {}
),
}

# TODO rename all output_directory, output_folder => output_path, quant_dir->quant_path (except cli parameter)

# important to suppress matplotlib output
matplotlib.use("Agg")

try:
SearchPlan(
output_directory,
raw_path_list,
library_path,
fasta_path_list,
config,
quant_dir,
).run_plan()
SearchPlan(output_directory, config, cli_params_config).run_plan()

except Exception as e:
if isinstance(e, CustomError):
Expand All @@ -382,5 +292,5 @@ def run(*args, **kwargs):


# uncomment for debugging:
# if __name__ == "__main__":
# run()
if __name__ == "__main__":
run()
22 changes: 12 additions & 10 deletions alphadia/constants/default.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# configuration for the extraction plan
version: 1

library: null
# These values are typically filled via CLI parameters
output_directory: null
raw_path_list: []
output: null
library_path: null
raw_paths: []
fasta_paths: []
quant_directory: null

general:
thread_count: 10
Expand Down Expand Up @@ -63,31 +65,31 @@ library_prediction:
# composition: H(-2)2H(8)13C(2)
custom_modifications:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively: allow to add keys for the custom_modifications key

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update: now that we can overwrite lists, the original format can be used again

# Dimethyl @K channel decoy
Dimethyl:d12@K:
- name: Dimethyl:d12@K
composition: H(-2)2H(8)13C(2)

# Dimethyl @Any_N-term channel decoy
Dimethyl:d12@Any_N-term:
- name: Dimethyl:d12@Any_N-term
composition: H(-2)2H(8)13C(2)

# Dimethyl @Protein_N-term channel decoy
Dimethyl:d12@Protein_N-term:
- name: Dimethyl:d12@Protein_N-term
composition: H(-2)2H(8)13C(2)

# mTRAQ @K channel decoy
mTRAQ:d12@K:
- name: mTRAQ:d12@K
composition: H(12)C(1)13C(10)15N(2)O(1)

# mTRAQ @Any_N-term channel decoy
mTRAQ:d12@Any_N-term:
- name: mTRAQ:d12@Any_N-term
composition: H(12)C(1)13C(14)15N(2)O(1)

# mTRAQ @Protein_N-term channel decoy
mTRAQ:d12@Protein_N-term:
- name: mTRAQ:d12@Protein_N-term
composition: H(12)C(1)13C(14)15N(2)O(1)

# SILAC heavy @K channel decoy
Label:13C(12)@K:
- name: Label:13C(12)@K
composition: C(12)

search:
Expand Down
10 changes: 10 additions & 0 deletions alphadia/constants/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ class OutputKeys(metaclass=ConstantsClass):
MS2_ERROR = "ms2_error"
RT_ERROR = "rt_error"
MOBILITY_ERROR = "mobility_error"


class ConfigKeys(metaclass=ConstantsClass):
"""String constants for accessing the config."""

OUTPUT_DIRECTORY = "output_directory"
LIBRARY_PATH = "library_path"
RAW_PATHS = "raw_paths"
FASTA_PATHS = "fasta_paths"
QUANT_DIRECTORY = "quant_directory"
3 changes: 3 additions & 0 deletions alphadia/constants/multistep.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ transfer:
enabled: True

# override settings that could have been set by the user:
quant_directory: null
general:
save_library: False
reuse_quant: False
Expand All @@ -23,6 +24,7 @@ library:
# predict: True

# override settings that could have been set by the user:
quant_directory: null
general:
save_library: False
reuse_quant: False
Expand All @@ -37,6 +39,7 @@ mbr:
search:
target_num_candidates: 5
# override settings that could have been set by the user:
quant_directory: null
general:
reuse_quant: False
library_prediction:
Expand Down
Loading
Loading