def remove_pdbs_only(pwd, name, time):
    pdb_files = [a for a in os.listdir(pwd) if a[-7:] == '.pdb.gz']
    run_filters = DCRP.generate_run_filters(
        args={
            'ddg': 24.0,
            'sasa': 1400,
            'shape': 0.6,
            'packstat': 0.6,
            'buried_2': 3,
            'hbonds': -10.
        })
    score_dict = RF.score2dict('%sall_%s_%s.score' % (pwd, name, time))
    passed, failed = DCRP.all_who_pass_run_filters({}, score_dict, run_filters)
    if len(passed) != 0:
        print('there are %i passed scores, so choosing from there' %
              len(list(passed.keys())))
        best_structs = DCRP.best_n_structures(
            {
                'filter': 'ddg',
                'n': min([10, len(list(passed.keys()))])
            }, passed)
    else:
        print('there are no passed, so choosing from the failed')
        best_structs = DCRP.best_n_structures(
            {
                'filter': 'ddg',
                'n': min([5, len(list(failed.keys()))])
            }, failed)
    best_names = [a['description'] for a in best_structs]
    [os.remove(pdb) for pdb in pdb_files if pdb[:-7] not in best_names]
def process_folder(args):
    import time
    pwd = os.getcwd()+'/'
    os.chdir(args['folder'])

    name = pwd.split('/')[-2]
    time = time.strftime("%d.%0-m")

    if not args['force_process']:
        if os.path.isfile('%sall_%s_%s.err' % (pwd, name, time)):
            print('found %sall_%s_%s.err, STOPPING' % (pwd, name, time))
            if args['remove_pdbs']:
                remove_pdbs_only(pwd, name, time)
                sys.exit()
            return 'not finished'
        if not is_folder_finished(pwd):
            return 'not finished'
    sc_files = [a for a in os.listdir(pwd) if a[-3:] == '.sc']
    pdb_files = [a for a in os.listdir(pwd) if a[-7:] == '.pdb.gz']
    err_files = [a for a in os.listdir(pwd) if a[:4] == 'err.']
    job_files = [a for a in os.listdir(pwd) if a[:4] == 'job.']
    print('found a total of %i job files, %i err files, %i pdbs and %i scores' % (len(job_files), len(err_files),
                                                                                  len(pdb_files), len(sc_files)))
    if len(sc_files) == 0:
        return 'no scores'
    combine_scores('%sall_%s_%s.score' % (pwd, name, time), sc_files)
    non_triv_errs = process_errors('%sall_%s_%s.err' % (pwd, name, time), err_files)

    if non_triv_errs == 0:
        print('removing out.* and job.*')
        [os.remove(out) for out in os.listdir(pwd) if out[:4] == 'out.']
        [os.remove(job) for job in job_files]
        try:
            os.remove('./command')
        except:
            pass

    run_filters = DCRP.generate_run_filters(args={'ddg': 24.0, 'sasa': 1400, 'shape': 0.6, 'packstat': 0.6,
                                                  'buried_2': 3, 'hbonds': -10.})
    score_dict = RF.score2dict('%sall_%s_%s.score' % (pwd, name, time))
    passed, failed = DCRP.all_who_pass_run_filters({}, score_dict, run_filters)
    if len(passed) != 0:
        print('there are %i passed scores, so choosing from there' % len(list(passed.keys())))
        best_structs = DCRP.best_n_structures({'filter': 'ddg', 'n': min([10, len(list(passed.keys()))])}, passed)
    else:
        print('there are no passed, so choosing from the failed')
        best_structs = DCRP.best_n_structures({'filter': 'ddg', 'n': min([10, len(list(failed.keys()))])}, failed)
    best_names = [a['description'] for a in best_structs]
    print('the best:', best_names)
    print('removing all other pdbs')
    [os.remove(pdb) for pdb in pdb_files if pdb[:-7] not in best_names]
    os.chdir(pwd)
def remove_pdbs_only(pwd, name, time):
    pdb_files = [a for a in os.listdir(pwd) if a[-7:] == '.pdb.gz']
    run_filters = DCRP.generate_run_filters(args={'ddg': 24.0, 'sasa': 1400, 'shape': 0.6, 'packstat': 0.6,
                                                  'buried_2': 3, 'hbonds': -10.})
    score_dict = RF.score2dict('%sall_%s_%s.score' % (pwd, name, time))
    passed, failed = DCRP.all_who_pass_run_filters({}, score_dict, run_filters)
    if len(passed) != 0:
        print('there are %i passed scores, so choosing from there' % len(list(passed.keys())))
        best_structs = DCRP.best_n_structures({'filter': 'ddg', 'n': min([10, len(list(passed.keys()))])}, passed)
    else:
        print('there are no passed, so choosing from the failed')
        best_structs = DCRP.best_n_structures({'filter': 'ddg', 'n': min([5, len(list(failed.keys()))])}, failed)
    best_names = [a['description'] for a in best_structs]
    [os.remove(pdb) for pdb in pdb_files if pdb[:-7] not in best_names]
def process_folder(args):
    import time
    pwd = os.getcwd() + '/'
    os.chdir(args['folder'])

    name = pwd.split('/')[-2]
    time = time.strftime("%d.%0-m")

    if not args['force_process']:
        if os.path.isfile('%sall_%s_%s.err' % (pwd, name, time)):
            print('found %sall_%s_%s.err, STOPPING' % (pwd, name, time))
            if args['remove_pdbs']:
                remove_pdbs_only(pwd, name, time)
                sys.exit()
            return 'not finished'
        if not is_folder_finished(pwd):
            return 'not finished'
    sc_files = [a for a in os.listdir(pwd) if a[-3:] == '.sc']
    pdb_files = [a for a in os.listdir(pwd) if a[-7:] == '.pdb.gz']
    err_files = [a for a in os.listdir(pwd) if a[:4] == 'err.']
    job_files = [a for a in os.listdir(pwd) if a[:4] == 'job.']
    print(
        'found a total of %i job files, %i err files, %i pdbs and %i scores' %
        (len(job_files), len(err_files), len(pdb_files), len(sc_files)))
    if len(sc_files) == 0:
        return 'no scores'
    combine_scores('%sall_%s_%s.score' % (pwd, name, time), sc_files)
    non_triv_errs = process_errors('%sall_%s_%s.err' % (pwd, name, time),
                                   err_files)

    if non_triv_errs == 0:
        print('removing out.* and job.*')
        [os.remove(out) for out in os.listdir(pwd) if out[:4] == 'out.']
        [os.remove(job) for job in job_files]
        try:
            os.remove('./command')
        except:
            pass

    run_filters = DCRP.generate_run_filters(
        args={
            'ddg': 24.0,
            'sasa': 1400,
            'shape': 0.6,
            'packstat': 0.6,
            'buried_2': 3,
            'hbonds': -10.
        })
    score_dict = RF.score2dict('%sall_%s_%s.score' % (pwd, name, time))
    passed, failed = DCRP.all_who_pass_run_filters({}, score_dict, run_filters)
    if len(passed) != 0:
        print('there are %i passed scores, so choosing from there' %
              len(list(passed.keys())))
        best_structs = DCRP.best_n_structures(
            {
                'filter': 'ddg',
                'n': min([10, len(list(passed.keys()))])
            }, passed)
    else:
        print('there are no passed, so choosing from the failed')
        best_structs = DCRP.best_n_structures(
            {
                'filter': 'ddg',
                'n': min([10, len(list(failed.keys()))])
            }, failed)
    best_names = [a['description'] for a in best_structs]
    print('the best:', best_names)
    print('removing all other pdbs')
    [os.remove(pdb) for pdb in pdb_files if pdb[:-7] not in best_names]
    os.chdir(pwd)