-
Notifications
You must be signed in to change notification settings - Fork 0
/
UsiParser.js
137 lines (129 loc) · 4.46 KB
/
UsiParser.js
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
UsiResponse = class UsiResponse {
constructor(type) {
this.type = type;
this.sequence = '';
this.precursorCharge = 11588;
this.aMz = [];
this.aInt = [];
this.spectrum_name = '';
this.modifications = [];
// name
// index
// site
}
parseAttributes(response) {
this.precursorCharge = parseInt(response.attributes.filter((element) => element.name === 'charge state')[0].value);
this.spectrum_name = response.attributes.filter((element) => element.name === 'spectrum name')[0].value;
this.sequence = response.attributes.filter((element) => element.accession === 'MS:1000888')[0].value;
}
parseSpectrumName(spectrumNameString) {
// [iTRAQ4plex]-LHFFM[Oxidation]PGFAPLTSR/2
let arySpectrumName;
arySpectrumName = spectrumNameString.split('/');
let modString;
modString = arySpectrumName[0];
let nTerminalModification;
nTerminalModification = false;
let cTerminalModification;
cTerminalModification = false;
if (modString[0] === '[') {
nTerminalModification = true;
}
let positionTracker = 0;
if (nTerminalModification) {
const position = modString.indexOf(']', 0); // returns -1 if false
const modification = modString.substring(1, position);
const m = {
name: modification,
index: -1,
site: 'N-terminus',
};
this.modifications.push(m);
positionTracker = position + 2; // + 2 to get over ']-'
}
modString = modString.substring(positionTracker);
const check = modString.indexOf('-[', 0);
if (check !== -1) {
const modification = modString.substring(check + 2, modString.length - 1); // +2 to remove '-[' and length -1 for ']'
const m = {
name: modification,
index: this.sequence.length,
site: 'C-terminus',
};
this.modifications.push(m);
modString = modString.substring(0, check);
}
// parse Modifications within peptide
while (modString.indexOf('[', 0) !== -1) { // LHFFMPGFAPLTSR
const check = modString.indexOf('[', 0);
const position = modString.indexOf(']', 0); // returns -1 if false
const modification = modString.substring(check + 1, position);
positionTracker = position + 2; // + 2 to get over ']-'
const m = {
name: modification,
index: check,
site: modString[check - 1],
};
this.modifications.push(m);
modString = modString.substring(0, check)
+ modString.substring(position + 1);
}
this.modifications = this.modifications.map((el) => {
el.index += -1;
return el;
});
}
parseData(response) {
switch (this.type) {
case 'ProteomeCentral':
this.parseAttributes(response);
this.parseSpectrumName(this.spectrum_name);
this.aMz = response.mzs;
this.aInt = response.intensities;
break;
case 'PeptideAtlas':
this.parseAttributes(response);
this.parseSpectrumName(this.spectrum_name);
this.aMz = response.mzs;
this.aInt = response.intensities;
break;
case 'pride': // use data
this.sequence = response.peptideSequence;
this.precursorCharge = response.charge;
this.aMz = response.mzs;
this.aInt = response.intensities;
//
break;
case 'peptideatlas':
const regexp = /^(\s)*(?<mz>(\d)*\.(\d)*)(\s)*(?<intensity>(\d)*\.(\d)*)/gm;
const r = [...response.matchAll(regexp)]
.map((e) => ({
mz: parseFloat(e.groups.mz),
intensity: parseFloat(e.groups.intensity),
}));
const regexpPCM = /^(\s)*PeptideIon tag: (?<peptideSequence>([A-Z]*))\/(?<precursorCharge>(\d))/gm;
const r1 = [...response.matchAll(regexpPCM)]
.map((e) => ({
peptideSequence: e.groups.peptideSequence,
precursorCharge: e.groups.precursorCharge,
}));
this.sequence = r1[0].peptideSequence;
this.precursorCharge = r1[0].precursorCharge;
this.aMz = r.map((e) => (e.mz));
this.aInt = r.map((e) => (e.intensity));
//
break;
case 'jpost':
this.sequence = response.sequence;
this.precursorCharge = response.charge;
this.aMz = JSON.parse(response.ms2peaks).map((a) => (a[0]));
this.aInt = JSON.parse(response.ms2peaks).map((a) => (a[1]));
break;
default:
//
break;
}
}
};
// exports.UsiResponse = UsiResponse;
module.exports = { UsiResponse };