-
Notifications
You must be signed in to change notification settings - Fork 132
/
base_adaptor.py
138 lines (99 loc) · 2.95 KB
/
base_adaptor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import uuid
"""abstract base class.
"""
__all__ = ['Structure', 'Trajectory']
class Structure:
"""abstract base class
"""
def __init__(self):
self.ext = "pdb"
self.params = {}
self.id = str(uuid.uuid4())
def get_structure_string(self):
raise NotImplementedError()
class Trajectory:
"""abstract base class
"""
def __init__(self):
self.id = str(uuid.uuid4())
self.shown = True
def get_coordinates(self, index):
raise NotImplementedError()
@property
def n_frames(self):
raise NotImplementedError()
__doc__ = """
Extend NGLView classes
Structures
==========
```python
import nglview
struc = nglview.PdbIdStructure("3pqr") # load file from RCSB PDB
view = nglview.NGLWidget(struc) # create widget
view # display widget
```
Trajectories
============
To enable trajectory access pass a second `Trajectory` argument to the widget
constructor or supply a combined `Structure`/`Trajectory` object as the first
argument.
Seperate `Structure` and `Trajectory` objects using `FileStructure` and
`SimpletrajStructure` (requires the [`simpletraj`](https://github.com/arose/simpletraj)
package):
```python
import nglview
struc = nglview.FileStructure(nglview.datafiles.GRO)
traj = nglview.SimpletrajStructure(nglview.datafiles.XTC)
nglview.NGLWidget(struc, traj)
```
Combined `Structure`/`Trajectory` object utilizing `MDTrajTrajectory` which
wraps a trajectory loaded with [MDTraj](http://mdtraj.org/):
```python
import nglview
import mdtraj
traj = mdtraj.load(nglview.datafiles.XTC, top=nglview.datafiles.GRO)
strucTraj = nglview.MDTrajTrajectory(traj)
nglview.NGLWidget(strucTraj)
```
The displayed frame can be changed by setting the `frame` property of the
widget instance `w`:
```python
view.frame = 100 # set to frame no 100
```
Interface classes
=================
You can create your own adaptors simply by following the interfaces for `Structure` and `Trajectory`,
which can also be combined into a single class.
Structure
---------
```python
class MyStructure(nglview.Structure):
ext = "pdb" # or gro, cif, mol2, sdf
params = {} # loading options passed to NGL
def get_structure_string(self):
return "structure in the self.ext format"
```
Trajectory
----------
```python
class MyTrajectory(nglview.Trajectory):
def get_coordinates(self, index):
# return 2D numpy array, shape=(n_atoms, 3)
@property
def n_frames(self):
return 2 # return number of frames
```
Combined
--------
```python
class MyStructureTrajectory(nglview.Structure, nglview.Trajectory):
ext = "pdb" # or gro, cif, mol2, sdf
params = {} # loading options passed to NGL
def get_structure_string(self):
return "structure in the self.ext format"
def get_coordinates(self, index):
# return 2D numpy array, shape=(n_atoms, 3)
def n_frames(self):
# return total frames
```
"""