ó
ˆé¯Ic           @   sl   d  Z  d d l m Z d d l m Z d d l m Z d f  d „  ƒ  YZ d „  Z e	 d k rh e ƒ  n  d	 S(
   s†   
Contains classes to deal with generic sequence alignment stuff not
specific to a particular program or format.

classes:
o Alignment
iÿÿÿÿ(   t   Seq(   t	   SeqRecord(   t   Alphabett	   Alignmentc           B   s•   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d d d d „ Z d „  Z d „  Z RS(   s¢   Represent a set of alignments.

    This is a base class to represent alignments, which can be subclassed
    to deal with an alignment in a specific format.
    c         C   sI   t  | t j ƒ p! t  | t j ƒ s3 t d ƒ ‚ n  | |  _ g  |  _ d S(   s£  Initialize a new Alignment object.

        Arguments:
        o alphabet - The alphabet to use for the sequence objects that are
        created. This alphabet must be a gapped type.

        e.g.
        >>> from Bio.Alphabet import IUPAC, Gapped
        >>> align = Alignment(Gapped(IUPAC.unambiguous_dna, "-"))
        >>> align.add_sequence("Alpha", "ACTGCTAGCTAG")
        >>> align.add_sequence("Beta",  "ACT-CTAGCTAG")
        >>> align.add_sequence("Gamma", "ACTGCTAGATAG")
        >>> print align
        Gapped(IUPACUnambiguousDNA(), '-') alignment with 3 rows and 12 columns
        ACTGCTAGCTAG Alpha
        ACT-CTAGCTAG Beta
        ACTGCTAGATAG Gamma
        s   Invalid alphabet argumentN(   t
   isinstanceR   t   AlphabetEncodert
   ValueErrort	   _alphabett   _records(   t   selft   alphabet(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyt   __init__   s
    	c         C   sO   t  | j ƒ d k r) d | j | j f Sd | j d  | j d | j f Sd S(   sŒ   Returns a truncated string representation of a SeqRecord (PRIVATE).

        This is a PRIVATE function used by the __str__ method.
        i2   s   %s %ss
   %s...%s %si,   iýÿÿÿN(   t   lent   seqt   id(   R	   t   record(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyt	   _str_line5   s    c         C   sÖ   t  |  j ƒ } d t |  j ƒ | |  j ƒ  f g } | d k ro | j g  |  j D] } |  j | ƒ ^ qP ƒ nZ | j g  |  j d  D] } |  j | ƒ ^ qƒ ƒ | j d ƒ | j |  j |  j d ƒ ƒ d j | ƒ S(   sC  Returns a multi-line string summary of the alignment.

        This output is intended to be readable, but large alignments are
        shown truncated.  A maximum of 20 rows (sequences) and 50 columns
        are shown, with the record identifiers.  This should fit nicely on a
        single screen.  e.g.

        >>> from Bio.Alphabet import IUPAC, Gapped
        >>> align = Alignment(Gapped(IUPAC.unambiguous_dna, "-"))
        >>> align.add_sequence("Alpha", "ACTGCTAGCTAG")
        >>> align.add_sequence("Beta",  "ACT-CTAGCTAG")
        >>> align.add_sequence("Gamma", "ACTGCTAGATAG")
        >>> print align
        Gapped(IUPACUnambiguousDNA(), '-') alignment with 3 rows and 12 columns
        ACTGCTAGCTAG Alpha
        ACT-CTAGCTAG Beta
        ACTGCTAGATAG Gamma

        See also the alignment's format method.
        s(   %s alignment with %i rows and %i columnsi   i   s   ...iÿÿÿÿs   
(	   R   R   t   strR   t   get_alignment_lengtht   extendR   t   appendt   join(   R	   t   rowst   linest   rec(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyt   __str__@   s    "/0c         C   s8   d |  j  t |  j ƒ |  j ƒ  t |  j ƒ t |  ƒ f S(   s÷  Returns a representation of the object for debugging.

        The representation cannot be used with eval() to recreate the object,
        which is usually possible with simple python ojects.  For example:

        <Bio.Align.Generic.Alignment instance (2 records of length 14,
        SingleLetterAlphabet()) at a3c184c>

        The hex string is the memory address of the object, see help(id).
        This provides a simple way to visually distinguish alignments of
        the same size.
        s1   <%s instance (%i records of length %i, %s) at %x>(   t	   __class__R   R   R   t   reprR   R   (   R	   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyt   __repr__`   s    c         C   s   |  j  | ƒ S(   sû  Returns the alignment as a string in the specified file format.

        The format should be a lower case string supported as an output
        format by Bio.AlignIO (such as "fasta", "clustal", "phylip",
        "stockholm", etc), which is used to turn the alignment into a
        string.

        e.g.
        >>> from Bio.Alphabet import IUPAC, Gapped
        >>> align = Alignment(Gapped(IUPAC.unambiguous_dna, "-"))
        >>> align.add_sequence("Alpha", "ACTGCTAGCTAG")
        >>> align.add_sequence("Beta",  "ACT-CTAGCTAG")
        >>> align.add_sequence("Gamma", "ACTGCTAGATAG")
        >>> print align.format("fasta")
        >Alpha
        ACTGCTAGCTAG
        >Beta
        ACT-CTAGCTAG
        >Gamma
        ACTGCTAGATAG
        <BLANKLINE>
        >>> print align.format("phylip")
         3 12
        Alpha      ACTGCTAGCT AG
        Beta       ACT-CTAGCT AG
        Gamma      ACTGCTAGAT AG
        <BLANKLINE>

        For Python 2.6, 3.0 or later see also the built in format() function.
        (   t
   __format__(   R	   t   format(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyR   w   s    !c         C   s]   | rO d d l  m  } d d l m } | ƒ  } | j |  g | | ƒ | j ƒ  St |  ƒ Sd S(   s7  Returns the alignment as a string in the specified file format.

        This method supports the python format() function added in
        Python 2.6/3.0.  The format_spec should be a lower case
        string supported by Bio.AlignIO as an output file format.
        See also the alignment's format() method.iÿÿÿÿ(   t   StringIO(   t   AlignION(   R   t   BioR    t   writet   getvalueR   (   R	   t   format_specR   R    t   handle(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyR   ›   s    	
c         C   s   |  j  S(   sF  Return all of the sequences involved in the alignment.

        The return value is a list of SeqRecord objects.

        This method is semi-obsolete, as the Alignment object itself offers
        much of the functionality of a list of SeqRecord objects (e.g.
        iteration or slicing to create a sub-alignment).
        (   R   (   R	   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyt   get_all_seqs¬   s    	c         C   s   t  |  j ƒ S(   s.  Iterate over alignment rows as SeqRecord objects.

        e.g.
        >>> from Bio.Alphabet import IUPAC, Gapped
        >>> align = Alignment(Gapped(IUPAC.unambiguous_dna, "-"))
        >>> align.add_sequence("Alpha", "ACTGCTAGCTAG")
        >>> align.add_sequence("Beta",  "ACT-CTAGCTAG")
        >>> align.add_sequence("Gamma", "ACTGCTAGATAG")
        >>> for record in align :
        ...    print record.id
        ...    print record.seq
        Alpha
        ACTGCTAGCTAG
        Beta
        ACT-CTAGCTAG
        Gamma
        ACTGCTAGATAG
        (   t   iterR   (   R	   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyt   __iter__·   s    c         C   s   |  j  | j S(   sH  Retrieve a sequence by row number (OBSOLETE).

        Returns:
        o A Seq object for the requested sequence.

        Raises:
        o IndexError - If the specified number is out of range.

        NOTE: This is a legacy method.  In new code where you need to access
        the rows of the alignment (i.e. the sequences) consider iterating
        over them or accessing them as SeqRecord objects.  e.g.

        for record in alignment :
            print record.id
            print record.seq
        first_record = alignment[0]
        last_record = alignment[-1]
        (   R   R   (   R	   t   number(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyt   get_seq_by_numÌ   s    c         C   s   t  |  j ƒ S(   s€  Returns the number of sequences in the alignment.

        Use len(alignment) to get the number of sequences (i.e. the number of
        rows), and alignment.get_alignment_length() to get the length of the
        longest sequence (i.e. the number of columns).

        This is easy to remember if you think of the alignment as being like a
        list of SeqRecord objects.
        (   R   R   (   R	   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyt   __len__á   s    
c         C   sE   d } x8 |  j  D]- } t | j ƒ | k r t | j ƒ } q q W| S(   sç  Return the maximum length of the alignment.

        All objects in the alignment should (hopefully) have the same
        length. This function will go through and find this length
        by finding the maximum length of sequences in the alignment.

        >>> from Bio.Alphabet import IUPAC, Gapped
        >>> align = Alignment(Gapped(IUPAC.unambiguous_dna, "-"))
        >>> align.add_sequence("Alpha", "ACTGCTAGCTAG")
        >>> align.add_sequence("Beta",  "ACT-CTAGCTAG")
        >>> align.add_sequence("Gamma", "ACTGCTAGATAG")
        >>> align.get_alignment_length()
        12

        If you want to know the number of sequences in the alignment,
        use len(align) instead:

        >>> len(align)
        3
        
        i    (   R   R   R   (   R	   t
   max_lengthR   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyR   í   s
    g      ð?c         C   sw   t  | |  j ƒ } t | d | d | ƒ} | r@ | | j d <n  | rV | | j d <n  | | j d <|  j j | ƒ d S(   sý  Add a sequence to the alignment.

        This doesn't do any kind of alignment, it just adds in the sequence
        object, which is assumed to be prealigned with the existing
        sequences.

        Arguments:
        o descriptor - The descriptive id of the sequence being added.
                       This will be used as the resulting SeqRecord's
                       .id property (and, for historical compatibility,
                       also the .description property)
        o sequence - A string with sequence info.
        o start - You can explicitly set the start point of the sequence.
        This is useful (at least) for BLAST alignments, which can just
        be partial alignments of sequences.
        o end - Specify the end of the sequence, which is important
        for the same reason as the start.
        o weight - The weight to place on the sequence in the alignment.
        By default, all sequences have the same weight. (0.0 => no weight,
        1.0 => highest weight)
        R   t   descriptiont   startt   endt   weightN(   R    R   R   t   annotationsR   R   (   R	   t
   descriptort   sequenceR.   R/   R0   t   new_seqt
   new_record(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyt   add_sequence  s    			c         C   sS   d } | d k r$ | |  j  ƒ  k s* t ‚ x" |  j D] } | | j | 7} q4 W| S(   s»  Returns a string containing a given column.

        e.g.
        >>> from Bio.Alphabet import IUPAC, Gapped
        >>> align = Alignment(Gapped(IUPAC.unambiguous_dna, "-"))
        >>> align.add_sequence("Alpha", "ACTGCTAGCTAG")
        >>> align.add_sequence("Beta",  "ACT-CTAGCTAG")
        >>> align.add_sequence("Gamma", "ACTGCTAGATAG")
        >>> align.get_column(0)
        'AAA'
        >>> align.get_column(3)
        'G-G'
        t    i    (   R   t   AssertionErrorR   R   (   R	   t   colt   col_strR   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyt
   get_column>  s
    $c         C   s   t  | t ƒ r |  j | St  | t ƒ rL t |  j ƒ } |  j | | _ | St | ƒ d k rq t d d ƒ ‚ n t d ƒ ‚ d S(   sz  Access part of the alignment.

        We'll use the following example alignment here for illustration:

        >>> from Bio.Alphabet import IUPAC, Gapped
        >>> align = Alignment(Gapped(IUPAC.unambiguous_dna, "-"))
        >>> align.add_sequence("Alpha",  "ACTGCTAGCTAG")
        >>> align.add_sequence("Beta",   "ACT-CTAGCTAG")
        >>> align.add_sequence("Gamma",  "ACTGCTAGATAG")
        >>> align.add_sequence("Delta",  "ACTGCTTGCTAG")
        >>> align.add_sequence("Epsilon","ACTGCTTGATAG")
        
        You can access a row of the alignment as a SeqRecord using an integer
        index (think of the alignment as a list of SeqRecord objects here):

        >>> first_record = align[0]
        >>> print first_record.id, first_record.seq
        Alpha ACTGCTAGCTAG
        >>> last_record = align[-1]
        >>> print last_record.id, last_record.seq
        Epsilon ACTGCTTGATAG

        You can also access use python's slice notation to create a sub-alignment
        containing only some of the SeqRecord objects:

        >>> sub_alignment = align[2:5]
        >>> print sub_alignment
        Gapped(IUPACUnambiguousDNA(), '-') alignment with 3 rows and 12 columns
        ACTGCTAGATAG Gamma
        ACTGCTTGCTAG Delta
        ACTGCTTGATAG Epsilon

        This includes support for a step, i.e. align[start:end:step], which
        can be used to select every second sequence:

        >>> sub_alignment = align[::2]
        >>> print sub_alignment
        Gapped(IUPACUnambiguousDNA(), '-') alignment with 3 rows and 12 columns
        ACTGCTAGCTAG Alpha
        ACTGCTAGATAG Gamma
        ACTGCTTGATAG Epsilon

        Or to get a copy of the alignment with the rows in reverse order:

        >>> rev_alignment = align[::-1]
        >>> print rev_alignment
        Gapped(IUPACUnambiguousDNA(), '-') alignment with 5 rows and 12 columns
        ACTGCTTGATAG Epsilon
        ACTGCTTGCTAG Delta
        ACTGCTAGATAG Gamma
        ACT-CTAGCTAG Beta
        ACTGCTAGCTAG Alpha

        Right now, these are the ONLY indexing operations supported.  The use of
        a second column based index is under discussion for a future update.
        i   s3   Row and Column indexing is not currently supported,s   but may be in future.s   Invalid index type.N(   R   t   intR   t   sliceR   R   R   t	   TypeError(   R	   t   indext	   sub_align(    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyt   __getitem__S  s    9N(   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R&   R(   R*   R+   R   t   NoneR6   R;   RA   (    (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyR      s    			 		$						2	c          C   s$   d GHd d l  }  |  j ƒ  d GHd S(   s"   Run the Bio.Seq module's doctests.s   Running doctests...iÿÿÿÿNt   Done(   t   doctestt   testmod(   RG   (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyt   _testž  s    
t   __main__N(
   RD   t   Bio.SeqR    t   Bio.SeqRecordR   R!   R   R   RI   RB   (    (    (    s„   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/Align/Generic.pyt   <module>   s   ÿ Š	