#include #include #include #include #include #include #include #include #include #include #include"tools.hpp" // 自己写的库,在src/tools/tools.hpp当中,注意要使用C++23标准编译 #include #include #include #include // 这两个宏用来申请读入和读出流,实现反射并输出日志,获取申请流的变量名字 #define OPEN_IFS_AND_CHECK(file_path,value_name)std::ifstream value_name(file_path);if(value_name.is_open()==false){std::stringstream ss;ss<<"cannot open input file stream : "< &DNAsequence, const size_t buf_size) //注意这里使用引用DNA sequence,避免拷贝开销 { static const std::unordered_map complement = { //这里使用查表的方式大大提高CPU速度,因为if分支CPU不容易命中缓存,需要使用查表加速 {'A', 'T'}, {'a', 'T'}, {'T', 'A'}, {'t', 'A'}, {'C', 'G'}, {'c', 'G'}, {'G', 'C'}, {'g', 'C'} }; std::reverse(DNAsequence.begin(), DNAsequence.begin() + buf_size); //翻转DNA序列 for (std::remove_const_t i = 0; i < buf_size; ++i) { auto it = complement.find(DNAsequence[i]);//查表并替换 if (it != complement.end()) { DNAsequence[i] = it->second; } } } class Spent{ // 使用RAII原理的自动计时器,计算主函数运行时间,析构时自动输出 private: const decltype(std::chrono::system_clock::now()) start; const std::string_view name; public: Spent(std::string_view name)noexcept:start(std::chrono::system_clock::now()),name(name){ zt::print("[Timer: ",name,"]"," Start timing","\n"); } ~Spent()noexcept{ const auto end = std::chrono::system_clock::now(); const auto dur = std::chrono::duration_cast (end-start); zt::print("[Timer: ",name,"]"," Stop timing , using ", dur,"\n"); } }; int main() { try{ std::ios_base::sync_with_stdio(false); //加了没效果 using namespace std; Spent all_spent("All spent"); //自动计时器,给主函数计时 std::array buf; unsigned long long lines = 0; filesystem::path input_path("filteredReads.txt"),output_path("reversedSequence.txt"); // ifstream input_file_stream(input_path); // ofstream output_file_stream(output_path); OPEN_IFS_AND_CHECK(input_path, input_file_stream) //创建输入和输出流 OPEN_OFS_AND_CHECK(output_path, output_file_stream) // string l = ""; zt::print("Undergoing transformation\n"); while (input_file_stream.getline(buf.data(),MAX_SIZE,'\n')) { int m = lines%2; const auto buf_len = strlen(buf.data()); const std::string_view suffix("\n"); //设置一个每个DNA序列结尾的字符,这里是以\n换行来结尾 if (m == 1){ // output_file_stream << reverseComplement(buf) << endl; reverseComplement(buf,buf_len); } for(int i=0;i