Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test coverage of filter.write_vcf and refactor #479

Merged
merged 2 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions augur/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,20 @@ def read_vcf(filename):
return sequences, sequences.copy()


def write_vcf(compressed, input_file, output_file, dropped_samps):
#Read in/write out according to file ending
inCall = "--gzvcf" if compressed else "--vcf"
outCall = "| gzip -c" if output_file.lower().endswith('.gz') else ""
def write_vcf(input_filename, output_filename, dropped_samps):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concern as #478 about changing the API. If this is only used internally it is probably ok, but if they have any users this will cause code breakage.

if _filename_gz(input_filename):
input_arg = "--gzvcf"
else:
input_arg = "--vcf"

if _filename_gz(output_filename):
output_pipe = "| gzip -c"
else:
output_pipe = ""

toDrop = " ".join(["--remove-indv "+shquote(s) for s in dropped_samps])
call = ["vcftools", toDrop, inCall, shquote(input_file), "--recode --stdout", outCall, ">", shquote(output_file)]
drop_args = ["--remove-indv " + shquote(s) for s in dropped_samps]

call = ["vcftools"] + drop_args + [input_arg, shquote(input_filename), "--recode --stdout", output_pipe, ">", shquote(output_filename)]

print("Filtering samples using VCFTools with the call:")
print(" ".join(call))
Expand Down Expand Up @@ -339,7 +346,7 @@ def run(args):
if len(dropped_samps) == len(all_seq): #All samples have been dropped! Stop run, warn user.
print("ERROR: All samples have been dropped! Check filter rules and metadata file format.")
return 1
write_vcf(is_compressed, args.sequences, args.output, dropped_samps)
write_vcf(args.sequences, args.output, dropped_samps)

else:
seq_to_keep = [seq for id,seq in seqs.items() if id in seq_keep]
Expand Down Expand Up @@ -370,3 +377,7 @@ def run(args):
print("\t%i sequences were added back because of '%s'" % (num_included_by_metadata, args.include_where))

print("%i sequences have been written out to %s" % (len(seq_keep), args.output))


def _filename_gz(filename):
return filename.lower().endswith(".gz")
56 changes: 56 additions & 0 deletions tests/test_filter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import augur.filter

import pytest


Expand All @@ -14,6 +15,11 @@ def mock_priorities_file_malformed(mocker):
mocker.patch("builtins.open", mocker.mock_open(read_data="strain1 X\n"))


@pytest.fixture
def mock_run_shell_command(mocker):
mocker.patch("augur.filter.run_shell_command")


class TestFilter:
def test_read_vcf_compressed(self):
seq_keep, all_seq = augur.filter.read_vcf(
Expand Down Expand Up @@ -47,3 +53,53 @@ def test_read_priority_scores_malformed(self, mock_priorities_file_malformed):
def test_read_priority_scores_does_not_exist(self):
with pytest.raises(FileNotFoundError):
augur.filter.read_priority_scores("/does/not/exist.txt")

def test_write_vcf_compressed_input(self, mock_run_shell_command):
augur.filter.write_vcf(
"tests/builds/tb/data/lee_2015.vcf.gz", "output_file.vcf.gz", []
)

augur.filter.run_shell_command.assert_called_once_with(
"vcftools --gzvcf tests/builds/tb/data/lee_2015.vcf.gz --recode --stdout | gzip -c > output_file.vcf.gz",
raise_errors=True,
)

def test_write_vcf_uncompressed_input(self, mock_run_shell_command):
augur.filter.write_vcf(
"tests/builds/tb/data/lee_2015.vcf", "output_file.vcf.gz", []
)

augur.filter.run_shell_command.assert_called_once_with(
"vcftools --vcf tests/builds/tb/data/lee_2015.vcf --recode --stdout | gzip -c > output_file.vcf.gz",
raise_errors=True,
)

def test_write_vcf_compressed_output(self, mock_run_shell_command):
augur.filter.write_vcf(
"tests/builds/tb/data/lee_2015.vcf", "output_file.vcf.gz", []
)

augur.filter.run_shell_command.assert_called_once_with(
"vcftools --vcf tests/builds/tb/data/lee_2015.vcf --recode --stdout | gzip -c > output_file.vcf.gz",
raise_errors=True,
)

def test_write_vcf_uncompressed_output(self, mock_run_shell_command):
augur.filter.write_vcf(
"tests/builds/tb/data/lee_2015.vcf", "output_file.vcf", []
)

augur.filter.run_shell_command.assert_called_once_with(
"vcftools --vcf tests/builds/tb/data/lee_2015.vcf --recode --stdout > output_file.vcf",
raise_errors=True,
)

def test_write_vcf_dropped_samples(self, mock_run_shell_command):
augur.filter.write_vcf(
"tests/builds/tb/data/lee_2015.vcf", "output_file.vcf", ["x", "y", "z"]
)

augur.filter.run_shell_command.assert_called_once_with(
"vcftools --remove-indv x --remove-indv y --remove-indv z --vcf tests/builds/tb/data/lee_2015.vcf --recode --stdout > output_file.vcf",
raise_errors=True,
)