-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubstitution-matrix.js
More file actions
59 lines (46 loc) · 1.66 KB
/
substitution-matrix.js
File metadata and controls
59 lines (46 loc) · 1.66 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
async function fetchSubMat(option='BLOSUM62') {
const url = `https://www.ncbi.nlm.nih.gov/IEB/ToolBox/C_DOC/lxr/source/data/${option}`
const parser = new DOMParser()
let response = await fetch(url)
let raw_html = await response.text()
let doc = parser.parseFromString(raw_html, 'text/html')
return doc.querySelector('.filecontent-src').innerHTML
}
async function parse(text) {
let subMat = {};
// Clean a bit
text = text.replace(/\r/g, '').split('\n');
// In this structure, AA's for the target subs are always on the second line
let aminoAcids = text[1].split(' ').filter(item => item !== '')
// Parsing happens here. Clean each line, then zip it into an object
// After zipping, add it to subMat
text.filter(item => (!item.startsWith('#') && !item.startsWith(' ')))
.forEach(item => {
let line = item.split(' ');
let from = line.shift();
line = line.filter(item => item !== '');
let line_dict;
line_dict = Object.fromEntries(
aminoAcids.map((key, i) => [key.toString(), Number(line[i])])
);
subMat[from] = line_dict;
});
return subMat
}
export function getSubMat(option='BLOSUM62') {
if (option === 'BLOSUM62') {
return loadBLOSUM62()
} else if (option === 'PAM250') {
return loadPAM250()
} else {
console.error('Invalid substitution matrix selected')
}
}
async function loadBLOSUM62() {
let subMat = await fetchSubMat('BLOSUM62')
return parse(subMat);
}
async function loadPAM250() {
let subMat = await fetchSubMat('PAM250')
return parse(subMat);
}