# Annotating alignments and sequences

Section author: Peter Maxwell, Gavin Huttley

A Sequence with a couple of exons on it.

The corresponding sequence can be extracted either with slice notation or by asking the feature to do it, since the feature knows what sequence it belongs to.

Usually the only way to get a `Feature` object like `exon1` is to ask the sequence for it. There is one method for querying annotations by type and optionally by name:

If the sequence does not have a matching feature you get back an empty list, and slicing the sequence with that returns a sequence of length 0.

To construct a pseudo-feature covering (or excluding) multiple features, use `get_region_covering_all`:

eg: all the exon sequence:

or with slice notation:

Though `.get_region_covering_all` also guarantees no overlaps within the result, slicing does not:

You can use features, maps, slices or integers, but non-monotonic slices are not allowed:

Features are themselves sliceable:

When sequences are concatenated they keep their (non-overlapping) annotations:

Since features know their parents you can’t use a feature from one sequence to slice another:

Features are generally attached to the thing they annotate, but in those cases where a free-floating feature is created it can later be attached:

When dealing with sequences that can be reverse complemented (e.g. `DnaSequence`) features are not reversed. Features are considered to have strand specific meaning (.e.g CDS, exons) and so stay on their original strands. We create a sequence with a CDS that spans multiple exons, and show that after getting the reverse complement we have exactly the same result from getting the CDS annotation.

Sequence features can be accessed via a containing `Alignment`:

But these will be returned as alignment features with locations in alignment coordinates.

Similarly alignment features can be projected onto the aligned sequences, where they may end up falling across gaps:

We copy the annotations from another sequence,

even if the name is different.

If the feature lies outside the sequence being copied to, you get a lost span

You can copy to a sequence with a different name, in a different alignment if the feature lies within the length

If the sequence is shorter, again you get a lost span.

We consider cases where there are terminal gaps.

In this case, only those residues included within the feature are covered - note the omission of the T in `y` opposite the gap in `x`.

`Feature.as_one_span()`, is applied to the exon that straddles the gap in `x`. The result is we preserve that feature.

These properties also are consistently replicated with reverse complemented sequences.

Features can provide their coordinates, useful for custom analyses.

Annotated regions can be masked (observed sequence characters replaced by another), either through the sequence on which they reside or by projection from the alignment. Note that `mask_char` must be a valid character for the sequence `MolType`. Either the features (multiple can be named), or their shadow, can be masked.

We create an alignment with a sequence that has two different annotation types.

Each sequence should correctly mask either the single feature, it’s shadow, or the multiple features, or shadow.

The same methods can be applied to annotated Alignment’s.

It shouldn’t matter whether annotated coordinates are entered separately, or as a series.

We add annotations to the sequences as a series.

We add the annotations to the sequences one segment at a time.

These different constructions should generate the same output.

Annotations should be correctly masked, whether the sequence has been reverse complemented or not. We use the plus/minus strand CDS containing sequences created above.