Skip to content
Snippets Groups Projects
Commit 26eb25ab authored by Martin Larralde's avatar Martin Larralde
Browse files

Add `unittest` for Python bindings with a Rust harness loader

parent 692be1bc
No related branches found
No related tags found
No related merge requests found
......@@ -31,4 +31,9 @@ pyo3-built = "0.4.7"
[features]
default = []
extension-module = ["pyo3/extension-module"]
nightly = ["pyo3/nightly"]
\ No newline at end of file
nightly = ["pyo3/nightly"]
[[test]]
name = "unittest"
path = "lightmotif/tests/unittest.rs"
harness = false
from . import (
test_dna
)
def load_tests(loader, suite, pattern):
suite.addTests(loader.loadTestsFromModule(test_dna))
return suite
import gzip
import os
import tempfile
import unittest
import lightmotif
SEQUENCE = "ATGTCCCAACAACGATACCCCGAGCCCATCGCCGTCATCGGCTCGGCATGCAGATTCCCAGGCG"
EXPECTED = [
-23.07094 , -18.678621 , -15.219191 , -17.745737 ,
-18.678621 , -23.07094 , -17.745737 , -19.611507 ,
-27.463257 , -29.989803 , -14.286304 , -26.53037 ,
-15.219191 , -10.826873 , -10.826873 , -22.138054 ,
-38.774437 , -30.922688 , -5.50167 , -24.003826 ,
-18.678621 , -15.219191 , -35.315006 , -17.745737 ,
-10.826873 , -30.922688 , -23.07094 , -6.4345555,
-31.855574 , -23.07094 , -15.219191 , -31.855574 ,
-8.961102 , -26.53037 , -27.463257 , -14.286304 ,
-15.219191 , -26.53037 , -23.07094 , -18.678621 ,
-14.286304 , -18.678621 , -26.53037 , -16.152077 ,
-17.745737 , -18.678621 , -17.745737 , -14.286304 ,
-30.922688 , -18.678621
]
class TestDNAMotif(unittest.TestCase):
def test_calculate(self):
counts = lightmotif.create(["GTTGACCTTATCAAC", "GTTGATCCAGTCAAC"])
frequencies = counts.normalize(0.1)
pssm = frequencies.log_odds()
seq = lightmotif.EncodedSequence(SEQUENCE)
striped = seq.stripe()
scores = pssm.calculate(striped)
self.assertEqual(len(scores), len(EXPECTED))
for x, y in zip(scores, EXPECTED):
self.assertAlmostEqual(x, y, places=5)
\ No newline at end of file
extern crate lightmotif_py;
extern crate pyo3;
use std::path::Path;
use pyo3::prelude::PyResult;
use pyo3::types::PyDict;
use pyo3::types::PyList;
use pyo3::types::PyModule;
use pyo3::Python;
pub fn main() -> PyResult<()> {
// get the relative path to the project folder
let folder = Path::new(file!())
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap();
// spawn a Python interpreter
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| {
// insert the project folder in `sys.modules` so that
// the main module can be imported by Python
let sys = py.import("sys")?;
sys.getattr("path")?
.downcast::<PyList>()?
.insert(0, folder)?;
// create a Python module from our rust code with debug symbols
let module = PyModule::new(py, "lightmotif.lib")?;
lightmotif_py::init(py, &module).unwrap();
sys.getattr("modules")?
.downcast::<PyDict>()?
.set_item("lightmotif.lib", module)?;
// run unittest on the tests
let kwargs = PyDict::new(py);
kwargs.set_item("exit", false).unwrap();
kwargs.set_item("verbosity", 2u8).unwrap();
py.import("unittest").unwrap().call_method(
"TestProgram",
("lightmotif.tests",),
Some(kwargs),
)?;
Ok(())
})
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment