Skip to content

Commit

Permalink
add hetatm flag
Browse files Browse the repository at this point in the history
  • Loading branch information
biasmv committed Jan 31, 2015
1 parent fb0754c commit defe822
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 6 deletions.
6 changes: 6 additions & 0 deletions doc/mol.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ The Mol (and MolView) API
**Available Atom Predicates:**

* *aname* An atom is included iff the atom name it is equal to *aname*. To match against multiple atom names, use the plural forms cnames/chains.
* *hetatm* An atom is included iff the atom hetatm flag matches the provided value.

**Examples:**

Expand Down Expand Up @@ -359,6 +360,11 @@ The Atom (and AtomView) API

The actual coordinates of the atom.

.. function:: mol.Atom.isHetatm()
mol.AtomView.isHetatm()

Returns true when the atom was imported from a HETATM record, false if not. This flag is only meaningful for structures imported from PDB files and will return false for other file formats.


The Bond API
-----------------------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion src/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ PDBReader.prototype = {
if (alt_loc !== ' ' && alt_loc !== 'A') {
return true;
}
var isHetatm = line[0] === 'H';
var chainName = line[21];
var resName = line.substr(17, 3).trim();
var fullAtomName = line.substr(12, 4);
Expand Down Expand Up @@ -207,7 +208,7 @@ PDBReader.prototype = {
if (element === '') {
element = guessAtomElementFromName(fullAtomName);
}
var atom = this._currRes.addAtom(atomName, pos, element);
var atom = this._currRes.addAtom(atomName, pos, element, isHetatm);
// in case parseConect records is set to true, store away the atom serial
if (this._options.conectRecords) {
var serial = parseInt(line.substr(6,5).trim(), 10);
Expand Down
21 changes: 16 additions & 5 deletions src/mol.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ function _atomPredicates(dict) {
if (dict.aname !== undefined) {
predicates.push(function(a) { return a.name() === dict.aname; });
}
if (dict.hetatm !== undefined) {
predicates.push(function(a) { return a.isHetatm() === dict.hetatm; });
}
if (dict.anames !== undefined) {
predicates.push(function(a) {
var n = a.name();
Expand Down Expand Up @@ -341,7 +344,7 @@ MolBase.prototype = {
}
// when what is not one of the simple strings above, we assume what
// is a dictionary containing predicates which have to be fulfilled.
return _dictSelect(this, what);
return _dictSelect(this, what || {});
},

selectWithin : (function() {
Expand Down Expand Up @@ -948,9 +951,10 @@ derive(Residue, ResidueBase, {

full : function() { return this; },

addAtom : function(name, pos, element) {
addAtom : function(name, pos, element, isHetatm) {
var atom = new Atom(this, name, pos, element,
this.structure().nextAtomIndex());
this.structure().nextAtomIndex(),
isHetatm);
this._atoms.push(atom);
return atom;
},
Expand All @@ -969,10 +973,11 @@ derive(Residue, ResidueBase, {

});

function Atom(residue, name, pos, element, index) {
function Atom(residue, name, pos, element, index, isHetatm) {
AtomBase.call(this);
this._residue = residue;
this._bonds = [];
this._isHetatm = !!isHetatm;
this._name = name;
this._pos = pos;
this._index = index;
Expand All @@ -990,7 +995,10 @@ derive(Atom, AtomBase, {
full : function() { return this; },
qualifiedName : function() {
return this.residue().qualifiedName()+'.'+this.name();
}
},
isHetatm : function() {
return this._isHetatm;
},
});

var Bond = function(atom_a, atom_b) {
Expand Down Expand Up @@ -1200,6 +1208,9 @@ derive(AtomView, AtomBase, {
index : function() { return this._atom.index(); },
qualifiedName : function() {
return this._atom.qualifiedName();
},
isHetatm : function() {
return this._atom.isHetatm();
}
});

Expand Down
1 change: 1 addition & 0 deletions tests/all.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@
<script src="/tests/colors.js"></script>
<script src="/tests/pdb-io.js"></script>
<script src="/tests/viewer-render.js"></script>
<script src="/tests/mol-select.js"></script>
</body>
</html
21 changes: 21 additions & 0 deletions tests/mol-select.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var SELECT_HETATM='\
ATOM 3316 C GLY B 214 24.173 7.911 -3.276 1.00 94.23 C\n\
ATOM 3317 O GLY B 214 24.730 8.496 -4.208 1.00 94.94 O\n\
ATOM 3318 OXT GLY B 214 23.962 8.474 -2.196 1.00 95.71 O\n\
TER 3319 GLY B 214 \n\
HETATM 3320 PA AP5 A 215 18.089 46.955 20.531 1.00 17.77 P\n\
HETATM 3321 O1A AP5 A 215 17.885 47.954 21.576 1.00 16.47 O\n\
HETATM 3322 O2A AP5 A 215 18.847 47.325 19.359 1.00 15.16 O\n\
ATOM 3317 O GLY A 216 24.730 8.496 -4.208 1.00 94.94 O\n\
END\n\
'

test('dict select select by hetatm flag', function(assert) {
var structure = io.pdb(HETATM);
var view = structure.select({ hetatm: true });
assert.strictEqual(view.atomCount(), 3);
view = structure.select({ hetatm: false });
assert.strictEqual(view.atomCount(), 4);

});

37 changes: 37 additions & 0 deletions tests/pdb-io.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,41 @@ test('ignores conect records when conectRecords flag is not set', function(asser
});


var HETATM='\
ATOM 3316 C GLY B 214 24.173 7.911 -3.276 1.00 94.23 C\n\
ATOM 3317 O GLY B 214 24.730 8.496 -4.208 1.00 94.94 O\n\
ATOM 3318 OXT GLY B 214 23.962 8.474 -2.196 1.00 95.71 O\n\
TER 3319 GLY B 214 \n\
HETATM 3320 PA AP5 A 215 18.089 46.955 20.531 1.00 17.77 P\n\
HETATM 3321 O1A AP5 A 215 17.885 47.954 21.576 1.00 16.47 O\n\
HETATM 3322 O2A AP5 A 215 18.847 47.325 19.359 1.00 15.16 O\n\
ATOM 3317 O GLY A 216 24.730 8.496 -4.208 1.00 94.94 O\n\
END\n\
'

test('sets HETATM flag', function(assert) {
var structure = io.pdb(HETATM);
var atoms = [];
structure.eachAtom(function(a) { atoms.push(a) });
assert.strictEqual(atoms[0].isHetatm(), false);
assert.strictEqual(atoms[1].isHetatm(), false);
assert.strictEqual(atoms[2].isHetatm(), false);
assert.strictEqual(atoms[3].isHetatm(), true);
assert.strictEqual(atoms[4].isHetatm(), true);
assert.strictEqual(atoms[5].isHetatm(), true);
assert.strictEqual(atoms[6].isHetatm(), false);

// check that accessor also works for views. not strictly an IO test
var view = structure.select();
atoms = [];
view.eachAtom(function(a) { atoms.push(a) });
assert.strictEqual(atoms[0].isHetatm(), false);
assert.strictEqual(atoms[1].isHetatm(), false);
assert.strictEqual(atoms[2].isHetatm(), false);
assert.strictEqual(atoms[3].isHetatm(), true);
assert.strictEqual(atoms[4].isHetatm(), true);
assert.strictEqual(atoms[5].isHetatm(), true);
assert.strictEqual(atoms[6].isHetatm(), false);

});

0 comments on commit defe822

Please sign in to comment.