-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
300 lines (222 loc) · 8.64 KB
/
main.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <fstream>
#include <iostream>
#include "main.h"
/*
This is a program which contains a parser for combiPeptData, calculatedRTs and peakAreas csv files outputted by Pescal++
Though really, this program fetches peptides which shared between Mascot and X!Tandem search results and outputs the calculated retention times and peak areas of these peptides
*/
int main(){
//We will fetch all the sequence, modification position, and charge for each peptide and store them in vectors
//...said vectors...
std::vector<std::string> mascotSequences;
std::vector<std::string> mascotModPos;
std::vector<std::string> mascotCharges;
//this function fills said vectors
getMascot(mascotSequences, mascotModPos, mascotCharges);
//more of said vectors, but these will hold the X!Tandem peptides
std::vector<std::string> xTandemSequences;
std::vector<std::string> xTandemModPos;
std::vector<std::string> xTandemCharges;
//this is another function filling said vectors, but this time for X!Tandem results
getXTandem(xTandemSequences, xTandemModPos, xTandemCharges);
//We'll store the peptide db IDs of peptides which are identical between each identification software in this handy vector of type pair
std::vector<std::pair<int, int> > matchedIndexes;
//Now we will fill the above handy vector. For every mascot sequence, modpos, and charge, check if there exists an xTandem equivalent. If there is, store the db ID from Mascot and X!Tandem in an int pair
//While we're here, we might as well output all these matched peptides to a file "sharedPeptides.csv"
std::ofstream file("sharedPeptides.csv");
int xTandemIndex=0;
for(int i=0, size=mascotSequences.size(); i<size; ++i){
int compare = mascotSequences[i].compare(xTandemSequences[xTandemIndex]);
while(compare>0 && xTandemIndex<xTandemSequences.size()){
++xTandemIndex;
compare = mascotSequences[i].compare(xTandemSequences[xTandemIndex]);
}
bool found=0;
int tempIndex=xTandemIndex;
while(compare==0&&!found){
if(mascotModPos[i]==xTandemModPos[tempIndex] && mascotCharges[i]==xTandemCharges[tempIndex]){
found=1;
std::pair<int, int> indexes;
indexes.first = i;
indexes.second = tempIndex;
matchedIndexes.push_back(indexes);
//this is the bit where we output the matched peptides to the file
file << mascotSequences[i] << "," << mascotModPos[i] << "," << mascotCharges[i] << "\n";
}
if(tempIndex<xTandemSequences.size()){
++tempIndex;
compare = mascotSequences[i].compare(xTandemSequences[tempIndex]);
} else{
break;
}
}
}
//output all the matched db IDs to a file "indexes.csv"
std::ofstream indexFile("indexes.csv");
for(int i=0, size=matchedIndexes.size(); i<size; ++i){
indexFile << matchedIndexes[i].first << "," << matchedIndexes[i].second << "\n";
}
//Now we have a list of indexes of shared peptides, let's get the quant for each
std::vector<double> mascotQuants;
std::vector<double> xTandemQuants;
getMascotQuants(mascotQuants);
getXTandemQuants(xTandemQuants);
//output the quant for each matched peptide to a file "comparedQuants.csv"
std::ofstream quantFile("comparedQuants.csv");
for(int i=0, size=matchedIndexes.size(); i<size; ++i){
if(!(mascotQuants[matchedIndexes[i].first]==0 || xTandemQuants[matchedIndexes[i].second]==0)){
quantFile << matchedIndexes[i].first << "," << mascotQuants[matchedIndexes[i].first] << "," << xTandemQuants[matchedIndexes[i].second] << "\n";
}
}
//Let's get the retention times of each matched peptide
std::vector<double> mascotRTs;
std::vector<double> xTandemRTs;
getMascotRTs(mascotRTs);
getXTandemRTs(xTandemRTs);
//And store these retention times to file "comparedRTs.csv"
std::ofstream RTFile("comparedRTs.csv");
for(int i=0, size=matchedIndexes.size(); i<size; ++i){
RTFile << matchedIndexes[i].first << "," << mascotRTs[matchedIndexes[i].first] << "," << xTandemRTs[matchedIndexes[i].second] << "\n";
}
}
void getMascot(std::vector<std::string> &mascotSequences, std::vector<std::string> &mascotModPos, std::vector<std::string> &mascotCharges){
std::ifstream file("F:\\data\\CTAM\\analysis\\mascotPosOfMod\\combiPeptData.csv");
std::string value;
//first getline just moves the cursor past the first row, since we don't want the headers
std::getline(file, value, '\n');
for(int peptide=0; peptide<45187; ++peptide){
//skip the first five quatation marks so that we get the sequence
for(int i=0; i<5; ++i){
std::getline(file, value, '\"');
}
//get the sequence
std::getline(file, value, '\"');
mascotSequences.push_back(value);
//skip the next 7 quatation marks to get the charge
for(int i=0; i<7; ++i){
std::getline(file, value, '\"');
}
//get the charge
std::getline(file, value, '\"');
mascotCharges.push_back(value);
//skip the next quatation mark to get the modPos string
std::getline(file, value, '\"');
//get the modPos string
std::getline(file, value, '\"');
mascotModPos.push_back(value);
std::getline(file, value, '\n');
}
}
void getXTandem(std::vector<std::string> &xTandemSequences, std::vector<std::string> &xTandemModPos, std::vector<std::string> &xTandemCharges){
std::ifstream file("F:\\data\\CTAM\\analysis\\peptideShakerPosOfMod\\combiPeptData.csv");
std::string value;
//first getline just moves the cursor past the first row, since we don't want the headers
std::getline(file, value, '\n');
for(int peptide=0; peptide<59410; ++peptide){
//skip the first five quatation marks so that we get the sequence
for(int i=0; i<5; ++i){
std::getline(file, value, '\"');
}
//get the sequence
std::getline(file, value, '\"');
xTandemSequences.push_back(value);
//skip the next 7 quatation marks to get the charge
for(int i=0; i<7; ++i){
std::getline(file, value, '\"');
}
//get the charge
std::getline(file, value, '\"');
xTandemCharges.push_back(value);
//skip the next quatation mark to get the modPos string
std::getline(file, value, '\"');
//get the modPos string
std::getline(file, value, '\"');
std::size_t found=0;
while(found!=std::string::npos){
found = value.find("4", found+1);
if(found!=std::string::npos){
value[found] = '3';
}
}
xTandemModPos.push_back(value);
std::getline(file, value, '\n');
}
}
void getMascotQuants(std::vector<double> &mascotQuants){
std::ifstream file("F:\\data\\CTAM\\analysis\\mascotPosOfMod\\peakAreas.csv");
std::string value;
//not interested in first line
std::getline(file, value, '\n');
for(int i=0; i<45187; ++i){
for(int j=0; j<352; ++j){
std::getline(file, value, '\"');
std::getline(file, value, '\"');
}
//get quant value
std::getline(file, value, '\"');
std::getline(file, value, '\"');
mascotQuants.push_back(std::stod(value));
std::getline(file, value, '\n');
}
}
void getXTandemQuants(std::vector<double> &xTandemQuants){
std::ifstream file("F:\\data\\CTAM\\analysis\\peptideShakerPosOfMod\\peakAreas.csv");
std::string value;
//not interested in first line
//std::getline(file, value, '\n');
for(int i=0; i<59410; ++i){
for(int j=0; j<70; ++j){
std::getline(file, value, '\"');
std::getline(file, value, '\"');
}
//get quant value
std::getline(file, value, '\"');
std::getline(file, value, '\"');
xTandemQuants.push_back(std::stod(value));
std::getline(file, value, '\n');
}
}
void getMascotRTs(std::vector<double> &mascotRTs){
std::ifstream file("F:\\data\\CTAM\\analysis\\mascotPosOfMod\\calculatedRTs.csv");
std::string value;
//not interested in first line
std::getline(file, value, '\n');
for(int i=0; i<45187; ++i){
for(int j=0; j<352; ++j){
std::getline(file, value, '\"');
std::getline(file, value, '\"');
}
//get quant value
std::getline(file, value, '\"');
std::getline(file, value, '\"');
mascotRTs.push_back(std::stod(value));
std::getline(file, value, '\n');
}
}
void getXTandemRTs(std::vector<double> &xTandemRTs){
std::ifstream file("F:\\data\\CTAM\\analysis\\peptideShakerPosOfMod\\calculatedRTs.csv");
std::string value;
//not interested in first line
std::getline(file, value, '\n');
for(int i=0; i<59410; ++i){
for(int j=0; j<257; ++j){
std::getline(file, value, '\"');
std::getline(file, value, '\"');
}
//get quant value
std::getline(file, value, '\"');
std::getline(file, value, '\"');
xTandemRTs.push_back(std::stod(value));
std::getline(file, value, '\n');
}
}
template<class Iter, class T>
Iter vectorBinarySearch(Iter begin, Iter end, T &val){
// Finds the lower bound in at most log(last - first) + 1 comparisons
Iter i = std::lower_bound(begin, end, val);
if (i != end && !(val < *i))
return i; // found
else
std::cout << "error: vector binary search found no matches!\n";
return end; // not found
}