allpy
view allpy/base.py @ 724:fd531580b9af
Merge between Burkov and others.
author | Boris Burkov <BurkovBA@gmail.com> |
---|---|
date | Fri, 08 Jul 2011 16:20:20 +0400 |
parents | b71f9c1d1509 83a40cd34923 |
children | 0a7917b4ca71 |
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."""
124 @classmethod
126 """Create sequence from a list of monomer objecst."""
136 @classmethod
138 """Create sequences from string of one-letter codes."""
151 """Returns sequence of one-letter codes."""
155 """Hash sequence by identity."""
160 """Alignment. It is a list of Columns."""
163 """Mapping of related types. SHOULD be redefined in subclasses."""
166 """Ordered list of sequences in alignment. Read, but DO NOT FIDDLE!"""
169 """Initialize empty alignment."""
174 # Alignment grow & IO methods
175 # ==============================
178 """Add sequence to alignment. Return self.
180 If sequence is too short, pad it with gaps on the right.
181 """
190 """Add row from a string of one-letter codes and gaps. Return self."""
198 ]
205 """Add row from row_as_list representation and sequence. Return self."""
214 """Pad alignment with empty columns on the right to width n."""
219 """Append sequences from file to alignment. Return self.
221 If sequences in file have gaps (detected as characters belonging to
222 `gaps` set), treat them accordingly.
223 """
228 """Write alignment in FASTA file as sequences with gaps."""
232 # Data access methods for alignment
233 # =================================
236 """Return list of rows (temporary objects) in alignment.
238 Each row is a dictionary of { column : monomer }.
240 For gap positions there is no key for the column in row.
242 Each row has attribute `sequence` pointing to the sequence the row is
243 describing.
245 Modifications of row have no effect on the alignment.
246 """
247 # For now, the function returns a list rather than iterator.
248 # It is yet to see, whether memory performance here becomes critical,
249 # or is random access useful.
261 """Return list of rows (temporary objects) in alignment.
263 Each row here is a list of either monomer or None (for gaps).
265 Each row has attribute `sequence` pointing to the sequence of row.
267 Modifications of row have no effect on the alignment.
268 """
279 """Return list of string representation of rows in alignment.
281 Each row has attribute `sequence` pointing to the sequence of row.
283 `gap` is the symbol to use for gap.
284 """
299 """Return representaion of row as list with `Monomers` and `None`s."""
303 """Return string representaion of row in alignment.
305 String will have gaps represented by `gap` symbol (defaults to '-').
306 """
315 """Return list of columns (temorary objects) in alignment.
317 Each column here is a list of either monomer or None (for gaps).
319 Items of column are sorted in the same way as alignment.sequences.
321 Modifications of column have no effect on the alignment.
322 """
332 # Alignment / Block editing methods
333 # =================================
336 """Remove all gaps from alignment and flush results to one side.
338 `whence` must be one of 'left', 'right' or 'center'
339 """
351 """Remove all empty columns."""
357 """Turn all row positions into gaps (but keep sequences intact)."""
363 """Replace contents of `dst` with those of `new`.
365 Replace contents of elements using function `merge(dst_el, new_le)`.
366 """
373 """Replace contents of sequences with those of `new` alignment."""
374 # XXX: we manually copy sequence contents here
375 # XXX: we only copy, overlapping parts and link to the rest
385 """Replace column contents with those of `new` alignment.
387 In other words: copy gap patterns from `new` to `self`.
389 `self.sequences` and `new.sequences` should have the same contents.
390 """
399 ]
404 """Replace alignment contents with those of other alignment."""
410 """Apply function to the alignment (or block); inject results back.
412 - `function(block)` must return block with same line order.
413 - if `copy_descriptions` is False, ignore new sequence names.
414 - if `copy_contents` is False, don't copy sequence contents too.
416 `function` (object) may have attributes `copy_descriptions` and
417 `copy_contents`, which override the same named arguments.
418 """
427 """Realign self.
429 I.e.: apply function to self to produce a new alignment, then update
430 self to have the same gap patterns as the new alignment.
432 This is the same as process(function, False, False)
433 """
438 """Column of alignment.
440 Column is a dict of { sequence : monomer }.
442 For sequences that have gaps in current row, given key is not present in
443 the column.
444 """
447 """Mapping of related types. SHOULD be redefined in subclasses."""
454 """Return hash by identity."""
463 #!!!!!!!!! READ HOW index OF LIST COMPARES OBJECTS AND BASIC TYPES
470 """Block of alignment.
472 Block is an intersection of several rows & columns. (The collections of
473 rows and columns are represented as ordered lists, to retain display order
474 of Alignment or add ability to tweak it). Most of blocks look like
475 rectangular part of alignment if you shuffle alignment rows the right way.
476 """
479 """Alignment the block belongs to."""
482 """List of sequences in block."""
485 """List of columns in block."""
487 @classmethod
489 """Build new block from alignment.
491 If sequences are not given, the block uses all sequences in alignment.
493 If columns are not given, the block uses all columns in alignment.
495 In both cases we use exactly the list used in alignment, thus, if new
496 sequences or columns are added to alignment, the block tracks this too.
497 """
509 """Base class for sequence and alignment markups.
511 We shall call either sequence or alignment a container. And we shall call
512 either monomers or columns elements respectively.
514 Markup behaves like a dictionary of [element] -> value.
516 Every container has a dictionary of [name] -> markup. It is Markup's
517 responsibility to add itself to this dictionary and to avoid collisions
518 while doing it.
519 """
522 """Name of markup elements"""
525 """Register self within container.
527 Assure the name is not taken before. If name is not given, look in the
528 class. Make sure we have some name at all.
529 """
537 """Recalculate markup values (if they are generated automatically)."""
538 pass
540 @classmethod
542 """Restore markup from `record`. (Used for loading from file).
544 `record` is a dict of all metadata and data related to one markup. All
545 keys and values in `record` are strings, markup must parse them itself.
547 Markup values should be stored in `record['markup']`, which is a list
548 of items separated with either `record['separator']` or a comma.
549 """
553 """Save markup to `record`, for saving to file.
555 For description of `record` see docstring for `from_record` method.
556 """
560 """Return list of elements in the container in proper order."""
564 """Return list of markup values in container."""
568 """Markup for sequence.
570 Behaves like a dictionary of [monomer] -> value. Value may be anything
571 or something specific, depending on subclass.
573 Actual values are stored in monomers themselves as attributes.
574 """
582 """Return list of monomers."""
586 """Return list of markup values, if every monomer is marked up."""
590 """Part of Mapping collection interface."""
596 """Part of Mapping collection interface."""
600 """Part of Mapping collection interface."""
604 """Part of Mapping collection interface."""
608 """Markupf for alignment.
610 Is a dictionary of [column] -> value. Value may be anything or something
611 specific, depending on subclass.
612 """
620 """Return a list of columns."""
624 """Return a list of makrup values, if every column is marked up."""
627 # vim: set ts=4 sts=4 sw=4 et: