【发布时间】:2020-10-15 10:33:55
【问题描述】:
我是 C++ 新手,在读取和更改输入文件中的行以使用下一行并将其保存到另一个输出文件中时,我需要帮助。
我有一个以 .fastq 格式存储的单个 DNA 序列的示例,其结构如下。
@Read_1
AGACUUUACGCT
+
++//187-,/02
所以每个 DNA 序列都有四行信息。
我的目标是将 DNA 字符串(第 2 行,长度 12)拆分为随机长度的不同片段,并将每个片段保存为单独的新序列。但要保留 .fastq 结构,我需要保留第 3 行和第 4 行的信息!所以理想的输出是:
@Read_1_1
AGAC
+
++//
@Read_1_2
UU
+
18
@Read_1_3
UACGCT
+
7-,/02
在这个理想的输出中,输入的第 4 行已被拆分以匹配每个 DNA 片段(但我可以使用 substr 来做到这一点,所以这不是问题)。我的问题是,当我拆分 DNA 序列(第 2 行)并将它们保存为新读取时,我需要第 3 行和第 4 行的信息。
我正在使用 C++ 进行编码,并且我制作了一些有效的函数,并进行了一些失败的尝试:
当我打开文件时,我创建了一个函数 (DNA_fragmentation),它将 DNA (line2) 随机分成一些片段,如下所示:
AGAC
UU
UACGCT
因此,当我使用此函数时,我正在读取第 2 行,然后将这些片段保存到 std::vectorstd::string 并使用 for 循环将这些片段及其读取(从第 1 行)保存到新文件中,给我输入:
@Read_1_1
AGAC
@Read_1_2
UU
@Read_1_3
UACGCT
我的问题是我不知道如何为每个新片段添加第 3 行和第 4 行,因为它们是在我打开并从原始文件读取第 2 行时创建的。如何从下一行中提取信息?
要读取文件并使用以下功能分隔功能:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
std::string fafq_seq(std::string in_name, std::string out_name) {
std::ifstream myfile(in_name);
std::ofstream out_file(out_name);
if (myfile.is_open() && out_file.is_open())
{
std::string line;
while( std::getline(myfile,line) )
{
int ID = 1;
std::string read_ID;
// This is line 1, which always match with @
if (line.rfind("@",0) == 0) {
continue;
} // Then reading line 2 the DNA sequence
else if (line.rfind("A", 0) == 0 ||
line.rfind("T", 0) == 0 ||
line.rfind("G", 0) == 0 ||
line.rfind("C", 0) == 0){
std::string Seq = line;
// creating a vector with each of the DNA pieces using my DNA_fragmentation function
std::vector<std::string> Damage = DNA_fragmentation(Seq,2,8);
// For each fragment im adding a new read and saving the output
for (int i=0; i<Damage.size();i++){
// adding what corresponds to line 1 starting with @
out_file << "@Read_" << ID << "_" << i+1 << std::endl;
// adding the DNA pieces
out_file << Damage[i] << std::endl;
}
ID += 1;
}
else {
// iterating through line 3 and 4, which is where im not sure how to handle my problem
out_file << line << std::endl;
}
}
out_file.close();
myfile.close();
}
}
int main() {
std::string File = "TestSeq.fastq";
fafq_seq(File,"Test_out.fastq");
return 0;
}
我知道这是一个很长的问题,对我来说进一步解释有点困难,但我希望这个问题有意义。但只要任何 cmets 或帮助将不胜感激。谢谢。
【问题讨论】: