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

add download dir and template arguments to generic idc program #138

Merged
merged 3 commits into from
Nov 18, 2024
Merged
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
29 changes: 25 additions & 4 deletions idc_index/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,18 @@ def download_from_manifest(
"generic_argument",
type=str,
)
@click.option(
"--download-dir",
required=False,
type=click.Path(),
help="Path to the directory to download the files to.",
)
@click.option(
"--dir-template",
type=str,
default=IDCClient.DOWNLOAD_HIERARCHY_DEFAULT,
help="Download directory hierarchy template. This variable defines the folder hierarchy for the organizing the downloaded files in downloadDirectory. Defaults to index.DOWNLOAD_HIERARCHY_DEFAULT set to %collection_id/%PatientID/%StudyInstanceUID/%Modality_%SeriesInstanceUID. The template string can be built using a combination of selected metadata attributes (PatientID, collection_id, Modality, StudyInstanceUID, SeriesInstanceUID) that must be prefixed by '%'. The following special characters can be used as separators: '-' (hyphen), '/' (slash for subdirectories), '_' (underscore). When set to empty string (\"\") all files will be downloaded to the download directory with no subdirectories.",
)
@click.option(
"--log-level",
type=click.Choice(
Expand All @@ -304,7 +316,7 @@ def download_from_manifest(
default="info",
help="Set the logging level for the CLI module.",
)
def download(generic_argument, log_level):
def download(generic_argument, download_dir, dir_template, log_level):
"""Download content given the input parameter.

Determine whether the input parameter corresponds to a file manifest or a list of collection_id, PatientID, StudyInstanceUID, or SeriesInstanceUID values, and download the corresponding files into the current directory. Default parameters will be used for organizing the downloaded files into folder hierarchy. Use `download_from_selection()` and `download_from_manifest()` functions if granular control over the download process is needed.
Expand All @@ -316,12 +328,17 @@ def download(generic_argument, log_level):

logger_cli.info(f"Downloading from IDC {client.get_idc_version()} index")

download_dir = Path.cwd()
if download_dir:
download_dir = Path(download_dir)
else:
download_dir = Path.cwd()

if Path(generic_argument).is_file():
# Parse the input parameters and pass them to IDC
logger_cli.info("Detected manifest file, downloading from manifest.")
client.download_from_manifest(generic_argument, downloadDir=download_dir)
client.download_from_manifest(
generic_argument, downloadDir=download_dir, dirTemplate=dir_template
)
# this is not a file manifest
else:
# Split the input string and filter out any empty values
Expand All @@ -344,7 +361,11 @@ def check_and_download(column_name, item_ids, download_dir, kwarg_name):
)
logger_cli.info(f"Identified matching {column_name}: {matched_ids}")
client.download_from_selection(
**{kwarg_name: matched_ids, "downloadDir": download_dir}
**{
kwarg_name: matched_ids,
"downloadDir": download_dir,
"dirTemplate": dir_template,
}
)
return True

Expand Down
Loading