示例#1
0
def update_fast_reaction(data_dir, tau=0.7, end_t=1.0, tag="M"):
    """
    update fast reaction based on reference trajectory
    """
    fn0 = os.path.join(data_dir, "input", "reaction_info_base_backup.json")
    fn1 = os.path.join(data_dir, "input", "reaction_info_base.json")

    rxn_info = rwc.read_configuration(fn1)

    time_v = np.loadtxt(os.path.join(data_dir, "output",
                                     "time_dlsode_" + str(tag) + ".csv"),
                        delimiter=",")
    rxn_rates = np.loadtxt(os.path.join(
        data_dir, "output", "reaction_rate_dlsode_" + str(tag) + ".csv"),
                           delimiter=",")

    actual_time = float(tau) * float(end_t)
    for _, val in enumerate(rxn_info):
        actual_rate = interpolation.interp1d(time_v, rxn_rates[:, int(val)],
                                             actual_time)
        if actual_rate != 0:
            time_scale = np.log10(actual_rate)
            if time_scale >= -100 and time_scale <= 100:
                # print(time_scale)
                rxn_info[val]["time_scale"] = time_scale

    if os.path.isfile(fn1):
        copy2(fn1, fn0)

    rwc.write_configuration(rxn_info, fn1)
示例#2
0
def get_normalized_concentration_at_time(data_dir,
                                         tag="fraction",
                                         tau=10.0,
                                         end_t=1.0,
                                         exclude_names=None,
                                         renormalization=True):
    """
    return normalized species concentration at time
    """
    if exclude_names is None:
        exclude_names = []
    time = np.loadtxt(os.path.join(data_dir, "output",
                                   "time_dlsode_" + str(tag) + ".csv"),
                      delimiter=",")
    conc_all = np.loadtxt(os.path.join(
        data_dir, "output", "concentration_dlsode_" + str(tag) + ".csv"),
                          delimiter=",")

    n_spe = np.shape(conc_all)[1]

    conc = [float] * n_spe
    for i in range(n_spe):
        conc[i] = interpolation.interp1d(time, conc_all[:, i], tau * end_t)

    _, s_n_idx = psri.parse_spe_info(data_dir)
    exclude_idx_list = [int(s_n_idx[x]) for x in exclude_names]
    # set the concentration of these species to be zero
    for _, idx in enumerate(exclude_idx_list):
        conc[idx] = 0.0

    if renormalization is False:
        return conc

    _, s_n_idx = psri.parse_spe_info(data_dir)
    # renormalization
    exclude_idx_list = [int(s_n_idx[x]) for x in exclude_names]
    # set the concentration of these species to be zero
    for _, idx in enumerate(exclude_idx_list):
        conc[idx] = 0.0
    conc /= np.sum(conc)
    return conc
示例#3
0
def pathway_time_2_array_index(data_dir,
                               init_spe=None,
                               atom_followed="C",
                               end_t=1.0,
                               species_path=False,
                               time=1.0):
    """
    pathway time converted to array index, pathway time read from pathway_time_canditate*
    """
    suffix = get_suffix(data_dir,
                        init_spe=init_spe,
                        atom_followed=atom_followed,
                        end_t=end_t)
    prefix = ""
    if species_path is True:
        prefix = "species_"

    f_n_path_time = os.path.join(
        data_dir, "output",
        prefix + "pathway_time_candidate" + suffix + ".csv")

    p_time = np.genfromtxt(f_n_path_time, dtype=float, delimiter=',')

    # in case of two dimensional pathway time
    if len(np.shape(p_time)) == 2:
        p_time = p_time[0, :]

    y_idx = [float(i) for i in range(len(p_time))]
    array_idx = interpolation.interp1d(p_time, y_idx, time)

    array_idx = int(array_idx)

    if array_idx >= len(p_time):
        array_idx = len(p_time) - 1
    if array_idx < 0:
        array_idx = 0

    return array_idx
示例#4
0
def init_directed_network_from_X_and_R_at_a_time(data_dir,
                                                 tag="M",
                                                 tau=10.0,
                                                 end_t=0.5,
                                                 end_t2=None,
                                                 x_y_dict=None):
    """
    init directed network
    without parallel edges
    return networkx.DiGraph
    at least time-snapshot network at a time, end_t,
    a second time can be added, but don't change network structure, 
    instead add a second attribute "weight2" to edges
    """
    s_idx_2_name, _ = psri.parse_spe_info(data_dir)
    spe_alias = read_spe_alias(
        os.path.join(data_dir, "input", "spe_alias.json"))

    time_v = np.loadtxt(os.path.join(data_dir, "output", "time_dlsode_M.csv"),
                        dtype=float,
                        delimiter=',')
    conc_mat = np.loadtxt(os.path.join(
        data_dir, "output", "concentration_dlsode_" + str(tag) + ".csv"),
                          delimiter=",")
    rxn_rates_mat = np.loadtxt(os.path.join(
        data_dir, "output", "reaction_rate_dlsode_" + str(tag) + ".csv"),
                               delimiter=",")
    # the time point where reference time tau is
    # use interpolation here
    idx_array = [i for i in range(len(time_v))]
    time_axis = int(
        round(interpolation.interp1d(time_v, idx_array, tau * end_t)))
    if time_axis >= len(time_v):
        time_axis = len(time_v) - 1

    conc_v = conc_mat[time_axis, :]
    rxn_rates_v = rxn_rates_mat[time_axis, :]

    if end_t2 is not None:
        time_axis2 = int(
            round(interpolation.interp1d(time_v, idx_array, tau * end_t2)))
        if time_axis2 >= len(time_v):
            time_axis2 = len(time_v) - 1
        rxn_rates_v2 = rxn_rates_mat[time_axis2, :]

    species_set = set()
    species_pair_weight = {}
    if end_t2 is not None:
        species_pair_weight2 = {}

    # species pairs-reactions-coefficient
    s_p_r_c = psri.parse_species_pair_reaction(data_dir)
    # print(s_p_r_c)
    for s1, s2 in s_p_r_c:
        species_set.add(int(s1))
        # print(s1, s2)
        species_set.add(int(s2))
        if (int(s1), int(s2)) not in species_pair_weight:
            species_pair_weight.update({(int(s1), int(s2)): 0.0})
        if end_t2 is not None:
            if (int(s1), int(s2)) not in species_pair_weight2:
                species_pair_weight2.update({(int(s1), int(s2)): 0.0})

        for idx in s_p_r_c[(s1, s2)]:
            r_idx = int(s_p_r_c[(s1, s2)][idx]['r_idx'])
            c1 = float(s_p_r_c[(s1, s2)][idx]['c1'])
            c2 = float(s_p_r_c[(s1, s2)][idx]['c2'])
            flux = rxn_rates_v[r_idx] * c2 / c1
            species_pair_weight[(int(s1), int(s2))] += flux

            if end_t2 is not None:
                flux2 = rxn_rates_v2[r_idx] * c2 / c1
                species_pair_weight2[(int(s1), int(s2))] += flux2

    # print(species_set)
    # print(species_pair_weight)

    edge_weight_v = []
    for idx, key in enumerate(species_pair_weight):
        edge_weight_v.append(float(species_pair_weight[key]))
    if end_t2 is not None:
        edge_weight_v2 = []
        for idx, key in enumerate(species_pair_weight2):
            edge_weight_v2.append(float(species_pair_weight2[key]))

    # rescase concentrations
    # conc_v = rescale_array(conc_v, 10.0, 25.0)
    conc_v = rescale_array_v2(conc_v, 1.0, 5.0, 15.0, 25.0, -12)
    # edge_weight_v = rescale_array(edge_weight_v, 2.0, 25.0)
    edge_weight_v = rescale_array_v2(edge_weight_v, 0.5, 1.0, 15.0, 25.0, -9)
    # edge weights write to file
    e_w_fn1 = os.path.join(data_dir, "output",
                           "edge_weight1_" + str(end_t) + ".csv")
    f_hanlder1 = open(e_w_fn1, 'w')
    f_hanlder1.write("Source,Target,Weight" + str(end_t) + "\n")
    # np.savetxt(e_w_fn1, edge_weight_v, fmt='%.18e', newline='\n')
    if end_t2 is not None:
        # edge_weight_v2 = rescale_array(edge_weight_v2, 2.0, 25.0)
        edge_weight_v2 = rescale_array_v2(edge_weight_v2, 1.5, 2.5, 15.0, 25.0,
                                          -9)

        e_w_fn2 = os.path.join(data_dir, "output",
                               "edge_weight2_" + str(end_t2) + ".csv")
        # np.savetxt(e_w_fn2, edge_weight_v2, fmt='%.18e', newline='\n')
        f_hanlder2 = open(e_w_fn2, 'w')
        f_hanlder2.write("Source,Target,Weight" + str(end_t2) + "\n")

    # final directed graph
    di_graph = nx.DiGraph()
    # add nodes first
    for idx, val in enumerate(species_set):
        weight = float(conc_v[int(val)])
        node_name = change_spe_name(s_idx_2_name[str(val)], spe_alias, None)
        # add a layer to control whether to show the species label name
        label_name = ''
        if s_idx_2_name[str(val)] in spe_alias:
            label_name = node_name
        if x_y_dict is None:
            di_graph.add_node(node_name, label=label_name, weight=weight)
        else:
            di_graph.add_node(node_name,
                              label=label_name,
                              weight=weight,
                              x=x_y_dict[node_name][0],
                              y=x_y_dict[node_name][1])

    # add edges
    for idx, key in enumerate(species_pair_weight):
        src = key[0]
        dst = key[1]
        src_name = change_spe_name(s_idx_2_name[str(src)], spe_alias, None)
        dst_name = change_spe_name(s_idx_2_name[str(dst)], spe_alias, None)
        name = src_name + "," + dst_name
        # write weight1 to file
        f_hanlder1.write(src_name + "," + dst_name + "," + str(weight) + "\n")
        if end_t2 is None:
            weight = float(edge_weight_v[idx])
            di_graph.add_edge(src_name,
                              dst_name,
                              name=name,
                              weight=weight,
                              weight2=weight)
        else:
            weight = float(edge_weight_v[idx])
            weight2 = float(edge_weight_v2[idx])
            di_graph.add_edge(src_name,
                              dst_name,
                              name=name,
                              weight=weight,
                              weight2=weight2)
            # write weight2 to file
            f_hanlder2.write(src_name + "," + dst_name + "," + str(weight2) +
                             "\n")

    f_hanlder1.close()
    if end_t2 is not None:
        f_hanlder2.close()

    return di_graph
示例#5
0
def cal_passage_time_distribution(data_dir,
                                  spe_idx=62,
                                  tau=10.0,
                                  t_f=0.5,
                                  n_point=7000):
    """
    calculate passage time distribution,
    """
    f_n_time = os.path.join(data_dir, "output", "time_dlsode_M.csv")
    f_n_drc = os.path.join(data_dir, "output", "drc_dlsode_M.csv")

    time_o = np.loadtxt(f_n_time, dtype=float, delimiter=',')
    drc = np.loadtxt(f_n_drc, dtype=float, delimiter=',')

    tf_idx = 0
    if t_f is None:
        tf_idx = len(drc) - 1
    else:
        idx_array = [i for i in range(len(time_o))]
        tf_idx = int(
            round(interpolation.interp1d(time_o, idx_array, tau * t_f)))

    if tf_idx == 0:
        tf_idx = 1

    if n_point is None or n_point <= 3:
        n_point = tf_idx
    time = np.linspace(time_o[0], time_o[tf_idx], n_point)
    spe_drc = np.zeros(len(time))
    for idx, val in enumerate(time):
        # use value at time point 1, avoid 0 value
        spe_drc[idx] = interpolation.interp1d(time_o[1::], drc[1::, spe_idx],
                                              val)

    # integral of k
    spe_drc_int = np.zeros(len(spe_drc))
    for i in range(1, len(spe_drc_int)):
        spe_drc_int[i] = spe_drc_int[i-1] + \
            (spe_drc[i])*(time[i] - time[i-1])

    norm_factor = 0.0
    # norm_factor = spe_drc_int[0] - spe_drc_int[-1]
    print(spe_drc, spe_drc_int)

    # survival probability
    survival_P = deepcopy(spe_drc_int)
    for idx, val in enumerate(survival_P):
        survival_P[idx] = np.exp(-1.0 * val)
    print(survival_P)

    survival_P_gradient = np.gradient(survival_P, time)
    print(survival_P_gradient)

    for i in range(1, len(survival_P_gradient)):
        norm_factor += survival_P_gradient[i] * (time[i] - time[i - 1])

    for idx, val in enumerate(survival_P_gradient):
        survival_P_gradient[idx] /= norm_factor
    print(survival_P_gradient)
    print(sum(survival_P_gradient) * (time[1] - time[0]))

    f_n_time_pt = os.path.join(data_dir, "output",
                               "time_pt_dlsode_M_" + str(t_f) + ".csv")
    f_n_pt = os.path.join(data_dir, "output",
                          "pt_dlsode_M_" + str(t_f) + ".csv")
    np.savetxt(f_n_time_pt, time, fmt='%.18e', delimiter='\n')
    np.savetxt(f_n_pt, survival_P_gradient, fmt='%.18e', delimiter='\n')

    return time, survival_P_gradient
