Skip to content

Commit

Permalink
Merge pull request nglviewer#979 from papillot/fix-calculate-chain-names
Browse files Browse the repository at this point in the history
Fix calculate chain names
  • Loading branch information
fredludlow committed May 26, 2023
2 parents a01d79c + 2d7dc8d commit 83cea20
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/structure/structure-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,13 @@ interface ChainData {
rCount: number
}

/**
* When no chain names are set for the given structure, calculates
* chains based on:
* - polymer connectivity: when adjacent residues are not bonded, a new chain is created.
* - non polymer chemical type: adjacent residues which are not polymers but are of the same
* chemical type (e.g. water molecules) are grouped into the same chain.
**/
export function calculateChainnames (structure: Structure, useExistingBonds = false) {
if (Debug) Log.time('calculateChainnames')

Expand Down Expand Up @@ -486,14 +493,15 @@ export function calculateChainnames (structure: Structure, useExistingBonds = fa
//

chainStore.count = 0
modelStore.chainCount.fill(0, 0, modelStore.count)
modelStore.chainOffset.fill(0, 0, modelStore.count)
chainData.forEach(function (d) {
addChain(d.mIndex, d.chainname, d.rStart, d.rCount)
})

let chainOffset = 0
structure.eachModel(function (mp) {
modelStore.chainOffset[ mp.index ] = chainOffset
modelStore.chainCount[ mp.index ] -= 1
chainOffset += modelStore.chainCount[ mp.index ]
})
}
Expand Down
75 changes: 75 additions & 0 deletions test/data/noChainNameTerRecords.pdb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
ATOM 80 N GLU 1 5.065 -16.537 19.510 1.00 0.00
ATOM 81 H GLU 1 4.515 -16.433 20.351 1.00 0.00
ATOM 82 CA GLU 1 6.268 -17.344 19.571 1.00 0.00
ATOM 83 HA GLU 1 7.113 -16.760 19.208 1.00 0.00
ATOM 84 CB GLU 1 6.565 -17.797 21.002 1.00 0.00
ATOM 85 HB2 GLU 1 6.584 -16.921 21.650 1.00 0.00
ATOM 86 HB3 GLU 1 5.773 -18.472 21.328 1.00 0.00
ATOM 87 CG GLU 1 7.906 -18.514 21.078 1.00 0.00
ATOM 88 HG2 GLU 1 7.890 -19.330 20.356 1.00 0.00
ATOM 89 HG3 GLU 1 8.677 -17.796 20.797 1.00 0.00
ATOM 90 CD GLU 1 8.207 -19.065 22.455 1.00 0.00
ATOM 91 OE1 GLU 1 7.690 -18.570 23.473 1.00 0.00
ATOM 92 OE2 GLU 1 8.993 -20.035 22.480 1.00 0.00
ATOM 93 C GLU 1 6.092 -18.554 18.657 1.00 0.00
ATOM 94 O GLU 1 5.041 -19.212 18.706 1.00 0.00
ATOM 95 N THR 2 7.068 -18.844 17.812 1.00 0.00
ATOM 96 H THR 2 7.832 -18.200 17.667 1.00 0.00
ATOM 97 CA THR 2 7.021 -20.068 16.993 1.00 0.00
ATOM 98 HA THR 2 6.433 -20.780 17.572 1.00 0.00
ATOM 99 CB THR 2 6.350 -19.878 15.620 1.00 0.00
ATOM 100 HB THR 2 7.000 -19.269 14.991 1.00 0.00
ATOM 101 CG2 THR 2 6.108 -21.225 14.947 1.00 0.00
ATOM 102 HG21 THR 2 5.458 -21.834 15.575 1.00 0.00
ATOM 103 HG22 THR 2 5.633 -21.068 13.978 1.00 0.00
ATOM 104 HG23 THR 2 7.060 -21.737 14.806 1.00 0.00
ATOM 105 OG1 THR 2 5.041 -19.283 15.729 1.00 0.00
ATOM 106 HG1 THR 2 4.661 -19.183 14.853 1.00 0.00
ATOM 107 C THR 2 8.441 -20.605 16.832 1.00 0.00
ATOM 108 O THR 2 8.767 -21.337 15.884 1.00 0.00
ATOM 109 OXT THR 2 9.330 -20.261 17.608 1.00 0.00
TER
ATOM 110 N GLY 3 0.878 -15.170 13.641 1.00 0.00
ATOM 111 H1 GLY 3 1.078 -14.318 14.145 1.00 0.00
ATOM 112 H2 GLY 3 1.261 -15.949 14.157 1.00 0.00
ATOM 113 H3 GLY 3 -0.097 -15.170 13.377 1.00 0.00
ATOM 114 CA GLY 3 1.619 -15.138 12.383 1.00 0.00
ATOM 115 HA2 GLY 3 2.284 -16.001 12.358 1.00 0.00
ATOM 116 HA3 GLY 3 0.903 -15.208 11.564 1.00 0.00
ATOM 117 C GLY 3 2.444 -13.872 12.211 1.00 0.00
ATOM 118 O GLY 3 3.130 -13.412 13.134 1.00 0.00
ATOM 119 N GLN 4 2.399 -13.231 11.036 1.00 0.00
ATOM 120 H GLN 4 1.970 -13.673 10.235 1.00 0.00
ATOM 121 CA GLN 4 3.012 -11.902 10.856 1.00 0.00
ATOM 122 HA GLN 4 3.638 -11.655 11.713 1.00 0.00
ATOM 123 CB GLN 4 3.885 -11.910 9.599 1.00 0.00
ATOM 124 HB2 GLN 4 3.316 -11.439 8.797 1.00 0.00
ATOM 125 HB3 GLN 4 4.774 -11.316 9.810 1.00 0.00
ATOM 126 CG GLN 4 4.302 -13.317 9.169 1.00 0.00
ATOM 127 HG2 GLN 4 4.167 -13.999 10.008 1.00 0.00
ATOM 128 HG3 GLN 4 3.673 -13.636 8.338 1.00 0.00
ATOM 129 CD GLN 4 5.754 -13.372 8.728 1.00 0.00
ATOM 130 OE1 GLN 4 6.601 -13.915 9.595 1.00 0.00
ATOM 131 NE2 GLN 4 6.109 -12.934 7.628 1.00 0.00
ATOM 132 HE21 GLN 4 7.081 -12.982 7.358 1.00 0.00
ATOM 133 HE22 GLN 4 5.425 -12.530 7.004 1.00 0.00
ATOM 134 C GLN 4 1.955 -10.813 10.763 1.00 0.00
ATOM 135 OXT GLN 4 0.899 -11.080 10.157 1.00 0.00
TER
ATOM 136 Na+ Na+ 5 5.700 -12.918 30.268 1.00 0.00
TER
ATOM 137 Na+ Na+ 6 18.319 -15.431 38.033 1.00 0.00
TER
ATOM 138 O WAT 7 3.173 -9.533 29.721 1.00 0.00
ATOM 139 H1 WAT 7 4.130 -9.533 29.721 1.00 0.00
ATOM 140 H2 WAT 7 2.933 -8.606 29.721 1.00 0.00
TER
ATOM 141 O WAT 8 19.967 -17.141 38.257 1.00 0.00
ATOM 142 H1 WAT 8 20.924 -17.141 38.257 1.00 0.00
ATOM 143 H2 WAT 8 19.727 -16.214 38.257 1.00 0.00
TER
ATOM 144 O WAT 9 6.348 -9.617 28.564 1.00 0.00
ATOM 145 H1 WAT 9 7.305 -9.617 28.564 1.00 0.00
ATOM 146 H2 WAT 9 6.108 -8.690 28.564 1.00 0.00
TER
END
21 changes: 21 additions & 0 deletions test/structure/tests-structure-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { guessElement } from '../../src/structure/structure-utils'
import StringStreamer from '../../src/streamer/string-streamer'
import PdbParser from '../../src/parser/pdb-parser'

import { join } from 'path'
import * as fs from 'fs'
import { Structure } from '../../src/ngl'

describe('structure-utils/guessElement', function () {
describe('basic', function () {
Expand Down Expand Up @@ -28,3 +33,19 @@ describe('structure-utils/guessElement', function () {

})
})

describe('structure-utils/calculateChainNames', function () {
it('Calculates new chain names when none is set', function () {
var file = join(__dirname, '/../data/noChainNameTerRecords.pdb')
var str = fs.readFileSync(file, 'utf-8')
var streamer = new StringStreamer(str)
var pdbParser = new PdbParser(streamer)
return pdbParser.parse().then(function (structure: Structure) {
const chainNames: string[] = []
structure.eachChain(cp => chainNames.push(cp.chainname))

expect(chainNames).toEqual(['A', 'B', 'C', 'D'])
expect(structure.modelStore.chainCount[0]).toBe(structure.chainStore.count)
})
})
})

0 comments on commit 83cea20

Please sign in to comment.