-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.cpp
167 lines (134 loc) · 4.71 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
#include "database.h"
#include <fstream>
#include <iostream>
#include <bitset>
#include <algorithm>
using namespace std;
Database::Database(string filename) {
//== Constructeur ==//
this->filename = filename;
loadData(filename);
print();
loadProteins(filename);
loadHeaders(filename);
}
void Database::loadData(const string &filename) {
//== Partie d'extraction d'information du fichier .pin ==//
ifstream pinFile;
pinFile.open(filename + ".pin", ios::binary | ios::in);
// Vérification d'erreur d'ouverture
if (!pinFile.is_open()) {
cout << "Erreur ouverture de " << filename << ".pin\n";
exit(1);
}
//--Extraction--//
//-Version-//
pinFile.read((char*)&version,sizeof(uint32_t));
version = __builtin_bswap32(version);
//-DatabaseType-//
pinFile.read((char*)&databaseType,sizeof(uint32_t));
databaseType = __builtin_bswap32(databaseType);
//-TitleLength//
pinFile.read((char*)&titleLength,sizeof(uint32_t));
titleLength = __builtin_bswap32(titleLength);
//-Title-//
char titleString[titleLength];
for(unsigned int i=0; i<titleLength; ++i){
pinFile.read((char*)&titleString[i], sizeof(char));
}
title = (string)titleString;
//-TimestampLength-//
pinFile.read((char*)×tampLength,sizeof(uint32_t));
timestampLength = __builtin_bswap32(timestampLength);
//-Timestamp-//
char timestampcopy[timestampLength];
for(unsigned int i=0; i<timestampLength; ++i){
pinFile.read((char*)×tampcopy[i], sizeof(char));
}
timestamp = string(timestampcopy);
//-NbrSequences-//
pinFile.read((char*)&nbrSequences,sizeof(uint32_t));
nbrSequences = __builtin_bswap32(nbrSequences);
//-NbrResidues-//
pinFile.read((char*)&nbrResidues,sizeof(uint64_t));
//-MaxSequence-//
pinFile.read((char*)&maxSequence,sizeof(uint32_t));
maxSequence = __builtin_bswap32(maxSequence);
//-HeaderOffsetTable-//
headerOffsets.reserve(nbrSequences+1); //fix the size of the vector
uint32_t offset; //the offset is get as 4 bytes before convert it into an integer
for (unsigned int i = 0; i <= nbrSequences; i++) {
pinFile.read((char*) &offset, sizeof(uint32_t));
headerOffsets.push_back(__builtin_bswap32(offset)); //convert bytes and store them as integer
}
//-SequenceOffsetTable-//
sequenceOffsets.reserve(nbrSequences+1); //fix the size of the vector
for (unsigned int i = 0; i <= nbrSequences; i++) {
pinFile.read((char*) &offset, sizeof(uint32_t));
sequenceOffsets.push_back(__builtin_bswap32(offset)); //convert bytes and store them as integer
}
pinFile.close();
}
void Database::loadProteins(const string &filename) {
//== Partie d'extraction d'informations du fichier .psq ==//
ifstream psqFile;
psqFile.open(filename + ".psq", ios::binary | ios::in);
psqFile.seekg(0, ios::beg);
// Vérification d'erreur d'ouverture
if (!psqFile.is_open()) {
cout << "Erreur Ouverture de " << filename << ".psq\n";
exit(1);
}
//--Extraction--//
int sequenceLength;
for (unsigned int i = 0; i < nbrSequences ; i++){
char firstByte[1];
psqFile.read((char*) &firstByte, sizeof(firstByte));
sequenceLength = sequenceOffsets[i+1] - sequenceOffsets[i] -1 ;
char* sequence = new char[sequenceLength];
psqFile.read(sequence, sequenceLength);
Protein * prot = new Protein();
prot->setSequence(sequence, sequenceLength);
proteins.push_back(*prot);
}
psqFile.close();
}
void Database::loadHeaders(const string &filename) {
//== Partie d'extraction d'informations du fichier .phr ==//
ifstream phrFile;
phrFile.open(filename + ".phr", ios::binary | ios::in);
phrFile.seekg(1, ios::beg);
// Vérification d'erreur d'ouverture
if (!phrFile.is_open()) {
cout << "Erreur ouverture de " << filename << ".phr\n";
exit(1);
}
//--Extraction--//
int headerLength;
for (unsigned int i = 0; i < nbrSequences ; i++){
headerLength = headerOffsets[i+1] - headerOffsets[i];
char header[headerLength];
phrFile.read((char*) &header, headerLength);
proteins[i].setHeader(string(header).substr(8, string::npos));
}
phrFile.close();
}
Protein & Database::getProtein(unsigned int index) {
return proteins[index];
}
void Database::print(std::ostream & out) {
//== Print les infos contenue dans la database ==//
out << " ===== Database =====" << endl;
out << " - Version : " << this->version << endl;
out << " - Type de la Database : " << this->databaseType << endl;
out << " - Titre : " << this->title << endl;
out << " - Timestamp : " << this->timestamp << endl;
out << " - Nombre de Séquences : " << this->nbrSequences << endl;
out << " - Nombre de Residues : " << this->nbrResidues << endl;
out << " - Plus Longue Séquence : " << this->maxSequence << endl;
out << " ====================" << endl;
out<< "\n" <<endl;
}
int Database::getNbrSequences() {
return nbrSequences;
}