Skip to content

Commit

Permalink
feat: add libraries input to convert
Browse files Browse the repository at this point in the history
  • Loading branch information
maxibor committed Feb 12, 2024
1 parent 2fc3e05 commit f23b0de
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
44 changes: 39 additions & 5 deletions AMDirT/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@
from json import load


class MutuallyExclusiveOption(click.Option):
# Credits goes to Stan Chang for this code snippet
# https://gist.github.com/stanchan/bce1c2d030c76fe9223b5ff6ad0f03db

def __init__(self, *args, **kwargs):
self.mutually_exclusive = set(kwargs.pop("mutually_exclusive", []))
help = kwargs.get("help", "")
if self.mutually_exclusive:
ex_str = ", ".join(self.mutually_exclusive)
kwargs["help"] = help + (
" NOTE: This argument is mutually exclusive with "
" arguments: [" + ex_str + "]."
)
super(MutuallyExclusiveOption, self).__init__(*args, **kwargs)

def handle_parse_result(self, ctx, opts, args):
if self.mutually_exclusive.intersection(opts) and self.name in opts:
raise click.UsageError(
"Illegal usage: `{}` is mutually exclusive with "
"arguments `{}`.".format(self.name, ", ".join(self.mutually_exclusive))
)

return super(MutuallyExclusiveOption, self).handle_parse_result(ctx, opts, args)


def get_table_list():
json_path = get_json_path()
with open(json_path, "r") as f:
Expand Down Expand Up @@ -110,6 +135,20 @@ def viewer(ctx, no_args_is_help=True, **kwargs):
type=click.Path(exists=True),
help="(Optional) JSON file listing AncientMetagenomeDir tables",
)
@click.option(
"--libraries",
type=click.Path(readable=True, file_okay=True, dir_okay=False, exists=True),
help=("(Optional) Path to libraries table"),
cls=MutuallyExclusiveOption,
mutually_exclusive=["librarymetadata"],
)
@click.option(
"--librarymetadata",
is_flag=True,
help="Generate AncientMetagenomeDir libraries table of all samples in input table",
cls=MutuallyExclusiveOption,
mutually_exclusive=["libraries"],
)
@click.option(
"-o",
"--output",
Expand All @@ -123,11 +162,6 @@ def viewer(ctx, no_args_is_help=True, **kwargs):
is_flag=True,
help="Generate BibTeX file of all publications in input table",
)
@click.option(
"--librarymetadata",
is_flag=True,
help="Generate AncientMetagenomeDir libraries table of all samples in input table",
)
@click.option(
"--curl",
is_flag=True,
Expand Down
37 changes: 29 additions & 8 deletions AMDirT/convert/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

def run_convert(
samples,
libraries,
table_name,
tables=None,
output=".",
Expand All @@ -40,9 +41,10 @@ def run_convert(
"""Run the AMDirT conversion application to input samplesheet tables for different pipelines
Args:
tables (str): Path to JSON file listing tables
samples (str): Path to AncientMetagenomeDir filtered samples tsv file
libraries(str): Optional path to AncientMetagenomeDir filtered libraries tsv file
table_name (str): Name of the table of the table to convert
tables (str): Path to JSON file listing tables
output (str): Path to output table. Defaults to "."
"""
os.makedirs(output, exist_ok=True)
Expand Down Expand Up @@ -79,14 +81,33 @@ def run_convert(
else:
logger.info("Input sample dataset is valid")
samples = pd.read_csv(samples, sep="\t")
libraries = pd.read_csv(remote_resources["libraries"][table_name], sep="\t")
remote_libraries = pd.read_csv(
remote_resources["libraries"][table_name], sep="\t"
)

selected_libraries = get_libraries(
samples=samples,
libraries=libraries,
table_name=table_name,
supported_archives=supported_archives,
)
if not libraries:
selected_libraries = get_libraries(
samples=samples,
libraries=remote_libraries,
table_name=table_name,
supported_archives=supported_archives,
)
else:
dataset_valid = list()
v = AMDirValidator(schema, libraries)
dataset_valid.append(v.parsing_ok)
if v.parsing_ok:
dataset_valid.append(v.validate_schema())
dataset_valid.append(v.check_duplicate_rows())
dataset_valid.append(v.check_columns())

dataset_valid = all(dataset_valid)
if dataset_valid is False:
v.to_rich()
raise DatasetValidationError("Input libraries dataset is not valid")
else:
logger.info("Input libraries dataset is valid")
selected_libraries = pd.read_csv(libraries, sep="\t")

accession_table = prepare_accession_table(
samples=samples,
Expand Down

0 comments on commit f23b0de

Please sign in to comment.