Skip to content

Commit

Permalink
feat(add): Linters
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikstranneheim committed May 6, 2024
1 parent ae0d5eb commit 8a16b38
Show file tree
Hide file tree
Showing 25 changed files with 149 additions and 137 deletions.
1 change: 1 addition & 0 deletions crunchy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Invoke as ``crunchy`` (if installed)
or ``python -m crunchy`` (no install required).
"""

import sys

from crunchy.cli.base import base_command
Expand Down
11 changes: 5 additions & 6 deletions crunchy/cli/auto_cmd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Code for CLI auto command"""

import logging
import pathlib

Expand All @@ -19,8 +20,7 @@ def abort_if_false(ctx, param, value):

@click.group()
def auto():
"""Run whole pipeline by compressing, comparing and deleting original files.
"""
"""Run whole pipeline by compressing, comparing and deleting original files."""
LOG.info("Running crunchy auto")


Expand Down Expand Up @@ -57,7 +57,8 @@ def fastq(ctx, indir, spring_path, first, second, dry_run):

if indir:
LOG.info(
"This will recursively compress and delete fastqs in %s", indir,
"This will recursively compress and delete fastqs in %s",
indir,
)
indir = pathlib.Path(indir)
if not indir.is_dir():
Expand All @@ -67,9 +68,7 @@ def fastq(ctx, indir, spring_path, first, second, dry_run):

else:
if not (first and second and spring_path):
LOG.warning(
"Please specify either a directory or two fastqs and a spring path"
)
LOG.warning("Please specify either a directory or two fastqs and a spring path")
return
LOG.info(
"Running auto, this will compress and delete fastqs %s, %s into %s",
Expand Down
16 changes: 8 additions & 8 deletions crunchy/cli/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""CLI functionality for crunchy"""

import logging

import click
Expand All @@ -18,9 +19,7 @@


@click.group()
@click.option(
"--spring-binary", default="spring", show_default=True, help="Path to spring binary"
)
@click.option("--spring-binary", default="spring", show_default=True, help="Path to spring binary")
@click.option(
"--samtools-binary",
default="samtools",
Expand All @@ -36,7 +35,9 @@
)
@click.version_option(__version__)
@click.option(
"--reference", "-r", help="Path to reference genome",
"--reference",
"-r",
help="Path to reference genome",
)
@click.option(
"--log-level",
Expand All @@ -45,12 +46,11 @@
help="Choose what log messages to show",
)
@click.option(
"--tmp-dir", help="If specific temp dir should be used",
"--tmp-dir",
help="If specific temp dir should be used",
)
@click.pass_context
def base_command(
ctx, spring_binary, samtools_binary, threads, reference, log_level, tmp_dir
):
def base_command(ctx, spring_binary, samtools_binary, threads, reference, log_level, tmp_dir):
"""Base command for crunchy
\b
Expand Down
7 changes: 2 additions & 5 deletions crunchy/cli/checksum_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@

@click.command()
@click.argument("infile", type=click.Path(exists=True))
@click.option(
"--algorithm", "-a", type=click.Choice(["md5", "sha1", "sha256"]), default="sha256"
)
@click.option("--algorithm", "-a", type=click.Choice(["md5", "sha1", "sha256"]), default="sha256")
def checksum(infile, algorithm):
"""Generate the checksum for a file
"""
"""Generate the checksum for a file"""
LOG.info("Running checksum")
click.echo(get_checksum(pathlib.Path(infile), algorithm))
9 changes: 5 additions & 4 deletions crunchy/cli/compare_cmd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Code for checksum module."""

import logging
import pathlib

Expand All @@ -13,11 +14,11 @@
@click.option("--first", "-f", type=click.Path(exists=True), required=True)
@click.option("--second", "-s", type=click.Path(exists=True))
@click.option(
"--checksum", "-c", help="If the file should be compared to a checksum directly",
)
@click.option(
"--algorithm", "-a", type=click.Choice(["md5", "sha1", "sha256"]), default="sha256"
"--checksum",
"-c",
help="If the file should be compared to a checksum directly",
)
@click.option("--algorithm", "-a", type=click.Choice(["md5", "sha1", "sha256"]), default="sha256")
@click.option("--dry-run", is_flag=True)
def compare(first, second, algorithm, checksum, dry_run):
"""Compare two files by generating checksums. Fails if two files differ.
Expand Down
29 changes: 15 additions & 14 deletions crunchy/cli/compress_cmd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""CLI functions to compress."""

import logging
from pathlib import Path
from typing import Optional
Expand Down Expand Up @@ -36,7 +37,9 @@ def compress():
help="Second read in pair",
)
@click.option(
"--spring-path", "-o", help="Path to spring file",
"--spring-path",
"-o",
help="Path to spring file",
)
@click.option(
"--check-integrity",
Expand All @@ -50,9 +53,7 @@ def compress():
help="If a json file with metada should be produced",
)
@click.pass_context
def fastq(
ctx, first_read, second_read, spring_path, dry_run, check_integrity, metadata_file
):
def fastq(ctx, first_read, second_read, spring_path, dry_run, check_integrity, metadata_file):
"""Compress a pair of FASTQ files with Spring."""
LOG.info("Running compress fastq")
if dry_run:
Expand All @@ -77,7 +78,7 @@ def fastq(
first_read=first_read, second_read=second_read, spring=spring_path
)

metadata_path: Optional[Path]= dump_spring_metadata(metadata) if metadata_file else None
metadata_path: Optional[Path] = dump_spring_metadata(metadata) if metadata_file else None
if not check_integrity:
return

Expand All @@ -101,12 +102,8 @@ def fastq(

success = True
try:
ctx.invoke(
compare, first=str(first_spring), checksum=checksums[0], dry_run=dry_run
)
ctx.invoke(
compare, first=str(second_spring), checksum=checksums[1], dry_run=dry_run
)
ctx.invoke(compare, first=str(first_spring), checksum=checksums[0], dry_run=dry_run)
ctx.invoke(compare, first=str(second_spring), checksum=checksums[1], dry_run=dry_run)
except click.Abort:
LOG.error("Uncompressed Spring differ from original FASTQs")
success = False
Expand Down Expand Up @@ -137,7 +134,9 @@ def fastq(
help="Path to bam file",
)
@click.option(
"--cram-path", "-c", help="Path to cram file",
"--cram-path",
"-c",
help="Path to cram file",
)
@click.option("--dry-run", is_flag=True)
@click.pass_context
Expand All @@ -155,12 +154,14 @@ def bam(ctx, bam_path: click.Path, cram_path: str, dry_run: bool):
cram_path: Path = Path(cram_path) if cram_path else cram_outpath(bam_path)
file_exists(cram_path, exists=False)
compress_cram(
bam_path=bam_path, cram_path=cram_path, cram_api=cram_api, dry_run=dry_run,
bam_path=bam_path,
cram_path=cram_path,
cram_api=cram_api,
dry_run=dry_run,
)

LOG.info("Compression successful")


for sub_cmd in [fastq, bam]:
compress.add_command(sub_cmd)

32 changes: 18 additions & 14 deletions crunchy/cli/decompress_cmd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Code for decompress CLI fuctions"""

import logging
import pathlib

Expand Down Expand Up @@ -32,18 +33,20 @@ def decompress():
help="Fastq file that spring archive will be decompressed to. Second read in pair",
)
@click.option(
"--first-checksum", help="Checksum from the original fastq file, first in pair",
"--first-checksum",
help="Checksum from the original fastq file, first in pair",
)
@click.option(
"--second-checksum", help="Checksum from the original fastq file, second in pair",
"--second-checksum",
help="Checksum from the original fastq file, second in pair",
)
@click.option(
"--dry-run", is_flag=True, help="Skip deleting original files",
"--dry-run",
is_flag=True,
help="Skip deleting original files",
)
@click.pass_context
def spring(
ctx, spring_path, first_read, second_read, first_checksum, second_checksum, dry_run
):
def spring(ctx, spring_path, first_read, second_read, first_checksum, second_checksum, dry_run):
"""Decompress a spring file to fastq files"""
LOG.info("Running decompress spring")
spring_api = ctx.obj.get("spring_api")
Expand Down Expand Up @@ -74,12 +77,8 @@ def spring(
return

try:
ctx.invoke(
compare, first=str(first_read), checksum=first_checksum, dry_run=dry_run
)
ctx.invoke(
compare, first=str(second_read), checksum=second_checksum, dry_run=dry_run
)
ctx.invoke(compare, first=str(first_read), checksum=first_checksum, dry_run=dry_run)
ctx.invoke(compare, first=str(second_read), checksum=second_checksum, dry_run=dry_run)
except click.Abort:
LOG.error("Uncompressed spring differ from given checksum")
LOG.info("Deleting decompressed fastq files")
Expand All @@ -100,7 +99,9 @@ def spring(
help="Path to bam file",
)
@click.option(
"--dry-run", is_flag=True, help="Skip deleting original files",
"--dry-run",
is_flag=True,
help="Skip deleting original files",
)
@click.pass_context
def cram(ctx, cram_path, bam_path, dry_run):
Expand All @@ -111,7 +112,10 @@ def cram(ctx, cram_path, bam_path, dry_run):
bam_path = pathlib.Path(bam_path)

decompress_cram(
cram_path=cram_path, bam_path=bam_path, cram_api=cram_api, dry_run=dry_run,
cram_path=cram_path,
bam_path=bam_path,
cram_api=cram_api,
dry_run=dry_run,
)


Expand Down
1 change: 1 addition & 0 deletions crunchy/cli/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utilities for crunchy cli functions"""

import logging
import pathlib

Expand Down
22 changes: 10 additions & 12 deletions crunchy/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import subprocess
from pathlib import Path
from subprocess import CalledProcessError
from typing import Optional, List, Generator
from typing import Generator, List, Optional

LOG = logging.getLogger(__name__)

Expand All @@ -19,7 +19,9 @@ class Process:
called, that will be handled in this module.Output form stdout and stdin will be handeld here.
"""

def __init__(self, binary: str, config: Optional[str] = None, config_parameter: str = "--config"):
def __init__(
self, binary: str, config: Optional[str] = None, config_parameter: str = "--config"
):
"""
Args:
binary(str): Path to binary for the process to use
Expand All @@ -46,14 +48,14 @@ def run_command(self, parameters=None):
command.extend(parameters)

LOG.info("Running command %s", " ".join(command))
res = subprocess.run(
command, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
res = subprocess.run(command, check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

self.stdout = res.stdout.decode("utf-8").rstrip()
self.stderr = res.stderr.decode("utf-8").rstrip()
if res.returncode != 0:
LOG.critical(f"Call {command} exit with a non zero exit code", )
LOG.critical(
f"Call {command} exit with a non zero exit code",
)
LOG.critical(self.stderr)
raise CalledProcessError(command, res.returncode)

Expand Down Expand Up @@ -106,9 +108,7 @@ def __init__(self, binary: str, threads: int = 8, tmp_dir: Optional[str] = None)
self.threads: int = threads
self.tmp: Optional[str] = tmp_dir

def decompress(
self, spring_path: Path, first: Path, second: Path
) -> bool:
def decompress(self, spring_path: Path, first: Path, second: Path) -> bool:
"""Run the spring decompress command."""
parameters = ["-d", "-i", str(spring_path), "-o", str(first), str(second)]
if first.suffix == ".gz":
Expand Down Expand Up @@ -138,9 +138,7 @@ def decompress(
LOG.error(self.stderr)
return False

def compress(
self, first: Path, second: Path, outfile: Path
) -> bool:
def compress(self, first: Path, second: Path, outfile: Path) -> bool:
"""Run the spring compression command."""
parameters = [
"-c",
Expand Down
4 changes: 1 addition & 3 deletions crunchy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def find_fastq_pairs(directory: pathlib.Path) -> Iterator[tuple]:
LOG.debug("Read already found: %s", pth)
continue
if len(splitted) < 3:
LOG.info(
"Fastq filename %s does not follow illumina conventions", file_name
)
LOG.info("Fastq filename %s does not follow illumina conventions", file_name)
continue
# Check if we have a part of a read pair
spring_name = pathlib.Path("_".join(splitted[:-2]))
Expand Down
1 change: 1 addition & 0 deletions crunchy/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
"""Holds the current version"""

__version__ = "1.0.3"
5 changes: 1 addition & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Based on https://github.com/kennethreitz/setup.py"""


import contextlib
import io
import os
Expand All @@ -26,9 +25,7 @@ def parse_reqs(req_path="./requirements.txt"):
install_requires = []
with io.open(os.path.join(HERE, req_path), encoding="utf-8") as handle:
# remove comments and empty lines
lines = (
line.strip() for line in handle if line.strip() and not line.startswith("#")
)
lines = (line.strip() for line in handle if line.strip() and not line.startswith("#"))

for line in lines:
if line.startswith("-i") or line.startswith("-e"):
Expand Down
Loading

0 comments on commit 8a16b38

Please sign in to comment.