Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

foldx Buildmodel mutation interfact - convenience interface #374

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/biotite/application/foldx/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This source code is part of the Biotite package and is distributed
# under the 3-Clause BSD License. Please see 'LICENSE.rst' for further
# information.

"""
A subpackage for static ligand docking with *FoldX*.
"""

__name__ = "biotite.application.foldX"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
__name__ = "biotite.application.foldX"
__name__ = "biotite.application.foldx"

__author__ = "Mojmir Mutny"

from .app import *
190 changes: 190 additions & 0 deletions src/biotite/application/foldx/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
# This source code is part of the Biotite package and is distributed
# under the 3-Clause BSD License. Please see 'LICENSE.rst' for further
# information.

__name__ = "biotite.application.foldX"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
__name__ = "biotite.application.foldX"
__name__ = "biotite.application.foldx"

__author__ = "Mojmir Mutny"
__all__ = ["FoldXApp"]

import copy
from tempfile import NamedTemporaryFile
import numpy as np
import os
from os import chdir, getcwd, remove
from ..localapp import LocalApp, cleanup_tempfile
from ..application import AppState, requires_state
from ...structure.io.pdbqt import PDBQTFile
from ...structure.io.pdb import PDBFile
from ...structure.io.pdbx import PDBxFile
from ...structure.residues import get_residue_starts_for, get_residue_masks
from ...structure.bonds import find_connected
from ...structure.error import BadStructureError
Comment on lines +9 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of these imports are actually unused. Please remove the unused imports, when you finished your work on the PR.



class FoldXApp(LocalApp):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I read so far, FoldX has multiple subcommands, and the one used here is called BuildModel. Hence, i propose the following:

Suggested change
class FoldXApp(LocalApp):
class BuildModelApp(LocalApp):

This is consistent with e.g. the viennarna subpackage, where the classes are called RNAfoldApp and RNAplotApp.

"""
Mutate a protein with *FoldX*

Parameters
----------
receptor : AtomArray, shape=(n,)
The structure of the proiten molecule.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The structure of the proiten molecule.
The structure of the protein molecule.


mutation : str, in classical mutation format
subunit : std, a subunit index in the pdb file
Comment on lines +33 to +34
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parameter description style is not consistent with NumpyDoc.

bin_path : str, optional
Path to the *FoldX* binary.

Examples
--------
>>> # simple protein
>>> >>> receptor = atom_array
>>> app = FoldXApp(receptor,
... "A34G+E44G", subunit = "A"
... )
Comment on lines +40 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
>>> # simple protein
>>> >>> receptor = atom_array
>>> app = FoldXApp(receptor,
... "A34G+E44G", subunit = "A"
... )
>>> receptor = atom_array
>>> app = FoldXApp(receptor,
... "A34G+E44G", subunit = "A"
... )

"""

def __init__(self, receptor, mutation, subunit = 'B', bin_path="foldx"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP 8 fix

Suggested change
def __init__(self, receptor, mutation, subunit = 'B', bin_path="foldx"):
def __init__(self, receptor, mutation, subunit="B", bin_path="foldx"):


super().__init__(bin_path)

self._receptor = receptor.copy()
self._mutation = mutation
self._subunit = subunit
self._seed = None

self._receptor_file = NamedTemporaryFile(
"w", suffix=".pdb", delete=False
)
self._mutated_receptor_file = NamedTemporaryFile(
"w", suffix=".pdb", delete=False
)

self._rotabase_file = NamedTemporaryFile(
"w", suffix=".txt", delete=False
)

self._folding_file = NamedTemporaryFile(
"w", suffix=".txt", delete=False, prefix = "individual_list"
)

@requires_state(AppState.CREATED)
def set_seed(self, seed):
"""
Fix the seed for the random number generator to get
reproducible results.

By default, the seed is chosen randomly.

Parameters
----------
seed : int
The seed for the random number generator.
"""
self._seed = seed


def setup_folding_file(self, subunit, mutation):
"""
Create a temporarily folding file for the mutation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Create a temporarily folding file for the mutation
Create a temporarily folding file for the mutation.


Parameters
----------
subnut: str
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
subnut: str
subunit: str

mutation :str
"""
entry = [elem[0]+subunit+elem[1:] for elem in mutation.split("+")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP 8 fix

Suggested change
entry = [elem[0]+subunit+elem[1:] for elem in mutation.split("+")]
entry = [elem[0] + subunit + elem[1:] for elem in mutation.split("+")]

self._folding_file.write(";".join(entry)+";\n")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP 8 fix

Suggested change
self._folding_file.write(";".join(entry)+";\n")
self._folding_file.write(";".join(entry) + ";\n")

self._folding_file.flush()


def run(self):
# Use different atom ID ranges for atoms in ligand and receptor
# for unambiguous assignment, if the receptor contains flexible
# residues
receptor_file = PDBFile()
receptor_file.set_structure(self._receptor)
receptor_file.write(self._receptor_file)

self._receptor_file.flush()

# set up folding file
self.setup_folding_file(self._subunit,self._mutation)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP 8 fix

Suggested change
self.setup_folding_file(self._subunit,self._mutation)
self.setup_folding_file(self._subunit, self._mutation)


# tempfile
temp = "/".join(self._receptor_file.name.split("/")[0:-1])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use join() and split() from os.path to ensure that this works also on operating systems, that use other separators than /.


# set up rotabase - copy to tempfile
rotabase_path = "/".join(os.path.realpath(__file__).split("/")[0:-1])+"/rotabase.txt"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the rotabase.txt is part of the FoldX software and hence governed by its license, am I correct? In this case the rotabase cannot be shipped with Biotite but needs to be supplied as separate input file to the Application object.

#os.popen('cp '+rotabase_path+' '+getcwd()+"/rotabase.txt")
os.popen('cp '+rotabase_path+' '+self._rotabase_file.name)
Comment on lines +119 to +120
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using shutil.copyfile() makes file copying more stable on all operating systems.


arguments = [
"--command", "BuildModel",
"--pdb", self._receptor_file.name.split("/")[-1],
"--pdb-dir", temp,
"--mutant-file", self._folding_file.name,
"--output-dir", temp,
"--rotabaseLocation", self._rotabase_file.name,
#"--output-file", self._mutated_receptor_file.name.split("/")[-1],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#"--output-file", self._mutated_receptor_file.name.split("/")[-1],

"--clean-mode", "1",
"--pdbHydrogens","1"
]
self._output_filename = temp+"/"+self._receptor_file.name.split("/")[-1][:-4]+"_1.pdb"
self.set_arguments(arguments)

super().run()

def clean_up(self):
super().clean_up()
cleanup_tempfile(self._receptor_file)
cleanup_tempfile(self._mutated_receptor_file)
cleanup_tempfile(self._folding_file)
cleanup_tempfile(self._rotabase_file)

# remove rotabase file
#os.remove(getcwd()+"/rotabase.txt")

# remove output_file
os.remove(self._output_filename)


def evaluate(self):
super().evaluate()
out_file = PDBFile.read(self._output_filename)
models = out_file.get_structure(include_bonds = True, model=1)
self.new_mutant = models
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this variable can be considered private.

Suggested change
self.new_mutant = models
self._new_mutant = models



@requires_state(AppState.JOINED)
def get_mutant(self):
"""
Get the mutant protein structure
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Get the mutant protein structure
Get the mutant protein structure.


Returns
-------
ligand : AtomArrayStack
The mutated protein
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The mutated protein
The mutated protein.


"""
return self.new_mutant
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return self.new_mutant
return self._new_mutant


@staticmethod
def mutate(receptor, mutation, bin_path="vina"):
"""
Mutate the protein with *FoldX BuildModel*.

This is a convenience function, that wraps the :class:`FoldX`
execution.

Parameters
----------

Returns
-------

"""
app = FoldXApp( receptor, mutation, bin_path)
app.start()
app.join()
return app.get_mutant()
Loading