Skip to content

Commit

Permalink
Merge pull request #110 from Traksewt/add-multiple-residues
Browse files Browse the repository at this point in the history
add addResidues to MolView
  • Loading branch information
biasmv committed Jul 15, 2015
2 parents ed07279 + 56c1927 commit 0f255c6
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 24 deletions.
9 changes: 9 additions & 0 deletions doc/mol.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ Mol (and MolView)

:returns: the newly created :class:`pv.mol.ChainView` instance

.. function:: pv.mol.Mol.addResidues(residues, includeAllAtoms)

Adds all residues to their respective chain

:param residues: list of new residues
:param includeAllAtoms: when true, all atoms of the residue are directly added as new AtomViews to the residue. When set to false (the default), a new residue view is created with an empty list of atoms.

:returns: a map of chain name to chain for the affected chains with new residues.

.. function:: pv.mol.MolView.addAtom(atom)

Adds the given atom to the view. If the atom is already contained in the view, it is not added again. If an atom's residue or chain are not yet part of the view, they are added as well.
Expand Down
13 changes: 13 additions & 0 deletions src/mol/mol.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,19 @@ utils.derive(MolView, MolBase, {
return chain.containsResidue(residue);
},

addResidues : function (residues, recurse) {
var that = this;
var chainsViews = {};
residues.forEach(function (residue) {
var chainName = residue.chain().name();
if (typeof chainsViews[chainName] === 'undefined') {
chainsViews[chainName] = that.addChain(residue.chain(), false);
}
chainsViews[chainName].addResidue(residue, recurse);
});
return chainsViews;
},


chains : function() { return this._chains; },

Expand Down
23 changes: 23 additions & 0 deletions src/tests/mol/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,29 @@ test('residue select on structure', function(assert) {
assert.strictEqual(view.chains().length, 1);
});


test('add residues', function(assert) {
var rnums = [268,903,904,905];
var firstView = FRAGMENT.select({rnums : rnums });
assert.ok(firstView.chain('A')._rnumsOrdered === true);
var i, res, res2;
for (i = 0; i < rnums.length; ++i) {
res = firstView.chain('A').residueByRnum(rnums[i]);
assert.strictEqual(res.num(), rnums[i]);
}
var secondView = FRAGMENT.createEmptyView();
var r = [];
firstView.eachResidue(function(x) { r.push(x); });
secondView.addResidues(r, true);
var other = [];
secondView.eachResidue(function(x) { other.push(x); });
for (i = 0; i < rnums.length; ++i) {
res = firstView.chain('A').residueByRnum(rnums[i]);
res2 = secondView.chain('A').residueByRnum(rnums[i]);
assert.strictEqual(res.num(), res2.num());
}
});

test('atom select on structure', function(assert) {
var view = FRAGMENT.atomSelect(function(a) { return a.name() === 'CA'; });
var count = 0;
Expand Down
61 changes: 37 additions & 24 deletions src/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,30 +525,43 @@ Viewer.prototype = {
},

addListener : function(eventName, callback) {
var callbacks = this.listenerMap[eventName];
if (typeof callbacks === 'undefined') {
callbacks = [];
this.listenerMap[eventName] = callbacks;
}
if (callback === 'center') {
var cb = utils.bind(this._mouseHandler,
this._mouseHandler._centerOnClicked);
callbacks.push(cb);
} else {
callbacks.push(callback);
}
// in case viewer is already initialized, fire viewerReady immediately.
// Otherwise, the callback would never be invoked in this case:
//
// document.addEventListener('DOMContentLoaded', function() {
// viewer = pv.Viewer(...);
// viewer.on('viewerReady', function(viewer) {
// });
// });
if (this._initialized && eventName === 'viewerReady') {
// don't use dispatch here, we only want this very callback to be
// invoked.
callback(this, null);
if (eventName === 'keypress' ||
eventName === 'keydown' ||
eventName === 'keyup') {
// handle keypress events directly onto the parent domElement
// mouse downs will make it have focus
// this._domElement
document.addEventListener(eventName, utils.bind(this, callback),
false);

}
else {

var callbacks = this.listenerMap[eventName];
if (typeof callbacks === 'undefined') {
callbacks = [];
this.listenerMap[eventName] = callbacks;
}
if (callback === 'center') {
var cb = utils.bind(this._mouseHandler,
this._mouseHandler._centerOnClicked);
callbacks.push(cb);
} else {
callbacks.push(callback);
}
// in case viewer is already initialized, fire viewerReady immediately.
// Otherwise, the callback would never be invoked in this case:
//
// document.addEventListener('DOMContentLoaded', function() {
// viewer = pv.Viewer(...);
// viewer.on('viewerReady', function(viewer) {
// });
// });
if (this._initialized && eventName === 'viewerReady') {
// don't use dispatch here, we only want this very callback to be
// invoked.
callback(this, null);
}
}
},

Expand Down

0 comments on commit 0f255c6

Please sign in to comment.