-
Notifications
You must be signed in to change notification settings - Fork 652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add .write
to trajectory reader
#1037
Comments
The headache I can see with this is for trajectory writers that require more than the Reader contains, (eg |
Well we would also write a more generic write/copy function in the MDAnalysis namespace that wraps def write(ag, fname):
with mda.Writer(fname, n_atoms=ag.n_atoms) as w:
for ts in ag.universe.trajectory:
w.write(ag)
# call it with
mda.write(mda.Universe(PSF, DCD), 'test.pdb)) That is easier to implement and would allow to write sub selections. |
To be more specific and avoid confusions we could also name the generic function |
I still like the concise semantics of |
The writers will already complain them self if a TimeStep object isn't enough with an exception. So that would just work. |
I think |
The way that people will commonly access this method as is (And in any case, we are already overloading Oliver Beckstein
|
I like |
Optional
|
I updated the original comment with the specs. Hopefully that makes it easier to implement. Differences to what @kain88-de originally proposed
|
I am not sure if the following is a good idea but I'll throw it out nevertheless: u.trajectory[start:stop:step].write("new.dcd") It would mean overloading the Generator object. Probably not a very pythonic thing to do. |
Conceptually, the simplicity and readability of that slice construct seem quite pythonic to me. I really like it! |
This is a proof of concept that replaces the iterators returned by the proto reader __getitem__ by an iterable class. That class has a __len__ method to adress #1894 and can have a write method to adress #1037. In the current implementation, each type of input for the proto reader's __getitem__ returns a different class. This allow to test for the correct behavior only once. Ideally, the iterables should be imutable to avoid inconsistencies.
This is a proof of concept that replaces the iterators returned by the proto reader __getitem__ by an iterable class. That class has a __len__ method to adress #1894 and can have a write method to adress #1037. In the current implementation, each type of input for the proto reader's __getitem__ returns a different class. This allow to test for the correct behavior only once. Ideally, the iterables should be imutable to avoid inconsistencies.
See #1037 Not having access to the topology, the method cannot write PDB files or similar formats.
Like the
ag.write
it would be convenient to have atrajectory.write
method. This method would always write the complete trajectory with all atoms. To write only a subset the more generalWriter
still has to be used. This makes conversion of trajectory formats or concatenating trajectories very easy.It is also a good fit to write a MemoryReader back to disk.
Proposed API
All trajectory readers get a new method
Reader.write()
that allows one to write the trajectory to disk (possibly in a different format):The semantics should evoke the idea that a trajectory is considered the object to be written.
Optional slicing kwargs
kwargs to determine which frames to write
start
stop
step
To be used as
Optional
atoms
kwargWhat to do when a writer needs more information than available to the Reader #1037 (comment) ? Add an optional kwarg
atoms
toReader.write(filename, atoms=<AtomGroup-like>)
that can be used to extract all the necessary additional information.The behavior should be:
atoms
is provided, only write those atoms as a subselection. This will allow for a very concise way to strip trajectories of water:u.trajectory.write("protein_only.xtc", atoms=u.select_atoms("protein"))
.atoms
provided: raise aValueError
and tell the user to supply the atomgroup orUniverse
inatoms
(e.g.,u.trajectory.write("new.xtc", atoms=u)
)atoms
is provided: write the trajectory for the given atoms.Using the
atoms
argument to write subselections is in line with @kain88-de 's idea #1037 (comment) .Progress meter
And it would be nice if we could also have an optional progress meter #928 (
quiet=False
, defaultquiet=True
... orverbose=False
once we solve #903).Basic implementation
Implementation could look similar to the following (without all the detailed error checking and checking for
atoms
and no ProgressMeter):History
.write
to trajectory reader #1037 (comment) and added slicing kwargsThe text was updated successfully, but these errors were encountered: