Skip to content

Commit

Permalink
Merge branch 'release-v0.0.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
cartoonist committed Jul 26, 2018
2 parents 3a43ce1 + 6aad489 commit 23723b0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 19 additions & 5 deletions vcfy/ksnper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
:license: MIT, see LICENSE for more details.
"""

import sys
import csv

import click
Expand Down Expand Up @@ -71,7 +72,7 @@ def ksnpcounts(snpbv, k):
yield kcount


def write_csv(output, vcf_file, ref_file, k, dialect='unix'):
def write_csv(output, vcf_file, ref_file, k, dialect='unix', compressed=None):
"""Write CSV file.
Args:
Expand All @@ -86,14 +87,20 @@ def write_csv(output, vcf_file, ref_file, k, dialect='unix'):
The length of the k-mer.
dialect : str
This string specifies the dialect of the output CSV file.
compressed : bool
Whether input VCF is compressed or not. It is determined by file
extension if it is not specified.
"""
csv_writer = csv.DictWriter(output,
fieldnames=['k', 'count'],
dialect=dialect,
quoting=csv.QUOTE_NONE)
csv_writer.writeheader()

vcf_reader = vcf.Reader(vcf_file)
if isinstance(vcf_file, str):
vcf_reader = vcf.Reader(filename=vcf_file, compressed=compressed)
else:
vcf_reader = vcf.Reader(vcf_file, compressed=compressed)
if ref_file is None:
ref_file = open(vcf_reader.metadata['reference'], 'r')
bv = compute_snpbv(vcf_reader, reflen(ref_file))
Expand All @@ -102,19 +109,26 @@ def write_csv(output, vcf_file, ref_file, k, dialect='unix'):


@click.command()
@click.argument('vcf', type=click.File('r'), default="-")
@click.argument('vcf', type=str, default="-")
@click.option('-o', '--output', type=click.File('w'), default="-",
help="Write to this file instead of standard output.")
@click.option('-r', '--reference', type=click.File('r'), default=None,
help=("Reference genome FASTA file. It will be inferred from VCF "
"header, if not specified."))
@click.option('-k', type=int, required=True, help="The value of k.")
@click.option('-c', is_flag=True, default=None,
help="Set if the input VCF is compressed")
@click.option('-d', '--dialect', type=click.Choice(csv.list_dialects()),
default='unix', show_default=True,
help="Use this CSV dialect.")
def cli(**kwargs):
"""Report the number of SNPs in all k-mers. Specify the k and the VCF file,
it reports number of SNPS occurred in each k-mer.
"""
stdin_fsock = sys.stdin.buffer if kwargs['c'] else sys.stdin
write_csv(kwargs.pop('output'),
kwargs.pop('vcf'),
kwargs.pop('vcf') if kwargs['vcf'] != '-' else stdin_fsock,
kwargs.pop('reference'),
kwargs.pop('k'),
kwargs.pop('dialect'))
kwargs.pop('dialect'),
kwargs.pop('c'))
2 changes: 1 addition & 1 deletion vcfy/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
__license__ = 'MIT'

# Release
__version__ = '0.0.5'
__version__ = '0.0.6'
__status__ = DS_PREALPHA

# Package data
Expand Down

0 comments on commit 23723b0

Please sign in to comment.