Skip to content

Commit

Permalink
revert reformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jdidion committed Feb 22, 2024
1 parent 4f85bc5 commit 578b685
Show file tree
Hide file tree
Showing 16 changed files with 215 additions and 53 deletions.
20 changes: 16 additions & 4 deletions fgpyo/fasta/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ 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 @@ -144,7 +147,10 @@ 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 @@ -156,7 +162,10 @@ 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 @@ -181,7 +190,10 @@ 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: 18 additions & 4 deletions fgpyo/fasta/tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ 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 @@ -53,10 +60,17 @@ 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: 22 additions & 5 deletions fgpyo/io/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,33 @@ 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 @@ -107,7 +121,10 @@ def test_writer(suffix: str, expected: Any,) -> None:
"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: 10 additions & 2 deletions fgpyo/sam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,11 @@ 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 @@ -879,7 +883,11 @@ def write_to(self, writer: SamFile, primary_only: bool = False,) -> None:
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: 3 additions & 1 deletion fgpyo/sam/tests/test_sam.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ 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: 3 additions & 1 deletion fgpyo/sam/tests/test_template_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ 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: 37 additions & 5 deletions fgpyo/tests/test_read_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,24 @@ 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 @@ -43,8 +58,24 @@ 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 @@ -67,7 +98,8 @@ 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: 32 additions & 7 deletions fgpyo/util/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ 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 @@ -112,7 +114,11 @@ 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 @@ -128,7 +134,11 @@ 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 @@ -141,7 +151,12 @@ 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 @@ -170,8 +185,16 @@ 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 @@ -231,7 +254,9 @@ 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: 15 additions & 4 deletions fgpyo/util/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ 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 @@ -145,7 +147,10 @@ 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 @@ -159,7 +164,11 @@ def record_alignment(self, rec: AlignedSegment,) -> bool:
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 @@ -178,7 +187,9 @@ def _log(self, refname: Optional[str] = None, position: Optional[int] = None,) -

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: 6 additions & 1 deletion fgpyo/util/tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ 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 578b685

Please sign in to comment.