Пример #1
0
def control_dictionaries_creator():
    """
    Create the control dictionary containing the values corresponding to the score of bp and ppt for every control exons
    """
    exon_class.set_debug(0)
    dir_path = os.path.dirname(os.path.realpath(__file__))
    fasterdb = os.path.dirname(os.path.realpath(__file__)).replace(
        "src/minimum_free_energy", "data/fasterDB_lite.db")
    seddb = os.path.dirname(os.path.realpath(__file__)).replace(
        "src/minimum_free_energy", "data/sed.db")
    ctrl_dir = dir_path + "/control_dictionaries/"
    cnx = sqlite3.connect(fasterdb)
    cnx_sed = sqlite3.connect(seddb)
    if not os.path.isdir(ctrl_dir):
        os.mkdir(ctrl_dir)
    exon_type = "CCE"
    exon2remove = union_dataset_function.get_exon_regulated_by_sf(
        cnx_sed, "down")
    ctrl_exon_list = get_control_exon_information(cnx, exon_type, exon2remove)
    print("retrieving upstream intron sequence")
    list_exon = [
        exon_class.ExonClass(cnx, exon[0], exon[1], exon[2])
        for exon in ctrl_exon_list
    ]
    print("calculating mfe")
    mfe_list_3ss, mfe_list_5ss = function.mfe_calculator(list_exon)
    cur_file = open(ctrl_dir + exon_type + "_mfe.py", "w")
    cur_file.write("mfe_3ss=" + str(mfe_list_3ss) + "\n")
    cur_file.write("mfe_5ss=" + str(mfe_list_5ss) + "\n")
    cur_file.close()
def extract_exon_files(cnx, filename):
    """
    :param cnx: (sqlite3 connect object) connection to fasterDB lite
    :param filename: (string) the name of a file containing exons
    :return: (list of Exonclass object) list of exons
    """
    exon_list = []
    with open(filename, "r") as outfile:
        line = outfile.readline()
        while line:
            line = line.replace("\n", "")
            line = line.split("\t")
            exon = exon_class.ExonClass(cnx, str(line[0]), int(line[0]),
                                        int(line[1]))
            exon_list.append(exon)
            line = outfile.readline()
    return exon_list
Пример #3
0
def computing_mfe(cnx, df, output):
    """
    Add a column mfe 5'ss to the existing dataframe.

    :param cnx: (sqlite3 dataframe object) connection to fasterdb
    :param df: (pandas dataframe) table of exons
    :param output: (str) files were the mfe results will be created
    :return:  (pandas dataframe) table of exons with mfe data
    """
    exon_class_list = []
    exon_list = df[["gene_name", "gene_id", "pos"]].values
    for exon in exon_list:
        exon_class_list.append(
            exon_class.ExonClass(cnx, exon[0], exon[1], exon[2]))
    mfe_3ss, mfe_5ss = function.mfe_calculator(exon_class_list,
                                               output,
                                               ps=True)
    df["mfe_5ss"] = mfe_5ss
    return df
Пример #4
0
def control_dictionaries_creator():
    """
    Create the control dictionary containing the values corresponding to the score of bp and ppt for every control exons
    """
    exon_class.set_debug(0)
    dir_path = os.path.dirname(os.path.realpath(__file__))
    fasterdb = os.path.dirname(os.path.realpath(__file__)).replace(
        "src", "data/fasterDB_lite.db")
    seddb = os.path.dirname(os.path.realpath(__file__)).replace(
        "src", "data/sed.db")
    ctrl_dir = dir_path + "/control_dictionaries/"
    cnx = sqlite3.connect(fasterdb)
    cnx_sed = sqlite3.connect(seddb)
    if not os.path.isdir(ctrl_dir):
        os.mkdir(ctrl_dir)
    exon_type = "CCE"
    sizes = [100, 25, 50, 35]
    exon2remove = union_dataset_function.get_exon_regulated_by_sf(
        cnx_sed, "down")
    ctrl_exon_list = get_control_exon_information(cnx, exon_type, exon2remove,
                                                  "down")
    for size in sizes:
        print("size : %s" % size)
        print("retrieving upstream intron sequence")
        list_exon = [
            exon_class.ExonClass(cnx, exon[0], exon[1], exon[2])
            for exon in ctrl_exon_list
        ]
        print("calculating bp and ppt score")
        bp_score_list, ppt_score_list, nb_bp_list, nb_good_bp_list, \
            sequence_list, ag_count_list, hbound_list = function.bp_ppt_calculator(list_exon, size)
        cur_file = open(
            ctrl_dir + exon_type + "_" + str(size) + "_bp_ppt_score.py", "w")
        cur_file.write("bp_score=" + str(bp_score_list) + "\n")
        cur_file.write("ppt_score=" + str(ppt_score_list) + "\n")
        cur_file.write("nb_bp=" + str(nb_bp_list) + "\n")
        cur_file.write("nb_good_bp=" + str(nb_good_bp_list) + "\n")
        cur_file.write("bp_seq=" + str(sequence_list) + "\n")
        cur_file.write("ag_count=" + str(ag_count_list) + "\n")
        cur_file.write("hbound=" + str(hbound_list) + "\n")
        cur_file.close()
