11
22import json
33import os
4+ import re
45from .Base_Helper import BaseHelper
56from .Biochem_Helper import BiochemHelper
67
@@ -373,6 +374,23 @@ def readComplexesFile(self, path, includeLinenum=True, noFormat=False):
373374 raise DuplicateComplexError ('Complex %s on line %d is a duplicate' % (role ['id' ], linenum ))
374375
375376 return
377+
378+ def _gen_reaction_info (self , reaction ):
379+ stoich = []
380+ for prod , part in enumerate (reaction .split (" <=> " )):
381+ for match in re .finditer ('\((\d+)\) (\w+)\[(\d)\]' , part ):
382+ coeff = match .group (1 ) if prod else "-" + match .group (1 )
383+ stoich .append (":" .join ([coeff , match .group (2 ), match .group (3 ), '0' , match .group (2 )]))
384+ stoich = ";" .join (stoich )
385+ return {
386+ 'name' : 'null' ,
387+ 'deltag' : 'null' ,
388+ 'deltagerr' : 'null' ,
389+ 'status' : 'OK' ,
390+ 'reversibility' : '=' ,
391+ 'is_obsolete' : 0 ,
392+ 'stoichiometry' : stoich
393+ }
376394
377395 def readReactionsFile (self , path , includeLinenum = True , noFormat = False ):
378396 ''' Read the contents of a reactions file.
@@ -398,8 +416,8 @@ def readReactionsFile(self, path, includeLinenum=True, noFormat=False):
398416 self .numUniversal = 0
399417
400418 # The following fields are required in a reactions file.
401- required = { 'id' , 'compartment' , 'direction' , 'gfdir' , 'type' , 'base_cost' ,
402- 'forward_cost' , 'reverse_cost' , 'complexes' }
419+ required = {'id' , 'compartment' , 'direction' , 'gfdir' , 'type' , 'base_cost' ,
420+ 'forward_cost' , 'reverse_cost' , 'complexes' }
403421
404422 # Read the reactions from the specified file.
405423 with open (path , 'r' ) as handle :
@@ -425,8 +443,11 @@ def readReactionsFile(self, path, includeLinenum=True, noFormat=False):
425443 reactionId = fields [fieldNames ['id' ]]
426444 try :
427445 masterReaction = self .masterReactions [reactionId ]
428- except :
429- raise ReactionNotFoundError ('Reaction %s not found in master biochemistry' % (reactionId ))
446+ except KeyError :
447+ if 'custom_reaction' in fieldNames and fields [fieldNames ['custom_reaction' ]]:
448+ masterReaction = self ._gen_reaction_info (fields [fieldNames ['custom_reaction' ]])
449+ else :
450+ raise ReactionNotFoundError ('Reaction %s not found in master biochemistry' % (reactionId ))
430451
431452 # Check the reaction status.
432453 #if 'OK' not in masterReaction['status']:
@@ -454,6 +475,7 @@ def readReactionsFile(self, path, includeLinenum=True, noFormat=False):
454475
455476 # Make sure all of the compartments are valid.
456477 compartmentIds = fields [fieldNames ['compartment' ]].split ('|' )
478+ compartmentIds .sort (key = lambda x : ["m" , "n" , "p" , "x" , "z" , "c" , "e" ].index (x ))
457479 idcomp = compartmentIds [0 ]
458480 for cindex in range (len (compartmentIds )):
459481 try :
@@ -491,6 +513,8 @@ def readReactionsFile(self, path, includeLinenum=True, noFormat=False):
491513 reaction ['deltaG' ] = 10000000
492514 if reaction ['deltaGErr' ] == 'null' :
493515 reaction ['deltaGErr' ] = 10000000
516+ if reaction ['name' ] == 'null' :
517+ reaction ['name' ] = reaction ['id' ]
494518 if not isinstance (reaction ['base_cost' ], (int , long , float , complex )):
495519 reaction ['base_cost' ] = float (reaction ['base_cost' ])
496520 if not isinstance (reaction ['maxforflux' ], (int , long , float , complex )):
0 commit comments