-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFLASHTnTFile3.cpp
More file actions
277 lines (227 loc) · 11.6 KB
/
FLASHTnTFile3.cpp
File metadata and controls
277 lines (227 loc) · 11.6 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
271
272
273
274
275
276
277
#include <OpenMS/ANALYSIS/TOPDOWN/DeconvolvedSpectrum.h>
#include <OpenMS/FORMAT/FLASHTnTFile.h>
#include <OpenMS/CHEMISTRY/ProForma.h> // New inclusion for ProForma
namespace OpenMS
{
/**
@brief FLASHTnT output *.tsv file format
@ingroup FileIO
**/
void FLASHTnTFile::writeTagHeader(std::fstream& fs)
{
fs << "TagIndex\tScan\tRetentionTime\tProteoformIndex\tProteinAccession\tProteinDescription\tTagSequence\tNmass\tCmass\tStartPosition\tDeltaMass\tL"
"ength\tDeNovoScore\tMasses\tMassScores\n";
}
/// write header line for PrSM file
void FLASHTnTFile::writePrSMHeader(std::fstream& fs)
{
fs << "ProteoformIndex\tScan\tRetentionTime\tNumMass\tProteinAccession\tProteinDescription\tProteoformMass\tDatabaseSequence\tProteinSequence\tProf"
"orma\tMatchedAminoAcidCount\tCoverage(%)\tStartPosition\tEndPosition"
"\tTagCount\tTagIndices\tModCount\tModMass\tModID\tModAccession\tModStart\tModEnd\tScore\tPrSMLevelQvalue\tProteoformLevelQvalue\n";
}
/// write header line for Proteoform file
void FLASHTnTFile::writeProHeader(std::fstream& fs)
{
fs << "ProteoformIndex\tScan\tRetentionTime\tNumMass\tProteinAccession\tProteinDescription\tProteoformMass\tDatabaseSequence\tProteinSequence\tProf"
"orma\tMatchedAminoAcidCount\tCoverage(%)\tStartPosition\tEndPosition"
"\tTagCount\tTagIndices\tModCount\tModMass\tModID\tModAccession\tModStart\tModEnd\tScore\tProteoformLevelQvalue\n";
}
/// write the features in regular file output
void FLASHTnTFile::writeTags(const FLASHTnTAlgorithm& tnt, double flanking_mass_tol, std::fstream& fs)
{
auto tags = std::vector<FLASHHelperClasses::Tag>();
tnt.getTags(tags);
for (int c = 0; c < 2; c++)
{
for (const auto& tag : tags)
{
auto hits = std::vector<ProteinHit>();
tnt.getProteoformHitsMatchedBy(tag, hits);
if (c == 0 && hits.empty()) continue;
if (c == 1 && ! hits.empty()) continue;
String acc = "";
String description = "";
String hitindices = "";
String positions = "";
String delta_masses = "";
for (const auto& hit : hits)
{
if (! acc.empty()) acc += ";";
if (! description.empty()) description += ";";
if (! hitindices.empty()) hitindices += ";";
if (! positions.empty()) positions += ";";
if (! delta_masses.empty()) delta_masses += ";";
acc += hit.getAccession();
String proteindescription = hit.getDescription();
if (proteindescription.empty()) { proteindescription = " "; }
description += proteindescription;
hitindices += (String)hit.getMetaValue("Index");
auto pos = std::vector<int>();
auto masses = std::vector<double>();
FLASHTaggerAlgorithm::getMatchedPositionsAndFlankingMassDiffs(pos, masses, flanking_mass_tol, hit, tag);
if (pos.size() != 0) { positions += std::to_string(pos[0]); }
if (masses.size() != 0) { delta_masses += std::to_string(masses[0]); }
}
fs << tag.getIndex() << "\t" << tag.getScan() << "\t" << tag.getRetentionTime() << "\t" << hitindices << "\t" << acc << "\t" << description
<< "\t" << tag.getSequence() << "\t" << std::to_string(tag.getNtermMass()) << "\t" << std::to_string(tag.getCtermMass()) << "\t" << positions
<< "\t" << delta_masses << "\t" << tag.getLength() << "\t" << tag.getScore() << "\t";
for (const auto& mz : tag.getMzs())
{
fs << std::to_string(mz) << ",";
}
fs << "\t";
for (size_t i = 0; i <= tag.getLength(); i++) // Fixed signed/unsigned comparison issue
{
fs << std::to_string(tag.getScore(i)) << ",";
}
fs << "\n";
}
}
}
String generateProFormaString(const String& sequence,
const std::vector<double>& mod_masses,
const std::vector<int>& mod_starts,
const std::vector<int>& mod_ends,
const std::vector<String>& mod_ids,
const std::vector<String>& mod_accs)
{
ProForma proforma(AASequence::fromString(sequence)); // Create ProForma object
// Map to hold ranges that require parentheses
std::map<std::pair<int, int>, String> range_modifications;
// Loop through modifications and add them to the sequence
for (size_t i = 0; i < mod_masses.size(); ++i)
{
// Check if the modification applies to a range
if (mod_starts[i] != mod_ends[i])
{
// Mark range modifications using round brackets
String range_mod = "(" + sequence.substr(mod_starts[i], mod_ends[i] - mod_starts[i]) + ")[";
// Add modification information
if (! mod_ids[i].empty()) { range_mod += mod_ids[i]; }
else if (mod_masses[i] != 0.0) { range_mod += (mod_masses[i] > 0 ? "+" : "") + std::to_string(mod_masses[i]); }
if (! mod_accs[i].empty()) { range_mod += ":" + mod_accs[i]; }
range_mod += "]";
range_modifications[{mod_starts[i], mod_ends[i]}] = range_mod;
}
else
{
// For single position modifications, add them directly
proforma.addModification(mod_starts[i], mod_ids[i], mod_masses[i]);
}
}
// Build the ProForma string with range modifications
String proforma_str = proforma.toProFormaString();
// Insert range modifications into the ProForma string
for (const auto& range_mod : range_modifications)
{
// Insert the range modification at the correct position
int start_pos = range_mod.first.first; // Start of the range
int end_pos = range_mod.first.second; // End of the range
// Find where to insert the range modification in the ProForma string
proforma_str.insert(start_pos, range_mod.second);
}
return proforma_str; // Return the final ProForma string
}
void OpenMS::FLASHTnTFile::writePrSMs(const std::vector<ProteinHit>& hits, std::fstream& fs)
{
for (const auto& hit : hits)
{
if (! hit.metaValueExists("Index")) continue;
String tagindices = "", modmasses = "", modstarts = "", modends = "", modids = "", modaccs = "";
int cntr = 0;
std::vector<FLASHHelperClasses::Tag> tags;
std::vector<int> indices;
if (hit.metaValueExists("TagIndices")) indices = (std::vector<int>)hit.getMetaValue("TagIndices").toIntList();
for (const int& index : indices)
{
if (! tagindices.empty()) tagindices += ";";
tagindices += std::to_string(index);
cntr++;
}
std::vector<double> mod_masses = hit.getMetaValue("Modifications");
std::vector<int> mod_starts = hit.getMetaValue("ModificationStarts");
std::vector<int> mod_ends = hit.getMetaValue("ModificationEnds");
std::vector<String> mod_ids = hit.getMetaValue("ModificationIDs");
std::vector<String> mod_accs = hit.getMetaValue("ModificationACCs");
for (size_t i = 0; i < mod_masses.size(); i++) // Fixed signed/unsigned comparison issue
{
if (! modmasses.empty()) modmasses += ";";
modmasses += std::to_string(mod_masses[i]);
if (! modstarts.empty()) modstarts += ";";
modstarts += std::to_string(mod_starts[i]);
if (! modends.empty()) modends += ";";
modends += std::to_string(mod_ends[i]);
if (! modids.empty()) modids += ";";
modids += mod_ids[i];
if (! modaccs.empty()) modaccs += ";";
modaccs += mod_accs[i];
}
int start = hit.getMetaValue("StartPosition");
int end = hit.getMetaValue("EndPosition");
int start_in_seq = start < 0 ? 0 : (start - 1);
int end_in_seq = end < 0 ? hit.getSequence().size() : end;
// Use ProForma for sequence generation
String proformaStr = generateProFormaString(hit.getSequence().substr(start_in_seq, end_in_seq - start_in_seq), mod_masses, mod_starts, mod_ends,
mod_ids, mod_accs);
fs << hit.getMetaValue("Index") << "\t" << hit.getMetaValue("Scan") << "\t" << hit.getMetaValue("RT") << "\t" << hit.getMetaValue("NumMass")
<< "\t" << hit.getAccession() << "\t" << hit.getDescription() << "\t" << hit.getMetaValue("Mass") << "\t" << hit.getSequence() << "\t"
<< hit.getSequence().substr(start_in_seq, end_in_seq - start_in_seq) << "\t" << proformaStr << "\t" << hit.getMetaValue("MatchedAA") << "\t"
<< 100.0 * hit.getCoverage() << "\t" << start << "\t" << end << "\t" << cntr << "\t" << tagindices << "\t" << mod_masses.size() << "\t"
<< modmasses << "\t" << modids << "\t" << modaccs << "\t" << modstarts << "\t" << modends << "\t" << hit.getScore() << "\t"
<< std::to_string((hit.metaValueExists("qvalue") ? (double)hit.getMetaValue("qvalue") : -1)) << "\t"
<< std::to_string((hit.metaValueExists("proqvalue") ? (double)hit.getMetaValue("proqvalue") : -1)) << "\n";
}
}
void OpenMS::FLASHTnTFile::writeProteoforms(const std::vector<ProteinHit>& hits, std::fstream& fs, double pro_fdr)
{
for (const auto& hit : hits)
{
if (! hit.metaValueExists("Index")) continue;
if (! hit.metaValueExists("Representative")) continue;
if (hit.metaValueExists("proqvalue") && (double)hit.getMetaValue("proqvalue") > pro_fdr) continue;
String tagindices = "", modmasses = "", modstarts = "", modends = "", modids = "", modaccs = "";
int cntr = 0;
std::vector<FLASHHelperClasses::Tag> tags;
std::vector<int> indices;
if (hit.metaValueExists("TagIndices")) indices = (std::vector<int>)hit.getMetaValue("TagIndices").toIntList();
for (const int& index : indices)
{
if (! tagindices.empty()) tagindices += ";";
tagindices += std::to_string(index);
cntr++;
}
std::vector<double> mod_masses = hit.getMetaValue("Modifications");
std::vector<int> mod_starts = hit.getMetaValue("ModificationStarts");
std::vector<int> mod_ends = hit.getMetaValue("ModificationEnds");
std::vector<String> mod_ids = hit.getMetaValue("ModificationIDs");
std::vector<String> mod_accs = hit.getMetaValue("ModificationACCs");
for (size_t i = 0; i < mod_masses.size(); i++) // Fixed signed/unsigned comparison issue
{
if (! modmasses.empty()) modmasses += ";";
modmasses += std::to_string(mod_masses[i]);
if (! modstarts.empty()) modstarts += ";";
modstarts += std::to_string(mod_starts[i]);
if (! modends.empty()) modends += ";";
modends += std::to_string(mod_ends[i]);
if (! modids.empty()) modids += ";";
modids += mod_ids[i];
if (! modaccs.empty()) modaccs += ";";
modaccs += mod_accs[i];
}
int start = hit.getMetaValue("StartPosition");
int end = hit.getMetaValue("EndPosition");
int start_in_seq = start < 0 ? 0 : (start - 1);
int end_in_seq = end < 0 ? hit.getSequence().size() : end;
// Use ProForma to generate ProForma string
String proformaStr = generateProFormaString(hit.getSequence().substr(start_in_seq, end_in_seq - start_in_seq), mod_masses, mod_starts, mod_ends,
mod_ids, mod_accs);
fs << hit.getMetaValue("Index") << "\t" << hit.getMetaValue("Scan") << "\t" << hit.getMetaValue("RT") << "\t" << hit.getMetaValue("NumMass")
<< "\t" << hit.getAccession() << "\t" << hit.getDescription() << "\t" << hit.getMetaValue("Mass") << "\t" << hit.getSequence() << "\t"
<< hit.getSequence().substr(start_in_seq, end_in_seq - start_in_seq) << "\t" << proformaStr << "\t" << hit.getMetaValue("MatchedAA") << "\t"
<< 100.0 * hit.getCoverage() << "\t" << start << "\t" << end << "\t" << cntr << "\t" << tagindices << "\t" << mod_masses.size() << "\t"
<< modmasses << "\t" << modids << "\t" << modaccs << "\t" << modstarts << "\t" << modends << "\t" << hit.getScore() << "\t"
<< std::to_string((hit.metaValueExists("proqvalue") ? (double)hit.getMetaValue("proqvalue") : -1)) << "\n";
}
}
}
// namespace OpenMS