def extract_data(cnx, cnx_sed, list_files, list_names, pos):
    """

    :param cnx: (sqlite3 connect object) connection to fasterDB lite
    :param cnx_sed: (sqlite3 connect object) connection to sed
    :param list_files: (list of string) list of files containing exon set
    :param list_names: (list of string) the name of exon set
    :param pos: (int) the position of interest within the list ``list_files`` and ``list_names``. \
    Those 2 lists must hace the same lenght
    :return: (list of ExonClass object) list of exon.
    """
    if list_files:
        exon_list = extract_exon_files(cnx, list_files[pos])
    else:
        exon_list_tmp = union_dataset_function.get_every_events_4_a_sl(
            cnx_sed, list_names[pos], "down")
        exon_list = [
            exon_class.ExonClass(cnx, str(exon[0]), int(exon[0]), int(exon[1]))
            for exon in exon_list_tmp
        ]
    print("%s : %s exons" % (list_names[pos], len(exon_list)))
    return exon_list
def get_exon_info(cnx, info_list, debug):
    """
    Get every information we need on an exon

    :param debug: (int) 0 no debug, 1 debug mode
    :param cnx: (sqlite3 object) return all the information we need to connect to FasterDB lite
    :param info_list: (list of list of string and int and int) each sublist contains \
    a string : gene_symbol and 2 int : the gene_id and the exobn position on gene respectively
    :return: (a list of ExonClass object) list of exons
    """
    print("Getting exons information !")
    exon_list = []
    exon_class.set_debug(debug)
    count = 0
    ll = str(len(info_list))
    for exon_info in info_list:
        exon_list.append(exon_class.ExonClass(cnx, exon_info[0], exon_info[1], exon_info[2]))
        count += 1
        percent = round(float(count) / len(info_list) * 100, 1)
        sys.stdout.write("Progression : " + str(count) + " / " + ll + " - " + str(percent) + " %\r")
        sys.stdout.flush()
    return exon_list
Пример #7
0
def handle_nb_bp_recovering(cnx, exon_list, output, sf_name, regulation, target):
    """
    Recover nb bp for the exon list regulated by ``sf_name``
    :param cnx: (sqlite3 connect object) connection to fasterDB database
    :param exon_list: (list of 2 int) gene id + exon pos in gene
    :param output: (string) folder where the result will be created
    :param sf_name: (string) the name of the splicing factor studied
    :param regulation: (string) regulation up or down
    :param target: (string) the value we want to recover
    :return: (list of int) the list of good quality branch point
    """
    output_file = "%s%s_%s_%s_nt.py" % (output, sf_name, regulation, size_bp_up_seq)
    if not os.path.isfile(output_file):
        new_exon_list = [exon_class.ExonClass(cnx, str(exon[0]), int(exon[0]), int(exon[1])) for exon in exon_list]
        bp_score_list, ppt_score_list, nb_bp_list, nb_good_bp_list, \
            sequence_list, ag_count_list, hbound_list = function.bp_ppt_calculator(new_exon_list, size_bp_up_seq)
        with open(output_file, "w") as bp_file:
            bp_file.write("bp_score=%s\n" % str(bp_score_list))
            bp_file.write("ppt_score=%s\n" % str(ppt_score_list))
            bp_file.write("nb_bp=%s\n" % str(nb_bp_list))
            bp_file.write("nb_good_bp=%s\n" % str(nb_good_bp_list))
            bp_file.write("bp_seq=%s\n" % str(sequence_list))
            bp_file.write("ag_count=%s\n" % str(ag_count_list))
            bp_file.write("hbound=%s\n" % str(hbound_list))
    else:
        sys.path.insert(0, output)
        mod = __import__(output_file.split("/")[-1].replace(".py", ""))
        nb_good_bp_list = mod.nb_good_bp
        hbound_list = mod.hbound
        ag_count_list = mod.ag_count
    if target == "nb_good_bp":
        return nb_good_bp_list
    elif target == "hbound":
        return hbound_list
    else:
        return ag_count_list
