Beispiel #1
0
def main():
    # example: python bart_plot.py user_key
    # print (sys.argv)

    # get argv
    script_name = sys.argv[0]
    user_key = sys.argv[1]  # user_key is needed

    import do_process
    user_data = do_process.get_user_data(user_key)
    user_path = user_data['user_path']

    bart_result_file = ''
    bart_auc_file = ''
    auc_result_dict = {}

    bart_title = [
        'tf_name', 'tf_score', 'p_value', 'z_score', 'max_auc', 'r_rank'
    ]
    bart_output_dir = os.path.join(user_path, 'download/bart_output')

    bart_auc_ext = '_auc.txt'
    for root, dirs, files in os.walk(bart_output_dir):
        for bart_file in files:
            if bart_auc_ext in bart_file:
                bart_auc_file = os.path.join(root, bart_file)
                user_file_name = bart_file.strip(bart_auc_ext)
                auc_result_dict[user_file_name] = {}
                auc_result_dict[user_file_name]['auc'] = bart_auc_file

    bart_res_ext = '_bart_results.txt'
    for root, dirs, files in os.walk(bart_output_dir):
        for bart_file in files:
            if bart_res_ext in bart_file:
                bart_result_file = os.path.join(root, bart_file)
                user_file_name = bart_file.strip(bart_res_ext)
                if user_file_name not in auc_result_dict:
                    auc_result_dict[user_file_name] = {}
                    # something definitely went wrong if no _auc.txt files!
                    # the plot can not be plotted!!
                    # only the statistics will be shown!
                auc_result_dict[user_file_name]['res'] = bart_result_file

    for user_file_name, bart_files in auc_result_dict.items():
        bart_auc_file = bart_files['auc']
        bart_result_file = bart_files['res']
        AUCs = get_AUCs(bart_auc_file)
        bart_df = pd.read_csv(bart_result_file,
                              sep='\t',
                              names=bart_title[1:],
                              index_col=0,
                              skiprows=1)
        plot_top_tf(bart_df, bart_output_dir, AUCs)
Beispiel #2
0
def is_bart_done(user_path):
    user_key = os.path.basename(user_path)
    import do_process
    user_data = do_process.get_user_data(user_key)
    for user_file in user_data['files']:
        uploaded_file = os.path.basename(user_file).split('.')[
            0]  # path/to/user/upload/filename.bam(txt)
        res_file_path = os.path.join(
            user_path,
            'download/bart_output/' + uploaded_file + '_bart_results.txt'
        )  # path/to/user/download/filename_bart_results.txt
        if not os.path.exists(res_file_path):
            return False

    return True
Beispiel #3
0
def send_mail():
    usercase_dir = os.path.join(PROJECT_DIR, 'usercase')
    for userkey in os.listdir(usercase_dir):
        user_path = os.path.join(usercase_dir, userkey)
        # iterate the user path
        if not os.path.isdir(user_path):
            continue
        
        user_data = do_process.get_user_data(userkey)
        if not user_data:
            continue
        if 'status' not in user_data: # not yet processing
            continue

        if 'user_email' in user_data:
            if user_data['user_email'] == '':
                continue

            if user_data['status'] == 'Done':
                logger.info('Send Email: send success e-mail to user {}'.format(userkey))
                send_flag, send_msg = utils.send_user_key(user_data['user_email'], userkey, user_data['status'])
                if send_flag:
                    logger.info('Send Email: ' + send_msg)
                else:
                    logger.error('Send Email: ' + send_msg)
                user_data['status'] = 'Sent'
                do_process.init_user_config(user_path, user_data)


            if user_data['status'] == 'Error':
                logger.info('Job Finish: send error e-mail to user {}'.format(userkey))
                send_flag, send_msg = utils.send_user_key(user_data['user_email'], userkey, user_data['status'])

                if send_flag:
                    logger.info('Send Email: ' + send_msg)
                else:
                    logger.error('Send Email: ' + send_msg)
                user_data['status'] = 'Sent'
                do_process.init_user_config(user_path, user_data)
Beispiel #4
0
def main():
    # example: python exe_mb_pipline.py 3 user_key True/False
    # print (sys.argv)

    # get argv
    script_name = sys.argv[0]
    repeat_times = int(sys.argv[1])
    user_key = sys.argv[2]
    bart_flag = bool(sys.argv[3])

    import do_process
    user_data = do_process.get_user_data(user_key)
    user_path = user_data['user_path']
    files = user_data['files']

    err_msg = ""
    for i in range(repeat_times):
        marge_output_dir = os.path.join(user_path, 'marge_{}'.format(i))
        if init_marge(marge_output_dir):
            config_marge(user_data, marge_output_dir)
            subprocess.call(["snakemake", "-n"], stdout=subprocess.PIPE, cwd=marge_output_dir)
        else:
            err_msg += "Error in init marge NO.%d \n" % (i+1) 

    import multiprocessing
    pool = multiprocessing.Pool(processes=MARGE_CORE)
    for i in range(repeat_times):
        marge_output_dir = os.path.join(user_path, 'marge_{}'.format(i))
        pool.apply_async(exe_marge, args=(marge_output_dir, ))

    pool.close()
    pool.join() 

    # get marge output
    auc_scores = []
    auc_files = []
    import re
    pattern = r"\d+\.?\d*" # integer or float

    # find AUC score
    for i in range(repeat_times):
        marge_output_dir = os.path.join(user_path, 'marge_{}'.format(i))
        for upload_file in files:
            filename = os.path.basename(upload_file)
            filename, file_ext = os.path.splitext(filename)
            regression_score_file = os.path.join(marge_output_dir, 'margeoutput/regression/{}_target_regressionInfo.txt'.format(filename))

            if not os.path.exists(regression_score_file):
                err_msg += "File not exists: %s" % (regression_score_file)
                continue
            
            with open(regression_score_file, 'r') as fopen:
                for line in fopen:
                    if 'AUC = ' in line:
                        score = re.findall(pattern, line)[0]
                        auc_scores.append(float(score))
                        auc_files.append(marge_output_dir)

    # find max AUC score
    max_auc = max(auc_scores)
    max_index = -1
    for i in range(len(auc_scores)):
        if auc_scores[i] == max_auc:
            max_index = i
            break

    # find max AUC folder & change it to folder /marge_data
    if max_index == -1:
        err_msg += "Severe error in marge process!!\n"
    else:
        auc_file = auc_files[i]
        os.rename(auc_file, os.path.join(user_path, 'marge_data'))

    # if bart
    print (user_data)
    if bart_flag:
        exe_bart_geneset(user_data)

    print (err_msg)
Beispiel #5
0
def get_result():
    user_key = request.args['user_key']
    user_data = do_process.get_user_data(user_key)
    results = do_process.generate_results(user_data)
    return render_template('result_demonstration.html', results=results)