view allpy/fileio.py @ 666:d9a3e99ae19b
blocks3d/www: mark conservative columns inside blocks
Previously letters were marked if column is conservative over all
sequences of alignment. Currently it was changed to conservatism
over block including this letter.
Additional attribute storing conservatism of columns inside block
was added to block. This caused all colors of blocks to be changed.
author |
boris <bnagaev@gmail.com> |
date |
Fri, 01 Jul 2011 03:21:08 +0400 |
parents |
b6a53615f1e9 |
children |
80043822a41e |
line source
2 from subprocess import Popen, PIPE
3 from tempfile import NamedTemporaryFile
7 """Automatical file IO."""
8 def __new__(cls, file, format="fasta"):
10 return FastaFile(file)
12 return EmbossFile(file, format)
14 class FastaFile(object):
15 """Fasta parser & writer."""
17 def __init__(self, file, wrap_column=70):
19 self.wrap_column = wrap_column
21 def write_string(self, string, name, description=''):
22 """Append one sequence to file."""
24 name += " " + description
25 self.file.write(">%s\n" % name)
28 self.file.write(string[:self.wrap_column]+"\n")
29 string = string[self.wrap_column:]
31 self.file.write(string+"\n")
34 def write_strings(self, sequences):
35 """Write sequences to file.
37 Sequences are given as list of tuples (string, name, description).
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(" ")
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):
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
64 FastaFile(pipe.stdin).write_strings(self.fix_sequences(sequences))
66 for line in pipe.stdout:
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
79 for line in self.file:
80 pipe.stdin.write(line)
82 return FastaFile(pipe.stdout).read_strings()
84 # vim: set et ts=4 sts=4 sw=4: