allpy
view allpy/base.py @ 646:612c618fb7b0
Changed __repr__ for sequence and monomer in allpy.base. Added row and
row_as_string methods to alignment. New faster blocks_finder works, but
glitches, though.
author | Boris Burkov <BurkovBA@gmail.com> |
---|---|
date | Tue, 07 Jun 2011 17:37:06 +0400 |
parents | a1307c0bb030 |
children | b35116e13f35 |
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
46 # Save the class in data.monomers so that it can be pickled
47 # Some names are not unique, we append underscores to them
48 # in order to fix it.
49 # XXX: this WILL fail with dna 0AV != rna A2M, which both have
50 # name "2'-O-METHYLADENOSINE 5'-(DIHYDROGEN PHOSPHATE)"
58 # We duplicate distinguished long names into Monomer itself, so that we
59 # can use Monomer.from_code3 to create the relevant type of monomer.
63 @classmethod
65 """Create all relevant subclasses of Monomer."""
69 @classmethod
71 """Create new monomer from 1-letter code."""
74 @classmethod
76 """Create new monomer from 3-letter code."""
79 @classmethod
81 """Create new monomer from full name."""
88 """Returns one-letter code"""
92 """Monomers within same monomer type are compared by code1."""
102 """Sequence of Monomers.
104 This behaves like list of monomer objects. In addition to standard list
105 behaviour, Sequence has the following attributes:
107 * name -- str with the name of the sequence
108 * description -- str with description of the sequence
109 * source -- str denoting source of the sequence
111 Any of them may be empty (i.e. hold empty string)
112 """
115 """Mapping of related types. SHOULD be redefined in subclasses."""
121 @classmethod
123 """Create sequence from a list of monomer objecst."""
133 @classmethod
135 """Create sequences from string of one-letter codes."""
148 """Returns sequence of one-letter codes."""
152 """Hash sequence by identity."""
156 """Alignment. It is a list of Columns."""
159 """Mapping of related types. SHOULD be redefined in subclasses."""
162 """Ordered list of sequences in alignment. Read, but DO NOT FIDDLE!"""
165 """Initialize empty alignment."""
169 # Alignment grow & IO methods
170 # ==============================
173 """Add sequence to alignment. Return self.
175 If sequence is too short, pad it with gaps on the right.
176 """
185 """Add row from a string of one-letter codes and gaps. Return self."""
193 ]
200 """Add row from row_as_list representation and sequence. Return self."""
209 """Pad alignment with empty columns on the right to width n."""
214 """Append sequences from file to alignment. Return self.
216 If sequences in file have gaps (detected as characters belonging to
217 `gaps` set), treat them accordingly.
218 """
226 """Write alignment in FASTA file as sequences with gaps."""
231 # Data access methods for alignment
232 # =================================
235 """Creates and returns temporary list of monomers and Nones"""
245 """Creates string of one-letter monomers' codes and gaps ("-")"""
257 """Return list of rows (temporary objects) in alignment.
259 Each row is a dictionary of { column : monomer }.
261 For gap positions there is no key for the column in row.
263 Each row has attribute `sequence` pointing to the sequence the row is
264 describing.
266 Modifications of row have no effect on the alignment.
267 """
268 # For now, the function returns a list rather than iterator.
269 # It is yet to see, whether memory performance here becomes critical,
270 # or is random access useful.
282 """Return list of rows (temporary objects) in alignment.
284 Each row here is a list of either monomer or None (for gaps).
286 Each row has attribute `sequence` pointing to the sequence of row.
288 Modifications of row have no effect on the alignment.
289 """
300 """Return list of string representation of rows in alignment.
302 Each row has attribute `sequence` pointing to the sequence of row.
304 `gap` is the symbol to use for gap.
305 """
320 """Return list of columns (temorary objects) in alignment.
322 Each column here is a list of either monomer or None (for gaps).
324 Items of column are sorted in the same way as alignment.sequences.
326 Modifications of column have no effect on the alignment.
327 """
336 # Alignment / Block editing methods
337 # =================================
340 """Remove all gaps from alignment and flush results to one side.
342 `whence` must be one of 'left', 'right' or 'center'
343 """
355 """Remove all empty columns."""
361 """Turn all row positions into gaps (but keep sequences intact)."""
367 """Replace contents of `dst` with those of `new`.
369 Replace contents of elements using function `merge(dst_el, new_le)`.
370 """
377 """Replace contents of sequences with those of `new` alignment."""
378 # XXX: we manually copy sequence contents here
379 # XXX: we only copy, overlapping parts and link to the rest
389 """Replace column contents with those of `new` alignment.
391 In other words: copy gap patterns from `new` to `self`.
393 `self.sequences` and `new.sequences` should have the same contents.
394 """
403 ]
408 """Replace alignment contents with those of other alignment."""
414 """Apply function to the alignment (or block); inject results back.
416 - `function(block)` must return block with same line order.
417 - if `copy_descriptions` is False, ignore new sequence names.
418 - if `copy_contents` is False, don't copy sequence contents too.
420 `function` (object) may have attributes `copy_descriptions` and
421 `copy_contents`, which override the same named arguments.
422 """
431 """Realign self.
433 I.e.: apply function to self to produce a new alignment, then update
434 self to have the same gap patterns as the new alignment.
436 This is the same as process(function, False, False)
437 """
442 """Column of alignment.
444 Column is a dict of { sequence : monomer }.
446 For sequences that have gaps in current row, given key is not present in
447 the column.
448 """
451 """Mapping of related types. SHOULD be redefined in subclasses."""
454 """Return hash by identity."""
458 """Block of alignment.
460 Block is an intersection of several rows & columns. (The collections of
461 rows and columns are represented as ordered lists, to retain display order
462 of Alignment or add ability to tweak it). Most of blocks look like
463 rectangular part of alignment if you shuffle alignment rows the right way.
464 """
467 """Alignment the block belongs to."""
470 """List of sequences in block."""
473 """List of columns in block."""
475 @classmethod
477 """Build new block from alignment.
479 If sequences are not given, the block uses all sequences in alignment.
481 If columns are not given, the block uses all columns in alignment.
483 In both cases we use exactly the list used in alignment, thus, if new
484 sequences or columns are added to alignment, the block tracks this too.
485 """
496 # vim: set ts=4 sts=4 sw=4 et: