allpy
view allpy/base.py @ 794:0d6c1227ede8
Automated merge with ssh://kodomo/allpy
author | Daniil Alexeyevsky <dendik@kodomo.fbb.msu.ru> |
---|---|
date | Wed, 13 Jul 2011 11:22:59 +0400 |
parents | fbf79117dd18 |
children | 91e73fb1ac79 |
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.
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.
64 @classmethod
66 """Create all relevant subclasses of Monomer."""
70 @classmethod
72 """Create new monomer from 1-letter code."""
77 @classmethod
79 """Create new monomer from 3-letter code."""
82 @classmethod
84 """Create new monomer from full name."""
91 """Returns one-letter code"""
95 """Monomers within same monomer type are compared by code1."""
105 """Sequence of Monomers.
107 This behaves like list of monomer objects. In addition to standard list
108 behaviour, Sequence has the following attributes:
110 * name -- str with the name of the sequence
111 * description -- str with description of the sequence
112 * source -- str denoting source of the sequence
114 Any of them may be empty (i.e. hold empty string)
115 """
118 """Mapping of related types. SHOULD be redefined in subclasses."""
128 @classmethod
130 """Create sequence from a list of monomer objecst."""
140 @classmethod
142 """Create sequences from string of one-letter codes."""
154 """Returns sequence of one-letter codes."""
158 """Hash sequence by identity."""
162 """Alignment. It is a list of Columns."""
165 """Mapping of related types. SHOULD be redefined in subclasses."""
168 """Ordered list of sequences in alignment. Read, but DO NOT FIDDLE!"""
171 """Initialize empty alignment."""
176 # Alignment grow & IO methods
177 # ==============================
180 """Add sequence to alignment. Return self.
182 If sequence is too short, pad it with gaps on the right.
183 """
192 """Add row from a string of one-letter codes and gaps. Return self."""
200 ]
207 """Add row from row_as_list representation and sequence. Return self."""
216 """Pad alignment with empty columns on the right to width n."""
221 """Append sequences from file to alignment. Return self.
223 If sequences in file have gaps (detected as characters belonging to
224 `gaps` set), treat them accordingly.
225 """
230 """Write alignment in FASTA file as sequences with gaps."""
234 # Data access methods for alignment
235 # =================================
238 """Return list of rows (temporary objects) in alignment.
240 Each row is a dictionary of { column : monomer }.
242 For gap positions there is no key for the column in row.
244 Each row has attribute `sequence` pointing to the sequence the row is
245 describing.
247 Modifications of row have no effect on the alignment.
248 """
249 # For now, the function returns a list rather than iterator.
250 # It is yet to see, whether memory performance here becomes critical,
251 # or is random access useful.
263 """Return list of rows (temporary objects) in alignment.
265 Each row here is a list of either monomer or None (for gaps).
267 Each row has attribute `sequence` pointing to the sequence of row.
269 Modifications of row have no effect on the alignment.
270 """
281 """Return list of string representation of rows in alignment.
283 Each row has attribute `sequence` pointing to the sequence of row.
285 `gap` is the symbol to use for gap.
286 """
301 """Return representaion of row as list with `Monomers` and `None`s."""
305 """Return string representaion of row in alignment.
307 String will have gaps represented by `gap` symbol (defaults to '-').
308 """
317 """Return list of columns (temorary objects) in alignment.
319 Each column here is a list of either monomer or None (for gaps).
321 Items of column are sorted in the same way as alignment.sequences.
323 Modifications of column have no effect on the alignment.
324 """
334 # Alignment / Block editing methods
335 # =================================
338 """Remove all gaps from alignment and flush results to one side.
340 `whence` must be one of 'left', 'right' or 'center'
341 """
353 """Remove all empty columns."""
359 """Turn all row positions into gaps (but keep sequences intact)."""
365 """Replace contents of `dst` with those of `new`.
367 Replace contents of elements using function `merge(dst_el, new_le)`.
368 """
375 """Replace contents of sequences with those of `new` alignment."""
376 # XXX: we manually copy sequence contents here
377 # XXX: we only copy, overlapping parts and link to the rest
387 """Replace column contents with those of `new` alignment.
389 In other words: copy gap patterns from `new` to `self`.
391 `self.sequences` and `new.sequences` should have the same contents.
392 """
401 ]
406 """Replace alignment contents with those of other alignment."""
412 """Apply function to the alignment (or block); inject results back.
414 - `function(block)` must return block with same line order.
415 - if `copy_descriptions` is False, ignore new sequence names.
416 - if `copy_contents` is False, don't copy sequence contents too.
418 `function` (object) may have attributes `copy_descriptions` and
419 `copy_contents`, which override the same named arguments.
420 """
429 """Realign self.
431 I.e.: apply function to self to produce a new alignment, then update
432 self to have the same gap patterns as the new alignment.
434 This is the same as process(function, False, False)
435 """
440 """Column of alignment.
442 Column is a dict of { sequence : monomer }.
444 For sequences that have gaps in current row, given key is not present in
445 the column.
446 """
449 """Mapping of related types. SHOULD be redefined in subclasses."""
452 """Return hash by identity."""
456 """Block of alignment.
458 Block is an intersection of several rows & columns. (The collections of
459 rows and columns are represented as ordered lists, to retain display order
460 of Alignment or add ability to tweak it). Most of blocks look like
461 rectangular part of alignment if you shuffle alignment rows the right way.
462 """
465 """Alignment the block belongs to."""
468 """List of sequences in block."""
471 """List of columns in block."""
473 @classmethod
475 """Build new block from alignment.
477 If sequences are not given, the block uses all sequences in alignment.
479 If columns are not given, the block uses all columns in alignment.
481 In both cases we use exactly the list used in alignment, thus, if new
482 sequences or columns are added to alignment, the block tracks this too.
483 """
495 """Base class for sequence and alignment markups.
497 We shall call either sequence or alignment a container. And we shall call
498 either monomers or columns elements respectively.
500 Markup behaves like a dictionary of [element] -> value.
502 Every container has a dictionary of [name] -> markup. It is Markup's
503 responsibility to add itself to this dictionary and to avoid collisions
504 while doing it.
505 """
508 """Name of markup elements"""
511 """Register self within container.
513 Assure the name is not taken before. If name is not given, look in the
514 class. Make sure we have some name at all.
515 """
523 """Recalculate markup values (if they are generated automatically)."""
524 pass
526 @classmethod
528 """Restore markup from `record`. (Used for loading from file).
530 `record` is a dict of all metadata and data related to one markup. All
531 keys and values in `record` are strings, markup must parse them itself.
533 Markup values should be stored in `record['markup']`, which is a list
534 of items separated with either `record['separator']` or a comma.
535 """
539 """Save markup to `record`, for saving to file.
541 For description of `record` see docstring for `from_record` method.
542 """
546 """Return list of elements in the container in proper order."""
550 """Return list of markup values in container."""
554 """Markup for sequence.
556 Behaves like a dictionary of [monomer] -> value. Value may be anything
557 or something specific, depending on subclass.
559 Actual values are stored in monomers themselves as attributes.
560 """
568 """Return list of monomers."""
572 """Return list of markup values, if every monomer is marked up."""
576 """Part of Mapping collection interface."""
582 """Part of Mapping collection interface."""
586 """Part of Mapping collection interface."""
590 """Part of Mapping collection interface."""
594 """Markupf for alignment.
596 Is a dictionary of [column] -> value. Value may be anything or something
597 specific, depending on subclass.
598 """
606 """Return a list of columns."""
610 """Return a list of makrup values, if every column is marked up."""
613 # vim: set ts=4 sts=4 sw=4 et: