allpy
view allpy/base.py @ 537:364232e42888
Reimplemented flush(direction) as processor
| author | Daniil Alexeyevsky <dendik@kodomo.fbb.msu.ru> |
|---|---|
| date | Mon, 28 Feb 2011 21:17:41 +0300 |
| parents | 5dfb9b9761d5 |
| children | 737b52785e5e |
line source
7 # import this very module as means of having all related classes in one place
11 """Set of characters to recoginze as gaps when parsing alignment."""
14 """Monomer object."""
17 """Either of 'dna', 'rna', 'protein'."""
20 """Mapping of related types. SHOULD be redefined in subclasses."""
23 """A mapping from 1-letter code to Monomer subclass."""
26 """A mapping from 3-letter code to Monomer subclass."""
29 """A mapping from full monomer name to Monomer subclass."""
31 @classmethod
33 """Create new subclass of Monomer for given monomer type."""
35 pass
48 # We duplicate distinguished long names into Monomer itself, so that we
49 # can use Monomer.from_code3 to create the relevant type of monomer.
53 @classmethod
55 """Create all relevant subclasses of Monomer."""
59 @classmethod
61 """Create new monomer from 1-letter code."""
64 @classmethod
66 """Create new monomer from 3-letter code."""
69 @classmethod
71 """Create new monomer from full name."""
78 """Returns one-letter code"""
82 """Monomers within same monomer type are compared by code1."""
90 """Sequence of Monomers.
92 This behaves like list of monomer objects. In addition to standard list
93 behaviour, Sequence has the following attributes:
95 * name -- str with the name of the sequence
96 * description -- str with description of the sequence
97 * source -- str denoting source of the sequence
99 Any of them may be empty (i.e. hold empty string)
100 """
103 """Mapping of related types. SHOULD be redefined in subclasses."""
109 @classmethod
111 """Create sequence from a list of monomer objecst."""
121 @classmethod
123 """Create sequences from string of one-letter codes."""
132 """Returns sequence of one-letter codes."""
136 """Hash sequence by identity."""
140 """Alignment. It is a list of Columns."""
143 """Mapping of related types. SHOULD be redefined in subclasses."""
146 """Ordered list of sequences in alignment. Read, but DO NOT FIDDLE!"""
149 """Initialize empty alignment."""
153 # Alignment grow & IO methods
154 # ==============================
157 """Add sequence to alignment. Return self.
159 If sequence is too short, pad it with gaps on the right.
160 """
169 """Add row from a string of one-letter codes and gaps. Return self."""
177 ]
184 """Add row from row_as_list representation and sequence. Return self."""
193 """Pad alignment with empty columns on the right to width n."""
198 """Append sequences from file to alignment. Return self.
200 If sequences in file have gaps (detected as characters belonging to
201 `gaps` set), treat them accordingly.
202 """
215 """Write alignment in FASTA file as sequences with gaps."""
231 # Data access methods for alignment
232 # =================================
235 """Return list of rows (temporary objects) in alignment.
237 Each row is a dictionary of { column : monomer }.
239 For gap positions there is no key for the column in row.
241 Each row has attribute `sequence` pointing to the sequence the row is
242 describing.
244 Modifications of row have no effect on the alignment.
245 """
246 # For now, the function returns a list rather than iterator.
247 # It is yet to see, whether memory performance here becomes critical,
248 # or is random access useful.
260 """Return list of rows (temporary objects) in alignment.
262 Each row here is a list of either monomer or None (for gaps).
264 Each row has attribute `sequence` pointing to the sequence of row.
266 Modifications of row have no effect on the alignment.
267 """
278 """Return list of columns (temorary objects) in alignment.
280 Each column here is a list of either monomer or None (for gaps).
282 Items of column are sorted in the same way as alignment.sequences.
284 Modifications of column have no effect on the alignment.
285 """
294 # Alignment / Block editing methods
295 # =================================
298 """Remove all gaps from alignment and flush results to one side.
300 `whence` must be one of 'left', 'right' or 'center'
301 """
313 """Remove all empty columns."""
319 """Turn all row positions into gaps (but keep sequences intact)."""
325 """Replace contents of `dst` with those of `new`.
327 Replace contents of elements using function `merge(dst_el, new_le)`.
328 """
335 """Replace contents of sequences with those of `new` alignment."""
336 # XXX: we manually copy sequence contents here
337 # XXX: we only copy, overlapping parts and link to the rest
347 """Replace column contents with those of `new` alignment.
349 In other words: copy gap patterns from `new` to `self`.
351 `self.sequences` and `new.sequences` should have the same contents.
352 """
361 ]
366 """Replace alignment contents with those of other alignment."""
372 """Apply function to the alignment (or block); inject results back.
374 - `function(block)` must return block with same line order.
375 - if `copy_descriptions` is False, ignore new sequence names.
376 - if `copy_contents` is False, don't copy sequence contents too.
378 `function` (object) may have attributes `copy_descriptions` and
379 `copy_contents`, which override the same named arguments.
380 """
389 """Realign self.
391 I.e.: apply function to self to produce a new alignment, then update
392 self to have the same gap patterns as the new alignment.
394 This is the same as process(function, False, False)
395 """
400 """Column of alignment.
402 Column is a dict of { sequence : monomer }.
404 For sequences that have gaps in current row, given key is not present in
405 the column.
406 """
409 """Mapping of related types. SHOULD be redefined in subclasses."""
412 """Return hash by identity."""
416 """Block of alignment.
418 Block is an intersection of several rows & columns. (The collections of
419 rows and columns are represented as ordered lists, to retain display order
420 of Alignment or add ability to tweak it). Most of blocks look like
421 rectangular part of alignment if you shuffle alignment rows the right way.
422 """
425 """Alignment the block belongs to."""
428 """List of sequences in block."""
431 """List of columns in block."""
433 @classmethod
435 """Build new block from alignment.
437 If sequences are not given, the block uses all sequences in alignment.
439 If columns are not given, the block uses all columns in alignment.
441 In both cases we use exactly the list used in alignment, thus, if new
442 sequences or columns are added to alignment, the block tracks this too.
443 """
454 # vim: set ts=4 sts=4 sw=4 et:
