-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.cpp
223 lines (175 loc) · 6 KB
/
database.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "database.h"
#include <fstream>
#include <iostream>
#include <bitset>
#include <algorithm>
using namespace std;
Database::Database(string filename) {
this->filename = filename;
loadHeader(filename);
printInfos();
loadProteins(filename);
loadHeaders(filename);
}
void Database::loadHeader(const string &filename) {
ifstream dbFile;
dbFile.open(filename + ".pin", ios::binary | ios::in);
if (dbFile.is_open()) {
//version
char versionB[4]; //array of 4 bytes (int32) --> B(ytes), every array of bytes end with B
dbFile.read((char*)&versionB, sizeof(versionB));
version = bytesToIntBigEndian(versionB, 4);
//type
char typeB[4];
dbFile.read((char*)&typeB, sizeof(typeB));
type = bytesToIntBigEndian(typeB, 4);
//title lenght
char titleLengthB[4];
dbFile.read((char*)&titleLengthB, sizeof(titleLengthB));
titleLength = bytesToIntBigEndian(titleLengthB, 4);
//title
char titleB[titleLength + 1];
titleB[titleLength] = '\0';
dbFile.read((char*)&titleB, sizeof(titleB)-1);
title = string(titleB);//Bug : there are 3 weird characters at the end
//timestamp length
char timestampLengthB[4];
dbFile.read((char*)×tampLengthB, sizeof(timestampLengthB));
timestampLength = bytesToIntBigEndian(timestampLengthB, 4);
//timestamp
char timestampB[timestampLength];
dbFile.read((char*)×tampB, sizeof(timestampB));
timestamp = string(timestampB);
//number of sequences
char nbrSequencesB[4];
dbFile.read((char*)&nbrSequencesB, sizeof(nbrSequencesB));
nbrSequences = bytesToIntBigEndian(nbrSequencesB, 4);
//residue count
char residueCountB[8];
dbFile.read((char*)&residueCountB, sizeof(residueCountB));
residueCount = bytesToIntLittleEndian(residueCountB, 8);
//Length of the longest sequence
char maxLengthB[4];
dbFile.read((char*)&maxLengthB, sizeof(maxLengthB));
maxLength = bytesToIntBigEndian(maxLengthB, 4);
//header offset table
headerOffsets.reserve(nbrSequences+1); //fix the size of the vector
char offsetB[4]; //the offset is get as 4 bytes before convert it into an integer
for (int i = 0; i <= nbrSequences; i++) {
dbFile.read((char*) &offsetB, sizeof(offsetB));
headerOffsets.push_back(bytesToIntBigEndian(offsetB, 4)); //convert bytes and store them as integer
}
//sequence offset table
sequenceOffsets.reserve(nbrSequences+1); //fix the size of the vector
for (int i = 0; i <= nbrSequences; i++) {
dbFile.read((char*) &offsetB, sizeof(offsetB));
sequenceOffsets.push_back(bytesToIntBigEndian(offsetB, 4)); //convert bytes and store them as integer
}
dbFile.close();
}
else {
cout << "Unable to open database file : " << filename << ".pin\n";
throw string("Unable to load file");
}
}
void Database::loadProteins(const string &filename) {
//load sequences from file
if (!headerOffsets.empty() && !sequenceOffsets.empty()) {
ifstream dbFile;
dbFile.open(filename + ".psq", ios::binary | ios::in);
dbFile.seekg(0, ios::beg);
if (dbFile.is_open()) {
int sequenceSize;
for (int i = 0; i <= nbrSequences-1 ; i++){ //nbrSequences + 1
char firstByte[1];
dbFile.read((char*) &firstByte, sizeof(firstByte)); // go one byte forward to avoid the null byte at the beginning
sequenceSize = sequenceOffsets[i+1] - sequenceOffsets[i] -1 ;
char* sequence = new char[sequenceSize];
dbFile.read(sequence, sequenceSize);
Protein * prot = new Protein();
prot->setSequence(sequence, sequenceSize);
proteins.push_back(*prot);
}
}
else {
cout << "Unable to open database file : " << filename << ".psq\n";
throw string("Unable to load file");
}
}
else cout << "Header offsets or sequence offsets not yet loaded\n";
}
void Database::loadHeaders(const string &filename) {
//load headers from file
if (!headerOffsets.empty() && !sequenceOffsets.empty()) {
ifstream dbFile;
dbFile.open(filename + ".phr", ios::binary | ios::in);
dbFile.seekg(1, ios::beg);
if (dbFile.is_open()) {
int headerSize;
for (int i = 0; i <= nbrSequences-1 ; i++){
headerSize = headerOffsets[i+1] - headerOffsets[i];
char header[headerSize];
dbFile.read((char*) &header, headerSize);
proteins[i].setHeader(string(header).substr(8, string::npos));
}
}
else {
cout << "Unable to open database file : " << filename << ".phr\n";
throw string("Unable to load file");
}
}
else cout << "Header offsets or sequence offsets not yet loaded\n";
}
int Database::bytesToIntLittleEndian(char bytes[], int lenght) {
/*convert a byte array (little endian) to an integer */
int res = 0;
int j = lenght-1;
for(int i = lenght-1; i >= 0; i--) {
res += (bytes[i] & 0xFF) << (8*j);
j--;
}
return res;
}
int Database::bytesToIntBigEndian(char bytes[], int lenght) {
/*convert a byte array (big endian) to an integer */
int res = 0;
int j = 0;
for(int i = lenght-1; i >= 0; i--) {
res += (bytes[i] & 0xFF) << (8*j);
j++;
}
return res;
}
bool Database::contains(Protein protein) {
/*check if this database contains the protein passed as parameter*/
int i = -1;
for (Protein prot : proteins) {
i++;
if (prot == protein) {
cout << "protein d'index : " << i << endl;
return true;
}
}
return false;
}
Protein & Database::getProtein(int index) {
if (index < nbrSequences) {
return proteins[index];
}
return proteins[nbrSequences-1]; //if index not valid, return last protein
}
void Database::printInfos(std::ostream & out) {
out << "Database informations :" << endl;
out << " - Version : " << version << "\n";
out << " - Type : " << type << "\n";
//out << " - Title lenght : " << titleLength << "\n";
out << " - Title : " << title << "\n";
//out << " - Timestamp Length : " << timestampLength << "\n";
out << " - Timestamp : " << timestamp << "\n";
out << " - Number of sequences : " << nbrSequences << "\n";
out << " - Residue count : " << residueCount << "\n";
out << " - Longuest sequence : " << maxLength << "\n";
}
int Database::getNbrSequences() {
return nbrSequences;
}