Package SloppyCell :: Package ReactionNetworks :: Module IO
[hide private]

Source Code for Module SloppyCell.ReactionNetworks.IO

  1  import os 
  2  import sets 
  3   
  4  try: 
  5      import SBMLInterface as SBML 
  6      to_SBML_file = SBML.toSBMLFile 
  7      from_SBML_file = SBML.fromSBMLFile 
  8  except ImportError: 
  9      print 'SBML import and export not available.' 
 10   
 11  import SloppyCell 
 12  from SloppyCell.KeyedList_mod import KeyedList 
 13  import SloppyCell.ExprManip as ExprManip 
 14  import Network_mod 
 15  import Trajectory_mod 
 16   
17 -def net_DOT_file(net, filename = None, size=(7.5,10)):
18 lines = [] 19 lines.append('digraph "%s" {' % net.id) 20 lines.append('\tsize="'+str(size)+'!"') 21 lines.append('\tratio=fill') 22 for id in net.species.keys(): 23 lines.append('\t"%s"[color=black]' % net.get_component_name(id)) 24 25 lines.append('') 26 for reac_id, rxn in net.reactions.items(): 27 rxn_name = net.get_component_name(reac_id) 28 lines.append('\t"%s"[shape=box][color=red]' % rxn_name) 29 for rid, stoich in rxn.stoichiometry.items(): 30 rname = net.get_component_name(rid) 31 if stoich > 0: 32 lines.append('\t\t"%s" -> "%s";' % (rxn_name, rname)) 33 elif stoich < 0: 34 lines.append('\t\t"%s" -> "%s";' % (rname, rxn_name)) 35 else: 36 lines.append('\t\t"%s" -> "%s"[arrowhead=dot];' % (rname, 37 rxn_name)) 38 39 lines.append('}') 40 41 if filename is None: 42 filename = '%s.dot' % net.id 43 f = file(filename, 'w') 44 f.write(os.linesep.join(lines)) 45 f.close()
46 47
48 -def eqns_TeX_file(net, filename=None, simpleTeX=False, landscape=False):
49 """ 50 Output a TeX file containing the network equations and other information. 51 52 net: Network to work with. 53 filename: Filename for resulting TeX file. If filename==None, the output 54 file will be <network id>.tex 55 simpleTeX: If True, some TeX that causes problems in WYSIWYG editors such as 56 LyX will be omitted. 57 """ 58 net.compile() 59 60 lines = [] 61 lines.append(r'\documentclass{article}') 62 lines.append(r'\usepackage{amsmath}') 63 lines.append(r'\usepackage{fullpage}') 64 lines.append(r'\usepackage{longtable}') 65 if landscape == True: 66 lines.append(r'\usepackage[a4paper,landscape]{geometry}') 67 lines.append(r'\begin{document}') 68 lines.append(_net_eqns_to_TeX(net, simpleTeX)) 69 lines.append(r'\end{document}') 70 71 if filename is None: 72 filename = '%s.tex' % net.id 73 f = file(filename, 'w') 74 f.write(os.linesep.join(lines)) 75 f.close()
76
77 -def _net_eqns_to_TeX(net, simpleTeX=False):
78 """ 79 Return a string that contains the longtable-bound TeX'd equations for the 80 network. Also includes events and current optimizable parameter values. 81 82 simpleTeX: If True, some TeX that causes problems in WYSIWYG editors such as 83 LyX will be omitted. 84 """ 85 # Build up our name_dict. We wrap variables in a mathrm 86 name_dict = dict([(id, r'\mathrm{%s}' % net.get_component_name(id, True)) 87 for id in net.variables.keys()] + 88 [(id, net.get_component_name(id, True)) 89 for id in net.functionDefinitions.keys()]) 90 # Species get wrapped in [ ] 91 species_dict = dict([(id, r'\left[\mathrm{%s}\right]' 92 % net.get_component_name(id, True)) 93 for id in net.species.keys()]) 94 name_dict.update(species_dict) 95 96 outputs = [] 97 if net.functionDefinitions: 98 func_KL = KeyedList() 99 for func_id, func in net.functionDefinitions.items(): 100 lhs = '%s(%s)' % (func_id, r','.join(func.variables)) 101 func_KL.set(lhs, func.math) 102 funcs_str = ExprManip.Py2TeX.dict2TeX(func_KL, name_dict, 103 split_terms=False, 104 simpleTeX=simpleTeX) 105 outputs.extend([r'\section*{Function Definitions}', funcs_str]) 106 107 if net.assignmentRules: 108 assigns_str = ExprManip.Py2TeX.dict2TeX(net.assignmentRules, name_dict, 109 split_terms=True, 110 simpleTeX=simpleTeX) 111 outputs.extend([r'\section*{Assignment Rules}', assigns_str]) 112 113 if net.diff_eq_rhs: 114 eqns_str = ExprManip.Py2TeX.dict2TeX(net.diff_eq_rhs, name_dict, 115 r'\frac{d\,%s}{dt}', 116 split_terms=True, 117 simpleTeX=simpleTeX) 118 outputs.extend([r'\section*{Differential Equations}', eqns_str]) 119 120 if net.algebraicRules: 121 eqns_str = ExprManip.Py2TeX.dict2TeX(net.algebraicRules, name_dict, 122 lhs_form='0', split_terms=True, 123 simpleTeX=simpleTeX) 124 outputs.extend([r'\section*{Algebraic Equations}', eqns_str]) 125 126 if net.events: 127 outputs.append(r'\section*{Events}') 128 129 for e in net.events: 130 e_name = net.get_component_name(e.id, TeX_form=True) 131 outputs.append(r'$%s$' % (e_name)) 132 trigger_str = ExprManip.Py2TeX.expr2TeX(e.trigger, name_dict) 133 outputs.append(r'Trigger: $%s$' % trigger_str) 134 135 for id, result in e.event_assignments.items(): 136 outputs.append(r'\begin{itemize}') 137 id_str = ExprManip.Py2TeX.expr2TeX(id, name_dict) 138 rule_str = ExprManip.Py2TeX.expr2TeX(str(result), name_dict) 139 outputs.append(r'\item $%s = %s$' % (id_str, rule_str)) 140 outputs.append(r'\end{itemize}') 141 142 143 144 145 146 if net.optimizableVars: 147 outputs.append(r'\section*{Optimizable Parameters}') 148 params = net.GetParameters() 149 outputs.append(r'\begin{longtable}{|r|l|}') 150 outputs.append(r'\hline') 151 for id, value in params.items(): 152 id_str = ExprManip.Py2TeX.expr2TeX(id, name_dict) 153 value_str = ExprManip.Py2TeX.expr2TeX(str(value), name_dict) 154 outputs.append(r'$%s$& $%s$\\' % (id_str, value_str)) 155 outputs.append(r'\hline') 156 outputs.append(r'\end{longtable}') 157 158 outputs_copy = [] 159 for line in outputs: 160 # fix and_func and or_func 161 line = line.replace('and_func','and\_func') 162 line = line.replace('or_func','or\_func') 163 outputs_copy.append(line) 164 165 return os.linesep.join(outputs_copy)
166 167
168 -def dynamic_function_from_file(obj, filename):
169 """ 170 Load a dynamic function from a file and attach it to the obj. (A Network 171 or Trajectory.) 172 173 The filename must be <function_name>.py 174 """ 175 f = file(filename, 'r') 176 function_body = f.read() 177 f.close() 178 179 basename = os.path.basename(filename) 180 func = os.path.splitext(basename)[0] 181 file_type = os.path.splitext(basename)[1][1:] 182 183 if isinstance(obj, Trajectory_mod.Trajectory): 184 setattr(obj, '%s_functionBody' % func, function_body) 185 # We try to get the attribute 'namespace' for the object. 186 Network_mod._exec_dynamic_func(obj, func, getattr(obj, 'namespace', {})) 187 elif isinstance(obj, Network_mod.Network): 188 if file_type == 'py': 189 obj._dynamic_funcs_python[func] = function_body 190 obj.exec_dynamic_functions(disable_c = True) 191 elif file_type == 'c': 192 obj.exec_dynamic_functions(curr_c_code = function_body)
193
194 -def output_dynamic_functions(obj, directory = SloppyCell._TEMP_DIR):
195 """ 196 Output .py files for this objects's dynamic functions into the given 197 directory. 198 """ 199 if isinstance(obj, Trajectory_mod.Trajectory): 200 for func in obj._dynamic_funcs: 201 body = getattr(obj, '%s_functionBody' % func, None) 202 if body is not None: 203 f = file(os.path.join(directory, '%s.py' % func), 'w') 204 f.write(getattr(obj, '%s_functionBody' % func)) 205 f.close() 206 elif isinstance(obj, Network_mod.Network): 207 for func, body in obj._dynamic_funcs_python.items(): 208 if body != None: 209 f = file(os.path.join(directory, '%s.py' % func), 'w') 210 f.write(body) 211 f.close() 212 c_code = obj.get_c_code() 213 f = file(os.path.join(directory, '%s.c' % obj.get_id()), 'w') 214 f.write(c_code) 215 f.close()
216