allpy
changeset 149:85fc264975a2
everwhere: mkstemp --> NamedTemporaryFile. unlink useless temp files
block.pdb_save in progress
author | boris <bnagaev@gmail.com> |
---|---|
date | Mon, 25 Oct 2010 00:03:35 +0400 |
parents | bc4c291cc2b0 |
children | f7dead025719 |
files | geometrical_core/geometrical_core.py lib/block.py lib/project.py |
diffstat | 3 files changed, 36 insertions(+), 21 deletions(-) [+] |
line diff
1.1 --- a/geometrical_core/geometrical_core.py Sun Oct 24 23:17:24 2010 +0400 1.2 +++ b/geometrical_core/geometrical_core.py Mon Oct 25 00:03:35 2010 +0400 1.3 @@ -8,7 +8,7 @@ 1.4 Project = project.Project 1.5 import argparse 1.6 import os 1.7 -from tempfile import mkstemp 1.8 +from tempfile import NamedTemporaryFile 1.9 1.10 r = argparse.FileType('r') 1.11 w = argparse.FileType('w') 1.12 @@ -99,6 +99,8 @@ 1.13 p.add_argument('-a',help='Cores count',metavar='NEW_ATOMS',type=pos,default=c.ac_count) 1.14 p.add_argument('-x',help='Superposition core identifier',type=i_nng,default=0) 1.15 1.16 +tmp_file = None 1.17 + 1.18 try: 1.19 args = p.parse_args() 1.20 1.21 @@ -113,12 +115,11 @@ 1.22 project = Project(args.i) 1.23 except: 1.24 args.i.close() 1.25 - tmp_file, tmp_filename = mkstemp() 1.26 - os.close(tmp_file) # this is file descriptor, not normal file object. 1.27 - 1.28 + tmp_file = NamedTemporaryFile(delete=False) 1.29 + tmp_file.close() 1.30 os.system('seqret %(msf)s %(fasta)s' % \ 1.31 - {'fasta': tmp_filename, 'msf': args.i.name}) 1.32 - args.i = open(tmp_filename) 1.33 + {'msf': args.i.name, 'fasta': tmp_file.name}) 1.34 + args.i = open(tmp_file.name) 1.35 project = Project(args.i) 1.36 1.37 project.pdb_auto_add(args.c) 1.38 @@ -175,5 +176,6 @@ 1.39 print t 1.40 exit() 1.41 1.42 +if tmp_file: 1.43 + os.unlink(tmp_file) 1.44 1.45 -
2.1 --- a/lib/block.py Sun Oct 24 23:17:24 2010 +0400 2.2 +++ b/lib/block.py Mon Oct 25 00:03:35 2010 +0400 2.3 @@ -8,6 +8,8 @@ 2.4 import config 2.5 from graph import Graph 2.6 from Bio.PDB import Superimposer 2.7 +from tempfile import NamedTemporaryFile 2.8 +import os 2.9 2.10 class Block(object): 2.11 """ Block of alignment 2.12 @@ -147,8 +149,8 @@ 2.13 2.14 def superimpose(self): 2.15 """ Superimpose all pdb_chains in this block """ 2.16 - sequences_chains = [] 2.17 - if len(self.sequences) >= 1: 2.18 + sequences_chains = list(self.sequences_chains()) 2.19 + if len(sequences_chains) >= 1: 2.20 sup = Superimposer() 2.21 fixed_sequence, fixed_chain = sequences_chains.pop() 2.22 fixed_atoms = self.ca_atoms(fixed_sequence, fixed_chain) 2.23 @@ -159,10 +161,20 @@ 2.24 sup.apply(moving_atoms) 2.25 2.26 def pdb_save(self, out_file): 2.27 - """ Superimpose all pdb_chains in this block """ 2.28 - tmp_file, tmp_filename = mkstemp() 2.29 - os.close(tmp_file) # this is file descriptor, not normal file object. 2.30 - sequence.pdb_save(tmp_filename, chain) 2.31 + """ Save all sequences 2.32 + 2.33 + Returns {(sequence, chain): CHAIN} 2.34 + CHAIN is chain letter in new file 2.35 + """ 2.36 + tmp_file = NamedTemporaryFile(delete=False) 2.37 + tmp_file.close() 2.38 + 2.39 + for sequence, chain in self.sequences_chains(): 2.40 + sequence.pdb_save(tmp_file.name, chain) 2.41 + # TODO: read from tmp_file.name 2.42 + # change CHAIN 2.43 + # add to out_file 2.44 + 2.45 + os.unlink(NamedTemporaryFile) 2.46 2.47 2.48 -
3.1 --- a/lib/project.py Sun Oct 24 23:17:24 2010 +0400 3.2 +++ b/lib/project.py Mon Oct 25 00:03:35 2010 +0400 3.3 @@ -12,7 +12,8 @@ 3.4 from monomer import AminoAcidType 3.5 import allpy_data 3.6 import os 3.7 -from tempfile import mkstemp 3.8 +from tempfile import NamedTemporaryFile 3.9 +import os 3.10 import block 3.11 3.12 Block = block.Block 3.13 @@ -178,13 +179,11 @@ 3.14 3.15 uses old Monomers and Sequences objects 3.16 """ 3.17 - tmp_file, tmp_filename = mkstemp() 3.18 - os.close(tmp_file) # this is file descriptor, not normal file object. 3.19 - tmp_file = open(tmp_filename, 'w') 3.20 + tmp_file = NamedTemporaryFile(delete=False) 3.21 self.save_fasta(tmp_file) 3.22 tmp_file.close() 3.23 - os.system("muscle -in %(tmp)s -out %(tmp)s" % {'tmp': tmp_filename}) 3.24 - sequences, alignment = Project.from_fasta(open(tmp_filename)) 3.25 + os.system("muscle -in %(tmp)s -out %(tmp)s" % {'tmp': tmp_file.name}) 3.26 + sequences, alignment = Project.from_fasta(open(tmp_file.name)) 3.27 for sequence in self.sequences: 3.28 try: 3.29 new_sequence = [i for i in sequences if sequence==i][0] 3.30 @@ -201,7 +200,9 @@ 3.31 if monomer != old_monomer: 3.32 raise Exception("Align: alignment errors") 3.33 self.alignment[sequence].append(old_monomer) 3.34 - 3.35 + os.unlink(tmp_file.name) 3.36 + 3.37 + 3.38 def column(self, sequence=None, sequences=None, original=None): 3.39 """ returns list of columns of alignment 3.40