Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
tsoc
openmm
Commits
889baef6
Unverified
Commit
889baef6
authored
Aug 02, 2023
by
Peter Eastman
Committed by
GitHub
Aug 02, 2023
Browse files
writeFile() accepts a filename instead of a file object (#4162)
parent
c07c5bd7
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
48 deletions
+56
-48
wrappers/python/openmm/app/pdbfile.py
wrappers/python/openmm/app/pdbfile.py
+10
-6
wrappers/python/openmm/app/pdbxfile.py
wrappers/python/openmm/app/pdbxfile.py
+9
-5
wrappers/python/tests/TestPdbFile.py
wrappers/python/tests/TestPdbFile.py
+28
-17
wrappers/python/tests/TestPdbxFile.py
wrappers/python/tests/TestPdbxFile.py
+9
-20
No files found.
wrappers/python/openmm/app/pdbfile.py
View file @
889baef6
...
@@ -6,7 +6,7 @@ Simbios, the NIH National Center for Physics-Based Simulation of
...
@@ -6,7 +6,7 @@ Simbios, the NIH National Center for Physics-Based Simulation of
Biological Structures at Stanford, funded under the NIH Roadmap for
Biological Structures at Stanford, funded under the NIH Roadmap for
Medical Research, grant U54 GM072970. See https://simtk.org.
Medical Research, grant U54 GM072970. See https://simtk.org.
Portions copyright (c) 2012-202
1
Stanford University and the Authors.
Portions copyright (c) 2012-202
3
Stanford University and the Authors.
Authors: Peter Eastman
Authors: Peter Eastman
Contributors:
Contributors:
...
@@ -277,8 +277,8 @@ class PDBFile(object):
...
@@ -277,8 +277,8 @@ class PDBFile(object):
The Topology defining the model to write
The Topology defining the model to write
positions : list
positions : list
The list of atomic positions to write
The list of atomic positions to write
file :
file=stdout
file :
string or file
A file to write to
the name of the file to write. Alternatively you can pass an open file object.
keepIds : bool=False
keepIds : bool=False
If True, keep the residue and chain IDs specified in the Topology
If True, keep the residue and chain IDs specified in the Topology
rather than generating new ones. Warning: It is up to the caller to
rather than generating new ones. Warning: It is up to the caller to
...
@@ -287,9 +287,13 @@ class PDBFile(object):
...
@@ -287,9 +287,13 @@ class PDBFile(object):
extraParticleIdentifier : string='EP'
extraParticleIdentifier : string='EP'
String to write in the element column of the ATOM records for atoms whose element is None (extra particles)
String to write in the element column of the ATOM records for atoms whose element is None (extra particles)
"""
"""
PDBFile
.
writeHeader
(
topology
,
file
)
if
isinstance
(
file
,
str
):
PDBFile
.
writeModel
(
topology
,
positions
,
file
,
keepIds
=
keepIds
,
extraParticleIdentifier
=
extraParticleIdentifier
)
with
open
(
file
,
'w'
)
as
output
:
PDBFile
.
writeFooter
(
topology
,
file
)
PDBFile
.
writeFile
(
topology
,
positions
,
output
,
keepIds
,
extraParticleIdentifier
)
else
:
PDBFile
.
writeHeader
(
topology
,
file
)
PDBFile
.
writeModel
(
topology
,
positions
,
file
,
keepIds
=
keepIds
,
extraParticleIdentifier
=
extraParticleIdentifier
)
PDBFile
.
writeFooter
(
topology
,
file
)
@
staticmethod
@
staticmethod
def
writeHeader
(
topology
,
file
=
sys
.
stdout
):
def
writeHeader
(
topology
,
file
=
sys
.
stdout
):
...
...
wrappers/python/openmm/app/pdbxfile.py
View file @
889baef6
...
@@ -6,7 +6,7 @@ Simbios, the NIH National Center for Physics-Based Simulation of
...
@@ -6,7 +6,7 @@ Simbios, the NIH National Center for Physics-Based Simulation of
Biological Structures at Stanford, funded under the NIH Roadmap for
Biological Structures at Stanford, funded under the NIH Roadmap for
Medical Research, grant U54 GM072970. See https://simtk.org.
Medical Research, grant U54 GM072970. See https://simtk.org.
Portions copyright (c) 2015-202
0
Stanford University and the Authors.
Portions copyright (c) 2015-202
3
Stanford University and the Authors.
Authors: Peter Eastman
Authors: Peter Eastman
Contributors: Jason Swails
Contributors: Jason Swails
...
@@ -266,8 +266,8 @@ class PDBxFile(object):
...
@@ -266,8 +266,8 @@ class PDBxFile(object):
The Topology defining the model to write
The Topology defining the model to write
positions : list
positions : list
The list of atomic positions to write
The list of atomic positions to write
file :
file=stdout
file :
string or file
A file to write to
the name of the file to write. Alternatively you can pass an open file object.
keepIds : bool=False
keepIds : bool=False
If True, keep the residue and chain IDs specified in the Topology
If True, keep the residue and chain IDs specified in the Topology
rather than generating new ones. Warning: It is up to the caller to
rather than generating new ones. Warning: It is up to the caller to
...
@@ -276,8 +276,12 @@ class PDBxFile(object):
...
@@ -276,8 +276,12 @@ class PDBxFile(object):
entry : str=None
entry : str=None
The entry ID to assign to the CIF file
The entry ID to assign to the CIF file
"""
"""
PDBxFile
.
writeHeader
(
topology
,
file
,
entry
,
keepIds
)
if
isinstance
(
file
,
str
):
PDBxFile
.
writeModel
(
topology
,
positions
,
file
,
keepIds
=
keepIds
)
with
open
(
file
,
'w'
)
as
output
:
PDBxFile
.
writeFile
(
topology
,
positions
,
output
,
keepIds
,
entry
)
else
:
PDBxFile
.
writeHeader
(
topology
,
file
,
entry
,
keepIds
)
PDBxFile
.
writeModel
(
topology
,
positions
,
file
,
keepIds
=
keepIds
)
@
staticmethod
@
staticmethod
def
writeHeader
(
topology
,
file
=
sys
.
stdout
,
entry
=
None
,
keepIds
=
False
):
def
writeHeader
(
topology
,
file
=
sys
.
stdout
,
entry
=
None
,
keepIds
=
False
):
...
...
wrappers/python/tests/TestPdbFile.py
View file @
889baef6
import
sys
import
tempfile
import
unittest
import
unittest
from
openmm.app
import
*
from
openmm.app
import
*
from
openmm
import
*
from
openmm
import
*
from
openmm.unit
import
*
from
openmm.unit
import
*
import
openmm.app.element
as
elem
import
openmm.app.element
as
elem
if
sys
.
version_info
>=
(
3
,
0
):
from
io
import
StringIO
from
io
import
StringIO
else
:
from
cStringIO
import
StringIO
class
TestPdbFile
(
unittest
.
TestCase
):
class
TestPdbFile
(
unittest
.
TestCase
):
...
@@ -47,23 +44,37 @@ class TestPdbFile(unittest.TestCase):
...
@@ -47,23 +44,37 @@ class TestPdbFile(unittest.TestCase):
def
test_WriteFile
(
self
):
def
test_WriteFile
(
self
):
"""Write a file, read it back, and make sure it matches the original."""
"""Write a file, read it back, and make sure it matches the original."""
def
compareFiles
(
pdb1
,
pdb2
):
self
.
assertEqual
(
len
(
pdb2
.
positions
),
8
)
for
(
p1
,
p2
)
in
zip
(
pdb1
.
positions
,
pdb2
.
positions
):
self
.
assertVecAlmostEqual
(
p1
,
p2
)
for
(
v1
,
v2
)
in
zip
(
pdb1
.
topology
.
getPeriodicBoxVectors
(),
pdb2
.
topology
.
getPeriodicBoxVectors
()):
self
.
assertVecAlmostEqual
(
v1
,
v2
,
1e-4
)
self
.
assertVecAlmostEqual
(
pdb1
.
topology
.
getUnitCellDimensions
(),
pdb2
.
topology
.
getUnitCellDimensions
(),
1e-4
)
for
atom1
,
atom2
in
zip
(
pdb1
.
topology
.
atoms
(),
pdb2
.
topology
.
atoms
()):
self
.
assertEqual
(
atom1
.
element
,
atom2
.
element
)
self
.
assertEqual
(
atom1
.
name
,
atom2
.
name
)
self
.
assertEqual
(
atom1
.
residue
.
name
,
atom2
.
residue
.
name
)
pdb1
=
PDBFile
(
'systems/triclinic.pdb'
)
pdb1
=
PDBFile
(
'systems/triclinic.pdb'
)
# First try writing to an open file object.
output
=
StringIO
()
output
=
StringIO
()
PDBFile
.
writeFile
(
pdb1
.
topology
,
pdb1
.
positions
,
output
)
PDBFile
.
writeFile
(
pdb1
.
topology
,
pdb1
.
positions
,
output
)
input
=
StringIO
(
output
.
getvalue
())
input
=
StringIO
(
output
.
getvalue
())
pdb2
=
PDBFile
(
input
)
pdb2
=
PDBFile
(
input
)
output
.
close
();
output
.
close
()
input
.
close
();
input
.
close
()
self
.
assertEqual
(
len
(
pdb2
.
positions
),
8
)
compareFiles
(
pdb1
,
pdb2
)
for
(
p1
,
p2
)
in
zip
(
pdb1
.
positions
,
pdb2
.
positions
):
self
.
assertVecAlmostEqual
(
p1
,
p2
)
# Now try a filename.
for
(
v1
,
v2
)
in
zip
(
pdb1
.
topology
.
getPeriodicBoxVectors
(),
pdb2
.
topology
.
getPeriodicBoxVectors
()):
self
.
assertVecAlmostEqual
(
v1
,
v2
,
1e-4
)
with
tempfile
.
TemporaryDirectory
()
as
tempdir
:
self
.
assertVecAlmostEqual
(
pdb1
.
topology
.
getUnitCellDimensions
(),
pdb2
.
topology
.
getUnitCellDimensions
(),
1e-4
)
filename
=
os
.
path
.
join
(
tempdir
,
'temp.pdb'
)
for
atom1
,
atom2
in
zip
(
pdb1
.
topology
.
atoms
(),
pdb2
.
topology
.
atoms
()):
PDBFile
.
writeFile
(
pdb1
.
topology
,
pdb1
.
positions
,
filename
)
self
.
assertEqual
(
atom1
.
element
,
atom2
.
element
)
pdb2
=
PDBFile
(
filename
)
self
.
assertEqual
(
atom1
.
name
,
atom2
.
name
)
compareFiles
(
pdb1
,
pdb2
)
self
.
assertEqual
(
atom1
.
residue
.
name
,
atom2
.
residue
.
name
)
def
test_BinaryStream
(
self
):
def
test_BinaryStream
(
self
):
"""Test reading a stream that was opened in binary mode."""
"""Test reading a stream that was opened in binary mode."""
...
...
wrappers/python/tests/TestPdbxFile.py
View file @
889baef6
import
tempfile
import
unittest
import
unittest
from
openmm.app
import
*
from
openmm.app
import
*
from
openmm
import
*
from
openmm
import
*
from
openmm.unit
import
*
from
openmm.unit
import
*
import
openmm.app.element
as
elem
import
openmm.app.element
as
elem
import
os
import
os
if
sys
.
version_info
>=
(
3
,
0
):
from
io
import
StringIO
from
io
import
StringIO
else
:
from
cStringIO
import
StringIO
class
TestPdbxFile
(
unittest
.
TestCase
):
class
TestPdbxFile
(
unittest
.
TestCase
):
"""Test the PDBx/mmCIF file parser"""
"""Test the PDBx/mmCIF file parser"""
...
@@ -16,22 +14,13 @@ class TestPdbxFile(unittest.TestCase):
...
@@ -16,22 +14,13 @@ class TestPdbxFile(unittest.TestCase):
"""Test conversion from PDB to PDBx"""
"""Test conversion from PDB to PDBx"""
mol
=
PDBFile
(
'systems/ala_ala_ala.pdb'
)
mol
=
PDBFile
(
'systems/ala_ala_ala.pdb'
)
with
tempfile
.
TemporaryDirectory
()
as
tempdir
:
# Write to 'file'
filename
=
os
.
path
.
join
(
tempdir
,
'temp.pdbx'
)
output
=
StringIO
()
PDBxFile
.
writeFile
(
mol
.
topology
,
mol
.
positions
,
filename
,
keepIds
=
True
)
PDBxFile
.
writeFile
(
mol
.
topology
,
mol
.
positions
,
output
,
try
:
keepIds
=
True
)
pdbx
=
PDBxFile
(
filename
)
except
Exception
:
# Read from 'file'
self
.
fail
(
'Parser failed to read PDBx/mmCIF file'
)
input
=
StringIO
(
output
.
getvalue
())
try
:
pdbx
=
PDBxFile
(
input
)
except
Exception
:
self
.
fail
(
'Parser failed to read PDBx/mmCIF file'
)
# Close file handles
output
.
close
()
input
.
close
()
def
test_Triclinic
(
self
):
def
test_Triclinic
(
self
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment