[Biopython-dev] [Bug 2554] Creating an Alignment from a list of SeqRecord objects

bugzilla-daemon at portal.open-bio.org bugzilla-daemon at portal.open-bio.org
Mon Jul 28 10:49:45 UTC 2008


http://bugzilla.open-bio.org/show_bug.cgi?id=2554





------- Comment #1 from biopython-bugzilla at maubp.freeserve.co.uk  2008-07-28 06:49 EST -------
There is an unwanted "not" in the code snippet in comment 0.

Here is a preliminary implementation of the revised __init__ method plus append
and extend (Bug 2533):

    def __init__(self, records, alphabet=None):
        """Initialize a new Alignment object.

        Arguments:
        records - A list (or iterator) of SeqRecord objects, whose sequences
                  are all the same length.  This an be an empty list.
        alphabet - The alphabet for the whole alignment, typically a gapped
                  alphabet, which should be a super-set of the individual
                  record alphabets.  If omitted, a consensus alphabet is used.

        NOTE - Earlier versions of Biopython only accepted a single argument,
        an alphabet.  This is still supported via a backwards compatible
        "hack" so as not to disrupt existing scripts and users.
        """
        if isinstance(records, Alphabet.Alphabet) \
        or isinstance(records, Alphabet.AlphabetEncoder):
            if alphabet is None :
                #Backwards compatible mode!
                alphabet = records
                records = []
            else :
                raise ValueError("Invalid records argument")
        if alphabet is not None :
            if not (isinstance(alphabet, Alphabet.Alphabet) \
            or isinstance(alphabet, Alphabet.AlphabetEncoder)):
                raise ValueError("Invalid alphabet argument")
            self._alphabet = alphabet
        else :
            #Default while we add sequences, will take a consensus later
            self._alphabet = Alphabet.single_letter_alphabet

        self._records = []
        self.extend(records)
        if alphabet is None :
            #No alphabet was given, take a consensus alphabet
            #TODO - Use a generator expression once we drop python 2.3:
            self.alphabet = Alphabet._consensus_alphabet([rec.seq.alphabet for
\
                                                          rec in
self._records])
        self._records = []

    def extend(self, records) :
        """Add more SeqRecord objects to the alignment as rows.

        They must all have the same length as the original alignment, and have
        alphabets compatible with the alignment's alphabet."""
        for rec in records :
            self.append(rec)

    def append(self, record) :
        """Add one more SeqRecord object to the alignment as a new row.

        This must have the same length as the original alignment (unless this
is
        the first record), and have an alphabet compatible with the alignment's
        alphabet."""
        if not isinstance(record, SeqRecord) :
            raise TypeError("New sequence is not a SeqRecord object")
        if self._records and len(record) <> self.get_alignment_length() :
            raise ValueError("New sequence is not of length %i" \
                             % self.get_alignment_length())
        #Using not self._alphabet.contains(record.seq.alphabet) needs fixing
        #for AlphabetEncoders (e.g. gapped versus ungapped).
        if not Alphabet._are_alphabets_compatible(self._alphabet, \
                                                  record.seq.alphabet) :
            raise ValueError("New sequence's alphabet is incompatible")
        self._records.append(record)

The unit tests look fine with this addition.  Of course, new tests to verify
this functionality explicitly should then be added (and I could take advantage
of this in Bio.AlignIO too).


-- 
Configure bugmail: http://bugzilla.open-bio.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.



More information about the Biopython-dev mailing list