diff --git a/src/hgvs/pretty/datacompiler.py b/src/hgvs/pretty/datacompiler.py index 95781ed3..45e770d2 100644 --- a/src/hgvs/pretty/datacompiler.py +++ b/src/hgvs/pretty/datacompiler.py @@ -187,7 +187,7 @@ def data( seq_end = display_end if var_c_or_n is not None: - tx_ac = var_c_or_n.ac + tx_ac = var_c_or_n.ac else: tx_ac = "" # can't show transcript , since there is none. @@ -228,7 +228,7 @@ def data( var_p = None reference_data = None - position_details:List[PositionDetail] = [] + position_details: List[PositionDetail] = [] prev_mapped_pos = None prev_c_pos = -1 prev_n_pos = -1 diff --git a/src/hgvs/pretty/renderer/prot_mapping_renderer.py b/src/hgvs/pretty/renderer/prot_mapping_renderer.py index 9de2316c..decb0243 100644 --- a/src/hgvs/pretty/renderer/prot_mapping_renderer.py +++ b/src/hgvs/pretty/renderer/prot_mapping_renderer.py @@ -13,21 +13,20 @@ def display(self, data: VariantData) -> str: var_str = "" - count = -1 + count = -1 for pdata in data.position_details: count += 1 if not pdata.mapped_pos: - var_str += " " + var_str += " " continue prot_data = pdata.protein_data if not prot_data or prot_data.aa_pos < 0: - var_str +=" " + var_str += " " continue - - if len(var_str) > count: + if len(var_str) > count: continue aa_pos = prot_data.aa_pos @@ -39,7 +38,6 @@ def display(self, data: VariantData) -> str: var_str += "." continue - var_str += " " return var_str diff --git a/src/hgvs/pretty/renderer/prot_ruler_renderer.py b/src/hgvs/pretty/renderer/prot_ruler_renderer.py index a31433e2..3bcc48a4 100644 --- a/src/hgvs/pretty/renderer/prot_ruler_renderer.py +++ b/src/hgvs/pretty/renderer/prot_ruler_renderer.py @@ -28,19 +28,18 @@ def display(self, data: VariantData) -> str: prev_aa = -1 continue - if len(var_str) > count: continue aa_pos = prot_data.aa_pos - if aa_pos == prev_aa : - var_str += ' ' + if aa_pos == prev_aa: + var_str += " " continue prev_aa = aa_pos - if (aa_pos + 1) % 10 == 0: + if (aa_pos + 1) % 10 == 0: var_str += f"{aa_pos+1} " continue diff --git a/src/hgvs/pretty/renderer/tx_alig_renderer.py b/src/hgvs/pretty/renderer/tx_alig_renderer.py index 27c4a948..90418742 100644 --- a/src/hgvs/pretty/renderer/tx_alig_renderer.py +++ b/src/hgvs/pretty/renderer/tx_alig_renderer.py @@ -59,11 +59,11 @@ def display(self, data: VariantData) -> str: coding = True - if cig == "=": + if cig == "=": if c_pos: - bg_col = math.ceil((c_pos+1) / 3) % 2 + bg_col = math.ceil((c_pos + 1) / 3) % 2 elif n_pos: - bg_col = math.ceil((n_pos+1) / 3) % 2 + bg_col = math.ceil((n_pos + 1) / 3) % 2 else: var_str += " " continue diff --git a/src/hgvs/pretty_print.py b/src/hgvs/pretty_print.py index 96328ae2..adcb1e6d 100644 --- a/src/hgvs/pretty_print.py +++ b/src/hgvs/pretty_print.py @@ -16,8 +16,9 @@ from hgvs.pretty.renderer.tx_mapping_renderer import TxMappingRenderer from hgvs.pretty.renderer.tx_pos import TxRulerRenderer from hgvs.pretty.renderer.tx_ref_disagree_renderer import TxRefDisagreeRenderer -from hgvs.sequencevariant import SequenceVariant from hgvs.repeats import RepeatAnalyser +from hgvs.sequencevariant import SequenceVariant + TGREEN = "\033[32m" # Green Text TGREENBG = "\033[30;42m" TRED = "\033[31m" # Red Text @@ -131,9 +132,10 @@ def _colorize_hgvs(self, hgvs_str: str) -> str: return var_str + def get_hgvs_names( + self, sv: SequenceVariant, tx_ac: str = None + ) -> Tuple[SequenceVariant, SequenceVariant]: - def get_hgvs_names(self, sv: SequenceVariant, tx_ac:str =None)->Tuple[SequenceVariant, SequenceVariant]: - var_c_or_n = None if sv.type == "g": var_g = sv @@ -146,7 +148,7 @@ def get_hgvs_names(self, sv: SequenceVariant, tx_ac:str =None)->Tuple[SequenceVa # map back to genome var_g = self._map_to_chrom(sv) var_c_or_n = sv - + return var_g, var_c_or_n def display( @@ -158,7 +160,7 @@ def display( ) -> str: """Takes a variant and prints the genomic context around it.""" - var_g, var_c_or_n = self.get_hgvs_names(sv,tx_ac) + var_g, var_c_or_n = self.get_hgvs_names(sv, tx_ac) self.data_compiler = DataCompiler(config=self.config) @@ -180,7 +182,9 @@ def display( if self.config.infer_hgvs_c: var_c_or_n = self._infer_hgvs_c(var_g) - return self.create_repre(var_g, var_c_or_n, display_start, display_end, self.data_compiler) + return self.create_repre( + var_g, var_c_or_n, display_start, display_end, self.data_compiler + ) def create_repre( self, @@ -197,7 +201,7 @@ def create_repre( fully_justified_var = data.fully_justified if self.config.showLegend: - head = "hgvs_g : " + head = "hgvs_g : " head_c = "hgvs_c : " head_n = "hgvs_n : " head_p = "hgvs_p : " @@ -216,9 +220,9 @@ def create_repre( var_c_print = self._colorize_hgvs(str(var_c_or_n)) else: var_c_print = str(var_c_or_n) - if data.var_c_or_n.type == 'c': - var_str += head_c - elif data.var_c_or_n.type == 'n': + if data.var_c_or_n.type == "c": + var_str += head_c + elif data.var_c_or_n.type == "n": var_str += head_n var_str += var_c_print + "\n" @@ -268,7 +272,7 @@ def create_repre( ) fully_justified_str = fully_justified_renderer.display(data) - if self.config.showAllShuffleableRegions : + if self.config.showAllShuffleableRegions: var_str += shuffled_seq_header + left_shuffled_str + "\n" var_str += shuffled_seq_header + right_shuffled_str + "\n" var_str += shuffled_seq_header + fully_justified_str + "\n" @@ -276,7 +280,14 @@ def create_repre( else: var_str += shuffled_seq_header + left_shuffled_str + "\n" - renderers_cls = [TxAligRenderer, TxMappingRenderer, TxRulerRenderer, ProtSeqRenderer, ProtMappingRenderer, ProtRulerRenderer ] + renderers_cls = [ + TxAligRenderer, + TxMappingRenderer, + TxRulerRenderer, + ProtSeqRenderer, + ProtMappingRenderer, + ProtRulerRenderer, + ] for cls in renderers_cls: renderer = cls(self.config, data.strand) diff --git a/src/hgvs/repeats.py b/src/hgvs/repeats.py index cdfb511e..c3cc146e 100644 --- a/src/hgvs/repeats.py +++ b/src/hgvs/repeats.py @@ -12,15 +12,16 @@ def count_pattern_occurence(pattern, string): matches = re.findall(pattern, string) return len(matches) + def count_repetitive_units(s): length = len(s) - + for i in range(1, length + 1): unit = s[:i] if length % i == 0: if unit * (length // i) == s: return length // i, unit - + return 1, s @@ -28,7 +29,6 @@ class RepeatAnalyser: def __init__(self, fs: VariantCoords) -> None: - self.is_repeat = False self.ref_count = 0 self.alt_count = 0 diff --git a/tests/test_pretty_print.py b/tests/test_pretty_print.py index 06b196a6..d94f8633 100644 --- a/tests/test_pretty_print.py +++ b/tests/test_pretty_print.py @@ -164,7 +164,7 @@ def test_insertion(self): + " : 1,643,270 1,643,280 1,643,290 1,643,300 1,643,310\n" + "chrom pos : . | . | . | . | . | \n" + "seq -> : TCACTGGGGTGTCATCCTCATCGTCATCTTCGTAATTGAGGGAGCAAA\n" - + "region : |------| \n" + + "region : |------| \n" + "tx seq <- : AGTGACCCCACAGTAGGAGTAGCAGTAGAAGCATTAACTCCCTCGTTT\n" + "tx pos : | . | . | . | . | .\n" + " : 950 940 930 920 910\n" @@ -191,7 +191,7 @@ def test_insertion_size_1(self): + " : 36,561,650 36,561,670\n" + "chrom pos : . | . | . | . | \n" + "seq -> : ACCTCGTTGGGGTGGAATCCATCCACGGGCTCGATGAGCT\n" - + "region : ^^ \n" + + "region : ^^ \n" + "tx seq <- : GAGCAACCCCACCTTAGGTAGGTGCCCGAGCTACTCGA\n" + "tx pos : | . | . | . | \n" + " : 1500 1490 1480 1470\n" @@ -217,7 +217,7 @@ def test_del_2bp(self): + " : 36,561,650 36,561,670\n" + "chrom pos : . | . | . | . | \n" + "seq -> : TACCTCGTTGGGGTGGAATCCATCCACGGGCTCGATGAGCTG\n" - + "region : xx \n" + + "region : xx \n" + "tx seq <- : GAGCAACCCCACCTTAGGTAGGTGCCCGAGCTACTCGAC\n" + "tx pos : | . | . | . | .\n" + " : 1500 1490 1480 1470\n" @@ -243,7 +243,7 @@ def test_del_1bp_shuffleable(self): + " : 36,561,650 36,561,670\n" + "chrom pos : . | . | . | . | \n" + "seq -> : TTACCTCGTTGGGGTGGAATCCATCCACGGGCTCGATGAGCT\n" - + "region : xx \n" + + "region : xx \n" + "tx seq <- : GAGCAACCCCACCTTAGGTAGGTGCCCGAGCTACTCGA\n" + "tx pos : | . | . | . | \n" + " : 1500 1490 1480 1470\n" @@ -264,7 +264,7 @@ def test_del_1bp(self): result = result.split("\n") expected_str = ( - "hgvs_g : NC_000007.13:g.36561663del\n" + "hgvs_g : NC_000007.13:g.36561663del\n" + "hgvs_c : NM_001177507.2:c.1485del\n" + "hgvs_p : NP_001170978.1:p.(Asp495GlufsTer123)\n" + " : 36,561,650 36,561,670\n" @@ -329,7 +329,7 @@ def test_dup_1bp(self): + " : 1500 1490 1480 1470\n" + "aa seq <- : GluAsnProHisPheGlyAspValProGluIleLeuGln\n" + "aa pos : ||| ... ||| \n" - + " : 500 490 \n" + + " : 500 490 \n" + "ref>alt : A>AA\n" ).split("\n") for r, e in zip(result, expected_str): @@ -351,7 +351,7 @@ def test_identity(self): + " : 36,561,650 36,561,670\n" + "chrom pos : . | . | . | . | \n" + "seq -> : ACCTCGTTGGGGTGGAATCCATCCACGGGCTCGATGAGCTG\n" - + "region : = \n" + + "region : = \n" + "tx seq <- : GAGCAACCCCACCTTAGGTAGGTGCCCGAGCTACTCGAC\n" + "tx pos : | . | . | . | .\n" + " : 1500 1490 1480 1470\n" @@ -423,7 +423,7 @@ def test_hgvs_c(self): + " 33,049,650 33,049,670 33,049,690 33,049,710 33,049,730 33,049,750 33,049,770 33,049,790\n" + " . | . | . | . | . | . | . | . | . | . | . | . | . | . | . |\n" + "CTGGGGCGCCGGGGGCTGCCATGGGGCCGGTGGGGGCGACCGAGCTGCTCGCCTGCCTCTGGACTCGCGGGCGAAGCCGCCACGGAGCTGGGGGCGCTGGCGCGAGCCCCGCCCCGCTCGAGTCCGGCCCCGCCCCTGGCCCGCCCC\n" - + " |-------------------------| \n" + + " |-------------------------| \n" + "GACCCCGCGGCCCCCGACGGTAccccggccacccccgctggctcgacgagcggacggagacctgagcgcccgcttcggcggtgcctcgacccccgcgaccgcgctcggggcggggcgagctcaggccggggcgggga \n" + " | . | . | . | . | . | . | . | . | . | . | . | . | . | . \n" + " 20 10 1 -10 -20 -30 -40 -50 -60 -70 -80 -90 -100 -110\n" @@ -591,7 +591,7 @@ def test_rna_coding(self): + "chrom pos : | . | . | . | . |\n" + "seq -> : TGCTGATCTTTGGATGTTCTGGTTAGTCTAAGAAGGAGAGT\n" + "seq <- : ACGACTAGAAACCTACAAGACCAATCAGATTCTTCCTCTCA\n" - + "region : A \n" + + "region : A \n" + "tx seq -> : GATGTTCTGGTTAGTCTAAGAAGGAGAGT\n" + "tx pos : . | . | . |\n" + " : 10 20 30\n" diff --git a/tests/test_repeats.py b/tests/test_repeats.py index df28c562..74c37b7f 100644 --- a/tests/test_repeats.py +++ b/tests/test_repeats.py @@ -1,69 +1,60 @@ -import os -from unittest import TestCase +import unittest -from hgvs.pretty.datacompiler import DataCompiler -from hgvs.pretty.models import PrettyConfig -import pytest from parameterized import parameterized -from support import CACHE import hgvs - +import hgvs.dataproviders +from hgvs.pretty.datacompiler import DataCompiler +from hgvs.pretty.models import PrettyConfig from hgvs.repeats import RepeatAnalyser, count_repetitive_units -class TestRepeats(TestCase): - @classmethod - def setUpClass(cls): - cls.hdp = hgvs.dataproviders.uta.connect( - mode=os.environ.get("HGVS_CACHE_MODE", "run"), cache=CACHE - ) - cls.vm = hgvs.variantmapper.VariantMapper(cls.hdp) - cls.hp = hgvs.parser.Parser() +class TestRepeats(unittest.TestCase): + @classmethod + def setUpClass(self): + self.hdp = hgvs.dataproviders.uta.connect() @parameterized.expand( - [ - ('abc' , 'abc' ,1), - ('CC' , 'C' , 2), - ('GAGA' , 'GA', 2), - ('', '' , 1), - ('AAAAAB', 'AAAAAB', 1), - ('ABBCABBCABBC', 'ABBC', 3) - ] + [ + ("abc", "abc", 1), + ("CC", "C", 2), + ("GAGA", "GA", 2), + ("", "", 1), + ("AAAAAB", "AAAAAB", 1), + ("ABBCABBCABBC", "ABBC", 3), + ] ) - def test_count_repetitive_units(self, s: str, u:str, c: int): + def test_count_repetitive_units(self, s: str, u: str, c: int): observed_c, observed_u = count_repetitive_units(s) - + self.assertEqual(u, observed_u) self.assertEqual(c, observed_c) - - @parameterized.expand( [ ("NC_000019.10:g.45770205del", True, "C", 6, 5, "C[6]>C[5]"), ("NC_000019.10:g.45770204_45770205del", True, "C", 6, 4, "C[6]>C[4]"), ("NC_000019.10:g.45770205insC", True, "C", 6, 7, "C[6]>C[7]"), ("NC_000019.10:g.45770206del", False, None, 0, 0, "A>"), - ("NC_000007.13:g.36561662dup", True, "C", 2, 3, "C[2]>C[3]"), + ("NC_000007.13:g.36561662dup", True, "C", 2, 3, "C[2]>C[3]"), ] ) def test_homopolymer(self, hgvs_g, is_repeat, repeat_unit, ref_count, alt_count, s): - var_g = self.hp.parse_hgvs_variant(hgvs_g) + hp = hgvs.parser.Parser() + var_g = hp.parse_hgvs_variant(hgvs_g) config = PrettyConfig(self.hdp, None, None) dc = DataCompiler(config) fs = dc.get_shuffled_variant(var_g, 0) ra = RepeatAnalyser(fs) - + self.assertEqual(s, str(ra)) self.assertEqual(is_repeat, ra.is_repeat) self.assertEqual(repeat_unit, ra.repeat_unit) self.assertEqual(ref_count, ra.ref_count) self.assertEqual(alt_count, ra.alt_count) - @parameterized.expand( [ @@ -85,12 +76,12 @@ def test_homopolymer(self, hgvs_g, is_repeat, repeat_unit, ref_count, alt_count, ), ("NC_000005.10:g.123346517_123346518insATTA", True, "ATTA", 2, 3, "ATTA[2]>ATTA[3]"), ("NC_000005.10:g.123346522_123346525dup", True, "ATTA", 2, 3, "ATTA[2]>ATTA[3]"), - ("NC_000019.10:g.45770210_45770212del", True, 'GCA', 20, 19, "GCA[20]>GCA[19]") + ("NC_000019.10:g.45770210_45770212del", True, "GCA", 20, 19, "GCA[20]>GCA[19]"), ] ) def test_repeats(self, hgvs_g, is_repeat, repeat_unit, ref_count, alt_count, s): - - var_g = self.hp.parse_hgvs_variant(hgvs_g) + hp = hgvs.parser.Parser() + var_g = hp.parse_hgvs_variant(hgvs_g) config = PrettyConfig(self.hdp, None, None) dc = DataCompiler(config) fs = dc.get_shuffled_variant(var_g, 0)