This commit is contained in:
Zengtudor 2024-09-18 16:27:10 +08:00
parent e2d486cfd1
commit ba4fa5e19f

View File

@ -1,4 +1,6 @@
#include <array> #include <array>
#include <cstddef>
#include <cstdio>
#include <exception> #include <exception>
#include <filesystem> #include <filesystem>
#include<fstream> #include<fstream>
@ -8,6 +10,9 @@
#include<algorithm> #include<algorithm>
#include<iostream> #include<iostream>
#include"tools.hpp" #include"tools.hpp"
#include <cstring>
#include <string>
// std::string reversedComplement(std::string DNAsequence); // std::string reversedComplement(std::string DNAsequence);
@ -17,16 +22,17 @@
const size_t MAX_SIZE = 5e4+5; const size_t MAX_SIZE = 5e4+5;
std::string reverseComplement(std::array<char, MAX_SIZE> DNAsequence) void reverseComplement(std::array<char, MAX_SIZE> &DNAsequence,const size_t buf_size) // 注意这里要用'&'取引用,这样不用拷贝,大大减少耗时
{ {
using namespace std; using namespace std;
string l = ""; // string l = "";
int i; int i;
int q = DNAsequence.length(); int q = strlen(DNAsequence.data());
reverse(DNAsequence.begin(), DNAsequence.end()); //将字符串反过来 reverse(DNAsequence.begin(), DNAsequence.begin()+q); //将字符串反过来
for (i = 0; i < q; i++) for (i = 0; i < q; i++)
{ {
char &l = DNAsequence[i];
if (DNAsequence[i] == 'A') if (DNAsequence[i] == 'A')
l = l + 'T'; l = l + 'T';
if (DNAsequence[i] == 'a') if (DNAsequence[i] == 'a')
@ -44,7 +50,6 @@ std::string reverseComplement(std::array<char, MAX_SIZE> DNAsequence)
if (DNAsequence[i] == 'T') if (DNAsequence[i] == 'T')
l = l + 'A'; l = l + 'A';
} }
return l;
} }
int main() int main()
@ -63,16 +68,18 @@ int main()
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 = "";
while (input_file_stream.getline(buf.data(),50000,'\n')) while (input_file_stream.getline(buf.data(),MAX_SIZE,'\n'))
{ {
int m = lines%2; int m = lines%2;
if (m == 1) const auto buf_len = strlen(buf.data());
reverseComplement(buf);
// output_file_stream << reverseComplement(buf) << endl; if (m == 1){
// else // output_file_stream << reverseComplement(buf) << endl;
output_file_stream << buf << endl; reverseComplement(buf,buf_len);
}
output_file_stream.write(buf.data(), buf_len);
lines++; lines++;
} }