|
| 1 | +#! /usr/bin/env python |
| 2 | +""" |
| 3 | +StochPy - Stochastic modeling in Python (http://stochpy.sourceforge.net) |
| 4 | +
|
| 5 | +Copyright (C) 2010-2015 T.R Maarlveld, B.G. Olivier, F.J. Bruggeman all rights reserved. |
| 6 | +
|
| 7 | +Timo R. Maarleveld ([email protected]) |
| 8 | +Centrum Wiskunde & Informatica, Amsterdam, Netherlands |
| 9 | +VU University, Amsterdam, Netherlands |
| 10 | +
|
| 11 | +Permission to use, modify, and distribute this software is given under the |
| 12 | +terms of the StochPy (BSD style) license. |
| 13 | +
|
| 14 | +NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. |
| 15 | +""" |
| 16 | +from __future__ import division, print_function, absolute_import |
| 17 | + |
| 18 | +__doc__ = """ |
| 19 | + StochPy: Stochastic Modeling in Python |
| 20 | + ===================================== |
| 21 | + |
| 22 | + StochPy (Stochastic modeling in Python) is a flexible software tool for stochastic simulation in cell biology. It provides various stochastic |
| 23 | + simulation algorithms, SBML support, analyses of the probability distributions of molecule copy numbers and event waiting times, analyses of stochastic time |
| 24 | + series, and a range of additional statistical functions and plotting facilities for stochastic simulations. |
| 25 | + |
| 26 | + Options: |
| 27 | + -------- |
| 28 | + - Stochastic Simulations |
| 29 | + - Variety of stochastic simulation output analysis: |
| 30 | + --> Time Simulation |
| 31 | + --> Distribution |
| 32 | + --> Waiting times |
| 33 | + --> Propensities |
| 34 | + - Cell Division simulations |
| 35 | + - SBML and PSC MDL input format. |
| 36 | +
|
| 37 | + StochPy can be used in an interactive Python shell: |
| 38 | +
|
| 39 | + Usage |
| 40 | + ----- |
| 41 | + >>> import stochpy |
| 42 | + >>> utils = stochpy.Utils() |
| 43 | + >>> utils.doExample1() |
| 44 | + >>> utils.doExample2() |
| 45 | + >>> smod = stochpy.SSA() # stochastic simulation algorithm module |
| 46 | + >>> help(smod) |
| 47 | + >>> help(stochpy.SSA) # (some windows versions) |
| 48 | + >>> stochpy? |
| 49 | + >>> smod.DoStochSim() |
| 50 | + >>> smod.PlotSpeciesTimeSeries() |
| 51 | + >>> converter = stochpy.SBML2PSC() |
| 52 | + >>> converter?? |
| 53 | + >>> help(stochpy.SBML2PSC) |
| 54 | + """ |
| 55 | + |
| 56 | +from .core2.version import __version__ |
| 57 | + |
| 58 | +import os,shutil,sys |
| 59 | + |
| 60 | +try: |
| 61 | + import readline |
| 62 | + _IsReadline = True |
| 63 | +except: |
| 64 | + _IsReadline = False |
| 65 | + |
| 66 | +try: |
| 67 | + from numpy.distutils.core import setup, Extension |
| 68 | + _IsNumPy = True |
| 69 | +except Exception as ex: |
| 70 | + _IsNumPy = False |
| 71 | + print(ex) |
| 72 | + print("StochPy requires NumPy") |
| 73 | + print("See http://numpy.scipy.org/ for more information about NumPy") |
| 74 | + os.sys.exit(-1) |
| 75 | + |
| 76 | +try: |
| 77 | + import matplotlib |
| 78 | + _IsMPL = True |
| 79 | +except: |
| 80 | + _IsMPL = False |
| 81 | + print("Warning: The Matplotlib module is not available, so plotting is not possible") |
| 82 | + print("Info: See http://matplotlib.sourceforge.net/ for more information about Matplotlib.") |
| 83 | + |
| 84 | +_IsPlotting = False |
| 85 | +try: |
| 86 | + import matplotlib.pyplot as plt |
| 87 | + _IsPlotting = True |
| 88 | +except Exception as er: |
| 89 | + print(er) |
| 90 | + |
| 91 | +def InitiateModels(directory): |
| 92 | + """ |
| 93 | + Build several models written in PSC MDL and SBML |
| 94 | + |
| 95 | + Input: |
| 96 | + - *directory* (string) |
| 97 | + """ |
| 98 | + from .pscmodels import Burstmodel |
| 99 | + from .pscmodels import BirthDeath |
| 100 | + from .pscmodels import ImmigrationDeath |
| 101 | + from .pscmodels import DecayingDimerizing |
| 102 | + from .pscmodels import Autoreg |
| 103 | + from .pscmodels import CellDivision as celldivision |
| 104 | + from .pscmodels import GeneDuplication |
| 105 | + from .pscmodels import dsmts_001_01 |
| 106 | + from .pscmodels import dsmts_001_11 |
| 107 | + from .pscmodels import dsmts_001_19 |
| 108 | + from .pscmodels import dsmts_002_10 |
| 109 | + from .pscmodels import dsmts_003_03 |
| 110 | + from .pscmodels import dsmts_003_04 |
| 111 | + from .pscmodels import chain5 |
| 112 | + from .pscmodels import chain50 |
| 113 | + from .pscmodels import chain500 |
| 114 | + from .pscmodels import chain1500 |
| 115 | + from .pscmodels import Isomerization |
| 116 | + from .pscmodels import Polymerase |
| 117 | + from .pscmodels import TranscriptionIntermediate |
| 118 | + from .pscmodels import Schlogl |
| 119 | + from .pscmodels import SignalingTimeVaryingL |
| 120 | + from .pscmodels import Signaling3cCD |
| 121 | + |
| 122 | + models = {} |
| 123 | + models['Signaling3cCD.psc'] = Signaling3cCD.model |
| 124 | + models['SignalingTimeVaryingL.psc'] = SignalingTimeVaryingL.model |
| 125 | + models['Schlogl.psc'] = Schlogl.model |
| 126 | + models['Burstmodel.psc'] = Burstmodel.model |
| 127 | + models['ImmigrationDeath.psc'] = ImmigrationDeath.model |
| 128 | + models['BirthDeath.psc'] = BirthDeath.model |
| 129 | + models['DecayingDimerizing.psc'] = DecayingDimerizing.model |
| 130 | + models['Autoreg.psc'] = Autoreg.model |
| 131 | + models['Autoreg.xml'] = Autoreg.xml_model |
| 132 | + models['CellDivision.psc'] = celldivision.model |
| 133 | + models['GeneDuplication.psc'] = GeneDuplication.model |
| 134 | + models['Isomerization.psc'] = Isomerization.model |
| 135 | + models['Polymerase.psc'] = Polymerase.model |
| 136 | + models['TranscriptionIntermediate.psc'] = TranscriptionIntermediate.model |
| 137 | + models['dsmts-001-01.xml.psc'] = dsmts_001_01.model |
| 138 | + models['dsmts-001-01.xml'] = dsmts_001_01.xml_model |
| 139 | + models['dsmts-001-11.xml.psc'] = dsmts_001_11.model |
| 140 | + models['dsmts-001-11.xml'] = dsmts_001_11.xml_model |
| 141 | + models['dsmts-001-19.xml.psc'] = dsmts_001_19.model |
| 142 | + models['dsmts-001-19.xml'] = dsmts_001_19.xml_model |
| 143 | + models['dsmts-002-10.xml.psc'] = dsmts_002_10.model |
| 144 | + models['dsmts-002-10.xml'] = dsmts_002_10.xml_model |
| 145 | + models['dsmts-003-03.xml.psc'] = dsmts_003_03.model |
| 146 | + models['dsmts-003-03.xml'] = dsmts_003_03.xml_model |
| 147 | + models['dsmts-003-04.xml.psc'] = dsmts_003_04.model |
| 148 | + models['dsmts-003-04.xml'] = dsmts_003_04.xml_model |
| 149 | + models['chain5.psc'] = chain5.model |
| 150 | + models['chain50.psc'] = chain50.model |
| 151 | + models['chain500.psc'] = chain500.model |
| 152 | + models['chain1500.psc'] = chain1500.model |
| 153 | + |
| 154 | + model_names = list(models) |
| 155 | + dir_models = os.listdir(directory) |
| 156 | + for mod_name in model_names: |
| 157 | + if mod_name not in dir_models: |
| 158 | + print("Info: Model {0:s} copied to {1:s}".format(mod_name ,directory) ) |
| 159 | + file_out = open(os.path.join(directory,mod_name),'w') |
| 160 | + file_out.write(models[mod_name]) |
| 161 | + file_out.close() |
| 162 | + |
| 163 | + |
| 164 | +output_dir = None |
| 165 | +model_dir = None |
| 166 | +if os.sys.platform != 'win32': |
| 167 | + if not os.path.exists(os.path.join(os.path.expanduser('~'),'Stochpy')): |
| 168 | + os.makedirs(os.path.join(os.path.expanduser('~'),'Stochpy')) |
| 169 | + if not os.path.exists(os.path.join(os.path.expanduser('~'),'Stochpy', 'pscmodels')): |
| 170 | + os.makedirs(os.path.join(os.path.expanduser('~'),'Stochpy','pscmodels')) |
| 171 | + if not os.path.exists(os.path.join(os.path.expanduser('~'),'Stochpy', 'temp')): |
| 172 | + os.makedirs(os.path.join(os.path.expanduser('~'),'Stochpy','temp')) |
| 173 | + |
| 174 | + output_dir = os.path.join(os.path.expanduser('~'),'Stochpy') |
| 175 | + model_dir = os.path.join(os.path.expanduser('~'),'Stochpy','pscmodels') |
| 176 | + temp_dir = os.path.join(os.path.expanduser('~'),'Stochpy','temp') |
| 177 | + InitiateModels(model_dir) |
| 178 | +else: |
| 179 | + if not os.path.exists(os.path.join(os.getenv('HOMEDRIVE')+os.path.sep,'Stochpy')): |
| 180 | + os.makedirs(os.path.join(os.getenv('HOMEDRIVE')+os.path.sep,'Stochpy')) |
| 181 | + if not os.path.exists(os.path.join(os.getenv('HOMEDRIVE')+os.path.sep,'Stochpy','pscmodels')): |
| 182 | + os.makedirs(os.path.join(os.getenv('HOMEDRIVE')+os.path.sep,'Stochpy','pscmodels')) |
| 183 | + if not os.path.exists(os.path.join(os.getenv('HOMEDRIVE')+os.path.sep,'Stochpy','temp')): |
| 184 | + os.makedirs(os.path.join(os.getenv('HOMEDRIVE')+os.path.sep,'Stochpy','temp')) |
| 185 | + |
| 186 | + output_dir = os.path.join(os.getenv('HOMEDRIVE')+os.path.sep,'Stochpy',) |
| 187 | + model_dir = os.path.join(os.getenv('HOMEDRIVE')+os.path.sep,'Stochpy','pscmodels') |
| 188 | + temp_dir = os.path.join(os.getenv('HOMEDRIVE')+os.path.sep,'Stochpy','temp') |
| 189 | + InitiateModels(model_dir) |
| 190 | + |
| 191 | +from .modules.SBML2PSC import SBML2PSC |
| 192 | +from .modules.StochSim import SSA |
| 193 | +from .modules.StochPyUtils import Utils |
| 194 | +from .modules.StochPyCellDivision import CellDivision |
| 195 | +from .modules.StochPyDemo import Demo |
| 196 | +from .modules import Analysis as Analysis |
| 197 | + |
| 198 | +try: |
| 199 | + from .modules.NucleosomeTool import NucleosomeModelBuilder |
| 200 | + from .modules.NucleosomeTool import NucleosomeSimulator |
| 201 | +except Exception as er: |
| 202 | + pass # ignore |
| 203 | + |
| 204 | +def DeletePreviousOutput(path,type): |
| 205 | + """ |
| 206 | + Delete output of earlier simulations |
| 207 | + |
| 208 | + Input: |
| 209 | + - *path* (string) |
| 210 | + - *type* (string) |
| 211 | + """ |
| 212 | + for filename in os.listdir(path): |
| 213 | + if filename.endswith(type): |
| 214 | + filename_path = os.path.join(path,filename) |
| 215 | + os.remove(filename_path) |
| 216 | + |
| 217 | +def DeleteExistingData(path): |
| 218 | + """ |
| 219 | + Delete all existing StochKit simulation data |
| 220 | + |
| 221 | + Input: |
| 222 | + - *path* (string) |
| 223 | + """ |
| 224 | + if os.path.exists(path): |
| 225 | + for maps in os.listdir(path): |
| 226 | + dir2delete = os.path.join(path,maps) |
| 227 | + shutil.rmtree(dir2delete, ignore_errors=True) |
| 228 | + |
| 229 | +def SaveInteractiveSession(filename='interactiveSession.py',path=output_dir): |
| 230 | + """ |
| 231 | + Save the interactive session |
| 232 | + |
| 233 | + Input: |
| 234 | + - *filename*: [default = interactiveSession.py'] (string) |
| 235 | + - *path*: (string) |
| 236 | + """ |
| 237 | + if not _IsReadline: |
| 238 | + print("Error: install 'readline' first") |
| 239 | + elif _IsReadline: |
| 240 | + historyPath = os.path.join(path,filename) |
| 241 | + if not os.path.exists(path): |
| 242 | + os.makedirs(directory) |
| 243 | + |
| 244 | + readline.write_history_file(historyPath) |
| 245 | + file_in = open(historyPath,'r') |
| 246 | + history_list = file_in.readlines() |
| 247 | + n_import_statement = 0 |
| 248 | + for command in history_list: |
| 249 | + if 'import' in command and 'stochpy' in command: |
| 250 | + n_import_statement +=1 |
| 251 | + |
| 252 | + n=0 |
| 253 | + file_out = open(historyPath,'w') |
| 254 | + for command in history_list: |
| 255 | + if 'import' in command and 'stochpy' in command: |
| 256 | + n+=1 |
| 257 | + if n==n_import_statement: |
| 258 | + file_out.write(command) |
| 259 | + file_out.close() |
| 260 | + print("Info: Interactive session successfully saved at {0:s}".format(historyPath) ) |
| 261 | + print("Info: use 'ipython {0:s} to restart modeling with this interactive session".format(filename) ) |
| 262 | + |
| 263 | + |
| 264 | +DeletePreviousOutput(temp_dir,'.dat') |
| 265 | +DeletePreviousOutput(temp_dir,'.xml') |
| 266 | +DeletePreviousOutput(temp_dir,'.txt') |
| 267 | +DeletePreviousOutput(temp_dir,'temp_parse_module') |
| 268 | +DeleteExistingData(temp_dir) |
| 269 | +#readline.clear_history() |
| 270 | + |
| 271 | +print(""" |
| 272 | +####################################################################### |
| 273 | +# # |
| 274 | +# Welcome to the interactive StochPy environment # |
| 275 | +# # |
| 276 | +####################################################################### |
| 277 | +# StochPy: Stochastic modeling in Python # |
| 278 | +# http://stochpy.sourceforge.net # |
| 279 | +# Copyright(C) T.R Maarleveld, B.G. Olivier, F.J Bruggeman 2010-2015 # |
| 280 | +# DOI: 10.1371/journal.pone.0079345 # |
| 281 | + |
| 282 | +# VU University, Amsterdam, Netherlands # |
| 283 | +# Centrum Wiskunde Informatica, Amsterdam, Netherlands # |
| 284 | +# StochPy is distributed under the BSD licence. # |
| 285 | +####################################################################### |
| 286 | +""") |
| 287 | +print("Version {0:s}".format(__version__) ) |
| 288 | +print("Output Directory: {0:s}".format(output_dir) ) |
| 289 | +print("Model Directory: {0:s}".format(model_dir) ) |
| 290 | +#print("Warning: Figure freezing? Try a different matplotlib backend (stochpy.plt.switch_backend) and/or set IsInteractive to False (see user guide)") |
0 commit comments