我编写了一种流类,可以按顺序读取 FASTA 文件:
界面:
class Sequence_stream{
public:
Sequence_stream(const char* Filename, std::string Format); // Constructor.
NucleotideSequence get(); // Get a sequence from file.
private:
std::string FileName;
std::ifstream FileStream;
std::string FileFormat;
};
执行:
Sequence_stream::Sequence_stream(const char* Filename, std::string Format)
{
FileName = Filename;
FileStream.open(FileName);
FileFormat = Format;
std::cout << "Filestream is open: " << FileStream.is_open() << std::endl;
}
NucleotideSequence Sequence_stream::get()
{
if(FileFormat=="fasta")
{
if (FileStream.is_open())
{
char currentchar;
std::string name;
std::vector<Nucleotide> sequence;
currentchar = FileStream.get();
if (currentchar == '>') { // Check that the start of the first line is the fasta head character.
currentchar = FileStream.get(); // Proceed to get the full name of the sequence. Get characters until the newline character.
while(currentchar != '\n')
{
name.append(currentchar);
currentchar = FileStream.get();
} // done getting names, now let's get the sequence.
currentchar = FileStream.get();
while(currentchar != '>')
{
if(currentchar != '\n'){
sequence.push_back(Nucleotide(currentchar));
}
currentchar = FileStream.get();
}
if(currentchar == '>')
{
FileStream.unget();
}
return NucleotideSequence(name, sequence);
} else {
std::cout << "The first line of the file was not a fasta format description line beginning with '>'. Are you sure the file is of FASTA format?" << std::endl;
return;
}
} else {
std::cout << "The filestream is not open" << endl;
}
return NucleotideSequence(name, sequence);
}
}
我在使用 get 方法时遇到问题,我试图逐个字符地读取序列并将其添加到一个名为 name 的字符串中,但是当我编译它时抱怨它不是一个常量字符:
/local/yrq12edu/Dropbox/libHybRIDS/HybRIDS_Sequences.cpp|75|错误:从 'char' 到 'const char*' 的无效转换 [-fpermissive]|
但是,变量 currentchar 不能是常量,因为它在获取新字符时的循环期间会发生变化 - 最好的方法是如何做到这一点?我曾想过将 current char 设为 const char* 而不是 char 但是当我尝试尊重它时会收到大量只读错误,它会在代码中获取字符值:
NucleotideSequence Sequence_stream::get()
{
if(FileFormat=="fasta")
{
if (FileStream.is_open())
{
const char* currentchar;
std::string name;
std::vector<Nucleotide> sequence;
*currentchar = FileStream.get();
if (*currentchar == '>') { // Check that the start of the first line is the fasta head character.
*currentchar = FileStream.get(); // Proceed to get the full name of the sequence. Get characters until the newline character.
while(*currentchar != '\n')
{
name.append(currentchar);
*currentchar = FileStream.get();
} // done getting names, now let's get the sequence.
*currentchar = FileStream.get();
while(*currentchar != '>')
{
if(*currentchar != '\n'){
sequence.push_back(Nucleotide(*currentchar));
}
*currentchar = FileStream.get();
}
if(*currentchar == '>')
{
FileStream.unget();
}
return NucleotideSequence(name, sequence);
} else {
std::cout << "The first line of the file was not a fasta format description line beginning with '>'. Are you sure the file is of FASTA format?" << std::endl;
return;
}
} else {
std::cout << "The filestream is not open" << endl;
}
return NucleotideSequence(name, sequence);
}
}
我该怎么办?我想也许将 currentchar 的值分配给一个临时常量,然后将其输入到 append 中?
谢谢,本。