-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Allow PDBIO to write to an open IO object without closing it #4200
Conversation
Bio/PDB/PDBIO.py
Outdated
if write_end: | ||
fhandle.write("END \n") | ||
|
||
if should_close: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be able to use isinstance(file, str)
here instead of a flag variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
Tip: append ?w=1
on the diff view in GitHub to hide the indentation change which makes this look more dramatic than it really is.
Looks good to me as well. I'll merge it. Thanks Joe! @peterjc Nice trick with the |
To help remember this, it's a |
There is also a checkbox to hide white space under the settings dropdown in the diff viewer. |
I see, via the cog icon - I wonder how long that's been there? Thanks! |
Could you check if solution could be like PDBParser [PDBParser.py] handles it: from Bio.File import as_handle ##github.com/biopython/biopython/blob/… and in PDBIO.py save method #[https://github.com/biopython/biopython/blob/7ef56557bdca66eabd62334e61c4dc3153c997ff/Bio/PDB/PDBIO.py#L299] change with fhandle to with as_handle(fhandle) , was referring to old PDBIO 1.80 that was using with fhandle or betterr remove "if isinstance else ..." and change "with fhandle:" to "with as_handle(file, mode = 'w') as fhandle:" |
I hereby agree to dual licence this and any previous contributions under both
the Biopython License Agreement AND the BSD 3-Clause License.
I have read the
CONTRIBUTING.rst
file, have runpre-commit
locally, and understand that continuous integration checks will be used to
confirm the Biopython unit tests and style checks pass with these changes.
I have added my name to the alphabetical contributors listings in the files
NEWS.rst
andCONTRIB.rst
as part of this pull request, am listedalready, or do not wish to be listed. (This acknowledgement is optional.)
Recent changes to
PDBIO
in be77131 switched to using awith fhandle
block for the open file handle. This is fine in the case of the file being opened byPDBIO
, but another useful situation is having an open file handle or string object that you can read from or write to again.This PR only closes the file handle if
PDBIO
opened it, allowing for example:whereas on Biopython 1.79 the last line errors as
io_str
has been closed bysave
:Most of the code change is white space, it just adds a flag that closes the handle depending on whether
open
was called.This initially came to my attention due to a downstream error in NGLView (https://bioinformatics.stackexchange.com/questions/20151/file-i-o-error-using-nglview-show-biopythonstructure) which uses this behaviour to access the string of a written PDB file without writing a file (https://github.com/nglviewer/nglview/blob/master/nglview/adaptor.py#L193-L200).