Repository: whatshap/whatshap

Issue:
Allow `--chromosome` to accept a comma-delimited list of chromosomes.
**[Original report](https://bitbucket.org/whatshap/whatshap/issue/207) by William Rowell (Bitbucket: [williamrowell](https://bitbucket.org/williamrowell), GitHub: [williamrowell](https://github.com/williamrowell)).**

----------------------------------------

I would like to calculate phase block size only for autosomes in a male, and while I can use tabix to remove the sex chromosomes, it would be convenient to pass a list of autosomes to whatshap stats.  Would it be possible to allow the `--chromosome` argument for `whatshap phase` and/or `whatshap stats` to accept a comma delimited list as an argument?


Discussion:

Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index 36a944bf..d09daa00 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -6,9 +6,11 @@ development version
 -------------------
 
 * :issue:`400`: Fixed artificial overinflation of block length stats in ``whatshap stats``.
+* :issue:`207`: Enable comma-separated chromosomes as argument to ``whatshap stats``.
 * :pr:`410`: ``haplotag`` gained support for tagging data with ploidy greater
   than two (use option ``--ploidy``).
 
+
 v1.6 (2022-09-06)
 -----------------
 
diff --git a/whatshap/cli/stats.py b/whatshap/cli/stats.py
index 14ed390f..3ead9a4d 100644
--- a/whatshap/cli/stats.py
+++ b/whatshap/cli/stats.py
@@ -27,8 +27,8 @@ def add_arguments(parser):
         "and ignore all other variants.")
     add("--block-list", metavar="FILE", help="Write list of all blocks to FILE (one block per line)")
     add("--chromosome", dest="chromosomes", metavar="CHROMOSOME", default=[], action="append",
-        help="Name of chromosome to process. If not given, all chromosomes in the "
-        "input VCF are considered. Can be used multiple times")
+        help="Name of chromosome(s) to process. If not given, all chromosomes in the "
+        "input VCF are considered. Can be used multiple times and accepts a comma-separated list. ")
     add("vcf", metavar="VCF", help="Phased VCF file")
 # fmt: on
 
@@ -319,6 +319,12 @@ class PhasingStats:
             )
 
 
+def unpack_chromosomes(chromosomes: List[str]) -> List[str]:
+    """Unpack list chromosomes by splitting comma-separated entries."""
+    unpacked = (chromosome for entry in chromosomes for chromosome in entry.split(","))
+    return [chromosome for chromosome in unpacked if chromosome != ""]
+
+
 def parse_chr_lengths(filename) -> Dict[str, int]:
     """
     Parse chromosome lengths from file filename. The file should have two columns with
@@ -462,6 +468,10 @@ def run_stats(
     chr_lengths=None,
 ):
     gtfwriter = tsv_file = block_list_file = None
+
+    if chromosomes is not None:
+        chromosomes = unpack_chromosomes(chromosomes)
+
     with ExitStack() as stack:
         if gtf:
             gtf_file = stack.enter_context(open(gtf, "wt"))


Test patch:
diff --git a/tests/test_run_stats.py b/tests/test_run_stats.py
index 50595124..85bac2df 100644
--- a/tests/test_run_stats.py
+++ b/tests/test_run_stats.py
@@ -2,7 +2,7 @@
 Tests for 'whatshap stats'
 """
 from collections import namedtuple
-from whatshap.cli.stats import run_stats
+from whatshap.cli.stats import run_stats, unpack_chromosomes
 
 
 def test_stats1(tmp_path):
@@ -132,3 +132,13 @@ def test_overlapping_phaseblocks(tmp_path):
     assert entry.blocks == "3"
     assert entry.bp_per_block_sum == "660"
     assert entry.block_n50[:-1] == "150"
+
+
+def test_unpack_chromosomes_comma():
+    unpack_comma = unpack_chromosomes(["chrA,chrB,chrC"])
+    assert unpack_comma == ["chrA", "chrB", "chrC"]
+
+
+def test_unpack_chromosomes_mixed():
+    unpack_comma = unpack_chromosomes(["chrA,chrB,", "chrC"])
+    assert unpack_comma == ["chrA", "chrB", "chrC"]