00001 import os
00002 from ost import seq, mol, io
00003 import tempfile
00004 def SaveToTempDir(objects, seq_format='fasta', structure_format='pdb'):
00005 """
00006 Take all objects and saves them to a temporary directory. The paths of the
00007 saved files will be returned as a tuple. This works for alignments, and
00008 structure files. The output format for sequence files and structure files can
00009 be changed by setting to seq_format and structure_format parameters
00010 appropriately.
00011 """
00012
00013 tmp_dir_name=tempfile.mkdtemp()
00014 file_names=[]
00015 for index, obj in enumerate(objects):
00016 if isinstance(obj, seq.AlignmentHandle):
00017 name=os.path.join(tmp_dir_name, 'aln%02d.fasta' % (index+1))
00018 io.SaveAlignment(obj, name, seq_format)
00019 file_names.append(name)
00020 continue
00021 if isinstance(obj, seq.SequenceHandle):
00022 name=os.path.join(tmp_dir_name, 'seq%02d.fasta' % (index+1))
00023 io.SaveSequence(obj, name, seq_format)
00024 file_names.append(name)
00025 continue
00026 if isinstance(obj, seq.ConstSequenceList) or isinstance(obj, seq.SequenceList):
00027 name=os.path.join(tmp_dir_name, 'sql%02d.fasta' % (index+1))
00028 io.SaveSequenceList(obj, name, seq_format)
00029 file_names.append(name)
00030 continue
00031 if isinstance(obj, mol.EntityView) or isinstance(obj, mol.EntityHandle):
00032 name=os.path.join(tmp_dir_name, tmp_dir_name, 'mol%02d.pdb' % (index+1))
00033 io.SaveEntity(obj, name, structure_format)
00034 file_names.append(name)
00035 continue
00036 return file_names
00037
00038 class TempDirWithFiles:
00039 def __init__(self, objects, seq_format='fasta', structure_format='pdb'):
00040 self.files=SaveToTempDir(objects, seq_format=seq_format,
00041 structure_format=structure_format)
00042 self.dirname=os.path.dirname(self.files[0])
00043 def Cleanup(self):
00044 import shutil
00045 shutil.rmtree(self.dirname)
00046
00047