#NEWLINE# @ experimental ( as_of = '0.4.0' ) #NEWLINE# def global_pairwise_align( seq1 , seq2 , gap_open_penalty , gap_extend_penalty , substitution_matrix , penalize_terminal_gaps = False ) : #NEWLINE# #INDENT# 'Globally align a pair of seqs or alignments with Needleman-Wunsch\n\n    Parameters\n    ----------\n    seq1 : GrammaredSequence or TabularMSA\n        The first unaligned sequence(s).\n    seq2 : GrammaredSequence or TabularMSA\n        The second unaligned sequence(s).\n    gap_open_penalty : int or float\n        Penalty for opening a gap (this is substracted from previous best\n        alignment score, so is typically positive).\n    gap_extend_penalty : int or float\n        Penalty for extending a gap (this is substracted from previous best\n        alignment score, so is typically positive).\n    substitution_matrix: 2D dict (or similar)\n        Lookup for substitution scores (these values are added to the\n        previous best alignment score).\n    penalize_terminal_gaps: bool, optional\n        If True, will continue to penalize gaps even after one sequence has\n        been aligned through its end. This behavior is true Needleman-Wunsch\n        alignment, but results in (biologically irrelevant) artifacts when\n        the sequences being aligned are of different length. This is ``False``\n        by default, which is very likely to be the behavior you want in all or\n        nearly all cases.\n\n    Returns\n    -------\n    tuple\n        ``TabularMSA`` object containing the aligned sequences, alignment score\n        (float), and start/end positions of each input sequence (iterable\n        of two-item tuples). Note that start/end positions are indexes into the\n        unaligned sequences.\n\n    See Also\n    --------\n    local_pairwise_align\n    local_pairwise_align_protein\n    local_pairwise_align_nucleotide\n    skbio.alignment.local_pairwise_align_ssw\n    global_pairwise_align_protein\n    global_pairwise_align_nucelotide\n\n    Notes\n    -----\n    This algorithm (in a slightly more basic form) was originally described\n    in [1]_. The scikit-bio implementation was validated against the\n    EMBOSS needle web server [2]_.\n\n    This function can be use to align either a pair of sequences, a pair of\n    alignments, or a sequence and an alignment.\n\n    References\n    ----------\n    .. [1] A general method applicable to the search for similarities in\n       the amino acid sequence of two proteins.\n       Needleman SB, Wunsch CD.\n       J Mol Biol. 1970 Mar;48(3):443-53.\n    .. [2] http://www.ebi.ac.uk/Tools/psa/emboss_needle/\n\n    ' #NEWLINE# warn ( "You're using skbio's python implementation of Needleman-Wunsch alignment. This is known to be very slow (e.g., thousands of times slower than a native C implementation). We'll be adding a faster version soon (see https://github.com/biocore/scikit-bio/issues/254 to track progress on this)." , EfficiencyWarning ) #NEWLINE# for seq in ( seq1 , seq2 ) : #NEWLINE# #INDENT# if ( not isinstance ( seq , ( GrammaredSequence , TabularMSA ) ) ) : #NEWLINE# #INDENT# raise TypeError ( ( '`seq1` and `seq2` must be GrammaredSequence subclasses or TabularMSA, not type %r' % type ( seq ) . __name__ ) ) #NEWLINE# #UNINDENT# #UNINDENT# seq1 = _coerce_alignment_input_type ( seq1 ) #NEWLINE# seq2 = _coerce_alignment_input_type ( seq2 ) #NEWLINE# if ( seq1 . dtype is not seq2 . dtype ) : #NEWLINE# #INDENT# raise TypeError ( ( '`seq1` and `seq2` must have the same dtype: %r != %r' % ( seq1 . dtype . __name__ , seq2 . dtype . __name__ ) ) ) #NEWLINE# #UNINDENT# if penalize_terminal_gaps : #NEWLINE# #INDENT# init_matrices_f = _init_matrices_nw #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# init_matrices_f = _init_matrices_nw_no_terminal_gap_penalty #NEWLINE# #UNINDENT# ( score_matrix , traceback_matrix ) = _compute_score_and_traceback_matrices ( seq1 , seq2 , gap_open_penalty , gap_extend_penalty , substitution_matrix , new_alignment_score = ( - np . inf ) , init_matrices_f = init_matrices_f , penalize_terminal_gaps = penalize_terminal_gaps ) #NEWLINE# end_row_position = ( traceback_matrix . shape [ 0 ] - 1 ) #NEWLINE# end_col_position = ( traceback_matrix . shape [ 1 ] - 1 ) #NEWLINE# ( aligned1 , aligned2 , score , seq1_start_position , seq2_start_position ) = _traceback ( traceback_matrix , score_matrix , seq1 , seq2 , end_row_position , end_col_position ) #NEWLINE# start_end_positions = [ ( seq1_start_position , ( end_col_position - 1 ) ) , ( seq2_start_position , ( end_row_position - 1 ) ) ] #NEWLINE# msa = TabularMSA ( ( aligned1 + gap_extend_penalty ) ) #NEWLINE# return ( msa , score , start_end_positions )