添加了注释

This commit is contained in:
Zengtudor 2024-09-18 19:28:51 +08:00
parent 2ecddfa22f
commit 1f8beaa44d

View File

@ -15,27 +15,27 @@
#include <unordered_map> #include <unordered_map>
// std::string reversedComplement(std::string DNAsequence); // 这两个宏用来申请读入和读出流,实现反射并输出日志,获取申请流的变量名字
#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 : "<<file_path.filename();throw std::runtime_error(ss.str());}else{zt::print("Open input file stream to value ["#value_name"] ok , from [",file_path.filename(),"]\n");} #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 : "<<file_path.filename();throw std::runtime_error(ss.str());}else{zt::print("Open input file stream to value ["#value_name"] ok , from [",file_path.filename(),"]\n");}
#define OPEN_OFS_AND_CHECK(file_path,value_name)std::ofstream value_name(file_path);if(value_name.is_open()==false){std::stringstream ss;ss<<"cannot open output file stream : "<<file_path.filename();throw std::runtime_error(ss.str());}else{zt::print("Open output file stream to value ["#value_name"] ok , from [",file_path.filename(),"]\n");} #define OPEN_OFS_AND_CHECK(file_path,value_name)std::ofstream value_name(file_path);if(value_name.is_open()==false){std::stringstream ss;ss<<"cannot open output file stream : "<<file_path.filename();throw std::runtime_error(ss.str());}else{zt::print("Open output file stream to value ["#value_name"] ok , from [",file_path.filename(),"]\n");}
//最大DNA序列长度
const size_t MAX_SIZE = 5e4+5; const size_t MAX_SIZE = 5e4+5;
void reverseComplement(std::array<char, MAX_SIZE> &DNAsequence, const size_t buf_size) void reverseComplement(std::array<char, MAX_SIZE> &DNAsequence, const size_t buf_size) //注意这里使用引用DNA sequence避免拷贝开销
{ {
static const std::unordered_map<char, char> complement = { static const std::unordered_map<char, char> complement = { //这里使用查表的方式大大提高CPU速度因为if分支CPU不容易命中缓存需要使用查表加速
{'A', 'T'}, {'a', 'T'}, {'A', 'T'}, {'a', 'T'},
{'T', 'A'}, {'t', 'A'}, {'T', 'A'}, {'t', 'A'},
{'C', 'G'}, {'c', 'G'}, {'C', 'G'}, {'c', 'G'},
{'G', 'C'}, {'g', 'C'} {'G', 'C'}, {'g', 'C'}
}; };
std::reverse(DNAsequence.begin(), DNAsequence.begin() + buf_size); std::reverse(DNAsequence.begin(), DNAsequence.begin() + buf_size); //翻转DNA序列
for (std::remove_const_t<decltype(buf_size)> i = 0; i < buf_size; ++i) { for (std::remove_const_t<decltype(buf_size)> i = 0; i < buf_size; ++i) {
auto it = complement.find(DNAsequence[i]); auto it = complement.find(DNAsequence[i]);//查表并替换
if (it != complement.end()) { if (it != complement.end()) {
DNAsequence[i] = it->second; DNAsequence[i] = it->second;
} }
@ -43,7 +43,7 @@ void reverseComplement(std::array<char, MAX_SIZE> &DNAsequence, const size_t buf
} }
class Spent{ class Spent{ // 使用RAII原理的自动计时器计算主函数运行时间析构时自动输出
private: private:
const decltype(std::chrono::system_clock::now()) start; const decltype(std::chrono::system_clock::now()) start;
const std::string_view name; const std::string_view name;
@ -51,7 +51,7 @@ public:
Spent(std::string_view name)noexcept:start(std::chrono::system_clock::now()),name(name){ Spent(std::string_view name)noexcept:start(std::chrono::system_clock::now()),name(name){
zt::print("[Timer: ",name,"]"," Start timing","\n"); zt::print("[Timer: ",name,"]"," Start timing","\n");
} }
~Spent(){ ~Spent()noexcept{
const auto end = std::chrono::system_clock::now(); const auto end = std::chrono::system_clock::now();
const auto dur = std::chrono::duration_cast<std::chrono::milliseconds> (end-start); const auto dur = std::chrono::duration_cast<std::chrono::milliseconds> (end-start);
zt::print("[Timer: ",name,"]"," Stop timing , using ", dur,"\n"); zt::print("[Timer: ",name,"]"," Stop timing , using ", dur,"\n");
@ -64,7 +64,7 @@ int main()
std::ios_base::sync_with_stdio(false); //加了没效果 std::ios_base::sync_with_stdio(false); //加了没效果
using namespace std; using namespace std;
Spent all_spent("All spent"); Spent all_spent("All spent"); //自动计时器,给主函数计时
std::array<char,MAX_SIZE> buf; std::array<char,MAX_SIZE> buf;
unsigned long long lines = 0; unsigned long long lines = 0;
@ -73,7 +73,7 @@ int main()
// ifstream input_file_stream(input_path); // ifstream input_file_stream(input_path);
// ofstream output_file_stream(output_path); // ofstream output_file_stream(output_path);
OPEN_IFS_AND_CHECK(input_path, input_file_stream) OPEN_IFS_AND_CHECK(input_path, input_file_stream) //创建输入和输出流
OPEN_OFS_AND_CHECK(output_path, output_file_stream) OPEN_OFS_AND_CHECK(output_path, output_file_stream)
// string l = ""; // string l = "";
@ -82,15 +82,15 @@ int main()
{ {
int m = lines%2; int m = lines%2;
const auto buf_len = strlen(buf.data()); const auto buf_len = strlen(buf.data());
const std::string_view suffix("\n"); const std::string_view suffix("\n"); //设置一个每个DNA序列结尾的字符这里是以\n换行来结尾
if (m == 1){ if (m == 1){
// output_file_stream << reverseComplement(buf) << endl; // output_file_stream << reverseComplement(buf) << endl;
reverseComplement(buf,buf_len); reverseComplement(buf,buf_len);
} }
for(int i=0;i<suffix.size();i++){ for(int i=0;i<suffix.size();i++){
buf[buf_len+i]=suffix[i]; buf[buf_len+i]=suffix[i]; // 将末尾增加字符追加到结尾。注意不能过长超过MAX_SIZE
} }
output_file_stream.write(buf.data(), buf_len+suffix.size()); output_file_stream.write(buf.data(), buf_len+suffix.size()); // 写入文件
lines++; lines++;
} }
@ -99,7 +99,7 @@ int main()
zt::print( zt::print(
"Caught an error because:\n", "Caught an error because:\n",
"\t",NAME_VALUE(e.what()),"\n" "\t",NAME_VALUE(e.what()),"\n"
"closing\n" "Closing\n"
); );
}catch(...){ }catch(...){
zt::print( zt::print(