frag_pele/Growing/add_fragment_from_pdbs.py
def join_structures(core_bond, fragment_bond, core_structure, fragment_structure, pdb_complex,
                    pdb_fragment, chain_complex, chain_fragment, output_path, only_grow=False,
                    core_resnum=None):
    """
    It joins two ProDy structures into a single one, merging both bonds (core bond and fragment bond) creating a unique
    bond between the molecules. In order to do that this function performs a cross superimposition (in BioPython) of
    the whole fragment using as reference (fixed part) the atoms of the bond. Then, it transforms this BioPython object
    into ProDy molecule with the coordinates modified. Once we have all ready, the Hydrogens of the bonds will be
    deleted accordingly with the bond type, and both structures will be concatenated into a single ProDy object.

    :param core_bond: Bio.PDB.Atom list with two elements: [heavy atom, hydrogen atom]. These two atoms have to be the
    ones participating in the bond of the core that we would like to use as linking point between core and fragment.
    :param fragment_bond: Bio.PDB.Atom list with two elements: [hydrogen atom, heavy atom]. These two atoms have to be the
    ones participating in the bond of the fragment that we would like to use as linking point between fragment and core.
    :param list_of_atoms: Bio.PDB.Atom list with all the atoms of the fragment. These atoms have to contain
    coordinates.
    :param core_structure: ProDy molecule that contain only the core ligand.
    :param fragment_structure: ProDy molecule that contain only the fragment ligand.
    :param pdb_complex: path to the file in PDB with the protein-ligand complex.
    :param pdb_fragment: path to the file in PDB with the fragment.
    :param chain_complex: label of the ligand chain in the pdb_complex.
    :param chain_fragment: label of the ligand chain in the pdb_fragment.
    :return: ProDy molecule with the core_structure and the fragment_structure (with the coordinates modified)
    concatenated.
    """
    name_to_replace_core = core_bond[1].name
    name_to_replace_fragment = fragment_bond[0].name
    if only_grow:
        return 0, name_to_replace_core, name_to_replace_fragment
    if RDKIT:

        if core_resnum:
            atoms_to_delete_core = tree_detector.main(pdb_complex, (core_bond[0].name, name_to_replace_core),
                                                      chain=chain_complex, resnum=core_resnum)
        else:
            atoms_to_delete_core = tree_detector.main(pdb_complex, (core_bond[0].name, name_to_replace_core),
                                                      chain=chain_complex)
            
        atoms_to_delete_fragment = tree_detector.main(pdb_fragment, (fragment_bond[1].name, name_to_replace_fragment),
                                                      chain=chain_fragment)
    else:
        print("WARNING: YOU CAN NOT REPLACE HEAVY ATOMS FOR HYDROGENS WITHOUT RDKIT!")
    fragment_rdkit = rdkit.Chem.MolFromPDBFile(pdb_fragment, removeHs=False)
    bond_type = detect_bond_type(fragment_rdkit, fragment_bond[0], fragment_bond[1])
    print(bond_type)
    if RDKIT:
        if core_bond[1].element != "H":
            atom_replaced_idx = replace_heavy_by_hydrogen(core_bond[1], core_structure)
            new_coords, new_dist = correct_hydrogen_position(hydrogen_atom=core_structure[atom_replaced_idx],
                                                             atom_to_bond_with=core_bond[0],
                                                             structure=core_structure[atom_replaced_idx])
            core_structure[atom_replaced_idx].setCoords(new_coords)
            names_to_keep = list(
                set(core_structure.getNames()) ^ set(atoms_to_delete_core))  # Compare two sets and get the common items
            names_to_keep.remove(name_to_replace_core)
            core_structure = core_structure.select("name {}".format(" ".join(names_to_keep)))
            prody.writePDB(os.path.join(output_path, "{}.pdb".format(core_structure.getResnames()[0])),
                           core_structure)  # Overwrite the initial structure
            name_to_replace_core = core_structure[atom_replaced_idx].getName()
            core_bond[1].coord = new_coords
        if fragment_bond[0].element != "H":
            atom_replaced_idx = replace_heavy_by_hydrogen(fragment_bond[0], fragment_structure)
            new_coords, new_dist = correct_hydrogen_position(hydrogen_atom=fragment_structure[atom_replaced_idx],
                                                             atom_to_bond_with=fragment_bond[1],
                                                             structure=fragment_structure[atom_replaced_idx])
            fragment_structure[atom_replaced_idx].setCoords(new_coords)
            names_to_keep = list(
                set(fragment_structure.getNames()) ^ set(atoms_to_delete_fragment))  # Compare two sets and get the common items
            names_to_keep.remove(name_to_replace_fragment)
            fragment_structure = fragment_structure.select("name {}".format(" ".join(names_to_keep)))
            prody.writePDB(os.path.join(output_path, "{}.pdb".format(fragment_structure.getResnames()[0])),
                           fragment_structure)  # Overwrite the initial structure
            name_to_replace_fragment = fragment_structure[atom_replaced_idx].getName()
            fragment_bond[0].coord = new_coords
    bio_list = from_pdb_to_bioatomlist([os.path.join(output_path, "{}".format(fragment_structure.getResnames()[0]))])[0] # Its a list, so we keep only the unique element that is inside
    # Superimpose atoms of the fragment to the core bond
    pdb_joiner.superimpose(core_bond, fragment_bond, bio_list)
    # Get the new coords and change them in prody
    transform_coords_from_bio2prody(fragment_structure, bio_list)
    # Now, we have to remove the hydrogens of the binding
    h_atom_names = [name_to_replace_core, name_to_replace_fragment]
    # Correcting linking distance
    new_coords, new_distance = correct_bonding_distance(atom_reference=core_bond[0], atom_to_correct=fragment_bond[1],
                                                        reference_structure=core_structure, movil_structure=fragment_structure,
                                                        bond_type=bond_type)
    if bond_type == "double":
        hydrogen_to_delete = pdb_joiner.get_H_bonded_to_atom(core_bond[0].name, core_structure,
                                                             bond_dist=new_distance,
                                                 	     banned_hydrogen=name_to_replace_core,
                                                             chain=chain_complex)
        names_to_keep = list(core_structure.getNames())
        names_to_keep.remove(hydrogen_to_delete)
        core_structure = core_structure.select("name {}".format(" ".join(names_to_keep)))

    if bond_type == "triple":
        for n in range(2):
            hydrogen_to_delete = pdb_joiner.get_H_bonded_to_atom(core_bond[0].name, core_structure,
                                                                 bond_dist=new_distance,
                                                                 banned_hydrogen=name_to_replace_core,
                                                                 chain=chain_complex)
            names_to_keep = list(core_structure.getNames())
            names_to_keep.remove(hydrogen_to_delete)
            core_structure = core_structure.select("name {}".format(" ".join(names_to_keep)))

    fragment_structure.setCoords(new_coords)
    merged_structure = bond(h_atom_names, [core_structure, fragment_structure])
    return merged_structure, name_to_replace_core, name_to_replace_fragment, new_distance

    It joins two ProDy structures into a single one, merging both bonds (core bond and fragment bond) creating a unique
    bond between the molecules. In order to do that this function performs a cross superimposition (in BioPython) of
    the whole fragment using as reference (fixed part) the atoms of the bond. Then, it transforms this BioPython object
    into ProDy molecule with the coordinates modified. Once we have all ready, the Hydrogens of the bonds will be
    deleted accordingly with the bond type, and both structures will be concatenated into a single ProDy object.

    :param core_bond: Bio.PDB.Atom list with two elements: [heavy atom, hydrogen atom]. These two atoms have to be the
    ones participating in the bond of the core that we would like to use as linking point between core and fragment.
    :param fragment_bond: Bio.PDB.Atom list with two elements: [hydrogen atom, heavy atom]. These two atoms have to be the
    ones participating in the bond of the fragment that we would like to use as linking point between fragment and core.
    :param list_of_atoms: Bio.PDB.Atom list with all the atoms of the fragment. These atoms have to contain
    coordinates.
    :param core_structure: ProDy molecule that contain only the core ligand.
    :param fragment_structure: ProDy molecule that contain only the fragment ligand.
    :param pdb_complex: path to the file in PDB with the protein-ligand complex.
    :param pdb_fragment: path to the file in PDB with the fragment.
    :param chain_complex: label of the ligand chain in the pdb_complex.
    :param chain_fragment: label of the ligand chain in the pdb_fragment.
    :return: ProDy molecule with the core_structure and the fragment_structure (with the coordinates modified)
    concatenated.
    
