Skip to content

Commit 7e137f8

Browse files
committed
Fix producePdb atom num for >100k atoms
1 parent 9fa8538 commit 7e137f8

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎src/routes/projects/structure/produce-pdb/index.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ const producePdb = (topologyData, frameCoordinates, atomIndices) => {
5757
const tempFactor = '0.00';
5858
// Add 1 to de count and set the PDB index according to the count
5959
count += 1;
60-
const pdbCount = count.toString().padStart(5, ' ');
60+
// Wrap the displayed serial to 1 after 99999 to keep its five-column field fixed.
61+
const pdbCount = ((count - 1) % 99999 + 1).toString().padStart(5, ' ');
6162
// Add the new line to the PDB content
6263
pdbContent += `ATOM ${pdbCount} ${pdbAtomName} ${pdbResidueName}${pdbChainName}${pdbResidueNumber}` +
6364
`${pdbResidueIcode} ${xCoord}${yCoord}${zCoord} ${occupancy} ${tempFactor} ${pdbAtomElements}\n`;
@@ -66,4 +67,4 @@ const producePdb = (topologyData, frameCoordinates, atomIndices) => {
6667
return pdbContent;
6768
};
6869

69-
module.exports = producePdb;
70+
module.exports = producePdb;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const producePdb = require('.');
2+
3+
test('atom serial rollover preserves fixed columns and coordinate indexing', () => {
4+
const nAtoms = 200001;
5+
const topology = {
6+
atom_names: Array(nAtoms).fill('CA'),
7+
atom_elements: Array(nAtoms).fill('C'),
8+
atom_residue_indices: Array(nAtoms).fill(0),
9+
residue_names: ['ALA'],
10+
residue_numbers: [1],
11+
residue_chain_indices: [0],
12+
chain_names: ['A'],
13+
};
14+
const coordinates = Array(nAtoms).fill([1.25, -2.5, 3.75]);
15+
coordinates[99999] = [4.5, 5.25, -6.75];
16+
const lines = producePdb(topology, coordinates).trimEnd().split('\n').slice(1);
17+
18+
expect(lines).toHaveLength(nAtoms);
19+
for (const [index, serial] of [[0, ' 1'], [99998, '99999'],
20+
[99999, ' 1'], [100000, ' 2'], [199997, '99999'],
21+
[199998, ' 1'], [199999, ' 2'], [200000, ' 3']]) {
22+
const line = lines[index];
23+
expect(line).toHaveLength(78);
24+
expect(line.slice(6, 11)).toBe(serial);
25+
expect(line.slice(12, 16)).toBe(' CA ');
26+
expect(line.slice(17, 20)).toBe('ALA');
27+
expect(line[21]).toBe('A');
28+
expect(line.slice(22, 26)).toBe(' 1');
29+
expect([line.slice(30, 38), line.slice(38, 46), line.slice(46, 54)].map(Number))
30+
.toEqual(coordinates[index]);
31+
expect(line.slice(76, 78)).toBe(' C');
32+
}
33+
});

0 commit comments

Comments
 (0)