[Biopython] slicing an alignment

Peter biopython at maubp.freeserve.co.uk
Fri Feb 12 22:50:35 UTC 2010


On Fri, Feb 12, 2010 at 6:35 PM, Alan Bergland <Alan_Bergland at brown.edu> wrote:
> Hi all,
>
>        I am a newbie, so my sincere apologies if this is a really naive
> question.  Is there an easy way to slice an alignment.  For instance, if I
> have imported a fasta alignment like this:
>
>        >seq1
>        ACCCGT
>        >seq2
>        ACCGGT
>
>        Is there a single commmand that would slice out site 2 from the whole
> alignment and return "C, C"?  Or, do I have to call each record and slice
> individually?

Hi Alan,

That is a good question - and making this easy via slicing and adding
alignments is on our to-do list. Your email is a nice reminder. See:
http://bugzilla.open-bio.org/show_bug.cgi?id=2551
http://bugzilla.open-bio.org/show_bug.cgi?id=2552

For now, this can be done by manually looping over each row, and
slicing and adding that record, then taking these edited records and
making a new alignment. Something like this (untested):

from Bio import SeqIO, AlignIO
old_alignment = AlignIO.read(open("example.aln"), "clustal")
cut_records = [rec[3:]+rec[4:] for rec in old_alignment]
new_alignment = SeqIO.to_alignment(cut_records)

Peter




More information about the Biopython mailing list