allpy
view allpy/base.py @ 670:1a4a04829ba6
This is a half-rollback commit with base.py from central repo and my blocks_finder.py
author | Boris Burkov <BurkovBA@gmail.com> |
---|---|
date | Fri, 01 Jul 2011 11:40:00 +0400 |
parents | 2f016bfda114 |
children | b0d3c9413cf9 |
line source
8 # import this very module as means of having all related classes in one place
12 """Set of characters to recoginze as gaps when parsing alignment."""
15 """Monomer object."""
18 """Either of 'dna', 'rna', 'protein'."""
21 """Mapping of related types. SHOULD be redefined in subclasses."""
24 """A mapping from 1-letter code to Monomer subclass."""
27 """A mapping from 3-letter code to Monomer subclass."""
30 """A mapping from full monomer name to Monomer subclass."""
32 @classmethod
34 """Create new subclass of Monomer for given monomer type."""
36 pass
47 # Save the class in data.monomers so that it can be pickled
48 # Some names are not unique, we append underscores to them
49 # in order to fix it.
57 # We duplicate distinguished long names into Monomer itself, so that we
58 # can use Monomer.from_code3 to create the relevant type of monomer.
62 @classmethod
64 """Create all relevant subclasses of Monomer."""
68 @classmethod
70 """Create new monomer from 1-letter code."""
73 @classmethod
75 """Create new monomer from 3-letter code."""
78 @classmethod
80 """Create new monomer from full name."""
87 """Returns one-letter code"""
91 """Monomers within same monomer type are compared by code1."""
101 """Sequence of Monomers.
103 This behaves like list of monomer objects. In addition to standard list
104 behaviour, Sequence has the following attributes:
106 * name -- str with the name of the sequence
107 * description -- str with description of the sequence
108 * source -- str denoting source of the sequence
110 Any of them may be empty (i.e. hold empty string)
111 """
114 """Mapping of related types. SHOULD be redefined in subclasses."""
120 @classmethod
122 """Create sequence from a list of monomer objecst."""
132 @classmethod
134 """Create sequences from string of one-letter codes."""
147 """Returns sequence of one-letter codes."""
151 """Hash sequence by identity."""
155 """Alignment. It is a list of Columns."""
158 """Mapping of related types. SHOULD be redefined in subclasses."""
161 """Ordered list of sequences in alignment. Read, but DO NOT FIDDLE!"""
164 """Initialize empty alignment."""
168 # Alignment grow & IO methods
169 # ==============================
172 """Add sequence to alignment. Return self.
174 If sequence is too short, pad it with gaps on the right.
175 """
184 """Add row from a string of one-letter codes and gaps. Return self."""
192 ]
199 """Add row from row_as_list representation and sequence. Return self."""
208 """Pad alignment with empty columns on the right to width n."""
213 """Append sequences from file to alignment. Return self.
215 If sequences in file have gaps (detected as characters belonging to
216 `gaps` set), treat them accordingly.
217 """
225 """Write alignment in FASTA file as sequences with gaps."""
230 # Data access methods for alignment
231 # =================================
234 """Return list of rows (temporary objects) in alignment.
236 Each row is a dictionary of { column : monomer }.
238 For gap positions there is no key for the column in row.
240 Each row has attribute `sequence` pointing to the sequence the row is
241 describing.
243 Modifications of row have no effect on the alignment.
244 """
245 # For now, the function returns a list rather than iterator.
246 # It is yet to see, whether memory performance here becomes critical,
247 # or is random access useful.
259 """Return list of rows (temporary objects) in alignment.
261 Each row here is a list of either monomer or None (for gaps).
263 Each row has attribute `sequence` pointing to the sequence of row.
265 Modifications of row have no effect on the alignment.
266 """
277 """Return list of string representation of rows in alignment.
279 Each row has attribute `sequence` pointing to the sequence of row.
281 `gap` is the symbol to use for gap.
282 """
297 """Return representaion of row as list with `Monomers` and `None`s."""
301 """Return string representaion of row in alignment.
303 String will have gaps represented by `gap` symbol (defaults to '-').
304 """
313 """Return list of columns (temorary objects) in alignment.
315 Each column here is a list of either monomer or None (for gaps).
317 Items of column are sorted in the same way as alignment.sequences.
319 Modifications of column have no effect on the alignment.
320 """
330 # Alignment / Block editing methods
331 # =================================
334 """Remove all gaps from alignment and flush results to one side.
336 `whence` must be one of 'left', 'right' or 'center'
337 """
349 """Remove all empty columns."""
355 """Turn all row positions into gaps (but keep sequences intact)."""
361 """Replace contents of `dst` with those of `new`.
363 Replace contents of elements using function `merge(dst_el, new_le)`.
364 """
371 """Replace contents of sequences with those of `new` alignment."""
372 # XXX: we manually copy sequence contents here
373 # XXX: we only copy, overlapping parts and link to the rest
383 """Replace column contents with those of `new` alignment.
385 In other words: copy gap patterns from `new` to `self`.
387 `self.sequences` and `new.sequences` should have the same contents.
388 """
397 ]
402 """Replace alignment contents with those of other alignment."""
408 """Apply function to the alignment (or block); inject results back.
410 - `function(block)` must return block with same line order.
411 - if `copy_descriptions` is False, ignore new sequence names.
412 - if `copy_contents` is False, don't copy sequence contents too.
414 `function` (object) may have attributes `copy_descriptions` and
415 `copy_contents`, which override the same named arguments.
416 """
425 """Realign self.
427 I.e.: apply function to self to produce a new alignment, then update
428 self to have the same gap patterns as the new alignment.
430 This is the same as process(function, False, False)
431 """
436 """Column of alignment.
438 Column is a dict of { sequence : monomer }.
440 For sequences that have gaps in current row, given key is not present in
441 the column.
442 """
445 """Mapping of related types. SHOULD be redefined in subclasses."""
448 """Return hash by identity."""
452 """Block of alignment.
454 Block is an intersection of several rows & columns. (The collections of
455 rows and columns are represented as ordered lists, to retain display order
456 of Alignment or add ability to tweak it). Most of blocks look like
457 rectangular part of alignment if you shuffle alignment rows the right way.
458 """
461 """Alignment the block belongs to."""
464 """List of sequences in block."""
467 """List of columns in block."""
469 @classmethod
471 """Build new block from alignment.
473 If sequences are not given, the block uses all sequences in alignment.
475 If columns are not given, the block uses all columns in alignment.
477 In both cases we use exactly the list used in alignment, thus, if new
478 sequences or columns are added to alignment, the block tracks this too.
479 """
490 # vim: set ts=4 sts=4 sw=4 et: