This commit is contained in:
Zengtudor 2024-09-18 15:27:36 +08:00
parent 7d96c74496
commit b1ee6b8d03
4 changed files with 179 additions and 5 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
.xmake .xmake
.vscode .vscode
build build
filteredReads.txt
reversedSequence.txt

View File

@ -1,3 +1,98 @@
int main(){ #include <exception>
#include<fstream>
#include <stdexcept>
#include<string>
#include<algorithm>
#include<iostream>
#include <type_traits>
#include"tools.hpp"
// std::string reversedComplement(std::string DNAsequence);
std::string reverseComplement(std::string DNAsequence)
{
using namespace std;
string l = "";
int i;
int q = DNAsequence.length();
reverse(DNAsequence.begin(), DNAsequence.end()); //将字符串反过来
for (i = 0; i < q; i++)
{
if (DNAsequence[i] == 'A')
l = l + 'T';
if (DNAsequence[i] == 'a')
l = l + 'T';
if (DNAsequence[i] == 'c')
l = l + 'G';
if (DNAsequence[i] == 'C')
l = l + 'G';
if (DNAsequence[i] == 'g')
l = l + 'C';
if (DNAsequence[i] == 'G')
l = l + 'C';
if (DNAsequence[i] == 't')
l = l + 'A';
if (DNAsequence[i] == 'T')
l = l + 'A';
}
return l;
}
template <class ...Args>
void check_fstream_isopen(const Args&...args)noexcept(false){
bool is_open=true;
(((!args.is_open())?is_open=false:true),...);
if(is_open==false){
throw std::runtime_error("cannot open file stream ");
}
}
int main()
{
try{
std::ios_base::sync_with_stdio(false); //加了没效果
using namespace std;
char buf[50000];
int lines = 0;
ifstream inputFile("filteredReads.txt");
ofstream outputFile("reversedSequence.txt");
check_fstream_isopen(inputFile,outputFile);
zt::print("open file ok!");
string l = "";
while (inputFile.getline(buf,50000,'\n'))
{
int m = lines%2;
if (m == 1)
outputFile << reverseComplement(buf) << endl;
else
outputFile << buf << endl;
lines++;
}
return 0;
}catch(const std::exception &e){
zt::print(
"Caught an error because:\n",
"\t",NAME_VALUE(e.what()),"\n"
"closing\n"
);
}catch(...){
zt::print(
"Caught an unknown error because:\n",
"Closing\n"
);
}
return -1;
} }

72
src/tools/tools.hpp Normal file
View File

@ -0,0 +1,72 @@
#pragma once
#include <iostream>
#include <ostream>
#include <sstream>
#define NAME_VALUE(v)#v," : ",(v)
namespace zt {
template<typename T>
class Range{
private:
const T _start,_end,_step;
public:
constexpr Range()=delete;
constexpr Range(const T start,const T end,const T step)noexcept:_start(start),_end(end),_step(step){}
constexpr Range(const T start,const T end)noexcept:Range(start,end,1){}
constexpr Range(const T end)noexcept:Range(0,end,1){}
struct Iterator{
T current;
const T step;
constexpr Iterator()=delete;
constexpr Iterator(const T start,const T step)noexcept:current(start),step(step){}
constexpr bool operator!=(const Iterator &other)const noexcept{
return current<other.current;
}
constexpr Iterator& operator++()noexcept{
current+=step;
return *this;
}
constexpr T operator*()const noexcept{
return current;
}
};
constexpr Iterator begin()const{
return Iterator(_start,_step);
}
constexpr Iterator end()const{
return Iterator(_end,_step);
}
};
template<typename ...Args>
inline void raw_print(std::ostream &os,const Args&...args){
std::ostringstream ss;
((ss<<args),...);
os<<ss.str();
return;
}
template<typename ...Args>
inline void print(const Args&...args){
raw_print(std::cout,args...);
return;
}
inline void print(){
return;
}
template<typename ...Args>
inline void eprint(const Args&...args){
raw_print(std::cerr,args...);
return;
}
inline void eprint(){
return;
}
}

View File

@ -1,4 +1,9 @@
add_rules("mode.debug","mode.release") add_rules("mode.debug","mode.release")
add_includedirs("src/tools")
set_languages("c++23")
set_rundir("./")
target("dna") target("dna")
add_files("src/main.cpp") add_files("src/main.cpp")