It joins two ProDy structures into a single one, merging both bonds (core bond and fragment bond) creating a unique
bond between the molecules. In order to do that this function performs a cross superimposition (in BioPython) of
the whole fragment using as reference (fixed part) the atoms of the bond. Then, it transforms this BioPython object
into ProDy molecule with the coordinates modified. Once we have all ready, the Hydrogens of the bonds will be
deleted accordingly with the bond type, and both structures will be concatenated into a single ProDy object.
It joins two ProDy structures into a single one merging both bonds ( core bond and fragment bond ) creating a unique bond between the molecules . In order to do that this function performs a cross superimposition ( in BioPython ) of the whole fragment using as reference ( fixed part ) the atoms of the bond . Then it transforms this BioPython object into ProDy molecule with the coordinates modified . Once we have all ready the Hydrogens of the bonds will be deleted accordingly with the bond type and both structures will be concatenated into a single ProDy object .
def join_structures(core_bond, fragment_bond, core_structure, fragment_structure, pdb_complex,
                    pdb_fragment, chain_complex, chain_fragment, output_path, only_grow=False,
                    core_resnum=None):
    name_to_replace_core = core_bond[1].name
    name_to_replace_fragment = fragment_bond[0].name
    if only_grow:
        return 0, name_to_replace_core, name_to_replace_fragment
    if RDKIT:
        if core_resnum:
            atoms_to_delete_core = tree_detector.main(pdb_complex, (core_bond[0].name, name_to_replace_core),
                                                      chain=chain_complex, resnum=core_resnum)
        else:
            atoms_to_delete_core = tree_detector.main(pdb_complex, (core_bond[0].name, name_to_replace_core),
                                                      chain=chain_complex)
        atoms_to_delete_fragment = tree_detector.main(pdb_fragment, (fragment_bond[1].name, name_to_replace_fragment),
                                                      chain=chain_fragment)
    else:
        print("WARNING: YOU CAN NOT REPLACE HEAVY ATOMS FOR HYDROGENS WITHOUT RDKIT!")
    fragment_rdkit = rdkit.Chem.MolFromPDBFile(pdb_fragment, removeHs=False)
    bond_type = detect_bond_type(fragment_rdkit, fragment_bond[0], fragment_bond[1])
    print(bond_type)
    if RDKIT:
        if core_bond[1].element != "H":
            atom_replaced_idx = replace_heavy_by_hydrogen(core_bond[1], core_structure)
            new_coords, new_dist = correct_hydrogen_position(hydrogen_atom=core_structure[atom_replaced_idx],
                                                             atom_to_bond_with=core_bond[0],
                                                             structure=core_structure[atom_replaced_idx])
            core_structure[atom_replaced_idx].setCoords(new_coords)
            names_to_keep = list(
                set(core_structure.getNames()) ^ set(atoms_to_delete_core))  
            names_to_keep.remove(name_to_replace_core)
            core_structure = core_structure.select("name {}".format(" ".join(names_to_keep)))
            prody.writePDB(os.path.join(output_path, "{}.pdb".format(core_structure.getResnames()[0])),
                           core_structure)  
            name_to_replace_core = core_structure[atom_replaced_idx].getName()
            core_bond[1].coord = new_coords
        if fragment_bond[0].element != "H":
            atom_replaced_idx = replace_heavy_by_hydrogen(fragment_bond[0], fragment_structure)
            new_coords, new_dist = correct_hydrogen_position(hydrogen_atom=fragment_structure[atom_replaced_idx],
                                                             atom_to_bond_with=fragment_bond[1],
                                                             structure=fragment_structure[atom_replaced_idx])
            fragment_structure[atom_replaced_idx].setCoords(new_coords)
            names_to_keep = list(
                set(fragment_structure.getNames()) ^ set(atoms_to_delete_fragment))  
            names_to_keep.remove(name_to_replace_fragment)
            fragment_structure = fragment_structure.select("name {}".format(" ".join(names_to_keep)))
            prody.writePDB(os.path.join(output_path, "{}.pdb".format(fragment_structure.getResnames()[0])),
                           fragment_structure)  
            name_to_replace_fragment = fragment_structure[atom_replaced_idx].getName()
            fragment_bond[0].coord = new_coords
    bio_list = from_pdb_to_bioatomlist([os.path.join(output_path, "{}".format(fragment_structure.getResnames()[0]))])[0] 
    pdb_joiner.superimpose(core_bond, fragment_bond, bio_list)
    transform_coords_from_bio2prody(fragment_structure, bio_list)
    h_atom_names = [name_to_replace_core, name_to_replace_fragment]
    new_coords, new_distance = correct_bonding_distance(atom_reference=core_bond[0], atom_to_correct=fragment_bond[1],
                                                        reference_structure=core_structure, movil_structure=fragment_structure,
                                                        bond_type=bond_type)
    if bond_type == "double":
        hydrogen_to_delete = pdb_joiner.get_H_bonded_to_atom(core_bond[0].name, core_structure,
                                                             bond_dist=new_distance,
                                                 	     banned_hydrogen=name_to_replace_core,
                                                             chain=chain_complex)
        names_to_keep = list(core_structure.getNames())
        names_to_keep.remove(hydrogen_to_delete)
        core_structure = core_structure.select("name {}".format(" ".join(names_to_keep)))
    if bond_type == "triple":
        for n in range(2):
            hydrogen_to_delete = pdb_joiner.get_H_bonded_to_atom(core_bond[0].name, core_structure,
                                                                 bond_dist=new_distance,
                                                                 banned_hydrogen=name_to_replace_core,
                                                                 chain=chain_complex)
            names_to_keep = list(core_structure.getNames())
            names_to_keep.remove(hydrogen_to_delete)
            core_structure = core_structure.select("name {}".format(" ".join(names_to_keep)))
    fragment_structure.setCoords(new_coords)
    merged_structure = bond(h_atom_names, [core_structure, fragment_structure])
    return merged_structure, name_to_replace_core, name_to_replace_fragment, new_distance
