
:>"^{                 @   s   d  Z  d d l m Z d d l Z d d l Z d d l m Z d d l m Z d d l	 m
 Z
 d d l m Z d Z d	 Z Gd
 d   d e  Z Gd d   d e  Z d d d d  Z d S)zExtract information from alignment objects.

In order to try and avoid huge alignment objects with tons of functions,
functions which return summary type information about alignments should
be put into classes in this module.
    )print_functionN)Alphabet)IUPAC)Seq)	FreqTableg?g      ?c               @   s	  e  Z d  Z d Z d d   Z d d d d d d	  Z d d d d d
 d  Z d d   Z d d d  Z d d   Z	 d d   Z
 d d d  Z d d d d  Z d d   Z d d   Z d d d d d d d d  Z d d d d d   Z d! d"   Z d# d$   Z d S)%SummaryInfozCalculate summary info about the alignment.

    This class should be used to caclculate information summarizing the
    results of an alignment. This may either be straight consensus info
    or more complicated things.
    c             C   s   | |  _  g  |  _ d S)zInitialize with the alignment to calculate information on.

        ic_vector attribute. A list of ic content for each column number.
        N)	alignment	ic_vector)selfr    r   8/tmp/pip-build-ww9dw3qa/biopython/Bio/Align/AlignInfo.py__init__'   s    	zSummaryInfo.__init__gffffff?XNr   c             C   s  d } |  j  j   } x}t |  D]o} i  } d }	 x |  j  D] }
 | t |
 j  k  r> |
 j | d k r> |
 j | d k r> |
 j | | k r d | |
 j | <n | |
 j | d 7<|	 d }	 q> Wg  } d } xQ | D]I } | | | k r| g } | | } q | | | k r | j |  q W| rH|	 d k rH| | 7} q" t |  d k rt |  t |	  | k r| | d 7} q" | | 7} q" W| d k r|  j |  } t | |  S)a  Output a fast consensus sequence of the alignment.

        This doesn't do anything fancy at all. It will just go through the
        sequence residue by residue and count up the number of each type
        of residue (ie. A or G or T or C for DNA) in all sequences in the
        alignment. If the percentage of the most common residue type is
        greater then the passed threshold, then we will add that residue type,
        otherwise an ambiguous character will be added.

        This could be made a lot fancier (ie. to take a substitution matrix
        into account), but it just meant for a quick and dirty consensus.

        Arguments:
         - threshold - The threshold value that is required to add a particular
           atom.
         - ambiguous - The ambiguous character to be added when the threshold is
           not reached.
         - consensus_alpha - The alphabet to return for the consensus sequence.
           If this is None, then we will try to guess the alphabet.
         - require_multiple - If set as 1, this will require that more than
           1 sequence be part of an alignment to put it in the consensus (ie.
           not just 1 sequence and gaps).

         r   -.   N)	r   get_alignment_lengthrangelenseqappendfloat_guess_consensus_alphabetr   )r
   	threshold	ambiguousconsensus_alpharequire_multiple	consensuscon_lenn	atom_dict	num_atomsrecord	max_atomsmax_sizeatomr   r   r   dumb_consensus/   s:    &	zSummaryInfo.dumb_consensusc             C   s  d } |  j  j   } xWt |  D]I} i  } d }	 xl |  j  D]a }
 | t |
 j  k  r> |
 j | | k r d | |
 j | <n | |
 j | d 7<|	 d 7}	 q> Wg  } d } xQ | D]I } | | | k r | g } | | } q | | | k r | j |  q W| r"|	 d k r"| | 7} q" t |  d k rat |  t |	  | k ra| | d 7} q" | | 7} q" W| d k r|  j |  } t | |  S)ah  Output a fast consensus sequence of the alignment, allowing gaps.

        Same as dumb_consensus(), but allows gap on the output.

        Things to do:
         - Let the user define that with only one gap, the result
           character in consensus is gap.
         - Let the user select gap character, now
           it takes the same as input.

        r   r   r   N)	r   r   r   r   r   r   r   r   r   )r
   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r   r   r   gap_consensusy   s8    	zSummaryInfo.gap_consensusc             C   s\  t  j |  j j  } xD |  j D]9 } t  j | j j  } t | | j  s t d   q Wt	 | d  rX| j
 d k	 rX| | j
 k rXt | t j  r | t j   j
 k r t j   } qXt  j } n t | t j  r| t j   j
 k rt j   } qXt  j } nK t | t j  rO| t j   j
 k rCt j   } qXt  j } n	 t  j } | S)a   Pick an (ungapped) alphabet for an alignment consesus sequence (PRIVATE).

        This just looks at the sequences we have, checks their type, and
        returns as appropriate type which seems to make sense with the
        sequences we've got.
        z\Alignment contains a sequence with                                 an incompatible alphabet.lettersN)r   _get_base_alphabetr   	_alphabetr   Zalphabet
