Skip to content

Using IUPAC alphabets in mixed case Seq objects #1716

Closed

Description

@peterjc

Migrating from old RedMine issue https://redmine.open-bio.org/issues/2532 opened 30 June 2008 - cross reference #1040

Bio.Alphabets.IUPAC defines a number of alphabets with defined lists of valid letters which are in upper case ONLY.

Bio.Nexus and Bio.Sequencing.Phd create Seq objects which use these alphabets even with mixed case sequences.

This contradicts how I think the alphabet's .letters property is intended to be used (although currently this is not enforced by the Seq object).

I suggest either:

(a) Bio.Nexus etc switch to using generic DNA/RNA alphabets for any Seq objects including lower case letters (or more simply, all Seq objects).

(b) We add lower case and mixed case variants of the alphabet objects, and use the mixed case IUPAC alphabets in Bio.Nexus etc for the Seq objects.

There is also the option of (c) Extend the existing upper case only IUPAC alphabets to include lower case too, but I fear this could have unexpected side effects (e.g. where people looping over the expected set of letters).

Activity

  1. peterjc commented on Jul 5, 2018

    @peterjc
    MemberAuthor

    There were a few patches.

    I wrote this on 2008-12-22, mixed_case_IUPAC.patch Magnifier - Adding lower and mixed case IUPAC Alphabets (2.57 KB)

    Index: IUPAC.py
    ===================================================================
    RCS file: /home/repository/biopython/biopython/Bio/Alphabet/IUPAC.py,v
    retrieving revision 1.6
    diff -r1.6 IUPAC.py
    19c19,31
    < class ExtendedIUPACProtein(Alphabet.ProteinAlphabet):
    ---
    > 
    > class MixedcaseExtendedIUPACProtein(Alphabet.ProteinAlphabet):
    >     """Extended mixedcase IUPAC protein single letter alphabet including X etc.
    >     """
    >     letters = IUPACData.extended_protein_letters \
    >             + IUPACData.extended_protein_letters.lower()
    > 
    > class LowercaseExtendedIUPACProtein(MixedcaseExtendedIUPACProtein):
    >     """Extended lowercase IUPAC protein single letter alphabet including X etc.
    >     """
    >     letters = IUPACData.extended_protein_letters.lower()
    > 
    > class ExtendedIUPACProtein(MixedcaseExtendedIUPACProtein):
    39c51,59
    < class IUPACProtein(ExtendedIUPACProtein):
    ---
    > class MixedcaseIUPACProtein(MixedcaseExtendedIUPACProtein):
    >     """Mixedcase IUPAC protein single letter alphabet of the 20 standard amino acids."""
    >     letters = IUPACData.protein_letters + IUPACData.protein_letters.lower()
    > 
    > class LowercaseIUPACProtein(MixedcaseIUPACProtein, LowercaseExtendedIUPACProtein):
    >     """Lowercase IUPAC protein single letter alphabet of the 20 standard amino acids."""
    >     letters = IUPACData.protein_letters.lower()
    > 
    > class IUPACProtein(MixedcaseIUPACProtein, ExtendedIUPACProtein):
    49c69,79
    < class IUPACAmbiguousDNA(Alphabet.DNAAlphabet):
    ---
    > assert IUPACData.ambiguous_dna_letters == IUPACData.ambiguous_dna_letters.upper()
    > class MixedcaseIUPACAmbiguousDNA(Alphabet.DNAAlphabet):
    >     """Uppercase IUPAC ambiguous DNA."""
    >     letters = IUPACData.ambiguous_dna_letters \
    >             + IUPACData.ambiguous_dna_letters.lower()
    > 
    > class LowercaseIUPACAmbiguousDNA(MixedcaseIUPACAmbiguousDNA):
    >     """Lowercase IUPAC ambiguous DNA."""
    >     letters = IUPACData.ambiguous_dna_letters.lower()
    >     
    > class IUPACAmbiguousDNA(MixedcaseIUPACAmbiguousDNA):
    55c85,93
    < class IUPACUnambiguousDNA(IUPACAmbiguousDNA):
    ---
    > class MixedcaseIUPACUnambiguousDNA(MixedcaseIUPACAmbiguousDNA):
    >     """Uppercase IUPAC unambiguous DNA (letters GATC only)."""
    >     letters = IUPACData.unambiguous_dna_letters
    > 
    > class LowercaseIUPACUnambiguousDNA(MixedcaseIUPACUnambiguousDNA, LowercaseIUPACAmbiguousDNA):
    >     """Uppercase IUPAC unambiguous DNA (letters GATC only)."""
    >     letters = IUPACData.unambiguous_dna_letters
    > 
    > class IUPACUnambiguousDNA(MixedcaseIUPACUnambiguousDNA, IUPACAmbiguousDNA):
    64c102
    <     """Extended IUPAC DNA alphabet.
    ---
    >     """Uppercase extended IUPAC DNA alphabet.
  2. peterjc commented on Jul 5, 2018

    @peterjc
    MemberAuthor

    Another patch I wrote 2008-12-22 nexus_alphabets.patch Magnifier - Patch for Bio/Nexus/Nexus.py (non IUPAC) alphabet handling (1.83 KB)

    Index: Nexus.py
    ===================================================================
    RCS file: /home/repository/biopython/biopython/Bio/Nexus/Nexus.py,v
    retrieving revision 1.26
    diff -r1.26 Nexus.py
    18a19
    > from Bio import Alphabet
    522a524
    >         self.alphabet=Alphabet.SingleLetterAlphabet()
    695d696
    <                 self.alphabet=copy.deepcopy(IUPAC.ambiguous_dna)
    699d699
    <                 self.alphabet=copy.deepcopy(IUPAC.ambiguous_rna)
    703d702
    <                 self.alphabet=copy.deepcopy(IUPAC.protein)
    755a755,780
    >         #Setup the Alphabet object to cover all the valid letters (in preparation
    >         #for the Seq object checking this - see bug 2597).  We can't use the
    >         #default IUPAC alphabets (even if we did have a mixed case variants as
    >         #suggested on Bug 2532) as we want to include assorted additional characters
    >         #like "?" for missing.
    > 
    >         #Note - want to create a new instance, and then set it's letters property.
    >         #We do NOT want to alter a shared instance!  (See Bug 2380).
    >         if self.datatype == "dna" :
    >             a = Alphabet.DNAAlphabet()
    >         elif self.datatype == "rna" :
    >             a = Alphabet.RNAAlphabet()
    >         elif self.datatype == "protein" :
    >             a = Alphabet.ProteinAlphabet()
    >         else :
    >             a = Alphabet.SingleLetterAlphabet()
    >         #Can't set the letters property when create the object, so do it here:
    >         a.letters = self.valid_characters.replace(self.gap,"")
    > 
    >         if self.missing not in a.letters :
    >             #Just adding this directly...
    >             #TODO - Introduce a new alphabet decorator for this special meaning?
    >             a.letters += self.missing
    >         
    >         #Now add the gap char using the specific alphabet decorator,
    >         self.alphabet = Alphabet.Gapped(a, gap_char = self.gap)
  3. peterjc commented on Jul 5, 2018

    @peterjc
    MemberAuthor

    I wrote this on 2008-06-30, phd_alpha.patch Magnifier - Patch to Bio.Sequencing.Phd (759 Bytes)

    Index: Phd.py
    ===================================================================
    RCS file: /home/repository/biopython/biopython/Bio/Sequencing/Phd.py,v
    retrieving revision 1.7
    diff -r1.7 Phd.py
    22c22
    < from Bio.Alphabet import IUPAC
    ---
    > from Bio import Alphabet
    165c165,169
    <         self.data.seq = Seq.Seq(''.join([n[0] for n in self.data.sites]), IUPAC.IUPACAmbiguousDNA())
    ---
    >         #TODO - Introduce an upper and lower case version of IUPACAmbiguousDNA?
    >         #IUPACAmbiguousDNA is defined with only upper case letters, so we
    >         #shouldn't just blindly use that.
    >         self.data.seq = Seq.Seq(''.join([n[0] for n in self.data.sites]),
    >                                 Alphabet.generic_dna)
    219a224
    >         print repr(record.seq)
  4. peterjc commented on Jul 5, 2018

    @peterjc
    MemberAuthor

    Final patch was eee.txt Magnifier - Hack not to break on Ns for unknown bases in ESTs (1.29 KB) by Martin Mokrejs, 2008-07-18

    diff -u -w /usr/lib/python2.5/site-packages/Bio/Data/CodonTable.py.ori /usr/lib/python2.5/site-packages/Bio/Data/CodonTable.py
    --- /usr/lib/python2.5/site-packages/Bio/Data/CodonTable.py.ori 2008-07-19 02:02:30.000000000 +0200
    +++ /usr/lib/python2.5/site-packages/Bio/Data/CodonTable.py     2008-07-19 02:03:36.000000000 +0200
    @@ -632,8 +632,9 @@
                 self._cache[codon] = KeyError
                 raise KeyError, codon  # stop codon
             except TranslationError:
    -            self._cache[codon] = TranslationError
    -            raise TranslationError, codon  # does not code
    +            #self._cache[codon] = TranslationError
    +            #raise TranslationError, codon  # does not code or is unknown base N, R, etc.
    +           possible = ['X']
             assert len(possible) > 0, "unambiguous codons must code"
     
             # Hah!  Only one possible protein, so use it
    @@ -660,7 +661,7 @@
                 raise TranslationError, codon   # no valid translation
     
             # All of these are valid, so choose one
    -        # To be unique, sort by smallet ambiguity then alphabetically
    +        # To be unique, sort by smallest ambiguity then alphabetically
             # Can get this if "X" encodes for everything.
             def _sort(x, y, table = self.ambiguous_protein):
                 a = cmp(len(table[x]), len(table[y]))
    
  5. peterjc commented on Jul 24, 2020

    @peterjc
    MemberAuthor

    We're dropping Bio.Alphabets (see #2046), so this is moot now.

    Thank you @chebizarro for help with issue triage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions