Esempio n. 1
0
def get_SCprofiles_from_dir(output_dir,
                            compute_topoisomerase=False,
                            timepoints=None):
    """
    Provides a list with successive tuples of Barr_fix,SC_profile that can be used to draw the distribution. 
    - if compute_topoisomerase, then also lists for those: then the argument must be the input file: params.ini
    - timepoints is an array of indexes, from 0 to the maximal timeindex
    """
    sigma_info = np.load(output_dir + "/all_res/save_sigma_info.npz")
    RNAPs_info = np.load(output_dir + "/all_res/save_RNAPs_info.npz")
    Barr_pos = sigma_info["save_Barr_pos"]
    dom_sigma_info = sigma_info["dom_sigma_info"]
    #print(dom_sigma_info[96:110])
    # select timepoints
    if timepoints is None:
        timepoints = np.arange(len(dom_sigma_info))
        sigma = dom_sigma_info
        barr = Barr_pos
        RNAPs_pos_info = RNAPs_info["RNAPs_info"][:, 1, :]
    else:
        sigma = dom_sigma_info[timepoints]
        barr = Barr_pos[timepoints]
        RNAPs_pos_info = RNAPs_info["RNAPs_info"][:, 1, timepoints]
    # compute topoisomerases?
    if not compute_topoisomerase:
        return [(barr[i], s) for i, s in enumerate(sigma)]
    else:
        inf = compute_topoisomerase
        config = sim.read_config_file(inf)
        # get promoter values from the config file
        m = config.getfloat('PROMOTER', 'm')
        sigma_t = config.getfloat('PROMOTER', 'sigma_t')
        epsilon = config.getfloat('PROMOTER', 'epsilon')
        # get topoisomerase concentrations
        GYRASE_CONC = config.getfloat('SIMULATION', 'GYRASE_CONC')
        TOPO_CONC = config.getfloat('SIMULATION', 'TOPO_CONC')
        # topoisomerase behavior
        TOPO_CTE = config.getfloat('TOPOISOMERASES', 'TOPO_CTE')
        GYRASE_CTE = config.getfloat('TOPOISOMERASES', 'GYRASE_CTE')
        k_GYRASE = config.getfloat('TOPOISOMERASES', 'k_GYRASE')
        x0_GYRASE = config.getfloat('TOPOISOMERASES', 'x0_GYRASE')
        k_TOPO = config.getfloat('TOPOISOMERASES', 'k_TOPO')
        x0_TOPO = config.getfloat('TOPOISOMERASES', 'x0_TOPO')
        # compute topo activity in
        gyr_act = [
            GYRASE_CONC * 1 / (1 + np.exp(-k_GYRASE * (s - x0_GYRASE))) *
            GYRASE_CTE for s in sigma
        ]
        topo_act = [
            TOPO_CONC * 1 / (1 + np.exp(k_TOPO * (s - x0_TOPO))) * TOPO_CTE
            for s in sigma
        ]
        return [(barr[i], s) for i, s in enumerate(sigma)], gyr_act, topo_act
Esempio n. 2
0
def copy_to_working_path(path_params_seq, working_path):
    #modify the config file from path_params_seq and copy it in working_path.
    config = simulation.read_config_file(path_params_seq + "params_seq.ini")
    config.set('INPUTS', 'TSS', working_path + "TSS.dat")
    config.set('INPUTS', 'TTS', working_path + "TTS.dat")
    config.set('INPUTS', 'GFF', working_path + "gff.gff")
    config.set('INPUTS', 'BARR_FIX', working_path + "prot.dat")
    #write it in working_path
    with open(working_path + "params_seq.ini", 'w') as configfile:
        config.write(configfile)
    #copy the files.
    oss("cp " + path_params_seq + "TSS.dat " + working_path + "TSS.dat")
    oss("cp " + path_params_seq + "TTS.dat " + working_path + "TTS.dat")
    oss("cp " + path_params_seq + "prot.dat " + working_path + "prot.dat")
    oss("cp " + path_params_seq + "gff.gff " + working_path + "gff.gff")
Esempio n. 3
0
def plot_promoter_response_and_SCvalues(INI_file, outfile=None):
    """
    For given simulation, plot the promoter response curve together with initiatil and equilibrium SC values
    """
    if outfile is None:
        outfile = INI_file.split(".")[0] + "_promoter"
    config = sim.read_config_file(INI_file)
    # get promoter values from the config file
    m = config.getfloat('PROMOTER', 'm')
    sigma_t = config.getfloat('PROMOTER', 'sigma_t')
    epsilon = config.getfloat('PROMOTER', 'epsilon')
    # get topoisomerase constants
    GYRASE_CONC = config.getfloat('SIMULATION', 'GYRASE_CONC')
    TOPO_CONC = config.getfloat('SIMULATION', 'TOPO_CONC')
    TOPO_CTE = config.getfloat('TOPOISOMERASES', 'TOPO_CTE')
    GYRASE_CTE = config.getfloat('TOPOISOMERASES', 'GYRASE_CTE')
    # topoisomerase behavior
    k_GYRASE = config.getfloat('TOPOISOMERASES', 'k_GYRASE')
    x0_GYRASE = config.getfloat('TOPOISOMERASES', 'x0_GYRASE')
    k_TOPO = config.getfloat('TOPOISOMERASES', 'k_TOPO')
    x0_TOPO = config.getfloat('TOPOISOMERASES', 'x0_TOPO')
    # sigma0,sigma_eq
    sigma_eq = SC_numerical_solution(GYRASE_CONC, TOPO_CONC, GYRASE_CTE,
                                     TOPO_CTE, k_GYRASE, k_TOPO, x0_GYRASE,
                                     x0_TOPO)
    try:
        SIGMA_0 = config.getfloat('SIMULATION', 'SIGMA_0')
    except:
        SIGMA_0 = sigma_eq
    # -------------------------
    prom = lambda sig: np.exp((1 / (1 + np.exp(
        (sig - sigma_t) / epsilon))) * m)
    #
    fig = plt.figure(figsize=(4, 3))  # 3.2,2.7
    sigs = np.arange(-.12, .04, .005)
    plt.plot(sigs, prom(sigs), color="black")
    plt.axvline(SIGMA_0, color="gray", ls="--", lw=.5, label="initial")
    plt.axvline(sigma_eq, color="gray", lw=.5, label="equil")
    plt.xlabel("σ")
    plt.ylabel("supercoiling activation factor")
    plt.legend()
    plt.tight_layout()
    for ext in exts:
        plt.savefig(outfile + ext)
    plt.close()
Esempio n. 4
0
def get_cov_bp(INI_file):
    """
    Analyzes initiation file to get the array of positions in reduced units, for plotting
    """
    path = INI_file.rpartition("/")[0]
    if path == "":
        path = "."
    path += "/"
    # read the config file
    config = sim.read_config_file(INI_file)
    # get inputs infos from the config file
    GFF_file = path + config.get('INPUTS', 'GFF')
    DELTA_X = config.getfloat('GLOBAL', 'DELTA_X')
    # To draw the beautiful genes we need to read the GFF, TSS and TTS files to get some info ;)
    gff_df_raw = sim.load_gff(GFF_file)
    # to get the cov_bp (a verifier)
    genome_size = sim.get_genome_size(gff_df_raw)
    genome = int(genome_size / DELTA_X)
    #print(genome)
    cov_bp = np.arange(0, genome_size, DELTA_X)
    #print(cov_bp,len(cov_bp))
    #cov_bp = np.resize(cov_bp, genome)
    return cov_bp
Esempio n. 5
0
def evol_master(arg):
    scipy.random.seed()
    fitness_file, p_inversion = arg
    # read the evolution parameters from the config file
    config = sim.read_config_file(INI_file)
    environment_file = config.get('EVOLUTION', 'environment')
    temperature = config.getfloat('EVOLUTION', 'temperature')
    temperature_rate = config.getfloat('EVOLUTION', 'temperature_rate')
    nbiter = config.getint('EVOLUTION', 'nbiter')
    #p_inversion = config.getfloat('EVOLUTION', 'p_inversion')
    indel_size = config.getint('EVOLUTION', 'indel_size')
    #fitness_file = config.get('EVOLUTION', 'output_file')

    pth = INI_file[:-10]
    expected_profile = pd.read_table(pth + environment_file,
                                     sep='\t',
                                     header=None)[1]

    tss, tts, prot, genome_size = sim.load_genome(INI_file)
    gene_expression = sim.start_transcribing(INI_file, output_dir, tss, tts,
                                             prot, genome_size)
    fitness = get_fitness(gene_expression, expected_profile)

    with open(fitness_file, 'w') as f:
        f.write("fitness\tgenome size\tmutation type\n")

    for it in range(nbiter):
        temperature *= temperature_rate
        tss, tts, prot, genome_size, fitness, mutation_type = evol_main_loop(
            tss, tts, prot, genome_size, fitness, p_inversion, temperature,
            indel_size, expected_profile)
        print("iteration : " + str(it) + "\tfitness : " + str(fitness))
        # mutation_type is 0 if no changes occured 1 for inversion and 2 for indel
        with open(fitness_file, 'a') as f:
            f.write(
                str(fitness) + "\t" + str(genome_size) + "\t" +
                str(mutation_type) + "\n")
