allpy
changeset 640:69079d72d207
allpy.fileio: use stdin/stdout when communicating with EMBOSS [closes #38]
The new implementation allows using any special files for fileio,
including stdin/stdout, urllib, StringIO, etc.
The new implementation is also cleaner and not prone to leaving temporary files
behind in any cases.
author | Daniil Alexeyevsky <dendik@kodomo.fbb.msu.ru> |
---|---|
date | Fri, 03 Jun 2011 15:29:47 +0400 |
parents | b6a53615f1e9 |
children | e90119f01df8 |
files | allpy/fileio.py |
diffstat | 1 files changed, 15 insertions(+), 16 deletions(-) [+] |
line diff
1.1 --- a/allpy/fileio.py Mon May 30 20:38:58 2011 +0400 1.2 +++ b/allpy/fileio.py Fri Jun 03 15:29:47 2011 +0400 1.3 @@ -1,4 +1,5 @@ 1.4 import os 1.5 +from subprocess import Popen, PIPE 1.6 from tempfile import NamedTemporaryFile 1.7 import util 1.8 1.9 @@ -57,14 +58,13 @@ 1.10 1.11 def write_strings(self, sequences): 1.12 """Write sequences to file.""" 1.13 - try: 1.14 - tmpfile = NamedTemporaryFile('w', delete=False) 1.15 - FastaFile(tmpfile).write_strings(self.fix_sequences(sequences)) 1.16 - tmpfile.close() 1.17 - os.system("seqret %s::%s %s" % (self.format, tmpfile.name, self.file.name)) 1.18 - finally: 1.19 - tmpfile.close() 1.20 - os.unlink(tmpfile.name) 1.21 + pipe = Popen(['seqret', 'stdin', '%s::stdout' % self.format], 1.22 + stdin=PIPE, stdout=PIPE 1.23 + ) 1.24 + FastaFile(pipe.stdin).write_strings(self.fix_sequences(sequences)) 1.25 + pipe.stdin.close() 1.26 + for line in pipe.stdout: 1.27 + self.file.write(line) 1.28 1.29 def fix_sequences(self, sequences): 1.30 """EMBOSS does not permit : in file names. Fix sequences for that.""" 1.31 @@ -73,13 +73,12 @@ 1.32 1.33 def read_strings(self): 1.34 """Read sequences from file.""" 1.35 - try: 1.36 - tmpfile = NamedTemporaryFile(delete=False) 1.37 - self.file.flush() 1.38 - os.system("seqret %s %s::%s" % (self.file.name, self.format, tmpfile.name)) 1.39 - sequences = FastaFile(tmpfile).read_strings() 1.40 - finally: 1.41 - os.unlink(tmpfile) 1.42 - return sequences 1.43 + pipe = Popen(['seqret', '%s::stdin' % self.format, 'stdout'], 1.44 + stdin=PIPE, stdout=PIPE 1.45 + ) 1.46 + for line in self.file: 1.47 + pipe.stdin.write(line) 1.48 + pipe.stdin.close() 1.49 + return FastaFile(pipe.stdout).read_strings() 1.50 1.51 # vim: set et ts=4 sts=4 sw=4: