From abba7e6e2561fe056d1c8ef8b4498b5be8a2b067 Mon Sep 17 00:00:00 2001 From: marco <marco@5a81b35b-ba03-0410-adc8-b2c5c5119f08> Date: Thu, 15 Apr 2010 09:31:34 +0000 Subject: [PATCH] clustalw bindings git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@2014 5a81b35b-ba03-0410-adc8-b2c5c5119f08 --- modules/bindings/pymod/CMakeLists.txt | 3 +- modules/bindings/pymod/clustalw.py | 22 +++++++++++++ modules/bindings/pymod/utils.py | 47 +++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 modules/bindings/pymod/clustalw.py create mode 100644 modules/bindings/pymod/utils.py diff --git a/modules/bindings/pymod/CMakeLists.txt b/modules/bindings/pymod/CMakeLists.txt index c17cb439f..86fad8a93 100644 --- a/modules/bindings/pymod/CMakeLists.txt +++ b/modules/bindings/pymod/CMakeLists.txt @@ -1 +1,2 @@ -pymod(NAME bindings PY __init__.py lga.py hbplus.py msms.py tmtools.py dssp.py) +pymod(NAME bindings PY __init__.py lga.py hbplus.py msms.py tmtools.py + dssp.py clustalw.py utils.py) diff --git a/modules/bindings/pymod/clustalw.py b/modules/bindings/pymod/clustalw.py new file mode 100644 index 000000000..3625ede81 --- /dev/null +++ b/modules/bindings/pymod/clustalw.py @@ -0,0 +1,22 @@ +from ost.bindings import utils +from ost import settings, io, seq +import os +import subprocess + +def ClustalW(seq1, seq2, clustalw=None): + clustalw_path=settings.Locate(('clustalw', 'clustalw2'), + explicit_file_name=clustalw) + seq_list=seq.CreateSequenceList() + seq_list.AddSequence(seq1) + seq_list.AddSequence(seq2) + temp_dir=utils.TempDirWithFiles((seq_list,)) + out=os.path.join(temp_dir.dirname, 'out.fasta') + command='%s -infile="%s" -output=fasta -outfile="%s"' % (clustalw_path, + temp_dir.files[0], + out) + ps=subprocess.Popen(command, shell=True, stdout=subprocess.PIPE) + ps.stdout.readlines() + aln=io.LoadAlignment(out) + temp_dir.Cleanup() + return aln + \ No newline at end of file diff --git a/modules/bindings/pymod/utils.py b/modules/bindings/pymod/utils.py new file mode 100644 index 000000000..27d4008d8 --- /dev/null +++ b/modules/bindings/pymod/utils.py @@ -0,0 +1,47 @@ +import os +from ost import seq, mol, io +import tempfile +def SaveToTempDir(objects, seq_format='fasta', structure_format='pdb'): + """ + Take all objects and saves them to a temporary directory. The paths of the + saved files will be returned as a tuple. This works for alignments, and + structure files. The output format for sequence files and structure files can + be changed by setting to seq_format and structure_format parameters + appropriately. + """ + # create temporary directory + tmp_dir_name=tempfile.mkdtemp() + file_names=[] + for index, obj in enumerate(objects): + if isinstance(obj, seq.AlignmentHandle): + name=os.path.join(tmp_dir_name, 'aln%02d.fasta' % (index+1)) + io.SaveAlignment(obj, name, seq_format) + file_names.append(name) + continue + if isinstance(obj, seq.SequenceHandle): + name=os.path.join(tmp_dir_name, 'seq%02d.fasta' % (index+1)) + io.SaveSequence(obj, name, seq_format) + file_names.append(name) + continue + if isinstance(obj, seq.ConstSequenceList) or isinstance(obj, seq.SequenceList): + name=os.path.join(tmp_dir_name, 'sql%02d.fasta' % (index+1)) + io.SaveSequenceList(obj, name, seq_format) + file_names.append(name) + continue + if isinstance(obj, mol.EntityView) or isinstance(obj, mol.EntityHandle): + name=os.path.join(tmp_dir_name, tmp_dir_name, 'mol%02d.pdb' % (index+1)) + io.SaveEntity(model, name, structure_format) + file_names.append(name) + continue + return file_names + +class TempDirWithFiles: + def __init__(self, objects, seq_format='fasta', structure_format='pdb'): + self.files=SaveToTempDir(objects, seq_format=seq_format, + structure_format=structure_format) + self.dirname=os.path.dirname(self.files[0]) + def Cleanup(self): + import shutil + shutil.rmtree(self.dirname) + + -- GitLab