add pybind surport
This commit is contained in:
parent
9fdacbf43c
commit
5ab0bc15bc
1
dna.pyi
Normal file
1
dna.pyi
Normal file
@ -0,0 +1 @@
|
||||
def dna_reverse(input_file_path: str, output_file_path: str):...
|
5
py_example.py
Normal file
5
py_example.py
Normal file
@ -0,0 +1,5 @@
|
||||
import dna
|
||||
|
||||
help(dna)
|
||||
|
||||
dna.dna_reverse("filteredReads.txt","reversedSequence.txt")
|
79
src/main.cpp
79
src/main.cpp
@ -1,5 +1,12 @@
|
||||
#include "dna.hpp"
|
||||
|
||||
#ifdef DNA_IS_PYBIND
|
||||
#include <pybind11/pybind11.h>
|
||||
// #include <pybind11/stl_bind.h>
|
||||
namespace py = pybind11;
|
||||
#endif
|
||||
|
||||
|
||||
//注意,这个函数会被并行执行,请只访问begin<=i<end之间的内容,以免出现数据竞争导致程序异常
|
||||
void reverseComplement(char *begin, char *end)
|
||||
{
|
||||
@ -14,7 +21,7 @@ void reverseComplement(char *begin, char *end)
|
||||
std::reverse(begin, end); //翻转DNA序列
|
||||
|
||||
for (ptrdiff_t i = 0; i < (end - begin); ++i) {
|
||||
static int _ = (zt::print(NAME_VALUE(omp_get_num_threads()),"\n"),0); // 打印线程数量
|
||||
// static int _ = (zt::print(NAME_VALUE(omp_get_num_threads()),"\n"),0); // 打印线程数量
|
||||
auto it = complement.find(begin[i]);
|
||||
if (it != complement.end()) {
|
||||
begin[i] = it->second;
|
||||
@ -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
|
19
xmake.lua
19
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")
|
||||
|
Loading…
Reference in New Issue
Block a user