Skip to content

Commit

Permalink
Merge branch 'master' of github.com:rigdenlab/SIMBAD
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Simkovic committed May 3, 2017
2 parents 21d59ec + 6f1f3b1 commit 2a1aec6
Show file tree
Hide file tree
Showing 8 changed files with 879,774 additions and 631,057 deletions.
4 changes: 2 additions & 2 deletions simbad/command_line/simbad_contaminant.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def main():
args = p.parse_args()

if args.work_dir:
logging.info('Making a named work directory: %s' % args.work_dir)
logging.info('Making a named work directory: %s', args.work_dir)
try:
os.mkdir(args.work_dir)
except:
except OSError:
msg = "Cannot create work_dir {0}".format(args.work_dir)
simbad.util.exit_util.exit_error(msg, sys.exc_info()[2])
else:
Expand Down
169 changes: 169 additions & 0 deletions simbad/scripts/lattice_database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
"""Script to update the database for the lattice parameter search"""

__author__ = "Adam Simpkin"
__date__ = "28 Apr 2017"
__version__ = "0.1"

import cctbx.crystal
import cctbx.uctbx
import cPickle
import numpy
import os
import urllib

import simbad.constants


class _LatticeParameters(object):
"""A basic lattice parameter sorting class"""

__slots__ = ('pdb_code', 'a', 'b', 'c', 'alpha', 'beta', 'gamma', 'space_group')

def __init__(self, pdb_code, a, b, c, alpha, beta, gamma, space_group):
self.pdb_code = pdb_code
self.a = a
self.b = b
self.c = c
self.alpha = alpha
self.beta = beta
self.gamma = gamma
self.space_group = space_group

def __repr__(self):
return "{0}(pdb_code={1} a={2} b={3} c={4} alpha={5}, beta={6}, gamma={7}, space_group={8}".format(
self.__class__.__name__, self.pdb_code, self.a, self.b, self.c,
self.alpha, self.beta, self.gamma, self.space_group)

def _as_dict(self):
"""Convert the :obj:`_LatticeParameterScore <simbad.lattice.search._LatticeParameterScore>`
object to a dictionary"""
dictionary = {}
for k in self.__slots__:
dictionary[k] = getattr(self, k)
return dictionary


def custom_report():
"""Create a custom report from the PDB and returns a :obj: _LatticeParameters containing
the pdb code, lattice parameters and space group for every structure in the PDBs"""

link = 'http://www.rcsb.org/pdb/rest/customReport.csv?pdbids=*&customReportColumns=lengthOfUnitCellLatticeA,' \
'lengthOfUnitCellLatticeB,lengthOfUnitCellLatticeC,unitCellAngleAlpha,unitCellAngleBeta,unitCellAngleGamma,' \
'spaceGroup&format=csv&service=wsfile'

urllib.urlretrieve(link, os.path.join(os.getcwd(), 'results.csv'))

results = []
with open(os.path.join(os.getcwd(), 'results.csv'), 'r') as f:
for line in f:
if line.startswith('structureId'):
continue
else:
try:
tmp = line.split('","')
pdb_code = tmp[0][1:]
a = float(tmp[1])
b = float(tmp[2])
c = float(tmp[3])
alpha = float(tmp[4])
beta = float(tmp[5])
gamma = float(tmp[6])
space_group = tmp[7][:-2]

data = _LatticeParameters(pdb_code, a, b, c, alpha, beta, gamma, space_group)
results.append(data)
except ValueError:
pass

os.unlink(os.path.join(os.getcwd(), 'results.csv'))

return results


def calculate_niggli_cell(unit_cell, space_group):
"""Calculate the parameters of the Niggli cell
Parameters
----------
unit_cell : list, tuple
The parameters of the unit cell
space_group : str
The space group
Returns
-------
list
The Niggli cell parameters
"""

unit_cell = cctbx.uctbx.unit_cell(unit_cell)

xs = cctbx.crystal.symmetry(
unit_cell=unit_cell,
space_group=space_group
)
niggli_cell = xs.change_basis(xs.change_of_basis_op_to_niggli_cell()).unit_cell()
niggli_cell = numpy.array(eval(str(niggli_cell))).tolist()
return niggli_cell


def create_niggli_cell_db(crystal_data):
"""Generate a new database for the lattice parameter search, overwrites old file"""

# pickle_file = 'niggli_database.cpk'
pickle_file = simbad.constants.SIMBAD_LATTICE_DB
names = []
lattice_pars = []

for i, c in enumerate(crystal_data):
unit_cell = ' '.join(str(p) for p in [c.a, c.b, c.c, c.alpha, c.beta, c.gamma])

if c.space_group.replace(' ', '') == "A1":
space_group = "P1"
elif c.space_group.replace(' ', '') == "B2":
space_group = "B112"
elif c.space_group.replace(' ', '') == "C1211":
space_group = "C2"
elif c.space_group.replace(' ', '') == "F422":
space_group = "I422"
elif c.space_group.replace(' ', '') == "I21":
space_group = "I2"
elif c.space_group.replace(' ', '') == "I1211":
space_group = "I2"
elif c.space_group.replace(' ', '') == "P21212A":
space_group = "P212121"
elif c.space_group.replace(' ', '') == "R3":
space_group = "R3:R"
elif c.space_group.replace(' ', '') == "C4212":
space_group = "P422"
else:
space_group = c.space_group

try:
niggli_cell = calculate_niggli_cell(unit_cell, space_group)
except AssertionError:
pass
except ValueError:
pass
except RuntimeError:
pass

if niggli_cell:
vals = numpy.array(niggli_cell)
if len(vals) == 6:
lattice_pars.append(vals)
names.append(c.pdb_code)

print "Total files loaded = {0}".format(i)

database = [names, lattice_pars]

cPickle.dump(database, open(pickle_file, "w"))

return


if __name__ == "__main__":
results = custom_report()
create_niggli_cell_db(results)
16 changes: 8 additions & 8 deletions simbad/util/config_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _get_config_file(self, cmd_file=None):
logger.critical(msg)
raise RuntimeError(msg)

logger.debug("Using configuration file: {0}")
logger.debug("Using configuration file: %s", config_file)
return config_file

def _process_options(self):
Expand Down Expand Up @@ -122,19 +122,19 @@ def _process_options(self):

def _preset_options(self, mode):
assert hasattr(self, mode), "Unknown mode: {0}".format(mode)
logger.info("Using preset mode: %s" % mode)
logger.info("Using preset mode: %s", mode)
for k, v in getattr(self, mode).iteritems():
if 'cmdline_flags' in self.d and k in self.d['cmdline_flags']:
if self.d[k] == v:
msg = 'WARNING! {0} flag {1} => {2} was duplicated on the command line!'.format(mode, v, k)
msg = 'WARNING! {0} flag {1} > {2} was duplicated on the command line!'.format(mode, v, k)
else:
msg = 'WARNING! Overridng {0} setting: {1} => {2} with {3}'.format(mode, k, v, self.d[k])
msg = 'WARNING! Overriding {0} setting: {1} > {2} with {3}'.format(mode, k, v, self.d[k])
logger.critical(msg)
elif k in self.d:
logger.debug("%s overriding default setting: %s => %s with %s" % mode, k, v, self.d[k])
logger.debug("%s overriding default setting: %s -> %s with %s", mode, k, v, self.d[k])
self.d[k] = v
else:
logger.debug("%s setting: %s => %s" % mode, k, v)
logger.debug("%s setting: %s -> %s", mode, k, v)
self.d[k] = v
return

Expand Down Expand Up @@ -198,7 +198,7 @@ def _read_cmdline_opts(self, cmdline_opts):
if k not in self.d:
self.d[k] = v
elif v is not None:
logger.debug("Cmdline setting %s: %s => %s" % k, self.d[k], v)
logger.debug("Cmdline setting %s: %s -> %s", k, self.d[k], v)
self.d[k] = v

self.d['cmdline_flags'] = cmdline_flags
Expand Down Expand Up @@ -229,7 +229,7 @@ def write_config_file(self, config_file=None):
config_file = os.path.join(self.d['work_dir'], self.d['name'] + ".ini")
# Write config to job specific directory
self.d["out_config_file"] = config_file
logger.info("SIMBAD configuration written to: %s" % config_file)
logger.info("SIMBAD configuration written to: %s", config_file)
with open(config_file, "w") as out: config.write(out)
return

Expand Down

0 comments on commit 2a1aec6

Please sign in to comment.