Skip to content

Commit

Permalink
Merge branch 'CW-1831-accept-stream' into 'dev'
Browse files Browse the repository at this point in the history
Pore-c-py accept data stream

See merge request epi2melabs/pore-c-py!35
  • Loading branch information
sarahjeeeze committed Apr 5, 2023
2 parents 2aa4b89 + 39bf33e commit 960a533
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v2.0.3]
### Added
- Digest sub command will accept stdin input.

## [v2.0.2]
### Added
- Threads parameter to chunk_bam sub command.
Expand Down
2 changes: 1 addition & 1 deletion pore_c_py/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Init."""

__version__ = "2.0.2"
__version__ = "2.0.3"
48 changes: 37 additions & 11 deletions pore_c_py/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def porec_parser():
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
digest_parse.set_defaults(func=digest_bam)
digest_parse.add_argument(
"input", type=Path,
"input", type=Path, default=[sys.stdin], nargs='*',
help="An unaligned BAM file or FASTQ file or a directory of same")
digest_parse.add_argument(
"enzyme",
Expand All @@ -60,6 +60,13 @@ def porec_parser():
"--output", type=Path, default=sys.stdout,
help="An unaligned BAM file with a separate record for each monomer,"
"default will write to standard out.")
digest_parse.add_argument(
"--header", type=Path,
help="The output bam requires a header."
"If INPUT parameter is stdin any existing headers will not be"
"accessible so optionally provide an existing bam that"
"contains headers to be copied to the output bam."
"Otherwise the header will be 'pore-c-py'.")
digest_parse.add_argument(
"--recursive", default=False, action="store_true",
help=(
Expand Down Expand Up @@ -184,25 +191,44 @@ def porec_parser():


def digest_bam(args):
"""Digest entry point."""
"""Digest entry point.
Read in concatemer sequences from unaligned bam or fastq.
Cuts each read with enzyme provided,
and tags with relevant concatemer info.
Outputs a bam containing each monomer sequence.
"""
logger = utils.get_named_logger("Digest")
logger.info(f"Digesting concatemers from {args.input}.")
input_files = list(
utils.find_files(
args.input, glob=args.glob, recursive=args.recursive))
with pysam.AlignmentFile(
str(input_files[0]), "r", check_sq=False) as inputfile:
header = align_tools.update_header(inputfile.header)
# Default header will be pore-c-py.
# If input type is a file or directory, Header will be copied from that.
# If input type is stdin, there is an optional header parameter available
# to supply a preferred header present in an existing bam.
get_header = args.header
input_files = [args.input[0]]
input_mode = "rb"
pysam_kwargs = {"text": "pore-c-py"}
if sys.stdin not in args.input:
input_files = list(
utils.find_files(
args.input[0], glob=args.glob, recursive=args.recursive))
get_header = str(input_files[0])
input_mode = "r"
if args.header or sys.stdin not in args.input:
with pysam.AlignmentFile(
get_header, input_mode, check_sq=False) as inputfile:
header = align_tools.update_header(inputfile.header)
pysam_kwargs = {"header": header}
mode = "wb"
if (args.output is not sys.stdout) and \
(not utils.stdout_is_regular_file()):
mode = "wb0"
with pysam.AlignmentFile(
args.output, header=header,
threads=args.threads, mode=mode) as outbam:
args.output,
threads=args.threads, mode=mode, **pysam_kwargs) as outbam:
for input_file in input_files:
with pysam.AlignmentFile(
str(input_file), "r", check_sq=False) as inputfile:
input_file, input_mode, check_sq=False) as inputfile:
for monomer in digest.get_concatemer_seqs(
inputfile, enzyme=args.enzyme,
remove_tags=args.remove_tags):
Expand Down

0 comments on commit 960a533

Please sign in to comment.