def join_structures ( core_bond , fragment_bond , core_structure , fragment_structure , pdb_complex , pdb_fragment , chain_complex , chain_fragment , output_path , only_grow = False , core_resnum = None ) : name_to_replace_core = core_bond [ 1 ] . name name_to_replace_fragment = fragment_bond [ 0 ] . name if only_grow : return 0 , name_to_replace_core , name_to_replace_fragment if RDKIT : if core_resnum : atoms_to_delete_core = tree_detector . main ( pdb_complex , ( core_bond [ 0 ] . name , name_to_replace_core ) , chain = chain_complex , resnum = core_resnum ) else : atoms_to_delete_core = tree_detector . main ( pdb_complex , ( core_bond [ 0 ] . name , name_to_replace_core ) , chain = chain_complex ) atoms_to_delete_fragment = tree_detector . main ( pdb_fragment , ( fragment_bond [ 1 ] . name , name_to_replace_fragment ) , chain = chain_fragment ) else : print ( "WARNING: YOU CAN NOT REPLACE HEAVY ATOMS FOR HYDROGENS WITHOUT RDKIT!" ) fragment_rdkit = rdkit . Chem . MolFromPDBFile ( pdb_fragment , removeHs = False ) bond_type = detect_bond_type ( fragment_rdkit , fragment_bond [ 0 ] , fragment_bond [ 1 ] ) print ( bond_type ) if RDKIT : if core_bond [ 1 ] . element != "H" : atom_replaced_idx = replace_heavy_by_hydrogen ( core_bond [ 1 ] , core_structure ) new_coords , new_dist = correct_hydrogen_position ( hydrogen_atom = core_structure [ atom_replaced_idx ] , atom_to_bond_with = core_bond [ 0 ] , structure = core_structure [ atom_replaced_idx ] ) core_structure [ atom_replaced_idx ] . setCoords ( new_coords ) names_to_keep = list ( set ( core_structure . getNames ( ) ) ^ set ( atoms_to_delete_core ) ) names_to_keep . remove ( name_to_replace_core ) core_structure = core_structure . select ( "name {}" . format ( " " . join ( names_to_keep ) ) ) prody . writePDB ( os . path . join ( output_path , "{}.pdb" . format ( core_structure . getResnames ( ) [ 0 ] ) ) , core_structure ) name_to_replace_core = core_structure [ atom_replaced_idx ] . getName ( ) core_bond [ 1 ] . coord = new_coords if fragment_bond [ 0 ] . element != "H" : atom_replaced_idx = replace_heavy_by_hydrogen ( fragment_bond [ 0 ] , fragment_structure ) new_coords , new_dist = correct_hydrogen_position ( hydrogen_atom = fragment_structure [ atom_replaced_idx ] , atom_to_bond_with = fragment_bond [ 1 ] , structure = fragment_structure [ atom_replaced_idx ] ) fragment_structure [ atom_replaced_idx ] . setCoords ( new_coords ) names_to_keep = list ( set ( fragment_structure . getNames ( ) ) ^ set ( atoms_to_delete_fragment ) ) names_to_keep . remove ( name_to_replace_fragment ) fragment_structure = fragment_structure . select ( "name {}" . format ( " " . join ( names_to_keep ) ) ) prody . writePDB ( os . path . join ( output_path , "{}.pdb" . format ( fragment_structure . getResnames ( ) [ 0 ] ) ) , fragment_structure ) name_to_replace_fragment = fragment_structure [ atom_replaced_idx ] . getName ( ) fragment_bond [ 0 ] . coord = new_coords bio_list = from_pdb_to_bioatomlist ( [ os . path . join ( output_path , "{}" . format ( fragment_structure . getResnames ( ) [ 0 ] ) ) ] ) [ 0 ] pdb_joiner . superimpose ( core_bond , fragment_bond , bio_list ) transform_coords_from_bio2prody ( fragment_structure , bio_list ) h_atom_names = [ name_to_replace_core , name_to_replace_fragment ] new_coords , new_distance = correct_bonding_distance ( atom_reference = core_bond [ 0 ] , atom_to_correct = fragment_bond [ 1 ] , reference_structure = core_structure , movil_structure = fragment_structure , bond_type = bond_type ) if bond_type == "double" : hydrogen_to_delete = pdb_joiner . get_H_bonded_to_atom ( core_bond [ 0 ] . name , core_structure , bond_dist = new_distance , banned_hydrogen = name_to_replace_core , chain = chain_complex ) names_to_keep = list ( core_structure . getNames ( ) ) names_to_keep . remove ( hydrogen_to_delete ) core_structure = core_structure . select ( "name {}" . format ( " " . join ( names_to_keep ) ) ) if bond_type == "triple" : for n in range ( 2 ) : hydrogen_to_delete = pdb_joiner . get_H_bonded_to_atom ( core_bond [ 0 ] . name , core_structure , bond_dist = new_distance , banned_hydrogen = name_to_replace_core , chain = chain_complex ) names_to_keep = list ( core_structure . getNames ( ) ) names_to_keep . remove ( hydrogen_to_delete ) core_structure = core_structure . select ( "name {}" . format ( " " . join ( names_to_keep ) ) ) fragment_structure . setCoords ( new_coords ) merged_structure = bond ( h_atom_names , [ core_structure , fragment_structure ] ) return merged_structure , name_to_replace_core , name_to_replace_fragment , new_distance
It joins two ProDy structures into a single one, merging both bonds (core bond and fragment bond) creating a unique
bond between the molecules.
It joins two ProDy structures into a single one merging both bonds ( core bond and fragment bond ) creating a unique bond between the molecules .
"""
    It joins two ProDy structures into a single one, merging both bonds (core bond and fragment bond) creating a unique
    bond between the molecules. In order to do that this function performs a cross superimposition (in BioPython) of
    the whole fragment using as reference (fixed part) the atoms of the bond. Then, it transforms this BioPython object
    into ProDy molecule with the coordinates modified. Once we have all ready, the Hydrogens of the bonds will be
    deleted accordingly with the bond type, and both structures will be concatenated into a single ProDy object.

    :param core_bond: Bio.PDB.Atom list with two elements: [heavy atom, hydrogen atom]. These two atoms have to be the
    ones participating in the bond of the core that we would like to use as linking point between core and fragment.
    :param fragment_bond: Bio.PDB.Atom list with two elements: [hydrogen atom, heavy atom]. These two atoms have to be the
    ones participating in the bond of the fragment that we would like to use as linking point between fragment and core.
    :param list_of_atoms: Bio.PDB.Atom list with all the atoms of the fragment. These atoms have to contain
    coordinates.
    :param core_structure: ProDy molecule that contain only the core ligand.
    :param fragment_structure: ProDy molecule that contain only the fragment ligand.
    :param pdb_complex: path to the file in PDB with the protein-ligand complex.
    :param pdb_fragment: path to the file in PDB with the fragment.
    :param chain_complex: label of the ligand chain in the pdb_complex.
    :param chain_fragment: label of the ligand chain in the pdb_fragment.
    :return: ProDy molecule with the core_structure and the fragment_structure (with the coordinates modified)
    concatenated.
    """ # Compare two sets and get the common items # Overwrite the initial structure # Compare two sets and get the common items # Overwrite the initial structure # Its a list, so we keep only the unique element that is inside # Superimpose atoms of the fragment to the core bond # Get the new coords and change them in prody # Now, we have to remove the hydrogens of the binding # Correcting linking distance
