Skip to content

Commit

Permalink
rever reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jdidion committed Feb 22, 2024
1 parent 6416a39 commit 4f85bc5
Show file tree
Hide file tree
Showing 16 changed files with 238 additions and 261 deletions.
21 changes: 5 additions & 16 deletions fgpyo/fasta/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def samtools_dict(*args: Any) -> None:
def samtools_faidx(*args: Any) -> None:
pass


else:
from pysam import dict as samtools_dict
from pysam import faidx as samtools_faidx
Expand Down Expand Up @@ -91,10 +92,7 @@ class ContigBuilder:
"""

def __init__(
self,
name: str,
assembly: str,
species: str,
self, name: str, assembly: str, species: str,
):
self.name = name
self.assembly = assembly
Expand Down Expand Up @@ -146,10 +144,7 @@ class FastaBuilder:
"""

def __init__(
self,
assembly: str = "testassembly",
species: str = "testspecies",
line_length: int = 80,
self, assembly: str = "testassembly", species: str = "testspecies", line_length: int = 80,
):
self.assembly: str = assembly
self.species: str = species
Expand All @@ -161,10 +156,7 @@ def __getitem__(self, key: str) -> ContigBuilder:
return self.__contig_builders[key]

def add(
self,
name: str,
assembly: Optional[str] = None,
species: Optional[str] = None,
self, name: str, assembly: Optional[str] = None, species: Optional[str] = None,
) -> ContigBuilder:
"""
Creates and returns a new ContigBuilder for a contig with the provided name.
Expand All @@ -189,10 +181,7 @@ def add(
self.__contig_builders[name] = builder
return builder

def to_file(
self,
path: Path,
) -> None:
def to_file(self, path: Path,) -> None:
"""
Writes out the set of accumulated contigs to a FASTA file at the `path` given.
Also generates the accompanying fasta index file (`.fa.fai`) and sequence
Expand Down
22 changes: 4 additions & 18 deletions fgpyo/fasta/tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,10 @@ def test_bases_length_from_ContigBuilder_add_default() -> None:


@pytest.mark.parametrize(
"name, bases, times, length_bases",
[
("chr1", "AAA", 3, 9),
("chr2", "TTT", 10, 30),
],
"name, bases, times, length_bases", [("chr1", "AAA", 3, 9), ("chr2", "TTT", 10, 30),],
)
def test_bases_length_from_ContigBuilder_add(
name: str,
bases: str,
times: int,
length_bases: int,
name: str, bases: str, times: int, length_bases: int,
) -> None:
"""Checks that the number of bases in each contig is correct"""
builder = FastaBuilder()
Expand All @@ -60,17 +53,10 @@ def test_contig_dict_is_not_accessable() -> None:

@pytest.mark.parametrize(
"name, bases, times, expected",
[
("chr3", "AAA a", 3, ("AAAA" * 3)),
("chr2", "TT T gT", 10, ("TTTGT" * 10)),
],
[("chr3", "AAA a", 3, ("AAAA" * 3)), ("chr2", "TT T gT", 10, ("TTTGT" * 10)),],
)
def test_bases_string_from_ContigBuilder_add(
name: str,
bases: str,
times: int,
expected: str,
tmp_path: Path,
name: str, bases: str, times: int, expected: str, tmp_path: Path,
) -> None:
"""
Reads bases back from fasta and checks that extra spaces are removed and bases are uppercase
Expand Down
27 changes: 5 additions & 22 deletions fgpyo/io/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,33 +84,19 @@ def test_assert_path_is_writeable_pass() -> None:


@pytest.mark.parametrize(
"suffix, expected",
[
(".gz", io.TextIOWrapper),
(".fa", io.TextIOWrapper),
],
"suffix, expected", [(".gz", io.TextIOWrapper), (".fa", io.TextIOWrapper),],
)
def test_reader(
suffix: str,
expected: Any,
) -> None:
def test_reader(suffix: str, expected: Any,) -> None:
"""Tests fgpyo.io.to_reader"""
with NamedTemp(suffix=suffix, mode="r", delete=True) as read_file:
with fio.to_reader(path=Path(read_file.name)) as reader:
assert isinstance(reader, expected)


@pytest.mark.parametrize(
"suffix, expected",
[
(".gz", io.TextIOWrapper),
(".fa", io.TextIOWrapper),
],
"suffix, expected", [(".gz", io.TextIOWrapper), (".fa", io.TextIOWrapper),],
)
def test_writer(
suffix: str,
expected: Any,
) -> None:
def test_writer(suffix: str, expected: Any,) -> None:
"""Tests fgpyo.io.to_writer()"""
with NamedTemp(suffix=suffix, mode="w", delete=True) as write_file:
with fio.to_writer(path=Path(write_file.name)) as writer:
Expand All @@ -121,10 +107,7 @@ def test_writer(
"suffix, list_to_write",
[(".txt", ["Test with a flat file", 10]), (".gz", ["Test with a gzip file", 10])],
)
def test_read_and_write_lines(
suffix: str,
list_to_write: List[Any],
) -> None:
def test_read_and_write_lines(suffix: str, list_to_write: List[Any],) -> None:
"""Test fgpyo.fio.read_lines and write_lines"""
with NamedTemp(suffix=suffix, mode="w", delete=True) as read_file:
fio.write_lines(path=Path(read_file.name), lines_to_write=list_to_write)
Expand Down
12 changes: 2 additions & 10 deletions fgpyo/sam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,11 +863,7 @@ def all_recs(self) -> Iterator[AlignedSegment]:
for rec in recs:
yield rec

def write_to(
self,
writer: SamFile,
primary_only: bool = False,
) -> None:
def write_to(self, writer: SamFile, primary_only: bool = False,) -> None:
"""Write the records associated with the template to file.
Args:
Expand All @@ -883,11 +879,7 @@ def write_to(
for rec in rec_iter:
writer.write(rec)

def set_tag(
self,
tag: str,
value: Union[str, int, float, None],
) -> None:
def set_tag(self, tag: str, value: Union[str, int, float, None],) -> None:
"""Add a tag to all records associated with the template.
Setting a tag to `None` will remove the tag.
Expand Down
4 changes: 1 addition & 3 deletions fgpyo/sam/tests/test_sam.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ def test_sam_file_open_writing(


def test_sam_file_open_writing_header_keyword(
expected_records: List[pysam.AlignedSegment],
header_dict: AlignmentHeader,
tmp_path: Path,
expected_records: List[pysam.AlignedSegment], header_dict: AlignmentHeader, tmp_path: Path,
) -> None:
# Use SamWriter
# use header as a keyword argument
Expand Down
4 changes: 1 addition & 3 deletions fgpyo/sam/tests/test_template_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ def test_to_templates() -> None:
assert len(list(template2.all_recs())) == 2


def test_write_template(
tmp_path: Path,
) -> None:
def test_write_template(tmp_path: Path,) -> None:
builder = SamBuilder()
template = Template.build(
[
Expand Down
42 changes: 5 additions & 37 deletions fgpyo/tests/test_read_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,9 @@ def _S(off: int, len: int) -> ReadSegment:
("1M", (_M(0, 1),)),
("1S", (_S(0, 1),)),
("101T", (_T(0, 101),)),
(
"5B101T",
(
_B(0, 5),
_T(5, 101),
),
),
("5B101T", (_B(0, 5), _T(5, 101),),),
("123456789T", (_T(0, 123456789),)),
(
"10T10B10B10S10M",
(
_T(0, 10),
_B(10, 10),
_B(20, 10),
_S(30, 10),
_M(40, 10),
),
),
("10T10B10B10S10M", (_T(0, 10), _B(10, 10), _B(20, 10), _S(30, 10), _M(40, 10),),),
],
)
def test_read_structure_from_string(string: str, segments: Tuple[ReadSegment, ...]) -> None:
Expand All @@ -58,24 +43,8 @@ def test_read_structure_from_string(string: str, segments: Tuple[ReadSegment, ..
@pytest.mark.parametrize(
"string,segments",
[
(
"75T 8B 8B 75T",
(
_T(0, 75),
_B(75, 8),
_B(83, 8),
_T(91, 75),
),
),
(
" 75T 8B 8B 75T ",
(
_T(0, 75),
_B(75, 8),
_B(83, 8),
_T(91, 75),
),
),
("75T 8B 8B 75T", (_T(0, 75), _B(75, 8), _B(83, 8), _T(91, 75),),),
(" 75T 8B 8B 75T ", (_T(0, 75), _B(75, 8), _B(83, 8), _T(91, 75),),),
],
)
def test_read_structure_from_string_with_whitespace(
Expand All @@ -98,8 +67,7 @@ def test_read_structure_variable_once_and_only_once_last_segment_ok(


@pytest.mark.parametrize(
"string",
["++M", "5M++T", "5M70+T", "+M+T", "+M70T"],
"string", ["++M", "5M++T", "5M70+T", "+M+T", "+M70T"],
)
def test_read_structure_variable_once_and_only_once_last_segment_exception(string: str) -> None:
with pytest.raises(Exception):
Expand Down
39 changes: 7 additions & 32 deletions fgpyo/util/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ def split_at_given_level(


def _get_parser(
cls: Type,
type_: TypeAlias,
parsers: Optional[Dict[type, Callable[[str], Any]]] = None,
cls: Type, type_: TypeAlias, parsers: Optional[Dict[type, Callable[[str], Any]]] = None,
) -> partial:
"""Attempts to find a parser for a provided type.
Expand Down Expand Up @@ -114,11 +112,7 @@ def get_parser() -> partial:
assert (
len(subtypes) == 1
), "Lists are allowed only one subtype per PEP specification!"
subtype_parser = _get_parser(
cls,
subtypes[0],
parsers,
)
subtype_parser = _get_parser(cls, subtypes[0], parsers,)
return functools.partial(
lambda s: list(
[]
Expand All @@ -134,11 +128,7 @@ def get_parser() -> partial:
assert (
len(subtypes) == 1
), "Sets are allowed only one subtype per PEP specification!"
subtype_parser = _get_parser(
cls,
subtypes[0],
parsers,
)
subtype_parser = _get_parser(cls, subtypes[0], parsers,)
return functools.partial(
lambda s: set(
set({})
Expand All @@ -151,12 +141,7 @@ def get_parser() -> partial:
)
elif typing.get_origin(type_) == tuple:
subtype_parsers = [
_get_parser(
cls,
subtype,
parsers,
)
for subtype in typing.get_args(type_)
_get_parser(cls, subtype, parsers,) for subtype in typing.get_args(type_)
]

def tuple_parse(tuple_string: str) -> Tuple[Any, ...]:
Expand Down Expand Up @@ -185,16 +170,8 @@ def tuple_parse(tuple_string: str) -> Tuple[Any, ...]:
len(subtypes) == 2
), "Dict object must have exactly 2 subtypes per PEP specification!"
(key_parser, val_parser) = (
_get_parser(
cls,
subtypes[0],
parsers,
),
_get_parser(
cls,
subtypes[1],
parsers,
),
_get_parser(cls, subtypes[0], parsers,),
_get_parser(cls, subtypes[1], parsers,),
)

def dict_parse(dict_string: str) -> Dict[Any, Any]:
Expand Down Expand Up @@ -254,9 +231,7 @@ def dict_parse(dict_string: str) -> Dict[Any, Any]:


def attr_from(
cls: Type,
kwargs: Dict[str, str],
parsers: Optional[Dict[type, Callable[[str], Any]]] = None,
cls: Type, kwargs: Dict[str, str], parsers: Optional[Dict[type, Callable[[str], Any]]] = None,
) -> Any:
"""Builds an attr class from key-word arguments
Expand Down
19 changes: 4 additions & 15 deletions fgpyo/util/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ def __exit__(
return False

def record(
self,
reference_name: Optional[str] = None,
position: Optional[int] = None,
self, reference_name: Optional[str] = None, position: Optional[int] = None,
) -> bool:
"""Record an item at a given genomic coordinate.
Args:
Expand All @@ -147,10 +145,7 @@ def record(
else:
return False

def record_alignment(
self,
rec: AlignedSegment,
) -> bool:
def record_alignment(self, rec: AlignedSegment,) -> bool:
"""Correctly record pysam.AlignedSegments (zero-based coordinates).
Args:
Expand All @@ -164,11 +159,7 @@ def record_alignment(
else:
return self.record(rec.reference_name, rec.reference_start + 1)

def _log(
self,
refname: Optional[str] = None,
position: Optional[int] = None,
) -> None:
def _log(self, refname: Optional[str] = None, position: Optional[int] = None,) -> None:
"""Helper method to print the log message.
Args:
Expand All @@ -187,9 +178,7 @@ def _log(

self.printer(f"{self.verb} {self.count:,d} {self.noun}: {coordinate}")

def log_last(
self,
) -> bool:
def log_last(self,) -> bool:
"""Force logging the last record, for example when progress has completed."""
if self._count_mod_unit != 0:
self._log(refname=self._last_reference_name, position=self._last_position)
Expand Down
7 changes: 1 addition & 6 deletions fgpyo/util/tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ def test_progress_logger_as_context_manager() -> None:


@pytest.mark.parametrize(
"record",
[
(r1_mapped_named),
(r2_unmapped_named),
(r2_unmapped_un_named),
],
"record", [(r1_mapped_named), (r2_unmapped_named), (r2_unmapped_un_named),],
)
def test_record_alignment_mapped_record(record: pysam.AlignedSegment) -> None:
# Define instance of ProgressLogger
Expand Down
Loading

0 comments on commit 4f85bc5

Please sign in to comment.