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

allpy

view allpy/fileio.py @ 668:3bdcee61356d

blocks3d/www: fix log-file caused bug of building r2w was configured to use /tmp/blocks3d-www-build.log. It caused build error if this file has been creted by other user. Log-file path was changed to ./output/blocks3d-www-build.log Makefile was changed to create ./output directory before running r2w
author boris (kodomo) <bnagaev@gmail.com>
date Fri, 01 Jul 2011 10:32:37 +0400
parents b6a53615f1e9
children 80043822a41e
line source
1 import os
2 from subprocess import Popen, PIPE
3 from tempfile import NamedTemporaryFile
4 import util
6 class File(object):
7 """Automatical file IO."""
8 def __new__(cls, file, format="fasta"):
9 if format == "fasta":
10 return FastaFile(file)
11 else:
12 return EmbossFile(file, format)
14 class FastaFile(object):
15 """Fasta parser & writer."""
17 def __init__(self, file, wrap_column=70):
18 self.file = file
19 self.wrap_column = wrap_column
21 def write_string(self, string, name, description=''):
22 """Append one sequence to file."""
23 if description:
24 name += " " + description
25 self.file.write(">%s\n" % name)
26 if self.wrap_column:
27 while string:
28 self.file.write(string[:self.wrap_column]+"\n")
29 string = string[self.wrap_column:]
30 else:
31 self.file.write(string+"\n")
32 self.file.flush()
34 def write_strings(self, sequences):
35 """Write sequences to file.
37 Sequences are given as list of tuples (string, name, description).
38 """
39 for string, name, description in sequences:
40 self.write_string(string, name, description)
42 def read_strings(self):
43 for part in self.file.read().split("\n>"):
44 header, _, body = part.partition("\n")
45 header = header.lstrip(">")
46 name, _, description = header.partition(" ")
47 name = name.strip()
48 description = description.strip()
49 body = util.remove_each(body, " \n\r\t\v")
50 yield (name, description, body)
52 class EmbossFile(object):
53 """Parser & writer for file formats supported by EMBOSS."""
55 def __init__(self, file, format):
56 self.file = file
57 self.format = format
59 def write_strings(self, sequences):
60 """Write sequences to file."""
61 pipe = Popen(['seqret', 'stdin', '%s::stdout' % self.format],
62 stdin=PIPE, stdout=PIPE
63 )
64 FastaFile(pipe.stdin).write_strings(self.fix_sequences(sequences))
65 pipe.stdin.close()
66 for line in pipe.stdout:
67 self.file.write(line)
69 def fix_sequences(self, sequences):
70 """EMBOSS does not permit : in file names. Fix sequences for that."""
71 for name, description, sequence in sequences:
72 yield name.replace(':', '_'), description, sequence
74 def read_strings(self):
75 """Read sequences from file."""
76 pipe = Popen(['seqret', '%s::stdin' % self.format, 'stdout'],
77 stdin=PIPE, stdout=PIPE
78 )
79 for line in self.file:
80 pipe.stdin.write(line)
81 pipe.stdin.close()
82 return FastaFile(pipe.stdout).read_strings()
84 # vim: set et ts=4 sts=4 sw=4: