Skip to content

Commit

Permalink
Merge pull request #29 from rcsb/development-dwp
Browse files Browse the repository at this point in the history
V0.88.0 - Add copy method to PdbxContainers for copying entire categories
  • Loading branch information
epeisach authored Apr 29, 2024
2 parents f7c5f14 + 4db9671 commit 726a4a1
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
2 changes: 2 additions & 0 deletions HISTORY.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,5 @@
23-Feb-2024 V0.86.0 - BinaryCIF writer properly tracks integer types to ensure
proper types passed through encoders. Ensure compatibility with ciftools-java and Mol*.
6-Apr-2024 V0.87.0 - Correct internal data of PdbxContainers when rename operation is used
24-Apr-2024 V0.88.0 - Add copy method to PdbxContainers for copying entire categories;
Update rename method to fail if new category name already exists (i.e., don't allow overwrite)
2 changes: 1 addition & 1 deletion mmcif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
__author__ = "John Westbrook"
__email__ = "john.westbrook@rcsb.org"
__license__ = "Apache 2.0"
__version__ = "0.87.0"
__version__ = "0.88.0"

__apiUrl__ = "https://mmcif.wwpdb.org"
57 changes: 54 additions & 3 deletions mmcif/api/PdbxContainers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
# 4-Apr-2018 jdw adding internal __eq__ and __hash__ methods
# 6-Aug-2018 jdw add setters/getters for container properties
# 5-Feb-2019 jdw add merge method and logging
# 24-Apr-2024 dwp add copy method; update rename method to fail if new category name already exists
# (i.e., don't allow overwrite)
##
"""
Expand All @@ -46,6 +48,7 @@

import logging
import sys
import copy

__docformat__ = "google en"
__author__ = "John Westbrook"
Expand Down Expand Up @@ -85,7 +88,7 @@ def attributePart(name):
if i == -1:
return None
else:
return name[i + 1 :]
return name[i + 1:]
except Exception:
return None

Expand Down Expand Up @@ -197,15 +200,63 @@ def printIt(self, fh=sys.stdout, pType="brief"):
self.__objCatalog[nm].dumpIt(fh)

def rename(self, curName, newName):
"""Change the name of an object in place -"""
"""Change the name of an object in place
Will fail if newName already exists or curName doesn't exist.
Args:
curName (str): current category name
newName (str): new category name
Returns:
(bool): True for success or False otherwise
"""

try:
# first check if requested new category already exists
if newName in self.__objNameList:
logger.error("Category already exists, %r", newName)
return False
# also check if current category exists
if curName not in self.__objNameList:
logger.error("Category doesn't exist, %r", curName)
return False
i = self.__objNameList.index(curName)
self.__objNameList[i] = newName
self.__objCatalog[newName] = self.__objCatalog[curName]
self.__objCatalog[newName].setName(newName)
del self.__objCatalog[curName]
return True
except Exception:
except Exception as e:
logger.exception("Failing with %s", str(e))
return False

def copy(self, curName, newName):
"""Copy the object to a new category name.
Will fail if newName already exists or curName doesn't exist.
Args:
curName (str): current category name
newName (str): new category name
Returns:
(bool): True for success or False otherwise
"""
try:
# first check if requested new category already exists
if newName in self.__objNameList:
logger.error("Category already exists, %r", newName)
return False
# also check if current category exists
if curName not in self.__objNameList:
logger.error("Category doesn't exist, %r", curName)
return False
# now do the copy
obj = copy.deepcopy(self.__objCatalog[curName])
obj.setName(newName)
self.append(obj)
return True
except Exception as e:
logger.exception("Failing with %s", str(e))
return False

def remove(self, curName):
Expand Down
39 changes: 38 additions & 1 deletion mmcif/tests/testPdbxContainers.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,19 @@ def testRename(self):
self.assertIsNone(blk.getObj(newName))

# Now rename
# Test non existant
# Test non existent
logger.info("test non-existent")
self.assertFalse(blk.rename("noname", "othernoname"))

# Test overwrite
logger.info("test overwrite")
# First create a new category
self.assertTrue(blk.copy(curName, "othernoname"))
# Next test overwriting that name
self.assertFalse(blk.rename(curName, "othernoname"))
# Now remove that new category
self.assertTrue(blk.remove("othernoname"))

# Expect nothing changed
self.assertEqual(blk.getObjNameList(), [curName])
self.assertTrue(blk.exists(curName))
Expand All @@ -122,11 +132,38 @@ def testRename(self):
self.assertIsNone(blk.getObj(curName))
self.assertIsNotNone(blk.getObj(newName))

def testCopy(self):
"""Tests copy method
"""
blk = self.__createContainer()

curName = "pdbx_seqtool_mapping_ref"
newName = "newName"

# Test non-existent copy (should Fail)
self.assertFalse(blk.copy("noname", "othernoname"))

# Test actual copy
self.assertTrue(blk.copy(curName, newName))
self.assertTrue(blk.exists(curName))
self.assertTrue(blk.exists(newName))

# Test copy to already existent destination (should Fail)
self.assertFalse(blk.copy(curName, newName))

# Check that things look as expected
self.assertEqual(blk.getObjNameList(), [curName, newName])
self.assertTrue(blk.exists(curName))
self.assertTrue(blk.exists(newName))
self.assertIsNotNone(blk.getObj(curName))
self.assertIsNotNone(blk.getObj(newName))


def containerSuite():
suiteSelect = unittest.TestSuite()
suiteSelect.addTest(PdbxContainersTests("testGeneral"))
suiteSelect.addTest(PdbxContainersTests("testRename"))
suiteSelect.addTest(PdbxContainersTests("testCopy"))
return suiteSelect


Expand Down

0 comments on commit 726a4a1

Please sign in to comment.