-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniProtXML2PEFF.cpp
More file actions
605 lines (517 loc) · 17.8 KB
/
UniProtXML2PEFF.cpp
File metadata and controls
605 lines (517 loc) · 17.8 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#include <tinyxml2.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <regex>
#include <cstdlib>
#include <algorithm>
using namespace tinyxml2;
using namespace std;
/* -------------------------------
PSI-MOD mapping
-------------------------------- */
std::map<std::string, std::string> PSI_MAP = {
/* =========================
Phosphorylation
========================= */
{"Phosphoserine", "MOD:00046"},
{"Phosphothreonine", "MOD:00047"},
{"Phosphotyrosine", "MOD:00048"},
/* =========================
Acetylation / formylation
========================= */
{"N-acetylalanine", "MOD:00394"},
{"N-acetylaspartate", "MOD:00394"},
{"N-acetylcysteine", "MOD:00394"},
{"N-acetylglutamate", "MOD:00394"},
{"N-acetylglycine", "MOD:00394"},
{"N-acetylmethionine", "MOD:00394"},
{"N-acetylproline", "MOD:00394"},
{"N-acetylserine", "MOD:00394"},
{"N-acetylthreonine", "MOD:00394"},
{"N-acetyltyrosine", "MOD:00394"},
{"N-formylmethionine", "MOD:00160"},
/* =========================
Methylation
========================= */
{"Dimethylated arginine", "MOD:00638"},
{"Asymmetric dimethylarginine", "MOD:00077"},
{"Asymmetric dimethylarginine; by PRMT1", "MOD:00077"},
{"Symmetric dimethylarginine", "MOD:00076"},
{"Trimethyllysine", "MOD:00083"},
{"Lysine methyl ester", "MOD:00323"},
{"Leucine methyl ester", "MOD:00304"},
{"Aspartate methyl ester", "MOD:00407"},
{"Cysteine methyl ester", "MOD:00114"},
{"Glutamate methyl ester (Gln)", "MOD:00407"},
/* =========================
Oxidation / hydroxylation
========================= */
{"Methionine sulfoxide", "MOD:00719"},
{"Methionine sulfone", "MOD:00256"},
{"3-hydroxyproline", "MOD:00038"},
{"4-hydroxyproline", "MOD:00039"},
{"3,4-dihydroxyproline", "MOD:00287"},
{"4-hydroxylysine", "MOD:00240"},
{"3-hydroxyphenylalanine", "MOD:01385"},
{"3-hydroxytryptophan", "MOD:00327"},
{"3-hydroxytryptophan; by autocatalysis", "MOD:00327"},
{"3,4-dihydroxyarginine", "MOD:00374"},
{"4-hydroxyarginine", "MOD:00220"},
/* =========================
Deamidation
========================= */
{"Deamidated asparagine", "MOD:00684"},
{"Deamidated glutamine", "MOD:00685"},
{"Citrulline", "MOD:00219"},
/* =========================
Lipidation
========================= */
{"Myristoylation", "MOD:00438"},
{"Farnesylation", "MOD:00437"},
{"Geranylgeranylation", "MOD:00441"},
/* =========================
Sulfation
========================= */
{"Sulfocysteine", "MOD:00180"},
{"Sulfothreonine", "MOD:00180"},
{"Sulfotyrosine", "MOD:00367"},
/* =========================
ADP-ribosylation
========================= */
{"ADP-ribosylarginine", "MOD:00177"},
{"ADP-ribosylcysteine", "MOD:00178"},
{"ADP-ribosylserine", "MOD:00242"},
{"ADP-ribosylarginine", "MOD:00177"}
};
/* ============================================================ */
unordered_map<string, size_t> PTM_COUNTS;
unordered_map<string, size_t> VARIANT_SKIPPED;
unordered_map<string, size_t> VARIANT_COMPLEX;
vector<string> OUTPUT_SIMPLE;
/* ============================================================
Helpers
============================================================ */
static bool is_valid_aa(char c)
{
return (c >= 'A' && c <= 'Z' && c != 'B' && c != 'J' && c != 'O' && c != 'U' && c != 'X' && c != 'Z');
}
// Safe string getter - returns empty string if pointer is null
static string safe_string(const char* str)
{
return str ? string(str) : string();
}
string safeText(XMLElement* element)
{
return (element && element->GetText()) ? string(element->GetText()) : "";
}
/* ============================================================
Parse modified residues
============================================================ */
vector<pair<int, string>> parse_modres(XMLElement* entry, bool strict)
{
vector<pair<int, string>> mods;
for (auto* feat = entry->FirstChildElement("feature");
feat; feat = feat->NextSiblingElement("feature"))
{
// Safe attribute access with default value
string feat_type = safe_string(feat->Attribute("type"));
if (feat_type != "modified residue")
continue;
const char* desc = feat->Attribute("description");
auto* locElem = feat->FirstChildElement("location");
auto* posElem = locElem ? locElem->FirstChildElement("position") : nullptr;
// Skip if description or position is missing
if (!desc || !posElem) continue;
string d(desc);
PTM_COUNTS[d]++;
auto it = PSI_MAP.find(d);
if (it == PSI_MAP.end())
{
if (strict)
{
cerr << "ERROR: Unmapped PTM: " << d << endl;
exit(1);
}
continue;
}
mods.emplace_back(posElem->IntAttribute("position"), it->second);
}
return mods;
}
/* ============================================================
Parse VariantSimple
============================================================ */
vector<string> parse_variants(XMLElement* entry)
{
vector<string> variants;
regex simple_sub(R"(([A-Z])\s*->\s*([A-Z]))");
for (auto* feat = entry->FirstChildElement("feature");
feat; feat = feat->NextSiblingElement("feature"))
{
// Safe attribute access with default value
string feat_type = safe_string(feat->Attribute("type"));
if (feat_type != "sequence variant")
continue;
const char* desc = feat->Attribute("description");
auto* locElem = feat->FirstChildElement("location");
auto* posElem = locElem ? locElem->FirstChildElement("position") : nullptr;
// Get original and variation elements
auto* origElem = feat->FirstChildElement("original");
auto* varElem = feat->FirstChildElement("variation");
// Skip if position is missing
if (!posElem)
{
VARIANT_SKIPPED["complex_location"]++;
continue;
}
// Check if SGRP variant (skip these)
if (desc && string(desc).find("SGRP") != string::npos)
{
VARIANT_SKIPPED["SGRP"]++;
continue;
}
char ref = '\0';
char alt = '\0';
// Try to parse from <original> and <variation> elements first
if (origElem && varElem)
{
const char* origText = origElem->GetText();
const char* varText = varElem->GetText();
if (origText && varText && strlen(origText) == 1 && strlen(varText) == 1)
{
ref = origText[0];
alt = varText[0];
}
}
// If that didn't work, try parsing from description
if (ref == '\0' && desc)
{
string d(desc);
smatch m;
if (regex_search(d, m, simple_sub))
{
ref = m[1].str()[0];
alt = m[2].str()[0];
}
}
// If we still don't have valid ref/alt, skip
if (ref == '\0' || alt == '\0')
{
VARIANT_SKIPPED["non_simple"]++;
continue;
}
if (!is_valid_aa(ref) || !is_valid_aa(alt))
{
VARIANT_SKIPPED["invalid_aa"]++;
continue;
}
int pos = posElem->IntAttribute("position");
variants.push_back("(" + to_string(pos) + "|" + alt + ")");
}
return variants;
}
/* ============================================================
Parse VariantComplex
Handles: insertions, deletions, multi-residue substitutions,
and splice variants
Format: Each variant is (StartPos|EndPos|Sequence)
- Deletions: (pos1|pos2|) - empty sequence
- Insertions: (pos|pos|INSERTED_SEQ)
- Substitutions: (pos1|pos2|VARIANT_SEQ)
============================================================ */
vector<string> parse_complex_variants(XMLElement* entry)
{
vector<string> simple_vars;
vector<string> complex_vars;
for (auto* feat = entry->FirstChildElement("feature"); feat; feat = feat->NextSiblingElement("feature"))
{
string feat_type = safe_string(feat->Attribute("type"));
if (feat_type != "sequence variant" && feat_type != "splice variant" && feat_type != "mutagenesis site")
{
continue;
}
const char* desc = feat->Attribute("description");
auto* locElem = feat->FirstChildElement("location");
if (!locElem)
{
VARIANT_SKIPPED["complex_location"]++;
continue;
}
auto* origElem = feat->FirstChildElement("original");
auto* varElem = feat->FirstChildElement("variation");
const char* origText = origElem ? origElem->GetText() : nullptr;
const char* varText = varElem ? varElem->GetText() : nullptr;
if (!origText || !varText)
{
VARIANT_SKIPPED["non_simple"]++;
continue;
}
auto* posElem = locElem->FirstChildElement("position");
auto* beginElem = locElem->FirstChildElement("begin");
auto* endElem = locElem->FirstChildElement("end");
int start_pos = 0;
int end_pos = 0;
if (posElem)
{
start_pos = end_pos = posElem->IntAttribute("position");
}
else if (beginElem && endElem)
{
start_pos = beginElem->IntAttribute("position");
end_pos = endElem->IntAttribute("position");
}
else if (beginElem || endElem)
{
start_pos = beginElem ? beginElem->IntAttribute("position") : endElem->IntAttribute("position");
end_pos = endElem ? endElem->IntAttribute("position") : start_pos;
}
else
{
VARIANT_SKIPPED["complex_location"]++;
continue;
}
// Determine if this should be VariantSimple
if (safe_string(origText).length() == 1 && safe_string(varText).length() == 1 &&
is_valid_aa(origText[0]) && is_valid_aa(varText[0]) &&
start_pos == end_pos)
{
string simple_entry = "(" + to_string(start_pos) + "|" + safe_string(varText) + ")";
simple_vars.push_back(simple_entry);
continue; // Skip processing as VariantComplex
}
// If not simple, classify as VariantComplex
string complex_entry = "(" + to_string(start_pos) + "|" + to_string(end_pos) + "|" + safe_string(varText) + ")";
complex_vars.push_back(complex_entry);
if (feat_type == "mutagenesis site")
{
VARIANT_COMPLEX["mutagenesis"]++;
}
else if (feat_type == "sequence variant")
{
VARIANT_COMPLEX["sequence_variant"]++;
}
}
// Pass simple_vars to OUTPUT_SIMPLE
OUTPUT_SIMPLE = simple_vars;
return complex_vars;
}
/* ============================================================
Main
============================================================ */
int main(int argc, char* argv[])
{
if (argc < 3)
{
cerr << "Usage: " << argv[0] << " input.xml output.peff [options]\n";
cerr << "Options:\n";
cerr << " --strict Exit on unmapped PTMs (default: skip)\n";
cerr << " --no-ptms Disable PTM processing (default: enabled)\n";
cerr << " --variant-simple Enable VariantSimple processing (default: disabled)\n";
cerr << " --variant-complex Enable VariantComplex processing (default: disabled)\n";
return 1;
}
// Parse command-line options
bool strict = false;
bool enable_ptms = true;
bool enable_variant_simple = false;
bool enable_variant_complex = false;
for (int i = 3; i < argc; i++)
{
string arg(argv[i]);
if (arg == "--strict")
{
strict = true;
}
else if (arg == "--no-ptms")
{
enable_ptms = false;
}
else if (arg == "--variant-simple")
{
enable_variant_simple = true;
}
else if (arg == "--variant-complex")
{
enable_variant_complex = true;
}
else
{
cerr << "Unknown option: " << arg << endl;
return 1;
}
}
XMLDocument doc;
if (doc.LoadFile(argv[1]) != XML_SUCCESS)
{
cerr << "Failed to load XML\n";
return 1;
}
ofstream peff(argv[2]);
if (!peff.is_open())
{
cerr << "Failed to open output file: " << argv[2] << endl;
return 1;
}
peff << "# PEFF 1.0 generated by https://github.com/UWPR/UniProtXML2PEFF\n";
peff << "# VariantSimple=" << (enable_variant_simple || enable_variant_complex ? "true" : "false") << "\n";
peff << "# VariantComplex=" << (enable_variant_complex ? "true" : "false") << "\n";
peff << "# ModResPsi=" << (enable_ptms ? "true" : "false") << "\n";
XMLElement* root = doc.RootElement();
if (!root)
{
cerr << "Invalid XML: no root element\n";
return 1;
}
size_t entries_processed = 0;
size_t entries_skipped = 0;
for (auto* entry = root->FirstChildElement("entry"); entry; entry = entry->NextSiblingElement("entry"))
{
auto* accElem = entry->FirstChildElement("accession");
auto* seqElem = entry->FirstChildElement("sequence");
if (!accElem || !seqElem)
{
cerr << "Entry missing ";
if (!accElem)
{
cerr << "<accession> ";
}
if (!seqElem)
{
cerr << "<sequence> ";
}
cerr << "- processing features within this entry regardless" << endl;
}
string acc = accElem ? accElem->GetText() : "UNKNOWN_ACCESSION";
string seq = seqElem ? seqElem->GetText() : "UNKNOWN_SEQUENCE";
seq.erase(remove(seq.begin(), seq.end(), '\n'), seq.end());
seq.erase(remove(seq.begin(), seq.end(), ' '), seq.end());
string db_type = "tr"; // Default to TrEMBL
const char* dataset = entry->Attribute("dataset");
if (dataset && string(dataset) == "Swiss-Prot")
{
db_type = "sp";
}
string entry_name = safe_string(entry->FirstChildElement("name") ? entry->FirstChildElement("name")->GetText() : "");
string recommendedName;
XMLElement* protein = entry->FirstChildElement("protein");
if (protein)
{
XMLElement* recName = protein->FirstChildElement("recommendedName");
if (recName)
{
recommendedName = safeText(recName->FirstChildElement("fullName"));
}
}
string organism;
XMLElement* organismElement = entry->FirstChildElement("organism");
if (organismElement)
{
for (XMLElement* nameElem = organismElement->FirstChildElement("name"); nameElem; nameElem = nameElem->NextSiblingElement("name"))
{
const char* type = nameElem->Attribute("type");
if (type && string(type) == "scientific")
{
organism = safeText(nameElem);
break;
}
}
}
string geneName;
XMLElement* geneElement = entry->FirstChildElement("gene");
if (geneElement)
{
for (XMLElement* nameElem = geneElement->FirstChildElement("name"); nameElem; nameElem = nameElem->NextSiblingElement("name"))
{
const char* type = nameElem->Attribute("type");
if (type && string(type) == "primary")
{
geneName = safeText(nameElem);
break;
}
}
}
// Clear OUTPUT_SIMPLE for this entry
OUTPUT_SIMPLE.clear();
// Parse features based on enabled options
vector<pair<int, string>> mods;
vector<string> vars;
vector<string> complex_vars;
if (enable_ptms)
{
mods = parse_modres(entry, strict);
}
if (enable_variant_simple)
{
vars = parse_variants(entry);
}
if (enable_variant_complex)
{
complex_vars = parse_complex_variants(entry);
}
peff << ">" << db_type << "|" << acc << "|" << entry_name;
if (!recommendedName.empty())
{
peff << " " << recommendedName;
}
if (!organism.empty())
{
peff << " OS=" << organism;
}
if (!geneName.empty())
{
peff << " GN=" << geneName;
}
// Write VariantSimple
// When VariantComplex is enabled, prefer OUTPUT_SIMPLE from parse_complex_variants (for compatibility)
// Otherwise, use vars from parse_variants if VariantSimple is enabled
vector<string> simple_output;
if (enable_variant_complex && !OUTPUT_SIMPLE.empty())
{
simple_output = OUTPUT_SIMPLE;
}
else if (enable_variant_simple)
{
simple_output = vars;
}
if (!simple_output.empty())
{
peff << " \\VariantSimple=";
for (size_t i = 0; i < simple_output.size(); ++i)
{
peff << simple_output[i];
}
}
// Write VariantComplex
if (!complex_vars.empty())
{
peff << " \\VariantComplex=";
for (size_t i = 0; i < complex_vars.size(); ++i)
{
peff << complex_vars[i];
}
}
if (!mods.empty())
{
peff << " \\ModResPsi=";
for (size_t i = 0; i < mods.size(); i++)
{
peff << "(" << mods[i].first << "|" << mods[i].second << ")";
}
}
peff << "\n";
if (!seq.empty() && seq != "UNKNOWN_SEQUENCE")
{
int iSequenceBreakLength = 60;
for (size_t i = 0; i < seq.length(); i += iSequenceBreakLength)
peff << seq.substr(i, iSequenceBreakLength) << "\n";
}
entries_processed++;
}
peff.close();
cerr << "Done. Processed " << entries_processed << " entries, skipped " << entries_skipped << " entries.\n";
return 0;
}