Skip to content

Commit

Permalink
Merge pull request #89 from griffithlab/tests
Browse files Browse the repository at this point in the history
Improve tests coverage
  • Loading branch information
susannasiebert committed Jun 19, 2020
2 parents f79b7ad + d12c2c3 commit df01df1
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 7 deletions.
4 changes: 2 additions & 2 deletions civicpy/civic.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def is_deletion(self):
def is_valid_for_vcf(self, emit_warnings=False):
if self.coordinates.chromosome2 or self.coordinates.start2 or self.coordinates.stop2:
warning = "Variant {} has a second set of coordinates. Skipping".format(self.id)
if self.coordinates.chromosome and self.coordinates.start and (self.coordinates.reference_bases or self.coordinates.variant_bases):
elif self.coordinates.chromosome and self.coordinates.start and (self.coordinates.reference_bases or self.coordinates.variant_bases):
if self._valid_ref_bases():
if self._valid_alt_bases():
return True
Expand Down Expand Up @@ -1253,7 +1253,7 @@ def _construct_hgvs_for_coordinate_query(coordinate_query):
base_hgvs = "{}:g.{}".format(chromosome, coordinate_query.start)
variant_type = _variant_type(coordinate_query)
if variant_type == "deletion":
if (coordinate_query.ref) > 1:
if len(coordinate_query.ref) > 1:
return"{}_{}del".format(base_hgvs, coordinate_query.stop)
else:
return "{}del".format(base_hgvs)
Expand Down
4 changes: 2 additions & 2 deletions civicpy/exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,13 @@ def addrecord(self, civic_record):
self._add_variant_record(civic_record.variant)
elif isinstance(civic_record, civic.Gene):
for variant in civic_record.variants:
if civic_record.variant.is_valid_for_vcf(emit_warnings=True):
if variant.is_valid_for_vcf(emit_warnings=True):
self._add_variant_record(variant)
elif isinstance(civic_record, civic.Variant):
if civic_record.is_valid_for_vcf(emit_warnings=True):
self._add_variant_record(civic_record)
else:
raise ValueError('Expected a CIViC Variant, Assertion or Evidence record.')
raise ValueError('Expected a CIViC Gene, Variant, Assertion or Evidence record.')

def addrecords(self, civic_records):
"""
Expand Down
94 changes: 93 additions & 1 deletion civicpy/tests/test_civic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from civicpy import civic, TEST_CACHE_PATH
from civicpy.civic import CoordinateQuery
import logging

ELEMENTS = [
'Assertion'
Expand Down Expand Up @@ -28,7 +29,10 @@ def v600e_assertion():


class TestGetFunctions(object):

def test_element_lookup_by_id(self):
assertion = civic.element_lookup_by_id('assertion', '1')
assert assertion['id'] == 1

def test_get_assertions(self):
test_ids = [1, 2, 3]
results = civic._get_elements_by_ids('assertion', test_ids)
Expand Down Expand Up @@ -79,6 +83,15 @@ def test_get_accepted_only(self):
evidence = civic.get_all_evidence(include_status=['accepted'])
assert len(evidence) >= 3247

#get_all_ids pulls from the live site so it will return more results than get_all_x
def test_get_all_ids(self):
evidence_ids = civic.get_all_evidence_ids()
assert len(evidence_ids) >= len(civic.get_all_evidence())

def test_properties(self, v600e):
evidence = v600e.evidence[0]
assert evidence.variant.name == 'V600E'
assert evidence.statement == evidence.description

class TestVariants(object):

Expand All @@ -94,6 +107,11 @@ def test_get_accepted_only(self):
variants = civic.get_all_variants(include_status=['accepted'])
assert len(variants) >= 1333

#get_all_ids pulls from the live site so it will return more results than get_all_x
def test_get_all_ids(self):
variant_ids = civic.get_all_variant_ids()
assert len(variant_ids) >= len(civic.get_all_variants())

def test_get_by_name(self, v600e):
variants = civic.search_variants_by_name("V600E")
assert len(variants) == 1
Expand All @@ -111,6 +129,13 @@ def test_sanitize_coordinate_bases(self):
assert v.coordinates.reference_bases not in ['', '-']
assert v.coordinates.variant_bases not in ['', '-']

def test_properties(self):
variant = civic.get_variant_by_id(11)
assert sorted(variant.aliases) == sorted(variant.variant_aliases)
assert sorted(variant.groups) == sorted(variant.variant_groups)
assert sorted(variant.types) == sorted(variant.variant_types)
assert variant.summary == variant.description


class TestVariantGroups(object):

Expand All @@ -137,6 +162,11 @@ def test_has_boolean_flags(self, v600e_assertion):
assert v600e_assertion.fda_companion_test is True
assert v600e_assertion.fda_regulatory_approval is True

def test_properties(self):
assertion = civic.get_assertion_by_id(18)
assert assertion.evidence == assertion.evidence_items
assert assertion.hpo_ids == [p.hpo_id for p in assertion.phenotypes if p.hpo_id]


class TestGenes(object):

Expand All @@ -152,6 +182,15 @@ def test_get_accepted_only(self):
genes = civic.get_all_genes(include_status=['accepted'])
assert len(genes) >= 322

#get_all_ids pulls from the live site so it might return more results than get_all_x
def test_get_all_ids(self):
genes = civic.get_all_genes(include_status=['accepted'])
gene_ids = civic.get_all_gene_ids()
assert len(gene_ids) >= len(genes)

def test_get_by_id(self):
gene = civic.get_gene_by_id(58)
assert gene.name == 'VHL'

class TestCoordinateSearch(object):

Expand Down Expand Up @@ -277,9 +316,62 @@ def test_build38_exact_search_variants(self, v600e):
assert len(search_results) == 1
assert search_results[0] == v600e

query = CoordinateQuery('7', 140753336, 140753337, 'TT', 'AC', 'GRCh38')
search_results = civic.search_variants_by_coordinates(query, search_mode='exact')
assert len(search_results) == 1
assert search_results[0].id == 563

query = CoordinateQuery('3', 10146548, 10146549, 'C', None, 'GRCh38')
search_results = civic.search_variants_by_coordinates(query, search_mode='exact')
assert len(search_results) == 1
assert search_results[0].id == 1918

query = CoordinateQuery('3', 10146618, 10146618, None, 'G', 'GRCh38')
search_results = civic.search_variants_by_coordinates(query, search_mode='exact')
assert len(search_results) == 1
assert search_results[0].id == 2042

def test_errors(self):
with pytest.raises(ValueError) as context:
query = CoordinateQuery('7', 140453136, 140453136, 'T', 'A')
variants_single = civic.search_variants_by_coordinates(query, search_mode='wrong_mode')
assert "unexpected search mode" in str(context.value)
with pytest.raises(ValueError) as context:
query = CoordinateQuery('7', 140753336, 140753336, '*', 'A', 'GRCh38')
search_results = civic.search_variants_by_coordinates(query, search_mode='exact')
assert "Can't use wildcard when searching for non-GRCh37 coordinates" in str(context.value)
with pytest.raises(ValueError) as context:
query = CoordinateQuery('7', 140753336, 140753336, None, None, 'GRCh38')
search_results = civic.search_variants_by_coordinates(query, search_mode='exact')
assert "alt or ref required for non-GRCh37 coordinate queries" in str(context.value)
with pytest.raises(ValueError) as context:
query = CoordinateQuery('7', 140753336, 140753336, 'T', 'A', 'GRCh38')
search_results = civic.search_variants_by_coordinates(query, search_mode='any')
assert "Only exact search mode is supported for non-GRCh37 coordinate queries" in str(context.value)



class TestDrugs(object):

def test_has_ncit_id(self, v600e_assertion):
trametinib = v600e_assertion.drugs[0]
assert trametinib.ncit_id == 'C77908'
assert 'pubchem_id' not in trametinib.keys()

#warning logging tests
LOGGER = logging.getLogger(__name__)

def test_is_valid_for_vcf_warnings(caplog):
fusion_variant = civic.get_variant_by_id(287)
fusion_variant.is_valid_for_vcf(emit_warnings=True)
assert "Variant 287 has a second set of coordinates. Skipping" in caplog.text

incomplete_coordinates_variant = civic.get_variant_by_id(27)
incomplete_coordinates_variant.is_valid_for_vcf(emit_warnings=True)
assert "Incomplete coordinates for variant 27. Skipping." in caplog.text

unsupported_var_bases_variant = civic.get_variant_by_id(613)
unsupported_var_bases_variant.is_valid_for_vcf(emit_warnings=True)
assert "Unsupported variant base(s) for variant 613. Skipping." in caplog.text

#currently no case for unsupported ref bases
16 changes: 16 additions & 0 deletions civicpy/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest
from civicpy import cli
import tempfile


class TestCli(object):
def test_create_cache(self):
tmp_file = tempfile.NamedTemporaryFile('w', delete=False)
cli.update(['--hard', '--cache-save-path', tmp_file.name], standalone_mode=False)

def test_create_and_annotate_vcf(self):
tmp_file = tempfile.NamedTemporaryFile('w', delete=False)
cli.create_vcf(["-v", tmp_file.name, "--include-status", "accepted"], standalone_mode=False)

tmp_annotated_file = tempfile.NamedTemporaryFile('w', delete=False)
cli.annotate_vcf(["--input-vcf", tmp_file.name, "--output-vcf", tmp_annotated_file.name, "--reference", "GRCh37", "--include-status", "accepted"], standalone_mode=False)
26 changes: 24 additions & 2 deletions civicpy/tests/test_exports.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from civicpy import exports, civic
from civicpy import civic
from civicpy.exports import VCFWriter
import io


Expand All @@ -10,7 +11,7 @@ def vcf_stream():

@pytest.fixture(scope='function')
def vcf_writer(vcf_stream):
return exports.VCFWriter(vcf_stream)
return VCFWriter(vcf_stream)

#snv
@pytest.fixture(scope="module")
Expand Down Expand Up @@ -86,3 +87,24 @@ def test_complex_deletion(self, vcf_writer, caplog, l158fs):
assert out_dict[0]['POS'] == '10191480'
assert out_dict[0]['REF'] == 'TGAA'
assert out_dict[0]['ALT'] == 'TC'

def test_addrecord_from_gene(self, vcf_writer):
gene = civic.get_gene_by_id(24)
vcf_writer.addrecord(gene)
assert len(vcf_writer.variant_records) <= len(gene.variants)

def test_addrecord_from_evidence(self, vcf_writer):
evidence = civic._get_element_by_id('evidence', 373)
vcf_writer.addrecord(evidence)
assert len(vcf_writer.variant_records) == 1
assert list(vcf_writer.variant_records)[0].id == evidence.variant.id

def test_addrecords(self, vcf_writer, v600e, l158fs):
vcf_writer.addrecords([v600e, l158fs])
assert len(vcf_writer.variant_records) == 2

def test_addrecord_wrong_type(self, vcf_writer):
evidence = civic._get_element_by_id('evidence', 373)
with pytest.raises(ValueError) as context:
vcf_writer.addrecord(evidence.source)
assert "Expected a CIViC Gene, Variant, Assertion or Evidence record" in str(context.value)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
'pytest',
'pytest-cov',
'python-coveralls',
'coverage<5.0',
],
'docs': [
'sphinx',
Expand Down

0 comments on commit df01df1

Please sign in to comment.