-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
265 additions
and
1 deletion.
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
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,153 @@ | ||
import logging | ||
import shutil | ||
import sys | ||
from pathlib import Path | ||
|
||
import rich | ||
import rich.console | ||
import rich.traceback | ||
import rich_click as click | ||
from rich.logging import RichHandler | ||
|
||
import bactopia | ||
|
||
# Set up Rich | ||
stderr = rich.console.Console(stderr=True) | ||
rich.traceback.install(console=stderr, width=200, word_wrap=True, extra_lines=1) | ||
click.rich_click.USE_RICH_MARKUP = True | ||
click.rich_click.OPTION_GROUPS = { | ||
"bactopia-atb-formatter": [ | ||
{"name": "Required Options", "options": ["--path"]}, | ||
{ | ||
"name": "Bactopia Directory Structure Options", | ||
"options": [ | ||
"--bactopia-dir", | ||
"--publish-mode", | ||
"--recursive", | ||
], | ||
}, | ||
{ | ||
"name": "Additional Options", | ||
"options": [ | ||
"--verbose", | ||
"--silent", | ||
"--version", | ||
"--help", | ||
], | ||
}, | ||
] | ||
} | ||
|
||
|
||
def search_path(path, pattern, recursive=False): | ||
if recursive: | ||
return Path(path).rglob(pattern) | ||
else: | ||
return Path(path).glob(pattern) | ||
|
||
|
||
def create_sample_directory(sample, assembly, bactopia_dir, publish_mode="symlink"): | ||
logging.debug(f"Creating {sample} directory ({bactopia_dir}/{sample})") | ||
sample_dir = Path(f"{bactopia_dir}/{sample}") | ||
if not sample_dir.exists(): | ||
sample_dir.mkdir(parents=True, exist_ok=True) | ||
|
||
# Make remaining subdirectories (which will be empty) | ||
Path(f"{bactopia_dir}/{sample}/main").mkdir(parents=True, exist_ok=True) | ||
Path(f"{bactopia_dir}/{sample}/main/gather").mkdir(parents=True, exist_ok=True) | ||
Path(f"{bactopia_dir}/{sample}/main/assembler").mkdir(parents=True, exist_ok=True) | ||
|
||
# Write the meta.tsv file | ||
logging.debug(f"Writing {sample}-meta.tsv") | ||
with open(f"{bactopia_dir}/{sample}/main/gather/{sample}-meta.tsv", "w") as meta_fh: | ||
meta_fh.write( | ||
"sample\truntype\toriginal_runtype\tis_paired\tis_compressed\tspecies\tgenome_size\n" | ||
) | ||
meta_fh.write( | ||
f"{sample}\tassembly_accession\tassembly_accession\tfalse\tfalse\null\0\n" | ||
) | ||
|
||
# Write the assembly file | ||
final_assembly = f"{bactopia_dir}/{sample}/main/assembler/{sample}.fna" | ||
final_assembly_path = Path(final_assembly) | ||
if publish_mode == "symlink": | ||
logging.debug(f"Creating symlink of {assembly} at {final_assembly}") | ||
final_assembly_path.symlink_to(assembly) | ||
else: | ||
logging.debug(f"Copying {assembly} to {final_assembly}") | ||
shutil.copyfile(assembly, final_assembly) | ||
|
||
return True | ||
|
||
|
||
@click.command() | ||
@click.version_option(bactopia.__version__, "--version", "-V") | ||
@click.option( | ||
"--path", "-p", required=True, help="Directory where FASTQ files are stored" | ||
) | ||
@click.option( | ||
"--bactopia-dir", | ||
"-b", | ||
default="bactopia", | ||
show_default=True, | ||
help="The path you would like to place bactopia structure", | ||
) | ||
@click.option( | ||
"--publish-mode", | ||
"-m", | ||
default="symlink", | ||
show_default=True, | ||
type=click.Choice(["symlink", "copy"], case_sensitive=False), | ||
help="Designates plascement of assemblies will be handled", | ||
) | ||
@click.option( | ||
"--recursive", "-r", is_flag=True, help="Traverse recursively through provided path" | ||
) | ||
@click.option("--verbose", is_flag=True, help="Increase the verbosity of output") | ||
@click.option("--silent", is_flag=True, help="Only critical errors will be printed") | ||
def atb_formatter( | ||
path, | ||
bactopia_dir, | ||
publish_mode, | ||
recursive, | ||
verbose, | ||
silent, | ||
): | ||
"""Restructure All-the-Bacteria assemblies to allow usage with Bactopia Tools""" | ||
# Setup logs | ||
logging.basicConfig( | ||
format="%(asctime)s:%(name)s:%(levelname)s - %(message)s", | ||
datefmt="%Y-%m-%d %H:%M:%S", | ||
handlers=[ | ||
RichHandler(rich_tracebacks=True, console=rich.console.Console(stderr=True)) | ||
], | ||
) | ||
logging.getLogger().setLevel( | ||
logging.ERROR if silent else logging.DEBUG if verbose else logging.INFO | ||
) | ||
|
||
abspath = Path(path).absolute() | ||
fasta_ext = ".fa" | ||
|
||
# Match Assemblies | ||
count = 0 | ||
logging.info( | ||
"Setting up Bactopia directory structure (use --verbose to see more details)" | ||
) | ||
for fasta in search_path(abspath, f"*{fasta_ext}", recursive=recursive): | ||
fasta_name = fasta.name.replace(fasta_ext, "") | ||
create_sample_directory(fasta_name, fasta, bactopia_dir, publish_mode) | ||
count += 1 | ||
logging.info(f"Bactopia directory structure created at {bactopia_dir}") | ||
logging.info(f"Total assemblies processed: {count}") | ||
|
||
|
||
def main(): | ||
if len(sys.argv) == 1: | ||
atb_formatter.main(["--help"]) | ||
else: | ||
atb_formatter() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "bactopia" | ||
version = "1.0.8" | ||
version = "1.0.9" | ||
description = "A Python package for working with Bactopia" | ||
authors = [ | ||
"Robert A. Petit III <[email protected]>", | ||
|
@@ -19,6 +19,7 @@ bactopia-prepare = "bactopia.cli.prepare:main" | |
bactopia-search = "bactopia.cli.search:main" | ||
bactopia-summary = "bactopia.cli.summary:main" | ||
bactopia-update = "bactopia.cli.update:main" | ||
bactopia-atb-formatter = "bactopia.cli.atb_formatter:main" | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.8.0" | ||
|