geniomhe-oop-dnapp/include/fasta.hpp

54 lines
1.3 KiB
C++
Raw Normal View History

2023-10-01 12:26:59 +02:00
/** Simple FASTA file format reader
*
* @author The BioloGeeks Team
* @date 2023-09-30
*/
#pragma once
#include <iostream>
#include <fstream>
#include <vector>
class Record {
public:
Record(std::string name, std::string seq) {
this->name = name;
this->seq = seq;
}
std::string name, seq;
};
namespace fasta {
/**
* @see https://www.rosettacode.org/wiki/FASTA_format?section=10#C++
*
*/
std::vector<Record> read_file(std::ifstream &input) {
std::string line, name, content;
std::vector<Record> Records;
// Read fasta file lines and append each FASTA records
// to the Records vector.
while (input.good()) {
std::getline(input, line);
if (line[0] == '>') {
if (!name.empty()) {
Record Record(name, content);
Records.push_back(Record);
name.clear();
content.clear();
}
name = line.substr(1);
} else {
content += line;
}
}
if (!name.empty()) {
Record Record(name, content);
Records.push_back(Record);
}
return Records;
}
}