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

allpy

view allpy/fileio.py @ 861:0dbbf6403b7f

Removed pair_cores/html.htm and blocks3d/html.htm, whcih were autogenerated files. Looks like this closes #93.
author Daniil Alexeyevsky <dendik@kodomo.fbb.msu.ru>
date Thu, 21 Jul 2011 16:41:08 +0400
parents 18119191a4c8
children cfcbd13f6761
line source
1 import os
2 from subprocess import Popen, PIPE
3 from tempfile import NamedTemporaryFile
4 import util
6 def get_markups_class(classname):
7 """This ugly helper is to avoid bad untimely import loops."""
8 import markups
9 return getattr(markups, classname)
11 class File(object):
12 """Automatical file IO."""
13 def __new__(cls, file, format="fasta", **kw):
14 if format == "fasta":
15 return FastaFile(file, **kw)
16 elif format == 'markup':
17 return MarkupFile(file, **kw)
18 elif format.startswith('markup:'):
19 subformat = format.split(':',1)[1]
20 return MarkupFile(file, format=subformat, **kw)
21 else:
22 return EmbossFile(file, format, **kw)
24 class AlignmentFile(object):
25 """Some helpers."""
27 def __init__(self, file, format='fasta', gaps='-', wrap_column=70):
28 self.file = file
29 self.format = format
30 self.gaps = gaps
31 self.wrap_column = wrap_column
33 def write_alignment(self, alignment):
34 """Append alignment to the file."""
35 self.write_strings(
36 (row, row.sequence.name, row.sequence.description)
37 for row in alignment.rows_as_strings(self.gaps)
38 )
40 def read_alignment(self, alignment):
41 """Read alignment from the file."""
42 append_row = alignment.append_row_from_string
43 source = getattr(self.file, 'name', '')
44 for name, description, body in self.read_strings():
45 append_row(body, name, description, source, self.gaps)
47 class FastaFile(AlignmentFile):
48 """Fasta parser & writer."""
50 def write_string(self, string, name, description=''):
51 """Append one sequence to file."""
52 if description:
53 name += " " + description
54 self.file.write(">%s\n" % name)
55 if self.wrap_column:
56 while string:
57 self.file.write(string[:self.wrap_column]+"\n")
58 string = string[self.wrap_column:]
59 else:
60 self.file.write(string+"\n")
61 self.file.flush()
63 def write_strings(self, sequences):
64 """Write sequences to file.
66 Sequences are given as list of tuples (string, name, description).
67 """
68 for string, name, description in sequences:
69 self.write_string(string, name, description)
71 def read_parts(self):
72 """Read parts beginning with > in FASTA file.
74 This is a drop-in replacement for self.file.read().split("\n>")
75 It is required for markup format, which combines parts read with
76 different parsers. Python prohibits combining iterators and file.read
77 methods on the same file.
78 """
79 part = None
80 for line in self.file:
81 if line.startswith(">"):
82 if part: yield part
83 part = ""
84 part += line
85 if part: yield part
87 def read_strings(self):
88 for part in self.read_parts():
89 header, _, body = part.partition("\n")
90 header = header.lstrip(">")
91 name, _, description = header.partition(" ")
92 name = name.strip()
93 description = description.strip()
94 body = util.remove_each(body, " \n\r\t\v")
95 yield (name, description, body)
97 class MarkupFile(AlignmentFile):
98 """Parser & writer for our own marked alignment file format.
100 Marked alignment file consists of a list of records, separated with one or
101 more empty lines. Each record consists of type name, header and optional
102 contents. Type name is a line, containing just one word, describing the
103 record type. Header is a sequence of lines, each in format `key: value`.
104 Content, if present, is separated from header with an empty line.
106 Type names and header key names are case-insensitive and '-' and '_' in
107 them are equivalent.
109 Known record types now are:
111 - `alignment` -- this must be the last record in file for now
112 - `sequence_markup`
113 - `alignment_markup`
115 Example::
117 sequence-markup
118 sequence-name: cyb5_mouse
119 sequence-description:
120 name: pdb_residue_number
121 type: SequencePDBResidueNumberMarkup
122 markup: -,12,121,122,123,124,13,14,15,-,-,16
124 alignment-markup
125 name: geometrical_core
126 type: AlignmentGeometricalCoreMarkup
127 markup: -,-,-,-,+,+,+,-,-,-,+,+,-,-,-,-
129 alignment
130 format: fasta
132 > cyb5_mouse
133 seqvencemouse
134 """
136 _empty_line = ''
137 """Helper attribute for write_empty_line."""
139 def write_alignment(self, alignment):
140 """Write alignment to file."""
141 self.write_markups(alignment.markups, 'alignment_markup')
142 for sequence in alignment.sequences:
143 record = {
144 'sequence_name': sequence.name,
145 'sequence_description': sequence.description,
147 self.write_markups(sequence.markups, 'sequence_markup'