isinstance	__class__
ValueErrorhasattrr)   r   ZIUPACUnambiguousDNAZgeneric_dnaZIUPACUnambiguousRNAZgeneric_rnaZIUPACProteinZExtendedIUPACProteinZgeneric_proteinZsingle_letter_alphabet)r
   r   ar#   Zaltr   r   r   r      s,    	z%SummaryInfo._guess_consensus_alphabetc          	   C   s   |  j  |  \ } } x t t |  j   D] } x t | d t |  j   D]d } |  j |  j | j |  j | j |  j | j j d d  |  j | j j d d  | |  } qN Wq+ W| S)a  Generate a replacement dictionary to plug into a substitution matrix.

        This should look at an alignment, and be able to generate the number
        of substitutions of different residues for each other in the
        aligned object.

        Will then return a dictionary with this information::

            {('A', 'C') : 10, ('C', 'A') : 12, ('G', 'C') : 15 ....}

        This also treats weighted sequences. The following example shows how
        we calculate the replacement dictionary. Given the following
        multiple sequence alignment::

            GTATC  0.5
            AT--C  0.8
            CTGTC  1.0

        For the first column we have::

            ('A', 'G') : 0.5 * 0.8 = 0.4
            ('C', 'G') : 0.5 * 1.0 = 0.5
            ('A', 'C') : 0.8 * 1.0 = 0.8

        We then continue this for all of the columns in the alignment, summing
        the information for each substitution in each column, until we end
        up with the replacement dictionary.

        Arguments:
         - skip_chars - A list of characters to skip when creating the dictionary.
           This defaults to an empty list.

        For instance, you might have Xs (screened stuff) or Ns, and not want
        to include the ambiguity characters in the dictionary.
        r   weightg      ?)_get_base_replacementsr   r   r   _pair_replacementr   annotationsget)r
   Z
skip_charsZrep_dict
skip_itemsZrec_num1Zrec_num2r   r   r   replacement_dictionary   s    %#z"SummaryInfo.replacement_dictionaryc       
      C   s   x t  t |   D] } | | } y | | }	 Wn t k
 rI | SYn X| | k r |	 | k r y | | |	 f | | 7<Wq t k
 r t d | |	 |  j j f   Yq Xq W| S)aX  Compare two sequences and generate info on the replacements seen (PRIVATE).

        Arguments:
         - seq1, seq2 - The two sequences to compare.
         - weight1, weight2 - The relative weights of seq1 and seq2.
         - start_dict - The dictionary containing the starting replacement
           info that we will modify.
         - ignore_chars - A list of characters to ignore when calculating
           replacements (ie. '-').

        Returns:
         - A replacment dictionary which is modified from initial_dict with
           the information from the sequence comparison.

        z(Residues %s, %s not found in alphabet %s)r   r   
IndexErrorKeyErrorr.   r   r+   )
r
   Zseq1Zseq2Zweight1Zweight2Z
start_dictZignore_charsresidue_numZresidue1Zresidue2r   r   r   r3     s    
	zSummaryInfo._pair_replacementc             C   s   |  j  j j } | d k sH t |  j  j t j  r | |  j  j j k r t   } x# |  j  D] } | j | j	  } q[ Wt
 |  } d j |  } | S)zKReturn a string containing the expected letters in the alignment (PRIVATE).Nr   )r   r+   r)   r,   r   Gappedgap_charsetunionr   sortedjoin)r
   all_lettersZset_lettersr#   Zlist_lettersr   r   r   _get_all_lettersB  s    	zSummaryInfo._get_all_lettersc             C   s   | d k r g  } i  } |  j    } t |  j j t j  rm | j |  j j j  | j |  j j j d  } xG | D]? } x6 | D]. } | | k r | | k r d | | | f <q Wqt W| | f S)a  Get a zeroed dictionary of all possible letter combinations (PRIVATE).

        This looks at the type of alphabet and gets the letters for it.
        It then creates a dictionary with all possible combinations of these
        letters as keys (ie. ('A', 'G')) and sets the values as zero.

        Returns:
         - The base dictionary created
         - A list of alphabet items to skip when filling the dictionary.
           (Right now the only thing I can imagine in this list is gap
           characters, but maybe X's or something else might be useful later.
           This will also include any characters that are specified to be
           skipped.) Defaults to an empty list.

        Nr   r   )	rB   r,   r   r+   r   r;   r   r<   replace)r
   r6   Zbase_dictionaryrA   Zfirst_letterZsecond_letterr   r   r   r2   T  s    z"SummaryInfo._get_base_replacementsc             C   s  |  j    } | s t  | d k r* g  } t | t  sE t d   t |  j j t j  rs | j	 |  j j j
  x  | D] } | j | d  } qz W| r | } t |  |  j j   k s t  n |  j   } g  } x t t |   D] } |  j |  } x |  j D] }	 y |	 j | }
 Wn t k
 r;d }
 Yn X|
 r
|
 | k r
|	 j j d d  } y | |
 | 7<Wq
t k
 rt d |
 |  j j f   Yq
Xq
W| j	 | | | f  q Wt |  S)a  Create a position specific score matrix object for the alignment.

        This creates a position specific score matrix (pssm) which is an
        alternative method to look at a consensus sequence.

        Arguments:
         - chars_to_ignore - A list of all characters not to include in
           the pssm.  If the alignment alphabet declares a gap character,
           then it will be excluded automatically.
         - axis_seq - An optional argument specifying the sequence to
           put on the axis of the PSSM. This should be a Seq object. If nothing
           is specified, the consensus sequence, calculated with default
           parameters, will be used.

        Returns:
         - A PSSM (position specific score matrix) object.

        Nz!chars_to_ignore should be a list.r   r1   g      ?z#Residue %s not found in alphabet %s)rB   AssertionErrorr,   list	TypeErrorr   r+   r   r;   r   r<   rC   r   r   r'   r   _get_base_lettersr   r8   r4   r5   r9   r.   PSSM)r
   Zaxis_seqchars_to_ignorerA   charZleft_seqZ	pssm_infor:   Z
score_dictr#   Zthis_residuer1   r   r   r   pos_specific_score_matrixy  sB    $z%SummaryInfo.pos_specific_score_matrixc             C   s%   i  } x | D] } d | | <q W| S)zGCreate a zeroed dictionary with all of the specified letters (PRIVATE).r   r   )r
   r)   Z	base_infoletterr   r   r   rG     s    zSummaryInfo._get_base_lettersc             C   s2   y |  j  j j } Wn t k
 r- d } Yn X| S)z9Return the gap character used in the alignment (PRIVATE).r   )r   r+   r<   AttributeError)r
   r<   r   r   r   _get_gap_char  s
    zSummaryInfo._get_gap_char   c          	   C   s  | d k r" t  |  j d j  } | d k r4 g  } | d k  s\ | t  |  j d j  k r t d | | d t  |  j d j  f   d } | st j |  j j  } t | t j  r t	 } n7 t | t j
  r t } n d }	 |	 d 7}	 t |	   ~ n t | t j  st d   |  j   }
 x  | D] } |
 j | d  }
 q2Wi  } x] t | |  D]L } |  j | |  j |
 | | | |  } |  j | | | |  } | | | <qdWt | j    } g  |  _ x2 t |  D]$ \ } } |  j j | | |  qW| S)a#  Calculate the information content for each residue along an alignment.

        Arguments:
         - start, end - The starting an ending points to calculate the
           information content. These points should be relative to the first
           sequence in the alignment, starting at zero (ie. even if the 'real'
           first position in the seq is 203 in the initial sequence, for
           the info content, we need to use zero). This defaults to the entire
           length of the first sequence.
         - e_freq_table - A FreqTable object specifying the expected frequencies
           for each letter in the alphabet we are using (e.g. {'G' : 0.4,
           'C' : 0.4, 'T' : 0.1, 'A' : 0.1}). Gap characters should not be
           included, since these should not have expected frequencies.
         - log_base - The base of the logathrim to use in calculating the
           information content. This defaults to 2 so the info is in bits.
         - chars_to_ignore - A listing of characters which should be ignored
           in calculating the info content. Defaults to none.

        Returns:
         - A number representing the info content for the specified region.

        Please see the Biopython manual for more information on how information
        content is calculated.

        Nr   zIStart (%s) and end (%s) are not in the                     range %s to %sz.Error in alphabet: not Nucleotide or Protein, zsupply expected frequenciesz)e_freq_table should be a FreqTable objectr   )r   r   r   r.   r   r*   r+   r,   ZProteinAlphabetProtein20RandomZNucleotideAlphabetNucleotide4Randomr   rB   rC   r   _get_letter_freqs_get_column_info_contentsumvaluesr	   	enumerater   )r
   startende_freq_tablelog_baserI   pseudo_countrandom_expectedZ
base_alphaZerrstrrA   rJ   Zinfo_contentr:   Z	freq_dictZcolumn_score
total_infoikr   r   r   information_content  sV    (&		
					zSummaryInfo.information_contentc             C   s  |  j  |  } d }	 |  j   }
 | d k  r= t d |   x | D] } yK | j | | k r | j j d d  } | | j | | 7<|	 | 7}	 WqD t k
 r t d | j | |  j j f   YqD XqD W| rGt	 | t
 j
  s t d   xO | D]G } | |
 k r | | k r t d t |  |
 g t |  f   q W|	 d k r}x | D] } | | d k sZt  qZWn{ xx | D]p } | r| s| r| r| | } n | } | | | | } |	 | } | | | | <q| | |	 | | <qW| S)ar  Determine the frequency of specific letters in the alignment (PRIVATE).

        Arguments:
         - residue_num - The number of the column we are getting frequencies
           from.
         - all_records - All of the SeqRecords in the alignment.
         - letters - The letters we are interested in getting the frequency
           for.
         - to_ignore - Letters we are specifically supposed to ignore.
         - pseudo_count - Optional argument specifying the Pseudo count (k)
           to add in order to prevent a frequency of 0 for a letter.
         - e_freq_table - An optional argument specifying the expected
           frequencies for each letter. This is a SubsMat.FreqTable instance.
         - random_expected - Optional argument that specify the frequency to use
           when e_freq_table is not defined.

        This will calculate the frequencies of each of the specified letters
        in the alignment at the given frequency, and return this as a
        dictionary where the keys are the letters and the values are the
        frequencies. Pseudo count can be added to prevent a null frequency
        r   z5Positive value required for pseudo_count, %s providedr1   g      ?z#Residue %s not found in alphabet %sz)e_freq_table should be a FreqTable objectzCletters in current column %s and not in expected frequency table %s)rG   rN   r.   r   r4   r5   r9   r   r+   r,   r   rE   rD   )r
   r:   Zall_recordsr)   Z	to_ignorer[   rY   r\   Z	freq_infoZtotal_countr<   r#   r1   keyrL   Z
ajust_freqZajusted_letter_countZajusted_totalr   r   r   rR   &  sL    


zSummaryInfo._get_letter_freqsc             C   s  |  j    } | r t | t j  s0 t d   xO | D]G } | | k r7 | | k r7 t d t |  t |  | g f   q7 Wd } x | D]{ } d }	 | | k r | r | | | | }	 n | | | }	 |	 d k r | | t j |	  t j |  }
 | |
 7} q W| S)a  Calculate the information content for a column (PRIVATE).

        Arguments:
         - obs_freq - The frequencies observed for each letter in the column.
         - e_freq_table - An optional argument specifying the expected
           frequencies for each letter. This is a SubsMat.FreqTable instance.
         - log_base - The base of the logathrim to use in calculating the
           info content.

        z)e_freq_table should be a FreqTable objectz6Expected frequency letters %s do not match observed %sg        r   )rN   r,   r   r.   rE   mathlog)r
   Zobs_freqrY   rZ   r\   r<   ra   r]   rL   Z	inner_logZletter_infor   r   r   rS   y  s(    	$z$SummaryInfo._get_column_info_contentc             C   s   |  j  d d  | f S)zReturn column of alignment.N)r   )r
   colr   r   r   
get_column  s    zSummaryInfo.get_column)__name__
__module____qualname____doc__r   r'   r(   r   r7   r3   rB   r2   rK   rG   rN   r`   rR   rS   re   r   r   r   r   r      s,   I<*7+%DSR-r   c               @   sF   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 S)rH   a  Represent a position specific score matrix.

    This class is meant to make it easy to access the info within a PSSM
    and also make it easy to print out the information in a nice table.

    Let's say you had an alignment like this::

        GTATC
        AT--C
        CTGTC

    The position specific score matrix (when printed) looks like::

          G A T C
        G 1 1 0 1
        T 0 0 3 0
        A 1 1 0 0
        T 0 0 2 0
        C 0 0 0 3

    You can access a single element of the PSSM using the following::

        your_pssm[sequence_number][residue_count_name]

    For instance, to get the 'T' residue for the second element in the
    above alignment you would need to do:

    your_pssm[1]['T']
    c             C   s   | |  _  d S)aW  Initialize with pssm data to represent.

        The pssm passed should be a list with the following structure:

        list[0] - The letter of the residue being represented (for instance,
        from the example above, the first few list[0]s would be GTAT...
        list[1] - A dictionary with the letter substitutions and counts.
        N)pssm)r
   rj   r   r   r   r     s    	zPSSM.__init__c             C   s   |  j  | d S)Nr   )rj   )r
   posr   r   r   __getitem__  s    zPSSM.__getitem__c             C   s   d } t  |  j d d  } x | D] } | d | 7} q$ W| d 7} xT |  j D]I } | d | d 7} x$ | D] } | d | d | 7} qo W| d 7} qP W| S)N r   r   z   %s
z%s z %.1f)r?   rj   )r
   outZall_residuesresitemr   r   r   __str__  s    
zPSSM.__str__c             C   s   |  j  | d S)z4Return the residue letter at the specified position.r   )rj   )r
   rk   r   r   r   get_residue  s    zPSSM.get_residueN)rf   rg   rh   ri   r   rl   rr   rs   r   r   r   r   rH     s
   rH   c             C   st   | p t  j } |  j s" |  j   |  j | j } x; t |  j  D]* \ } } | j d | | | | f  qB Wd S)zJ3 column output: position, aa in representative sequence, ic_vector value.z%d %s %.3f
N)sysstdoutr	   r`   r   r   rV   write)Zsummary_infoZfoutZ
rep_recordZrep_sequencerk   Zicr   r   r   print_info_content  s    	
rw   )ri   
__future__r   rb   rt   ZBior   ZBio.Alphabetr   ZBio.Seqr   ZBio.SubsMatr   rP   rQ   objectr   rH   rw   r   r   r   r   <module>   s     D