Skip to content
Merged
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
8 changes: 7 additions & 1 deletion amdirt/assets/tables.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@
"ancientmetagenome-hostassociated": "https://raw.githubusercontent.com/SPAAM-community/AncientMetagenomeDir/master/ancientmetagenome-hostassociated/libraries/ancientmetagenome-hostassociated_libraries_schema.json",
"ancientsinglegenome-hostassociated": "https://raw.githubusercontent.com/SPAAM-community/AncientMetagenomeDir/master/ancientsinglegenome-hostassociated/libraries/ancientsinglegenome-hostassociated_libraries_schema.json",
"test": "https://raw.githubusercontent.com/SPAAM-community/amdirt/dev/tests/data/libraries_schema.json"
},
"dates": {
"ancientsinglegenome-hostassociated": "https://raw.githubusercontent.com/SPAAM-community/AncientMetagenomeDir/master/ancientsinglegenome-hostassociated/dates/ancientsinglegenome-hostassociated_dates.tsv"
},
"dates_schema": {
"ancientsinglegenome-hostassociated": "https://raw.githubusercontent.com/SPAAM-community/AncientMetagenomeDir/master/ancientsinglegenome-hostassociated/dates/ancientsinglegenome-hostassociated_dates_schema.json"
}
}
}
7 changes: 6 additions & 1 deletion amdirt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ def viewer(ctx, no_args_is_help=True, **kwargs):
is_flag=True,
help="Generate BibTeX file of all publications in input table",
)
@click.option(
"--dates",
is_flag=True,
help="Generate AncientMetagenomeDir dates table of all samples in input table"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe mention its single genomes only currently?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Aida plans to add the dates for ancientmetagenome-hostassociated in the next hackathon. I wrote the function so that it will ignore this option when there is no dates table available for the category and instead returns an error message with an explanation.
I think this should be sufficient.

)
@click.option(
"--curl",
is_flag=True,
Expand Down Expand Up @@ -320,7 +325,7 @@ def merge(ctx, no_args_is_help=True, **kwargs):
"-y",
"--table_type",
help="Type of table to download",
type=click.Choice(["samples", "libraries"]),
type=click.Choice(["samples", "libraries", "dates"]),
default="samples",
show_default=True,
)
Expand Down
18 changes: 18 additions & 0 deletions amdirt/convert/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
prepare_aMeta_table,
is_merge_size_zero,
prepare_taxprofiler_table,
get_dates,
get_libraries,
get_remote_resources,
get_json_path,
Expand All @@ -27,6 +28,7 @@ def run_convert(
tables=None,
output=".",
bibliography=False,
dates=False,
librarymetadata=False,
curl=False,
aspera=False,
Expand Down Expand Up @@ -138,6 +140,22 @@ def run_convert(
else:
col_drop = ["archive_accession", "sample_host"]

if dates == True:
if table_name not in remote_resources["dates"]:
logger.error(f"No dates for {table_name} available in AncientMetagenomeDir at the moment.")
else:
tbl_file = f"{output}/AncientMetagenomeDir_filtered_dates.tsv"
dates_tbl = pd.read_csv(remote_resources['dates'][table_name],
sep="\t")
logger.info(f"Writing filtered dates table to {tbl_file}")
datesmetadata = get_dates(table_name, samples, dates_tbl)
datesmetadata.to_csv(
tbl_file,
sep="\t",
index=False,
)


if librarymetadata == True:
tbl_file = f"{output}/AncientMetagenomeDir_filtered_libraries.tsv"
logger.info(f"Writing filtered libraries table to {tbl_file}")
Expand Down
34 changes: 34 additions & 0 deletions amdirt/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,40 @@ def doi2bib(doi: str) -> str:
return r.text


def get_dates(
table_name: str,
samples: pd.DataFrame,
dates: pd.DataFrame
):
"""Get dates from the samples table

Args:
table_name (str): Name of the table of the table to convert
samples (pd.DataFrame): Sample table

Returns:
pd.DataFrame: filtered dates table
"""
samples = (samples
.rename({'archive_accession': 'archive_sample_accession'}, axis=1)
)
if table_name in [
"ancientmetagenome-environmental",
]:
sel_col = ["archive_accession"]
else:
sel_col = ["project_name", "publication_year",
"sample_name", "singlegenome_species",
"archive_project", "archive_sample_accession"]
selected_dates = dates.merge(
samples.loc[:, sel_col],
on=sel_col,
how="inner"
)

return selected_dates


@st.cache_data
def get_libraries(
table_name: str,
Expand Down
17 changes: 11 additions & 6 deletions amdirt/download/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def download(table: str, table_type: str, release: str, output: str = ".") -> st
table : str
The AncientMetagenomeDir table to download.
table_type : str
The type of table to download. Allowed values are ['samples', 'libraries'].
The type of table to download. Allowed values are ['samples', 'libraries', 'dates'].
release : str
The release of the table to download. Must be a valid release tag.
output: str
Expand Down Expand Up @@ -47,16 +47,21 @@ def download(table: str, table_type: str, release: str, output: str = ".") -> st
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:
if check_allowed_values(["samples", "libraries", "dates"], table_type) is False:
raise ValueError(
f"Invalid table type: {table_type}. Allowed values are ['samples', 'libraries']"
f"Invalid table type: {table_type}. Allowed values are ['samples', 'libraries', 'dates']"
)
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)
try:
t = requests.get(resources[table_type][table].replace("master", release))
with open(f"{output}/{table_filename}", "w") as fh:
fh.write(t.text)
except KeyError:
logger.warning(
f"Invalid table: {table}. {table} currently does not have any {table_type} information."
)

return table_filename