Esempio n. 6
0
def plot_mean_sigma_genes_v2(INI_file, sigma_info, RNAPs_pos_info):

    # path to the input files (remove the "params.ini" from the path)
    path = INI_file.rpartition("/")[0] + "/"
    if path=="/":
        path="./"
    # read the config file
    config = sim.read_config_file(INI_file)
    # get inputs infos from the config file
    GFF_file = path+config.get('INPUTS', 'GFF')
    TSS_file = path+config.get('INPUTS', 'TSS')
    TTS_file = path+config.get('INPUTS', 'TTS')
    Prot_file = path+config.get('INPUTS', 'BARR_FIX')

    SIGMA_0 = config.getfloat('SIMULATION', 'SIGMA_0')
    DELTA_X = config.getfloat('SIMULATION', 'DELTA_X')

    # load and get BARR_FIX positions
    prot = sim.load_tab_file(Prot_file)
    BARR_FIX = (prot['prot_pos'].values).astype(int)

    # To draw the beautiful genes we need to read the GFF, TSS and TTS files to get some info ;)
    gff_df_raw = sim.load_gff(GFF_file)
    # to get the cov_bp (a verifier)
    genome_size = sim.get_genome_size(gff_df_raw)
    genome = int(genome_size/DELTA_X)
    cov_bp = np.arange(0, genome_size, DELTA_X)
    cov_bp = np.resize(cov_bp, genome)

    gff_df = sim.rename_gff_cols(gff_df_raw)

    tss = sim.load_tab_file(TSS_file)
    Kon = tss['TSS_strength'].values

    tts = sim.load_tab_file(TTS_file)
    Poff = tts['TTS_proba_off'].values

    strands = sim.str2num(gff_df['strand'].values)

    # Create the figure and all axes to draw to
    fig = plt.figure(1, figsize=(9,6)) # 3.2,2.7
    gs = gridspec.GridSpec(2, 1, height_ratios=[9, 3, 1])
    # Color maps for formatting
    col_map = {}
    col_map['red']     = (0.95, 0.30, 0.25)
    col_map['green']   = (0.38, 0.82, 0.32)
    col_map['blue']    = (0.38, 0.65, 0.87)
    col_map['orange']  = (1.00, 0.75, 0.17)
    col_map['purple']  = (0.55, 0.35, 0.64)
    col_map['yellow']  = (0.98, 0.97, 0.35)
    col_map['grey']    = (0.70, 0.70, 0.70)
    col_map['dark_grey'] = (0.60, 0.60, 0.60)
    col_map['light_grey'] = (0.9, 0.9, 0.9)

    # CDS formatting options
    opt_CDSs = []

    Ps = []
    CDSs = []
    Ts = []

    design = []

    for i in gff_df.index.values:
        opt_CDSs.append({'label':'Gene%s \n%.03f'%(str(i+1),Kon[i]), 
                         'label_style':'italic', 
                         'label_y_offset':-5, 
                         'color':col_map['orange']})
        # Design of the construct
        if strands[i] == True:
            # Promoters
            Ps.append({'type':'Promoter', 'name':'P%s'%str(i+1), 'start':tss['TSS_pos'][i], 
                       'end':tss['TSS_pos'][i]+5, 'fwd':strands[i], 'opts':{'color':col_map['green']}})
            # Coding Sequence
            CDSs.append({'type':'CDS', 'name':'CDS%s'%str(i+1), 'start':gff_df['start'][i],  
                         'end':gff_df['end'][i], 'fwd':gff_df['strand'][i], 'opts':opt_CDSs[i]}) 
        else:
            # Promoters
            Ps.append({'type':'Promoter', 'name':'P%s'%str(i+1), 'start':tss['TSS_pos'][i], 
                       'end':tss['TSS_pos'][i]-5, 'fwd':strands[i], 'opts':{'color':col_map['green']}})
            # Coding Sequence
            CDSs.append({'type':'CDS', 'name':'CDS%s'%str(i+1), 'start':gff_df['end'][i],  
                         'end':gff_df['start'][i], 'fwd':gff_df['strand'][i], 'opts':opt_CDSs[i]}) 
        # Terminators
        Ts.append({'type':'Terminator', 'name':'T%s'%str(i+1), 'start':tts['TTS_pos'][i], 
              'end':tts['TTS_pos'][i]+5, 'fwd':strands[i], 'opts':{'color':col_map['red']}})

        # A design is merely a list of parts and their properties
        if strands[i] == True:
            design.append(Ps[i])
            design.append(CDSs[i]) 
            design.append(Ts[i])
        else:
            design.append(Ts[i])
            design.append(CDSs[i]) 
            design.append(Ps[i])
        
    ax_mean_sig = plt.subplot(gs[0])
    ax_dna = plt.subplot(gs[1])

    # Redender the DNA
    dr = dpl.DNARenderer(scale=7, linewidth=1)
    start, end = dr.renderDNA(ax_dna, design, dr.trace_part_renderers())

    # Set bounds and display options for the DNA axis
    dna_len = end-start
    ax_dna.set_xlim([cov_bp[0], cov_bp[-1]]) #start-50
    ax_dna.set_ylim([-8,8])
    #ax_dna.plot(5000, 'ro', markersize=15)
    for xc in BARR_FIX:
        ax_dna.axvline(x=xc, ymin=0.40, ymax=0.60, color='k', linewidth=5)
    
    ax_dna.plot([cov_bp[0],cov_bp[-1]], [0,0], color=(0,0,0), linewidth=1.0, zorder=1)
    ax_dna.axis('off')
    
    # plot of sigma and mean of sigma
    plt.ion()
    ax_mean_sig.plot(cov_bp, sigma_info, linewidth= 1.5)
    ax_mean_sig.legend(loc='best', fontsize = 12)
    ax_mean_sig.set_ylim([-0.2,0.2])
    ax_mean_sig.set_xlim([0, cov_bp[-1]])

    #ax_mean_sig.set_title(r"Title goes here", fontsize = 13)
    ax_mean_sig.set_ylabel(r'Supercoiling density $(\sigma)$')
    ax_mean_sig.set_xlabel('Position (bp)')
    ax_mean_sig.plot(RNAPs_pos_info*DELTA_X, np.full(len(RNAPs_pos_info), SIGMA_0, dtype=float), 'ro', markersize=12, label = "RNA Polymerase")
    ax_mean_sig.set_ylim([-0.2, 0.2])
    plt.pause(0.001)
    plt.gcf().clear()
    plt.show()  
