fa.cpp
2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cctype>
#include <cstring>
#include <sys/stat.h>
#include "fa.h"
#include "rna.h"
Fasta::Fasta() : path_(), name_(), seq_()
{
}
Fasta::Fasta(const std::string& path, const std::vector<std::string>& name, const std::vector<std::string>& seq)
{
path_ = path;
name_ = name;
seq_ = seq;
}
Fasta::Fasta(const Fasta& fa): path_(fa.path_), name_(fa.name_), seq_(fa.seq_)
{
}
Fasta& Fasta::operator=(const Fasta& fa)
{
if (this != &fa) {
path_ = fa.path_;
name_ = fa.name_;
seq_ = fa.seq_;
}
return *this;
}
const std::vector<std::string>& Fasta::name() const
{
return name_;
}
const std::vector<std::string>& Fasta::seq() const
{
return seq_;
}
const std::string& Fasta::path() const
{
return path_;
}
void Fasta::setName(std::vector<std::string> name)
{
name_ = name;
}
void Fasta::load(const char* file)
{
path_ = file;
std::string line, name, seq;
struct stat buf;
if( (stat(file, &buf) == 0))
{
std::ifstream ifs(file);
while (std::getline(ifs, line)) {
if (line[0]=='>') { /* header */
name=line.substr(1);
if(seq != "")
{
for(std::string::size_type i = 0; (i = seq.find("\n", i)) != std::string::npos;)
{
seq.replace(i, std::string("\n").length(), std::string(""));
i += std::string("").length();
}
seq_.push_back(seq);
}
name_.push_back(name);
seq = "";
}
else if (line[0] != '\0' and line[0] != '\n')
{
seq += line;
}
}
if(seq != "")
{
for(std::string::size_type i = 0; (i = seq.find("\n", i)) != std::string::npos;)
{
seq.replace(i, std::string("\n").length(), std::string(""));
i += std::string("").length();
}
seq_.push_back(seq);
}
}
else
{
throw std::string("The file " + path_ + " doesn't exist.");
}
}