-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathDataSet.cpp
More file actions
270 lines (246 loc) · 9.86 KB
/
DataSet.cpp
File metadata and controls
270 lines (246 loc) · 9.86 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*******************************************************************************
Copyright 2006-2012 Lukas Käll <lukas.kall@scilifelab.se>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*******************************************************************************/
#include "DataSet.h"
#include <cmath>
FeatureNames DataSet::featureNames_;
bool DataSet::decoyWarningTripped_ = false;
DataSet::DataSet() {}
DataSet::~DataSet() {
std::vector<PSMDescription*>::iterator it = psms_.begin();
for ( ; it != psms_.end(); ++it) {
PSMDescription::deletePtr(*it);
}
}
/*const double* DataSet::getFeatures(const int pos) const {
return &feature[pos];
}*/
bool DataSet::writeTabData(ofstream& out) {
unsigned int nf = static_cast<unsigned int>(FeatureNames::getNumFeatures());
std::vector<PSMDescription*>::iterator it = psms_.begin();
for ( ; it != psms_.end(); ++it) {
PSMDescription* psm = *it;
double* featureRow = psm->features;
out << psm->getId() << '\t' << label_ << '\t' << psm->scan << '\t'
<< psm->expMass << '\t' << psm->calcMass;
if (psm->hasSpectrumFileName()) {
out << '\t' << psm->getSpectrumFileName();
}
for (unsigned int ix = 0; ix < nf; ix++) {
out << '\t' << featureRow[ix];
}
out << '\t' << psm->getFullPeptide() << '\t';
psm->printProteins(out);
out << endl;
}
return true;
}
void DataSet::print_features() {
for (std::size_t i = 0; i < getSize(); i++) {
for (std::size_t j = 0; j < FeatureNames::getNumFeatures(); j++) {
cerr << j + 1 << ":" << psms_[i]->features[j] << " ";
}
cerr << endl;
}
}
void DataSet::print_10features() {
cerr << DataSet::getFeatureNames().getFeatureNames() << endl;
for (std::size_t i = 0; i < 10; i++) {
for (std::size_t j = 0; j < FeatureNames::getNumFeatures(); j++) {
cerr << psms_[i]->features[j] << "\t";
}
cerr << endl;
}
}
// TODO: find a way to make these three functions generic
void DataSet::fillScores(std::vector<ScoreHolder>& scores) {
std::vector<PSMDescription*>::iterator it = psms_.begin();
for ( ; it != psms_.end(); ++it) {
PSMDescription* psm = *it;
scores.push_back(ScoreHolder(.0, label_, psm));
}
}
void DataSet::fillFeatures(std::vector<double*>& features) {
std::vector<PSMDescription*>::iterator it = psms_.begin();
for ( ; it != psms_.end(); ++it) {
PSMDescription* psm = *it;
features.push_back(psm->features);
}
}
/**
* Read in psm details from a string out of tab delimited file
* @param dataStream filestream of tab delimited file, only passed to close on exception
* TODO: remove dataStream parameter and return int with type of error to handle upstream.
* @param line tab delimited string containing the psm details
*/
void DataSet::readPsm(const std::string& line, const unsigned int lineNr,
const std::vector<OptionalField>& optionalFields, FeatureMemoryPool& featurePool,
std::string decoyPrefix) {
PSMDescription* myPsm = NULL;
bool readProteins = true;
readPsm(line, lineNr, optionalFields, readProteins, myPsm, featurePool, decoyPrefix);
registerPsm(myPsm);
}
LabelType DataSet::readPsm(const std::string& line, const unsigned int lineNr,
const std::vector<OptionalField>& optionalFields, bool readProteins,
PSMDescription*& myPsm, FeatureMemoryPool& featurePool, std::string decoyPrefix) {
TabReader reader(line);
std::string tmp;
myPsm = new PSMDescription();
myPsm->setId(reader.readString());
LabelType label = reader.readInt() == 1 ? LabelType::TARGET : LabelType::DECOY;
bool hasScannr = false;
std::vector<OptionalField>::const_iterator it = optionalFields.begin();
for ( ; it != optionalFields.end(); ++it) {
switch (*it) {
case SCANNR: {
myPsm->scan = static_cast<unsigned int>(reader.readInt());
if (reader.error()) {
ostringstream temp;
temp << "ERROR: Reading tab file, error reading scan number of PSM "
<< myPsm->getId() << ". Check if scan number is an integer." << std::endl;
throw MyException(temp.str());
} else {
hasScannr = true;
}
break;
} case EXPMASS: {
myPsm->expMass = reader.readDouble();
break;
} case CALCMASS: {
myPsm->calcMass = reader.readDouble();
break;
} case RETTIME: {
myPsm->setRetentionTime(reader.readDouble());
break;
} case DELTAMASS: {
ostringstream temp;
temp << "ERROR: The parser tries to allocate a deprecated delta mass field for the PSM "
<< myPsm->getId() << std::endl;
throw MyException(temp.str());
break;
} case FILENAME: {
myPsm->setSpectrumFileName(reader.readString());
break;
} default: {
ostringstream temp;
temp << "ERROR: Unknown optional field." << std::endl;
throw MyException(temp.str());
break;
}
}
}
if (!hasScannr) myPsm->scan = lineNr;
unsigned int numFeatures = static_cast<unsigned int>(FeatureNames::getNumFeatures());
double* featureRow = featurePool.allocate();
myPsm->features = featureRow;
for (unsigned int j = 0; j < numFeatures; j++) {
featureRow[j] = reader.readDouble();
if (!isfinite(featureRow[j])) {
ostringstream oss;
oss << "ERROR: Reached strange feature with val=" << featureRow[j]
<< " col=" << j << " for PSM with id " << myPsm->getId() << endl;
if (NO_TERMINATE) {
std::cerr << oss.str();
std::cerr << "No-terminate flag set: setting value to 0 and ignoring the error." << std::endl;
featureRow[j] = 0.0;
} else {
throw MyException(oss.str() + "Terminating.\n");
}
}
}
if (reader.error()) {
ostringstream temp;
temp << "ERROR: Reading tab file, error reading in feature vector of PSM "
<< myPsm->getId() << ". Check if there are enough features on this line and "
<< "if they are all floating point numbers or integers." << std::endl;
throw MyException(temp.str());
}
std::string peptide_seq = reader.readString();
myPsm->setPeptide(peptide_seq);
if (reader.error()) {
ostringstream temp;
temp << "ERROR: Reading tab file, error reading PSM " << myPsm->getId()
<< ". Check if a peptide and at least one protein are specified." << std::endl;
throw MyException(temp.str());
} else if (ProteinProbEstimator::getCalcProteinLevelProb()) {
// MT: we only need the peptide sequences to be well formatted if protein inference is applied
if (peptide_seq.size() < 5) {
ostringstream temp;
temp << "ERROR: Reading tab file, the peptide sequence " << peptide_seq
<< " with PSM id " << myPsm->getId() << " is too short." << std::endl;
throw MyException(temp.str());
} else if (peptide_seq.at(1) != '.' && peptide_seq.at(peptide_seq.size()-1) != '.') {
ostringstream temp;
temp << "ERROR: Reading tab file, the peptide sequence " << peptide_seq
<< " with PSM id " << myPsm->getId() << " does not contain one or two of its"
<< " flanking amino acids." << std::endl;
throw MyException(temp.str());
}
}
if (readProteins) {
std::vector<std::string> proteins;
if (PSMDescription::getProteinNameSeparator() == "\t") {
while (!reader.error()) {
std::string tmp = reader.readString();
if (tmp.size() > 0) proteins.push_back(tmp);
}
} else {
std::string names = reader.readString();
size_t pos = 0;
std::string token;
while ((pos = names.find(PSMDescription::getProteinNameSeparator())) != std::string::npos) {
token = names.substr(0, pos);
if (token.size() > 0) proteins.push_back(token);
names.erase(0, pos + PSMDescription::getProteinNameSeparator().length());
}
if (names.size() > 0) proteins.push_back(names);
// check if end of line was reached after reading the protein string
// if this is not the case, it means the user probably incorrectly provided proteins as a tab separated input
if (!reader.error()) {
ostringstream temp;
temp << "ERROR: Reading tab file, error reading PSM " << myPsm->getId()
<< ". Found tab character after reading proteins even though " << PSMDescription::getProteinNameSeparator()
<< " was specified as --protein-name-separator." << std::endl;
if (NO_TERMINATE) {
std::cerr << temp.str();
std::cerr << "No-terminate flag set: ignoring error and continuing "
<< "without PSMs." << std::endl;
} else {
throw MyException(temp.str());
}
}
}
proteins.swap(myPsm->proteinIds); // shrink to fit
}
if (label == LabelType::DECOY) {
for (auto const& proteinId: myPsm->proteinIds) {
bool startsWithDecoyPrefix = (proteinId.rfind(decoyPrefix, 0) == 0);
if (!startsWithDecoyPrefix && VERB > 1 && !decoyWarningTripped_) {
std::cerr << "Warning: protein decoy prefix " << decoyPrefix
<< " doesn't match the decoy protein identifier "
<< proteinId << "." << std::endl;
decoyWarningTripped_ = true;
}
}
}
return label;
}
void DataSet::registerPsm(PSMDescription* myPsm) {
switch (label_) {
case LabelType::TARGET: { break; };
case LabelType::DECOY: { break; };
default: { throw MyException("ERROR : Reading PSM, class DataSet has not been initiated\
to neither target nor decoy label\n");}
}
psms_.push_back(myPsm);
}