def get_exon_info(cnx, sedb, fasterdb_file, exon_list, u1_exons, u2_exons):
    """

    :param cnx: (sqlite3 connect object) connexion to fasterdb
    :param fasterdb_file: (str) an sqlite3 database file
    :param sedb: (str) path to sed database
    :param exon_list:  (list of 2 int) list of exons
    :param u1_exons: (list of list of 2 int) list of exons regulated by U1
    :param u2_exons: (list of list of 2 int) list of exons regulated by U2
    :return: (list of list of value) list of data
    """
    dic = {-1: "-", 1: "+"}
    cursor = cnx.cursor()
    cursor.execute("ATTACH DATABASE ? as sed", (sedb, ))
    cursor.execute("ATTACH DATABASE ? as fasterdb", (fasterdb_file, ))
    if exon_list is None:
        query = """
                SELECT t1.id_gene, t1.pos_on_gene, t1.chromosome, 
                       t1.start_on_chromosome, t1.end_on_chromosome, 
                       t2.strand, t3.iupac_exon, t3.upstream_intron_size,
                       t3.downstream_intron_size
                FROM fasterdb.exons as t1, fasterdb.genes as t2, sed.sed as t3
                WHERE t3.gene_id =  t1.id_gene
                AND   t3.exon_pos = t1.pos_on_gene
                AND   t1.id_gene = t2.id
                AND   t3.exon_type LIKE '%CCE%'
                """
        cursor.execute(query)
        res = cursor.fetchall()
        new_res = []
        for exon in res:
            exon = list(exon)
            exon[3] = int(exon[3]) - 1
            cexon = exon_class_bp.ExonClass(cnx, str(exon[0]), exon[0],
                                            exon[1])
            exon_data = bp_ppt_calculator([cexon])
            mexon = exon_class.ExonClass(cnx, str(exon[0]), exon[0], exon[1])
            mfe_5ss, mfe_3ss = mfe_calculator([mexon])
            stretch = catch_index_error(stretch_counter([cexon])["T"], 0)
            dic_info = {
                "GC_content": exon[6].split(";")[4],
                "upstream_intron_size": exon[7],
                "downstream_intron_size": exon[8],
                "UNA_count": catch_index_error(exon_data[8], 0),
                "Hbound_count": catch_index_error(exon_data[6], 0),
                "good_bp": catch_index_error(exon_data[3], 0),
                "MFE_5SS": catch_index_error(mfe_5ss, 0),
                "MFE_3SS": catch_index_error(mfe_3ss, 0),
                "T_stretch": stretch,
                "U1-regulated": is_in(exon[0:2], u1_exons),
                "U2-regulated": is_in(exon[0:2], u2_exons),
            }

            new_res.append(exon[2:5] + ["%s_%s" % (exon[0], exon[1])] + \
                    ["0", dic[exon[5]]] + [str(dic_info)])
        return new_res
    count = 0
    tot = len(exon_list)
    result = []
    for exon in exon_list:
        count += 1
        query = """
                SELECT t1.chromosome, t1.start_on_chromosome, 
                       t1.end_on_chromosome, t2.strand, t3.iupac_exon,
                       t3.upstream_intron_size, t3.downstream_intron_size
                FROM fasterdb.exons as t1, fasterdb.genes as t2, sed.sed as t3
                WHERE t3.gene_id =  t1.id_gene
                AND   t3.exon_pos = t1.pos_on_gene
                AND   t1.id_gene = t2.id
                AND   t3.gene_id = %s
                AND   t3.exon_pos = %s
                """ % (exon[0], exon[1])
        cursor.execute(query)
        res = cursor.fetchall()
        if len(res) > 1:
            raise IndexError("Error only one row shoud be return for %s" %
                             exon)
        tmp = list(res[0])
        tmp[1] = int(tmp[1]) - 1
        cexon = exon_class_bp.ExonClass(cnx, str(exon[0]), exon[0], exon[1])
        exon_data = bp_ppt_calculator([cexon])
        mexon = exon_class.ExonClass(cnx, str(exon[0]), exon[0], exon[1])
        mfe_5ss, mfe_3ss = mfe_calculator([mexon])
        stretch = catch_index_error(stretch_counter([cexon])["T"], 0)
        dic_info = {
            "GC_content": tmp[4].split(";")[4],
            "upstream_intron_size": tmp[5],
            "downstream_intron_size": tmp[6],
            "UNA_count": catch_index_error(exon_data[8], 0),
            "Hbound_count": catch_index_error(exon_data[6], 0),
            "good_bp": catch_index_error(exon_data[3], 0),
            "MFE_5SS": catch_index_error(mfe_5ss, 0),
            "MFE_3SS": catch_index_error(mfe_3ss, 0),
            "T_stretch": stretch,
            "U1-regulated": is_in(exon[0:2], u1_exons),
            "U2-regulated": is_in(exon[0:2], u2_exons),
        }
        exon_data = tmp[0:3] + ["%s_%s" % (exon[0], exon[1])] + \
                    ["0", dic[tmp[3]]] + [str(dic_info)]
        result.append(exon_data)
        sys.stdout.write("Processing %s/%s\t\t\t\r" % (count, tot))
        sys.stdout.flush()
    return result