Skip to content

RemoveWaterInSystem

dstoeckel edited this page Mar 16, 2015 · 2 revisions

How can I remove water molecules from a system?

Water molecules within a System can be identified using the Selector. Selected AtomContainers can then be easily removed with the removedSelected method of class System.

C++

#!cpp
#include <BALL/KERNEL/selector.h>

System system;
...
// define the selector by Expression
Selector wat("residue(HOH) OR residue(WAT)");
// apply
system.apply(wat);

// do something, e.g. count 
cout << wat.getNumberOfSelectedAtoms() << endl;

// remove
system.removeSelected();
...
// !!! or !!! deselect
system.deselect();

Note: This does not only remove all selected atoms, but also results in emtpy AtomContainers, e.g. residues.

Python

from sys import *
import os
from BALL import *

my_directory = "/my/directory/"


files = os.listdir(my_directory)
print "Work on directory: ", my_directory

fdb = FragmentDB("")

# consider all files
for file in files:
  if (file[-4:]!=".pdb"):
    continue
  print "Current file: " , file
  # load file
  pdbfile = PDBFile(my_directory+file)
  S = System()
  pdbfile.read(S)
  print "Number of atoms:", S.countAtoms() 
  # normalize names
  S.apply(fdb.add_hydrogens)
  S.apply(fdb.build_bonds)
  S.apply(fdb.normalize_names) 
  # select water residues or something else 
  selector = Selector("residue(HOH)")  #Selector("residue(ASP) OR residue(ASN) OR name(CA)")
  S.apply(selector)
  print "Number of selected atoms:", len(selector.getSelectedAtoms())
  # delete the selected atoms
  S.removeSelected()
  # write 
  outname = file[:-4] + "_min.pdb"
  print "Write resulting structure as ", outname, " to disk"
  outfile = PDBFile()
  outfile.open(outname, OpenMode(File.MODE_OUT))
  outfile << S
  outfile.close()
Clone this wiki locally