-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotein.cpp
134 lines (108 loc) · 2.96 KB
/
protein.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "protein.h"
#include "codeTable.h"
#include <map>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
Protein::Protein() {
}
Protein::Protein(char* sequence, int length, string header) {
this->length = length;
this->sequence = sequence;
this->header = header;
}
Protein::~Protein() {}
void Protein::setSequence(char* sequence, int length) {
this->length = length;
this->sequence = sequence;
}
const char Protein::getResidue(int i) const {
return sequence[i];
}
void Protein::loadFromFile(const string filename) {
//load the header and the sequence of a protein from a fasta file containing only one protein
ifstream file;
file.open(filename);
CodeTable coder = CodeTable(); //blast code table
string seq;
string line;
if (file.is_open()) {
while (getline(file, line)) {
if (line[0] == '>') {
header = line;
}
else {
stringstream converter;
converter << &line[0];
char value;
while (converter >> value){
seq += value;
}
}
}
int sequenceSize = seq.size();
length = sequenceSize;
sequence = new char[sequenceSize+1];
for (int i = 0; i <= sequenceSize; i++) {
sequence[i] = coder.encode(seq[i]);
}
}
else {
cout << "Unable to open file : " << filename << "\n";
throw string("Unable to load file");
}
}
char* Protein::getSequence() const {
return sequence;
}
void Protein::setHeader(string header) {
this->header = header;
}
string Protein::decode() {
//return a sequence string in readable format (letters instead of binary)
CodeTable coder = CodeTable();
string decodedSequence = "";
for (int i = 0; i < length; i++) {
decodedSequence += coder.decode(sequence[i]);
//cout << "|" << hex << (int) sequence[i];
}
return decodedSequence;
}
bool Protein::operator==(Protein const & a) {
//define the == operator for Protein --> we can do : proteinA == proteinB
//two proteins are equal if they have the same sequence
return sequence == a.getSequence();
}
void Protein::print(string w, ostream& out) {
/*
* Print a protein. Print only the header, the sequence or both
* based on the parameter w
*/
if (w == "header" && !header.empty()) { //only print the header
if (header[0] != '>') { //if there is already a >, don't print another !
out << ">";
if (header[0] != 's') { //this is because of weird caracters at the begining (can't figure out why) -> solution : skip the 's' when reading the header
out << "s";
}
}
out << header << "\n";
}
else if (w == "sequence") { //only print the sequence
out << decode() << "\n";
}
else { // print the header and the sequence
if (header[0] != '>') {
out << ">";
if (header[0] != 's') { //this is because of weird caracters at the begining (can't figure out why) -> solution : skip the 's' when reading the header
out << "s";
}
}
out << header << "\n";
out << decode() << "\n";
}
}
const int Protein::size() const {
//return the lenght of the sequence
return length;
}