[英]How can I change the letters of a sequence in a text file?
我必須改進和擴展這段代碼。 詳細地說,我有一個帶有基因型代碼的文本文件(即 AGGGGCCCTATTCGCCC .....),它想要像這樣更改這些代碼:
A -> T
G -> C
C -> G
T -> A
我的意思是像上面那樣將A
更改為T
然后我將這個新代碼保存在我的文件中。
如果您能指導我完成這件事,我將不勝感激。
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
int readFile (std::string Genotype, std::vector<std::string>& fileContent)
{
// Opening the Genotype file
std::ifstream CGenotype("AT.txt");
// Checking if object is valid
if (CGenotype.fail())
{
std::cout << "Cannot open the Genotype File : " << Genotype << std::endl;
return EXIT_FAILURE;
}
if (CGenotype.peek() == std::ifstream::traits_type::eof())
{
std::cout << "The file is empty: " << Genotype << std::endl;
return EXIT_FAILURE;
}
std::string str;
// Reading the next line from genotype file untill it reaches the end.
while (std::getline(CGenotype, str))
{
// Line contains string of length > 0 then save it in vector
if (str.size() > 0)
{
fileContent.push_back(str);
}
}
//Closing the genotype file
CGenotype.close();
return EXIT_SUCCESS;
}
int writeFile (std::string Genotype, std::vector<std::string>& fileContent)
{
std::string str;
while (std::getline(CGenotype, str))
{
if (str== 'A';
cout << 'T';
else if (str== 'T';
cout << 'A';
else if (str== 'C';
cout << 'G';
else if (str== 'G';
cout << 'C';
}
CGenotype.close();
}
int main()
{
std::vector<std::string> fileContent;
// Getting the contents of genotype file in a vector
int fileCheck = readFile("AT.txt", fileContent);
if (!fileCheck)
{
// Printing the vector contents
for (std::string& line : fileContent)
std::cout << line << std::endl;
}
}
我在想這樣的事情(在適當的地方嵌入解釋):
#include <algorithm>
#include <iterator>
#include <fstream>
#include <filesystem>
int main()
{
{
// open input and disposable temporary output file
std::ifstream in("in.txt");
std::ofstream out("out.txt");
//read character from input file, write transformed character to output file
std::transform(std::istream_iterator<char>(in),
std::istream_iterator<char>(),
std::ostream_iterator<char>(out),
[](char val)
{
switch(val)
{
case 'A': return 'T';
case 'G': return 'C';
case 'C': return 'G';
case 'T': return 'A';
default: return val;
}
});
} // RAII closes open files here
// replace input file
std::filesystem::remove("in.txt");
std::filesystem::rename("out.txt", "in.txt");
}
沒有像其他答案那樣將文件轉換到位的理由:如果出現任何問題,直到輸入文件被 output 文件替換,輸入文件沒有損壞。 萬一發生故障,window 像半轉換文件一樣的損壞是最小的。
這樣的事情怎么樣? 這個版本處理每個字符,而不是每一行。 為了簡短起見,我沒有包含任何特定於域的錯誤處理。
我假設您想處理每個單獨的字符......並且每個字符都被替換為內聯或保持原樣。
int main() {
// ... Open the file (will default to both read and write)
std::fstream s("AT.txt");
// ... Get initial position (i.e., 0)
long pos = s.tellp() ;
// ... Repeat: read a character until you can't
while ( s.seekp(pos++) ) {
// ... Parse the current character
switch( s.peek() ) {
case 'A': s.write("T", 1); break ; // ... replace inline
case 'G': s.write("C", 1); break ; // ... replace inline
case 'C': s.write("G", 1); break ; // ... replace inline
case 'T': s.write("A", 1); break ; // ... replace inline
default: break ; // ... nothing to translate
}
}
// .... File will close automagically
return EXIT_SUCCESS ;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.