Advertisement
Guest User

Untitled

a guest
May 5th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "## Defining custom attributes with the new Topology system\n",
  8. "\n",
  9. "This notebook aims to explain how to augment an existing MDAnalysis Universe with additional data. This is designed to allow more advanced users to define extra properties for AtomGroups which can then be queried or selected according to.\n"
  10. ]
  11. },
  12. {
  13. "cell_type": "code",
  14. "execution_count": 1,
  15. "metadata": {
  16. "collapsed": true
  17. },
  18. "outputs": [],
  19. "source": [
  20. "import MDAnalysis as mda\n",
  21. "from MDAnalysis.core.topologyattrs import AtomAttr\n",
  22. "from MDAnalysis.core.selection import StringSelection\n",
  23. "import numpy as np"
  24. ]
  25. },
  26. {
  27. "cell_type": "code",
  28. "execution_count": 2,
  29. "metadata": {
  30. "collapsed": true
  31. },
  32. "outputs": [],
  33. "source": [
  34. "u = mda.Universe('./4E43.pdb')"
  35. ]
  36. },
  37. {
  38. "cell_type": "markdown",
  39. "metadata": {},
  40. "source": [
  41. "Define an Attribute class for the layer property. Here we subclass `AtomAttr` which is a shortcut for per-atom attributes.\n",
  42. "\n",
  43. "Also define an associated selection for layers. This uses the `StringSelection` subclass which is how other selections such as `name` and `type` work. Defining the Selection automatically adds it to the selection language."
  44. ]
  45. },
  46. {
  47. "cell_type": "code",
  48. "execution_count": 3,
  49. "metadata": {
  50. "collapsed": true
  51. },
  52. "outputs": [],
  53. "source": [
  54. "class Layer(AtomAttr):\n",
  55. " attrname = 'layers' # how it is called in AtomGroup\n",
  56. " singular = 'layer' # how it is called in Atom\n",
  57. " \n",
  58. "class LayerSelection(StringSelection):\n",
  59. " token = 'layer' # how to refer to it in a selection string\n",
  60. " field = 'layers' # where to look in AtomGroup"
  61. ]
  62. },
  63. {
  64. "cell_type": "markdown",
  65. "metadata": {},
  66. "source": [
  67. "Arbitrarily split the system into 'lower' and 'upper', obviously this could be done better...."
  68. ]
  69. },
  70. {
  71. "cell_type": "code",
  72. "execution_count": 4,
  73. "metadata": {
  74. "collapsed": false
  75. },
  76. "outputs": [],
  77. "source": [
  78. "a = 1877 // 2\n",
  79. "b = 1877 - a\n",
  80. "# Attributes must be able to handle numpy fancy indexing (ie a numpy array)\n",
  81. "layers = Layer(np.array(['lower'] * a + ['upper'] * b, dtype=object))"
  82. ]
  83. },
  84. {
  85. "cell_type": "markdown",
  86. "metadata": {},
  87. "source": [
  88. "Add the attribute to the Universe. This populates the namespace of all Atom, AtomGroup etc classes with the `layer` related data."
  89. ]
  90. },
  91. {
  92. "cell_type": "code",
  93. "execution_count": 5,
  94. "metadata": {
  95. "collapsed": true
  96. },
  97. "outputs": [],
  98. "source": [
  99. "u.add_TopologyAttr(layers)"
  100. ]
  101. },
  102. {
  103. "cell_type": "markdown",
  104. "metadata": {},
  105. "source": [
  106. "Create an arbitrary AtomGroup and access the new property or select by it!"
  107. ]
  108. },
  109. {
  110. "cell_type": "code",
  111. "execution_count": 6,
  112. "metadata": {
  113. "collapsed": false
  114. },
  115. "outputs": [
  116. {
  117. "name": "stdout",
  118. "output_type": "stream",
  119. "text": [
  120. "['lower' 'lower' 'lower' 'lower' 'lower']\n",
  121. "<AtomGroup with 9 atoms>\n"
  122. ]
  123. }
  124. ],
  125. "source": [
  126. "ag = u.atoms[::10]\n",
  127. "print ag.layers[:5]\n",
  128. "print ag.select_atoms('name N and layer upper')"
  129. ]
  130. },
  131. {
  132. "cell_type": "code",
  133. "execution_count": null,
  134. "metadata": {
  135. "collapsed": true
  136. },
  137. "outputs": [],
  138. "source": []
  139. }
  140. ],
  141. "metadata": {
  142. "kernelspec": {
  143. "display_name": "Python 2",
  144. "language": "python",
  145. "name": "python2"
  146. },
  147. "language_info": {
  148. "codemirror_mode": {
  149. "name": "ipython",
  150. "version": 2
  151. },
  152. "file_extension": ".py",
  153. "mimetype": "text/x-python",
  154. "name": "python",
  155. "nbconvert_exporter": "python",
  156. "pygments_lexer": "ipython2",
  157. "version": "2.7.6"
  158. }
  159. },
  160. "nbformat": 4,
  161. "nbformat_minor": 0
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement