diff --git a/tests/conftest.py b/tests/conftest.py index 5bbaa8e..a15735e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,14 @@ import os +from Bio import SeqIO +from Bio.SeqRecord import SeqRecord +from Bio.Seq import Seq + import numpy as np import pyBigWig import pytest +import pyfaidx +import random @pytest.fixture(scope="session") @@ -11,6 +17,46 @@ def momics_path(tmp_path_factory): return p +@pytest.fixture(scope="session") +def fa1(tmp_path_factory): + p = os.path.join(tmp_path_factory.getbasetemp(), "fa1") + nucleotides = ["A", "T", "C", "G"] + chrom_seqs = { + "I": "".join(random.choices(nucleotides, k=10000)), + "II": "".join(random.choices(nucleotides, k=20000)), + "III": "".join(random.choices(nucleotides, k=30000)), + } + records = [] + for chrom, sequence in chrom_seqs.items(): + record = SeqRecord(Seq(sequence), id=chrom, description="") + records.append(record) + + with open(p, "w") as output_handle: + SeqIO.write(records, output_handle, "fasta") + + return p + + +@pytest.fixture(scope="session") +def fa2(tmp_path_factory): + p = os.path.join(tmp_path_factory.getbasetemp(), "fa2") + nucleotides = ["A", "T", "C", "G"] + chrom_seqs = { + "I": "".join(random.choices(nucleotides, k=2000)), + "II": "".join(random.choices(nucleotides, k=1000)), + "III": "".join(random.choices(nucleotides, k=500)), + } + records = [] + for chrom, sequence in chrom_seqs.items(): + record = SeqRecord(Seq(sequence), id=chrom, description="") + records.append(record) + + with open(p, "w") as output_handle: + SeqIO.write(records, output_handle, "fasta") + + return p + + @pytest.fixture(scope="session") def bw1(tmp_path_factory): p = os.path.join(tmp_path_factory.getbasetemp(), "bw1") diff --git a/tests/test_api.py b/tests/test_api.py index 5de9ed0..ed182b9 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -72,15 +72,38 @@ def test_Momics_add_tracks(momics_path: str, bw1: str, bw2: str): print(mom.tracks()) -def test_Momics_query(momics_path: str): +def test_Momics_add_seq(momics_path: str, fa1: str, fa2: str): mom = momics.Momics(momics_path, create=False) - q = mom.query("I:991-1010") + + with pytest.raises(Exception, match=r".*do not have identical chromomosome.*"): + mom.add_sequence(fa2) + + mom.add_sequence(fa1) + + with pytest.raises(ValueError, match=r"Sequence already added to the repository"): + mom.add_sequence(fa2) + + print(mom.sequence()) + + +def test_Momics_query_tracks(momics_path: str): + mom = momics.Momics(momics_path, create=False) + q = mom.query_tracks("I:991-1010") assert q.shape == (40, 3) - q = mom.query("I") + q = mom.query_tracks("I") assert q.shape == (20000, 3) +def test_Momics_query_seqs(momics_path: str): + mom = momics.Momics(momics_path, create=False) + q = mom.query_sequence("I:991-1010") + assert q.shape == (20,) + + q = mom.query_sequence("I") + assert q.shape == (10000,) + + def test_Momics_remove_tracks(momics_path: str, bw1: str, bw2: str): mom = momics.Momics(momics_path, create=False) mom.add_tracks({"bw3": bw1}) @@ -94,5 +117,5 @@ def test_Momics_remove_tracks(momics_path: str, bw1: str, bw2: str): } ) assert mom.tracks().__eq__(out).all().all() - q = mom.query("I:991-1010") + q = mom.query_tracks("I:991-1010") assert np.unique(q["label"]).__eq__(["bw2", "bw3", "bw4"]).all()