Esempio n. 7
0
from multiprocessing import Pool
import pandas as pd
import scipy
from evolution import evol_master, get_fitness
from simulation import start_transcribing, load_genome, read_config_file

INI = 'params.ini'
tss, tts, prot, genome_size = load_genome(INI)
config = read_config_file(INI)
environment_file = config.get('EVOLUTION', 'environment')
expected_profile = pd.read_table(environment_file, sep='\t', header=None)[1]


def run(output_file):
    scipy.random.seed()
    gene_expression = start_transcribing(INI, "outputdir", tss, tts, prot,
                                         genome_size)
    fitness = get_fitness(gene_expression, expected_profile)
    with open(output_file, 'w') as f:
        f.write(str(fitness) + '\n')


if __name__ == '__main__':
    p = Pool(8)
    p.map(evol_master,
          [("results/nfitnessevolpinv" + str(i * 0.05) + ".txt", i * 0.05)
           for i in range(21)])
Esempio n. 8
0
def plot_genome(ax_dna, INI_file):
    """
    General Function that plots a genome from an INI file and puts it into a subplot
    """

    # path to the input files (remove the "params.ini" from the path)
    path = INI_file.rpartition("/")[0]
    if path == "":
        path = "."
    path += "/"
    # read the config file
    config = sim.read_config_file(INI_file)
    # get inputs infos from the config file
    GFF_file = path + config.get('INPUTS', 'GFF')
    TSS_file = path + config.get('INPUTS', 'TSS')
    TTS_file = path + config.get('INPUTS', 'TTS')
    Prot_file = path + config.get('INPUTS', 'BARR_FIX')

    SIGMA_0 = config.getfloat('SIMULATION', 'SIGMA_0')
    DELTA_X = config.getfloat('GLOBAL', 'DELTA_X')

    # load and get BARR_FIX positions
    prot = sim.load_tab_file(Prot_file)
    BARR_FIX = (prot['prot_pos'].values).astype(int)

    # To draw the beautiful genes we need to read the GFF, TSS and TTS files to get some info ;)
    gff_df_raw = sim.load_gff(GFF_file)
    # to get the cov_bp (a verifier)
    genome_size = sim.get_genome_size(gff_df_raw)
    genome = int(genome_size / DELTA_X)
    cov_bp = np.arange(0, genome_size, DELTA_X)
    cov_bp = np.resize(cov_bp, genome)

    gff_df = sim.rename_gff_cols(gff_df_raw)

    tss = sim.load_tab_file(TSS_file)
    Kon = tss['TSS_strength'].values

    tts = sim.load_tab_file(TTS_file)
    Poff = tts['TTS_proba_off'].values

    strands = sim.str2num(gff_df['strand'].values)

    # Color maps for formatting
    col_map = {}
    col_map['red'] = (0.95, 0.30, 0.25)
    col_map['green'] = (0.38, 0.82, 0.32)
    col_map['blue'] = (0.38, 0.65, 0.87)
    col_map['orange'] = (1.00, 0.75, 0.17)
    col_map['purple'] = (0.55, 0.35, 0.64)
    col_map['yellow'] = (0.98, 0.97, 0.35)
    col_map['grey'] = (0.70, 0.70, 0.70)
    col_map['dark_grey'] = (0.60, 0.60, 0.60)
    col_map['light_grey'] = (0.9, 0.9, 0.9)

    # CDS formatting options
    opt_CDSs = []

    Ps = []
    CDSs = []
    Ts = []

    design = []

    for i in gff_df.index.values:
        opt_CDSs.append({
            'label': 'Gene%s \n%.03f' % (str(i + 1), Kon[i]),
            'label_style': 'italic',
            'label_y_offset': -5,
            'label_size': 9,
            'color': col_map['orange']
        })
        # Design of the construct
        if strands[i] == True:
            # Promoters
            Ps.append({
                'type': 'Promoter',
                'name': 'P%s' % str(i + 1),
                'start': tss['TSS_pos'][i],
                'end': tss['TSS_pos'][i] + 5,
                'fwd': strands[i],
                'opts': {
                    'color': col_map['green']
                }
            })
            # Coding Sequence
            CDSs.append({
                'type': 'CDS',
                'name': 'CDS%s' % str(i + 1),
                'start': gff_df['start'][i],
                'end': gff_df['end'][i],
                'fwd': gff_df['strand'][i],
                'opts': opt_CDSs[i]
            })
        else:
            # Promoters
            Ps.append({
                'type': 'Promoter',
                'name': 'P%s' % str(i + 1),
                'start': tss['TSS_pos'][i],
                'end': tss['TSS_pos'][i] - 5,
                'fwd': strands[i],
                'opts': {
                    'color': col_map['green']
                }
            })
            # Coding Sequence
            CDSs.append({
                'type': 'CDS',
                'name': 'CDS%s' % str(i + 1),
                'start': gff_df['end'][i],
                'end': gff_df['start'][i],
                'fwd': gff_df['strand'][i],
                'opts': opt_CDSs[i]
            })
        # Terminators
        Ts.append({
            'type': 'Terminator',
            'name': 'T%s' % str(i + 1),
            'start': tts['TTS_pos'][i],
            'end': tts['TTS_pos'][i] + 5,
            'fwd': strands[i],
            'opts': {
                'color': col_map['red']
            }
        })

        # A design is merely a list of parts and their properties
        if strands[i] == True:
            design.append(Ps[i])
            design.append(CDSs[i])
            design.append(Ts[i])
        else:
            design.append(Ts[i])
            design.append(CDSs[i])
            design.append(Ps[i])

    # Redender the DNA
    dr = dpl.DNARenderer(scale=7, linewidth=1)
    start, end = dr.renderDNA(ax_dna, design, dr.trace_part_renderers())

    # Set bounds and display options for the DNA axis
    dna_len = end - start
    ax_dna.set_xlim([cov_bp[0], cov_bp[-1]])  #start-50
    ax_dna.set_ylim([-8, 8])
    #ax_dna.plot(5000, 'ro', markersize=15)
    for xc in BARR_FIX:
        ax_dna.axvline(x=xc, ymin=0.40, ymax=0.60, color='k', linewidth=5)

    ax_dna.plot([cov_bp[0], cov_bp[-1]], [0, 0],
                color=(0, 0, 0),
                linewidth=1.0,
                zorder=1)
    ax_dna.axis('off')
    return SIGMA_0, DELTA_X, BARR_FIX, cov_bp