-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from maxibor/download
feat: add download subcommand
- Loading branch information
Showing
7 changed files
with
167 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "1.5.0" | ||
__version__ = "1.6.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from AMDirT.core import ( | ||
logger, | ||
get_amdir_tags, | ||
get_remote_resources, | ||
check_allowed_values, | ||
) | ||
import requests | ||
|
||
|
||
def download(table: str, table_type: str, release: str, output: str = ".") -> str: | ||
""" | ||
Download a table from the AMDirT repository. | ||
Parameters | ||
---------- | ||
table : str | ||
The AncientMetagenomeDir table to download. | ||
table_type : str | ||
The type of table to download. Allowed values are ['samples', 'libraries']. | ||
release : str | ||
The release of the table to download. Must be a valid release tag. | ||
output: str | ||
The output directory to save the table. Default is the current directory. | ||
Returns | ||
------- | ||
str: | ||
The path to the downloaded table. | ||
Raises | ||
------ | ||
ValueError | ||
If an invalid table is provided. | ||
ValueError | ||
If an invalid table type is provided. | ||
ValueError | ||
If an invalid release is provided. | ||
""" | ||
|
||
resources = get_remote_resources() | ||
tags = get_amdir_tags() | ||
if tags != ["master"]: | ||
if check_allowed_values(tags, release) is False: | ||
raise ValueError(f"Invalid release: {release}. Allowed values are {tags}") | ||
|
||
tables = resources["samples"] | ||
if check_allowed_values(tables, table) is False: | ||
raise ValueError(f"Invalid table: {table}. Allowed values are {tables}") | ||
|
||
if check_allowed_values(["samples", "libraries"], table_type) is False: | ||
raise ValueError( | ||
f"Invalid table type: {table_type}. Allowed values are ['samples', 'libraries']" | ||
) | ||
table_filename = f"{table}_{table_type}_{release}.tsv" | ||
logger.info( | ||
f"Downloading {table} {table_type} table from {release} release, saving to {output}/{table_filename}" | ||
) | ||
t = requests.get(resources[table_type][table].replace("master", release)) | ||
with open(table_filename, "w") as fh: | ||
fh.write(t.text) | ||
|
||
return table_filename |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# download | ||
|
||
## What | ||
|
||
Download a copy of an AncientMetagenomeDir table. | ||
|
||
## When | ||
|
||
This command would be used when you want to download an AncientMetagenomeDir table locally. | ||
|
||
You typically do this if you're planning to use the `convert` command later. | ||
|
||
## How | ||
|
||
```bash | ||
AMDirT download --table ancientsinglegenome-hostassociated --table_type samples -r v23.12.0 -o . | ||
``` | ||
|
||
## Output | ||
|
||
This example command above will download the `ancientsinglegenome-hostassociated` `sample` table from the `v23.12.0` AncientMetagenomeDir release, and save it locally to `ancientmetagenome-hostassociated_samples_v23.12.0.tsv` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from AMDirT.download import download | ||
|
||
|
||
def test_download(): | ||
table = "ancientmetagenome-hostassociated" | ||
table_type = "samples" | ||
release = "v23.12.0" | ||
|
||
d = download(table, table_type, release, output=".") | ||
assert d == "ancientmetagenome-hostassociated_samples_v23.12.0.tsv" |