Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/allpy/file/56d62d405021/allpy/fileio.py
Дата изменения: Unknown
Дата индексирования: Mon Feb 4 03:33:27 2013
Кодировка:
allpy: 56d62d405021 allpy/fileio.py

allpy

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
1 import os
2 from tempfile import NamedTemporaryFile
3 import util
5 class File(object):
6 """Automatical file IO."""
7 def __new__(cls, file, format="fasta"):
8 if format == "fasta":
9 return FastaFile(file)
10 else:
11 return EmbossFile(file, format)
13 class FastaFile(object):
14 """Fasta parser & writer."""
16 def __init__(self, file, wrap_column=70):
17 self.file = file
18 self.wrap_column = wrap_column
20 def write_string(self, string, name, description=''):
21 """Append one sequence to file."""
22 if description:
23 name += " " + description
24 self.file.write(">%s\n" % name)
25 if self.wrap_column:
26 while string:
27 self.file.write(string[:self.wrap_column]+"\n")
28 string = string[self.wrap_column:]
29 else:
30 self.file.write(string+"\n")
31 self.file.flush()
33 def write_strings(self, sequences):
34 """Write sequences to file.
36 Sequences are given as list of tuples (string, name, description).
37 """
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(" ")
46 name = name.strip()
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):
55 self.file = file
56 self.format = 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))
63 tmpfile.close()
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)
76 self.file.flush()
77 os.system("seqret %s %s::%s" % (self.file.name, self.format, tmpfile))
78 sequences = FastaFile(tmpfile).read_strings()
79 os.unlink(tmpfile)
80 return sequences
82 # vim: set et ts=4 sts=4 sw=4: