Skip to content

Commit

Permalink
test: support API for sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
js2264 committed Sep 18, 2024
1 parent f29c8df commit 86ac6a3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
46 changes: 46 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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")
Expand Down
31 changes: 27 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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()

0 comments on commit 86ac6a3

Please sign in to comment.