LOOS  v2.3.2
LipidLibrary.py
1 #!/usr/bin/env python
2 
3 import os
4 import glob
5 import loos
6 import random
7 import sys
8 
9 # @cond TOOLS_INTERNAL
10 
11 class LipidLibrary:
12  """
13  Class to access a lipid library directory containing either PDB or
14  CHARMM CRD files.
15  """
16  def __init__(self, path):
17  self.path = path
18  if not os.path.exists(path):
19  print self.error_message()
20  sys.exit()
21 
22  all_files = os.listdir(self.path)
23 
24  # select files, either pdb or crd
25  self.structures = []
26  for file in all_files:
27  if (file.endswith(".pdb") or file.endswith(".crd")):
28  self.structures.append(file)
29 
30  if self.size() == 0:
31  print self.error_message()
32  sys.exit()
33 
34  def size(self):
35  return len(self.structures)
36 
37  def pick_structure(self):
38  # pick a structure file at random, and build up the filename
39  filename = os.path.join(self.path, random.choice(self.structures))
40 
41  # read the file
42  lipid = loos.createSystem(filename)
43 
44  # return the AtomicGroup
45  return lipid
46 
47  def __getitem__(self, index):
48  return self.structures[index]
49 
50  def error_message(self):
51  msg = "Error reading lipid library (" + self.path + ")\n"
52  msg += "Either directory doesn't exist or there are no pdb/crd files\n"
53  msg += "Try downloading our lipid library from \n"
54  msg += "http://sourceforge.net/projects/loos/files/loos%20material/lipid_library.tgz/download\n"
55  msg += "Exiting...."
56 
57  return msg
58 
59 # @endcond
AtomicGroup createSystem(const std::string &filename, const std::string &filetype)
Definition: sfactories.cpp:119