view allpy/fileio.py @ 599:56d62d405021
pair_cores_all.py: close openned files to avoid error
error "Too many openned files" occured in child processes
multiprocessing seems to use old process when new python process
is created to avoid increasing of process number. Therefore
files openned in childred are not closed automatically
author |
boris (kodomo) <bnagaev@gmail.com> |
date |
Sun, 03 Apr 2011 17:05:32 +0400 |
parents |
ab3f5300bf4e |
children |
2a119d110ff6 |
line source
2 from tempfile import NamedTemporaryFile
6 """Automatical file IO."""
7 def __new__(cls, file, format="fasta"):
11 return EmbossFile(file, format)
13 class FastaFile(object):
14 """Fasta parser & writer."""
16 def __init__(self, file, wrap_column=70):
18 self.wrap_column = wrap_column
20 def write_string(self, string, name, description=''):
21 """Append one sequence to file."""
23 name += " " + description
24 self.file.write(">%s\n" % name)
27 self.file.write(string[:self.wrap_column]+"\n")
28 string = string[self.wrap_column:]
30 self.file.write(string+"\n")
33 def write_strings(self, sequences):
34 """Write sequences to file.
36 Sequences are given as list of tuples (string, name, description).
38 for string, name, description in sequences:
39 self.write_string(string, name, description)
41 def read_strings(self):
42 for part in self.file.read().split("\n>"):
43 header, _, body = part.partition("\n")
44 header = header.lstrip(">")
45 name, _, description = header.partition(" ")
47 description = description.strip()
48 body = util.remove_each(body, " \n\r\t\v")
49 yield (name, description, body)
51 class EmbossFile(object):
52 """Parser & writer for file formats supported by EMBOSS."""
54 def __init__(self, file, format):
58 def write_strings(self, sequences):
59 """Write sequences to file."""
60 # XXX: in case of exceptions files are not closed, nor unlinked
61 tmpfile = NamedTemporaryFile('w', delete=False)
62 FastaFile(tmpfile).write_strings(self.fix_sequences(sequences))
64 os.system("seqret %s::%s %s" % (self.format, tmpfile, self.file.name))
65 os.unlink(tmpfile.name)
67 def fix_sequences(self, sequences):
68 """EMBOSS does not permit : in file names. Fix sequences for that."""
69 for name, description, sequence in sequences:
70 yield name.replace(':', '_'), description, sequence
72 def read_strings(self):
73 """Read sequences from file."""
74 # XXX: in case of exceptions files are not closed, nor unlinked
75 tmpfile = NamedTemporaryFile(delete=False)
77 os.system("seqret %s %s::%s" % (self.file.name, self.format, tmpfile))
78 sequences = FastaFile(tmpfile).read_strings()
82 # vim: set et ts=4 sts=4 sw=4: