def run_biojava_alignment(cmd_class,
                          code1,
                          code2,
                          alignment,
                          transformed,
                          superposed,
                          pdb1=None,
                          pdb2=None,
                          log=None):
    try:
        exec_dir = tempfile.mkdtemp()

        pdbid1, chain1 = extract_ident(code1)
        pdbid2, chain2 = extract_ident(code2)

        pdb1 = pdb1 or '{0}.pdb'.format(pdbid1)
        pdb2 = pdb2 or '{0}.pdb'.format(pdbid2)

        # Setup command based on parameters
        cmd = _biojava_base_cmd.bake(cmd_class, "-pdb1", code1, "-pdb2", code2,
                                     "-pdbFilePath", exec_dir,
                                     *BIOJAVA_COMMON_ARGS)

        # BioJava Structure is very picky about names
        for pdb, code in [(pdb1, pdbid1), (pdb2, pdbid2)]:
            entfile = "pdb{0}.ent".format(code.lower())
            os.symlink(pdb, os.path.join(exec_dir, entfile))

        logging.info("Running biojava: {0!s} in {1}".format(cmd, exec_dir))
        alignment_result = cmd(_cwd=exec_dir, _err=log)

        with open(alignment, 'w') as f:
            f.write(str(alignment_result.stdout))

        superposed_pdb = os.path.join(exec_dir, ALIGNMENT_TEMP_PDB)

        pdb2_transformed = get_pdb_selection(
            pdbid2,
            chain=chain2,
            model=1,
            root=superposed_pdb,
        )

        shutil.move(os.path.join(exec_dir, ALIGNMENT_TEMP_PDB), superposed)
        shutil.move(pdb2_transformed, transformed)
    except Exception as e:
        raise
    else:
        shutil.rmtree(exec_dir)
def get_charged_pdb(code,
                    chain=None,
                    model=0,
                    alignment_id=None,
                    force=False,
                    log=None):
    selection_path = get_pdb_selection(code,
                                       chain=chain,
                                       model=model,
                                       alignment_id=alignment_id)
    base, ext = split_ext_gz(selection_path)
    protonated = base + '.H'
    if force or not os.path.exists(protonated):
        run_reduce(selection_path, protonated, log=log)
    return protonated
Exemplo n.º 3
0
def create_context_from_pdb_code(code, chain):
    pdb = get_pdb_selection(code, chain)
    params = ResidueContextParams(source=pdb, chain=chain, name=code)
    builder = ContextBuilder(params)
    context = builder.run()
    return context
Exemplo n.º 4
0
def run_residuecontext_alignment(code1,
                                 code2,
                                 alignment,
                                 translation,
                                 transformed,
                                 pdb1=None,
                                 pdb2=None,
                                 log=None):
    """
    :param cmd:
    :param code1:
    :param code2:
    :param alignment:
    :param transformed:
    :param superposed:
    :param code1:
    :param code2:
    :param log:
    :return:
    """

    try:
        exec_dir = tempfile.mkdtemp()
        working_dir = os.path.join(exec_dir, 'working')
        os.mkdir(working_dir)

        ident1 = code1.split()
        ident2 = code2.split()

        pdbid1 = ident1[0]
        pdbid2 = ident2[0]

        if len(ident1) > 1:
            chain1 = ident1[1]
        else:
            chain1 = 'A'

        if len(ident2) > 1:
            chain2 = ident2[1]
        else:
            chain2 = 'A'

        # Setup command based on parameters
        cmd = _residue_context_cmd.bake(pdbid1.upper(), pdbid2.upper(), chain1,
                                        chain2, "working",
                                        *RESIDUE_CONTEXT_COMMON_ARGS)

        # ResidueContext is picky about names
        # And also requires all PDB record types to be 6 characters
        # But biopython writes them out without padding
        for (pdb, code) in [(pdb1, ident1[0]), (pdb2, ident2[0])]:
            entfile = "{}.pdb".format(code.upper())
            with open(pdb) as src, \
                    open(os.path.join(working_dir, entfile), 'w') as dst:
                for line in src:
                    if line == 'END\n':
                        dst.write('END   \n')
                    elif line == 'TER\n':
                        dst.write('TER   \n')
                    else:
                        dst.write(line)

        logging.info("Running residue context: {0!s} in {1}".format(
            cmd, exec_dir))
        cmd(_cwd=exec_dir, _err=log, _env=RESIDUE_CONTEXT_ENV)

        pdb2_transformed = get_pdb_selection(
            pdbid2.upper(),
            chain=chain2,
            root=working_dir,
        )

        shutil.move(os.path.join(exec_dir, ALIGNMENT_OUTPUT), alignment)
        shutil.move(
            os.path.join(working_dir,
                         "{0}.pdb-transform.txt".format(ident2[0].upper())),
            translation)
        shutil.move(pdb2_transformed, transformed)
    except Exception as e:
        raise
    else:
        shutil.rmtree(exec_dir)
Exemplo n.º 5
0
def run_alignment_comparisons(identifier, code1, code2):
    print("starting...")
    working_dir = werkzeug.security.safe_join(ALIGNMENT_JOB_DIR,
                                              str(identifier))
    progress_file = os.path.join(working_dir, 'status')

    if os.path.exists(working_dir):
        shutil.rmtree(working_dir)

    os.mkdir(working_dir)
    with open(progress_file, 'w') as f:
        print("running", file=f)

    pdbid1, chain1 = extract_ident(code1)
    pdbid2, chain2 = extract_ident(code2)

    with open(os.path.join(working_dir, ALIGNMENT_DATA_FILE), 'w') as f:
        json.dump(
            {
                'structure1': {
                    'ident': code1,
                    'pdb': pdbid1,
                    'chain': chain1
                },
                'structure2': {
                    'ident': code2,
                    'pdb': pdbid2,
                    'chain': chain2
                }
            }, f)

    pdb1src = get_pdb_selection(pdbid1, chain=chain1)
    pdb2src = get_pdb_selection(pdbid2, chain=chain2)

    pdb1 = os.path.join(working_dir, "{0}.pdb".format(pdbid1))
    pdb2 = os.path.join(working_dir, "{0}.pdb".format(pdbid2))

    shutil.copy(pdb1src, pdb1)
    shutil.copy(pdb2src, pdb2)

    rcontext_dir = os.path.join(working_dir, "rcontext")
    ce_dir = os.path.join(working_dir, "ce")
    fatcat_dir = os.path.join(working_dir, "fatcat")
    dali_dir = os.path.join(working_dir, "dali")

    alignment_basename = 'alignment.txt'
    translated_basename = 'transformed-{0}.pdb'.format(pdbid2)
    superposed_basename = 'superposed.pdb'
    transform_basename = '{0}.pdb-transform.txt'.format(pdbid2)

    biojava_code1 = ident_to_biojava(pdbid1, chain1)
    biojava_code2 = ident_to_biojava(pdbid2, chain2)

    os.mkdir(ce_dir)
    run_biojava_alignment(BIOJAVA_CE_CLASS,
                          biojava_code1,
                          biojava_code2,
                          alignment=os.path.join(ce_dir, alignment_basename),
                          transformed=os.path.join(ce_dir,
                                                   translated_basename),
                          superposed=os.path.join(ce_dir, superposed_basename),
                          pdb1=pdb1,
                          pdb2=pdb2)
    os.symlink(pdb1, os.path.join(ce_dir, "{0}.pdb".format(pdbid1)))
    os.symlink(pdb2, os.path.join(ce_dir, "{0}.pdb".format(pdbid2)))
    phi = get_electrostatics_grid(pdbid1, chain=chain1, alignment_id=ce_dir)
    get_vanderderwaals_grids(pdbid1,
                             chain=chain1,
                             alignment_id=ce_dir,
                             box=phi,
                             scale=1 / phi.scale)
    phi = get_electrostatics_grid(pdbid2, chain=chain2, alignment_id=ce_dir)
    get_vanderderwaals_grids(pdbid2,
                             chain=chain2,
                             alignment_id=ce_dir,
                             box=phi,
                             scale=1 / phi.scale)

    os.mkdir(fatcat_dir)
    run_biojava_alignment(BIOJAVA_FATCAT_CLASS,
                          biojava_code1,
                          biojava_code2,
                          alignment=os.path.join(fatcat_dir,
                                                 alignment_basename),
                          transformed=os.path.join(fatcat_dir,
                                                   translated_basename),
                          superposed=os.path.join(fatcat_dir,
                                                  superposed_basename),
                          pdb1=pdb1,
                          pdb2=pdb2)
    os.symlink(pdb1, os.path.join(fatcat_dir, "{0}.pdb".format(pdbid1)))
    os.symlink(pdb2, os.path.join(fatcat_dir, "{0}.pdb".format(pdbid2)))
    phi = get_electrostatics_grid(pdbid1,
                                  chain=chain1,
                                  alignment_id=fatcat_dir)
    get_vanderderwaals_grids(pdbid1,
                             chain=chain1,
                             alignment_id=fatcat_dir,
                             box=phi,
                             scale=1 / phi.scale)
    phi = get_electrostatics_grid(pdbid2,
                                  chain=chain2,
                                  alignment_id=fatcat_dir)
    get_vanderderwaals_grids(pdbid2,
                             chain=chain2,
                             alignment_id=fatcat_dir,
                             box=phi,
                             scale=1 / phi.scale)

    os.mkdir(rcontext_dir)
    run_residuecontext_alignment(
        ident_to_rcontext(pdbid1, chain1),
        ident_to_rcontext(pdbid2, chain2),
        alignment=os.path.join(rcontext_dir, alignment_basename),
        transformed=os.path.join(rcontext_dir, translated_basename),
        translation=os.path.join(rcontext_dir, transform_basename),
        pdb1=pdb1,
        pdb2=pdb2)
    os.symlink(pdb1, os.path.join(rcontext_dir, "{0}.pdb".format(pdbid1)))
    os.symlink(pdb2, os.path.join(rcontext_dir, "{0}.pdb".format(pdbid2)))
    phi = get_electrostatics_grid(pdbid1,
                                  chain=chain1,
                                  alignment_id=rcontext_dir)
    get_vanderderwaals_grids(pdbid1,
                             chain=chain1,
                             alignment_id=rcontext_dir,
                             box=phi,
                             scale=1 / phi.scale)
    phi = get_electrostatics_grid(pdbid2,
                                  chain=chain2,
                                  alignment_id=rcontext_dir)
    get_vanderderwaals_grids(pdbid2,
                             chain=chain2,
                             alignment_id=rcontext_dir,
                             box=phi,
                             scale=1 / phi.scale)

    os.mkdir(dali_dir)
    run_dalilite_alignment(ident_to_rcontext(pdbid1, chain1),
                           ident_to_rcontext(pdbid2, chain2),
                           alignment=os.path.join(dali_dir,
                                                  alignment_basename),
                           translation=os.path.join(dali_dir,
                                                    transform_basename),
                           transformed=os.path.join(dali_dir,
                                                    translated_basename),
                           pdb1=pdb1,
                           pdb2=pdb2)
    os.symlink(pdb1, os.path.join(dali_dir, "{0}.pdb".format(pdbid1)))
    os.symlink(pdb2, os.path.join(dali_dir, "{0}.pdb".format(pdbid2)))
    phi = get_electrostatics_grid(pdbid1, chain=chain1, alignment_id=dali_dir)
    get_vanderderwaals_grids(pdbid1,
                             chain=chain1,
                             alignment_id=dali_dir,
                             box=phi,
                             scale=1 / phi.scale)
    phi = get_electrostatics_grid(pdbid2, chain=chain2, alignment_id=dali_dir)
    get_vanderderwaals_grids(pdbid2,
                             chain=chain2,
                             alignment_id=dali_dir,
                             box=phi,
                             scale=1 / phi.scale)

    with open(progress_file, 'w') as f:
        print("done", file=f)
    print("Finished.")