|
| 1 | +# pylint: disable=no-member |
| 2 | +import sys |
| 3 | +import os |
| 4 | +import argparse |
| 5 | + |
| 6 | +import MDAnalysis as mda |
| 7 | + |
| 8 | + |
| 9 | +def parse_arguments() -> argparse.Namespace: |
| 10 | + """ This function parses the arguments. |
| 11 | +
|
| 12 | + Returns: |
| 13 | + argparse.Namespace: The command line arguments |
| 14 | + """ |
| 15 | + parser = argparse.ArgumentParser() |
| 16 | + parser.add_argument('--input_pdb_path', type=str) |
| 17 | + parser.add_argument('--output_pdb_path', type=str) |
| 18 | + parser.add_argument('--output_pdb_ligand_path', type=str) |
| 19 | + args = parser.parse_args() |
| 20 | + return args |
| 21 | + |
| 22 | + |
| 23 | +def extract_ligand_protein(input_pdb_path: str, output_pdb_path: str, output_pdb_ligand_path: str) -> None: |
| 24 | + """ Extract ligand & protein from the PDB file |
| 25 | +
|
| 26 | + Args: |
| 27 | + input_pdb_path (str): The path to the input pdb file |
| 28 | + output_pdb_path (str): The path to the output pdb file |
| 29 | + output_pdb_ligand_path (str): The path to the output pdb ligand file |
| 30 | + """ |
| 31 | + |
| 32 | + # Load the PDB file |
| 33 | + u = mda.Universe(input_pdb_path) |
| 34 | + |
| 35 | + # Get unique residue names |
| 36 | + protein_atoms = u.select_atoms('protein') # use simple atom selection when possible |
| 37 | + |
| 38 | + # Create a new Universe with only protein atoms |
| 39 | + protein_u = mda.Universe.empty(n_atoms=protein_atoms.n_atoms, trajectory=True) # needed for coordinates |
| 40 | + protein_u.atoms = protein_atoms |
| 41 | + |
| 42 | + # duplicate the universe object |
| 43 | + dup_u = mda.Universe(input_pdb_path) |
| 44 | + |
| 45 | + # now do the same for the ligand, not protein and not water or salts |
| 46 | + ligand_atoms = u.select_atoms('not protein') |
| 47 | + |
| 48 | + try: |
| 49 | + # guess the bonds, since input PDB may not have bonds |
| 50 | + dup_u.atoms.guess_bonds() |
| 51 | + except ValueError: |
| 52 | + # ValueError: vdw radii for types: AS. These can be defined manually using the keyword 'vdwradii' |
| 53 | + print('Error: Could not guess bonds. Check the input PDB file.') |
| 54 | + |
| 55 | + has_bonds = False |
| 56 | + try: |
| 57 | + num_bonds = len(dup_u.atoms.bonds) |
| 58 | + has_bonds = True |
| 59 | + except mda.exceptions.NoDataError: |
| 60 | + print('No bonds found in the PDB file.') |
| 61 | + |
| 62 | + # Identify water molecules based on the connectivity pattern (Oxygen bonded to two Hydrogens) |
| 63 | + if has_bonds: |
| 64 | + water_indices = set() |
| 65 | + for atom in dup_u.atoms: # dont use selection resname == 'HOH', pdb file may have different water residue names |
| 66 | + if atom.name == 'O' and len(atom.bonds) == 2: # if hydrogens are added |
| 67 | + bonded_atoms_names = set([a.name for a in atom.bonded_atoms]) |
| 68 | + if bonded_atoms_names == {'H'}: # Check if both bonds are Hydrogens |
| 69 | + water_indices.add(atom.index) |
| 70 | + water_indices.update([a.index for a in atom.bonded_atoms]) |
| 71 | + |
| 72 | + # now want to remove all salts, waters without H |
| 73 | + non_bonded = set() |
| 74 | + for atom in dup_u.atoms: |
| 75 | + if len(atom.bonds) == 0: |
| 76 | + non_bonded.add(atom.index) |
| 77 | + |
| 78 | + # Remove water by excluding the water indices |
| 79 | + if len(water_indices) > 0: |
| 80 | + water_indices_string = ' '.join([str(i) for i in water_indices]) |
| 81 | + ligand_atoms = ligand_atoms.select_atoms(f'not index {water_indices_string}') |
| 82 | + |
| 83 | + # Remove non bonded atoms |
| 84 | + if len(non_bonded) > 0: |
| 85 | + non_bonded_string = ' '.join([str(i) for i in non_bonded]) |
| 86 | + ligand_atoms = ligand_atoms.select_atoms(f'not index {non_bonded_string}') |
| 87 | + |
| 88 | + ligand_u = mda.Universe.empty(n_atoms=ligand_atoms.n_atoms, trajectory=True) # needed for coordinates |
| 89 | + ligand_u.atoms = ligand_atoms |
| 90 | + |
| 91 | + with open(output_pdb_path, mode="w", encoding='utf-8') as wfile: |
| 92 | + protein_u.atoms.write(output_pdb_path) |
| 93 | + if len(ligand_u.atoms) > 0: # will crash if no ligand atoms |
| 94 | + with open(output_pdb_ligand_path, mode="w", encoding='utf-8') as wfile: |
| 95 | + ligand_u.atoms.write(output_pdb_ligand_path) |
| 96 | + |
| 97 | + |
| 98 | +def main() -> None: |
| 99 | + """ Reads the command line arguments and extract protein from the PDB file |
| 100 | + """ |
| 101 | + args = parse_arguments() |
| 102 | + |
| 103 | + if not os.path.exists(args.input_pdb_path): |
| 104 | + print(f'Error: Can not find file {args.input_pdb_path}') |
| 105 | + sys.exit(1) |
| 106 | + |
| 107 | + extract_ligand_protein(args.input_pdb_path, args.output_pdb_path, args.output_pdb_ligand_path) |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == '__main__': |
| 111 | + main() |
0 commit comments