allpy
view allpy/base.py @ 900:7ebba94eece2
MarkupIOMixin: added attribute quotes to allow quotation around markup serialized string; fixed implementation of SequenceCaseMarkup based on that; [closes #125]
author | Daniil Alexeyevsky <dendik@kodomo.fbb.msu.ru> |
---|---|
date | Tue, 11 Oct 2011 17:21:44 +0400 |
parents | d550aec04455 |
children | 61f28f17f027 116f5bfc39b8 |
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 """Common functions for alignment and sequence for dealing with markups.
106 """
109 """Hook to be called from __init__ of actual class."""
113 """Create a markup object, add to self. Return the created markup.
115 - `name` is name for markup in `self.markups` dictionary
116 - optional `markup_class` is class for created markup
117 - if optional `use_existing` is true, it is no error, if same named
118 markup already exists (in this case, nothing is changed)
119 - optional keyword arguments are passed on to the markup constructor
121 For user markups you have to specify `name` and `markup_class`,
122 for the standard automatical markups just `name` is enough.
123 """
124 # We have to import markups here, and not in the module header
125 # so as not to create bad import loops.
126 # `base` module is used extensively in `markups` for inherinance,
127 # so breaking the loop here seems a lot easier.
141 """Remove markup."""
146 """Sequence of Monomers.
148 This behaves like list of monomer objects. In addition to standard list
149 behaviour, Sequence has the following attributes:
151 * name -- str with the name of the sequence
152 * description -- str with description of the sequence
153 * source -- str denoting source of the sequence
155 Any of them may be empty (i.e. hold empty string)
156 """
159 """Mapping of related types. SHOULD be redefined in subclasses."""
162 """Description of object kind."""
172 @classmethod
174 """Create sequence from a list of monomer objecst."""
184 @classmethod
186 """Create sequences from string of one-letter codes."""
198 """Returns sequence of one-letter codes."""
202 """Hash sequence by identity."""
206 """Alignment. It is a list of Columns."""
209 """Mapping of related types. SHOULD be redefined in subclasses."""
212 """Ordered list of sequences in alignment. Read, but DO NOT FIDDLE!"""
215 """Description of object kind."""
218 """Initialize empty alignment."""
223 # Alignment grow & IO methods
224 # ==============================
227 """Add sequence to alignment. Return self.
229 If sequence is too short, pad it with gaps on the right.
230 """
239 """Add row from a string of one-letter codes and gaps. Return self."""
247 ]
254 """Add row from row_as_list representation and sequence. Return self."""
263 """Pad alignment with empty columns on the right to width n."""
268 """Append sequences from file to alignment. Return self.
270 If sequences in file have gaps (detected as characters belonging to
271 `gaps` set), treat them accordingly.
272 """
277 """Write alignment in FASTA file as sequences with gaps."""
281 # Data access methods for alignment
282 # =================================
285 """Return list of rows (temporary objects) in alignment.
287 Each row is a dictionary of { column : monomer }.
289 For gap positions there is no key for the column in row.
291 Each row has attribute `sequence` pointing to the sequence the row is
292 describing.
294 Modifications of row have no effect on the alignment.
295 """
296 # For now, the function returns a list rather than iterator.
297 # It is yet to see, whether memory performance here becomes critical,
298 # or is random access useful.
310 """Return list of rows (temporary objects) in alignment.
312 Each row here is a list of either monomer or None (for gaps).
314 Each row has attribute `sequence` pointing to the sequence of row.
316 Modifications of row have no effect on the alignment.
317 """
328 """Return list of string representation of rows in alignment.
330 Each row has attribute `sequence` pointing to the sequence of row.
332 `gap` is the symbol to use for gap.
333 """
348 """Return representaion of row as list with `Monomers` and `None`s."""
352 """Return string representaion of row in alignment.
354 String will have gaps represented by `gap` symbol (defaults to '-').
355 """
364 """Return list of columns (temorary objects) in alignment.
366 Each column here is a list of either monomer or None (for gaps).
368 Items of column are sorted in the same way as alignment.sequences.
370 Modifications of column have no effect on the alignment.
371 """
381 # Alignment / Block editing methods
382 # =================================
385 """Remove all gaps from alignment and flush results to one side.
387 `whence` must be one of 'left', 'right' or 'center'
388 """
390 "aln.flush('left') is deprecated in favor of aln.realign(Left())"
391 )
403 """Remove all empty columns."""
409 """Turn all row positions into gaps (but keep sequences intact)."""
415 """Replace contents of `dst` with those of `new`.
417 Replace contents of elements using function `merge(dst_el, new_le)`.
418 """
425 """Replace contents of sequences with those of `new` alignment."""
426 # XXX: we manually copy sequence contents here
427 # XXX: we only copy, overlapping parts and link to the rest
437 """Replace column contents with those of `new` alignment.
439 In other words: copy gap patterns from `new` to `self`.
441 `self.sequences` and `new.sequences` should have the same contents.
442 """
451 ]
456 """Replace alignment contents with those of other alignment."""
462 """Apply function to the alignment (or block); inject results back.
464 - `function(block)` must return block with same line order.
465 - if `copy_descriptions` is False, ignore new sequence names.
466 - if `copy_contents` is False, don't copy sequence contents too.
468 `function` (object) may have attributes `copy_descriptions` and
469 `copy_contents`, which override the same named arguments.
470 """
472 "aln.process() is deprecated and may cause damage to your"
473 " alignment! It will be removed from 1.4.1 realease."
474 " Use aln.realign() instead."
475 )
484 """Realign self.
486 I.e.: apply function to self to produce a new alignment, then update
487 self to have the same gap patterns as the new alignment.
489 This is the same as process(function, False, False)
490 """
495 """Column of alignment.
497 Column is a dict of { sequence : monomer }.
499 For sequences that have gaps in current row, given key is not present in
500 the column.
501 """
504 """Mapping of related types. SHOULD be redefined in subclasses."""
507 """Return hash by identity."""
511 """Block of alignment.
513 Block is an intersection of several rows & columns. (The collections of
514 rows and columns are represented as ordered lists, to retain display order
515 of Alignment or add ability to tweak it). Most of blocks look like
516 rectangular part of alignment if you shuffle alignment rows the right way.
517 """
520 """Alignment the block belongs to."""
523 """List of sequences in block."""
526 """List of columns in block."""
528 @classmethod
530 """Build new block from alignment.
532 If sequences are not given, the block uses all sequences in alignment.
534 If columns are not given, the block uses all columns in alignment.
536 In both cases we use exactly the list used in alignment, thus, if new
537 sequences or columns are added to alignment, the block tracks this too.
538 """
550 """Base class for sequence and alignment markups.
552 We shall call either sequence or alignment a container. And we shall call
553 either monomers or columns elements respectively.
555 Markup behaves like a dictionary of [element] -> value.
557 Every container has a dictionary of [name] -> markup. It is Markup's
558 responsibility to add itself to this dictionary and to avoid collisions
559 while doing it.
560 """
563 """Name of markup elements."""
566 """If set to false, fileio should not save this markup."""
569 """Markup takes mandatory container and name and optional kwargs.
571 Markups should never be created by the user. They are created by
572 Sequence or Alignment.
573 """
579 """Recalculate markup values (if they are generated automatically)."""
580 pass
583 """Remove the traces of markup object. Do not call this yourself!"""
584 pass
586 @classmethod
588 """Restore markup from `record`. (Used for loading from file).
590 `record` is a dict of all metadata and data related to one markup. All
591 keys and values in `record` are strings, markup must parse them itself.
593 Markup values should be stored in `record['markup']`, which is a list
594 of items separated with either `record['separator']` or a comma.
595 """
599 """Save markup to `record`, for saving to file.
601 For description of `record` see docstring for `from_record` method.
602 """
606 """Return list of elements in the container in proper order."""
610 """Return list of markup values in container.
612 Possible arguments:
614 - `map` -- a function, applied to each existing value
615 - `default` -- a value to return for non-existing values
617 If `default` is not specified, the function fails on markups that do
618 not have all of the values set.
619 """
630 """Markup for sequence.
632 Behaves like a dictionary of [monomer] -> value. Value may be anything
633 or something specific, depending on subclass.
635 Actual values are stored in monomers themselves as attributes.
636 """
645 """Remove the traces of markup object. Do not call this yourself!"""
650 """Return list of monomers."""
654 """Part of Mapping collection interface."""
660 """Part of Mapping collection interface."""
664 """Part of Mapping collection interface."""
668 """Part of Mapping collection interface."""
672 """Part of Mapping collection interface."""
676 """Markupf for alignment.
678 Is a dictionary of [column] -> value. Value may be anything or something
679 specific, depending on subclass.
680 """
689 """Return a list of columns."""
692 # vim: set ts=4 sts=4 sw=4 et: