OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
utils.py
Go to the documentation of this file.
1 import os
2 from ost import seq, mol, io
3 import tempfile
4 def SaveToTempDir(objects, seq_format='fasta', structure_format='pdb'):
5  """
6  Take all objects and saves them to a temporary directory. The paths of the
7  saved files will be returned as a tuple. This works for alignments, and
8  structure files. The output format for sequence files and structure files can
9  be changed by setting to seq_format and structure_format parameters
10  appropriately.
11  """
12  # create temporary directory
13  tmp_dir_name=tempfile.mkdtemp()
14  file_names=[]
15  for index, obj in enumerate(objects):
16  if isinstance(obj, seq.AlignmentHandle):
17  name=os.path.join(tmp_dir_name, 'aln%02d.fasta' % (index+1))
18  io.SaveAlignment(obj, name, seq_format)
19  file_names.append(name)
20  continue
21  if isinstance(obj, seq.SequenceHandle):
22  name=os.path.join(tmp_dir_name, 'seq%02d.fasta' % (index+1))
23  io.SaveSequence(obj, name, seq_format)
24  file_names.append(name)
25  continue
26  if isinstance(obj, seq.ConstSequenceList) or isinstance(obj, seq.SequenceList):
27  name=os.path.join(tmp_dir_name, 'sql%02d.fasta' % (index+1))
28  io.SaveSequenceList(obj, name, seq_format)
29  file_names.append(name)
30  continue
31  if isinstance(obj, mol.EntityView) or isinstance(obj, mol.EntityHandle):
32  name=os.path.join(tmp_dir_name, tmp_dir_name, 'mol%02d.pdb' % (index+1))
33  io.SaveEntity(model, name, structure_format)
34  file_names.append(name)
35  continue
36  return file_names
37 
39  def __init__(self, objects, seq_format='fasta', structure_format='pdb'):
40  self.files=SaveToTempDir(objects, seq_format=seq_format,
41  structure_format=structure_format)
42  self.dirname=os.path.dirname(self.files[0])
43  def Cleanup(self):
44  import shutil
45  shutil.rmtree(self.dirname)
46 
47