Skip to content

Commit 6ea8a78

Browse files
committed
added setId() syntax checks and getGPRIds(), optimized __checkId__()
1 parent a01d984 commit 6ea8a78

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

src/CBModel.py

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
Author: Brett G. Olivier
2121
Contact email: [email protected]
22-
Last edit: $Author: bgoli $ ($Id: CBModel.py 644 2018-03-14 21:40:47Z bgoli $)
22+
Last edit: $Author: bgoli $ ($Id: CBModel.py 650 2018-06-28 16:14:26Z bgoli $)
2323
2424
"""
2525
## gets rid of "invalid variable name" info
@@ -261,22 +261,34 @@ def deleteAnnotation(self, key):
261261
assert key != self.annotation, '\nAnnotation key {} does not exist'.format(key)
262262
self.annotation.pop(key)
263263

264-
def __checkId__(self):
264+
def __checkId__(self, cid=None):
265265
"""
266-
Checks the validity of the object id, raises a Runtime warning if not.
266+
Checks the validity of the object id unless cid is provided, in which case it checks the provided id
267+
268+
- *cid* [default=None] an optional Id to test for validity, if None then the object id is used
267269
268270
"""
269-
cntr = 0
270-
for c in self.id:
271-
if cntr == 0 and c.isalpha() or c == '_':
272-
pass
273-
elif cntr > 0 and c.isalnum() or c == '_':
274-
pass
275-
else:
276-
print('\"{}\" is an invalid character in id: \"{}\"'.format(c, self.id))
277-
#raise RuntimeWarning('\n\"{}\" is an invalid character in id: \"{}\"'.format(c, self.id))
278-
return False
279-
cntr += 1
271+
if cid is None:
272+
cid = self.id
273+
274+
try:
275+
exec('{} = 0'.format(cid))
276+
except SyntaxError:
277+
#print('ERROR: Syntax error, \"{}\" is an invalid object identifier'.format(cid))
278+
return False
279+
280+
## this was the old way of doing it
281+
#cntr = 0
282+
#for c in cid:
283+
#if cntr == 0 and c.isalpha() or c == '_':
284+
#pass
285+
#elif cntr > 0 and c.isalnum() or c == '_':
286+
#pass
287+
#else:
288+
#print('\"{}\" is an invalid character in id: \"{}\"'.format(c, cid))
289+
##raise RuntimeWarning('\n\"{}\" is an invalid character in id: \"{}\"'.format(c, cid))
290+
#return False
291+
#cntr += 1
280292
return True
281293

282294
def setPid(self, fid):
@@ -299,6 +311,9 @@ def setId(self, fid):
299311
300312
"""
301313
fid = str(fid)
314+
if not self.__checkId__(fid):
315+
raise RuntimeError('ERROR: Id not set, \"{}\" is an invalid identifier.'.format(fid))
316+
302317
if self.__objref__ is not None:
303318
if fid not in self.__objref__().__global_id__:
304319
self.__objref__().__changeGlobalId__(self.id, fid, self)
@@ -1563,6 +1578,18 @@ def getGeneIds(self, substring=None):
15631578
else:
15641579
return [g.getId() for g in self.genes if substring in g.getId()]
15651580

1581+
def getGPRIds(self, substring=None):
1582+
"""
1583+
Returns a list of GPR Id's, applies a substring search if substring is defined
1584+
1585+
- *substring* search for this pattern anywhere in the id
1586+
1587+
"""
1588+
if substring is None:
1589+
return [g.getId() for g in self.gpr]
1590+
else:
1591+
return [g.getId() for g in self.gpr if substring in g.getId()]
1592+
15661593
def getGeneLabels(self, substring=None):
15671594
"""
15681595
Returns a list of gene labels (locus tags), applies a substring search if substring is defined
@@ -3224,6 +3251,11 @@ def setId(self, fid):
32243251
Reimplements @FBase.setId()
32253252
32263253
"""
3254+
3255+
fid = str(fid)
3256+
if not self.__checkId__(fid):
3257+
raise RuntimeError('ERROR: Id not set, \"{}\" is an invalid identifier.'.format(fid))
3258+
32273259
if self.__objref__ is not None:
32283260
if fid not in self.__objref__().__global_id__:
32293261
self.__objref__().__changeGlobalId__(self.id, fid, self)
@@ -3846,6 +3878,10 @@ def setId(self, fid):
38463878
38473879
"""
38483880

3881+
fid = str(fid)
3882+
if not self.__checkId__(fid):
3883+
raise RuntimeError('ERROR: Id not set, \"{}\" is an invalid identifier.'.format(fid))
3884+
38493885
oldId = self.getId()
38503886
if self.__objref__ is not None:
38513887
if fid not in self.__objref__().__global_id__:
@@ -4160,6 +4196,10 @@ def setId(self, fid, allow_rename=False):
41604196
41614197
"""
41624198

4199+
fid = str(fid)
4200+
if not self.__checkId__(fid):
4201+
raise RuntimeError('ERROR: Id not set, \"{}\" is an invalid identifier.'.format(fid))
4202+
41634203
NEWID = False
41644204
oldId = self.getId()
41654205
if fid == oldId:
@@ -4459,6 +4499,9 @@ def setId(self, fid):
44594499
44604500
"""
44614501
fid = str(fid)
4502+
if not self.__checkId__(fid):
4503+
raise RuntimeError('ERROR: Id not set, \"{}\" is an invalid identifier.'.format(fid))
4504+
44624505
if self.__objref__ is not None:
44634506
if fid not in self.__objref__().__global_id__:
44644507
mod = self.__objref__()

0 commit comments

Comments
 (0)