Example #1
0
    def generate_data(model, inputdir, outputdir, cluster_type="pp", pp_cpus=2, runs=1):
        """
        The first pipeline step: data generation.

        :param model: the model to process
        :param inputdir: the directory containing the model
        :param outputdir: the directory containing the output files
        :param cluster_type: pp for local Parallel Python, lsf for Load Sharing Facility, sge for Sun Grid Engine.
        :param pp_cpus: the number of CPU used by Parallel Python.
        :param runs: the number of model simulation
        :return: no output
        """

        if runs < 1:
            logger.error("variable " + str(runs) + " must be greater than 0. Please, check your configuration file.")
            return

        if not os.path.isfile(os.path.join(inputdir, model)):
            logger.error(os.path.join(inputdir, model) + " does not exist.")
            return

        copasi = get_copasi()
        if copasi is None:
            logger.error(
                "CopasiSE not found! Please check that CopasiSE is installed and in the PATH environmental variable.")
            return

        # folder preparation
        refresh_directory(outputdir, model[:-4])

        # execute runs simulations.
        logger.info("Simulating model " + model + " for " + str(runs) + " time(s)")
        # Replicate the copasi file and rename its report file
        groupid = "_" + get_rand_alphanum_str(20) + "_"
        group_model = model[:-4] + groupid

        for i in xrange(1, runs + 1):
            shutil.copyfile(os.path.join(inputdir, model), os.path.join(inputdir, group_model) + str(i) + ".cps")
            replace_string_in_file(os.path.join(inputdir, group_model) + str(i) + ".cps",
                                   model[:-4] + ".csv",
                                   group_model + str(i) + ".csv")

        # run copasi in parallel
        # To make things simple, the last 10 character of groupid are extracted and reversed.
        # This string will be likely different from groupid and is the string to replace with
        # the iteration number.
        str_to_replace = groupid[10::-1]
        command = copasi + " " + os.path.join(inputdir, group_model + str_to_replace + ".cps")
        parallel_computation(command, str_to_replace, cluster_type, runs, outputdir, pp_cpus)

        # move the report files
        report_files = [f for f in os.listdir(inputdir) if
                        re.match(group_model + '[0-9]+.*\.csv', f) or re.match(group_model + '[0-9]+.*\.txt', f)]
        for file in report_files:
            # Replace some string in the report file
            replace_str_copasi_sim_report(os.path.join(inputdir, file))
            # rename and move the output file
            shutil.move(os.path.join(inputdir, file), os.path.join(outputdir, file.replace(groupid, "_")[:-4] + ".csv"))

        # removed repeated copasi files
        repeated_copasi_files = [f for f in os.listdir(inputdir) if re.match(group_model + '[0-9]+.*\.cps', f)]
        for file in repeated_copasi_files:
            os.remove(os.path.join(inputdir, file))
Example #2
0
    def generate_data(model, inputdir, cluster_type, pp_cpus, nfits, outputdir, sim_data_dir, updated_models_dir):
        """
        The first pipeline step: data generation.

        :param model: the model to process
        :param inputdir: the directory containing the model
        :param cluster_type: pp for parallel python, lsf for load sharing facility, sge for sun grid engine
        :param pp_cpus: the number of cpu for parallel python
        :param nfits: the number of fits to perform
        :param outputdir: the directory to store the results
        :param sim_data_dir: the directory containing the simulation data sets
        :param updated_models_dir: the directory containing the Copasi models with updated parameters for
               each estimation
        :return: no output
        """

        if int(nfits) < 1:
            logger.error("variable " + nfits + " must be greater than 0. Please, check your configuration file.")
            return

        if not os.path.isfile(os.path.join(inputdir, model)):
            logger.error(os.path.join(inputdir, model) + " does not exist.")
            return

        # folder preparation
        refresh_directory(sim_data_dir, model[:-4])
        refresh_directory(updated_models_dir, model[:-4])

        copasi = get_copasi()
        if copasi is None:
            logger.error(
                "CopasiSE not found! Please check that CopasiSE is installed and in the PATH environmental variable."
            )
            return

        logger.info("Configure Copasi:")
        logger.info(
            "Replicate a Copasi file configured for parameter estimation and randomise the initial parameter values"
        )
        groupid = "_" + get_rand_alphanum_str(20) + "_"
        group_model = model[:-4] + groupid
        pre_param_estim = RandomiseParameters(inputdir, model)
        pre_param_estim.print_parameters_to_estimate()
        pre_param_estim.generate_instances_from_template(nfits, groupid)

        logger.info("\n")
        logger.info("Parallel parameter estimation:")
        # To make things simple, the last 10 character of groupid are extracted and reversed.
        # This string will be likely different from groupid and is the string to replace with
        # the iteration number.
        str_to_replace = groupid[10::-1]
        command = (
            copasi
            + " -s "
            + os.path.join(inputdir, group_model + str_to_replace + ".cps")
            + " "
            + os.path.join(inputdir, group_model + str_to_replace + ".cps")
        )
        parallel_computation(command, str_to_replace, cluster_type, nfits, outputdir, pp_cpus)

        # Move the report files to the outputdir
        report_files = [
            f
            for f in os.listdir(inputdir)
            if re.match(group_model + "[0-9]+.*\.csv", f) or re.match(group_model + "[0-9]+.*\.txt", f)
        ]
        for file in report_files:
            # copy report and remove the groupid
            shutil.move(os.path.join(inputdir, file), os.path.join(sim_data_dir, file.replace(groupid, "_")))

        # removed repeated copasi files
        repeated_copasi_files = [f for f in os.listdir(inputdir) if re.match(group_model + "[0-9]+.*\.cps", f)]
        for file in repeated_copasi_files:
            # os.remove(os.path.join(inputdir, file))
            shutil.move(os.path.join(inputdir, file), os.path.join(updated_models_dir, file.replace(groupid, "_")))