ProDy molecule with the core_structure and the fragment_structure (with the coordinates modified)
concatenated.
ProDy molecule with the core_structure and the fragment_structure ( with the coordinates modified ) concatenated .
core_bond
Bio.PDB.Atom list with two elements: [heavy atom, hydrogen atom]. These two atoms have to be the
ones participating in the bond of the core that we would like to use as linking point between core and fragment.
Bio . PDB . Atom list with two elements : [ heavy atom hydrogen atom ] . These two atoms have to be the ones participating in the bond of the core that we would like to use as linking point between core and fragment .
fragment_bond
Bio.PDB.Atom list with two elements: [hydrogen atom, heavy atom]. These two atoms have to be the
ones participating in the bond of the fragment that we would like to use as linking point between fragment and core.
Bio . PDB . Atom list with two elements : [ hydrogen atom heavy atom ] . These two atoms have to be the ones participating in the bond of the fragment that we would like to use as linking point between fragment and core .
core_structure
ProDy molecule that contain only the core ligand.
ProDy molecule that contain only the core ligand .
fragment_structure
ProDy molecule that contain only the fragment ligand.
ProDy molecule that contain only the fragment ligand .
pdb_complex
path to the file in PDB with the protein-ligand complex.
path to the file in PDB with the protein - ligand complex .
pdb_fragment
path to the file in PDB with the fragment.
path to the file in PDB with the fragment .
chain_complex
label of the ligand chain in the pdb_complex.
label of the ligand chain in the pdb_complex .
chain_fragment
label of the ligand chain in the pdb_fragment.
label of the ligand chain in the pdb_fragment .
output_path
only_grow
core_resnum
list_of_atoms
Bio.PDB.Atom list with all the atoms of the fragment. These atoms have to contain
coordinates.
Bio . PDB . Atom list with all the atoms of the fragment . These atoms have to contain coordinates .