示例#6
0
def get_species_with_top_n_concentration(data_dir,
                                         exclude,
                                         top_n=10,
                                         traj_max_t=100.0,
                                         tau=10.0,
                                         end_t=1.0,
                                         tag="M",
                                         atoms=None):
    """
    get species concentration at a tau, where tau is the ratio of the time_wanted/end_time
    """
    if atoms is None:
        atoms = ["C"]
    if exclude is None:
        exclude = []

    time = np.loadtxt(os.path.join(data_dir, "output",
                                   "time_dlsode_" + str(tag) + ".csv"),
                      delimiter=",")
    conc_all = np.loadtxt(os.path.join(
        data_dir, "output", "concentration_dlsode_" + str(tag) + ".csv"),
                          delimiter=",")

    n_spe = np.shape(conc_all)[1]

    data = [float] * n_spe
    for i in range(n_spe):
        data[i] = interpolation.interp1d(time, conc_all[:, i], tau * end_t)

    c_idx_map = defaultdict(set)
    for idx, val in enumerate(data):
        c_idx_map[val].add(str(idx))
    c_idx_map = OrderedDict(sorted(c_idx_map.items(), reverse=True))

    spe_idx_name_dict, _ = psri.parse_spe_info(data_dir)
    spe_composition = psri.read_spe_composition(
        os.path.join(data_dir, "input", "spe_composition.json"))

    spe_idx_list = []
    counter = 0

    for _, val in enumerate(c_idx_map):
        if counter < top_n:
            spe_idx = next(iter(c_idx_map[val]))
            indicator = False
            for _, atom in enumerate(atoms):
                if atom in spe_composition[spe_idx_name_dict[spe_idx]]:
                    indicator = True
                    break
            if spe_idx_name_dict[spe_idx] not in exclude and indicator:
                print(val, spe_idx, spe_idx_name_dict[spe_idx])
                spe_idx_list.append(int(spe_idx))
                counter += 1

    # species doesn't contain atom we are interested in
    exclude_spe_name_list = []
    for idx, s_n_t in enumerate(spe_composition):
        indicator = False
        for _, atom in enumerate(atoms):
            if atom in spe_composition[s_n_t]:
                indicator = True
        if indicator is False:
            exclude_spe_name_list.append(s_n_t)
    spe_name_list = [str(spe_idx_name_dict[str(x)]) for x in spe_idx_list]
    return spe_idx_list, spe_name_list, exclude_spe_name_list