From ba7a44af0a1c8c0a48da31242918bbbd821d95ff Mon Sep 17 00:00:00 2001 From: JureZmrzlikar Date: Mon, 4 Nov 2024 12:17:25 +0100 Subject: [PATCH] Format code with ruff --- src/ngs_test_utils/testcase/__init__.py | 1 + src/ngs_test_utils/testcase/bam.py | 17 ++++++++++------- src/ngs_test_utils/testcase/bed.py | 1 + src/ngs_test_utils/testcase/fasta.py | 13 ++++++++----- src/ngs_test_utils/testcase/fastq.py | 5 +++-- src/ngs_test_utils/testcase/gtf.py | 3 ++- tests/test_bam.py | 4 +--- tests/test_fasta.py | 15 +++++++++++---- tests/test_fastq.py | 4 +++- 9 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/ngs_test_utils/testcase/__init__.py b/src/ngs_test_utils/testcase/__init__.py index de487f6..2b144c8 100644 --- a/src/ngs_test_utils/testcase/__init__.py +++ b/src/ngs_test_utils/testcase/__init__.py @@ -1,4 +1,5 @@ """Test case.""" + import os import tempfile import unittest diff --git a/src/ngs_test_utils/testcase/bam.py b/src/ngs_test_utils/testcase/bam.py index a23f9ee..a45994b 100644 --- a/src/ngs_test_utils/testcase/bam.py +++ b/src/ngs_test_utils/testcase/bam.py @@ -5,6 +5,7 @@ [2] http://pysam.readthedocs.io/en/stable/usage.html#creating-bam-cram-sam-files-from-scratch """ + import os import random @@ -72,8 +73,7 @@ def make_bam_segment( tags=None, **kwargs, ): - """ - Return pysam.AlignedSegment object. + """Return pysam.AlignedSegment object. Each pysam.AlignedSegment element has 11 mandatory tab-separated fields (qname, flag, rname, pos, mapq, cigar, rnext, pnext, @@ -87,7 +87,7 @@ def make_bam_segment( segment = pysam.AlignedSegment() if qname is None: - qname = "read-{}".format(random.randrange(1000, 9999)) + qname = f"read-{random.randrange(1000, 9999)}" segment.query_name = qname segment.flag = flag or self.get_flag_value(**kwargs) @@ -100,13 +100,17 @@ def make_bam_segment( segment.next_reference_start = pnext segment.template_length = tlen - length = sum([length for (operation, length) in segment.cigartuples if operation in [0, 1, 4, 7, 8]]) + length = sum( + [length for (operation, length) in segment.cigartuples if operation in [0, 1, 4, 7, 8]] + ) if seq is None: seq = FastaTestCaseMixin.make_fasta_sequence(size=length, include_n=False) segment.query_sequence = seq if qual is None: - qual = pysam.qualitystring_to_array(FastqTestCaseMixin.make_quality_scores(size=length)) + qual = pysam.qualitystring_to_array( + FastqTestCaseMixin.make_quality_scores(size=length) + ) segment.query_qualities = qual if tags is not None: @@ -115,8 +119,7 @@ def make_bam_segment( return segment def make_bam(self, chroms, segments): - """ - Make a synthetic BAM file. + """Make a synthetic BAM file. Each BAM file consists of header and alignment entries. diff --git a/src/ngs_test_utils/testcase/bed.py b/src/ngs_test_utils/testcase/bed.py index cc0931b..eee4396 100644 --- a/src/ngs_test_utils/testcase/bed.py +++ b/src/ngs_test_utils/testcase/bed.py @@ -1,4 +1,5 @@ """Test utilities for BED files.""" + import pybedtools as pbt diff --git a/src/ngs_test_utils/testcase/fasta.py b/src/ngs_test_utils/testcase/fasta.py index 5caf08f..a04421c 100644 --- a/src/ngs_test_utils/testcase/fasta.py +++ b/src/ngs_test_utils/testcase/fasta.py @@ -1,4 +1,5 @@ """Test utilities for FASTA files.""" + import os import numpy @@ -16,7 +17,9 @@ def make_fasta_sequence(size, include_n=False, rnd_seed=None): bases = BASES + ["N"] if include_n else BASES return "".join(numpy.random.choice(bases, size)) - def make_fasta(self, sequences=None, headers=None, num_sequences=10, seq_len=80, rnd_seed=None): + def make_fasta( + self, sequences=None, headers=None, num_sequences=10, seq_len=80, rnd_seed=None + ): """Make FASTA file.""" if sequences and headers is None: num_sequences = len(sequences) @@ -25,14 +28,14 @@ def make_fasta(self, sequences=None, headers=None, num_sequences=10, seq_len=80, numpy.random.seed(rnd_seed) if sequences is None: - random_seeds = numpy.random.randint(10 ** 5, size=num_sequences) + random_seeds = numpy.random.randint(10**5, size=num_sequences) sequences = [self.make_fasta_sequence(seq_len, rnd_seed=rnd) for rnd in random_seeds] if headers is None: - headers = ["{}".format(i + 1) for i in range(num_sequences)] + headers = [f"{i + 1}" for i in range(num_sequences)] out_file = self.get_filename(extension="fasta") - with open(out_file, "wt") as ofile: - for header, seq in zip(headers, sequences): + with open(out_file, "w") as ofile: + for header, seq in zip(headers, sequences, strict=False): ofile.write(">" + header + "\n") ofile.write(seq + "\n") diff --git a/src/ngs_test_utils/testcase/fastq.py b/src/ngs_test_utils/testcase/fastq.py index 6712599..2f52cac 100644 --- a/src/ngs_test_utils/testcase/fastq.py +++ b/src/ngs_test_utils/testcase/fastq.py @@ -1,4 +1,5 @@ """Test utilities for FASTQ files.""" + import numpy from ngs_test_utils.base.fastq import FastqEntry @@ -19,7 +20,7 @@ def make_sequence_id(self, rnd_seed=None): """Make sequence ID.""" if rnd_seed: numpy.random.seed(rnd_seed) - return "random_sequence_{}".format(numpy.random.randint(0, 10 ** 4)) + return f"random_sequence_{numpy.random.randint(0, 10**4)}" def make_fastq_entry(self, seq, seq_id=None, plus="+", quality=None, rnd_seed=None): """Make FASTQ entry.""" @@ -35,7 +36,7 @@ def make_fastq(self, entries): """Make FASTQ file from entries.""" filename = self.get_filename(extension="fastq") - with open(filename, "wt") as ofile: + with open(filename, "w") as ofile: for entry in entries: entry.write(ofile) diff --git a/src/ngs_test_utils/testcase/gtf.py b/src/ngs_test_utils/testcase/gtf.py index f8e8cbd..c93ea4a 100644 --- a/src/ngs_test_utils/testcase/gtf.py +++ b/src/ngs_test_utils/testcase/gtf.py @@ -1,4 +1,5 @@ """Test utilities for GTF files.""" + import pybedtools as pbt @@ -9,7 +10,7 @@ def create_gtf_attrs(self, **kwargs): """Create 9th column in GTF file - attributes.""" attrs = "" for name, value in kwargs.items(): - attrs += '{} "{}"; '.format(name, value) + attrs += f'{name} "{value}"; ' attrs = attrs.strip() if attrs: diff --git a/tests/test_bam.py b/tests/test_bam.py index 380d0b3..a6102b7 100644 --- a/tests/test_bam.py +++ b/tests/test_bam.py @@ -1,5 +1,3 @@ -import unittest - import pysam from ngs_test_utils import testcase @@ -36,5 +34,5 @@ def test_make_bam(self): self.assertEqual(bamfile.references, ("chr1", "chr2")) self.assertEqual(bamfile.mapped, 2) self.assertEqual(bamfile.unmapped, 1) - for read in bamfile.fetch(): + for _ in bamfile.fetch(): pass diff --git a/tests/test_fasta.py b/tests/test_fasta.py index b05c1ce..c1e3082 100644 --- a/tests/test_fasta.py +++ b/tests/test_fasta.py @@ -1,5 +1,3 @@ -import unittest - from ngs_test_utils import testcase @@ -15,8 +13,17 @@ def test_make_fasta_sequence(self): def test_make_fasta(self): # headers=None, sequences=None - fasta = self.make_fasta(sequences=None, headers=None, num_sequences=3, seq_len=20, rnd_seed=42) + fasta = self.make_fasta( + sequences=None, headers=None, num_sequences=3, seq_len=20, rnd_seed=42 + ) self.assertEqual( self.tsv_to_list(fasta), - [[">1"], ["CATTATGCTCGATCAGGCCG"], [">2"], ["CTTTACGCCTTTCTTTGATG"], [">3"], ["CTTTGGGGCACTTAACTCCC"]], + [ + [">1"], + ["CATTATGCTCGATCAGGCCG"], + [">2"], + ["CTTTACGCCTTTCTTTGATG"], + [">3"], + ["CTTTGGGGCACTTAACTCCC"], + ], ) diff --git a/tests/test_fastq.py b/tests/test_fastq.py index c2ebc5c..b79cd20 100644 --- a/tests/test_fastq.py +++ b/tests/test_fastq.py @@ -23,4 +23,6 @@ def test_make_fastq_entry(self): def test_make_fastq_file(self): entry = self.make_fastq_entry(seq="ACTG", rnd_seed=2) filename = self.make_fastq([entry]) - self.assertEqual(self.tsv_to_list(filename), [["random_sequence_7336"], ["ACTG"], ["+"], ["I0)7"]]) + self.assertEqual( + self.tsv_to_list(filename), [["random_sequence_7336"], ["ACTG"], ["+"], ["I0)7"]] + )