fastaq2fasta

评论2,789

之前在本博客上发表一篇利用sed 将fastq转换成fasta的文章,这种方法利用linux系统自带的程序sed进行处理,速度很快,但是后来发现当fastq中某一reads的质量值以‘@’开头的话就会出现错误而且还不容易发觉。为了方便数据转换,所以抽了个时间用c++写了一个快速把FastaQ转成FastA格式的程序,与大家分享。

在linux下运行下面的命令,编译一下就可以使用了
[code lang="shell"]
g++ fq2fa.cpp -o fq2fa
[/code]

fq2fa.cpp的源代码如下:

[code lang="cpp"]
/*
将fastaq 转换成 fasta 文件
*/

#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>

using namespace std;
long line = 0;

int main(int argc,char *argv[])
{

//对命令行下输入参数进行处理

ifstream fin;
ofstream fout;
if(argc != 3)//三个参数,这个值是3,还有一个是程序名称
{
cout << "ERROR: illegal argument number: " << argc << endl;
cout <<
"Uasage: \n \t fq2fa fastaq_file fasta_file \n" <<endl;
exit(0);
}

fin.open(argv[1]);//参数从1开始,0是名称
fout.open(argv[2]);
if(!fin.good())
{
cout << "ERROR: illegal input file path: " << argv[1] <<endl;
cout <<
"Uasage: \n \t fq2fa fastaq_file fasta_file \n" <<endl;
exit(0);
}
else if(!fout.good())
{
cout << "ERROR: illegal output file path" << endl;
cout <<
"Uasage: \n \t fq2fa fastaq_file fasta_file \n" <<endl;
exit(0);
}

cout << "Converting Data..." << endl;

/*ofstream fout("out.txt");
ifstream fin("sample.txt");*/
int mod;
line=0;
while(fin != NULL)
{
string s;
getline(fin,s,'\n');
if (s=="")
{
continue;
}
line=line+1;
mod=line % 4;
if (mod == 1)
{
s[0]='>';
fout << s << endl;
}

if (mod == 2)
{
fout << s << endl;
}
}
cout << "finished!" << endl;
fout.close();
fin.close();

return 0;
}

[/code]

发表评论

匿名网友