def to_pint(model, simplify=False): anfile = new_output_file("an") assert save(model, anfile, "an") import_colomoto_tool("pypint") an = pypint.load(anfile) if simplify: an = an.simplify() return an
def to_ginsim(model): """ Convert a bioLQM model into an equivalent GINsim model using the :py:mod:`ginsim` Python module. Please note that no layout is set for the regulatory graph. """ import_colomoto_tool("ginsim") ginml_file = new_output_file("ginml") assert save(model, ginml_file, "ginml") return ginsim.load(ginml_file)
def to_booleannet(model, mode='async'): bnetfile = new_output_file("booleannet") assert save(model, bnetfile, "booleannet") with open(bnetfile, 'r') as f: rules = f.read() rules = rules.replace('#BOOLEAN RULES', '') boolean2 = import_colomoto_tool("boolean2") return boolean2.Model(rules, mode)
def to_biolqm(maboss_model): biolqm = import_colomoto_tool("biolqm") bnet_filename = new_output_file("bnet") with open(bnet_filename, "w") as bnet_file: for node, rule in maboss_model.get_logical_rules().items(): bnet_file.write("%s, %s\n" % (node, rule)) return biolqm.load(bnet_filename)
def to_pyboolnet(self): PyBoolNet = import_colomoto_tool("PyBoolNet") fd, bnetfile = tempfile.mkstemp(".bnet") try: with os.fdopen(fd, "w") as fp: fp.write(self.source()) return PyBoolNet.FileExchange.bnet2primes(bnetfile) finally: os.unlink(bnetfile)
def from_ginsim(celf, lrg, **kwargs): ginsim = import_colomoto_tool("ginsim") fd, filename = tempfile.mkstemp(".sif") os.close(fd) try: ginsim.service("reggraph").export(lrg, filename) pkn = celf.from_sif(filename, **kwargs) finally: os.unlink(filename) return pkn
def loadBNet(bnet_filename): assert bnet_filename.lower().endswith( ".bnet"), "wrong extension for BNet file" if "://" in bnet_filename: from colomoto_jupyter.io import ensure_localfile bnet_filename = ensure_localfile(bnet_filename) from colomoto_jupyter import import_colomoto_tool biolqm = import_colomoto_tool("biolqm") return biolqm.to_maboss(biolqm.load(bnet_filename))
def __init__(self, data=None, Symbol_class=boolean.Symbol, allowed_in_name=('.', '_', ':', '-'), **kwargs): super().__init__() self.ba = boolean.BooleanAlgebra(NOT_class=NOT, Symbol_class=Symbol_class, allowed_in_token=allowed_in_name) if data: if isinstance(data, str): if "\n" in data or not os.path.exists(data): self.import_data(data.splitlines()) else: with open(data) as fp: self.import_data(fp) elif isinstance(data, dict): for a, f in data.items(): self[a] = f else: imported = False def biolqm_import(biolqm, lqm): bnfile = new_output_file(self.biolqm_format) assert biolqm.save(lqm, bnfile) with open(bnfile) as fp: self.import_data(fp) return True if "biolqm" in sys.modules: biolqm = sys.modules["biolqm"] if biolqm.is_biolqm_object(data): imported = biolqm_import(biolqm, data) if not imported and "ginsim" in sys.modules: ginsim = sys.modules["ginsim"] if ginsim.is_ginsim_object(data): biolqm = import_colomoto_tool("biolqm") imported = biolqm_import(biolqm, ginsim.to_biolqm(data)) if not imported: self.import_data(data) for a, f in kwargs.items(): self[a] = f
def load(model, fixed=None, mcl="", msm="", quiet=False): """ Execute StableMotifs analysis on the given Boolean network model. :param model: either a ``biolqm`` or ``ginsim`` object, or filename/URL of Boolean network in BooleanNet format :keyword dict[str,int] fixed: fix the given nodes to their associated given values :keyword str mcl: Optional threshold in the maximum cycle length (mcl). One must specify both a mcl and msm. :keyword str msm: Optional threshold in the maximum stable motif size (msm). One must specify both a mcl and msm. :keyword bool quiet: If True, skip computation output :rtype: :py:class:`.results.StableMotifsResult` instance """ # prepare temporary working space wd = tempfile.mkdtemp(prefix="StableMotifs-") # cleanup working directory at exit def cleanup(): shutil.rmtree(wd) atexit.register(cleanup) def biolqm_import(biolqm, lqm): if fixed: pert = " ".join((f"{node}%{value}" for node, value in fixed.items())) lqm = biolqm.perturbation(lqm, pert) modelfile = os.path.join(wd, "model.txt") assert biolqm.save(lqm, modelfile, "booleannet"), "Error converting from bioLQM" return modelfile, False is_modelfile = True if isinstance(model, BooleanNetwork): lqm = model.to_biolqm() biolqm = import_colomoto_tool("biolqm") modelfile, is_modelfile = biolqm_import(biolqm, lqm) elif "biolqm" in sys.modules: biolqm = sys.modules["biolqm"] if biolqm.is_biolqm_object(model): modelfile, is_modelfile = biolqm_import(biolqm, model) if is_modelfile and "ginsim" in sys.modules: ginsim = sys.modules["ginsim"] if ginsim.is_ginsim_object(model): model = ginsim.to_biolqm(model) biolqm = import_colomoto_tool("biolqm") modelfile, is_modelfile = biolqm_import(biolqm, model) if is_modelfile: modelfile = ensure_localfile(model) if fixed: biolqm = import_colomoto_tool("biolqm") model = biolqm.load(modelfile, "booleannet") modelfile, is_modelfile = biolqm_import(biolqm, model) else: shutil.copy(modelfile, wd) modelbase = os.path.basename(modelfile) model_name = modelbase.split(".")[0] # invoke StableMotifs argv = ["StableMotifs", modelbase] if mcl and msm: argv += list(map(str, [mcl, msm])) proc = subprocess.Popen(argv, cwd=wd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) with proc.stdout: for line in iter(proc.stdout.readline, b''): if quiet: continue sys.stdout.write(line) assert proc.wait() == 0, "An error occured while running StableMotifs" # return interface to results return StableMotifsResult(wd, model_name, fixed)
def to_pyboolnet(model): bnetfile = new_output_file("bnet") assert save(model, bnetfile, "bnet") PyBoolNet = import_colomoto_tool("PyBoolNet") bn = PyBoolNet.FileExchange.bnet2primes(bnetfile) return bn
def to_maboss(model): maboss = import_colomoto_tool("maboss") maboss_file = new_output_file("bnd") assert save(model, maboss_file, "bnd") return maboss.load(maboss_file, "%s.cfg" % maboss_file)
def to_pint(self): pypint = import_colomoto_tool("pypint") from pypint.converters import import_minibn return import_minibn(self)
def to_biolqm(self): bnfile = new_output_file(ext=self.biolqm_format) with open(bnfile, "w") as f: f.write(self.source()) biolqm = import_colomoto_tool("biolqm") return biolqm.load(bnfile)