From 5ab0bc15bc4dc1a241408bf42cb9289d1c5a71a2 Mon Sep 17 00:00:00 2001 From: Zengtudor Date: Sat, 21 Sep 2024 22:32:34 +0800 Subject: [PATCH] add pybind surport --- dna.pyi | 1 + py_example.py | 5 ++++ src/main.cpp | 79 ++++++++++++++++++++++++++++++++++++++------------- xmake.lua | 19 ++++++++++++- 4 files changed, 83 insertions(+), 21 deletions(-) create mode 100644 dna.pyi create mode 100644 py_example.py diff --git a/dna.pyi b/dna.pyi new file mode 100644 index 0000000..dd5a57d --- /dev/null +++ b/dna.pyi @@ -0,0 +1 @@ +def dna_reverse(input_file_path: str, output_file_path: str):... \ No newline at end of file diff --git a/py_example.py b/py_example.py new file mode 100644 index 0000000..2d82acc --- /dev/null +++ b/py_example.py @@ -0,0 +1,5 @@ +import dna + +help(dna) + +dna.dna_reverse("filteredReads.txt","reversedSequence.txt") \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 2fadc05..5094479 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,12 @@ #include "dna.hpp" +#ifdef DNA_IS_PYBIND +#include +// #include +namespace py = pybind11; +#endif + + //注意,这个函数会被并行执行,请只访问begin<=isecond; @@ -22,27 +29,59 @@ void reverseComplement(char *begin, char *end) } } +#ifndef DNA_IS_PYBIND int main() { try{ - //原理是这里定义处理函数,将函数传入dna::open_file_and_calculate,在open_file_and_calculate中会调用传入的函数 - //参数列表 <文件分块内存大小,单个DNA序列最长大小>("输入文件名","输出文件名",序列处理函数); - //这个函数在src/tools/dna里面 - dna::open_file_and_calculate<(size_t)4*1024 * 1024 *1024 , (size_t)5e4+5>("filteredReads.txt", "reversedSequence.txt",reverseComplement); - // dna::open_file_and_calculate<(size_t)30, (size_t)5e4 + 5>("test.txt", "test.out.txt", reverseComplement); + //原理是这里定义处理函数,将函数传入dna::open_file_and_calculate,在open_file_and_calculate中会调用传入的函数 + //参数列表 <文件分块内存大小,单个DNA序列最长大小>("输入文件名","输出文件名",序列处理函数); + //这个函数在src/tools/dna里面 + dna::open_file_and_calculate<(size_t)4*1024 * 1024 *1024 , (size_t)5e4+5>("filteredReads.txt", "reversedSequence.txt",reverseComplement); + // dna::open_file_and_calculate<(size_t)30, (size_t)5e4 + 5>("test.txt", "test.out.txt", reverseComplement); - }catch(const std::exception &e){ - zt::eprint( - "Caught an error because:\n", - "\t",NAME_VALUE(e.what()),"\n" - "Closing\n" - ); - throw e; - }catch(...){ - zt::eprint( - "Caught an unknown error :\n", - "Closing\n" - ); - throw; - } + }catch(const std::exception &e){ + zt::eprint( + "Caught an error because:\n", + "\t",NAME_VALUE(e.what()),"\n" + "Closing\n" + ); + throw e; + }catch(...){ + zt::eprint( + "Caught an unknown error :\n", + "Closing\n" + ); + throw; + } } + +#else + +void bind_func(std::string input_file_name,std::string out_put_file_name){ + try{ + dna::open_file_and_calculate<(size_t)4*1024 * 1024 *1024 , (size_t)5e4+5>(input_file_name, out_put_file_name,reverseComplement); + // dna::open_file_and_calculate<(size_t)1 * 1024 *1024 , (size_t)5e4+5>(input_file_name, out_put_file_name,reverseComplement); + }catch(const std::exception &e){ + zt::eprint( + "Caught an error because:\n", + "\t",NAME_VALUE(e.what()),"\n" + "Closing\n" + ); + throw e; + }catch(...){ + zt::eprint( + "Caught an unknown error :\n", + "Closing\n" + ); + throw; + } +} + +PYBIND11_MODULE(dna, m){ + m.doc() = "DNASequence processing functions"; // optional module docstring + + m.def("dna_reverse", &bind_func, "DNA is double-stranded and complementary to each other, and when sequencing a DNA sample you can't be sure which strand is being measured, so the complementary strands of all the DNA fragments are counted and assembled together with the original file." + ,py::arg("input_file_path"),py::arg("output_file_path")); +} + +#endif \ No newline at end of file diff --git a/xmake.lua b/xmake.lua index 4bc8bf7..30aa8b0 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,6 +1,8 @@ add_rules("mode.debug","mode.release") set_languages("c++23") +add_requires("pybind11") + if is_mode("release")then set_optimize("aggressive") --这里使用了激进的优化,可能会导致浮点数计算不准确,考虑到本项目没有浮点计算,酌情考虑开启 -- set_optimize("fastest") --上面和下面的二选一,--表示注释 @@ -24,5 +26,20 @@ end add_includedirs("src/tools") set_rundir("./") -target("dna") +target("dna_pybind") + add_defines("DNA_IS_PYBIND") + add_packages("pybind11") + set_kind("shared") + set_extension(".pyd") + add_files("src/main.cpp") + set_basename("dna") + after_build( + function (target) + print(target:targetdir()) + os.cp("*.py*",target:targetdir()) + end + ) + +target("dna") + set_kind("binary") add_files("src/main.cpp")