ó
¾xÂIc           @   s{   d  Z  d d l m Z d d l m Z m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z e	 d	 k rw e ƒ  n  d
 S(   s  
Bio.AlignIO support for the "stockholm" format (used in the PFAM database).

You are expected to use this module via the Bio.AlignIO functions (or the
Bio.SeqIO functions if you want to work directly with the gapped sequences).

For example, consider a Stockholm alignment file containing the following:

    # STOCKHOLM 1.0
    #=GC SS_cons       .................<<<<<<<<...<<<<<<<........>>>>>>>..
    AP001509.1         UUAAUCGAGCUCAACACUCUUCGUAUAUCCUC-UCAAUAUGG-GAUGAGGGU
    #=GR AP001509.1 SS -----------------<<<<<<<<---..<<-<<-------->>->>..--
    AE007476.1         AAAAUUGAAUAUCGUUUUACUUGUUUAU-GUCGUGAAU-UGG-CACGA-CGU
    #=GR AE007476.1 SS -----------------<<<<<<<<-----<<.<<-------->>.>>----

    #=GC SS_cons       ......<<<<<<<.......>>>>>>>..>>>>>>>>...............
    AP001509.1         CUCUAC-AGGUA-CCGUAAA-UACCUAGCUACGAAAAGAAUGCAGUUAAUGU
    #=GR AP001509.1 SS -------<<<<<--------->>>>>--->>>>>>>>---------------
    AE007476.1         UUCUACAAGGUG-CCGG-AA-CACCUAACAAUAAGUAAGUCAGCAGUGAGAU
    #=GR AE007476.1 SS ------.<<<<<--------->>>>>.-->>>>>>>>---------------
    //

This is a single multiple sequence alignment, so you would probably load this
using the Bio.AlignIO.read() function:

    >>> from Bio import AlignIO
    >>> handle = open("Stockholm/simple.sth", "rU")
    >>> align = AlignIO.read(handle, "stockholm")
    >>> handle.close()
    >>> print align
    SingleLetterAlphabet() alignment with 2 rows and 104 columns
    UUAAUCGAGCUCAACACUCUUCGUAUAUCCUC-UCAAUAUGG-G...UGU AP001509.1
    AAAAUUGAAUAUCGUUUUACUUGUUUAU-GUCGUGAAU-UGG-C...GAU AE007476.1
    >>> for record in align :
    ...     print record.id, len(record)
    AP001509.1 104
    AE007476.1 104

This example file is clearly using RNA, so you might want the Alignment object
(and the SeqRecord objects it holds) to reflect this, rather than simple using
the default single letter alphabet as shown above.  You can do this with an
optional argument to the Bio.AlignIO.read() function:

    >>> from Bio import AlignIO
    >>> from Bio.Alphabet import generic_rna
    >>> handle = open("Stockholm/simple.sth", "rU")
    >>> align = AlignIO.read(handle, "stockholm", alphabet=generic_rna)
    >>> handle.close()
    >>> print align
    RNAAlphabet() alignment with 2 rows and 104 columns
    UUAAUCGAGCUCAACACUCUUCGUAUAUCCUC-UCAAUAUGG-G...UGU AP001509.1
    AAAAUUGAAUAUCGUUUUACUUGUUUAU-GUCGUGAAU-UGG-C...GAU AE007476.1

In addition to the sequences themselves, this example alignment also includes
some GR lines for the secondary structure of the sequences.  These are strings,
with one character for each letter in the associated sequence:

    >>> for record in align :
    ...     print record.id
    ...     print record.seq
    ...     print record.letter_annotations['secondary_structure']
    AP001509.1
    UUAAUCGAGCUCAACACUCUUCGUAUAUCCUC-UCAAUAUGG-GAUGAGGGUCUCUAC-AGGUA-CCGUAAA-UACCUAGCUACGAAAAGAAUGCAGUUAAUGU
    -----------------<<<<<<<<---..<<-<<-------->>->>..---------<<<<<--------->>>>>--->>>>>>>>---------------
    AE007476.1
    AAAAUUGAAUAUCGUUUUACUUGUUUAU-GUCGUGAAU-UGG-CACGA-CGUUUCUACAAGGUG-CCGG-AA-CACCUAACAAUAAGUAAGUCAGCAGUGAGAU
    -----------------<<<<<<<<-----<<.<<-------->>.>>----------.<<<<<--------->>>>>.-->>>>>>>>---------------

Any general annotation for each row is recorded in the SeqRecord's annotations
dictionary.  You can output this alignment in many different file formats using
Bio.AlignIO.write(), or the Alignment object's format method:

    >>> print align.format("fasta")
    >AP001509.1
    UUAAUCGAGCUCAACACUCUUCGUAUAUCCUC-UCAAUAUGG-GAUGAGGGUCUCUAC-A
    GGUA-CCGUAAA-UACCUAGCUACGAAAAGAAUGCAGUUAAUGU
    >AE007476.1
    AAAAUUGAAUAUCGUUUUACUUGUUUAU-GUCGUGAAU-UGG-CACGA-CGUUUCUACAA
    GGUG-CCGG-AA-CACCUAACAAUAAGUAAGUCAGCAGUGAGAU
    <BLANKLINE>

Most output formats won't be able to hold the annotation possible in a Stockholm file:

    >>> print align.format("stockholm")
    # STOCKHOLM 1.0
    #=GF SQ 2
    AP001509.1 UUAAUCGAGCUCAACACUCUUCGUAUAUCCUC-UCAAUAUGG-GAUGAGGGUCUCUAC-AGGUA-CCGUAAA-UACCUAGCUACGAAAAGAAUGCAGUUAAUGU
    #=GS AP001509.1 AC AP001509.1
    #=GS AP001509.1 DE AP001509.1
    #=GR AP001509.1 SS -----------------<<<<<<<<---..<<-<<-------->>->>..---------<<<<<--------->>>>>--->>>>>>>>---------------
    AE007476.1 AAAAUUGAAUAUCGUUUUACUUGUUUAU-GUCGUGAAU-UGG-CACGA-CGUUUCUACAAGGUG-CCGG-AA-CACCUAACAAUAAGUAAGUCAGCAGUGAGAU
    #=GS AE007476.1 AC AE007476.1
    #=GS AE007476.1 DE AE007476.1
    #=GR AE007476.1 SS -----------------<<<<<<<<-----<<.<<-------->>.>>----------.<<<<<--------->>>>>.-->>>>>>>>---------------
    //
    <BLANKLINE>

Note that when writing Stockholm files, Biopython does not break long sequences up and
interleave them (as in the input file shown above).  The standard allows this simpler
layout, and it is more likely to be understood by other tools. 
    
Finally, as an aside, it can sometimes be useful to use Bio.SeqIO.parse() to iterate over
the two rows as SeqRecord objects - rather than working with Alignnment objects.
Again, if you want to you can specify this is RNA:

    >>> from Bio import SeqIO
    >>> from Bio.Alphabet import generic_rna
    >>> handle = open("Stockholm/simple.sth", "rU")
    >>> for record in SeqIO.parse(handle, "stockholm", alphabet=generic_rna) :
    ...     print record.id
    ...     print record.seq
    ...     print record.letter_annotations['secondary_structure']
    AP001509.1
    UUAAUCGAGCUCAACACUCUUCGUAUAUCCUC-UCAAUAUGG-GAUGAGGGUCUCUAC-AGGUA-CCGUAAA-UACCUAGCUACGAAAAGAAUGCAGUUAAUGU
    -----------------<<<<<<<<---..<<-<<-------->>->>..---------<<<<<--------->>>>>--->>>>>>>>---------------
    AE007476.1
    AAAAUUGAAUAUCGUUUUACUUGUUUAU-GUCGUGAAU-UGG-CACGA-CGUUUCUACAAGGUG-CCGG-AA-CACCUAACAAUAAGUAAGUCAGCAGUGAGAU
    -----------------<<<<<<<<-----<<.<<-------->>.>>----------.<<<<<--------->>>>>.-->>>>>>>>---------------
    >>> handle.close()

iÿÿÿÿ(   t	   Alignment(   t   AlignmentIteratort   SequentialAlignmentWritert   StockholmWriterc           B   sr   e  Z d  Z i d d 6d d 6d d 6d d 6d	 d
 6d d 6d d 6Z i d d 6d d 6d d 6Z d „  Z d „  Z RS(   s    Stockholm/PFAM alignment writer.t   SSt   secondary_structuret   SAt   surface_accessibilityt   TMt   transmembranet   PPt   posterior_probabilityt   LIt   ligand_bindingt   ASt   active_sitet   INt   intront   OSt   organismt   OCt   organism_classificationt   LOt   lookc         C   s¿   | j  ƒ  } t | ƒ } | j ƒ  |  _ g  |  _ | d k rK t d ƒ ‚ n  |  j d k ri t d ƒ ‚ n  |  j j d ƒ |  j j d | ƒ x | D] } |  j | ƒ q” W|  j j d ƒ d S(   sû   Use this to write (another) single alignment to an open file.
        
        Note that sequences and their annotation are recorded
        together (rather than having a block of annotation followed
        by a block of aligned sequences).
        i    s   Must have at least one sequences    Non-empty sequences are requireds   # STOCKHOLM 1.0
s   #=GF SQ %i
s   //
N(	   t   get_all_seqst   lent   get_alignment_lengtht   _length_of_sequencest   _ids_writtent
   ValueErrort   handlet   writet   _write_record(   t   selft	   alignmentt   recordst   countt   record(    (    sŠ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/AlignIO/StockholmIO.pyt   write_alignment’   s    	c         C   sG  |  j  t | j ƒ k r' t d ƒ ‚ n  | j } | j d k	 rv d | j k rv | j | j d k rs | j } qs qv n  | j d d ƒ } d | j k rd | j k rd t	 | j d ƒ t	 | j d ƒ f } | t | ƒ | k rd | t	 | j d ƒ t	 | j d ƒ f } qn  | |  j
 k r<t d	 | ƒ ‚ n  |  j
 j | ƒ |  j j d
 | | j j ƒ  f ƒ d | j k r«|  j j d | |  j | j d ƒ f ƒ n2 | j rÝ|  j j d | |  j | j ƒ f ƒ n  | j r|  j j d | |  j | j ƒ f ƒ n  x4 | j D]) } |  j j d | |  j | ƒ f ƒ qWxk | j j ƒ  D]Z \ } } | |  j k rV|  j j d | |  j |  j | ƒ |  j t	 | ƒ ƒ f ƒ qVqVWxŒ | j j ƒ  D]{ \ } } | |  j k rÄt t	 | ƒ ƒ t | j ƒ k rÄ|  j j d | |  j |  j | ƒ |  j t	 | ƒ ƒ f ƒ qÄqÄWd S(   s$   Write a single SeqRecord to the files%   Sequences must all be the same lengtht	   accessiont    t   _t   startt   ends   /%s-%ss   %s/%s-%ss   Duplicate record identifier: %ss   %s %s
s   #=GS %s AC %s
s   #=GS %s DE %s
s   #=GS %s DR %s
s   #=GS %s %s %s
s   #=GR %s %s %s
N(   R   R   t   seqR   t   idt   namet   Nonet   annotationst   replacet   strR   t   appendR   R   t   tostringt   cleant   descriptiont   dbxrefst	   iteritemst   pfam_gs_mappingt   letter_annotationst   pfam_gr_mapping(   R!   R%   t   seq_namet   suffixt   xreft   keyt   value(    (    sŠ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/AlignIO/StockholmIO.pyR    ­   s\    	#!		0(   t   __name__t
   __module__t   __doc__R;   R9   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/AlignIO/StockholmIO.pyR      s   



	t   StockholmIteratorc           B   s„   e  Z d  Z i d d 6d d 6d d 6d d 6d	 d
 6d d 6d d 6Z i d d 6d d 6d d 6Z d „  Z d „  Z d „  Z d „  Z RS(   s   Loads a Stockholm file from PFAM into Alignment objects.

    The file may contain multiple concatenated alignments, which are loaded
    and returned incrementally.

    This parser will detect if the Stockholm file follows the PFAM
    conventions for sequence specific meta-data (lines starting #=GS
    and #=GR) and populates the SeqRecord fields accordingly.
    
    Any annotation which does not follow the PFAM conventions is currently
    ignored.

    If an accession is provided for an entry in the meta data, IT WILL NOT
    be used as the record.id (it will be recorded in the record's
    annotations).  This is because some files have (sub) sequences from
    different parts of the same accession (differentiated by different
    start-end positions).

    Wrap-around alignments are not supported - each sequences must be on
    a single line.  However, interlaced sequences should work.

    For more information on the file format, please see:
    http://www.bioperl.org/wiki/Stockholm_multiple_alignment_format
    http://www.cgb.ki.se/cgb/groups/sonnhammer/Stockholm.html

    For consistency with BioPerl and EMBOSS we call this the "stockholm"
    format.
    R   R   R   R   R	   R   R   R
   R   R   R   R   R   R   R   R   R   R   R   R   c         C   s   y |  j  } |  `  Wn  t k
 r5 |  j j ƒ  } n X| s@ d  S| j ƒ  d k sa t d ƒ ‚ n  i  } g  } i  } i  } i  } t } xÎ|  j j ƒ  } | s¡ Pn  | j ƒ  } | d k rÆ | |  _  Pqˆ | d k rÛ t } qˆ | d k rê qˆ | d d k r²| st ‚ g  | j	 d d ƒ D] } | j ƒ  ^ q}	 t
 |	 ƒ d	 k r[t d
 d | ƒ ‚ n  |	 \ }
 } |
 | k rƒ| j |
 ƒ n  | j |
 d ƒ | |
 c | j d d ƒ 7<qˆ t
 | ƒ d k rˆ | d  d k r&| d j ƒ  j	 d  d ƒ \ } } | | k r| g | | <qO| | j | ƒ qR| d  d k r9qR| d  d k rÃ| d j ƒ  j	 d  d	 ƒ \ }
 } } |
 | k r‡i  | |
 <n  | | |
 k r«| g | |
 | <qO| |
 | j | ƒ qR| d  d k rR| d j ƒ  j	 d  d	 ƒ \ }
 } } |
 | k ri  | |
 <n  | | |
 k r2d | |
 | <n  | |
 | c | j ƒ  7<qRqˆ qˆ Wt
 | ƒ t
 | ƒ k stt ‚ | |  _ | |  _ | |  _ | |  _ | rø| rø|  j d  k	 rê|  j t
 | ƒ k rêt d t
 | ƒ |  j f ƒ ‚ n  t |  j ƒ } | | _ t
 | j ƒ  d ƒ } xÙ | D]Ñ }
 | |
 } | t
 | ƒ k rPt d ƒ ‚ n  |  j |
 ƒ \ } } } | j |
 | d | d | ƒ| j ƒ  d } | j |
 k s¸| j |
 k s¸t ‚ |
 | _ | | _ |
 | _ | | j d <|  j |
 | ƒ qW| Sd  Sd  S(   Ns   # STOCKHOLM 1.0s   Did not find STOCKHOLM headers   //t    i    t   #R(   i   i   s%   Could not split line into identifier s   and sequence:
t   .t   -i   s   #=GF s   #=GC s   #=GS s   #=GR s5   Found %i records in this alignment, told to expect %is8   Sequences have different lengths, or repeated identifierR*   R+   iÿÿÿÿR'   (    t   _headert   AttributeErrorR   t   readlinet   stripR   t   Falset   Truet   AssertionErrort   splitR   R3   t
   setdefaultR1   R/   t   idst	   sequencest   seq_annotationt   seq_col_annotationt   records_per_alignmentR    t   alphabett   _annotationst   valuest   _identifier_splitt   add_sequenceR   R-   R6   R.   R0   t   _populate_meta_data(   R!   t   linet   seqsRR   t   gst   grt   gft   passed_end_alignmentt   xt   partsR-   R,   t   featuret   textR"   t   alignment_lengthR.   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/AlignIO/StockholmIO.pyt   next+  s°    	
	 		+"%%$					
$			c         C   s‘   | j  d ƒ d k r„ | j d d ƒ d } | j d ƒ d k r„ t t | j d ƒ ƒ \ } } | j d d ƒ d } | | | f Sn  | d d f S(   s7   Returns (name,start,end) string tuple from an identier.t   /iÿÿÿÿi   RH   i    N(   t   findRP   R$   t   mapt   intR/   (   R!   t
   identifiert	   start_endR*   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/AlignIO/StockholmIO.pyRZ   ½  s    c   
      C   s–   |  j  | ƒ \ } } } | | k r0 | g } n | | g } i  } xM | D]E } y+ x$ | | D] }	 | | |	 | |	 <q] WWqI t k
 r qI XqI W| S(   s¥  Takes an itentifier and returns dict of all meta-data matching it.

        For example, given "Q9PN73_CAMJE/149-220" will return all matches to
        this or "Q9PN73_CAMJE" which the identifier without its /start-end
        suffix.

        In the example below, the suffix is required to match the AC, but must
        be removed to match the OS and OC meta-data.

        # STOCKHOLM 1.0
        #=GS Q9PN73_CAMJE/149-220  AC Q9PN73
        ...
        Q9PN73_CAMJE/149-220               NKA...
        ...
        #=GS Q9PN73_CAMJE OS Campylobacter jejuni
        #=GS Q9PN73_CAMJE OC Bacteria 

        This function will return an empty dictionary if no data is found.(   RZ   t   KeyError(
   R!   Rm   t	   meta_dictR.   R*   R+   t   identifier_keyst   answert   identifier_keyt   feature_key(    (    sŠ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/AlignIO/StockholmIO.pyt   _get_meta_dataÇ  s    c         C   sa  |  j  | |  j ƒ } xà | D]Ø } | d k rb t | | ƒ d k sJ t ‚ | | d | j d <q | d k r‡ d j | | ƒ | _ q | d k r£ | | | _ q | |  j k rÖ d j | | ƒ | j |  j | <q d j | | ƒ | j d	 | <q W|  j  | |  j	 ƒ } xM | D]E } | |  j
 k rD| | | j |  j
 | <q| | | j d
 | <qWd S(   sl   Adds meta-date to a SecRecord's annotations dictionary.

        This function applies the PFAM conventions.t   ACi   i    R'   t   DEs   
t   DRs   , s   GS:s   GR:N(   Ru   RT   R   RO   R0   t   joinR6   R7   R9   RU   R;   R:   (   R!   Rm   R%   t   seq_dataRe   t   seq_col_data(    (    sŠ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/AlignIO/StockholmIO.pyR\   è  s"    $"(	   RA   RB   RC   R;   R9   Rh   RZ   Ru   R\   (    (    (    sŠ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/AlignIO/StockholmIO.pyRD   ÿ   s   



	’	
	!c          C   s³   d d l  }  d d l } | j j | j j d d d ƒ ƒ r¯ d GH| j j | j ƒ } | j | j j d d d ƒ ƒ | j j d ƒ s t	 ‚ |  j
 ƒ  | j | ƒ ~ d GHn  d S(   sÁ   Run the Bio.SeqIO module's doctests.

    This will try and locate the unit tests directory, and run the doctests
    from there in order that the relative paths used in the examples work.
    iÿÿÿÿNs   ..t   Testss   Runing doctests...s   Stockholm/simple.stht   Done(   t   doctestt   ost   patht   isdirRy   t   abspatht   curdirt   chdirt   isfileRO   t   testmod(   R~   R   t   cur_dir(    (    sŠ   /oak/stanford/groups/akundaje/marinovg/programs/biopython-1.50.tar.gz/biopython-1.50/build/lib.linux-x86_64-2.7/Bio/AlignIO/StockholmIO.pyt   _test  s    $
t   __main__N(
   RC   t   Bio.Align.GenericR    t
   InterfacesR   R   R   RD   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/AlignIO/StockholmIO.pyt   <module>}   s   ~ÿ 
	