Skip to content

Commit

Permalink
fix the code for new entry point
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimens committed Jun 3, 2024
1 parent 42afc53 commit ef7ed81
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/createrelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
run: rm -rf /opt/hostedtoolcache
- name: Recreate container
shell: micromamba-shell {0}
run: snakemake -s resources/container/containerize.smk --containerize > Dockerfile
run: harpy containerize
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/rebuildcontainer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,17 @@ jobs:
environment-file: resources/harpy.yaml
cache-environment: true
post-cleanup: 'all'
- name: Install harpy
shell: micromamba-shell {0}
run: |
python3 -m pip install --upgrade build && python3 -m build
pip install dist/*.whl
resources/buildforCI.sh
- name: Clear space
run: rm -rf /opt/hostedtoolcache
- name: Rebuild dockerfile
shell: micromamba-shell {0}
run: snakemake -s src/harpy/containerize.smk --containerize > Dockerfile
run: harpy containerize
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
Expand Down
25 changes: 4 additions & 21 deletions src/harpy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
enverror = "\033[1;33mERROR:\033[00m Harpy expects to run from within an active conda environment, but one was not detected."
print(enverror, file = sys.stderr)
fix = "\033[1;34mSOLUTION:\033[00m Activate the conda environment Harpy was installed into and run Harpy again."
print()
print(fix, file = sys.stderr)
print("\n" + fix, file = sys.stderr)
print(f"\n\033[1mDetails:\033[00m")
details = "In order to work correctly, Harpy expects several software packages to be available in the PATH, which are provided automatically with Harpy's conda-based installation. It also expects snakefiles, scripts, utilities, etc. to be in the /bin/ folder within that conda environment. If you're seeing this message, no active conda environment was detected upon execution, and Harpy exited as an early failsafe against unexpected runtime errors associated with \"missing\" files and packages."
print(details, file = sys.stderr)
exit(1)
sys.exit(1)

from . import align
from . import demultiplex
Expand All @@ -25,11 +24,10 @@
from . import simulate
from . import snp
from . import sv
from . import container
from .popgroups import popgroup
from .stitchparams import stitchparams
from .conda_deps import generate_conda_deps
import rich_click as click
import subprocess

click.rich_click.USE_MARKDOWN = True
click.rich_click.SHOW_ARGUMENTS = False
Expand All @@ -54,7 +52,6 @@ def cli():
**Documentation**: [https://pdimens.github.io/harpy/](https://pdimens.github.io/harpy/)
"""
pass

# main program
#cli.add_command(hpc)
Expand All @@ -69,6 +66,7 @@ def cli():
cli.add_command(impute.impute)
cli.add_command(phase.phase)
cli.add_command(simulate.simulate)
cli.add_command(container.containerize)
# demultiplex submodules
demultiplex.demultiplex.add_command(demultiplex.gen1)
# preflight submodules
Expand Down Expand Up @@ -119,18 +117,3 @@ def cli():
}

click.rich_click.OPTION_GROUPS = demultiplex.docstring | preflight.docstring | qc.docstring | align.docstring | snp.docstring | sv.docstring | impute.docstring | phase.docstring | simulate.docstring

def main():
try:
workflow = cli(standalone_mode = False)
if workflow == 0:
return 0
elif workflow is not None:
generate_conda_deps()
_module = subprocess.run(workflow, check = False)
return _module.returncode
except:
sys.exit(1)

if __name__ == '__main__':
sys.exit(main())
16 changes: 12 additions & 4 deletions src/harpy/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import os
import sys
import subprocess
from time import sleep
import rich_click as click
from .conda_deps import generate_conda_deps
from .helperfunctions import fetch_report, fetch_rule, fetch_script
from .fileparsers import get_samples_from_fastq, parse_fastq_inputs
from .printfunctions import print_error, print_solution, print_notice, print_onstart
Expand Down Expand Up @@ -114,7 +116,7 @@ def bwa(inputs, output_dir, genome, depth_window, threads, extra_params, quality
fetch_script(workflowdir, "bxStats.py")
fetch_report(workflowdir, "AlignStats.Rmd")

with open(f"{workflowdir}/config.yml", "w", encoding="uft-8") as config:
with open(f"{workflowdir}/config.yml", "w", encoding="utf-8") as config:
config.write(f"genomefile: {genome}\n")
config.write(f"seq_directory: {workflowdir}/input\n")
config.write(f"output_directory: {output_dir}\n")
Expand All @@ -131,7 +133,9 @@ def bwa(inputs, output_dir, genome, depth_window, threads, extra_params, quality
f"Samples: {len(samplenames)}\nOutput Directory: {output_dir}",
"align bwa"
)
return command
generate_conda_deps()
_module = subprocess.run(command)
sys.exit(_module.returncode)

@click.command(no_args_is_help = True, epilog = "read the docs for more information: https://pdimens.github.io/harpy/modules/align/ema")
@click.option('-p', '--platform', type = click.Choice(['haplotag', '10x'], case_sensitive=False), default = "haplotag", show_default=True, help = "Linked read bead technology\n[haplotag, 10x]")
Expand Down Expand Up @@ -223,7 +227,9 @@ def ema(inputs, output_dir, platform, whitelist, genome, depth_window, threads,
f"Samples: {len(samplenames)}\nPlatform: {platform}\nOutput Directory: {output_dir}/",
"align ema"
)
return command
generate_conda_deps()
_module = subprocess.run(command)
sys.exit(_module.returncode)

@click.command(no_args_is_help = True, epilog= "read the docs for more information: https://pdimens.github.io/harpy/modules/align/minimap/")
@click.option('-g', '--genome', type=click.Path(exists=True, dir_okay=False), required = True, help = 'Genome assembly for read mapping')
Expand Down Expand Up @@ -294,4 +300,6 @@ def minimap(inputs, output_dir, genome, depth_window, threads, extra_params, qua
f"Samples: {len(samplenames)}\nOutput Directory: {output_dir}",
"align minimap"
)
return command
generate_conda_deps()
_module = subprocess.run(command)
sys.exit(_module.returncode)
25 changes: 25 additions & 0 deletions src/harpy/container.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Command to regenerate Dockerfile for container building"""

import os
import sys
import subprocess
import rich_click as click
from .conda_deps import generate_conda_deps
from .helperfunctions import fetch_rule

@click.command(no_args_is_help = False, hidden = True)
def containerize():
"""
Configure conda and docker environments
**INTERNAL USE ONLY**. Used to recreate all the conda environments required
by the workflows and build a dockerfile from that.
"""
generate_conda_deps()
fetch_rule(os.getcwd(), "containerize.smk")
command = 'snakemake -s containerize.smk --containerize'

with open("Dockerfile", "w", encoding = "utf-8") as dockerfile:
_module = subprocess.run(command.split(), stdout = dockerfile)
os.remove("containerize.smk")
sys.exit(_module.returncode)
6 changes: 5 additions & 1 deletion src/harpy/demultiplex.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Harpy demultiplex workflows"""

import sys
import subprocess
import rich_click as click
from .conda_deps import generate_conda_deps
from .printfunctions import print_onstart
from .helperfunctions import fetch_rule
from .validations import validate_demuxschema
Expand Down Expand Up @@ -91,4 +93,6 @@ def gen1(r1_fq, r2_fq, i1_fq, i2_fq, output_dir, schema, threads, snakemake, ski
f"Haplotag Type: Generation I\nDemultiplex Schema: {schema}\nOutput Directory: {output_dir}",
"demultiplex gen1"
)
return command
generate_conda_deps()
_module = subprocess.run(command)
sys.exit(_module.returncode)
6 changes: 5 additions & 1 deletion src/harpy/impute.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import os
import sys
import subprocess
import rich_click as click
from .conda_deps import generate_conda_deps
from .helperfunctions import fetch_rule, fetch_report, fetch_script, biallelic_contigs
from .fileparsers import parse_alignment_inputs
from .printfunctions import print_onstart
Expand Down Expand Up @@ -99,4 +101,6 @@ def impute(inputs, output_dir, parameters, threads, vcf, vcf_samples, extra_para
f"Input VCF: {vcf}\nSamples in VCF: {len(samplenames)}\nAlignments Provided: {len(sn)}\nContigs Considered: {len(contigs)}\nOutput Directory: {output_dir}/",
"impute"
)
return command
generate_conda_deps()
_module = subprocess.run(command)
sys.exit(_module.returncode)
5 changes: 4 additions & 1 deletion src/harpy/phase.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import subprocess
import rich_click as click
from .conda_deps import generate_conda_deps
from .helperfunctions import fetch_rule, fetch_report
from .fileparsers import parse_alignment_inputs
from .printfunctions import print_onstart
Expand Down Expand Up @@ -100,4 +101,6 @@ def phase(inputs, output_dir, vcf, threads, molecule_distance, prune_threshold,
f"Input VCF: {vcf}\nSamples in VCF: {len(samplenames)}\nAlignments Provided: {len(sn)}\nOutput Directory: {output_dir}/",
"phase"
)
return command
generate_conda_deps()
_module = subprocess.run(command)
sys.exit(_module.returncode)
1 change: 0 additions & 1 deletion src/harpy/popgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,3 @@ def popgroup(inputdir, output):
for i in samplenames:
_ = file.write(i + '\tpop1\n')
print_notice(write_text + " Please review it, as all samples have been grouped into a single population")
return 0
10 changes: 8 additions & 2 deletions src/harpy/preflight.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import os
import sys
import subprocess
import rich_click as click
from .conda_deps import generate_conda_deps
from .helperfunctions import fetch_rule, fetch_report, fetch_script
from .printfunctions import print_onstart
from .fileparsers import parse_alignment_inputs, parse_fastq_inputs
Expand Down Expand Up @@ -86,7 +88,9 @@ def fastq(inputs, output_dir, threads, snakemake, quiet, conda, print_only):
f"Files: {len(sn)}\nOutput Directory: {output_dir}/",
"preflight fastq"
)
return command
generate_conda_deps()
_module = subprocess.run(command)
sys.exit(_module.returncode)

@click.command(no_args_is_help = True, epilog = "read the docs for more information: https://pdimens.github.io/harpy/modules/preflight/")
@click.option('-t', '--threads', default = 4, show_default = True, type = click.IntRange(min = 1, max_open = True), help = 'Number of threads to use')
Expand Down Expand Up @@ -141,4 +145,6 @@ def bam(inputs, output_dir, threads, snakemake, quiet, conda, print_only):
f"Samples: {len(sn)}\nOutput Directory: {output_dir}/",
"preflight bam"
)
return command
generate_conda_deps()
_module = subprocess.run(command)
sys.exit(_module.returncode)
10 changes: 7 additions & 3 deletions src/harpy/qc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Harpy sequence adapter trimming and quality control"""

import sys
import subprocess
import rich_click as click
from .conda_deps import generate_conda_deps
from .helperfunctions import fetch_report, fetch_rule, fetch_script
from .fileparsers import get_samples_from_fastq, parse_fastq_inputs
from .printfunctions import print_onstart
Expand Down Expand Up @@ -31,7 +33,7 @@
@click.option('--conda', is_flag = True, default = False, help = 'Use conda/mamba instead of container')
@click.option('--skipreports', is_flag = True, show_default = True, default = False, help = 'Don\'t generate HTML reports')
@click.option('--print-only', is_flag = True, hidden = True, show_default = True, default = False, help = 'Print the generated snakemake command and exit')
@click.argument('input', required=True, type=click.Path(exists=True), nargs=-1)
@click.argument('inputs', required=True, type=click.Path(exists=True), nargs=-1)
def qc(inputs, output_dir, min_length, max_length, ignore_adapters, extra_params, threads, snakemake, skipreports, quiet, conda, print_only):
"""
Remove adapters and quality trim sequences
Expand Down Expand Up @@ -69,7 +71,7 @@ def qc(inputs, output_dir, min_length, max_length, ignore_adapters, extra_params
fetch_rule(workflowdir, "qc.smk")
fetch_report(workflowdir, "BxCount.Rmd")

with open(f"{workflowdir}/config.yml", "w", encoding="uft-8") as config:
with open(f"{workflowdir}/config.yml", "w", encoding="utf-8") as config:
config.write(f"seq_directory: {workflowdir}/input\n")
config.write(f"output_directory: {output_dir}\n")
config.write(f"adapters: {ignore_adapters}\n")
Expand All @@ -83,4 +85,6 @@ def qc(inputs, output_dir, min_length, max_length, ignore_adapters, extra_params
f"Samples: {len(sn)}\nOutput Directory: {output_dir}/",
"qc"
)
return command
generate_conda_deps()
_module = subprocess.run(command)
sys.exit(_module.returncode)
23 changes: 17 additions & 6 deletions src/harpy/simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import os
import sys
import subprocess
import rich_click as click
from .conda_deps import generate_conda_deps
from .helperfunctions import fetch_rule, fetch_script, symlink
from .printfunctions import print_onstart, print_error
from .validations import validate_input_by_ext
Expand Down Expand Up @@ -164,8 +166,9 @@ def linkedreads(genome_hap1, genome_hap2, output_dir, outer_distance, mutation_r
onstart_text += f"Output Directory: {output_dir}/"
print_onstart(onstart_text, "simulate reads")

return command

generate_conda_deps()
_module = subprocess.run(command)
sys.exit(_module.returncode)

@click.command(no_args_is_help = True, epilog = "This workflow can be quite technical, please read the docs for more information: https://pdimens.github.io/harpy/modules/simulate/simulate-variants")
@click.option('-s', '--snp-vcf', type=click.Path(exists=True, dir_okay=False), help = 'VCF file of known snps to simulate')
Expand Down Expand Up @@ -289,7 +292,9 @@ def snpindel(genome, snp_vcf, indel_vcf, output_dir, prefix, snp_count, indel_co
config.write(f"workflow_call: {call_SM}\n")

print_onstart(printmsg.rstrip("\n"), "simulate variants: snpindel")
return command
generate_conda_deps()
_module = subprocess.run(command)
sys.exit(_module.returncode)

@click.command(no_args_is_help = True, epilog = "Please read the docs for more information: https://pdimens.github.io/harpy/modules/simulate/simulate-variants")
@click.option('-v', '--vcf', type=click.Path(exists=True, dir_okay=False), help = 'VCF file of known inversions to simulate')
Expand Down Expand Up @@ -389,7 +394,9 @@ def inversion(genome, vcf, prefix, output_dir, count, min_size, max_size, centro
config.write(f"workflow_call: {call_SM}\n")

print_onstart(printmsg.rstrip("\n"), "simulate variants: inversion")
return command
generate_conda_deps()
_module = subprocess.run(command)
sys.exit(_module.returncode)

@click.command(no_args_is_help = True, epilog = "Please read the docs for more information: https://pdimens.github.io/harpy/modules/simulate/simulate-variants")
@click.option('-v', '--vcf', type=click.Path(exists=True, dir_okay=False), help = 'VCF file of known copy number variants to simulate')
Expand Down Expand Up @@ -501,7 +508,9 @@ def cnv(genome, output_dir, vcf, prefix, count, min_size, max_size, dup_ratio, m
config.write(f"workflow_call: {call_SM}\n")

print_onstart(printmsg.rstrip("\n"),"simulate cnv")
return command
generate_conda_deps()
_module = subprocess.run(command)
sys.exit(_module.returncode)

@click.command(no_args_is_help = True, epilog = "Please read the docs for more information: https://pdimens.github.io/harpy/modules/simulate/simulate-variants")
@click.option('-v', '--vcf', type=click.Path(exists=True, dir_okay=False), help = 'VCF file of known translocations to simulate')
Expand Down Expand Up @@ -597,4 +606,6 @@ def translocation(genome, output_dir, prefix, vcf, count, centromeres, genes, he
config.write(f"workflow_call: {call_SM}\n")

print_onstart(printmsg.rstrip("\n"), "simulate variants: translocation")
return command
generate_conda_deps()
_module = subprocess.run(command)
sys.exit(_module.returncode)
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import os
import shutil
from conda_deps import generate_conda_deps

generate_conda_deps()

onsuccess:
shutil.rmtree(f'.harpy_envs', ignore_errors=True)
Expand Down
Loading

0 comments on commit ef7ed81

Please sign in to comment.