Skip to content

Commit

Permalink
Format code with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
JureZmrzlikar committed Nov 4, 2024
1 parent bb262b0 commit ba7a44a
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/ngs_test_utils/testcase/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test case."""

import os
import tempfile
import unittest
Expand Down
17 changes: 10 additions & 7 deletions src/ngs_test_utils/testcase/bam.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[2] http://pysam.readthedocs.io/en/stable/usage.html#creating-bam-cram-sam-files-from-scratch
"""

import os
import random

Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/ngs_test_utils/testcase/bed.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test utilities for BED files."""

import pybedtools as pbt


Expand Down
13 changes: 8 additions & 5 deletions src/ngs_test_utils/testcase/fasta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test utilities for FASTA files."""

import os

import numpy
Expand All @@ -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)
Expand All @@ -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")

Expand Down
5 changes: 3 additions & 2 deletions src/ngs_test_utils/testcase/fastq.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test utilities for FASTQ files."""

import numpy

from ngs_test_utils.base.fastq import FastqEntry
Expand All @@ -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."""
Expand All @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion src/ngs_test_utils/testcase/gtf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test utilities for GTF files."""

import pybedtools as pbt


Expand All @@ -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:
Expand Down
4 changes: 1 addition & 3 deletions tests/test_bam.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import unittest

import pysam

from ngs_test_utils import testcase
Expand Down Expand Up @@ -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
15 changes: 11 additions & 4 deletions tests/test_fasta.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import unittest

from ngs_test_utils import testcase


Expand All @@ -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"],
],
)
4 changes: 3 additions & 1 deletion tests/test_fastq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]
)

0 comments on commit ba7a44a

Please sign in to comment.